android: move handles into GraphicSelectionCanvasElement
Change-Id: Ib372b4fac35ae77bf06c65913b142ae3e80b0eb5
This commit is contained in:
committed by
Miklos Vajna
parent
0bc8e678a1
commit
aa5a78cf8b
@@ -13,6 +13,9 @@ import android.graphics.Paint;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.RectF;
|
||||
|
||||
import org.libreoffice.LOKitShell;
|
||||
import org.mozilla.gecko.gfx.LayerView;
|
||||
|
||||
/**
|
||||
* This class is responsible to draw and reposition the selection
|
||||
* rectangle.
|
||||
@@ -25,25 +28,72 @@ public class GraphicSelectionCanvasElement {
|
||||
private DragType mType = DragType.NONE;
|
||||
private PointF mStartDragPosition;
|
||||
|
||||
private GraphicSelectionHandleCanvasElement mHandles[] = new GraphicSelectionHandleCanvasElement[8];
|
||||
private GraphicSelectionHandleCanvasElement mDragHandle = null;
|
||||
|
||||
public GraphicSelectionCanvasElement(Paint graphicSelectionPaint) {
|
||||
mGraphicSelectionPaint = graphicSelectionPaint;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void reposition(RectF scaledRectangle) {
|
||||
mScaledRectangle = scaledRectangle;
|
||||
mDrawRectangle = scaledRectangle;
|
||||
|
||||
mHandles[0].reposition(scaledRectangle.left, scaledRectangle.top);
|
||||
mHandles[1].reposition(scaledRectangle.centerX(), scaledRectangle.top);
|
||||
mHandles[2].reposition(scaledRectangle.right, scaledRectangle.top);
|
||||
mHandles[3].reposition(scaledRectangle.left, scaledRectangle.centerY());
|
||||
mHandles[4].reposition(scaledRectangle.right, scaledRectangle.centerY());
|
||||
mHandles[5].reposition(scaledRectangle.left, scaledRectangle.bottom);
|
||||
mHandles[6].reposition(scaledRectangle.centerX(), scaledRectangle.bottom);
|
||||
mHandles[7].reposition(scaledRectangle.right, scaledRectangle.bottom);
|
||||
}
|
||||
|
||||
public boolean contains(float x, float y) {
|
||||
// Check if handle was hit
|
||||
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
|
||||
if (handle.contains(x, y)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return mScaledRectangle.contains(x, y);
|
||||
}
|
||||
|
||||
public void draw(Canvas canvas) {
|
||||
canvas.drawRect(mDrawRectangle, mGraphicSelectionPaint);
|
||||
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
|
||||
if (mType == DragType.MOVE || mType == DragType.EXTEND) {
|
||||
handle.drawSelected(canvas);
|
||||
} else {
|
||||
handle.draw(canvas);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dragStart(DragType type, PointF position) {
|
||||
mType = type;
|
||||
public void dragStart(PointF position) {
|
||||
mDragHandle = null;
|
||||
mType = DragType.NONE;
|
||||
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
|
||||
if (handle.contains(position.x, position.y)) {
|
||||
mDragHandle = handle;
|
||||
mType = DragType.EXTEND;
|
||||
sendGraphicSelectionStart(handle.mPosition);
|
||||
}
|
||||
}
|
||||
if (mDragHandle == null) {
|
||||
mType = DragType.MOVE;
|
||||
sendGraphicSelectionStart(position);
|
||||
}
|
||||
|
||||
mStartDragPosition = position;
|
||||
}
|
||||
|
||||
@@ -61,9 +111,40 @@ public class GraphicSelectionCanvasElement {
|
||||
}
|
||||
}
|
||||
|
||||
public void dragEnd() {
|
||||
mType = DragType.NONE;
|
||||
public void dragEnd(PointF position) {
|
||||
PointF point = new PointF();
|
||||
if (mDragHandle != null) {
|
||||
point.x = mDragHandle.mPosition.x;
|
||||
point.y = mDragHandle.mPosition.y;
|
||||
} else {
|
||||
point.x = mStartDragPosition.x;
|
||||
point.y = mStartDragPosition.y;
|
||||
}
|
||||
float deltaX = position.x - mStartDragPosition.x;
|
||||
float deltaY = position.y - mStartDragPosition.y;
|
||||
point.offset(deltaX, deltaY);
|
||||
|
||||
sendGraphicSelectionEnd(point);
|
||||
|
||||
mDrawRectangle = mScaledRectangle;
|
||||
mDragHandle = null;
|
||||
mType = DragType.NONE;
|
||||
}
|
||||
|
||||
private void sendGraphicSelectionStart(PointF screenPosition) {
|
||||
LayerView layerView = LOKitShell.getLayerView();
|
||||
if (layerView != null) {
|
||||
PointF documentPoint = layerView.getLayerClient().convertViewPointToLayerPoint(screenPosition);
|
||||
LOKitShell.sendTouchEvent("GraphicSelectionStart", documentPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendGraphicSelectionEnd(PointF screenPosition) {
|
||||
LayerView layerView = LOKitShell.getLayerView();
|
||||
if (layerView != null) {
|
||||
PointF documentPoint = layerView.getLayerClient().convertViewPointToLayerPoint(screenPosition);
|
||||
LOKitShell.sendTouchEvent("GraphicSelectionEnd", documentPoint);
|
||||
}
|
||||
}
|
||||
|
||||
public enum DragType {
|
||||
|
@@ -53,17 +53,11 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
|
||||
private GraphicSelectionCanvasElement mGraphicSelection;
|
||||
|
||||
private PointF mTouchStart = new PointF();
|
||||
private PointF mDeltaPoint = new PointF();
|
||||
|
||||
private boolean mGraphicSelectionVisible;
|
||||
private boolean mGraphicSelectionMove = false;
|
||||
|
||||
private LayerView mLayerView;
|
||||
|
||||
private GraphicSelectionHandleCanvasElement mHandles[] = new GraphicSelectionHandleCanvasElement[8];
|
||||
private GraphicSelectionHandleCanvasElement mDragHandle = null;
|
||||
|
||||
public TextCursorView(Context context) {
|
||||
super(context);
|
||||
initialize();
|
||||
@@ -102,15 +96,6 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
|
||||
mGraphicSelectionVisible = false;
|
||||
|
||||
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);
|
||||
|
||||
postDelayed(cursorAnimation, CURSOR_BLINK_TIME);
|
||||
|
||||
mInitialized = true;
|
||||
@@ -169,15 +154,6 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
RectF scaledGraphicSelection = convertPosition(mGraphicSelection.mRectangle, x, y, zoom);
|
||||
mGraphicSelection.reposition(scaledGraphicSelection);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -200,20 +176,6 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
}
|
||||
if (mGraphicSelectionVisible) {
|
||||
mGraphicSelection.draw(canvas);
|
||||
|
||||
if (mGraphicSelectionMove) {
|
||||
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
|
||||
if (mDragHandle == handle) {
|
||||
handle.drawSelected(canvas);
|
||||
} else {
|
||||
handle.draw(canvas);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
|
||||
handle.draw(canvas);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,32 +221,30 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent event) {
|
||||
if (mLayerView == null) {
|
||||
mLayerView = LOKitShell.getLayerView();
|
||||
if (mLayerView == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
switch (event.getActionMasked()) {
|
||||
case MotionEvent.ACTION_DOWN: {
|
||||
mTouchStart.x = event.getX();
|
||||
mTouchStart.y = event.getY();
|
||||
if (mGraphicSelectionVisible) {
|
||||
return checkIfGraphicSelectionWasHit();
|
||||
// Check if inside graphic selection was hit
|
||||
PointF startPosition = new PointF(event.getX(), event.getY());
|
||||
if (mGraphicSelection.contains(startPosition.x, startPosition.y)) {
|
||||
mGraphicSelectionMove = true;
|
||||
mGraphicSelection.dragStart(startPosition);
|
||||
invalidate();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MotionEvent.ACTION_UP: {
|
||||
if (mGraphicSelectionVisible && mGraphicSelectionMove) {
|
||||
mDeltaPoint.x = event.getX() - mTouchStart.x;
|
||||
mDeltaPoint.y = event.getY() - mTouchStart.y;
|
||||
return stopGraphicSelection();
|
||||
mGraphicSelectionMove = false;
|
||||
mGraphicSelection.dragEnd(new PointF(event.getX(), event.getY()));
|
||||
invalidate();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
case MotionEvent.ACTION_MOVE: {
|
||||
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;
|
||||
@@ -293,59 +253,6 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean checkIfGraphicSelectionWasHit() {
|
||||
// Check if handle was hit
|
||||
mDragHandle = null;
|
||||
for (GraphicSelectionHandleCanvasElement handle : mHandles) {
|
||||
if (handle.contains(mTouchStart.x, mTouchStart.y)) {
|
||||
mDragHandle = handle;
|
||||
mGraphicSelectionMove = true;
|
||||
mGraphicSelection.dragStart(GraphicSelectionCanvasElement.DragType.EXTEND, mTouchStart);
|
||||
sendGraphicSelectionStart(handle.mPosition);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Check if inside graphic selection was hit
|
||||
if (mGraphicSelection.contains(mTouchStart.x, mTouchStart.y)) {
|
||||
mGraphicSelectionMove = true;
|
||||
mGraphicSelection.dragStart(GraphicSelectionCanvasElement.DragType.MOVE, mTouchStart);
|
||||
sendGraphicSelectionStart(mTouchStart);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean stopGraphicSelection() {
|
||||
mGraphicSelectionMove = false;
|
||||
|
||||
PointF point = new PointF();
|
||||
if (mDragHandle != null) {
|
||||
point.x = mDragHandle.mPosition.x;
|
||||
point.y = mDragHandle.mPosition.y;
|
||||
} else {
|
||||
point.x = mTouchStart.x;
|
||||
point.y = mTouchStart.y;
|
||||
}
|
||||
point.offset(mDeltaPoint.x, mDeltaPoint.y);
|
||||
sendGraphicSelectionEnd(point);
|
||||
|
||||
mGraphicSelection.dragEnd();
|
||||
invalidate();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void sendGraphicSelectionStart(PointF screenPosition) {
|
||||
PointF documentPoint = mLayerView.getLayerClient().convertViewPointToLayerPoint(screenPosition);
|
||||
Log.i(LOGTAG, "Selection Start P: " + documentPoint + " : " + mGraphicSelection);
|
||||
LOKitShell.sendTouchEvent("GraphicSelectionStart", documentPoint);
|
||||
}
|
||||
|
||||
private void sendGraphicSelectionEnd(PointF screenPosition) {
|
||||
PointF documentPoint = mLayerView.getLayerClient().convertViewPointToLayerPoint(screenPosition);
|
||||
Log.i(LOGTAG, "Selection End P: " + documentPoint + " : " + mGraphicSelection);
|
||||
LOKitShell.sendTouchEvent("GraphicSelectionEnd", documentPoint);
|
||||
}
|
||||
}
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||
|
Reference in New Issue
Block a user