Update git submodules
[LibreOffice.git] / android / source / src / java / org / libreoffice / canvas / GraphicSelectionHandle.java
blob05c0dfac4dcf76294512c3cc1931e0ddfdd6f158
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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;
17 /**
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
20 * touched.
22 public class GraphicSelectionHandle extends CommonCanvasElement {
23 /**
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;
37 /**
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);
60 /**
61 * The position of the handle.
62 * @return
64 public HandlePosition getHandlePosition() {
65 return mHandlePosition;
68 /**
69 * Draws the handle to the canvas.
71 * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
73 @Override
74 public void onDraw(Canvas canvas) {
75 if (mSelected) {
76 drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mSelectedFillPaint);
77 } else {
78 drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mFillPaint);
82 /**
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);
90 /**
91 * Viewport has changed, reposition the handle to the input coordinates.
93 public void reposition(float x, float y) {
94 mPosition.x = x;
95 mPosition.y = 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)
111 @Override
112 public boolean onHitTest(float x, float y) {
113 return mHitRect.contains(x, y);
117 * Mark the handle as selected.
119 public void select() {
120 mSelected = true;
124 * Reset the selection for the handle.
126 public void reset() {
127 mSelected = false;
131 * All possible handle positions. The selection rectangle has 8 possible
132 * handles.
134 public enum HandlePosition {
135 TOP_LEFT,
136 TOP,
137 TOP_RIGHT,
138 RIGHT,
139 BOTTOM_RIGHT,
140 BOTTOM,
141 BOTTOM_LEFT,
142 LEFT
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */