aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/HoverOverlay.java
blob: 2e7c559db0b7b04510a4d75d9f4f7af4496556c6 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.gle2;

import static com.android.ide.eclipse.adt.internal.editors.layout.gle2.SwtDrawingStyle.HOVER;
import static com.android.ide.eclipse.adt.internal.editors.layout.gle2.SwtDrawingStyle.HOVER_SELECTION;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;

import java.util.List;

/**
 * The {@link HoverOverlay} paints an optional hover on top of the layout,
 * highlighting the currently hovered view.
 */
public class HoverOverlay extends Overlay {
    private final LayoutCanvas mCanvas;

    /** Hover border color. Must be disposed, it's NOT a system color. */
    private Color mHoverStrokeColor;

    /** Hover fill color. Must be disposed, it's NOT a system color. */
    private Color mHoverFillColor;

    /** Hover border select color. Must be disposed, it's NOT a system color. */
    private Color mHoverSelectStrokeColor;

    /** Hover fill select color. Must be disposed, it's NOT a system color. */
    private Color mHoverSelectFillColor;

    /** Vertical scaling & scrollbar information. */
    private CanvasTransform mVScale;

    /** Horizontal scaling & scrollbar information. */
    private CanvasTransform mHScale;

    /**
     * Current mouse hover border rectangle. Null when there's no mouse hover.
     * The rectangle coordinates do not take account of the translation, which
     * must be applied to the rectangle when drawing.
     */
    private Rectangle mHoverRect;

    /**
     * Constructs a new {@link HoverOverlay} linked to the given view hierarchy.
     *
     * @param canvas the associated canvas
     * @param hScale The {@link CanvasTransform} to use to transfer horizontal layout
     *            coordinates to screen coordinates.
     * @param vScale The {@link CanvasTransform} to use to transfer vertical layout
     *            coordinates to screen coordinates.
     */
    public HoverOverlay(LayoutCanvas canvas, CanvasTransform hScale, CanvasTransform vScale) {
        mCanvas = canvas;
        mHScale = hScale;
        mVScale = vScale;
    }

    @Override
    public void create(Device device) {
        if (SwtDrawingStyle.HOVER.getStrokeColor() != null) {
            mHoverStrokeColor = new Color(device, SwtDrawingStyle.HOVER.getStrokeColor());
        }
        if (SwtDrawingStyle.HOVER.getFillColor() != null) {
            mHoverFillColor = new Color(device, SwtDrawingStyle.HOVER.getFillColor());
        }

        if (SwtDrawingStyle.HOVER_SELECTION.getStrokeColor() != null) {
            mHoverSelectStrokeColor = new Color(device,
                    SwtDrawingStyle.HOVER_SELECTION.getStrokeColor());
        }
        if (SwtDrawingStyle.HOVER_SELECTION.getFillColor() != null) {
            mHoverSelectFillColor = new Color(device,
                    SwtDrawingStyle.HOVER_SELECTION.getFillColor());
        }
    }

    @Override
    public void dispose() {
        if (mHoverStrokeColor != null) {
            mHoverStrokeColor.dispose();
            mHoverStrokeColor = null;
        }

        if (mHoverFillColor != null) {
            mHoverFillColor.dispose();
            mHoverFillColor = null;
        }

        if (mHoverSelectStrokeColor != null) {
            mHoverSelectStrokeColor.dispose();
            mHoverSelectStrokeColor = null;
        }

        if (mHoverSelectFillColor != null) {
            mHoverSelectFillColor.dispose();
            mHoverSelectFillColor = null;
        }
    }

    /**
     * Sets the hover rectangle. The coordinates of the rectangle are in layout
     * coordinates. The recipient is will own this rectangle.
     * <p/>
     * TODO: Consider switching input arguments to two {@link LayoutPoint}s so
     * we don't have ambiguity about the coordinate system of these input
     * parameters.
     * <p/>
     *
     * @param x The top left x coordinate, in layout coordinates, of the hover.
     * @param y The top left y coordinate, in layout coordinates, of the hover.
     * @param w The width of the hover (in layout coordinates).
     * @param h The height of the hover (in layout coordinates).
     */
    public void setHover(int x, int y, int w, int h) {
        mHoverRect = new Rectangle(x, y, w, h);
    }

    /**
     * Removes the hover for the next paint.
     */
    public void clearHover() {
        mHoverRect = null;
    }

    @Override
    public void paint(GC gc) {
        if (mHoverRect != null) {
            // Translate the hover rectangle (in canvas coordinates) to control
            // coordinates
            int x = mHScale.translate(mHoverRect.x);
            int y = mVScale.translate(mHoverRect.y);
            int w = mHScale.scale(mHoverRect.width);
            int h = mVScale.scale(mHoverRect.height);


            boolean hoverIsSelected = false;
            List<SelectionItem> selections = mCanvas.getSelectionManager().getSelections();
            for (SelectionItem item : selections) {
                if (mHoverRect.equals(item.getViewInfo().getSelectionRect())) {
                    hoverIsSelected = true;
                    break;
                }
            }

            Color stroke = hoverIsSelected ? mHoverSelectStrokeColor : mHoverStrokeColor;
            Color fill = hoverIsSelected ? mHoverSelectFillColor : mHoverFillColor;

            if (stroke != null) {
                int oldAlpha = gc.getAlpha();
                gc.setForeground(stroke);
                gc.setLineStyle(hoverIsSelected ?
                        HOVER_SELECTION.getLineStyle() : HOVER.getLineStyle());
                gc.setAlpha(hoverIsSelected ?
                        HOVER_SELECTION.getStrokeAlpha() : HOVER.getStrokeAlpha());
                gc.drawRectangle(x, y, w, h);
                gc.setAlpha(oldAlpha);
            }

            if (fill != null) {
                int oldAlpha = gc.getAlpha();
                gc.setAlpha(hoverIsSelected ?
                        HOVER_SELECTION.getFillAlpha() : HOVER.getFillAlpha());
                gc.setBackground(fill);
                gc.fillRectangle(x, y, w, h);
                gc.setAlpha(oldAlpha);
            }
        }
    }
}