android: add Cursor class for drawing the cursor to the canvas
Change-Id: I5d8191ffb3d3dd3d3ab11c757a3b7d5dc3fb2e82
This commit is contained in:
committed by
Miklos Vajna
parent
4e0d2fcdda
commit
6d9af88998
@@ -0,0 +1,38 @@
|
||||
package org.libreoffice.canvas;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RectF;
|
||||
|
||||
public class Cursor extends CommonCanvasElement {
|
||||
private static final float CURSOR_WIDTH = 2f;
|
||||
private final Paint mCursorPaint = new Paint();
|
||||
public RectF mPosition = new RectF();
|
||||
public RectF mScaledPosition = new RectF();
|
||||
public int mAlpha = 0;
|
||||
|
||||
public Cursor() {
|
||||
mCursorPaint.setColor(Color.BLACK);
|
||||
mCursorPaint.setAlpha(0xFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onHitTest(float x, float y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas) {
|
||||
canvas.drawRect(mScaledPosition, mCursorPaint);
|
||||
}
|
||||
|
||||
public void reposition(RectF rect) {
|
||||
mScaledPosition = rect;
|
||||
mScaledPosition.right = mScaledPosition.left + CURSOR_WIDTH;
|
||||
}
|
||||
|
||||
public void cycleAlpha() {
|
||||
mCursorPaint.setAlpha(mCursorPaint.getAlpha() == 0 ? 0xFF : 0);
|
||||
}
|
||||
}
|
@@ -23,6 +23,7 @@ import android.view.View;
|
||||
|
||||
import org.libreoffice.LOKitShell;
|
||||
import org.libreoffice.R;
|
||||
import org.libreoffice.canvas.Cursor;
|
||||
import org.libreoffice.canvas.GraphicSelection;
|
||||
import org.libreoffice.canvas.SelectionHandle;
|
||||
import org.libreoffice.canvas.SelectionHandleEnd;
|
||||
@@ -40,15 +41,10 @@ import java.util.List;
|
||||
*/
|
||||
public class TextCursorView extends View implements View.OnTouchListener {
|
||||
private static final String LOGTAG = TextCursorView.class.getSimpleName();
|
||||
private static final float CURSOR_WIDTH = 2f;
|
||||
|
||||
private static final int CURSOR_BLINK_TIME = 500;
|
||||
|
||||
private boolean mInitialized = false;
|
||||
private RectF mCursorPosition = new RectF();
|
||||
private RectF mCursorScaledPosition = new RectF();
|
||||
private Paint mCursorPaint = new Paint();
|
||||
private int mCursorAlpha = 0;
|
||||
private boolean mCursorVisible;
|
||||
|
||||
private List<RectF> mSelections = new ArrayList<RectF>();
|
||||
private List<RectF> mScaledSelections = new ArrayList<RectF>();
|
||||
@@ -65,6 +61,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
private SelectionHandle mHandleStart;
|
||||
private SelectionHandle mHandleEnd;
|
||||
|
||||
private Cursor mCursor;
|
||||
|
||||
private SelectionHandle mDragHandle = null;
|
||||
|
||||
public TextCursorView(Context context) {
|
||||
@@ -87,9 +85,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
setOnTouchListener(this);
|
||||
mLayerView = layerView;
|
||||
|
||||
mCursorPaint.setColor(Color.BLACK);
|
||||
mCursorPaint.setAlpha(0xFF);
|
||||
mCursorVisible = false;
|
||||
mCursor = new Cursor();
|
||||
mCursor.setVisible(false);
|
||||
|
||||
mSelectionPaint.setColor(Color.BLUE);
|
||||
mSelectionPaint.setAlpha(50);
|
||||
@@ -113,10 +110,10 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
* @param position - new position of the cursor
|
||||
*/
|
||||
public void changeCursorPosition(RectF position) {
|
||||
if (RectUtils.fuzzyEquals(mCursorPosition, position)) {
|
||||
if (RectUtils.fuzzyEquals(mCursor.mPosition, position)) {
|
||||
return;
|
||||
}
|
||||
mCursorPosition = position;
|
||||
mCursor.mPosition = position;
|
||||
|
||||
ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
|
||||
repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
|
||||
@@ -149,10 +146,10 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
}
|
||||
|
||||
public void repositionWithViewport(float x, float y, float zoom) {
|
||||
mCursorScaledPosition = convertToScreen(mCursorPosition, x, y, zoom);
|
||||
mCursorScaledPosition.right = mCursorScaledPosition.left + CURSOR_WIDTH;
|
||||
RectF rect = convertToScreen(mCursor.mPosition, x, y, zoom);
|
||||
mCursor.reposition(rect);
|
||||
|
||||
RectF rect = convertToScreen(mHandleMiddle.mDocumentPosition, x, y, zoom);
|
||||
rect = convertToScreen(mHandleMiddle.mDocumentPosition, x, y, zoom);
|
||||
mHandleMiddle.reposition(rect.left, rect.bottom);
|
||||
|
||||
rect = convertToScreen(mHandleStart.mDocumentPosition, x, y, zoom);
|
||||
@@ -189,9 +186,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
||||
if (mCursorVisible) {
|
||||
canvas.drawRect(mCursorScaledPosition, mCursorPaint);
|
||||
}
|
||||
mCursor.draw(canvas);
|
||||
|
||||
mHandleMiddle.draw(canvas);
|
||||
mHandleStart.draw(canvas);
|
||||
mHandleEnd.draw(canvas);
|
||||
@@ -211,8 +207,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
*/
|
||||
private Runnable cursorAnimation = new Runnable() {
|
||||
public void run() {
|
||||
if (mCursorVisible) {
|
||||
mCursorPaint.setAlpha(mCursorPaint.getAlpha() == 0 ? 0xFF : 0);
|
||||
if (mCursor.isVisible()) {
|
||||
mCursor.cycleAlpha();
|
||||
invalidate();
|
||||
}
|
||||
postDelayed(cursorAnimation, CURSOR_BLINK_TIME);
|
||||
@@ -223,8 +219,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
* Show the cursor on the view.
|
||||
*/
|
||||
public void showCursor() {
|
||||
if (!mCursorVisible) {
|
||||
mCursorVisible = true;
|
||||
if (!mCursor.isVisible()) {
|
||||
mCursor.setVisible(true);
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
@@ -233,8 +229,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
||||
* Hide the cursor.
|
||||
*/
|
||||
public void hideCursor() {
|
||||
if (mCursorVisible) {
|
||||
mCursorVisible = false;
|
||||
if (mCursor.isVisible()) {
|
||||
mCursor.setVisible(false);
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user