1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
18 
19 import com.android.ide.common.api.Point;
20 
21 import org.eclipse.swt.dnd.DragSourceEvent;
22 import org.eclipse.swt.dnd.DragSourceListener;
23 import org.eclipse.swt.events.MouseEvent;
24 import org.eclipse.swt.events.MouseListener;
25 
26 /**
27  * A {@link LayoutPoint} is a coordinate in the Android canvas (in other words,
28  * it may differ from the canvas control mouse coordinate because the canvas may
29  * be zoomed and scrolled.)
30  */
31 public final class LayoutPoint {
32     /** Containing canvas which the point is relative to. */
33     private final LayoutCanvas mCanvas;
34 
35     /** The X coordinate of the canvas coordinate. */
36     public final int x;
37 
38     /** The Y coordinate of the canvas coordinate. */
39     public final int y;
40 
41     /**
42      * Constructs a new {@link LayoutPoint} from the given event. The event
43      * must be from a {@link MouseListener} associated with the
44      * {@link LayoutCanvas} such that the {@link MouseEvent#x} and
45      * {@link MouseEvent#y} fields are relative to the canvas.
46      *
47      * @param canvas The {@link LayoutCanvas} this point is within.
48      * @param event The mouse event to construct the {@link LayoutPoint}
49      *            from.
50      * @return A {@link LayoutPoint} which corresponds to the given
51      *         {@link MouseEvent}.
52      */
create(LayoutCanvas canvas, MouseEvent event)53     public static LayoutPoint create(LayoutCanvas canvas, MouseEvent event) {
54         // The mouse event coordinates should already be relative to the canvas
55         // widget.
56         assert event.widget == canvas : event.widget;
57         return ControlPoint.create(canvas, event).toLayout();
58     }
59 
60     /**
61      * Constructs a new {@link LayoutPoint} from the given event. The event
62      * must be from a {@link DragSourceListener} associated with the
63      * {@link LayoutCanvas} such that the {@link DragSourceEvent#x} and
64      * {@link DragSourceEvent#y} fields are relative to the canvas.
65      *
66      * @param canvas The {@link LayoutCanvas} this point is within.
67      * @param event The mouse event to construct the {@link LayoutPoint}
68      *            from.
69      * @return A {@link LayoutPoint} which corresponds to the given
70      *         {@link DragSourceEvent}.
71      */
create(LayoutCanvas canvas, DragSourceEvent event)72     public static LayoutPoint create(LayoutCanvas canvas, DragSourceEvent event) {
73         // The drag source event coordinates should already be relative to the
74         // canvas widget.
75         return ControlPoint.create(canvas, event).toLayout();
76     }
77 
78     /**
79      * Constructs a new {@link LayoutPoint} from the given x,y coordinates.
80      *
81      * @param canvas The {@link LayoutCanvas} this point is within.
82      * @param x The mouse event x coordinate relative to the canvas
83      * @param y The mouse event x coordinate relative to the canvas
84      * @return A {@link LayoutPoint} which corresponds to the given
85      *         layout coordinates.
86      */
create(LayoutCanvas canvas, int x, int y)87     public static LayoutPoint create(LayoutCanvas canvas, int x, int y) {
88         return new LayoutPoint(canvas, x, y);
89     }
90 
91     /**
92      * Constructs a new {@link LayoutPoint} with the given X and Y coordinates.
93      *
94      * @param canvas The canvas which contains this coordinate
95      * @param x The canvas X coordinate
96      * @param y The canvas Y coordinate
97      */
LayoutPoint(LayoutCanvas canvas, int x, int y)98     private LayoutPoint(LayoutCanvas canvas, int x, int y) {
99         mCanvas = canvas;
100         this.x = x;
101         this.y = y;
102     }
103 
104     /**
105      * Returns the equivalent {@link ControlPoint} to this
106      * {@link LayoutPoint}.
107      *
108      * @return The equivalent {@link ControlPoint} to this
109      *         {@link LayoutPoint}
110      */
toControl()111     public ControlPoint toControl() {
112         int cx = mCanvas.getHorizontalTransform().translate(x);
113         int cy = mCanvas.getVerticalTransform().translate(y);
114 
115         return ControlPoint.create(mCanvas, cx, cy);
116     }
117 
118     /**
119      * Returns this {@link LayoutPoint} as a {@link Point}, in the same coordinate space.
120      *
121      * @return a new {@link Point} in the same coordinate space
122      */
toPoint()123     public Point toPoint() {
124         return new Point(x, y);
125     }
126 
127     @Override
toString()128     public String toString() {
129         return "LayoutPoint [x=" + x + ", y=" + y + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
130     }
131 
132     @Override
hashCode()133     public int hashCode() {
134         final int prime = 31;
135         int result = 1;
136         result = prime * result + x;
137         result = prime * result + y;
138         return result;
139     }
140 
141     @Override
equals(Object obj)142     public boolean equals(Object obj) {
143         if (this == obj)
144             return true;
145         if (obj == null)
146             return false;
147         if (getClass() != obj.getClass())
148             return false;
149         LayoutPoint other = (LayoutPoint) obj;
150         if (x != other.x)
151             return false;
152         if (y != other.y)
153             return false;
154         return true;
155     }
156 }
157