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

View File

@ -20,29 +20,39 @@ import android.graphics.RectF;
* touched.
*/
public class GraphicSelectionHandleCanvasElement {
private final HandlePosition mHandlePosition;
public PointF mPosition = new PointF();
private float mRadius = 20.0f;
private Paint mGraphicHandleFillPaint = new Paint();
private Paint mGraphicSelectionPaint = new Paint();
private Paint mGraphicHandleSelectedFillPaint = new Paint();
private Paint mStrokePaint = new Paint();
private Paint mFillPaint = new Paint();
private Paint mSelectedFillPaint = new Paint();
private RectF mHitRect = new RectF();
private boolean mSelected = false;
public GraphicSelectionHandleCanvasElement(Paint graphicSelectionPaint) {
mGraphicSelectionPaint = graphicSelectionPaint;
public GraphicSelectionHandleCanvasElement(HandlePosition position) {
mHandlePosition = position;
mGraphicHandleFillPaint.setStyle(Paint.Style.FILL);
mGraphicHandleFillPaint.setColor(Color.WHITE);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setColor(Color.BLACK);
mStrokePaint.setStrokeWidth(3);
mGraphicHandleSelectedFillPaint.setStyle(Paint.Style.FILL);
mGraphicHandleSelectedFillPaint.setColor(Color.BLACK);
mFillPaint.setStyle(Paint.Style.FILL);
mFillPaint.setColor(Color.WHITE);
mSelectedFillPaint.setStyle(Paint.Style.FILL);
mSelectedFillPaint.setColor(Color.BLUE);
}
public HandlePosition getHandlePosition() {
return mHandlePosition;
}
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 {
drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mFillPaint);
}
public void drawSelected(Canvas canvas) {
drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mGraphicSelectionPaint, mGraphicHandleSelectedFillPaint);
}
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) {
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: */

View File

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