diff --git a/core/src/com/saltosion/gladiator/GladiatorBrawler.java b/core/src/com/saltosion/gladiator/GladiatorBrawler.java index c7b0294..d80dc84 100644 --- a/core/src/com/saltosion/gladiator/GladiatorBrawler.java +++ b/core/src/com/saltosion/gladiator/GladiatorBrawler.java @@ -5,11 +5,14 @@ import com.badlogic.ashley.core.Entity; 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.graphics.g2d.Sprite; import com.badlogic.gdx.math.Vector2; import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CRenderedObject; +import com.saltosion.gladiator.gui.ButtonNode; +import com.saltosion.gladiator.gui.GUIManager; import com.saltosion.gladiator.input.InputHandler; import com.saltosion.gladiator.listeners.CombatListener; import com.saltosion.gladiator.systems.CombatSystem; @@ -19,6 +22,7 @@ import com.saltosion.gladiator.systems.RenderingSystem; import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Global; +import com.saltosion.gladiator.util.Log; import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.SpriteLoader; import com.saltosion.gladiator.util.SpriteSequence; @@ -26,6 +30,7 @@ import com.saltosion.gladiator.util.SpriteSequence; public class GladiatorBrawler extends ApplicationAdapter { private Engine engine; + private GUIManager guiManager; private InputHandler inputHandler; private Entity player; @@ -66,6 +71,11 @@ public class GladiatorBrawler extends ApplicationAdapter { } }); + // Initialize GUI + guiManager = new GUIManager(); + AppUtil.guiManager = this.guiManager; + initializeTestGUI(); + // Initialize stuff in the world initializePlayer(); initializeTestDummy(); @@ -161,6 +171,17 @@ public class GladiatorBrawler extends ApplicationAdapter { engine.addEntity(wall0); engine.addEntity(wall1); } + + public void initializeTestGUI() { + Sprite img = SpriteLoader.loadSprite(Name.GROUNDIMG); + ButtonNode button = new ButtonNode("test-button", img, img) { + @Override + public void click(float x, float y, Input.Buttons mouseButton) { + Log.info("I should never be pressed!"); + } + }; + guiManager.getRootNode().addChild(button); + } @Override public void resize(int width, int height) { diff --git a/core/src/com/saltosion/gladiator/gui/ButtonNode.java b/core/src/com/saltosion/gladiator/gui/ButtonNode.java new file mode 100644 index 0000000..68169fc --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/ButtonNode.java @@ -0,0 +1,33 @@ +package com.saltosion.gladiator.gui; + +import com.badlogic.gdx.graphics.g2d.Sprite; + +public abstract class ButtonNode extends GUINode implements InteractiveNode, ImageNode { + private final Sprite onHover; + private final Sprite normal; + private boolean hovered = false; + + public ButtonNode(String ID, Sprite onHover, Sprite normal) { + super(ID); + this.onHover = onHover; + this.normal = normal; + } + + @Override + public void mouseEnter(float x, float y) { + hovered = true; + } + + @Override + public void mouseLeave(float x, float y){ + hovered = false; + } + + @Override + public Sprite getImage() { + if (hovered) { + return onHover; + } + return normal; + } +} diff --git a/core/src/com/saltosion/gladiator/gui/GUIManager.java b/core/src/com/saltosion/gladiator/gui/GUIManager.java new file mode 100644 index 0000000..bd7fb43 --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/GUIManager.java @@ -0,0 +1,14 @@ +package com.saltosion.gladiator.gui; + +public class GUIManager { + private final GUINode rootNode; + + public GUIManager() { + this.rootNode = new GUINode("root"); + } + + public GUINode getRootNode() { + return this.rootNode; + } + +} diff --git a/core/src/com/saltosion/gladiator/gui/GUINode.java b/core/src/com/saltosion/gladiator/gui/GUINode.java new file mode 100644 index 0000000..b11de33 --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/GUINode.java @@ -0,0 +1,64 @@ +package com.saltosion.gladiator.gui; + +import com.badlogic.gdx.math.Vector2; +import java.util.ArrayList; + +public class GUINode { + private final Vector2 position; + private final ArrayList children; + private String ID = ""; + + public GUINode(String ID) { + this.ID = ID; + this.position = new Vector2(); + this.children = new ArrayList(); + } + + public GUINode setPosition(float x, float y) { + this.position.set(x, y); + return this; + } + + public GUINode setPosition(Vector2 pos) { + this.position.set(pos); + return this; + } + + public GUINode setID(String ID) { + this.ID = ID; + return this; + } + + public GUINode addChild(GUINode node) { + this.children.add(node); + return this; + } + + public GUINode removeChild(String id) { + for (GUINode child : this.children) { + if (child.ID.equals(id)) { + this.children.remove(child); + break; + } + } + return this; + } + + public GUINode removeChild(GUINode node) { + this.children.remove(node); + return this; + } + + public GUINode clearChildren() { + this.children.clear(); + return this; + } + + public Vector2 getPosition() { + return this.position; + } + + public String getID() { + return this.ID; + } +} diff --git a/core/src/com/saltosion/gladiator/gui/ImageNode.java b/core/src/com/saltosion/gladiator/gui/ImageNode.java new file mode 100644 index 0000000..a73b43c --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/ImageNode.java @@ -0,0 +1,8 @@ +package com.saltosion.gladiator.gui; + +import com.badlogic.gdx.graphics.g2d.Sprite; + +public interface ImageNode { + + public Sprite getImage(); +} diff --git a/core/src/com/saltosion/gladiator/gui/InteractiveNode.java b/core/src/com/saltosion/gladiator/gui/InteractiveNode.java new file mode 100644 index 0000000..f1e2e46 --- /dev/null +++ b/core/src/com/saltosion/gladiator/gui/InteractiveNode.java @@ -0,0 +1,27 @@ +package com.saltosion.gladiator.gui; + +import com.badlogic.gdx.Input.Buttons; + +public interface InteractiveNode { + /** + * Called when the mouse enters the node's elements. + * @param x position of the mouse. + * @param y position of the mouse. + */ + public void mouseEnter(float x, float y); + + /** + * Called when the mouse leaves the node's elements. + * @param x position of the mouse. + * @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); +} diff --git a/core/src/com/saltosion/gladiator/util/AppUtil.java b/core/src/com/saltosion/gladiator/util/AppUtil.java index 6112ddc..1d1e93e 100644 --- a/core/src/com/saltosion/gladiator/util/AppUtil.java +++ b/core/src/com/saltosion/gladiator/util/AppUtil.java @@ -3,11 +3,13 @@ package com.saltosion.gladiator.util; import com.badlogic.ashley.core.Engine; import com.badlogic.ashley.core.Entity; import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.gui.GUIManager; public class AppUtil { public static Entity player; public static Engine engine; + public static GUIManager guiManager; public static final int VPHEIGHT_CONST = 24;