package projectzombie.menu.gui; import gl_engine.matrix.Matrix4; import gl_engine.vec.Vec2d; import gl_engine.vec.Vec3d; import projectzombie.Main; import projectzombie.init.Models; import projectzombie.input.InputMode; import projectzombie.model.ModelGui; import projectzombie.text.Text; import projectzombie.util.gl.GlHelpers; public class GUIButton implements GUIComponent, GUISelectable { private Vec2d pos = new Vec2d(0, 0); private String text = ""; private GUIAlignment alignment = GUIAlignment.CENTRE; private Matrix4 matrix, matrix_text; private boolean selected = false; private boolean dirty = true; private GUISelectable[] SELECTABLE = {null, null, null, null}; private double getOffset() { switch(alignment) { case CENTRE: return Models.UI_BUTTON.getWidth() / 2; case RIGHT: return Models.UI_BUTTON.getWidth(); default: return 0; } } private void updateMatrix() { matrix = Matrix4.translate(pos.x - getOffset(), pos.y, 0); matrix_text = Matrix4.multiply(matrix, Matrix4.translate( Models.UI_BUTTON.getWidth() / 2 - 0.25 * text.length(), Models.UI_BUTTON.getHeight() / 2 - 0.25, 0)); matrix_text = Matrix4.multiply(Matrix4.scale(new Vec3d(0.5, 0.5, 0.5)), matrix_text); } public GUIButton() { this.text = ""; this.alignment = GUIAlignment.CENTRE; this.dirty = true; } public void setPos(Vec2d pos) { this.pos = pos; this.dirty = true; } public void setText(String text) { this.text = text; this.dirty = true; } public void setAlign(GUIAlignment alignment) { this.alignment = alignment; this.dirty = true; } @Override public void render(Vec2d mousePos) { if(this.dirty) { this.updateMatrix(); this.dirty = false; } ModelGui model; boolean mouseHover = InputMode.Controller ? this.selected : this.checkMouseHover(mousePos); if(mouseHover) { model = Models.UI_BUTTON_HOVER; } else { model = Models.UI_BUTTON; } model.setModel(matrix); model.render(); Text.render(text, matrix_text); } @Override public boolean checkMouseHover(Vec2d pos) { if(this.dirty) { this.updateMatrix(); this.dirty = false; } double offset = getOffset(); ModelGui model = Models.UI_BUTTON; return (pos.x > this.pos.x - offset && pos.x < this.pos.x + model.getWidth() - offset && pos.y > this.pos.y && pos.y < this.pos.y + model.getHeight()); } @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)); } } @Override public void update(Vec2d mousePos) { } @Override public void onRightClick(Vec2d pos) { } }