Skip to content

Commit

Permalink
➕ Added Button UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jabo-bernardo committed Apr 15, 2020
1 parent 3c548a3 commit 4b92fe0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/dev/jabo/kree/gameTest/MyScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

import java.awt.Graphics;

import dev.jabo.kree.Debug;
import dev.jabo.kree.Game;
import dev.jabo.kree.GameObject;
import dev.jabo.kree.Input;
import dev.jabo.kree.Scene;
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);

}

@Override
public void Initialize() {
public void Initialize() {

player.setScale(new Vector2(32, 32));
player.setPosition(new Vector2(400, 300));
Expand All @@ -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
Expand Down
99 changes: 99 additions & 0 deletions src/dev/jabo/kree/ui/Button.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 4b92fe0

Please sign in to comment.