Skip to content

Commit b0b90e1

Browse files
committed
Fixes for drag and drop when DPI scaling in use, reported by NASAWorldWind#274 (comment)
1 parent 953f680 commit b0b90e1

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

src/gov/nasa/worldwind/drag/DragContext.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141
public class DragContext
4242
{
4343
/**
44-
* In accordance with the AWT screen coordinates the top left point of the window is the origin.
44+
* In accordance with the GL surface coordinates the top left point of the window is the origin.
4545
*/
4646
protected Point point;
4747
/**
48-
* In accordance with the AWT screen coordinates the top left point of the window is the origin. This point is the
48+
* In accordance with the GL surface coordinates the top left point of the window is the origin. This point is the
4949
* previous screen point.
5050
*/
5151
protected Point previousPoint;
5252
/**
53-
* In accordance with the AWT screen coordinates the top left point of the window is the origin. This point refers
53+
* In accordance with the GL surface coordinates the top left point of the window is the origin. This point refers
5454
* to the initial point of the drag event.
5555
*/
5656
protected Point initialPoint;
@@ -81,19 +81,19 @@ public DragContext()
8181
}
8282

8383
/**
84-
* Returns the current screen point with the origin at the top left corner of the window.
84+
* Returns the current GL surface point with the origin at the top left corner of the window.
8585
*
86-
* @return the current screen point.
86+
* @return the current GL surface point.
8787
*/
8888
public Point getPoint()
8989
{
9090
return point;
9191
}
9292

9393
/**
94-
* Set the {@link DragContext} current screen point.
94+
* Set the {@link DragContext} current GL surface point.
9595
*
96-
* @param point the point to assign to the current screen point.
96+
* @param point the point to assign to the current GL surface point.
9797
*
9898
* @throws IllegalArgumentException if the point is null.
9999
*/
@@ -110,7 +110,7 @@ public void setPoint(Point point)
110110
}
111111

112112
/**
113-
* Returns the previous screen point with the origin at the top left corner of the window.
113+
* Returns the previous GL surface point with the origin at the top left corner of the window.
114114
*
115115
* @return the previous point.
116116
*/
@@ -120,9 +120,9 @@ public Point getPreviousPoint()
120120
}
121121

122122
/**
123-
* Set the {@link DragContext} previous screen point.
123+
* Set the {@link DragContext} previous GL surface point.
124124
*
125-
* @param previousPoint the screen point to assign to the previous screen point.
125+
* @param previousPoint the GL surface point to assign to the previous screen point.
126126
*
127127
* @throws IllegalArgumentException if the previousPoint is null.
128128
*/
@@ -139,20 +139,20 @@ public void setPreviousPoint(Point previousPoint)
139139
}
140140

141141
/**
142-
* Returns the initial screen point with the origin at the top left corner of the window. The initial point is the
143-
* screen point at the initiation of the drag event.
142+
* Returns the initial GL surface point with the origin at the top left corner of the window. The initial point is the
143+
* GL surface point at the initiation of the drag event.
144144
*
145-
* @return the initial screen point.
145+
* @return the initial GL surface point.
146146
*/
147147
public Point getInitialPoint()
148148
{
149149
return initialPoint;
150150
}
151151

152152
/**
153-
* Set the {@link DragContext} initial screen point.
153+
* Set the {@link DragContext} initial GL surface point.
154154
*
155-
* @param initialPoint the screen point to assign to the initial screen point.
155+
* @param initialPoint the GL surface point to assign to the initial screen point.
156156
*
157157
* @throws IllegalArgumentException if the initialPoint is null.
158158
*/

src/gov/nasa/worldwind/drag/DraggableSupport.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,7 @@ protected Vec4 computeScreenOffsetFromReferencePosition(Position dragObjectRefer
428428

429429
Vec4 screenPointOffset = new Vec4(
430430
dragContext.getInitialPoint().getX() - dragObjectScreenPoint.getX(),
431-
dragContext.getInitialPoint().getY() - (
432-
dragContext.getView().getViewport().getHeight()
433-
- dragObjectScreenPoint.getY() - 1.0)
431+
dragContext.getInitialPoint().getY() - dragObjectScreenPoint.getY()
434432
);
435433

436434
return screenPointOffset;

src/gov/nasa/worldwind/render/DrawContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,4 +1051,11 @@ public interface DrawContext extends WWObject, Disposable
10511051
* Convert AWT effective screen location to GL surface location using DPI scaling.
10521052
*/
10531053
int [] awtPointToGLpoint(Point pt);
1054+
1055+
/**
1056+
* Convert GL surface coordinate point to AWT device point using DPI scaling.
1057+
* @param glPoint
1058+
* @return
1059+
*/
1060+
public Point glPointToAwtPoint(Point glPoint);
10541061
}

src/gov/nasa/worldwind/render/DrawContextImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,17 @@ else if (altitudeMode == WorldWind.RELATIVE_TO_GROUND)
17521752
// Convert to GL surface coordinates
17531753
int [] glSurfacePt = drawable.getNativeSurface().convertToPixelUnits(awtPt);
17541754
int glSurfaceHeight = drawable.getSurfaceHeight();
1755-
glSurfacePt[1] = glSurfaceHeight - glSurfacePt[1] - 1;
1755+
glSurfacePt[1] = glSurfaceHeight-1 - glSurfacePt[1];
17561756
return glSurfacePt;
17571757
}
1758+
1759+
public Point glPointToAwtPoint(Point glPoint) {
1760+
GLDrawable drawable = glContext.getGLDrawable();
1761+
if (drawable == null) return glPoint;
1762+
1763+
final int viewportHeight = getView().getViewport().height;
1764+
int [] glPt = { glPoint.x, viewportHeight-1 - glPoint.y };
1765+
getGLDrawable().getNativeSurface().convertToWindowUnits(glPt);
1766+
return new Point(glPt[0], glPt[1]);
1767+
}
17581768
}

src/gov/nasa/worldwind/render/ScreenImage.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ public class ScreenImage extends WWObjectImpl implements Renderable, Exportable
7878
* is computed in <code>computeOffsets</code> and used in <code>draw</code> Initially <code>null</code>.
7979
*/
8080
protected Point screenLocation;
81-
/**
82-
* Indicates the location of this screen image in the viewport (on the screen) in AWT coordinates. This property is
83-
* assigned in <code>setScreenLocation</code> and <code>computeOffsets</code>. In <code>computeOffsets</code>, this
84-
* is computed by converting the <code>screenLocation</code> from OpenGL coordinates to AWT coordinates. Initially
85-
* <code>null</code>.
86-
*/
87-
protected Point awtScreenLocation;
8881
protected double dx;
8982
protected double dy;
9083
protected Layer pickLayer;
@@ -122,7 +115,7 @@ public void render(DrawContext dc)
122115
*/
123116
public Point getScreenLocation()
124117
{
125-
return this.awtScreenLocation;
118+
return this.screenLocation;
126119
}
127120

128121
/**
@@ -136,7 +129,7 @@ public Point getScreenLocation()
136129
public Point getScreenLocation(DrawContext dc)
137130
{
138131
this.computeOffsets(dc);
139-
return this.awtScreenLocation;
132+
return this.screenLocation;
140133
}
141134

142135
/**
@@ -151,17 +144,16 @@ public Point getScreenLocation(DrawContext dc)
151144
*/
152145
public void setScreenLocation(Point screenLocation)
153146
{
154-
// Use units PIXELS for the X screen offset, and and INSET_PIXELS for the Y screen offset. The Offset is in
155-
// OpenGL coordinates with the origin in the lower-left corner, but the Point is in AWT coordinates with the
156-
// origin in the upper-left corner. This offset translates the origin from the lower-left to the upper-left
157-
// corner.
158-
this.screenOffset = new Offset(screenLocation.getX(), screenLocation.getY(), AVKey.PIXELS, AVKey.INSET_PIXELS);
147+
// Use units PIXELS for the X screen offset, and and PIXELS for the Y screen offset. The Offset is in
148+
// OpenGL coordinates with the origin in the lower-left corner, as is the Point. This offset
149+
// translates the origin from the lower-left to the upper-left corner.
150+
this.screenOffset = new Offset(screenLocation.getX(), screenLocation.getY(), AVKey.PIXELS, AVKey.PIXELS);
159151
this.imageOffset = new Offset(0.5, 0.5, AVKey.FRACTION, AVKey.FRACTION);
160152

161153
// Set cached screen location to the initial screen location so that it can be retrieved if getScreenLocation()
162154
// is called before the image is rendered. This maintains backward compatibility with the previous behavior of
163155
// ScreenImage.
164-
this.awtScreenLocation = new Point(screenLocation);
156+
this.screenLocation = new Point(screenLocation);
165157
}
166158

167159
/**
@@ -544,12 +536,7 @@ else if (this.getImageSource() == null) // If no image source is set, draw a rec
544536
{
545537
this.screenLocation = new Point(viewportWidth / 2, viewportHeight / 2);
546538
}
547-
548-
// Convert the screen location from OpenGL to AWT coordinates and store the result in awtScreenLocation. The
549-
// awtScreenLocation property is used in getScreenLocation to indicate the screen location in AWT
550-
// coordinates.
551-
this.awtScreenLocation = new Point(this.screenLocation.x, viewportHeight - this.screenLocation.y);
552-
539+
553540
Point.Double overlayPoint;
554541
if (this.imageOffset != null)
555542
overlayPoint = this.imageOffset.computeOffset(this.width, this.height, null, null);

0 commit comments

Comments
 (0)