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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* Copyright (C) 2010 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.browser.view;
import com.android.browser.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.List;
public class PieMenu extends FrameLayout {
private static final int MAX_LEVELS = 5;
public interface PieController {
/**
* called before menu opens to customize menu
* returns if pie state has been changed
*/
public boolean onOpen();
}
/**
* A view like object that lives off of the pie menu
*/
public interface PieView {
public interface OnLayoutListener {
public void onLayout(int ax, int ay, boolean left);
}
public void setLayoutListener(OnLayoutListener l);
public void layout(int anchorX, int anchorY, boolean onleft);
public void draw(Canvas c);
public boolean onTouchEvent(MotionEvent evt);
}
private Point mCenter;
private int mRadius;
private int mRadiusInc;
private int mSlop;
private boolean mOpen;
private PieController mController;
private List<PieItem> mItems;
private int mLevels;
private int[] mCounts;
private PieView mPieView = null;
private Drawable mBackground;
// touch handling
PieItem mCurrentItem;
/**
* @param context
* @param attrs
* @param defStyle
*/
public PieMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
/**
* @param context
* @param attrs
*/
public PieMenu(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
/**
* @param context
*/
public PieMenu(Context context) {
super(context);
init(context);
}
private void init(Context ctx) {
mItems = new ArrayList<PieItem>();
mLevels = 0;
mCounts = new int[MAX_LEVELS];
Resources res = ctx.getResources();
mRadius = (int) res.getDimension(R.dimen.qc_radius_start);
mRadiusInc = (int) res.getDimension(R.dimen.qc_radius_increment);
mSlop = (int) res.getDimension(R.dimen.qc_slop);
mOpen = false;
setWillNotDraw(false);
setDrawingCacheEnabled(false);
mCenter = new Point(0,0);
mBackground = res.getDrawable(R.drawable.qc_background_normal);
}
public void setController(PieController ctl) {
mController = ctl;
}
public void addItem(PieItem item) {
// add the item to the pie itself
mItems.add(item);
int l = item.getLevel();
mLevels = Math.max(mLevels, l);
mCounts[l]++;
}
public void removeItem(PieItem item) {
mItems.remove(item);
}
public void clearItems() {
mItems.clear();
}
private boolean onTheLeft() {
return mCenter.x < mSlop;
}
/**
* guaranteed has center set
* @param show
*/
private void show(boolean show) {
mOpen = show;
if (mOpen) {
if (mController != null) {
boolean changed = mController.onOpen();
}
layoutPie();
}
if (!show) {
mCurrentItem = null;
mPieView = null;
}
invalidate();
}
private void setCenter(int x, int y) {
if (x < mSlop) {
mCenter.x = 0;
} else {
mCenter.x = getWidth();
}
mCenter.y = y;
}
private void layoutPie() {
int inner = mRadius;
int outer = mRadius + mRadiusInc;
int radius = mRadius;
for (int i = 0; i < mLevels; i++) {
int level = i + 1;
float sweep = (float) Math.PI / (mCounts[level] + 1);
float angle = sweep;
for (PieItem item : mItems) {
if (item.getLevel() == level) {
View view = item.getView();
view.measure(view.getLayoutParams().width,
view.getLayoutParams().height);
int w = view.getMeasuredWidth();
int h = view.getMeasuredHeight();
int x = (int) (outer * Math.sin(angle));
int y = mCenter.y - (int) (outer * Math.cos(angle)) - h / 2;
if (onTheLeft()) {
x = mCenter.x + x - w;
} else {
x = mCenter.x - x;
}
view.layout(x, y, x + w, y + h);
float itemstart = angle - sweep / 2;
item.setGeometry(itemstart, sweep, inner, outer);
angle += sweep;
}
}
inner += mRadiusInc;
outer += mRadiusInc;
}
}
@Override
protected void onDraw(Canvas canvas) {
if (mOpen) {
int w = mBackground.getIntrinsicWidth();
int h = mBackground.getIntrinsicHeight();
int left = mCenter.x - w;
int top = mCenter.y - h / 2;
mBackground.setBounds(left, top, left + w, top + h);
int state = canvas.save();
if (onTheLeft()) {
canvas.scale(-1, 1);
}
mBackground.draw(canvas);
canvas.restoreToCount(state);
for (PieItem item : mItems) {
drawItem(canvas, item);
}
if (mPieView != null) {
mPieView.draw(canvas);
}
}
}
private void drawItem(Canvas canvas, PieItem item) {
int outer = item.getOuterRadius();
int left = mCenter.x - outer;
int top = mCenter.y - outer;
// draw the item view
View view = item.getView();
int state = canvas.save();
canvas.translate(view.getX(), view.getY());
view.draw(canvas);
canvas.restoreToCount(state);
}
// touch handling for pie
@Override
public boolean onTouchEvent(MotionEvent evt) {
float x = evt.getX();
float y = evt.getY();
int action = evt.getActionMasked();
int edges = evt.getEdgeFlags();
if (MotionEvent.ACTION_DOWN == action) {
if ((x > getWidth() - mSlop) || (x < mSlop)) {
setCenter((int) x, (int) y);
show(true);
return true;
}
} else if (MotionEvent.ACTION_UP == action) {
if (mOpen) {
boolean handled = false;
if (mPieView != null) {
handled = mPieView.onTouchEvent(evt);
}
PieItem item = mCurrentItem;
deselect();
show(false);
if (!handled && (item != null)) {
item.getView().performClick();
}
return true;
}
} else if (MotionEvent.ACTION_CANCEL == action) {
if (mOpen) {
show(false);
}
deselect();
return false;
} else if (MotionEvent.ACTION_MOVE == action) {
boolean handled = false;
PointF polar = getPolar(x, y);
int maxr = mRadius + mLevels * mRadiusInc + 50;
if (mPieView != null) {
handled = mPieView.onTouchEvent(evt);
}
if (handled) {
invalidate();
return false;
}
if (polar.y > maxr) {
deselect();
show(false);
evt.setAction(MotionEvent.ACTION_DOWN);
if (getParent() != null) {
((ViewGroup) getParent()).dispatchTouchEvent(evt);
}
return false;
}
PieItem item = findItem(polar);
if (mCurrentItem != item) {
onEnter(item);
if ((item != null) && item.isPieView()) {
int cx = item.getView().getLeft() + (onTheLeft()
? item.getView().getWidth() : 0);
int cy = item.getView().getTop();
mPieView = item.getPieView();
layoutPieView(mPieView, cx, cy);
}
invalidate();
}
}
// always re-dispatch event
return false;
}
private void layoutPieView(PieView pv, int x, int y) {
pv.layout(x, y, onTheLeft());
}
/**
* enter a slice for a view
* updates model only
* @param item
*/
private void onEnter(PieItem item) {
// deselect
if (mCurrentItem != null) {
mCurrentItem.setSelected(false);
}
if (item != null) {
// clear up stack
playSoundEffect(SoundEffectConstants.CLICK);
item.setSelected(true);
mPieView = null;
}
mCurrentItem = item;
}
private void deselect() {
if (mCurrentItem != null) {
mCurrentItem.setSelected(false);
}
mCurrentItem = null;
mPieView = null;
}
private PointF getPolar(float x, float y) {
PointF res = new PointF();
// get angle and radius from x/y
res.x = (float) Math.PI / 2;
x = mCenter.x - x;
if (mCenter.x < mSlop) {
x = -x;
}
y = mCenter.y - y;
res.y = (float) Math.sqrt(x * x + y * y);
if (y > 0) {
res.x = (float) Math.asin(x / res.y);
} else if (y < 0) {
res.x = (float) (Math.PI - Math.asin(x / res.y ));
}
return res;
}
/**
*
* @param polar x: angle, y: dist
* @return the item at angle/dist or null
*/
private PieItem findItem(PointF polar) {
// find the matching item:
for (PieItem item : mItems) {
if ((item.getInnerRadius() < polar.y)
&& (item.getOuterRadius() > polar.y)
&& (item.getStartAngle() < polar.x)
&& (item.getStartAngle() + item.getSweep() > polar.x)) {
return item;
}
}
return null;
}
}
|