From adf75ef7a1bf60dfdfc99439750c8b6730ad179d Mon Sep 17 00:00:00 2001 From: Allexit Date: Tue, 12 May 2015 00:24:48 +0300 Subject: [PATCH] InteractiveNodes can now also be pressed against their will ! --- .../saltosion/gladiator/GladiatorBrawler.java | 12 ++++++-- .../gladiator/gui/InteractiveNode.java | 12 ++++++-- .../gladiator/input/InputHandler.java | 28 +++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index 6da3b4f..a203fd1 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -6,6 +6,7 @@ import com.badlogic.ashley.core.EntityListener; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; +import com.badlogic.gdx.Input.Buttons; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.math.Vector2; import com.saltosion.gladiator.components.CAI; @@ -187,8 +188,15 @@ public class GladiatorBrawler extends ApplicationAdapter { System.out.println(img1.getRegionHeight() + " - " + img1.getRegionWidth()); ButtonNode button = new ButtonNode("test-button", img1, img2) { @Override - public void click(float x, float y, Input.Buttons mouseButton) { - Log.info("I should never be pressed!"); + public void pressed(int x, int y, int mouseButton) { + Log.info("I should never be pressed against my will!"); + + } + + @Override + public void released(int x, int y, int mouseButton) { + Log.info("And now I was even released! Blasphemy!"); + } }; button.setPosition(0.12f, 0.5f); diff --git a/core/src/com/saltosion/gladiator/gui/InteractiveNode.java b/core/src/com/saltosion/gladiator/gui/InteractiveNode.java index f1e2e46..64bc7e5 100644 --- a/core/src/com/saltosion/gladiator/gui/InteractiveNode.java +++ b/core/src/com/saltosion/gladiator/gui/InteractiveNode.java @@ -16,12 +16,20 @@ public interface InteractiveNode { * @param y position of the mouse. */ public void mouseLeave(float x, float y); - + /** * Called when someone presses a mouse button on the node's elements. * @param x position of the mouse. * @param y position of the mouse. * @param mouseButton button pressed. */ - public void click(float x, float y, Buttons mouseButton); + public void pressed(int x, int y, int mouseButton); + /** + * + * Called when someone releases a mouse button on the node's elements. + * @param x position of the mouse. + * @param y position of the mouse. + * @param mouseButton button pressed. + */ + public void released(int x, int y, int mouseButton); } diff --git a/core/src/com/saltosion/gladiator/input/InputHandler.java b/core/src/com/saltosion/gladiator/input/InputHandler.java index 7b2433c..473ee67 100644 --- a/core/src/com/saltosion/gladiator/input/InputHandler.java +++ b/core/src/com/saltosion/gladiator/input/InputHandler.java @@ -56,12 +56,30 @@ public class InputHandler implements InputProcessor { @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { - return false; + for (String id : hoveredUIElements) { + for (GUINode node : AppUtil.guiManager.getAllRecursiveChildren(AppUtil.guiManager.getRootNode())) { + if (node.getID().equals(id)) { + if (node instanceof InteractiveNode) { + ((InteractiveNode) node).pressed(screenX, screenY, button); + } + } + } + } + return true; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { - return false; + for (String id : hoveredUIElements) { + for (GUINode node : AppUtil.guiManager.getAllRecursiveChildren(AppUtil.guiManager.getRootNode())) { + if (node.getID().equals(id)) { + if (node instanceof InteractiveNode) { + ((InteractiveNode) node).released(screenX, screenY, button); + } + } + } + } + return true; } @Override @@ -92,18 +110,18 @@ public class InputHandler implements InputProcessor { continue; } hoveredUIElements.add(node.getID()); - interactiveNode.mouseEnter(x, y); + interactiveNode.mouseEnter(screenX, screenY); } else { if (!hoveredUIElements.contains(node.getID(), false)) { continue; } hoveredUIElements.removeValue(node.getID(), false); - interactiveNode.mouseLeave(x, y); + interactiveNode.mouseLeave(screenX, screenY); } } } } - return false; + return true; } @Override