InteractiveNodes can now also be pressed against their will !

This commit is contained in:
Allexit 2015-05-12 00:24:48 +03:00
parent 87d78195d4
commit adf75ef7a1
3 changed files with 43 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import com.badlogic.ashley.core.EntityListener;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.components.CAI; import com.saltosion.gladiator.components.CAI;
@ -187,8 +188,15 @@ public class GladiatorBrawler extends ApplicationAdapter {
System.out.println(img1.getRegionHeight() + " - " + img1.getRegionWidth()); System.out.println(img1.getRegionHeight() + " - " + img1.getRegionWidth());
ButtonNode button = new ButtonNode("test-button", img1, img2) { ButtonNode button = new ButtonNode("test-button", img1, img2) {
@Override @Override
public void click(float x, float y, Input.Buttons mouseButton) { public void pressed(int x, int y, int mouseButton) {
Log.info("I should never be pressed!"); 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); button.setPosition(0.12f, 0.5f);

View File

@ -16,12 +16,20 @@ public interface InteractiveNode {
* @param y position of the mouse. * @param y position of the mouse.
*/ */
public void mouseLeave(float x, float y); public void mouseLeave(float x, float y);
/** /**
* Called when someone presses a mouse button on the node's elements. * Called when someone presses a mouse button on the node's elements.
* @param x position of the mouse. * @param x position of the mouse.
* @param y position of the mouse. * @param y position of the mouse.
* @param mouseButton button pressed. * @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);
} }

View File

@ -56,12 +56,30 @@ public class InputHandler implements InputProcessor {
@Override @Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 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 @Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) { 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 @Override
@ -92,18 +110,18 @@ public class InputHandler implements InputProcessor {
continue; continue;
} }
hoveredUIElements.add(node.getID()); hoveredUIElements.add(node.getID());
interactiveNode.mouseEnter(x, y); interactiveNode.mouseEnter(screenX, screenY);
} else { } else {
if (!hoveredUIElements.contains(node.getID(), false)) { if (!hoveredUIElements.contains(node.getID(), false)) {
continue; continue;
} }
hoveredUIElements.removeValue(node.getID(), false); hoveredUIElements.removeValue(node.getID(), false);
interactiveNode.mouseLeave(x, y); interactiveNode.mouseLeave(screenX, screenY);
} }
} }
} }
} }
return false; return true;
} }
@Override @Override