aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/relative/GuidelinePainter.java
blob: 2fe74768f4089952eb63ed34ebc6a2b630d06243 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2011 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.common.layout.relative;

import static com.android.SdkConstants.ATTR_LAYOUT_MARGIN_BOTTOM;
import static com.android.SdkConstants.ATTR_LAYOUT_MARGIN_LEFT;
import static com.android.SdkConstants.ATTR_LAYOUT_MARGIN_RIGHT;
import static com.android.SdkConstants.ATTR_LAYOUT_MARGIN_TOP;
import static com.android.SdkConstants.ATTR_LAYOUT_RESOURCE_PREFIX;
import static com.android.SdkConstants.ID_PREFIX;
import static com.android.SdkConstants.NEW_ID_PREFIX;

import com.android.annotations.NonNull;
import com.android.ide.common.api.DrawingStyle;
import com.android.ide.common.api.DropFeedback;
import com.android.ide.common.api.IFeedbackPainter;
import com.android.ide.common.api.IGraphics;
import com.android.ide.common.api.INode;
import com.android.ide.common.api.Point;
import com.android.ide.common.api.Rect;
import com.android.ide.common.api.SegmentType;
import com.android.ide.common.layout.relative.DependencyGraph.Constraint;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * The {@link GuidelinePainter} is responsible for painting guidelines during an operation
 * which uses a {@link GuidelineHandler} such as a resize operation.
 */
public final class GuidelinePainter implements IFeedbackPainter {
    // ---- Implements IFeedbackPainter ----
    @Override
    public void paint(@NonNull IGraphics gc, @NonNull INode node, @NonNull DropFeedback feedback) {
        GuidelineHandler state = (GuidelineHandler) feedback.userData;

        for (INode dragged : state.mDraggedNodes) {
            gc.useStyle(DrawingStyle.DRAGGED);
            Rect bounds = dragged.getBounds();
            if (bounds.isValid()) {
                gc.fillRect(bounds);
            }
        }

        Set<INode> horizontalDeps = state.mHorizontalDeps;
        Set<INode> verticalDeps = state.mVerticalDeps;
        Set<INode> deps = new HashSet<INode>(horizontalDeps.size() + verticalDeps.size());
        deps.addAll(horizontalDeps);
        deps.addAll(verticalDeps);
        if (deps.size() > 0) {
            gc.useStyle(DrawingStyle.DEPENDENCY);
            for (INode n : deps) {
                // Don't highlight the selected nodes themselves
                if (state.mDraggedNodes.contains(n)) {
                    continue;
                }
                Rect bounds = n.getBounds();
                gc.fillRect(bounds);
            }
        }

        if (state.mBounds != null) {
            if (state instanceof MoveHandler) {
                gc.useStyle(DrawingStyle.DROP_PREVIEW);
            } else {
                // Resizing
                if (state.haveSuggestions()) {
                    gc.useStyle(DrawingStyle.RESIZE_PREVIEW);
                } else {
                    gc.useStyle(DrawingStyle.RESIZE_FAIL);
                }
            }
            gc.drawRect(state.mBounds);

            // Draw baseline preview too
            if (feedback.dragBaseline != -1) {
                int y = state.mBounds.y + feedback.dragBaseline;
                gc.drawLine(state.mBounds.x, y, state.mBounds.x2(), y);
            }
        }

        List<String> strings = new ArrayList<String>();

        showMatch(gc, state.mCurrentLeftMatch, state, strings,
                state.mLeftMargin, ATTR_LAYOUT_MARGIN_LEFT);
        showMatch(gc, state.mCurrentRightMatch, state, strings,
                state.mRightMargin, ATTR_LAYOUT_MARGIN_RIGHT);
        showMatch(gc, state.mCurrentTopMatch, state, strings,
                state.mTopMargin, ATTR_LAYOUT_MARGIN_TOP);
        showMatch(gc, state.mCurrentBottomMatch, state, strings,
                state.mBottomMargin, ATTR_LAYOUT_MARGIN_BOTTOM);

        if (strings.size() > 0) {
            // Update the drag tooltip
            StringBuilder sb = new StringBuilder(200);
            for (String s : strings) {
                if (sb.length() > 0) {
                    sb.append('\n');
                }
                sb.append(s);
            }
            feedback.tooltip = sb.toString();

            // Set the tooltip orientation to ensure that it does not interfere with
            // the constraint arrows
            if (state.mCurrentLeftMatch != null) {
                feedback.tooltipX = SegmentType.RIGHT;
            } else if (state.mCurrentRightMatch != null) {
                feedback.tooltipX = SegmentType.LEFT;
            }
            if (state.mCurrentTopMatch != null) {
                feedback.tooltipY = SegmentType.BOTTOM;
            } else if (state.mCurrentBottomMatch != null) {
                feedback.tooltipY = SegmentType.TOP;
            }
        } else {
            feedback.tooltip = null;
        }

        if (state.mHorizontalCycle != null) {
            paintCycle(gc, state, state.mHorizontalCycle);
        }
        if (state.mVerticalCycle != null) {
            paintCycle(gc, state, state.mVerticalCycle);
        }
    }

    /** Paints a particular match constraint */
    private void showMatch(IGraphics gc, Match m, GuidelineHandler state, List<String> strings,
            int margin, String marginAttribute) {
        if (m == null) {
            return;
        }
        ConstraintPainter.paintConstraint(gc, state.mBounds, m);

        // Display the constraint. Remove the @id/ and @+id/ prefixes to make the text
        // shorter and easier to read. This doesn't use stripPrefix() because the id is
        // usually not a prefix of the value (for example, 'layout_alignBottom=@+id/foo').
        String constraint = m.getConstraint(false /* generateId */);
        String description = constraint.replace(NEW_ID_PREFIX, "").replace(ID_PREFIX, "");
        if (description.startsWith(ATTR_LAYOUT_RESOURCE_PREFIX)) {
            description = description.substring(ATTR_LAYOUT_RESOURCE_PREFIX.length());
        }
        if (margin > 0) {
            int dp = state.getRulesEngine().pxToDp(margin);
            description = String.format("%1$s, margin=%2$d dp", description, dp);
        }
        strings.add(description);
    }

    /** Paints a constraint cycle */
    void paintCycle(IGraphics gc, GuidelineHandler state, List<Constraint> cycle) {
        gc.useStyle(DrawingStyle.CYCLE);
        assert cycle.size() > 0;

        INode from = cycle.get(0).from.node;
        Rect fromBounds = from.getBounds();
        if (state.mDraggedNodes.contains(from)) {
            fromBounds = state.mBounds;
        }
        Point fromCenter = fromBounds.center();
        INode to = null;

        List<Point> points = new ArrayList<Point>();
        points.add(fromCenter);

        for (Constraint constraint : cycle) {
            assert constraint.from.node == from;
            to = constraint.to.node;
            assert from != null && to != null;

            Point toCenter = to.getBounds().center();
            points.add(toCenter);

            // Also go through the dragged node bounds
            boolean isDragged = state.mDraggedNodes.contains(to);
            if (isDragged) {
                toCenter = state.mBounds.center();
                points.add(toCenter);
            }

            from = to;
            fromCenter = toCenter;
        }

        points.add(fromCenter);
        points.add(points.get(0));

        for (int i = 1, n = points.size(); i < n; i++) {
            gc.drawLine(points.get(i-1), points.get(i));
        }
    }
}