Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / android / source / src / java / org / libreoffice / canvas / GraphicSelection.java
blob30a328d88c9c7d87d2e368eb864d75fc21440054
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 import org.libreoffice.LOKitShell;
18 import org.mozilla.gecko.gfx.LayerView;
20 import static org.libreoffice.canvas.GraphicSelectionHandle.HandlePosition;
22 /**
23 * This class is responsible to draw and reposition the selection
24 * rectangle.
26 public class GraphicSelection extends CommonCanvasElement {
27 private final Paint mPaintStroke;
28 private final Paint mPaintFill;
29 public RectF mRectangle = new RectF();
30 public RectF mScaledRectangle = new RectF();
31 private RectF mDrawRectangle = new RectF();
32 private DragType mType = DragType.NONE;
33 private PointF mStartDragPosition;
35 private GraphicSelectionHandle mHandles[] = new GraphicSelectionHandle[8];
36 private GraphicSelectionHandle mDragHandle = null;
37 private boolean mTriggerSinglePress = false;
39 /**
40 * Construct the graphic selection.
42 public GraphicSelection() {
43 // Create the paint, which is needed at drawing
44 mPaintStroke = new Paint();
45 mPaintStroke.setStyle(Paint.Style.STROKE);
46 mPaintStroke.setColor(Color.GRAY);
47 mPaintStroke.setStrokeWidth(2);
48 mPaintStroke.setAntiAlias(true);
50 mPaintFill = new Paint();
51 mPaintFill.setStyle(Paint.Style.FILL);
52 mPaintFill.setColor(Color.WHITE);
53 mPaintFill.setAlpha(200);
54 mPaintFill.setAntiAlias(true);
56 // Create the handles of the selection
57 mHandles[0] = new GraphicSelectionHandle(HandlePosition.TOP_LEFT);
58 mHandles[1] = new GraphicSelectionHandle(HandlePosition.TOP);
59 mHandles[2] = new GraphicSelectionHandle(HandlePosition.TOP_RIGHT);
60 mHandles[3] = new GraphicSelectionHandle(HandlePosition.LEFT);
61 mHandles[4] = new GraphicSelectionHandle(HandlePosition.RIGHT);
62 mHandles[5] = new GraphicSelectionHandle(HandlePosition.BOTTOM_LEFT);
63 mHandles[6] = new GraphicSelectionHandle(HandlePosition.BOTTOM);
64 mHandles[7] = new GraphicSelectionHandle(HandlePosition.BOTTOM_RIGHT);
67 /**
68 * Viewport has changed, reposition the selection to the new rectangle.
69 * @param scaledRectangle - rectangle of selection position on the document
71 public void reposition(RectF scaledRectangle) {
72 mScaledRectangle = scaledRectangle;
73 mDrawRectangle = scaledRectangle; // rectangle that will be draw
75 // reposition the handles too
76 mHandles[0].reposition(scaledRectangle.left, scaledRectangle.top);
77 mHandles[1].reposition(scaledRectangle.centerX(), scaledRectangle.top);
78 mHandles[2].reposition(scaledRectangle.right, scaledRectangle.top);
79 mHandles[3].reposition(scaledRectangle.left, scaledRectangle.centerY());
80 mHandles[4].reposition(scaledRectangle.right, scaledRectangle.centerY());
81 mHandles[5].reposition(scaledRectangle.left, scaledRectangle.bottom);
82 mHandles[6].reposition(scaledRectangle.centerX(), scaledRectangle.bottom);
83 mHandles[7].reposition(scaledRectangle.right, scaledRectangle.bottom);
86 /**
87 * Hit test for the selection.
88 * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
90 @Override
91 public boolean onHitTest(float x, float y) {
92 // Check if handle was hit
93 for (GraphicSelectionHandle handle : mHandles) {
94 if (handle.contains(x, y)) {
95 return true;
98 return mScaledRectangle.contains(x, y);
102 * Draw the selection on the canvas.
103 * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
105 @Override
106 public void onDraw(Canvas canvas) {
107 canvas.drawRect(mDrawRectangle, mPaintStroke);
108 if (mType != DragType.NONE) {
109 canvas.drawRect(mDrawRectangle, mPaintFill);
111 for (GraphicSelectionHandle handle : mHandles) {
112 handle.draw(canvas);
117 * Dragging on the screen has started.
118 * @param position - position where the dragging started
120 public void dragStart(PointF position) {
121 mDragHandle = null;
122 mType = DragType.NONE;
123 for (GraphicSelectionHandle handle : mHandles) {
124 if (handle.contains(position.x, position.y)) {
125 mDragHandle = handle;
126 mDragHandle.select();
127 mType = DragType.EXTEND;
128 sendGraphicSelectionStart(handle.mPosition);
131 if (mDragHandle == null) {
132 mType = DragType.MOVE;
133 sendGraphicSelectionStart(position);
135 mStartDragPosition = position;
136 mTriggerSinglePress = true;
140 * Dragging is in process.
141 * @param position - position of the drag
143 public void dragging(PointF position) {
144 if (mType == DragType.MOVE) {
145 float deltaX = position.x - mStartDragPosition.x;
146 float deltaY = position.y - mStartDragPosition.y;
148 mDrawRectangle = new RectF(mScaledRectangle);
149 mDrawRectangle.offset(deltaX, deltaY);
150 } else if (mType == DragType.EXTEND) {
151 adaptDrawRectangle(position.x, position.y);
153 mTriggerSinglePress = false;
157 * Dragging has ended.
158 * @param position - last position of the drag
160 public void dragEnd(PointF position) {
161 PointF point = new PointF();
162 if (mDragHandle != null) {
163 point.x = mDragHandle.mPosition.x;
164 point.y = mDragHandle.mPosition.y;
165 mDragHandle.reset();
166 mDragHandle = null;
167 } else {
168 point.x = mStartDragPosition.x;
169 point.y = mStartDragPosition.y;
171 float deltaX = position.x - mStartDragPosition.x;
172 float deltaY = position.y - mStartDragPosition.y;
173 point.offset(deltaX, deltaY);
175 sendGraphicSelectionEnd(point);
177 if (mTriggerSinglePress && mDragHandle == null) {
178 onSinglePress(point);
179 mTriggerSinglePress = false;
182 mDrawRectangle = mScaledRectangle;
183 mType = DragType.NONE;
187 * Adapt the selection depending on which handle was dragged.
189 private void adaptDrawRectangle(float x, float y) {
190 mDrawRectangle = new RectF(mScaledRectangle);
191 switch(mDragHandle.getHandlePosition()) {
192 case TOP_LEFT:
193 mDrawRectangle.left = x;
194 mDrawRectangle.top = y;
195 break;
196 case TOP:
197 mDrawRectangle.top = y;
198 break;
199 case TOP_RIGHT:
200 mDrawRectangle.right = x;
201 mDrawRectangle.top = y;
202 break;
203 case LEFT:
204 mDrawRectangle.left = x;
205 break;
206 case RIGHT:
207 mDrawRectangle.right = x;
208 break;
209 case BOTTOM_LEFT:
210 mDrawRectangle.left = x;
211 mDrawRectangle.bottom = y;
212 break;
213 case BOTTOM:
214 mDrawRectangle.bottom = y;
215 break;
216 case BOTTOM_RIGHT:
217 mDrawRectangle.right = x;
218 mDrawRectangle.bottom = y;
219 break;
224 * Send graphic selection start event to LOKitTread.
225 * @param screenPosition - screen position of the selection
227 private void sendGraphicSelectionStart(PointF screenPosition) {
228 sendGraphicSelection("GraphicSelectionStart", screenPosition);
232 * Send graphic selection end event to LOKitTread.
233 * @param screenPosition - screen position of the selection
235 private void sendGraphicSelectionEnd(PointF screenPosition) {
236 sendGraphicSelection("GraphicSelectionEnd", screenPosition);
240 * Send graphic selection event to LOKitTread.
241 * @param type - type of the graphic selection
242 * @param screenPosition - screen position of the selection
244 private void sendGraphicSelection(String type, PointF screenPosition)
246 LayerView layerView = LOKitShell.getLayerView();
247 if (layerView != null) {
248 // Position is in screen coordinates. We need to convert them to
249 // document coordinates.
250 PointF documentPoint = layerView.getLayerClient().convertViewPointToLayerPoint(screenPosition);
251 LOKitShell.sendTouchEvent(type, documentPoint);
256 * When a single press (no dragging happened) was performed.
258 private void onSinglePress(PointF screenPosition) {
259 sendGraphicSelection("LongPress", screenPosition);
263 * Set the visibility of the graphic selection.
265 @Override
266 public void setVisible(boolean visible) {
267 super.setVisible(visible);
268 for (GraphicSelectionHandle handle: mHandles) {
269 handle.setVisible(visible);
274 * Reset the selection.
276 public void reset() {
277 mDragHandle = null;
278 for (GraphicSelectionHandle handle : mHandles) {
279 handle.reset();
284 * Type of the selection dragging.
286 public enum DragType {
287 NONE,
288 MOVE,
289 EXTEND
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */