183 lines
3.6 KiB
Java
Executable File
183 lines
3.6 KiB
Java
Executable File
package projectzombie.menu.gui;
|
|
|
|
import gl_engine.matrix.Matrix4;
|
|
import gl_engine.vec.Vec2d;
|
|
import projectzombie.Main;
|
|
import projectzombie.init.Models;
|
|
import projectzombie.input.InputMode;
|
|
import projectzombie.model.Model;
|
|
import projectzombie.text.Text;
|
|
import projectzombie.util.gl.GlHelpers;
|
|
|
|
public class Button implements GUIComponent, GUISelectable
|
|
{
|
|
private Vec2d pos = new Vec2d(0, 0);
|
|
private String text = "";
|
|
private Alignment alignment = Alignment.CENTRE;
|
|
|
|
private boolean selected = false;
|
|
|
|
private GUISelectable[] SELECTABLE = {null, null, null, null};
|
|
|
|
public static final Vec2d textSize = new Vec2d(0.5, 0.5);
|
|
|
|
public void setPos(Vec2d pos) {
|
|
this.pos = pos;
|
|
}
|
|
|
|
public void setText(String text) {
|
|
this.text = text;
|
|
}
|
|
|
|
public void setAlign(Alignment alignment) {
|
|
this.alignment = alignment;
|
|
}
|
|
|
|
@Override
|
|
public void render(Vec2d mousePos)
|
|
{
|
|
double m = 3;
|
|
double w = textSize.x * m * 8;
|
|
double h = textSize.x * m / 2;
|
|
|
|
double w1 = 0;
|
|
double w2 = 0;
|
|
double wt = 0;
|
|
|
|
if(alignment == Alignment.LEFT) {
|
|
w1 = 0;
|
|
w2 = w;
|
|
wt = w/2;
|
|
}
|
|
|
|
if(alignment == Alignment.CENTRE) {
|
|
w1 = -w/2;
|
|
w2 = w/2;
|
|
wt = 0;
|
|
}
|
|
|
|
if(alignment == Alignment.RIGHT) {
|
|
w1 = -w;
|
|
w2 = 0;
|
|
wt = -w/2;
|
|
}
|
|
|
|
Model model;
|
|
boolean mouseHover = InputMode.Controller ? this.selected : this.checkMouseHover(mousePos);
|
|
if(mouseHover) {
|
|
model = Models.UI_BUTTON_HOVER;
|
|
} else {
|
|
model = Models.UI_BUTTON;
|
|
}
|
|
|
|
Matrix4 matrix = Matrix4.translate(pos.x, pos.y, 0);
|
|
|
|
model.bind();
|
|
model.render();
|
|
|
|
if(mouseHover) {
|
|
//GlHelpers.color3(0.8, 0.8, 0.8);
|
|
} else {
|
|
//GlHelpers.color3(0.68, 0.68, 0.68);
|
|
}
|
|
//Text.render(text, textSize);
|
|
}
|
|
|
|
@Override
|
|
public boolean checkMouseHover(Vec2d pos) {
|
|
|
|
double mx = pos.x / Main.window.getWidth() * 20 - 10;
|
|
double my = pos.y / Main.window.getHeight() * 20 - 10;
|
|
|
|
mx = mx * GlHelpers.getScale() / 10;
|
|
my = my * GlHelpers.getScale() / 10;
|
|
|
|
double m = 3;
|
|
double w = textSize.x * m * 8 / GlHelpers.getAspectRatio();
|
|
double h = textSize.x * m / 2;
|
|
|
|
double w1 = 0;
|
|
double w2 = 0;
|
|
|
|
if(alignment == Alignment.LEFT) {
|
|
w1 = 0;
|
|
w2 = w;
|
|
}
|
|
|
|
if(alignment == Alignment.CENTRE) {
|
|
w1 = -w/2;
|
|
w2 = w/2;
|
|
}
|
|
|
|
if(alignment == Alignment.RIGHT) {
|
|
w1 = -w;
|
|
w2 = 0;
|
|
}
|
|
|
|
return (
|
|
mx > w1 + this.pos.x &&
|
|
mx < w2 + this.pos.x &&
|
|
my > -h - this.pos.y &&
|
|
my < h - this.pos.y);
|
|
}
|
|
|
|
@Override
|
|
public void onMouseClick(Vec2d pos) {
|
|
}
|
|
|
|
@Override
|
|
public void onBack() {
|
|
}
|
|
|
|
@Override
|
|
public void setSelected(boolean status) {
|
|
this.selected = status;
|
|
}
|
|
|
|
@Override
|
|
public boolean getSelected() {
|
|
return this.selected;
|
|
}
|
|
|
|
@Override
|
|
public GUISelectable getNeighbour(GUISelectableDirection direction) {
|
|
if(direction == GUISelectableDirection.UP) {
|
|
return SELECTABLE[0];
|
|
}
|
|
if(direction == GUISelectableDirection.LEFT) {
|
|
return SELECTABLE[1];
|
|
}
|
|
if(direction == GUISelectableDirection.DOWN) {
|
|
return SELECTABLE[2];
|
|
}
|
|
if(direction == GUISelectableDirection.RIGHT) {
|
|
return SELECTABLE[3];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void setNeighbour(GUISelectable selectable, GUISelectableDirection direction) {
|
|
if(direction == GUISelectableDirection.UP) {
|
|
SELECTABLE[0] = selectable;
|
|
}
|
|
if(direction == GUISelectableDirection.LEFT) {
|
|
SELECTABLE[1] = selectable;
|
|
}
|
|
if(direction == GUISelectableDirection.DOWN) {
|
|
SELECTABLE[2] = selectable;
|
|
}
|
|
if(direction == GUISelectableDirection.RIGHT) {
|
|
SELECTABLE[3] = selectable;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onActivate() {
|
|
if(this.selected) {
|
|
this.onMouseClick(new Vec2d(0, 0));
|
|
}
|
|
}
|
|
}
|