android: Hit test for handles, show resize rect

Change-Id: Ibf9f98968fd9007be6dc5481d1652923ecb98b89
This commit is contained in:
Tomaž Vajngerl 2015-03-18 18:12:02 +09:00 committed by Miklos Vajna
parent a2675cb377
commit 892d6813ad
2 changed files with 111 additions and 31 deletions

View File

@ -12,28 +12,57 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import org.mozilla.gecko.gfx.RectUtils;
/**
* This class is responsible to draw the selection handles, track the handle
* position and perform a hit test to determine if the selection handle was
* touched.
*/
public class DrawElementHandle {
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 RectF mHitRect = new RectF();
public DrawElementHandle(Paint graphicSelectionPaint) {
mGraphicSelectionPaint = graphicSelectionPaint;
mGraphicHandleFillPaint.setStyle(Paint.Style.FILL);
mGraphicHandleFillPaint.setColor(Color.WHITE);
mGraphicHandleSelectedFillPaint.setStyle(Paint.Style.FILL);
mGraphicHandleSelectedFillPaint.setColor(Color.BLACK);
}
public void draw(Canvas canvas) {
canvas.drawCircle(mPosition.x, mPosition.y, mRadius, mGraphicHandleFillPaint);
canvas.drawCircle(mPosition.x, mPosition.y, mRadius, mGraphicSelectionPaint);
drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mGraphicSelectionPaint, mGraphicHandleFillPaint);
}
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) {
canvas.drawCircle(x, y, radius, fillPaint);
canvas.drawCircle(x, y, radius, strokePaint);
}
public void reposition(float x, float y) {
mPosition.x = x;
mPosition.y = y;
mHitRect.left = mPosition.x - mRadius * 2;
mHitRect.right = mPosition.x + mRadius * 2;
mHitRect.top = mPosition.y - mRadius * 2;
mHitRect.bottom = mPosition.y + mRadius * 2;
}
public boolean contains(float x, float y) {
return mHitRect.contains(x, y);
}
}

View File

@ -52,7 +52,6 @@ public class TextCursorView extends View implements View.OnTouchListener {
private RectF mGraphicScaledSelection = new RectF();
private Paint mGraphicSelectionPaint = new Paint();
private boolean mGraphicSelectionVisible;
private PointF mTouchStart = new PointF();
private PointF mDeltaPoint = new PointF();
@ -60,6 +59,7 @@ public class TextCursorView extends View implements View.OnTouchListener {
private LayerView mLayerView;
private DrawElementHandle mHandles[] = new DrawElementHandle[8];
private DrawElementHandle mDragHandle = null;
public TextCursorView(Context context) {
super(context);
@ -189,16 +189,31 @@ public class TextCursorView extends View implements View.OnTouchListener {
}
}
if (mGraphicSelectionVisible) {
if (mGraphicSelectionMove) {
for (DrawElementHandle handle : mHandles) {
if (mDragHandle == handle) {
handle.drawSelected(canvas);
} else {
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);
}
if (mGraphicSelectionMove) {
RectF one = new RectF(mGraphicScaledSelection);
one.offset(mDeltaPoint.x, mDeltaPoint.y);
canvas.drawRect(one, mGraphicSelectionPaint);
}
}
}
@ -256,31 +271,19 @@ public class TextCursorView extends View implements View.OnTouchListener {
case MotionEvent.ACTION_DOWN: {
mTouchStart.x = event.getX();
mTouchStart.y = event.getY();
if (mGraphicSelectionVisible && mGraphicScaledSelection.contains(mTouchStart.x, mTouchStart.y)) {
mGraphicSelectionMove = true;
PointF documentPoint = mLayerView.getLayerClient().convertViewPointToLayerPoint(mTouchStart);
Log.i(LOGTAG, "Down: " + documentPoint);
LOKitShell.sendTouchEvent("GraphicSelectionStart", documentPoint);
return true;
if (mGraphicSelectionVisible) {
return checkIfGraphicSelectionWasHit();
}
}
case MotionEvent.ACTION_UP: {
if (mGraphicSelectionMove) {
mGraphicSelectionMove = false;
mTouchStart.offset(mDeltaPoint.x, mDeltaPoint.y);
PointF documentPoint = mLayerView.getLayerClient().convertViewPointToLayerPoint(mTouchStart);
Log.i(LOGTAG, "Up: " + documentPoint);
LOKitShell.sendTouchEvent("GraphicSelectionEnd", documentPoint);
mTouchStart.x = 0.0f;
mTouchStart.y = 0.0f;
mDeltaPoint.x = 0.0f;
mDeltaPoint.y = 0.0f;
invalidate();
return true;
if (mGraphicSelectionVisible && mGraphicSelectionMove) {
mDeltaPoint.x = event.getX() - mTouchStart.x;
mDeltaPoint.y = event.getY() - mTouchStart.y;
return stopGraphicSelection();
}
}
case MotionEvent.ACTION_MOVE: {
if (mGraphicSelectionMove) {
if (mGraphicSelectionVisible && mGraphicSelectionMove) {
mDeltaPoint.x = event.getX() - mTouchStart.x;
mDeltaPoint.y = event.getY() - mTouchStart.y;
invalidate();
@ -290,6 +293,54 @@ public class TextCursorView extends View implements View.OnTouchListener {
}
return false;
}
private boolean checkIfGraphicSelectionWasHit() {
// Check if handle was hit
mDragHandle = null;
for (DrawElementHandle handle : mHandles) {
if (handle.contains(mTouchStart.x, mTouchStart.y)) {
mDragHandle = handle;
mGraphicSelectionMove = true;
sendGraphicSelectionStart(handle.mPosition);
return true;
}
}
// Check if inside graphic selection was hit
if (mGraphicScaledSelection.contains(mTouchStart.x, mTouchStart.y)) {
mGraphicSelectionMove = true;
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);
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: */