From 7b8de0fa63ebc52ef40ca1b62081de773dd2f8f6 Mon Sep 17 00:00:00 2001 From: jsrobson10 Date: Tue, 16 Jun 2020 16:24:29 +1000 Subject: [PATCH] Started working on buttons --- layer.bdf | Bin 369 -> 334 bytes .../display/DisplayRenderUI.java | 8 +- src/projectzombie/display/DisplayWindow.java | 4 +- src/projectzombie/init/Models.java | 14 +- src/projectzombie/menu/gui/Button.java | 129 +++++++++--------- src/projectzombie/menu/gui/ButtonGroup.java | 2 +- .../menu/gui/components/ButtonBasic.java | 3 +- src/projectzombie/model/Model.java | 10 +- src/projectzombie/model/ModelGui.java | 2 + src/projectzombie/model/ModelVertical.java | 1 + src/projectzombie/text/Text.java | 15 +- 11 files changed, 101 insertions(+), 87 deletions(-) diff --git a/layer.bdf b/layer.bdf index e309617007e0f2b3b7f9243ada7f062111e1a7f2..ab26ebd76a3ecdbc730ef32ddf3837786e9e4aef 100644 GIT binary patch delta 272 zcmey!bdG6)PQ7GJ<;G9m!=+`#aFj?;u5jfK-E&YCGXFOcb4drF5xP^O<> zmS;^=U{+#gR$P(ltclmOjhE{;j_fqgEWHb>eO@V?muSBFWZ5(orp&ao z4T@_{{#;tAs&SG3QnQZ1s$7@M$G)Dv%*&2n`7!6prYkZjlIOl$vhfMLc4W^NnSfQy fYN7k5JBClMYA#as_Ds`@n83N}j6B1FSBnDy3s!s4 delta 334 zcmX@d^pR&^Tcb0Tw2GxBXcJ4Y>;cOW1k^6ZOWA+ zB0Fs7%&bgZS*iNd)nRQ<9Y)$jg2HfhG}+5Dmjb&M$=mMvMvsBCCzr@mFk uls!^2y(&{~nMQU}T$P`eN?q{QFh)n-6E`NEiP@l_(j-~&nXy`bQY-)kdYXCw diff --git a/src/projectzombie/display/DisplayRenderUI.java b/src/projectzombie/display/DisplayRenderUI.java index 747e3df..933a823 100755 --- a/src/projectzombie/display/DisplayRenderUI.java +++ b/src/projectzombie/display/DisplayRenderUI.java @@ -20,6 +20,8 @@ public class DisplayRenderUI public static boolean showPos = false; public static int guiScale = 2; + public static Matrix4 camera, projection; + public static void renderGameGui() { { @@ -85,8 +87,8 @@ public class DisplayRenderUI public static void render() { - Matrix4 camera = Matrix4.identity(); - Matrix4 projection = Matrix4.scale(new Vec3d(0.1/GlHelpers.getAspectRatio(), 0.1, 1)); + camera = Matrix4.identity(); + 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_camera, true, camera.getArray()); @@ -99,6 +101,6 @@ public class DisplayRenderUI } // Render the loaded menu - //Main.menu.render(); + Main.menu.render(); } } diff --git a/src/projectzombie/display/DisplayWindow.java b/src/projectzombie/display/DisplayWindow.java index effb382..6fecfd4 100755 --- a/src/projectzombie/display/DisplayWindow.java +++ b/src/projectzombie/display/DisplayWindow.java @@ -76,13 +76,13 @@ public class DisplayWindow implements IMainloopTask width = w.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 window = GraphicsHelpers.initWindow("Project Zombie", width, height, monitor); // Make the context current - //GLFW.glfwMakeContextCurrent(this.window); + GLFW.glfwMakeContextCurrent(this.window); // Set the key handlers GLFW.glfwSetCursorPosCallback(this.window, new CursorPosCallback()); diff --git a/src/projectzombie/init/Models.java b/src/projectzombie/init/Models.java index d4cabd8..6c44c55 100755 --- a/src/projectzombie/init/Models.java +++ b/src/projectzombie/init/Models.java @@ -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_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_FIRING = new ModelVertical(Resources.ATLAS.get("/entity/boss1/boss_firing.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, 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, 10); + 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, 50); + 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, 50); 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_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_5.png"))); - public static final ModelGui UI_BUTTON = new ModelGui(Resources.ATLAS.get("/gui/button.png")); - public static final ModelGui UI_BUTTON_HOVER = new ModelGui(Resources.ATLAS.get("/tile/tree.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("/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_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_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_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")); // Player Back White Varient diff --git a/src/projectzombie/menu/gui/Button.java b/src/projectzombie/menu/gui/Button.java index 0237445..f34e3d8 100755 --- a/src/projectzombie/menu/gui/Button.java +++ b/src/projectzombie/menu/gui/Button.java @@ -2,10 +2,13 @@ package projectzombie.menu.gui; import gl_engine.matrix.Matrix4; import gl_engine.vec.Vec2d; +import gl_engine.vec.Vec3d; import projectzombie.Main; +import projectzombie.display.DisplayRenderUI; import projectzombie.init.Models; import projectzombie.input.InputMode; import projectzombie.model.Model; +import projectzombie.model.ModelGui; import projectzombie.text.Text; import projectzombie.util.gl.GlHelpers; @@ -14,55 +17,70 @@ public class Button implements GUIComponent, GUISelectable private Vec2d pos = new Vec2d(0, 0); private String text = ""; private Alignment alignment = Alignment.CENTRE; + private Matrix4 matrix, matrix_text; private boolean selected = false; + private boolean dirty = true; 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) { this.pos = pos; + this.dirty = true; } public void setText(String text) { this.text = text; + this.dirty = true; } public void setAlign(Alignment alignment) { this.alignment = alignment; + this.dirty = true; } @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(this.dirty) { + this.updateMatrix(); + this.dirty = false; } - 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; + ModelGui model; boolean mouseHover = InputMode.Controller ? this.selected : this.checkMouseHover(mousePos); if(mouseHover) { model = Models.UI_BUTTON_HOVER; @@ -70,55 +88,34 @@ public class Button implements GUIComponent, GUISelectable model = Models.UI_BUTTON; } - Matrix4 matrix = Matrix4.translate(pos.x, pos.y, 0); - - model.bind(); + model.setModel(matrix); 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); + Text.render(text, matrix_text); } @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; + public boolean checkMouseHover(Vec2d pos) + { + if(this.dirty) { + this.updateMatrix(); + this.dirty = false; } - if(alignment == Alignment.CENTRE) { - w1 = -w/2; - w2 = w/2; - } + double mx = -pos.x / Main.window.getWidth() * 2 + 1; + double my = -pos.y / Main.window.getHeight() * 2 + 1; - if(alignment == Alignment.RIGHT) { - w1 = -w; - w2 = 0; - } + ModelGui model = Models.UI_BUTTON; + Matrix4 mat = Matrix4.multiply(matrix, DisplayRenderUI.projection); + 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 ( - mx > w1 + this.pos.x && - mx < w2 + this.pos.x && - my > -h - this.pos.y && - my < h - this.pos.y); + //System.out.println("mx="+mx+" my="+my+" bx="+point.x+" by="+point.y+" w="+Models.UI_BUTTON.getWidth()+" h="+Models.UI_BUTTON.getHeight()); + + return (mx > point_s.x && mx < point_e.x && + my > point_s.y && my < point_e.y); } @Override diff --git a/src/projectzombie/menu/gui/ButtonGroup.java b/src/projectzombie/menu/gui/ButtonGroup.java index d24bc21..5bd5136 100755 --- a/src/projectzombie/menu/gui/ButtonGroup.java +++ b/src/projectzombie/menu/gui/ButtonGroup.java @@ -14,7 +14,7 @@ public class ButtonGroup implements GUIContainer for(int i=0;i 0) { b.setNeighbour(buttons.get(i - 1), GUISelectableDirection.UP); diff --git a/src/projectzombie/menu/gui/components/ButtonBasic.java b/src/projectzombie/menu/gui/components/ButtonBasic.java index b054662..dcb70be 100755 --- a/src/projectzombie/menu/gui/components/ButtonBasic.java +++ b/src/projectzombie/menu/gui/components/ButtonBasic.java @@ -15,7 +15,8 @@ public class ButtonBasic extends Button { public ButtonBasic(String text, Vec2d pos, ButtonCallback callback) { this(text, callback); - this.setPos(pos); + + setPos(pos); } @Override diff --git a/src/projectzombie/model/Model.java b/src/projectzombie/model/Model.java index 0b523ee..b82c931 100644 --- a/src/projectzombie/model/Model.java +++ b/src/projectzombie/model/Model.java @@ -32,8 +32,12 @@ public abstract class Model private static Model bound = null; - private void generate() + protected void generate() { + if(loaded) { + return; + } + float[] verticies = this.getVerticies(); TextureRef3D[] refs = this.getTextures(); @@ -109,4 +113,8 @@ public abstract class Model public void setModel(Matrix4 model) { GL33.glUniformMatrix4fv(Main.window.glsl_model, true, model.getArray()); } + + public boolean isLoaded() { + return loaded; + } } \ No newline at end of file diff --git a/src/projectzombie/model/ModelGui.java b/src/projectzombie/model/ModelGui.java index d1a500e..2e427d2 100644 --- a/src/projectzombie/model/ModelGui.java +++ b/src/projectzombie/model/ModelGui.java @@ -70,10 +70,12 @@ public class ModelGui extends Model @Override public double getHeight() { + generate(); return height; } public double getWidth() { + generate(); return width; } } diff --git a/src/projectzombie/model/ModelVertical.java b/src/projectzombie/model/ModelVertical.java index 9ef62b6..f30d879 100644 --- a/src/projectzombie/model/ModelVertical.java +++ b/src/projectzombie/model/ModelVertical.java @@ -69,6 +69,7 @@ public class ModelVertical extends Model @Override public double getHeight() { + generate(); return height; } } diff --git a/src/projectzombie/text/Text.java b/src/projectzombie/text/Text.java index 80c266b..efb999e 100755 --- a/src/projectzombie/text/Text.java +++ b/src/projectzombie/text/Text.java @@ -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_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_i = new ModelGui(Resources.ATLAS.get("/text/char_l_h.png")); - public static final Model CHAR_h = new ModelGui(Resources.ATLAS.get("/text/char_l_i.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_h.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_l = new ModelGui(Resources.ATLAS.get("/text/char_l_l.png")); @@ -188,10 +188,13 @@ public class Text case('<'): l = CHAR_L_THAN; break; } - Matrix4 model = Matrix4.multiply(Matrix4.translate(i, 0, 0), matrix); - - l.setModel(model); - l.render(); + if(l != null) + { + Matrix4 model = Matrix4.multiply(Matrix4.translate(i, 0, 0), matrix); + + l.setModel(model); + l.render(); + } } } }