Pause GUI

This commit is contained in:
Randy 2019-03-24 10:54:35 +01:00
parent ea804535d2
commit 1e454b0340
6 changed files with 144 additions and 2 deletions

View File

@ -46,6 +46,7 @@ import io.cubyz.multiplayer.client.CubzClient;
import io.cubyz.multiplayer.server.CubzServer;
import io.cubyz.ui.DebugGUI;
import io.cubyz.ui.MainMenuGUI;
import io.cubyz.ui.PauseGUI;
import io.cubyz.ui.UISystem;
import io.cubyz.utils.DiscordIntegration;
import io.cubyz.utils.TextureConverter;
@ -261,7 +262,10 @@ public class Cubyz implements IGameLogic {
}
}
if (window.isKeyPressed(GLFW.GLFW_KEY_ESCAPE) && mouse.isGrabbed()) {
mouse.setGrabbed(false);
if (gameUI.getMenuGUI() == null) {
Keyboard.setKeyPressed(GLFW.GLFW_KEY_ESCAPE, false);
gameUI.setMenu(new PauseGUI());
}
}
if (mouse.isLeftButtonPressed() && !mouse.isGrabbed()) {
mouse.setGrabbed(true);

View File

@ -63,4 +63,7 @@ public abstract class Component {
public abstract void render(long nvg, Window src);
public void init(long nvg, Window src) {}
public void dispose(long nvg, Window src) {}
}

View File

@ -0,0 +1,50 @@
package io.cubyz.ui;
import org.jungle.Keyboard;
import org.jungle.Window;
import org.lwjgl.glfw.GLFW;
import io.cubyz.client.Cubyz;
import io.cubyz.ui.components.Button;
public class PauseGUI extends MenuGUI {
private Button exit;
private Button game;
@Override
public void init(long nvg) {
Cubyz.mouse.setGrabbed(false);
exit = new Button();
game = new Button();
exit.setText("Exit to main menu");
game.setText("Continue");
game.setOnAction(() -> {
Cubyz.mouse.setGrabbed(true);
Cubyz.gameUI.setMenu(null);
});
exit.setSize(200, 50);
game.setSize(200, 50);
}
@Override
public void render(long nvg, Window win) {
if (Keyboard.isKeyPressed(GLFW.GLFW_KEY_ESCAPE)) {
Keyboard.setKeyPressed(GLFW.GLFW_KEY_ESCAPE, false);
Cubyz.mouse.setGrabbed(true);
Cubyz.gameUI.setMenu(null);
}
game.setPosition(win.getWidth() / 2 - 100, 100);
exit.setPosition(win.getWidth() / 2 - 100, win.getHeight() - 100);
exit.render(nvg, win);
game.render(nvg, win);
}
@Override
public boolean isFullscreen() {
return true;
}
}

View File

@ -60,7 +60,6 @@ public class Button extends Component {
NGraphics.fillRect(x, y, width, height);
NGraphics.setColor(255, 255, 255);
NGraphics.setFont("OpenSans Bold", fontSize);
NGraphics.drawText(x + (width / 2) - ((text.length() * 5) / 2), (int) (y + (height / 2) - fontSize / 2), text);
//int ascent = NGraphics.getAscent("ahh");
}

View File

@ -0,0 +1,37 @@
package io.cubyz.ui.components;
import java.util.ArrayList;
import org.jungle.Window;
import io.cubyz.ui.Component;
public class Container extends Component {
protected ArrayList<Component> childrens;
public Container() {
childrens = new ArrayList<>();
}
public void add(Component comp) {
if (comp == this) throw new IllegalArgumentException("comp == this");
childrens.add(comp);
}
public void remove(Component comp) {
childrens.remove(comp);
}
public void remove(int index) {
childrens.remove(index);
}
@Override
public void render(long nvg, Window src) {
for (Component child : childrens) {
child.render(nvg, src);
}
}
}

View File

@ -0,0 +1,49 @@
package io.cubyz.ui.components;
import org.jungle.MouseInput;
import org.jungle.Window;
import io.cubyz.client.Cubyz;
import io.cubyz.ui.Component;
import io.cubyz.ui.NGraphics;
public class ScrollingContainer extends Container {
int maxY = 0;
int scrollY = 0;
int scrollBarWidth = 20;
int mPickY = -1;
public void render(long nvg, Window src) {
MouseInput mouse = Cubyz.mouse;
maxY = 0;
for (Component child : childrens) {
maxY = Math.max(maxY, child.getY());
child.setY(child.getY() - scrollY);
child.render(nvg, src);
child.setY(child.getY() + scrollY);
}
if (maxY > height) {
NGraphics.setColor(0, 0, 0);
NGraphics.fillRect(x + width - scrollBarWidth, y, scrollBarWidth, 10);
if (mPickY == -1) {
if (mouse.getX() > x + width - scrollBarWidth && mouse.getX() < x + width) {
if (mouse.getY() > y && mouse.getY() < height) {
if (mouse.isLeftButtonPressed()) {
mPickY = (int) mouse.getY();
}
}
}
} else {
if (mouse.isLeftButtonPressed()) {
scrollY = (int) Math.min(maxY, mouse.getY() - mPickY);
} else {
mPickY = -1;
}
}
}
scrollY = Math.min(maxY, scrollY);
}
}