summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2015-03-23 16:51:47 +0900
committerMiklos Vajna <vmiklos@collabora.co.uk>2015-03-23 09:19:08 +0100
commit205b69526c1a8d79da08929b1832ffb5de6c61b2 (patch)
treebbeb71f3b506ed9e4278a8a0754022e9aa2dbb56 /android
parent19c59a34eb2c106ded992a0152cd9bbe8c9b70cb (diff)
android: extract drawing of selection into its own class
Change-Id: I8e94edeafbf5b7fd5f02a1429893c4b803c71afa
Diffstat (limited to 'android')
-rw-r--r--android/experimental/LOAndroid3/src/java/org/libreoffice/DrawElementGraphicSelection.java66
-rw-r--r--android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java68
2 files changed, 102 insertions, 32 deletions
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/DrawElementGraphicSelection.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/DrawElementGraphicSelection.java
new file mode 100644
index 000000000000..e4319fc28f96
--- /dev/null
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/DrawElementGraphicSelection.java
@@ -0,0 +1,66 @@
+package org.libreoffice;
+
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.PointF;
+import android.graphics.RectF;
+
+/**
+ * This class is responsible to draw and reposition the selection
+ * rectangle.
+ */
+public class DrawElementGraphicSelection {
+ private final Paint mGraphicSelectionPaint;
+ public RectF mRectangle = new RectF();
+ public RectF mScaledRectangle = new RectF();
+ private RectF mDrawRectangle = new RectF();
+ private DragType mType = DragType.NONE;
+ private PointF mStartDragPosition;
+
+ public DrawElementGraphicSelection(Paint graphicSelectionPaint) {
+ mGraphicSelectionPaint = graphicSelectionPaint;
+ }
+
+ public void reposition(RectF scaledRectangle) {
+ mScaledRectangle = scaledRectangle;
+ mDrawRectangle = scaledRectangle;
+ }
+
+ public boolean contains(float x, float y) {
+ return mScaledRectangle.contains(x, y);
+ }
+
+ public void draw(Canvas canvas) {
+ canvas.drawRect(mDrawRectangle, mGraphicSelectionPaint);
+ }
+
+ public void dragStart(DragType type, PointF position) {
+ mType = type;
+ mStartDragPosition = position;
+ }
+
+ public void dragging(PointF position) {
+ if (mType == DragType.MOVE) {
+
+ float deltaX = position.x - mStartDragPosition.x;
+ float deltaY = position.y - mStartDragPosition.y;
+
+ mDrawRectangle = new RectF(mScaledRectangle);
+ mDrawRectangle.offset(deltaX, deltaY);
+ } else if (mType == DragType.EXTEND) {
+ mDrawRectangle = new RectF(mScaledRectangle);
+ mDrawRectangle.union(position.x, position.y);
+ }
+ }
+
+ public void dragEnd() {
+ mType = DragType.NONE;
+ mDrawRectangle = mScaledRectangle;
+ }
+
+ enum DragType {
+ NONE,
+ MOVE,
+ EXTEND
+ }
+}
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java
index 26eb215a4f47..320cffc2ae9f 100644
--- a/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java
@@ -48,14 +48,16 @@ public class TextCursorView extends View implements View.OnTouchListener {
private Paint mSelectionPaint = new Paint();
private boolean mSelectionsVisible;
- private RectF mGraphicSelection = new RectF();
- private RectF mGraphicScaledSelection = new RectF();
private Paint mGraphicSelectionPaint = new Paint();
- private boolean mGraphicSelectionVisible;
+ private DrawElementGraphicSelection mGraphicSelection;
+
private PointF mTouchStart = new PointF();
private PointF mDeltaPoint = new PointF();
+
+ private boolean mGraphicSelectionVisible;
private boolean mGraphicSelectionMove = false;
+
private LayerView mLayerView;
private DrawElementHandle mHandles[] = new DrawElementHandle[8];
@@ -94,6 +96,9 @@ public class TextCursorView extends View implements View.OnTouchListener {
mGraphicSelectionPaint.setStyle(Paint.Style.STROKE);
mGraphicSelectionPaint.setColor(Color.BLACK);
mGraphicSelectionPaint.setStrokeWidth(2);
+
+ mGraphicSelection = new DrawElementGraphicSelection(mGraphicSelectionPaint);
+
mGraphicSelectionVisible = false;
mHandles[0] = new DrawElementHandle(mGraphicSelectionPaint);
@@ -144,39 +149,43 @@ public class TextCursorView extends View implements View.OnTouchListener {
return;
}
- mGraphicSelection = rectangle;
+ mGraphicSelection.mRectangle = rectangle;
ImmutableViewportMetrics metrics = layerView.getViewportMetrics();
repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
}
public void repositionWithViewport(float x, float y, float zoom) {
- mCursorScaledPosition = RectUtils.scale(mCursorPosition, zoom);
- mCursorScaledPosition.offset(-x, -y);
+ mCursorScaledPosition = convertPosition(mCursorPosition, x, y, zoom);
mCursorScaledPosition.right = mCursorScaledPosition.left + CURSOR_WIDTH;
mScaledSelections.clear();
for (RectF selection : mSelections) {
- RectF scaledSelection = RectUtils.scale(selection, zoom);
- scaledSelection.offset(-x, -y);
+ RectF scaledSelection = convertPosition(selection, x, y, zoom);
mScaledSelections.add(scaledSelection);
}
- mGraphicScaledSelection = RectUtils.scale(mGraphicSelection, zoom);
- mGraphicScaledSelection.offset(-x, -y);
+ RectF scaledGraphicSelection = convertPosition(mGraphicSelection.mRectangle, x, y, zoom);
+ mGraphicSelection.reposition(scaledGraphicSelection);
- mHandles[0].reposition(mGraphicScaledSelection.left, mGraphicScaledSelection.top);
- mHandles[1].reposition(mGraphicScaledSelection.centerX(), mGraphicScaledSelection.top);
- mHandles[2].reposition(mGraphicScaledSelection.right, mGraphicScaledSelection.top);
- mHandles[3].reposition(mGraphicScaledSelection.left, mGraphicScaledSelection.centerY());
- mHandles[4].reposition(mGraphicScaledSelection.right, mGraphicScaledSelection.centerY());
- mHandles[5].reposition(mGraphicScaledSelection.left, mGraphicScaledSelection.bottom);
- mHandles[6].reposition(mGraphicScaledSelection.centerX(), mGraphicScaledSelection.bottom);
- mHandles[7].reposition(mGraphicScaledSelection.right, mGraphicScaledSelection.bottom);
+ mHandles[0].reposition(scaledGraphicSelection.left, scaledGraphicSelection.top);
+ mHandles[1].reposition(scaledGraphicSelection.centerX(), scaledGraphicSelection.top);
+ mHandles[2].reposition(scaledGraphicSelection.right, scaledGraphicSelection.top);
+ mHandles[3].reposition(scaledGraphicSelection.left, scaledGraphicSelection.centerY());
+ mHandles[4].reposition(scaledGraphicSelection.right, scaledGraphicSelection.centerY());
+ mHandles[5].reposition(scaledGraphicSelection.left, scaledGraphicSelection.bottom);
+ mHandles[6].reposition(scaledGraphicSelection.centerX(), scaledGraphicSelection.bottom);
+ mHandles[7].reposition(scaledGraphicSelection.right, scaledGraphicSelection.bottom);
invalidate();
}
+ private RectF convertPosition(RectF cursorPosition, float x, float y, float zoom) {
+ RectF cursor = RectUtils.scale(cursorPosition, zoom);
+ cursor.offset(-x, -y);
+ return cursor;
+ }
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
@@ -189,6 +198,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
}
}
if (mGraphicSelectionVisible) {
+ mGraphicSelection.draw(canvas);
+
if (mGraphicSelectionMove) {
for (DrawElementHandle handle : mHandles) {
if (mDragHandle == handle) {
@@ -197,20 +208,7 @@ public class TextCursorView extends View implements View.OnTouchListener {
handle.draw(canvas);
}
}
-
- RectF selectionRect = new RectF(mGraphicScaledSelection);
- if (mDragHandle != null) {
- PointF dragPosition = new PointF(mDragHandle.mPosition.x, mDragHandle.mPosition.y);
- dragPosition.offset(mDeltaPoint.x, mDeltaPoint.y);
- selectionRect.union(dragPosition.x, dragPosition.y);
- canvas.drawRect(selectionRect, mGraphicSelectionPaint);
- } else {
- selectionRect.offset(mDeltaPoint.x, mDeltaPoint.y);
- canvas.drawRect(selectionRect, mGraphicSelectionPaint);
- }
} else {
- canvas.drawRect(mGraphicScaledSelection, mGraphicSelectionPaint);
-
for (DrawElementHandle handle : mHandles) {
handle.draw(canvas);
}
@@ -286,6 +284,7 @@ public class TextCursorView extends View implements View.OnTouchListener {
if (mGraphicSelectionVisible && mGraphicSelectionMove) {
mDeltaPoint.x = event.getX() - mTouchStart.x;
mDeltaPoint.y = event.getY() - mTouchStart.y;
+ mGraphicSelection.dragging(new PointF(event.getX(), event.getY()));
invalidate();
return true;
}
@@ -301,13 +300,15 @@ public class TextCursorView extends View implements View.OnTouchListener {
if (handle.contains(mTouchStart.x, mTouchStart.y)) {
mDragHandle = handle;
mGraphicSelectionMove = true;
+ mGraphicSelection.dragStart(DrawElementGraphicSelection.DragType.EXTEND, mTouchStart);
sendGraphicSelectionStart(handle.mPosition);
return true;
}
}
// Check if inside graphic selection was hit
- if (mGraphicScaledSelection.contains(mTouchStart.x, mTouchStart.y)) {
+ if (mGraphicSelection.contains(mTouchStart.x, mTouchStart.y)) {
mGraphicSelectionMove = true;
+ mGraphicSelection.dragStart(DrawElementGraphicSelection.DragType.MOVE, mTouchStart);
sendGraphicSelectionStart(mTouchStart);
return true;
}
@@ -316,6 +317,7 @@ public class TextCursorView extends View implements View.OnTouchListener {
private boolean stopGraphicSelection() {
mGraphicSelectionMove = false;
+
PointF point = new PointF();
if (mDragHandle != null) {
point.x = mDragHandle.mPosition.x;
@@ -326,6 +328,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
}
point.offset(mDeltaPoint.x, mDeltaPoint.y);
sendGraphicSelectionEnd(point);
+
+ mGraphicSelection.dragEnd();
invalidate();
return true;
}