summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/state/StateView.java
blob: 9353a430a6a8e593003733defcefe1f748188e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.state;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.widget.LinearLayout;
import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.FilterShowActivity;
import com.android.gallery3d.filtershow.imageshow.MasterImage;

public class StateView extends View {

    private static final String LOGTAG = "StateView";
    private Path mPath = new Path();
    private Paint mPaint = new Paint();

    public static int DEFAULT = 0;
    public static int BEGIN = 1;
    public static int END = 2;

    public static int UP = 1;
    public static int DOWN = 2;
    public static int LEFT = 3;
    public static int RIGHT = 4;

    private int mType = DEFAULT;
    private float mAlpha = 1.0f;
    private String mText = "Default";
    private float mTextSize = 32;
    private static int sMargin = 16;
    private static int sArrowHeight = 16;
    private static int sArrowWidth = 8;
    private int mOrientation = LinearLayout.VERTICAL;
    private int mDirection = DOWN;
    private boolean mDuplicateButton;
    private State mState;

    private int mEndsBackgroundColor;
    private int mEndsTextColor;
    private int mBackgroundColor;
    private int mTextColor;
    private int mSelectedBackgroundColor;
    private int mSelectedTextColor;
    private Rect mTextBounds = new Rect();

    public StateView(Context context) {
        this(context, DEFAULT);
    }

    public StateView(Context context, int type) {
        super(context);
        mType = type;
        Resources res = getResources();
        mEndsBackgroundColor = res.getColor(R.color.filtershow_stateview_end_background);
        mEndsTextColor = res.getColor(R.color.filtershow_stateview_end_text);
        mBackgroundColor = res.getColor(R.color.filtershow_stateview_background);
        mTextColor = res.getColor(R.color.filtershow_stateview_text);
        mSelectedBackgroundColor = res.getColor(R.color.filtershow_stateview_selected_background);
        mSelectedTextColor = res.getColor(R.color.filtershow_stateview_selected_text);
    }

    public String getText() {
        return mText;
    }

    public void setText(String text) {
        mText = text;
        invalidate();
    }

    public void setType(int type) {
        mType = type;
        invalidate();
    }

    @Override
    public void setSelected(boolean value) {
        super.setSelected(value);
        if (!value) {
            mDuplicateButton = false;
        }
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            ViewParent parent = getParent();
            if (parent instanceof PanelTrack) {
                ((PanelTrack) getParent()).onTouch(event, this);
            }
            if (mType == BEGIN) {
                MasterImage.getImage().setShowsOriginal(true);
            }
        }
        if (event.getActionMasked() == MotionEvent.ACTION_UP
                || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
            MasterImage.getImage().setShowsOriginal(false);
        }
        return true;
    }

    public void drawText(Canvas canvas) {
        if (mText == null) {
            return;
        }
        mPaint.reset();
        if (isSelected()) {
            mPaint.setColor(mSelectedTextColor);
        } else {
            mPaint.setColor(mTextColor);
        }
        if (mType == BEGIN) {
            mPaint.setColor(mEndsTextColor);
        }
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        mPaint.getTextBounds(mText, 0, mText.length(), mTextBounds);
        int x = (canvas.getWidth() - mTextBounds.width()) / 2;
        int y = mTextBounds.height() + (canvas.getHeight() - mTextBounds.height()) / 2;
        canvas.drawText(mText, x, y, mPaint);
    }

    public void onDraw(Canvas canvas) {
        canvas.drawARGB(0, 0, 0, 0);
        mPaint.reset();
        mPath.reset();

        float w = canvas.getWidth();
        float h = canvas.getHeight();
        float r = sArrowHeight;
        float d = sArrowWidth;

        if (mOrientation == LinearLayout.HORIZONTAL) {
            drawHorizontalPath(w, h, r, d);
        } else {
            if (mDirection == DOWN) {
                drawVerticalDownPath(w, h, r, d);
            } else {
                drawVerticalPath(w, h, r, d);
            }
        }

        if (mType == DEFAULT || mType == END) {
            if (mDuplicateButton) {
                mPaint.setARGB(255, 200, 0, 0);
            } else if (isSelected()) {
                mPaint.setColor(mSelectedBackgroundColor);
            } else {
                mPaint.setColor(mBackgroundColor);
            }
        } else {
            mPaint.setColor(mEndsBackgroundColor);
        }
        canvas.drawPath(mPath, mPaint);
        drawText(canvas);
    }

    private void drawHorizontalPath(float w, float h, float r, float d) {
        mPath.moveTo(0, 0);
        if (mType == END) {
            mPath.lineTo(w, 0);
            mPath.lineTo(w, h);
        } else {
            mPath.lineTo(w - d, 0);
            mPath.lineTo(w - d, r);
            mPath.lineTo(w, r + d);
            mPath.lineTo(w - d, r + d + r);
            mPath.lineTo(w - d, h);
        }
        mPath.lineTo(0, h);
        if (mType != BEGIN) {
            mPath.lineTo(0, r + d + r);
            mPath.lineTo(d, r + d);
            mPath.lineTo(0, r);
        }
        mPath.close();
    }

    private void drawVerticalPath(float w, float h, float r, float d) {
        if (mType == BEGIN) {
            mPath.moveTo(0, 0);
            mPath.lineTo(w, 0);
        } else {
            mPath.moveTo(0, d);
            mPath.lineTo(r, d);
            mPath.lineTo(r + d, 0);
            mPath.lineTo(r + d + r, d);
            mPath.lineTo(w, d);
        }
        mPath.lineTo(w, h);
        if (mType != END) {
            mPath.lineTo(r + d + r, h);
            mPath.lineTo(r + d, h - d);
            mPath.lineTo(r, h);
        }
        mPath.lineTo(0, h);
        mPath.close();
    }

    private void drawVerticalDownPath(float w, float h, float r, float d) {
        mPath.moveTo(0, 0);
        if (mType != BEGIN) {
            mPath.lineTo(r, 0);
            mPath.lineTo(r + d, d);
            mPath.lineTo(r + d + r, 0);
        }
        mPath.lineTo(w, 0);

        if (mType != END) {
            mPath.lineTo(w, h - d);

            mPath.lineTo(r + d + r, h - d);
            mPath.lineTo(r + d, h);
            mPath.lineTo(r, h - d);

            mPath.lineTo(0, h - d);
        } else {
            mPath.lineTo(w, h);
            mPath.lineTo(0, h);
        }

        mPath.close();
    }

    public void setBackgroundAlpha(float alpha) {
        if (mType == BEGIN) {
            return;
        }
        mAlpha = alpha;
        setAlpha(alpha);
        invalidate();
    }

    public float getBackgroundAlpha() {
        return mAlpha;
    }

    public void setOrientation(int orientation) {
        mOrientation = orientation;
    }

    public void setDuplicateButton(boolean b) {
        mDuplicateButton = b;
        invalidate();
    }

    public State getState() {
        return mState;
    }

    public void setState(State state) {
        mState = state;
        mText = mState.getText().toUpperCase();
        mType = mState.getType();
        invalidate();
    }

    public void resetPosition() {
        setTranslationX(0);
        setTranslationY(0);
        setBackgroundAlpha(1.0f);
    }

    public boolean isDraggable() {
        return mState.isDraggable();
    }
}