1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 package org
.libreoffice
.canvas
;
11 import android
.graphics
.Canvas
;
12 import android
.graphics
.Color
;
13 import android
.graphics
.Paint
;
14 import android
.graphics
.PointF
;
15 import android
.graphics
.RectF
;
18 * This class is responsible to draw the selection handles, track the handle
19 * position and perform a hit test to determine if the selection handle was
22 public class GraphicSelectionHandle
extends CommonCanvasElement
{
24 * The factor used to inflate the hit area.
26 private static final float HIT_AREA_INFLATE_FACTOR
= 1.75f
;
28 private final HandlePosition mHandlePosition
;
29 public PointF mPosition
= new PointF();
30 private float mRadius
= 20.0f
;
31 private Paint mStrokePaint
= new Paint();
32 private Paint mFillPaint
= new Paint();
33 private Paint mSelectedFillPaint
= new Paint();
34 private RectF mHitRect
= new RectF();
35 private boolean mSelected
= false;
38 * Construct the handle - set the handle position on the selection.
39 * @param position - the handle position on the selection
41 public GraphicSelectionHandle(HandlePosition position
) {
42 mHandlePosition
= position
;
44 mStrokePaint
.setStyle(Paint
.Style
.STROKE
);
45 mStrokePaint
.setColor(Color
.GRAY
);
46 mStrokePaint
.setStrokeWidth(3);
47 mStrokePaint
.setAntiAlias(true);
49 mFillPaint
.setStyle(Paint
.Style
.FILL
);
50 mFillPaint
.setColor(Color
.WHITE
);
51 mFillPaint
.setAlpha(200);
52 mFillPaint
.setAntiAlias(true);
54 mSelectedFillPaint
.setStyle(Paint
.Style
.FILL
);
55 mSelectedFillPaint
.setColor(Color
.GRAY
);
56 mSelectedFillPaint
.setAlpha(200);
57 mSelectedFillPaint
.setAntiAlias(true);
61 * The position of the handle.
64 public HandlePosition
getHandlePosition() {
65 return mHandlePosition
;
69 * Draws the handle to the canvas.
71 * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
74 public void onDraw(Canvas canvas
) {
76 drawFilledCircle(canvas
, mPosition
.x
, mPosition
.y
, mRadius
, mStrokePaint
, mSelectedFillPaint
);
78 drawFilledCircle(canvas
, mPosition
.x
, mPosition
.y
, mRadius
, mStrokePaint
, mFillPaint
);
83 * Draw a filled and stroked circle to the canvas.
85 private void drawFilledCircle(Canvas canvas
, float x
, float y
, float radius
, Paint strokePaint
, Paint fillPaint
) {
86 canvas
.drawCircle(x
, y
, radius
, fillPaint
);
87 canvas
.drawCircle(x
, y
, radius
, strokePaint
);
91 * Viewport has changed, reposition the handle to the input coordinates.
93 public void reposition(float x
, float y
) {
97 // inflate the radius by HIT_AREA_INFLATE_FACTOR
98 float inflatedRadius
= mRadius
* HIT_AREA_INFLATE_FACTOR
;
100 // reposition the hit area rectangle
101 mHitRect
.left
= mPosition
.x
- inflatedRadius
;
102 mHitRect
.right
= mPosition
.x
+ inflatedRadius
;
103 mHitRect
.top
= mPosition
.y
- inflatedRadius
;
104 mHitRect
.bottom
= mPosition
.y
+ inflatedRadius
;
108 * Hit test for the handle.
109 * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
112 public boolean onHitTest(float x
, float y
) {
113 return mHitRect
.contains(x
, y
);
117 * Mark the handle as selected.
119 public void select() {
124 * Reset the selection for the handle.
126 public void reset() {
131 * All possible handle positions. The selection rectangle has 8 possible
134 public enum HandlePosition
{
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */