LibreOffice Module android (master) 1
GraphicSelection.java
Go to the documentation of this file.
1/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9package org.libreoffice.canvas;
10
11import android.graphics.Canvas;
12import android.graphics.Color;
13import android.graphics.Paint;
14import android.graphics.PointF;
15import android.graphics.RectF;
16
20
22
28 private final Paint mPaintStroke;
29 private final Paint mPaintFill;
30 public RectF mRectangle = new RectF();
31 public RectF mScaledRectangle = new RectF();
32 private RectF mDrawRectangle = new RectF();
34 private PointF mStartDragPosition;
35
38 private boolean mTriggerSinglePress = false;
40
45 mContext = context;
46 // Create the paint, which is needed at drawing
47 mPaintStroke = new Paint();
48 mPaintStroke.setStyle(Paint.Style.STROKE);
49 mPaintStroke.setColor(Color.GRAY);
50 mPaintStroke.setStrokeWidth(2);
51 mPaintStroke.setAntiAlias(true);
52
53 mPaintFill = new Paint();
54 mPaintFill.setStyle(Paint.Style.FILL);
55 mPaintFill.setColor(Color.WHITE);
56 mPaintFill.setAlpha(200);
57 mPaintFill.setAntiAlias(true);
58
59 // Create the handles of the selection
68 }
69
74 public void reposition(RectF scaledRectangle) {
75 mScaledRectangle = scaledRectangle;
76 mDrawRectangle = scaledRectangle; // rectangle that will be draw
77
78 // reposition the handles too
79 mHandles[0].reposition(scaledRectangle.left, scaledRectangle.top);
80 mHandles[1].reposition(scaledRectangle.centerX(), scaledRectangle.top);
81 mHandles[2].reposition(scaledRectangle.right, scaledRectangle.top);
82 mHandles[3].reposition(scaledRectangle.left, scaledRectangle.centerY());
83 mHandles[4].reposition(scaledRectangle.right, scaledRectangle.centerY());
84 mHandles[5].reposition(scaledRectangle.left, scaledRectangle.bottom);
85 mHandles[6].reposition(scaledRectangle.centerX(), scaledRectangle.bottom);
86 mHandles[7].reposition(scaledRectangle.right, scaledRectangle.bottom);
87 }
88
93 @Override
94 public boolean onHitTest(float x, float y) {
95 // Check if handle was hit
96 for (GraphicSelectionHandle handle : mHandles) {
97 if (handle.contains(x, y)) {
98 return true;
99 }
100 }
101 return mScaledRectangle.contains(x, y);
102 }
103
108 @Override
109 public void onDraw(Canvas canvas) {
111 if (mType != DragType.NONE) {
113 }
114 for (GraphicSelectionHandle handle : mHandles) {
115 handle.draw(canvas);
116 }
117 }
118
123 public void dragStart(PointF position) {
124 mDragHandle = null;
126 for (GraphicSelectionHandle handle : mHandles) {
127 if (handle.contains(position.x, position.y)) {
128 mDragHandle = handle;
131 sendGraphicSelectionStart(handle.mPosition);
132 }
133 }
134 if (mDragHandle == null) {
137 }
139 mTriggerSinglePress = true;
140 }
141
146 public void dragging(PointF position) {
147 if (mType == DragType.MOVE) {
148 float deltaX = position.x - mStartDragPosition.x;
149 float deltaY = position.y - mStartDragPosition.y;
150
151 mDrawRectangle = new RectF(mScaledRectangle);
152 mDrawRectangle.offset(deltaX, deltaY);
153 } else if (mType == DragType.EXTEND) {
155 }
156 mTriggerSinglePress = false;
157 }
158
163 public void dragEnd(PointF position) {
164 PointF point = new PointF();
165 if (mDragHandle != null) {
169 mDragHandle = null;
170 } else {
173 }
174 float deltaX = position.x - mStartDragPosition.x;
175 float deltaY = position.y - mStartDragPosition.y;
176 point.offset(deltaX, deltaY);
177
179
180 if (mTriggerSinglePress && mDragHandle == null) {
182 mTriggerSinglePress = false;
183 }
184
187 }
188
192 private void adaptDrawRectangle(float x, float y) {
193 mDrawRectangle = new RectF(mScaledRectangle);
195 case TOP_LEFT:
196 mDrawRectangle.left = x;
197 mDrawRectangle.top = y;
198 break;
199 case TOP:
200 mDrawRectangle.top = y;
201 break;
202 case TOP_RIGHT:
203 mDrawRectangle.right = x;
204 mDrawRectangle.top = y;
205 break;
206 case LEFT:
207 mDrawRectangle.left = x;
208 break;
209 case RIGHT:
210 mDrawRectangle.right = x;
211 break;
212 case BOTTOM_LEFT:
213 mDrawRectangle.left = x;
214 mDrawRectangle.bottom = y;
215 break;
216 case BOTTOM:
217 mDrawRectangle.bottom = y;
218 break;
219 case BOTTOM_RIGHT:
220 mDrawRectangle.right = x;
221 mDrawRectangle.bottom = y;
222 break;
223 }
224 }
225
230 private void sendGraphicSelectionStart(PointF screenPosition) {
231 sendGraphicSelection("GraphicSelectionStart", screenPosition);
232 }
233
238 private void sendGraphicSelectionEnd(PointF screenPosition) {
239 sendGraphicSelection("GraphicSelectionEnd", screenPosition);
240 }
241
247 private void sendGraphicSelection(String type, PointF screenPosition)
248 {
250 if (layerView != null) {
251 // Position is in screen coordinates. We need to convert them to
252 // document coordinates.
253 PointF documentPoint = layerView.getLayerClient().convertViewPointToLayerPoint(screenPosition);
254 LOKitShell.sendTouchEvent(type, documentPoint);
255 }
256 }
257
261 private void onSinglePress(PointF screenPosition) {
262 sendGraphicSelection("LongPress", screenPosition);
263 }
264
268 @Override
269 public void setVisible(boolean visible) {
270 super.setVisible(visible);
271 for (GraphicSelectionHandle handle: mHandles) {
272 handle.setVisible(visible);
273 }
274 }
275
279 public void reset() {
280 mDragHandle = null;
281 for (GraphicSelectionHandle handle : mHandles) {
282 handle.reset();
283 }
284 }
285
289 public enum DragType {
292 EXTEND
294}
295/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Common static LOKit functions, functions to send events.
Definition: LOKitShell.java:26
static void sendTouchEvent(String touchType, PointF documentTouchCoordinate)
Send touch event to LOKitThread.
Definition: LOKitShell.java:83
Main activity of the LibreOffice App.
Common implementation to canvas elements.
This class is responsible to draw the selection handles, track the handle position and perform a hit ...
void reposition(float x, float y)
Viewport has changed, reposition the handle to the input coordinates.
void reset()
Reset the selection for the handle.
HandlePosition getHandlePosition()
The position of the handle.
This class is responsible to draw and reposition the selection rectangle.
void onDraw(Canvas canvas)
Draw the selection on the canvas.
void dragging(PointF position)
Dragging is in process.
void sendGraphicSelectionEnd(PointF screenPosition)
Send graphic selection end event to LOKitTread.
void sendGraphicSelectionStart(PointF screenPosition)
Send graphic selection start event to LOKitTread.
void dragStart(PointF position)
Dragging on the screen has started.
boolean onHitTest(float x, float y)
Hit test for the selection.
void setVisible(boolean visible)
Set the visibility of the graphic selection.
GraphicSelection(LibreOfficeMainActivity context)
Construct the graphic selection.
void adaptDrawRectangle(float x, float y)
Adapt the selection depending on which handle was dragged.
void onSinglePress(PointF screenPosition)
When a single press (no dragging happened) was performed.
void dragEnd(PointF position)
Dragging has ended.
void reposition(RectF scaledRectangle)
Viewport has changed, reposition the selection to the new rectangle.
void sendGraphicSelection(String type, PointF screenPosition)
Send graphic selection event to LOKitTread.
PointF convertViewPointToLayerPoint(PointF viewPoint)
A view rendered by the layer compositor.
Definition: LayerView.java:41
GeckoLayerClient getLayerClient()
Definition: LayerView.java:155
float y
float x
def position(n=-1)
def point()
const sal_uInt32 LEFT
const sal_uInt32 TOP
const sal_uInt32 BOTTOM
const sal_uInt32 RIGHT
BOTTOM_LEFT
BOTTOM_RIGHT
TOP_LEFT
TOP_RIGHT
ResultType type