Started working on buttons

This commit is contained in:
jsrobson10 2020-06-16 16:24:29 +10:00
parent eacf39f27b
commit 7b8de0fa63
11 changed files with 101 additions and 87 deletions

BIN
layer.bdf

Binary file not shown.

View File

@ -20,6 +20,8 @@ public class DisplayRenderUI
public static boolean showPos = false; public static boolean showPos = false;
public static int guiScale = 2; public static int guiScale = 2;
public static Matrix4 camera, projection;
public static void renderGameGui() public static void renderGameGui()
{ {
{ {
@ -85,8 +87,8 @@ public class DisplayRenderUI
public static void render() public static void render()
{ {
Matrix4 camera = Matrix4.identity(); camera = Matrix4.identity();
Matrix4 projection = Matrix4.scale(new Vec3d(0.1/GlHelpers.getAspectRatio(), 0.1, 1)); projection = Matrix4.scale(new Vec3d(0.1/GlHelpers.getAspectRatio(), 0.1, 1));
GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, projection.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, projection.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_camera, true, camera.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_camera, true, camera.getArray());
@ -99,6 +101,6 @@ public class DisplayRenderUI
} }
// Render the loaded menu // Render the loaded menu
//Main.menu.render(); Main.menu.render();
} }
} }

View File

@ -76,13 +76,13 @@ public class DisplayWindow implements IMainloopTask
width = w.get()*4; width = w.get()*4;
height = h.get()*4; height = h.get()*4;
GLFW.glfwWindowHint(GLFW.GLFW_DOUBLEBUFFER, GLFW.GLFW_FALSE); //GLFW.glfwWindowHint(GLFW.GLFW_DOUBLEBUFFER, GLFW.GLFW_FALSE);
// Create the window // Create the window
window = GraphicsHelpers.initWindow("Project Zombie", width, height, monitor); window = GraphicsHelpers.initWindow("Project Zombie", width, height, monitor);
// Make the context current // Make the context current
//GLFW.glfwMakeContextCurrent(this.window); GLFW.glfwMakeContextCurrent(this.window);
// Set the key handlers // Set the key handlers
GLFW.glfwSetCursorPosCallback(this.window, new CursorPosCallback()); GLFW.glfwSetCursorPosCallback(this.window, new CursorPosCallback());

View File

@ -30,10 +30,10 @@ public class Models
public static final Model TILE_LAVA_FLOW = new ModelTile(Resources.ATLAS.get("/tile/lava_flow.png"), 16, 50); public static final Model TILE_LAVA_FLOW = new ModelTile(Resources.ATLAS.get("/tile/lava_flow.png"), 16, 50);
public static final Model TILE_LANTERN = new ModelVertical(Resources.ATLAS.get("/tile/lantern.png"), 4, 5); public static final Model TILE_LANTERN = new ModelVertical(Resources.ATLAS.get("/tile/lantern.png"), 4, 5);
public static final Model ENTITY_BOSS_IDLE = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_idle.png"), new Vec2d(4, 4)); public static final Model ENTITY_BOSS_IDLE = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_still.png"), new Vec2d(4, 4), 4, 50);
public static final Model ENTITY_BOSS_FIRING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_firing.png"), new Vec2d(4, 4), 4, 10); public static final Model ENTITY_BOSS_FIRING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_firing.png"), new Vec2d(4, 4), 4, 50);
public static final Model ENTITY_BOSS_WALKING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_walking.png"), new Vec2d(4, 4), 4, 10); public static final Model ENTITY_BOSS_WALKING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_walking.png"), new Vec2d(4, 4), 4, 50);
public static final Model ENTITY_BOSS_WALKING_AND_FIRING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_walking_firing.png"), new Vec2d(4, 4), 4, 10); public static final Model ENTITY_BOSS_WALKING_AND_FIRING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_walking_firing.png"), new Vec2d(4, 4), 4, 50);
public static final Model ENTITY_TNT = new ModelVertical(Resources.ATLAS.get("/entity/tnt.png")); public static final Model ENTITY_TNT = new ModelVertical(Resources.ATLAS.get("/entity/tnt.png"));
public static final Model ENTITY_FLARE = new ModelVertical(Resources.ATLAS.get("/entity/flare.png")); public static final Model ENTITY_FLARE = new ModelVertical(Resources.ATLAS.get("/entity/flare.png"));
public static final Model ENTITY_DUMMY = new ModelVertical(Resources.ATLAS.get("/entity/dummy.png")); public static final Model ENTITY_DUMMY = new ModelVertical(Resources.ATLAS.get("/entity/dummy.png"));
@ -53,8 +53,8 @@ public class Models
new ModelVertical(Resources.ATLAS.get("/particle/smoke_4.png")), new ModelVertical(Resources.ATLAS.get("/particle/smoke_4.png")),
new ModelVertical(Resources.ATLAS.get("/particle/smoke_5.png"))); new ModelVertical(Resources.ATLAS.get("/particle/smoke_5.png")));
public static final ModelGui UI_BUTTON = new ModelGui(Resources.ATLAS.get("/gui/button.png")); public static final ModelGui UI_BUTTON = new ModelGui(Resources.ATLAS.get("/gui/button_normal.png"), new Vec2d(12, 1.5));
public static final ModelGui UI_BUTTON_HOVER = new ModelGui(Resources.ATLAS.get("/tile/tree.png")); public static final ModelGui UI_BUTTON_HOVER = new ModelGui(Resources.ATLAS.get("/gui/button_hover.png"), new Vec2d(12, 1.5));
public static final ModelGui UI_HEALTH_FG = new ModelGui(Resources.ATLAS.get("/gui/health_full.png"), new Vec2d(8, 0.5)); public static final ModelGui UI_HEALTH_FG = new ModelGui(Resources.ATLAS.get("/gui/health_full.png"), new Vec2d(8, 0.5));
public static final ModelGui UI_HEALTH_BG = new ModelGui(Resources.ATLAS.get("/gui/health_empty.png"), new Vec2d(8, 0.5)); public static final ModelGui UI_HEALTH_BG = new ModelGui(Resources.ATLAS.get("/gui/health_empty.png"), new Vec2d(8, 0.5));
@ -74,7 +74,7 @@ public class Models
public static final ModelItem ITEM_TNT = new ModelItem(Resources.ATLAS.get("/entity/tnt.png")); public static final ModelItem ITEM_TNT = new ModelItem(Resources.ATLAS.get("/entity/tnt.png"));
public static final ModelItem ITEM_FLARE = new ModelItem(Resources.ATLAS.get("/entity/flare.png")); public static final ModelItem ITEM_FLARE = new ModelItem(Resources.ATLAS.get("/entity/flare.png"));
public static final ModelItem ITEM_LANTERN = new ModelItem(Resources.ATLAS.get("/tile/lantern.png"), 4, 5); public static final ModelItem ITEM_LANTERN = new ModelItem(Resources.ATLAS.get("/tile/lantern.png"), 4, 5);
public static final ModelItem ITEM_SPAWN_ZOMBIE = new ModelItem(Resources.ATLAS.get("/entity/zombie_back_moving.png"), 4, 10); public static final ModelItem ITEM_SPAWN_ZOMBIE = new ModelItem(Resources.ATLAS.get("/entity/zombie_front_moving.png"), 4, 10);
public static final ModelItem ITEM_SPAWN_DUMMY = new ModelItem(Resources.ATLAS.get("/entity/dummy.png")); public static final ModelItem ITEM_SPAWN_DUMMY = new ModelItem(Resources.ATLAS.get("/entity/dummy.png"));
// Player Back White Varient // Player Back White Varient

View File

@ -2,10 +2,13 @@ package projectzombie.menu.gui;
import gl_engine.matrix.Matrix4; import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d; import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.display.DisplayRenderUI;
import projectzombie.init.Models; import projectzombie.init.Models;
import projectzombie.input.InputMode; import projectzombie.input.InputMode;
import projectzombie.model.Model; import projectzombie.model.Model;
import projectzombie.model.ModelGui;
import projectzombie.text.Text; import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers; import projectzombie.util.gl.GlHelpers;
@ -14,55 +17,70 @@ public class Button implements GUIComponent, GUISelectable
private Vec2d pos = new Vec2d(0, 0); private Vec2d pos = new Vec2d(0, 0);
private String text = ""; private String text = "";
private Alignment alignment = Alignment.CENTRE; private Alignment alignment = Alignment.CENTRE;
private Matrix4 matrix, matrix_text;
private boolean selected = false; private boolean selected = false;
private boolean dirty = true;
private GUISelectable[] SELECTABLE = {null, null, null, null}; private GUISelectable[] SELECTABLE = {null, null, null, null};
public static final Vec2d textSize = new Vec2d(0.5, 0.5); private void updateMatrix()
{
double offset;
switch(alignment) {
case CENTRE:
offset = Models.UI_BUTTON.getWidth() / 2;
break;
case RIGHT:
offset = Models.UI_BUTTON.getWidth();
break;
default:
offset = 0;
}
matrix = Matrix4.translate(pos.x - offset, 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 Button()
{
this.text = "";
this.alignment = Alignment.CENTRE;
this.dirty = true;
}
public void setPos(Vec2d pos) { public void setPos(Vec2d pos) {
this.pos = pos; this.pos = pos;
this.dirty = true;
} }
public void setText(String text) { public void setText(String text) {
this.text = text; this.text = text;
this.dirty = true;
} }
public void setAlign(Alignment alignment) { public void setAlign(Alignment alignment) {
this.alignment = alignment; this.alignment = alignment;
this.dirty = true;
} }
@Override @Override
public void render(Vec2d mousePos) public void render(Vec2d mousePos)
{ {
double m = 3; if(this.dirty) {
double w = textSize.x * m * 8; this.updateMatrix();
double h = textSize.x * m / 2; this.dirty = false;
double w1 = 0;
double w2 = 0;
double wt = 0;
if(alignment == Alignment.LEFT) {
w1 = 0;
w2 = w;
wt = w/2;
} }
if(alignment == Alignment.CENTRE) { ModelGui model;
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); boolean mouseHover = InputMode.Controller ? this.selected : this.checkMouseHover(mousePos);
if(mouseHover) { if(mouseHover) {
model = Models.UI_BUTTON_HOVER; model = Models.UI_BUTTON_HOVER;
@ -70,55 +88,34 @@ public class Button implements GUIComponent, GUISelectable
model = Models.UI_BUTTON; model = Models.UI_BUTTON;
} }
Matrix4 matrix = Matrix4.translate(pos.x, pos.y, 0); model.setModel(matrix);
model.bind();
model.render(); model.render();
if(mouseHover) { Text.render(text, matrix_text);
//GlHelpers.color3(0.8, 0.8, 0.8);
} else {
//GlHelpers.color3(0.68, 0.68, 0.68);
}
//Text.render(text, textSize);
} }
@Override @Override
public boolean checkMouseHover(Vec2d pos) { public boolean checkMouseHover(Vec2d pos)
{
double mx = pos.x / Main.window.getWidth() * 20 - 10; if(this.dirty) {
double my = pos.y / Main.window.getHeight() * 20 - 10; this.updateMatrix();
this.dirty = false;
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) { double mx = -pos.x / Main.window.getWidth() * 2 + 1;
w1 = -w/2; double my = -pos.y / Main.window.getHeight() * 2 + 1;
w2 = w/2;
}
if(alignment == Alignment.RIGHT) { ModelGui model = Models.UI_BUTTON;
w1 = -w; Matrix4 mat = Matrix4.multiply(matrix, DisplayRenderUI.projection);
w2 = 0; Vec3d point_s = Matrix4.multiply(mat, this.pos.xyn());
} Vec3d point_e = Matrix4.multiply(mat, new Vec3d(
model.getWidth() + this.pos.x,
model.getHeight() + this.pos.y, 0));
return ( //System.out.println("mx="+mx+" my="+my+" bx="+point.x+" by="+point.y+" w="+Models.UI_BUTTON.getWidth()+" h="+Models.UI_BUTTON.getHeight());
mx > w1 + this.pos.x &&
mx < w2 + this.pos.x && return (mx > point_s.x && mx < point_e.x &&
my > -h - this.pos.y && my > point_s.y && my < point_e.y);
my < h - this.pos.y);
} }
@Override @Override

View File

@ -14,7 +14,7 @@ public class ButtonGroup implements GUIContainer
for(int i=0;i<buttons.size();i++) { for(int i=0;i<buttons.size();i++) {
Button b = buttons.get(i); Button b = buttons.get(i);
b.setPos(new Vec2d(pos.x, pos.y + Button.textSize.y * -i * 3.6)); b.setPos(new Vec2d(pos.x, pos.y + 0.5 * -i * 3.6));
if(i > 0) { if(i > 0) {
b.setNeighbour(buttons.get(i - 1), GUISelectableDirection.UP); b.setNeighbour(buttons.get(i - 1), GUISelectableDirection.UP);

View File

@ -15,7 +15,8 @@ public class ButtonBasic extends Button {
public ButtonBasic(String text, Vec2d pos, ButtonCallback callback) { public ButtonBasic(String text, Vec2d pos, ButtonCallback callback) {
this(text, callback); this(text, callback);
this.setPos(pos);
setPos(pos);
} }
@Override @Override

View File

@ -32,8 +32,12 @@ public abstract class Model
private static Model bound = null; private static Model bound = null;
private void generate() protected void generate()
{ {
if(loaded) {
return;
}
float[] verticies = this.getVerticies(); float[] verticies = this.getVerticies();
TextureRef3D[] refs = this.getTextures(); TextureRef3D[] refs = this.getTextures();
@ -109,4 +113,8 @@ public abstract class Model
public void setModel(Matrix4 model) { public void setModel(Matrix4 model) {
GL33.glUniformMatrix4fv(Main.window.glsl_model, true, model.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_model, true, model.getArray());
} }
public boolean isLoaded() {
return loaded;
}
} }

View File

@ -70,10 +70,12 @@ public class ModelGui extends Model
@Override @Override
public double getHeight() { public double getHeight() {
generate();
return height; return height;
} }
public double getWidth() { public double getWidth() {
generate();
return width; return width;
} }
} }

View File

@ -69,6 +69,7 @@ public class ModelVertical extends Model
@Override @Override
public double getHeight() { public double getHeight() {
generate();
return height; return height;
} }
} }

View File

@ -43,8 +43,8 @@ public class Text
public static final Model CHAR_e = new ModelGui(Resources.ATLAS.get("/text/char_l_e.png")); public static final Model CHAR_e = new ModelGui(Resources.ATLAS.get("/text/char_l_e.png"));
public static final Model CHAR_f = new ModelGui(Resources.ATLAS.get("/text/char_l_f.png")); public static final Model CHAR_f = new ModelGui(Resources.ATLAS.get("/text/char_l_f.png"));
public static final Model CHAR_g = new ModelGui(Resources.ATLAS.get("/text/char_l_g.png")); public static final Model CHAR_g = new ModelGui(Resources.ATLAS.get("/text/char_l_g.png"));
public static final Model CHAR_i = new ModelGui(Resources.ATLAS.get("/text/char_l_h.png")); public static final Model CHAR_i = new ModelGui(Resources.ATLAS.get("/text/char_l_i.png"));
public static final Model CHAR_h = new ModelGui(Resources.ATLAS.get("/text/char_l_i.png")); public static final Model CHAR_h = new ModelGui(Resources.ATLAS.get("/text/char_l_h.png"));
public static final Model CHAR_j = new ModelGui(Resources.ATLAS.get("/text/char_l_j.png")); public static final Model CHAR_j = new ModelGui(Resources.ATLAS.get("/text/char_l_j.png"));
public static final Model CHAR_k = new ModelGui(Resources.ATLAS.get("/text/char_l_k.png")); public static final Model CHAR_k = new ModelGui(Resources.ATLAS.get("/text/char_l_k.png"));
public static final Model CHAR_l = new ModelGui(Resources.ATLAS.get("/text/char_l_l.png")); public static final Model CHAR_l = new ModelGui(Resources.ATLAS.get("/text/char_l_l.png"));
@ -188,10 +188,13 @@ public class Text
case('<'): l = CHAR_L_THAN; break; case('<'): l = CHAR_L_THAN; break;
} }
Matrix4 model = Matrix4.multiply(Matrix4.translate(i, 0, 0), matrix); if(l != null)
{
Matrix4 model = Matrix4.multiply(Matrix4.translate(i, 0, 0), matrix);
l.setModel(model); l.setModel(model);
l.render(); l.render();
}
} }
} }
} }