aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutPoint.java
blob: 818b2c4ef0add5d1cf43166ddd1490f6bb5452ae (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
/*
 * 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 com.android.ide.common.api.Point;

import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;

/**
 * A {@link LayoutPoint} is a coordinate in the Android canvas (in other words,
 * it may differ from the canvas control mouse coordinate because the canvas may
 * be zoomed and scrolled.)
 */
public final class LayoutPoint {
    /** Containing canvas which the point is relative to. */
    private final LayoutCanvas mCanvas;

    /** The X coordinate of the canvas coordinate. */
    public final int x;

    /** The Y coordinate of the canvas coordinate. */
    public final int y;

    /**
     * Constructs a new {@link LayoutPoint} from the given event. The event
     * must be from a {@link MouseListener} associated with the
     * {@link LayoutCanvas} such that the {@link MouseEvent#x} and
     * {@link MouseEvent#y} fields are relative to the canvas.
     *
     * @param canvas The {@link LayoutCanvas} this point is within.
     * @param event The mouse event to construct the {@link LayoutPoint}
     *            from.
     * @return A {@link LayoutPoint} which corresponds to the given
     *         {@link MouseEvent}.
     */
    public static LayoutPoint create(LayoutCanvas canvas, MouseEvent event) {
        // The mouse event coordinates should already be relative to the canvas
        // widget.
        assert event.widget == canvas : event.widget;
        return ControlPoint.create(canvas, event).toLayout();
    }

    /**
     * Constructs a new {@link LayoutPoint} from the given event. The event
     * must be from a {@link DragSourceListener} associated with the
     * {@link LayoutCanvas} such that the {@link DragSourceEvent#x} and
     * {@link DragSourceEvent#y} fields are relative to the canvas.
     *
     * @param canvas The {@link LayoutCanvas} this point is within.
     * @param event The mouse event to construct the {@link LayoutPoint}
     *            from.
     * @return A {@link LayoutPoint} which corresponds to the given
     *         {@link DragSourceEvent}.
     */
    public static LayoutPoint create(LayoutCanvas canvas, DragSourceEvent event) {
        // The drag source event coordinates should already be relative to the
        // canvas widget.
        return ControlPoint.create(canvas, event).toLayout();
    }

    /**
     * Constructs a new {@link LayoutPoint} from the given x,y coordinates.
     *
     * @param canvas The {@link LayoutCanvas} this point is within.
     * @param x The mouse event x coordinate relative to the canvas
     * @param y The mouse event x coordinate relative to the canvas
     * @return A {@link LayoutPoint} which corresponds to the given
     *         layout coordinates.
     */
    public static LayoutPoint create(LayoutCanvas canvas, int x, int y) {
        return new LayoutPoint(canvas, x, y);
    }

    /**
     * Constructs a new {@link LayoutPoint} with the given X and Y coordinates.
     *
     * @param canvas The canvas which contains this coordinate
     * @param x The canvas X coordinate
     * @param y The canvas Y coordinate
     */
    private LayoutPoint(LayoutCanvas canvas, int x, int y) {
        mCanvas = canvas;
        this.x = x;
        this.y = y;
    }

    /**
     * Returns the equivalent {@link ControlPoint} to this
     * {@link LayoutPoint}.
     *
     * @return The equivalent {@link ControlPoint} to this
     *         {@link LayoutPoint}
     */
    public ControlPoint toControl() {
        int cx = mCanvas.getHorizontalTransform().translate(x);
        int cy = mCanvas.getVerticalTransform().translate(y);

        return ControlPoint.create(mCanvas, cx, cy);
    }

    /**
     * Returns this {@link LayoutPoint} as a {@link Point}, in the same coordinate space.
     *
     * @return a new {@link Point} in the same coordinate space
     */
    public Point toPoint() {
        return new Point(x, y);
    }

    @Override
    public String toString() {
        return "LayoutPoint [x=" + x + ", y=" + y + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        LayoutPoint other = (LayoutPoint) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
}