summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/view/PieMenu.java
blob: 5185adbbfdd484872879d0c53926844be6834524 (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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
 * 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.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PieMenu extends FrameLayout {

    private static final int RADIUS_GAP = 10;

    public interface PieController {
        /**
         * called before menu opens to customize menu
         * returns if pie state has been changed
         */
        public boolean onOpen();
    }
    private Point mCenter;
    private int mRadius;
    private int mRadiusInc;
    private int mSlop;

    private boolean mOpen;
    private Paint mPaint;
    private Paint mSelectedPaint;
    private PieController mController;

    private Map<View, List<View>> mMenu;
    private List<View> mStack;

    private boolean mDirty;

    /**
     * @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) {
        this.setTag(new MenuTag(0));
        mStack = new ArrayList<View>();
        mStack.add(this);
        Resources res = ctx.getResources();
        mRadius = (int) res.getDimension(R.dimen.qc_radius);
        mRadiusInc = (int) res.getDimension(R.dimen.qc_radius_inc);
        mSlop = (int) res.getDimension(R.dimen.qc_slop);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(res.getColor(R.color.qc_slice_normal));
        mSelectedPaint = new Paint();
        mSelectedPaint.setAntiAlias(true);
        mSelectedPaint.setColor(res.getColor(R.color.qc_slice_active));
        mOpen = false;
        mMenu = new HashMap<View, List<View>>();
        setWillNotDraw(false);
        setDrawingCacheEnabled(false);
        mCenter = new Point(0,0);
        mDirty = true;
    }

    public void setController(PieController ctl) {
        mController = ctl;
    }

    public void setRadius(int r) {
        mRadius = r;
        requestLayout();
    }

    public void setRadiusIncrement(int ri) {
        mRadiusInc = ri;
        requestLayout();
    }

    /**
     * add a menu item to another item as a submenu
     * @param item
     * @param parent
     */
    public void addItem(View item, View parent) {
        List<View> subs = mMenu.get(parent);
        if (subs == null) {
            subs = new ArrayList<View>();
            mMenu.put(parent, subs);
        }
        subs.add(item);
        MenuTag tag = new MenuTag(((MenuTag) parent.getTag()).level + 1);
        item.setTag(tag);
    }

    public void addItem(View view) {
        // add the item to the pie itself
        addItem(view, this);
    }

    public void removeItem(View view) {
        List<View> subs = mMenu.get(view);
        mMenu.remove(view);
        for (View p : mMenu.keySet()) {
            List<View> sl = mMenu.get(p);
            if (sl != null) {
                sl.remove(view);
            }
        }
    }

    public void clearItems(View parent) {
        List<View> subs = mMenu.remove(parent);
        if (subs != null) {
            for (View sub: subs) {
                clearItems(sub);
            }
        }
    }

    public void clearItems() {
        mMenu.clear();
    }


    public void show(boolean show) {
        mOpen = show;
        if (mOpen) {
            if (mController != null) {
                boolean changed = mController.onOpen();
            }
            mDirty = true;
        }
        if (!show) {
            // hide sub items
            mStack.clear();
            mStack.add(this);
        }
        invalidate();
    }

    private void setCenter(int x, int y) {
        if (x < mSlop) {
            mCenter.x = 0;
        } else {
            mCenter.x = getWidth();
        }
        mCenter.y = y;
    }

    private boolean onTheLeft() {
        return mCenter.x < mSlop;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mOpen) {
            int radius = mRadius;
            // start in the center for 0 level menu
            float anchor = (float) Math.PI / 2;
            PointF angles = new PointF();
            int state = canvas.save();
            if (onTheLeft()) {
                // left handed
                canvas.scale(-1, 1);
            }
            for (View parent : mStack) {
                List<View> subs = mMenu.get(parent);
                if (subs != null) {
                    setGeometry(anchor, subs.size(), angles);
                }
                anchor = drawSlices(canvas, subs, radius, angles.x, angles.y);
                radius += mRadiusInc + RADIUS_GAP;
            }
            canvas.restoreToCount(state);
            mDirty = false;
        }
    }

    /**
     * draw the set of slices
     * @param canvas
     * @param items
     * @param radius
     * @param start
     * @param sweep
     * @return the angle of the selected slice
     */
    private float drawSlices(Canvas canvas, List<View> items, int radius,
            float start, float sweep) {
        float angle = start + sweep / 2;
        // gap between slices in degrees
        float gap = 1f;
        float newanchor = 0f;
        for (View item : items) {
            if (mDirty) {
                item.measure(item.getLayoutParams().width,
                        item.getLayoutParams().height);
                int w = item.getMeasuredWidth();
                int h = item.getMeasuredHeight();
                int x = (int) (radius * Math.sin(angle));
                int y =  mCenter.y - (int) (radius * Math.cos(angle)) - h / 2;
                if (onTheLeft()) {
                    x = mCenter.x + x - w / 2;
                } else {
                    x = mCenter.x - x - w / 2;
                }
                item.layout(x, y, x + w, y + h);
            }
            float itemstart = angle - sweep / 2;
            int inner = radius - mRadiusInc / 2;
            int outer = radius + mRadiusInc / 2;
            Path slice = makeSlice(getDegrees(itemstart) - gap,
                    getDegrees(itemstart + sweep) + gap,
                    outer, inner, mCenter);
            MenuTag tag = (MenuTag) item.getTag();
            tag.start = itemstart;
            tag.sweep = sweep;
            tag.inner = inner;
            tag.outer = outer;

            Paint p = item.isPressed() ? mSelectedPaint : mPaint;
            canvas.drawPath(slice, p);
            int state = canvas.save();
            if (onTheLeft()) {
                canvas.scale(-1, 1);
            }
            canvas.translate(item.getX(), item.getY());
            item.draw(canvas);
            canvas.restoreToCount(state);
            if (mStack.contains(item)) {
                // item is anchor for sub menu
                newanchor = angle;
            }
            angle += sweep;
        }
        return newanchor;
    }

    /**
     * converts a
     * @param angle from 0..PI to Android degrees (clockwise starting at 3 o'clock)
     * @return skia angle
     */
    private float getDegrees(double angle) {
        return (float) (270 - 180 * angle / Math.PI);
    }

    private Path makeSlice(float startangle, float endangle, int outerradius,
            int innerradius, Point center) {
        RectF bb = new RectF(center.x - outerradius, center.y - outerradius,
                center.x + outerradius, center.y + outerradius);
        RectF bbi = new RectF(center.x - innerradius, center.y - innerradius,
                center.x + innerradius, center.y + innerradius);
        Path path = new Path();
        path.arcTo(bb, startangle, endangle - startangle, true);
        path.arcTo(bbi, endangle, startangle - endangle);
        path.close();
        return path;
    }

    /**
     * all angles are 0 .. MATH.PI where 0 points up, and rotate counterclockwise
     * set the startangle and slice sweep in result
     * @param anchorangle : angle at which the menu is anchored
     * @param nslices
     * @param result : x : start, y : sweep
     */
    private void setGeometry(float anchorangle, int nslices, PointF result) {
        float span = (float) Math.min(anchorangle, Math.PI - anchorangle);
        float sweep = 2 * span / (nslices + 1);
        result.x = anchorangle - span + sweep / 2;
        result.y = sweep;
    }

    // touch handling for pie

    View mCurrentView;
    Rect mHitRect = new Rect();

    @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) {
                View v = mCurrentView;
                deselect();
                if (v != null) {
                    v.performClick();
                }
                show(false);
                return true;
            }
        } else if (MotionEvent.ACTION_CANCEL == action) {
            if (mOpen) {
                show(false);
            }
            deselect();
            return false;
        } else if (MotionEvent.ACTION_MOVE == action) {
            PointF polar = getPolar(x, y);
            if (polar.y > mRadius + 2 * mRadiusInc) {
                show(false);
                deselect();
                evt.setAction(MotionEvent.ACTION_DOWN);
                if (getParent() != null) {
                    ((ViewGroup) getParent()).dispatchTouchEvent(evt);
                }
                return false;
            }
            View v = findView(polar);
            if (mCurrentView != v) {
                onEnter(v);
                invalidate();
            }
        }
        // always re-dispatch event
        return false;
    }

    /**
     * enter a slice for a view
     * updates model only
     * @param view
     */
    private void onEnter(View view) {
        // deselect
        if (mCurrentView != null) {
            if (getLevel(mCurrentView) >= getLevel(view)) {
                mCurrentView.setPressed(false);
            }
        }
        if (view != null) {
            // clear up stack
            MenuTag tag = (MenuTag) view.getTag();
            int i = mStack.size() - 1;
            while (i > 0) {
                View v = mStack.get(i);
                if (((MenuTag) v.getTag()).level >= tag.level) {
                    v.setPressed(false);
                    mStack.remove(i);
                } else {
                    break;
                }
                i--;
            }
            List<View> items = mMenu.get(view);
            if (items != null) {
                mStack.add(view);
                mDirty = true;
            }
            view.setPressed(true);
        }
        mCurrentView = view;
    }

    private void deselect() {
        if (mCurrentView != null) {
            mCurrentView.setPressed(false);
        }
        mCurrentView = null;
    }

    private int getLevel(View v) {
        if (v == null) return -1;
        return ((MenuTag) v.getTag()).level;
    }

    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
     */
    private View findView(PointF polar) {
        // find the matching item:
        for (View parent : mStack) {
            List<View> subs = mMenu.get(parent);
            if (subs != null) {
                for (View item : subs) {
                    MenuTag tag = (MenuTag) item.getTag();
                    if ((tag.inner < polar.y)
                            && (tag.outer > polar.y)
                            && (tag.start < polar.x)
                            && (tag.start + tag.sweep > polar.x)) {
                        return item;
                    }
                }
            }
        }
        return null;
    }

    class MenuTag {

        int level;
        float start;
        float sweep;
        int inner;
        int outer;

        public MenuTag(int l) {
            level = l;
        }

    }

}