Skip to content

Commit 8f3c491

Browse files
authored
Fix display of context menu in ContextMenusOnShapes demo. (NASAWorldWind#274)
* Fix display of context menu in ContextMenusOnShapes demo. Retain copy of AWT screen coordinate when creating SelectEvent. * Fixes for drag and drop when DPI scaling in use, reported by NASAWorldWind#274 (comment) * Correct clamping of mouse position to viewport coordinates; previous edition has used AWT size of canvas, and mouse positions from MouseEvents are converted to GL surface coordinates early on. * Make pitch change of globe using right mouse work as it did before DPI scaling changes. Moving the mouse down should increase the pitch, so it appears that the top of the globe is rotated away from the eye. * Correction to commit 515167a; need to adjust mouse position Y for use by FlyViewInputHandler, and per frame mouse/key events. * Correct clamping of mouse position to viewport coordinates; previous edition has used AWT size of canvas, and mouse positions from MouseEvents are converted to GL surface coordinates early on. This commit fixes both X,Y limits, whereas commit d4b6bf4 only fixed Y.
1 parent 017d91e commit 8f3c491

File tree

11 files changed

+89
-69
lines changed

11 files changed

+89
-69
lines changed

src/gov/nasa/worldwind/awt/AWTInputHandler.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,26 @@ public void mouseClicked(final MouseEvent awtMouseEvent)
404404
if (pickedObjects != null && pickedObjects.getTopPickedObject() != null
405405
&& !pickedObjects.getTopPickedObject().isTerrain())
406406
{
407-
// Something is under the cursor, so it's deemed "selected".
407+
Point awtPt = awtMouseEvent.getPoint(); // AWT screen coordinates
408+
409+
// Something is under the cursor, so it's deemed "selected".
408410
if (MouseEvent.BUTTON1 == mouseEvent.getButton())
409411
{
410412
if (mouseEvent.getClickCount() <= 1)
411413
{
412414
this.callSelectListeners(new SelectEvent(this.wwd, SelectEvent.LEFT_CLICK,
413-
mouseEvent, pickedObjects));
415+
awtPt, mouseEvent, pickedObjects));
414416
}
415417
else
416418
{
417419
this.callSelectListeners(new SelectEvent(this.wwd, SelectEvent.LEFT_DOUBLE_CLICK,
418-
mouseEvent, pickedObjects));
420+
awtPt, mouseEvent, pickedObjects));
419421
}
420422
}
421423
else if (MouseEvent.BUTTON3 == mouseEvent.getButton())
422424
{
423425
this.callSelectListeners(new SelectEvent(this.wwd, SelectEvent.RIGHT_CLICK,
424-
mouseEvent, pickedObjects));
426+
awtPt, mouseEvent, pickedObjects));
425427
}
426428

427429
this.wwd.getView().firePropertyChange(AVKey.VIEW, null, this.wwd.getView());
@@ -472,16 +474,18 @@ public void mousePressed(MouseEvent awtMouseEvent)
472474
if (this.objectsAtButtonPress != null && objectsAtButtonPress.getTopPickedObject() != null
473475
&& !this.objectsAtButtonPress.getTopPickedObject().isTerrain())
474476
{
477+
Point awtPt = awtMouseEvent.getPoint(); // AWT screen coordinates
478+
475479
// Something is under the cursor, so it's deemed "selected".
476480
if (MouseEvent.BUTTON1 == mouseEvent.getButton())
477481
{
478482
this.callSelectListeners(new SelectEvent(this.wwd, SelectEvent.LEFT_PRESS,
479-
mouseEvent, this.objectsAtButtonPress));
483+
awtPt, mouseEvent, this.objectsAtButtonPress));
480484
}
481485
else if (MouseEvent.BUTTON3 == mouseEvent.getButton())
482486
{
483487
this.callSelectListeners(new SelectEvent(this.wwd, SelectEvent.RIGHT_PRESS,
484-
mouseEvent, this.objectsAtButtonPress));
488+
awtPt, mouseEvent, this.objectsAtButtonPress));
485489
}
486490

487491
// Initiate a repaint.
@@ -600,8 +604,9 @@ public void mouseDragged(MouseEvent awtMouseEvent)
600604
&& !pickedObjects.getTopPickedObject().isTerrain()))
601605
{
602606
this.isDragging = true;
603-
DragSelectEvent selectEvent = new DragSelectEvent(this.wwd, SelectEvent.DRAG, mouseEvent, pickedObjects,
604-
prevMousePoint);
607+
DragSelectEvent selectEvent = new DragSelectEvent(this.wwd, SelectEvent.DRAG,
608+
awtMouseEvent.getPoint(), mouseEvent,
609+
pickedObjects, prevMousePoint);
605610
this.callSelectListeners(selectEvent);
606611

607612
// If no listener consumed the event, then cancel the drag.
@@ -790,7 +795,7 @@ protected void cancelDrag()
790795
if (this.isDragging)
791796
{
792797
this.callSelectListeners(new DragSelectEvent(this.wwd, SelectEvent.DRAG_END, null,
793-
this.objectsAtButtonPress, this.mousePoint));
798+
null, this.objectsAtButtonPress, this.mousePoint));
794799
}
795800

796801
this.isDragging = false;

src/gov/nasa/worldwind/awt/AbstractViewInputHandler.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,27 +954,32 @@ public double computeDragSlope(Point point1, Point point2, Vec4 vec1, Vec4 vec2)
954954
return slope - 1.0;
955955
}
956956

957-
protected static Point constrainToSourceBounds(Point point, Object source)
957+
protected static Point constrainToSourceBounds(Point point, WorldWindow source)
958958
{
959959
if (point == null)
960960
return null;
961961

962962
if (!(source instanceof Component))
963963
return point;
964964

965-
Component c = (Component) source;
965+
// source.getHeight(), source.getWidth() are AWT coords height,
966+
// but the 'point' is MouseEvent GL surface coords.
967+
// Clamp to GL viewport size.
968+
int glWidth = source.getView().getViewport().width;
969+
int glHeight = source.getView().getViewport().height;
966970

967971
int x = (int) point.getX();
968972
if (x < 0)
969973
x = 0;
970-
if (x > c.getWidth())
971-
x = c.getWidth();
974+
if (x >= glWidth)
975+
x = glWidth - 1;
972976

973977
int y = (int) point.getY();
974978
if (y < 0)
975979
y = 0;
976-
if (y > c.getHeight())
977-
y = c.getHeight();
980+
981+
if (y >= glHeight)
982+
y = glHeight - 1;
978983

979984
return new Point(x, y);
980985
}

src/gov/nasa/worldwind/awt/BasicViewInputHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ public boolean inputActionPerformed(KeyEventState keys, String target,
219219

220220
Point movement = ViewUtil.subtract(point, lastPoint);
221221
int headingInput = movement.x;
222-
int pitchInput = movement.y;
222+
int pitchInput = -movement.y;
223223
Point totalMovement = ViewUtil.subtract(point, mouseDownPoint);
224224
int totalHeadingInput = totalMovement.x;
225-
int totalPitchInput = totalMovement.y;
225+
int totalPitchInput = -totalMovement.y;
226226

227227
ViewInputAttributes.DeviceAttributes deviceAttributes =
228228
getAttributes().getDeviceAttributes(ViewInputAttributes.DEVICE_MOUSE);
@@ -258,16 +258,16 @@ public boolean inputActionPerformed(AbstractViewInputHandler inputHandler,
258258
return false;
259259
}
260260

261+
// 'mouseEvent' is in GL surface coords, (0,0) in lower left of canvas
262+
// Make down mouse movement increase the pitch.
261263
Point movement = ViewUtil.subtract(point, lastPoint);
262264
int headingInput = movement.x;
263-
int pitchInput = movement.y;
265+
int pitchInput = -movement.y;
264266
if (mouseDownPoint == null)
265267
mouseDownPoint = lastPoint;
266268
Point totalMovement = ViewUtil.subtract(point, mouseDownPoint);
267269
int totalHeadingInput = totalMovement.x;
268-
int totalPitchInput = totalMovement.y;
269-
270-
270+
int totalPitchInput = -totalMovement.y;
271271

272272
ViewInputAttributes.DeviceAttributes deviceAttributes =
273273
getAttributes().getDeviceAttributes(ViewInputAttributes.DEVICE_MOUSE);

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/event/DragSelectEvent.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public class DragSelectEvent extends SelectEvent
4242
{
4343
private final java.awt.Point previousPickPoint;
4444

45-
public DragSelectEvent(Object source, String eventAction, MouseEvent mouseEvent, PickedObjectList pickedObjects,
46-
java.awt.Point previousPickPoint)
45+
public DragSelectEvent(Object source, String eventAction, java.awt.Point awtPt, MouseEvent mouseEvent,
46+
PickedObjectList pickedObjects,
47+
java.awt.Point previousPickPoint)
4748
{
48-
super(source, eventAction, mouseEvent, pickedObjects);
49+
super(source, eventAction, awtPt, mouseEvent, pickedObjects);
4950
this.previousPickPoint = previousPickPoint;
5051
}
5152

src/gov/nasa/worldwind/event/SelectEvent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ public class SelectEvent extends WWEvent
104104
private final Point pickPoint; // GL surface coordinates
105105
private final Rectangle pickRect; // GL surface coordinates
106106
private final MouseEvent mouseEvent; // GL surface coordinates
107+
private final Point awtMousePt; // AWT screen coordinates
107108
private final PickedObjectList pickedObjects;
108109

109-
public SelectEvent(Object source, String eventAction, MouseEvent mouseEvent, PickedObjectList pickedObjects)
110+
public SelectEvent(Object source, String eventAction, Point awtPt, MouseEvent mouseEvent, PickedObjectList pickedObjects)
110111
{
111112
super(source);
112113
this.eventAction = eventAction;
113114
this.pickPoint = mouseEvent != null ? mouseEvent.getPoint() : null;
114115
this.pickRect = null;
115116
this.mouseEvent = mouseEvent;
117+
this.awtMousePt = awtPt;
116118
this.pickedObjects = pickedObjects;
117119
}
118120

@@ -123,6 +125,7 @@ public SelectEvent(Object source, String eventAction, Point pickPoint, PickedObj
123125
this.pickPoint = pickPoint;
124126
this.pickRect = null;
125127
this.mouseEvent = null;
128+
this.awtMousePt = null;
126129
this.pickedObjects = pickedObjects;
127130
}
128131

@@ -133,6 +136,7 @@ public SelectEvent(Object source, String eventAction, Rectangle pickRectangle, P
133136
this.pickPoint = null;
134137
this.pickRect = pickRectangle;
135138
this.mouseEvent = null;
139+
this.awtMousePt = null;
136140
this.pickedObjects = pickedObjects;
137141
}
138142

@@ -150,6 +154,10 @@ public String getEventAction()
150154
return this.eventAction != null ? this.eventAction : "gov.nasa.worldwind.SelectEvent.UnknownEventAction";
151155
}
152156

157+
public Point getAwtMousePt() {
158+
return awtMousePt;
159+
}
160+
153161
public Point getPickPoint()
154162
{
155163
return this.pickPoint;

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
}

0 commit comments

Comments
 (0)