Skip to content

Commit

Permalink
fixed DraggableControl not processing mouse events in some cases
Browse files Browse the repository at this point in the history
It was possible for DraggableControls to return "processed = false"
for mouse events when it was being dragged very fast or when the mouse
leaves the screen while it was dragged. This should be fixed now.

Implementation note:
Nifty already supported exclusive mouse events for elements that are
focusable. When such an element was initially clicked the element
internally was marked as the one element that captured the mouse. Nifty
made sure that all mouse events were only sent to this element.

However it still was possible to have mouse events that didn't hit any
element at all and therefore were returned as "processed = false". This
has now been changed. Now all mouse events will be send to the element
that has captured the mouse no matter if the mouse hovers the element or
not.

Additionally when a DraggableControl is being dragged it is
now automatically marked as exclusive.
  • Loading branch information
void committed Sep 22, 2012
1 parent 33e8833 commit 8061eec
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void moveDraggableToPopup() {
draggable.markForMove(popup, new EndNotify() {
@Override
public void perform() {
draggable.getFocusHandler().requestExclusiveMouseFocus(draggable);
draggable.setConstraintX(new SizeValue(originalPositionX + "px"));
draggable.setConstraintY(new SizeValue(originalPositionY + "px"));
draggable.getParent().layoutElements();
Expand Down
9 changes: 3 additions & 6 deletions nifty-core/src/main/java/de/lessvoid/nifty/Nifty.java
Original file line number Diff line number Diff line change
Expand Up @@ -1360,12 +1360,9 @@ private NiftyMouseInputEvent createEvent(final int mouseX, final int mouseY, fin
}

private boolean processEvent(final NiftyMouseInputEvent mouseInputEvent) {
boolean handled = false;
if (mouseInputEventProcessor.canProcess(mouseInputEvent)) {
mouseInputEventProcessor.process(mouseInputEvent);
handled = forwardMouseEventToScreen(mouseInputEvent);
handleDynamicElements();
}
mouseInputEventProcessor.process(mouseInputEvent);
boolean handled = forwardMouseEventToScreen(mouseInputEvent);
handleDynamicElements();
pool.free(mouseInputEvent);
return handled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ public boolean hasAnyElementTheKeyboardFocus() {
return keyboardFocusElement != null;
}

public boolean hasAnyElementTheMouseFocus() {
return mouseFocusElement != null;
}

public Element findElement(final String defaultFocusElementId) {
for (Element element : entries) {
if (defaultFocusElementId.equals(element.getId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,8 @@ public void begin() {
hadAnyEvents = false;
}

public boolean canProcess(final NiftyMouseInputEvent mouseEvent) {
hadAnyEvents = true;

int mouseX = mouseEvent.getMouseX();
int mouseY = mouseEvent.getMouseY();
int mouseWheel = mouseEvent.getMouseWheel();
boolean button0Down = mouseEvent.isButton0Down();
boolean button1Down = mouseEvent.isButton1Down();
boolean button2Down = mouseEvent.isButton2Down();

if (mouseX != lastMouseX ||
mouseY != lastMouseY ||
mouseWheel != 0 ||
button0Down != lastButtonDown0 ||
button1Down != lastButtonDown1 ||
button2Down != lastButtonDown2) {
return true;
}

return false;
}

public void process(final NiftyMouseInputEvent mouse) {
hadAnyEvents = true;
mouse.setButton0InitialDown(!lastButtonDown0 && mouse.isButton0Down());
mouse.setButton0Release(lastButtonDown0 && !mouse.isButton0Down());
mouse.setButton1InitialDown(!lastButtonDown1 && mouse.isButton1Down());
Expand Down
12 changes: 9 additions & 3 deletions nifty-core/src/main/java/de/lessvoid/nifty/screen/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,15 @@ private boolean forwardMouseEventToLayers(final List < Element > layerList, fina
mouseOverHandler.reset();

long eventTime = timeProvider.getMsTime();
for (int i=0; i<layerList.size(); i++) {
Element layer = layerList.get(i);
layer.buildMouseOverElements(inputEvent, eventTime, mouseOverHandler);

if (focusHandler.hasAnyElementTheMouseFocus()) {
Element e = focusHandler.getMouseFocusElement();
mouseOverHandler.addMouseOverElement(e);
} else {
for (int i=0; i<layerList.size(); i++) {
Element layer = layerList.get(i);
layer.buildMouseOverElements(inputEvent, eventTime, mouseOverHandler);
}
}

if (log.isLoggable(Level.FINER)) {
Expand Down

0 comments on commit 8061eec

Please sign in to comment.