summaryrefslogtreecommitdiff
path: root/android/experimental/LOAndroid3/src/java/org/libreoffice/canvas/GraphicSelectionCanvasElement.java
blob: 39b2fe0e38a84cee33e99e14d3e912d262a71a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.canvas;

import android.graphics.Canvas;
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.
 */
public class GraphicSelectionCanvasElement {
    private final Paint mGraphicSelectionPaint;
    public RectF mRectangle = new RectF();
    public RectF mScaledRectangle = new RectF();
    private RectF mDrawRectangle = new RectF();
    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(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;
    }

    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) {
            mDrawRectangle = new RectF(mScaledRectangle);
            mDrawRectangle.union(position.x, position.y);
        }
    }

    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 {
        NONE,
        MOVE,
        EXTEND
    }
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */