android: fix handle and selection behaviour, clean-up

Change-Id: I1f4404b1beb8dc3d5f64c21443c8e50cefe6fc63
This commit is contained in:
Tomaž Vajngerl 2015-03-26 19:29:41 +09:00 committed by Miklos Vajna
parent 2f6c4d9937
commit ba93e70c9f
3 changed files with 110 additions and 42 deletions

View File

@ -9,6 +9,7 @@
package org.libreoffice.canvas; package org.libreoffice.canvas;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.RectF; import android.graphics.RectF;
@ -16,12 +17,14 @@ import android.graphics.RectF;
import org.libreoffice.LOKitShell; import org.libreoffice.LOKitShell;
import org.mozilla.gecko.gfx.LayerView; import org.mozilla.gecko.gfx.LayerView;
import static org.libreoffice.canvas.GraphicSelectionHandleCanvasElement.HandlePosition;
/** /**
* This class is responsible to draw and reposition the selection * This class is responsible to draw and reposition the selection
* rectangle. * rectangle.
*/ */
public class GraphicSelectionCanvasElement { public class GraphicSelectionCanvasElement {
private final Paint mGraphicSelectionPaint; private final Paint mPaint;
public RectF mRectangle = new RectF(); public RectF mRectangle = new RectF();
public RectF mScaledRectangle = new RectF(); public RectF mScaledRectangle = new RectF();
private RectF mDrawRectangle = new RectF(); private RectF mDrawRectangle = new RectF();
@ -31,17 +34,20 @@ public class GraphicSelectionCanvasElement {
private GraphicSelectionHandleCanvasElement mHandles[] = new GraphicSelectionHandleCanvasElement[8]; private GraphicSelectionHandleCanvasElement mHandles[] = new GraphicSelectionHandleCanvasElement[8];
private GraphicSelectionHandleCanvasElement mDragHandle = null; private GraphicSelectionHandleCanvasElement mDragHandle = null;
public GraphicSelectionCanvasElement(Paint graphicSelectionPaint) { public GraphicSelectionCanvasElement() {
mGraphicSelectionPaint = graphicSelectionPaint; mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(2);
mHandles[0] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[0] = new GraphicSelectionHandleCanvasElement(HandlePosition.TOP_LEFT);
mHandles[1] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[1] = new GraphicSelectionHandleCanvasElement(HandlePosition.TOP);
mHandles[2] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[2] = new GraphicSelectionHandleCanvasElement(HandlePosition.TOP_RIGHT);
mHandles[3] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[3] = new GraphicSelectionHandleCanvasElement(HandlePosition.LEFT);
mHandles[4] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[4] = new GraphicSelectionHandleCanvasElement(HandlePosition.RIGHT);
mHandles[5] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[5] = new GraphicSelectionHandleCanvasElement(HandlePosition.BOTTOM_LEFT);
mHandles[6] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[6] = new GraphicSelectionHandleCanvasElement(HandlePosition.BOTTOM);
mHandles[7] = new GraphicSelectionHandleCanvasElement(mGraphicSelectionPaint); mHandles[7] = new GraphicSelectionHandleCanvasElement(HandlePosition.BOTTOM_RIGHT);
} }
public void reposition(RectF scaledRectangle) { public void reposition(RectF scaledRectangle) {
@ -69,13 +75,9 @@ public class GraphicSelectionCanvasElement {
} }
public void draw(Canvas canvas) { public void draw(Canvas canvas) {
canvas.drawRect(mDrawRectangle, mGraphicSelectionPaint); canvas.drawRect(mDrawRectangle, mPaint);
for (GraphicSelectionHandleCanvasElement handle : mHandles) { for (GraphicSelectionHandleCanvasElement handle : mHandles) {
if (mType == DragType.MOVE || mType == DragType.EXTEND) { handle.draw(canvas);
handle.drawSelected(canvas);
} else {
handle.draw(canvas);
}
} }
} }
@ -85,6 +87,7 @@ public class GraphicSelectionCanvasElement {
for (GraphicSelectionHandleCanvasElement handle : mHandles) { for (GraphicSelectionHandleCanvasElement handle : mHandles) {
if (handle.contains(position.x, position.y)) { if (handle.contains(position.x, position.y)) {
mDragHandle = handle; mDragHandle = handle;
mDragHandle.select();
mType = DragType.EXTEND; mType = DragType.EXTEND;
sendGraphicSelectionStart(handle.mPosition); sendGraphicSelectionStart(handle.mPosition);
} }
@ -99,15 +102,47 @@ public class GraphicSelectionCanvasElement {
public void dragging(PointF position) { public void dragging(PointF position) {
if (mType == DragType.MOVE) { if (mType == DragType.MOVE) {
float deltaX = position.x - mStartDragPosition.x; float deltaX = position.x - mStartDragPosition.x;
float deltaY = position.y - mStartDragPosition.y; float deltaY = position.y - mStartDragPosition.y;
mDrawRectangle = new RectF(mScaledRectangle); mDrawRectangle = new RectF(mScaledRectangle);
mDrawRectangle.offset(deltaX, deltaY); mDrawRectangle.offset(deltaX, deltaY);
} else if (mType == DragType.EXTEND) { } else if (mType == DragType.EXTEND) {
mDrawRectangle = new RectF(mScaledRectangle); adaptDrawRectangle(position.x, position.y);
mDrawRectangle.union(position.x, position.y); }
}
private void adaptDrawRectangle(float x, float y) {
mDrawRectangle = new RectF(mScaledRectangle);
switch(mDragHandle.getHandlePosition()) {
case TOP_LEFT:
mDrawRectangle.left = x;
mDrawRectangle.top = y;
break;
case TOP:
mDrawRectangle.top = y;
break;
case TOP_RIGHT:
mDrawRectangle.right = x;
mDrawRectangle.top = y;
break;
case LEFT:
mDrawRectangle.left = x;
break;
case RIGHT:
mDrawRectangle.right = x;
break;
case BOTTOM_LEFT:
mDrawRectangle.left = x;
mDrawRectangle.bottom = y;
break;
case BOTTOM:
mDrawRectangle.bottom = y;
break;
case BOTTOM_RIGHT:
mDrawRectangle.right = x;
mDrawRectangle.bottom = y;
break;
} }
} }
@ -116,6 +151,8 @@ public class GraphicSelectionCanvasElement {
if (mDragHandle != null) { if (mDragHandle != null) {
point.x = mDragHandle.mPosition.x; point.x = mDragHandle.mPosition.x;
point.y = mDragHandle.mPosition.y; point.y = mDragHandle.mPosition.y;
mDragHandle.reset();
mDragHandle = null;
} else { } else {
point.x = mStartDragPosition.x; point.x = mStartDragPosition.x;
point.y = mStartDragPosition.y; point.y = mStartDragPosition.y;
@ -127,7 +164,6 @@ public class GraphicSelectionCanvasElement {
sendGraphicSelectionEnd(point); sendGraphicSelectionEnd(point);
mDrawRectangle = mScaledRectangle; mDrawRectangle = mScaledRectangle;
mDragHandle = null;
mType = DragType.NONE; mType = DragType.NONE;
} }
@ -147,6 +183,13 @@ public class GraphicSelectionCanvasElement {
} }
} }
public void reset() {
mDragHandle = null;
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
handle.reset();
}
}
public enum DragType { public enum DragType {
NONE, NONE,
MOVE, MOVE,

View File

@ -20,29 +20,39 @@ import android.graphics.RectF;
* touched. * touched.
*/ */
public class GraphicSelectionHandleCanvasElement { public class GraphicSelectionHandleCanvasElement {
private final HandlePosition mHandlePosition;
public PointF mPosition = new PointF(); public PointF mPosition = new PointF();
private float mRadius = 20.0f; private float mRadius = 20.0f;
private Paint mGraphicHandleFillPaint = new Paint(); private Paint mStrokePaint = new Paint();
private Paint mGraphicSelectionPaint = new Paint(); private Paint mFillPaint = new Paint();
private Paint mGraphicHandleSelectedFillPaint = new Paint(); private Paint mSelectedFillPaint = new Paint();
private RectF mHitRect = new RectF(); private RectF mHitRect = new RectF();
private boolean mSelected = false;
public GraphicSelectionHandleCanvasElement(Paint graphicSelectionPaint) { public GraphicSelectionHandleCanvasElement(HandlePosition position) {
mGraphicSelectionPaint = graphicSelectionPaint; mHandlePosition = position;
mGraphicHandleFillPaint.setStyle(Paint.Style.FILL); mStrokePaint.setStyle(Paint.Style.STROKE);
mGraphicHandleFillPaint.setColor(Color.WHITE); mStrokePaint.setColor(Color.BLACK);
mStrokePaint.setStrokeWidth(3);
mGraphicHandleSelectedFillPaint.setStyle(Paint.Style.FILL); mFillPaint.setStyle(Paint.Style.FILL);
mGraphicHandleSelectedFillPaint.setColor(Color.BLACK); mFillPaint.setColor(Color.WHITE);
mSelectedFillPaint.setStyle(Paint.Style.FILL);
mSelectedFillPaint.setColor(Color.BLUE);
}
public HandlePosition getHandlePosition() {
return mHandlePosition;
} }
public void draw(Canvas canvas) { public void draw(Canvas canvas) {
drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mGraphicSelectionPaint, mGraphicHandleFillPaint); if (mSelected) {
} drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mSelectedFillPaint);
} else {
public void drawSelected(Canvas canvas) { drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mFillPaint);
drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mGraphicSelectionPaint, mGraphicHandleSelectedFillPaint); }
} }
private void drawFilledCircle(Canvas canvas, float x, float y, float radius, Paint strokePaint, Paint fillPaint) { private void drawFilledCircle(Canvas canvas, float x, float y, float radius, Paint strokePaint, Paint fillPaint) {
@ -62,6 +72,25 @@ public class GraphicSelectionHandleCanvasElement {
public boolean contains(float x, float y) { public boolean contains(float x, float y) {
return mHitRect.contains(x, y); return mHitRect.contains(x, y);
} }
public void select() {
mSelected = true;
}
public void reset() {
mSelected = false;
}
public enum HandlePosition {
TOP_LEFT,
TOP,
TOP_RIGHT,
RIGHT,
BOTTOM_RIGHT,
BOTTOM,
BOTTOM_LEFT,
LEFT
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -49,8 +49,6 @@ public class TextCursorView extends View implements View.OnTouchListener {
private Paint mSelectionPaint = new Paint(); private Paint mSelectionPaint = new Paint();
private boolean mSelectionsVisible; private boolean mSelectionsVisible;
private Paint mGraphicSelectionPaint = new Paint();
private GraphicSelectionCanvasElement mGraphicSelection; private GraphicSelectionCanvasElement mGraphicSelection;
private boolean mGraphicSelectionVisible; private boolean mGraphicSelectionVisible;
@ -88,11 +86,7 @@ public class TextCursorView extends View implements View.OnTouchListener {
mSelectionPaint.setAlpha(50); mSelectionPaint.setAlpha(50);
mSelectionsVisible = false; mSelectionsVisible = false;
mGraphicSelectionPaint.setStyle(Paint.Style.STROKE); mGraphicSelection = new GraphicSelectionCanvasElement();
mGraphicSelectionPaint.setColor(Color.BLACK);
mGraphicSelectionPaint.setStrokeWidth(2);
mGraphicSelection = new GraphicSelectionCanvasElement(mGraphicSelectionPaint);
mGraphicSelectionVisible = false; mGraphicSelectionVisible = false;
@ -211,6 +205,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
public void showGraphicSelection() { public void showGraphicSelection() {
mGraphicSelectionVisible = true; mGraphicSelectionVisible = true;
mGraphicSelectionMove = false;
mGraphicSelection.reset();
invalidate(); invalidate();
} }