summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/libreoffice/canvas/GraphicSelectionHandle.java
blob: 68b445af6f9f7ebb9abeada67590a548c3a49a82 (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
/* -*- 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.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;

/**
 * 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 GraphicSelectionHandle extends CommonCanvasElement {
    /**
     * The factor used to inflate the hit area.
     */
    private final float HIT_AREA_INFLATE_FACTOR = 1.75f;

    private final HandlePosition mHandlePosition;
    public PointF mPosition = new PointF();
    private float mRadius = 20.0f;
    private Paint mStrokePaint = new Paint();
    private Paint mFillPaint = new Paint();
    private Paint mSelectedFillPaint = new Paint();
    private RectF mHitRect = new RectF();
    private boolean mSelected = false;

    /**
     * Construct the handle - set the handle position on the selection.
     * @param position - the handle position on the selection
     */
    public GraphicSelectionHandle(HandlePosition position) {
        mHandlePosition = position;

        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setColor(Color.GRAY);
        mStrokePaint.setStrokeWidth(3);
        mStrokePaint.setAntiAlias(true);

        mFillPaint.setStyle(Paint.Style.FILL);
        mFillPaint.setColor(Color.WHITE);
        mFillPaint.setAlpha(200);
        mFillPaint.setAntiAlias(true);

        mSelectedFillPaint.setStyle(Paint.Style.FILL);
        mSelectedFillPaint.setColor(Color.GRAY);
        mSelectedFillPaint.setAlpha(200);
        mSelectedFillPaint.setAntiAlias(true);
    }

    /**
     * The position of the handle.
     * @return
     */
    public HandlePosition getHandlePosition() {
        return mHandlePosition;
    }

    /**
     * Draws the handle to the canvas.
     *
     * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
     */
    @Override
    public void onDraw(Canvas canvas) {
        if (mSelected) {
            drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mSelectedFillPaint);
        } else {
            drawFilledCircle(canvas, mPosition.x, mPosition.y, mRadius, mStrokePaint, mFillPaint);
        }
    }

    /**
     * Draw a filled and stroked circle to the canvas.
     */
    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);
    }

    /**
     * Viewport has changed, reposition the handle to the input coordinates.
     */
    public void reposition(float x, float y) {
        mPosition.x = x;
        mPosition.y = y;

        // inflate the radius by HIT_AREA_INFLATE_FACTOR
        float inflatedRadius = mRadius * HIT_AREA_INFLATE_FACTOR;

        // reposition the hit area rectangle
        mHitRect.left = mPosition.x - inflatedRadius;
        mHitRect.right = mPosition.x + inflatedRadius;
        mHitRect.top = mPosition.y - inflatedRadius;
        mHitRect.bottom = mPosition.y + inflatedRadius;
    }

    /**
     * Hit test for the handle.
     * @see org.libreoffice.canvas.CanvasElement#draw(android.graphics.Canvas)
     */
    @Override
    public boolean onHitTest(float x, float y) {
        return mHitRect.contains(x, y);
    }

    /**
     * Mark the handle as selected.
     */
    public void select() {
        mSelected = true;
    }

    /**
     * Reset the selection for the handle.
     */
    public void reset() {
        mSelected = false;
    }

    /**
     * All possible handle positions. The selection rectangle has 8 possible
     * handles.
     */
    public enum HandlePosition {
        TOP_LEFT,
        TOP,
        TOP_RIGHT,
        RIGHT,
        BOTTOM_RIGHT,
        BOTTOM,
        BOTTOM_LEFT,
        LEFT
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */