Skip to content

Commit

Permalink
Fix UiContainer blocking buttonUp events when it did not accept the …
Browse files Browse the repository at this point in the history
…original buttonDown event
  • Loading branch information
tomcashman committed Jan 29, 2017
1 parent 98ffae1 commit 841fb20
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGES
@@ -1,5 +1,5 @@
[1.4.3-SNAPSHOT]
- Fix UiContainer blocking keyUp events when it did not accept the original keyDown event
- Fix UiContainer blocking keyUp/buttonUp events when it did not accept the original keyDown/buttonDown event

[1.4.2]
- Added TiledMap#getObjectGroups() method
Expand Down
8 changes: 8 additions & 0 deletions ui/src/main/java/org/mini2Dx/ui/UiContainer.java
Expand Up @@ -56,7 +56,10 @@ public class UiContainer extends ParentUiElement implements InputProcessor {

private final List<ControllerUiInput<?>> controllerInputs = new ArrayList<ControllerUiInput<?>>(1);
private final List<UiContainerListener> listeners = new ArrayList<UiContainerListener>(1);

private final Set<Integer> receivedKeyDowns = new HashSet<Integer>();
private final Set<String> receivedButtonDowns = new HashSet<String>();

private final UiContainerRenderTree renderTree;

private InputSource lastInputSource;
Expand Down Expand Up @@ -411,6 +414,7 @@ public boolean buttonDown(ControllerUiInput<?> controllerUiInput, ControllerButt
if (activeNavigation == null) {
return false;
}
receivedButtonDowns.add(button.getAbsoluteValue());
ActionableRenderNode hotkeyAction = activeNavigation.hotkey(button);
if (hotkeyAction != null) {
hotkeyAction.beginAction();
Expand All @@ -429,6 +433,10 @@ public boolean buttonDown(ControllerUiInput<?> controllerUiInput, ControllerButt
}

public boolean buttonUp(ControllerUiInput<?> controllerUiInput, ControllerButton button) {
//Button down was sent before this UI Container accepted input
if(!receivedButtonDowns.remove(button.getAbsoluteValue())) {
return false;
}
if (activeNavigation == null) {
return false;
}
Expand Down
42 changes: 42 additions & 0 deletions ui/src/test/java/org/mini2Dx/ui/UiContainerTest.java
Expand Up @@ -17,6 +17,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mini2Dx.core.controller.button.XboxOneButton;
import org.mini2Dx.core.game.GameContainer;
import org.mini2Dx.ui.element.AlignedModal;
import org.mini2Dx.ui.element.Modal;
Expand Down Expand Up @@ -103,4 +104,45 @@ public void testIgnoresRepeatedKeyUpEvents() {
Assert.assertEquals(true, uiContainer.keyUp(Keys.LEFT));
Assert.assertEquals(false, uiContainer.keyUp(Keys.LEFT));
}

@Test
public void testBlocksButtonUpWhenButtonDownPreviouslyReceived() {
mockery.checking(new Expectations() {
{
exactly(2).of(modal).hotkey(XboxOneButton.A);
will(returnValue(null));
}
});

uiContainer.setActiveNavigation(modal);
uiContainer.buttonDown(null, XboxOneButton.A);
Assert.assertEquals(true, uiContainer.buttonUp(null, XboxOneButton.A));
}

@Test
public void testIgnoresButtonUpWhenNoActiveNavigation() {
uiContainer.buttonDown(null, XboxOneButton.A);
Assert.assertEquals(false, uiContainer.buttonUp(null, XboxOneButton.A));
}

@Test
public void testIgnoresButtonUpWhenButtonDownNotPreviouslyReceived() {
uiContainer.setActiveNavigation(modal);
Assert.assertEquals(false, uiContainer.buttonUp(null, XboxOneButton.A));
}

@Test
public void testIgnoresRepeatedButtonUpEvents() {
mockery.checking(new Expectations() {
{
exactly(2).of(modal).hotkey(XboxOneButton.A);
will(returnValue(null));
}
});

uiContainer.setActiveNavigation(modal);
uiContainer.buttonDown(null, XboxOneButton.A);
Assert.assertEquals(true, uiContainer.buttonUp(null, XboxOneButton.A));
Assert.assertEquals(false, uiContainer.buttonUp(null, XboxOneButton.A));
}
}

0 comments on commit 841fb20

Please sign in to comment.