diff --git a/src/dev/jabo/kree/gameTest/MyScene.java b/src/dev/jabo/kree/gameTest/MyScene.java index a00c9d9..c1e16f5 100644 --- a/src/dev/jabo/kree/gameTest/MyScene.java +++ b/src/dev/jabo/kree/gameTest/MyScene.java @@ -2,6 +2,7 @@ import java.awt.Graphics; +import dev.jabo.kree.Debug; import dev.jabo.kree.Game; import dev.jabo.kree.GameObject; import dev.jabo.kree.Input; @@ -9,10 +10,12 @@ import dev.jabo.kree.Vector2; import dev.jabo.kree.components.LookAtObject; import dev.jabo.kree.components.MeshRenderer; +import dev.jabo.kree.ui.Button; public class MyScene extends Scene { GameObject player = new GameObject(this, "Player"); + Button myButton = new Button(this, "Play", new Vector2(0, 0), new Vector2(128, 64)); public MyScene(Game game) { super(game); @@ -20,7 +23,7 @@ public MyScene(Game game) { } @Override - public void Initialize() { + public void Initialize() { player.setScale(new Vector2(32, 32)); player.setPosition(new Vector2(400, 300)); @@ -33,6 +36,11 @@ public void Initialize() { @Override public void Update() { ((LookAtObject) player.getComponent("Look at Object")).setTarget(Input.getMouse()); + + if(myButton.onClick()) { + Debug.log("Hello"); + } + } @Override diff --git a/src/dev/jabo/kree/ui/Button.java b/src/dev/jabo/kree/ui/Button.java new file mode 100644 index 0000000..8524d10 --- /dev/null +++ b/src/dev/jabo/kree/ui/Button.java @@ -0,0 +1,99 @@ +package dev.jabo.kree.ui; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.Rectangle; + +import dev.jabo.kree.Input; +import dev.jabo.kree.Scene; +import dev.jabo.kree.Transform; +import dev.jabo.kree.Vector2; + +public class Button extends UserInterface { + + public Transform transform; + + private boolean hovering; + private boolean clicked; + + private Rectangle collider; + + private Point mousePoint; + + private Color backgroundColor, foregroundColor, hoverColor; + + private String text; + + private Font font; + + public Button(Scene parentScene, String text, Vector2 position, Vector2 scale) { + + this.text = text; + + transform = new Transform(); + transform.position = position; + transform.scale = scale; + + backgroundColor = Color.GRAY; + foregroundColor = Color.BLACK; + hoverColor = Color.LIGHT_GRAY; + + font = new Font("Arial", Font.PLAIN, 16); + + collider = new Rectangle(); + mousePoint = new Point(); + + AddToScene(parentScene); + + } + + @Override + public void Update() { + + collider.x = transform.position.x; + collider.y = transform.position.y; + collider.width = transform.scale.x; + collider.height = transform.scale.y; + + mousePoint.x = Input.getMouse().x; + mousePoint.y = Input.getMouse().y; + + if(collider.contains(mousePoint)) { + hovering = true; + if(!clicked) { + if(Input.leftMouseDown) { + clicked = true; + } else { + clicked = false; + } + } + } else { + hovering = false; + } + } + + @Override + public void Render(Graphics g) { + + g.setColor(backgroundColor); + if(hovering) { + g.setColor(hoverColor); + } + g.fillRect(transform.position.x, transform.position.y, transform.scale.x, transform.scale.y); + g.setColor(foregroundColor); + g.setFont(font); + g.drawString(text, transform.position.x + ((transform.scale.x / 2) - (g.getFontMetrics(font).stringWidth(text) / 2)), (transform.position.y + font.getSize()- 3) + ((transform.scale.y / 2) - (font.getSize() / 2))); + + } + + public boolean onClick() { + return clicked; + } + + public boolean onHover() { + return hovering; + } + +}