OSDN Git Service

Import translations. DO NOT MERGE
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ui / UndoBarView.java
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.gallery3d.ui;
18
19 import android.content.Context;
20 import android.view.MotionEvent;
21
22 import com.android.gallery3d.R;
23 import com.android.gallery3d.util.GalleryUtils;
24
25 public class UndoBarView extends GLView {
26     private static final String TAG = "UndoBarView";
27
28     private static final int WHITE = 0xFFFFFFFF;
29     private static final int GRAY = 0xFFAAAAAA;
30
31     private final NinePatchTexture mPanel;
32     private final StringTexture mUndoText;
33     private final StringTexture mDeletedText;
34     private final ResourceTexture mUndoIcon;
35     private final int mBarHeight;
36     private final int mBarMargin;
37     private final int mUndoTextMargin;
38     private final int mIconSize;
39     private final int mIconMargin;
40     private final int mSeparatorTopMargin;
41     private final int mSeparatorBottomMargin;
42     private final int mSeparatorRightMargin;
43     private final int mSeparatorWidth;
44     private final int mDeletedTextMargin;
45     private final int mClickRegion;
46
47     private OnClickListener mOnClickListener;
48     private boolean mDownOnButton;
49
50     // This is the layout of UndoBarView. The unit is dp.
51     //
52     //    +-+----+----------------+-+--+----+-+------+--+-+
53     // 48 | |    | Deleted        | |  | <- | | UNDO |  | |
54     //    +-+----+----------------+-+--+----+-+------+--+-+
55     //     4  16                   1 12  32  8        16 4
56     public UndoBarView(Context context) {
57         mBarHeight = (int) GalleryUtils.dpToPixel(48);
58         mBarMargin = (int) GalleryUtils.dpToPixel(4);
59         mUndoTextMargin = (int) GalleryUtils.dpToPixel(16);
60         mIconMargin = (int) GalleryUtils.dpToPixel(8);
61         mIconSize = (int) GalleryUtils.dpToPixel(32);
62         mSeparatorRightMargin = (int) GalleryUtils.dpToPixel(12);
63         mSeparatorTopMargin = (int) GalleryUtils.dpToPixel(10);
64         mSeparatorBottomMargin = (int) GalleryUtils.dpToPixel(10);
65         mSeparatorWidth = (int) GalleryUtils.dpToPixel(1);
66         mDeletedTextMargin = (int) GalleryUtils.dpToPixel(16);
67
68         mPanel = new NinePatchTexture(context, R.drawable.panel_undo_holo);
69         mUndoText = StringTexture.newInstance(context.getString(R.string.undo),
70                 GalleryUtils.dpToPixel(12), GRAY, 0, true);
71         mDeletedText = StringTexture.newInstance(
72                 context.getString(R.string.deleted),
73                 GalleryUtils.dpToPixel(16), WHITE);
74         mUndoIcon = new ResourceTexture(
75                 context, R.drawable.ic_menu_revert_holo_dark);
76         mClickRegion = mBarMargin + mUndoTextMargin + mUndoText.getWidth()
77                 + mIconMargin + mIconSize + mSeparatorRightMargin;
78     }
79
80     public void setOnClickListener(OnClickListener listener) {
81         mOnClickListener = listener;
82     }
83
84     @Override
85     protected void onMeasure(int widthSpec, int heightSpec) {
86         setMeasuredSize(0 /* unused */, mBarHeight);
87     }
88
89     @Override
90     protected void render(GLCanvas canvas) {
91         super.render(canvas);
92         int w = getWidth();
93         int h = getHeight();
94         mPanel.draw(canvas, mBarMargin, 0, w - mBarMargin * 2, mBarHeight);
95
96         int x = w - mBarMargin;
97         int y;
98
99         x -= mUndoTextMargin + mUndoText.getWidth();
100         y = (mBarHeight - mUndoText.getHeight()) / 2;
101         mUndoText.draw(canvas, x, y);
102
103         x -= mIconMargin + mIconSize;
104         y = (mBarHeight - mIconSize) / 2;
105         mUndoIcon.draw(canvas, x, y, mIconSize, mIconSize);
106
107         x -= mSeparatorRightMargin + mSeparatorWidth;
108         y = mSeparatorTopMargin;
109         canvas.fillRect(x, y, mSeparatorWidth,
110                 mBarHeight - mSeparatorTopMargin - mSeparatorBottomMargin, GRAY);
111
112         x = mBarMargin + mDeletedTextMargin;
113         y = (mBarHeight - mDeletedText.getHeight()) / 2;
114         mDeletedText.draw(canvas, x, y);
115     }
116
117     @Override
118     protected boolean onTouch(MotionEvent event) {
119         switch (event.getAction()) {
120             case MotionEvent.ACTION_DOWN:
121                 mDownOnButton = inUndoButton(event);
122                 break;
123             case MotionEvent.ACTION_UP:
124                 if (mDownOnButton) {
125                     if (mOnClickListener != null && inUndoButton(event)) {
126                         mOnClickListener.onClick(this);
127                     }
128                     mDownOnButton = false;
129                 }
130                 break;
131             case MotionEvent.ACTION_CANCEL:
132                 mDownOnButton = false;
133                 break;
134         }
135         return true;
136     }
137
138     // Check if the event is on the right of the separator
139     private boolean inUndoButton(MotionEvent event) {
140         float x = event.getX();
141         float y = event.getY();
142         int w = getWidth();
143         int h = getHeight();
144         return (x >= w - mClickRegion && x < w && y >= 0 && y < h);
145     }
146 }