Started adding buttons, better menus

This commit is contained in:
josua 2019-12-04 19:53:54 +11:00
parent f26416a6d5
commit 9584f7c882
25 changed files with 337 additions and 190 deletions

View File

@ -4,6 +4,9 @@
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/binary-data-format-v1.3.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/binary-data-format-v1.3.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/mainloop.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/mainloop.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2/joml-1.9.16.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2/joml-1.9.16-javadoc.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2/joml-1.9.16-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/LWJGL"/> <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/LWJGL"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

BIN
resources/texmap.old.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

View File

@ -154,7 +154,7 @@ public class DisplayRenderUI
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
GlHelpers.translate2(i * 2 / aspect_ratio, 0.3); GlHelpers.translate2(i * 2 / aspect_ratio, 0.3);
player_item.item.render(new Vec2d(0, 0), new Vec2d(1.5, 1.5)); player_item.item.render(new Vec2d(0, 0), new Vec2d(1.5, 1.5));
GlHelpers.translate2(0.5 / aspect_ratio, -0.1); GlHelpers.translate2(0, -0.1);
Text.render(Integer.toString(player_item.count), text_size); Text.render(Integer.toString(player_item.count), text_size);
GlHelpers.popMatrix(); GlHelpers.popMatrix();
} }
@ -167,7 +167,7 @@ public class DisplayRenderUI
if(!item_active.isEmpty()) if(!item_active.isEmpty())
{ {
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
GlHelpers.translate2(3.5, -7.25); GlHelpers.translate2(3.2, -7.25);
Text.render(item_active.item.getName(item_active.meta), text_size); Text.render(item_active.item.getName(item_active.meta), text_size);
GlHelpers.popMatrix(); GlHelpers.popMatrix();
} }

View File

@ -26,6 +26,7 @@ public class DisplayWindow implements IMainloopTask
private int width; private int width;
private int height; private int height;
private boolean fullscreen = true; private boolean fullscreen = true;
private boolean mouseVisibility_last = false;
public int getWidth() { public int getWidth() {
return this.width; return this.width;
@ -127,10 +128,13 @@ public class DisplayWindow implements IMainloopTask
} }
public void setMouseVisibility(boolean status) { public void setMouseVisibility(boolean status) {
if(status) { if(status != mouseVisibility_last) {
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL); mouseVisibility_last = status;
} else { if(status) {
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN); GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL);
} else {
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
}
} }
} }

View File

@ -44,6 +44,9 @@ public class Textures
public static final TextureReference ITEM_GRAPPLING_HOOK = texmap.getTextureReference(18, 19, 2, 3); public static final TextureReference ITEM_GRAPPLING_HOOK = texmap.getTextureReference(18, 19, 2, 3);
public static final TextureReference ENTITY_GRAPPLING_HOOK = texmap.getTextureReference(17, 18, 0, 16); public static final TextureReference ENTITY_GRAPPLING_HOOK = texmap.getTextureReference(17, 18, 0, 16);
public static final TextureReference BUTTON = texmap.getTextureReference(18, 26, 8, 9);
public static final TextureReference BUTTON_HOVER = texmap.getTextureReference(18, 26, 9, 10);
public static final AnimationReference ENTITY_BOSS_IDLE = new AnimationReference(50, public static final AnimationReference ENTITY_BOSS_IDLE = new AnimationReference(50,
texmap.getTextureReference(20, 22, 0, 2), texmap.getTextureReference(20, 22, 0, 2),
texmap.getTextureReference(22, 24, 0, 2), texmap.getTextureReference(22, 24, 0, 2),

View File

@ -4,19 +4,19 @@ import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWCursorPosCallbackI; import org.lwjgl.glfw.GLFWCursorPosCallbackI;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.util.math.vec.Vec2d;
public class CursorPosCallback implements GLFWCursorPosCallbackI public class CursorPosCallback implements GLFWCursorPosCallbackI
{ {
private boolean keepMouse_last = true;
@Override @Override
public void invoke(long window, double x, double y) { public void invoke(long window, double x, double y) {
if(keepMouse_last != Main.menu.keepMouse) { Main.menu.input.mousePos(new Vec2d(x, y));
Main.window.setMouseVisibility(!Main.menu.keepMouse); Main.window.setMouseVisibility(!Main.menu.keepMouse);
keepMouse_last = Main.menu.keepMouse;
} if(!Main.menu.keepMouse) { if(!Main.menu.keepMouse) {
return; return;
} }
int wx = Main.window.getWidth(); int wx = Main.window.getWidth();
int wy = Main.window.getHeight(); int wy = Main.window.getHeight();
x = x / wx - 0.5; x = x / wx - 0.5;

View File

@ -35,6 +35,7 @@ public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
private boolean esc_last = false; private boolean esc_last = false;
private boolean action_last = false; private boolean action_last = false;
private boolean fullscreen_last = false; private boolean fullscreen_last = false;
private boolean fireGun_last = false;
@Override @Override
public void invoke(long window, int key, int scancode, int action, int mods) public void invoke(long window, int key, int scancode, int action, int mods)
@ -165,8 +166,14 @@ public class KeyCallback implements GLFWKeyCallbackI, IMainloopTask
} }
if(fireGun) { if(fireGun) {
fireGun_last = true;
input.fire(true); input.fire(true);
} }
else if(fireGun_last) {
fireGun_last = false;
input.fire(false);
}
} }
} }

View File

@ -1,5 +1,7 @@
package projectzombie.input.types; package projectzombie.input.types;
import projectzombie.util.math.vec.Vec2d;
public interface Input public interface Input
{ {
public void move(boolean state, double angle); public void move(boolean state, double angle);
@ -11,4 +13,5 @@ public interface Input
public void pause(boolean state); public void pause(boolean state);
public void hotbarGoto(boolean state, int pos); public void hotbarGoto(boolean state, int pos);
public void hotbarShift(boolean state, int amount); public void hotbarShift(boolean state, int amount);
public void mousePos(Vec2d pos);
} }

View File

@ -1,51 +1,12 @@
package projectzombie.input.types; package projectzombie.input.types;
import projectzombie.Main; import projectzombie.menu.gui.GUI;
import projectzombie.menu.Menu;
import projectzombie.menu.MenuDeath;
public class InputDeath implements Input public class InputDeath extends InputGUI
{ {
@Override public InputDeath(GUI gui) {
public void move(boolean state, double angle) { super(gui);
}
@Override
public void fire(boolean state) {
Menu menu = Main.menu;
if(menu instanceof MenuDeath) {
MenuDeath menu_d = (MenuDeath) menu;
menu_d.resetting = state;
}
}
@Override
public void activateTile(boolean state) {
}
@Override
public void camera(boolean state, double amount) {
}
@Override
public void itemAction(boolean state) {
}
@Override
public void itemDrop(boolean state) {
}
@Override
public void pause(boolean state) {
}
@Override
public void hotbarGoto(boolean state, int pos) {
}
@Override
public void hotbarShift(boolean state, int amount) {
} }
} }

View File

@ -0,0 +1,58 @@
package projectzombie.input.types;
import projectzombie.menu.gui.GUI;
import projectzombie.util.math.vec.Vec2d;
public class InputGUI implements Input
{
private GUI gui;
private boolean gunStateLast = false;
public InputGUI(GUI gui) {
this.gui = gui;
}
@Override
public void move(boolean state, double angle) {
this.gui.onMove(angle);
}
@Override
public void fire(boolean state) {
if(state) {
gunStateLast = true;
}
else if(gunStateLast) {
gunStateLast = false;
gui.onMouseClick();
}
}
@Override
public void activateTile(boolean state) {}
@Override
public void camera(boolean state, double amount) {}
@Override
public void itemAction(boolean state) {}
@Override
public void itemDrop(boolean state) {}
@Override
public void pause(boolean state) {}
@Override
public void hotbarGoto(boolean state, int pos) {}
@Override
public void hotbarShift(boolean state, int amount) {}
@Override
public void mousePos(Vec2d pos) {
this.gui.updateMousePos(pos);
}
}

View File

@ -3,6 +3,7 @@ package projectzombie.input.types;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.menu.MenuGamePause; import projectzombie.menu.MenuGamePause;
import projectzombie.util.math.MathHelpers; import projectzombie.util.math.MathHelpers;
import projectzombie.util.math.vec.Vec2d;
public class InputGame implements Input public class InputGame implements Input
{ {
@ -66,4 +67,8 @@ public class InputGame implements Input
Main.player.inventory_hand = MathHelpers.mod(Main.player.inventory_hand, 6); Main.player.inventory_hand = MathHelpers.mod(Main.player.inventory_hand, 6);
} }
@Override
public void mousePos(Vec2d pos) {
}
} }

View File

@ -1,54 +1,21 @@
package projectzombie.input.types; package projectzombie.input.types;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.menu.Menu;
import projectzombie.menu.MenuGame; import projectzombie.menu.MenuGame;
import projectzombie.menu.MenuGamePause; import projectzombie.menu.gui.GUI;
public class InputGamePause implements Input public class InputGamePause extends InputGUI
{ {
@Override public InputGamePause(GUI gui) {
public void move(boolean state, double angle) { super(gui);
}
@Override
public void fire(boolean state) {
Menu menu = Main.menu;
if(menu instanceof MenuGamePause) {
MenuGamePause menu_gp = (MenuGamePause) menu;
menu_gp.resetting = state;
}
}
@Override
public void activateTile(boolean state) {
}
@Override
public void camera(boolean state, double amount) {
}
@Override
public void itemAction(boolean state) {
}
@Override
public void itemDrop(boolean state) {
} }
@Override @Override
public void pause(boolean state) { public void pause(boolean state) {
super.pause(state);
Main.menu = new MenuGame(); Main.menu = new MenuGame();
Main.game_paused = false; Main.game_paused = false;
} }
@Override
public void hotbarGoto(boolean state, int pos) {
}
@Override
public void hotbarShift(boolean state, int amount) {
}
} }

View File

@ -1,60 +0,0 @@
package projectzombie.input.types;
public class InputMenu implements Input
{
@Override
public void move(boolean state, double angle) {
// TODO
}
@Override
public void fire(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void activateTile(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void camera(boolean state, double amount) {
// TODO Auto-generated method stub
}
@Override
public void itemAction(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void itemDrop(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void pause(boolean state) {
// TODO Auto-generated method stub
}
@Override
public void hotbarGoto(boolean state, int pos) {
// TODO Auto-generated method stub
}
@Override
public void hotbarShift(boolean state, int amount) {
// TODO Auto-generated method stub
}
}

View File

@ -1,21 +1,25 @@
package projectzombie.menu; package projectzombie.menu;
import projectzombie.input.types.InputDeath; import projectzombie.input.types.InputDeath;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.components.ButtonRespawn;
import projectzombie.text.Text; import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers; import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.vec.Vec2d; import projectzombie.util.math.vec.Vec2d;
public class MenuDeath extends Menu public class MenuDeath extends Menu
{ {
private GUI gui;
public boolean resetting = false;
private MenuRespawn menu_restart = new MenuRespawn();
public MenuDeath() { public MenuDeath() {
this.doGameloop = false; this.doGameloop = false;
this.doGameRender = true; this.doGameRender = true;
this.keepMouse = false; this.keepMouse = false;
this.input = new InputDeath();
this.gui = new GUI();
this.input = new InputDeath(this.gui);
gui.add(new ButtonRespawn());
} }
@Override @Override
@ -35,18 +39,11 @@ public class MenuDeath extends Menu
// Render some text to say the player is dead // Render some text to say the player is dead
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
GlHelpers.translate2(0, 4); GlHelpers.translate2(0, 2.4);
Text.renderCenter("You Died", new Vec2d(1, 1)); Text.renderCenter("You Died", new Vec2d(1, 1));
GlHelpers.popMatrix(); GlHelpers.popMatrix();
this.menu_restart.render(); gui.render();
}
@Override
public void update() {
super.update();
this.menu_restart.update(resetting);
resetting = false;
} }
} }

View File

@ -1,20 +1,25 @@
package projectzombie.menu; package projectzombie.menu;
import projectzombie.input.types.InputGamePause; import projectzombie.input.types.InputGamePause;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.components.ButtonRespawn;
import projectzombie.text.Text; import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers; import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.vec.Vec2d; import projectzombie.util.math.vec.Vec2d;
public class MenuGamePause extends Menu public class MenuGamePause extends Menu
{ {
public boolean resetting; private GUI gui;
private MenuRespawn menu_restart = new MenuRespawn();
public MenuGamePause() { public MenuGamePause() {
this.doGameloop = false; this.doGameloop = false;
this.doGameRender = true; this.doGameRender = true;
this.keepMouse = false; this.keepMouse = false;
this.input = new InputGamePause();
this.gui = new GUI();
this.input = new InputGamePause(this.gui);
gui.add(new ButtonRespawn());
} }
@Override @Override
@ -34,18 +39,10 @@ public class MenuGamePause extends Menu
// Render some text to say the game is paused // Render some text to say the game is paused
GlHelpers.pushMatrix(); GlHelpers.pushMatrix();
GlHelpers.translate2(0, 4); GlHelpers.translate2(0, 2.4);
Text.renderCenter("Game Paused", new Vec2d(1, 1)); Text.renderCenter("Game Paused", new Vec2d(1, 1));
GlHelpers.popMatrix(); GlHelpers.popMatrix();
menu_restart.render(); gui.render();
} }
@Override
public void update() {
super.update();
menu_restart.update(resetting);
resetting = false;
}
} }

View File

@ -1,18 +1,23 @@
package projectzombie.menu; package projectzombie.menu;
import projectzombie.input.types.InputMenu; import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.GUI;
public class MenuMain extends Menu public class MenuMain extends Menu
{ {
private GUI gui;
public MenuMain() { public MenuMain() {
this.doGameloop = false; this.doGameloop = false;
this.doGameRender = false; this.doGameRender = false;
this.input = new InputMenu();
this.gui = new GUI();
this.input = new InputGUI(gui);
} }
@Override @Override
public void render() { public void render() {
this.gui.render();
} }
} }

View File

@ -0,0 +1,86 @@
package projectzombie.menu.gui;
import projectzombie.Main;
import projectzombie.init.Textures;
import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.gl.texture.TextureReference;
import projectzombie.util.math.vec.Vec2d;
public class Button implements GUIComponent
{
private Vec2d pos;
private String text;
private static Vec2d textSize = new Vec2d(0.5, 0.5);
public Button(Vec2d pos, String text) {
this.pos = pos;
this.text = text;
}
@Override
public void render(Vec2d mousePos) {
GlHelpers.pushMatrix();
GlHelpers.translate2(pos.x, pos.y);
double m = 2.5;
double w = textSize.x * m * 8 / 2;
double h = textSize.x * m / 2;
TextureReference tex;
boolean mouseHover = this.checkMouseHover(mousePos);
if(mouseHover) {
tex = Textures.BUTTON_HOVER;
} else {
tex = Textures.BUTTON;
}
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.begin();
{
tex.texCoord(0, 0); GlHelpers.vertex2(-w, -h);
tex.texCoord(1, 0); GlHelpers.vertex2( w, -h);
tex.texCoord(1, 1); GlHelpers.vertex2( w, h);
tex.texCoord(0, 1); GlHelpers.vertex2(-w, h);
}
GlHelpers.end();
GlHelpers.translate2(
(-textSize.x * text.length() / 2)/GlHelpers.getAspectRatio(),
-textSize.y / 2
);
if(mouseHover) {
GlHelpers.color3(0.8, 0.8, 0.8);
} else {
GlHelpers.color3(0.68, 0.68, 0.68);
}
Text.render(text, textSize);
GlHelpers.popMatrix();
}
@Override
public boolean checkMouseHover(Vec2d pos) {
double m = 2.5;
double w = textSize.x * m * 8 / 2 / GlHelpers.getAspectRatio();
double h = textSize.x * m / 2;
double mx = pos.x / Main.window.getWidth() * 20 - 10;
double my = pos.y / Main.window.getHeight() * 20 - 10;
return (
mx > -w - this.pos.x &&
mx < w - this.pos.x &&
my > -h - this.pos.y &&
my < h - this.pos.y
);
}
@Override
public void onMouseClick(Vec2d pos) {
}
}

View File

@ -0,0 +1,54 @@
package projectzombie.menu.gui;
import java.util.ArrayList;
import projectzombie.util.math.vec.Vec2d;
public class GUI implements GUIComponent, GUIContainer
{
private ArrayList<GUIComponent> components = new ArrayList<GUIComponent>();
private Vec2d mousePos = new Vec2d(0, 0);
@Override
public void render(Vec2d mousePos) {
for(GUIComponent c : components) {
c.render(mousePos);
}
}
public void render() {
this.render(mousePos);
}
@Override
public void add(GUIComponent c) {
components.add(c);
}
public void updateMousePos(Vec2d pos) {
this.mousePos = pos;
}
public void onMove(double angle) {
}
@Override
public boolean checkMouseHover(Vec2d pos) {
return false;
}
@Override
public void onMouseClick(Vec2d pos) {
for(GUIComponent c : components) {
if(c.checkMouseHover(mousePos)) {
c.onMouseClick(mousePos);
}
}
}
public void onMouseClick() {
this.onMouseClick(mousePos);
}
}

View File

@ -0,0 +1,11 @@
package projectzombie.menu.gui;
import projectzombie.util.math.vec.Vec2d;
public interface GUIComponent
{
public void render(Vec2d mousePos);
public boolean checkMouseHover(Vec2d pos);
public void onMouseClick(Vec2d pos);
}

View File

@ -0,0 +1,6 @@
package projectzombie.menu.gui;
public interface GUIContainer
{
public void add(GUIComponent c);
}

View File

@ -0,0 +1,35 @@
package projectzombie.menu.gui.components;
import java.util.Random;
import projectzombie.Main;
import projectzombie.display.bossbar.BossBars;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Layers;
import projectzombie.menu.MenuGame;
import projectzombie.menu.gui.Button;
import projectzombie.time.GameTimer;
import projectzombie.util.math.vec.Vec2d;
public class ButtonRespawn extends Button
{
private static final Random rand = new Random();
public ButtonRespawn() {
super(new Vec2d(0, -2), "Respawn");
}
@Override
public void onMouseClick(Vec2d pos) {
super.onMouseClick(pos);
// Reset the world and the player
Layers.init(rand.nextLong());
Main.player = new EntityPlayer();
Main.menu = new MenuGame();
Main.game_paused = false;
GameTimer.resetTime();
BossBars.clear();
}
}

View File

@ -95,7 +95,7 @@ public class Text
int w = Main.window.getWidth(); int w = Main.window.getWidth();
int h = Main.window.getHeight(); int h = Main.window.getHeight();
double aspect_ratio = ((double)w) / ((double)h); double aspect_ratio = ((double)w) / ((double)h);
GlHelpers.translate2(0 - ((text.length() - 2) * size.x / 2 / aspect_ratio), 0); GlHelpers.translate2(0 - (text.length() * size.x / 2 / aspect_ratio), 0);
Text.render(text, size); Text.render(text, size);
} }
@ -204,10 +204,10 @@ public class Text
if(l != null) if(l != null)
{ {
// Render the character // Render the character
l.texCoord(0, 1); GlHelpers.vertex2(sy*(i-1), 0); l.texCoord(0, 1); GlHelpers.vertex2(sy*i, 0 );
l.texCoord(0, 0); GlHelpers.vertex2(sy*(i-1), sx); l.texCoord(0, 0); GlHelpers.vertex2(sy*i, sx);
l.texCoord(1, 0); GlHelpers.vertex2(sy*i, sx); l.texCoord(1, 0); GlHelpers.vertex2(sy*(i+1), sx);
l.texCoord(1, 1); GlHelpers.vertex2(sy*i, 0); l.texCoord(1, 1); GlHelpers.vertex2(sy*(i+1), 0 );
} }
} }

View File

@ -25,6 +25,13 @@ public class GlHelpers
{ {
private static int MATRIX_COUNT = 0; private static int MATRIX_COUNT = 0;
public static double getAspectRatio() {
int w = Main.window.getWidth();
int h = Main.window.getHeight();
double aspect_ratio = ((double)w) / ((double)h);
return aspect_ratio;
}
public static void checkMatrixCount() { public static void checkMatrixCount() {
if(MATRIX_COUNT != 0) { if(MATRIX_COUNT != 0) {
MATRIX_COUNT = 0; MATRIX_COUNT = 0;
@ -45,9 +52,7 @@ public class GlHelpers
} }
public static void vertex2(double x, double y) { public static void vertex2(double x, double y) {
int w = Main.window.getWidth(); double aspect_ratio = getAspectRatio();
int h = Main.window.getHeight();
double aspect_ratio = ((double)w) / ((double)h);
glVertex2d(x/10/aspect_ratio, y/10); glVertex2d(x/10/aspect_ratio, y/10);
} }