Compare commits
2 Commits
c2f895bc8b
...
1d29972087
Author | SHA1 | Date | |
---|---|---|---|
1d29972087 | |||
10b81d018c |
@ -55,6 +55,7 @@ public class Game extends AutoTransientSupport {
|
||||
private transient GameStates gameStates = new GameStates(this);
|
||||
@Setter
|
||||
private int daytime = 0; // 0-600
|
||||
//
|
||||
private Configuration configuration = new Configuration();
|
||||
|
||||
public Game() {
|
||||
|
@ -12,6 +12,7 @@ import cz.jzitnik.game.mobs.EntityKill;
|
||||
import cz.jzitnik.game.smelting.Smelting;
|
||||
import cz.jzitnik.game.sprites.ui.Font;
|
||||
import cz.jzitnik.game.ui.Escape;
|
||||
import cz.jzitnik.game.ui.Options;
|
||||
|
||||
public class Dependencies {
|
||||
public PlaceHandler placeHandler = new PlaceHandler();
|
||||
@ -25,8 +26,10 @@ public class Dependencies {
|
||||
public Sound sound = new Sound();
|
||||
public Font font = new Font();
|
||||
public Escape escape;
|
||||
public Options options;
|
||||
|
||||
public Dependencies(Game game) {
|
||||
escape = new Escape(game);
|
||||
options = new Options(game);
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +207,6 @@ public class Font {
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
buffer.append("\033").append(background);
|
||||
buffer.append(" ".repeat(leftPad)).append(lines[i]);
|
||||
log.debug("Ansi escape codes: {}", Arrays.asList(lines[i].split("\033")));
|
||||
buffer.append("\033").append(background);
|
||||
buffer.append(" ".repeat(rightPad));
|
||||
buffer.append("\033[0m\n");
|
||||
|
@ -57,6 +57,8 @@ public class InputHandlerThread extends Thread {
|
||||
screenRenderer);
|
||||
case ESC -> game.getGameStates().dependencies.escape.mouse(mouseEvent, terminal, screenRenderer);
|
||||
case SAVE_EXIT -> {}
|
||||
case OPTIONS -> game.getGameStates().dependencies.options.handleMouse(mouseEvent, terminal, screenRenderer);
|
||||
case SAVED -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,26 +2,51 @@ package cz.jzitnik.game.ui;
|
||||
|
||||
import org.jline.terminal.MouseEvent;
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.sprites.ui.Font;
|
||||
import cz.jzitnik.game.sprites.ui.Font.*;
|
||||
import cz.jzitnik.tui.ScreenRenderer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import cz.jzitnik.tui.utils.Menu;
|
||||
|
||||
@Slf4j
|
||||
public class Escape {
|
||||
private Game game;
|
||||
private int btnWidth;
|
||||
private int leftPad;
|
||||
private int mainTextHeight;
|
||||
private int buttonHeight;
|
||||
private int textButtonMargin;
|
||||
|
||||
private boolean[] buttonsHover = { false, false, false };
|
||||
private Menu menu;
|
||||
|
||||
public Escape(Game game) {
|
||||
this.game = game;
|
||||
this.menu = new Menu(game, "2DCraft", new String[] { "Continue", "Options", "Save and exit" },
|
||||
this::onButtonClick);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
menu.reset();
|
||||
}
|
||||
|
||||
public void render(StringBuilder buffer, Terminal terminal) {
|
||||
menu.render(buffer, terminal);
|
||||
}
|
||||
|
||||
public void mouse(MouseEvent mouseEvent, Terminal terminal, ScreenRenderer screenRenderer) {
|
||||
menu.handleMouse(mouseEvent, terminal, screenRenderer);
|
||||
}
|
||||
|
||||
private void onButtonClick(int index, ScreenRenderer screenRenderer) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
game.setWindow(Window.WORLD);
|
||||
break;
|
||||
case 1:
|
||||
game.setWindow(Window.OPTIONS);
|
||||
break;
|
||||
case 2:
|
||||
game.setWindow(Window.SAVE_EXIT);
|
||||
screenRenderer.render(game);
|
||||
game.getGameStates().dependencies.gameSaver.save(game);
|
||||
game.setWindow(Window.SAVED);
|
||||
screenRenderer.render(game);
|
||||
System.exit(0);
|
||||
break;
|
||||
}
|
||||
screenRenderer.render(game);
|
||||
}
|
||||
|
||||
public void renderSave(StringBuilder buffer, Terminal terminal) {
|
||||
@ -43,143 +68,4 @@ public class Escape {
|
||||
buffer.append("\n".repeat(top));
|
||||
buffer.append(savingText.getData());
|
||||
}
|
||||
|
||||
public void render(StringBuilder buffer, Terminal terminal) {
|
||||
var font = game.getGameStates().dependencies.font;
|
||||
var width = terminal.getWidth();
|
||||
var height = terminal.getHeight();
|
||||
|
||||
log.debug("Terminal width: {}", width);
|
||||
log.debug("Terminal height: {}", height);
|
||||
|
||||
var twodcraft = font.line(terminal, "2DCraft", Size.LARGE, Align.CENTER);
|
||||
buffer.append(twodcraft.getData());
|
||||
mainTextHeight = twodcraft.getHeight();
|
||||
|
||||
if (height < 600) {
|
||||
textButtonMargin = 15;
|
||||
} else if (height < 800) {
|
||||
textButtonMargin = 30;
|
||||
} else {
|
||||
textButtonMargin = 50;
|
||||
}
|
||||
|
||||
buffer.append("\n".repeat(textButtonMargin));
|
||||
|
||||
renderButton(buffer, "Continue", width, font, terminal, buttonsHover[0]);
|
||||
buffer.append("\n".repeat(textButtonMargin / 2));
|
||||
renderButton(buffer, "Options", width, font, terminal, buttonsHover[1]);
|
||||
buffer.append("\n".repeat(textButtonMargin / 2));
|
||||
renderButton(buffer, "Save and exit", width, font, terminal, buttonsHover[2]);
|
||||
}
|
||||
|
||||
public void mouse(MouseEvent mouseEvent, Terminal terminal, ScreenRenderer screenRenderer) {
|
||||
int x = mouseEvent.getX();
|
||||
int y = mouseEvent.getY();
|
||||
var type = mouseEvent.getType();
|
||||
|
||||
int buttonx = x - leftPad;
|
||||
int buttony = y - (mainTextHeight + textButtonMargin);
|
||||
int margin = textButtonMargin / 2;
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
if (buttonx > 0 && buttonx <= btnWidth) {
|
||||
// Continue
|
||||
if (buttony > 0 && buttony < buttonHeight) {
|
||||
if (type == MouseEvent.Type.Pressed) {
|
||||
game.setWindow(Window.WORLD);
|
||||
screenRenderer.render(game);
|
||||
return;
|
||||
}
|
||||
|
||||
if (buttonsHover[0] == false) {
|
||||
buttonsHover[0] = true;
|
||||
changed = true;
|
||||
}
|
||||
} else if (buttonsHover[0]) {
|
||||
buttonsHover[0] = false;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// Options
|
||||
if (buttony > buttonHeight + margin && buttony < 2 * buttonHeight + margin) {
|
||||
if (type == MouseEvent.Type.Pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (buttonsHover[1] == false) {
|
||||
buttonsHover[1] = true;
|
||||
changed = true;
|
||||
}
|
||||
} else if (buttonsHover[1]) {
|
||||
buttonsHover[1] = false;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// Save and exit
|
||||
if (buttony > 2 * (buttonHeight + margin) && buttony < buttonHeight + 2 * (buttonHeight + margin)) {
|
||||
if (type == MouseEvent.Type.Pressed) {
|
||||
game.setWindow(Window.SAVE_EXIT);
|
||||
screenRenderer.render(game);
|
||||
|
||||
// Save game
|
||||
game.getGameStates().dependencies.gameSaver.save(game);
|
||||
|
||||
game.setWindow(Window.SAVED);
|
||||
screenRenderer.render(game);
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
if (buttonsHover[2] == false) {
|
||||
buttonsHover[2] = true;
|
||||
changed = true;
|
||||
}
|
||||
} else if (buttonsHover[2]) {
|
||||
buttonsHover[2] = false;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
screenRenderer.render(game);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderButton(StringBuilder buffer, String txt, int width, Font font, Terminal terminal,
|
||||
boolean selected) {
|
||||
int btnWidth = Math.min(350, (int) (width * (3.0 / 4)));
|
||||
this.btnWidth = btnWidth;
|
||||
int leftPad = (width - btnWidth) / 2;
|
||||
this.leftPad = leftPad;
|
||||
log.debug("Button width: {}px ", btnWidth);
|
||||
|
||||
final String color = selected ? "[48;2;70;70;70m" : "[48;2;116;115;113m";
|
||||
|
||||
var text = font.line(terminal, txt, Size.SMALL, Align.CENTER, new Custom.Width(btnWidth - 4),
|
||||
new Background(color));
|
||||
int btnHeight = text.getHeight() + 4;
|
||||
this.buttonHeight = btnHeight;
|
||||
var lines = text.getData().split("\n");
|
||||
|
||||
for (int y = 0; y < btnHeight; y++) {
|
||||
buffer.append(" ".repeat(leftPad));
|
||||
if (y == 0 || y == 1 || y == btnHeight - 1 || y == btnHeight - 2) {
|
||||
buffer.append("\033").append(color).append(" ".repeat(btnWidth));
|
||||
} else {
|
||||
buffer.append("\033").append(color).append(" ".repeat(2));
|
||||
buffer.append(lines[y - 2]);
|
||||
buffer.append("\033").append(color).append(" ".repeat(2));
|
||||
}
|
||||
|
||||
buffer.append("\033[0m\n");
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
for (int i = 0; i < buttonsHover.length; i++) {
|
||||
buttonsHover[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
85
src/main/java/cz/jzitnik/game/ui/Options.java
Normal file
85
src/main/java/cz/jzitnik/game/ui/Options.java
Normal file
@ -0,0 +1,85 @@
|
||||
package cz.jzitnik.game.ui;
|
||||
|
||||
import org.jline.terminal.MouseEvent;
|
||||
import org.jline.terminal.Terminal;
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.sprites.ui.Font.*;
|
||||
import cz.jzitnik.tui.ScreenRenderer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class Options {
|
||||
private Game game;
|
||||
private int top;
|
||||
private int sliderWidth;
|
||||
private int sliderLeftpad;
|
||||
|
||||
public Options(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
|
||||
public void render(StringBuilder buffer, Terminal terminal) {
|
||||
var buf = new StringBuilder();
|
||||
var font = game.getGameStates().dependencies.font;
|
||||
var config = game.getConfiguration();
|
||||
|
||||
var mainText = font.line(terminal, "Options", Align.CENTER, Size.LARGE);
|
||||
buf.append(mainText.getData());
|
||||
|
||||
buf.append("\n".repeat(15));
|
||||
|
||||
var sounds = font.line(terminal, "Sounds", Align.CENTER, Size.MEDIUM);
|
||||
buf.append(sounds.getData());
|
||||
|
||||
buf.append("\n".repeat(10));
|
||||
|
||||
top = mainText.getHeight() + 15 + sounds.getHeight() + 10;
|
||||
|
||||
renderSlider(buf, terminal, config.getSoundVolume());
|
||||
|
||||
buffer.append(buf);
|
||||
}
|
||||
|
||||
private void renderSlider(StringBuilder buffer, Terminal terminal, int percentage) {
|
||||
var defaultColor = "\033[48;2;116;115;113m";
|
||||
var fullColor = "\033[48;2;70;70;70m";
|
||||
int width = Math.min(550, (int) (terminal.getWidth() * (3.0 / 4)));
|
||||
int leftPad = (terminal.getWidth() - width) / 2;
|
||||
sliderLeftpad= leftPad;
|
||||
sliderWidth = width;
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
buffer.append(" ".repeat(leftPad));
|
||||
|
||||
for (int j = 0; j < width; j++) {
|
||||
if (((double) j / width) * 100 <= percentage) {
|
||||
buffer.append(fullColor).append(" ");
|
||||
} else {
|
||||
buffer.append(defaultColor).append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
buffer.append("\033[0m").append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
public void handleMouse(MouseEvent mouseEvent, Terminal terminal, ScreenRenderer screenRenderer) {
|
||||
int x = mouseEvent.getX();
|
||||
int y = mouseEvent.getY() - top;
|
||||
|
||||
if (x > sliderLeftpad && x <= sliderLeftpad + sliderWidth) {
|
||||
if (y > 0 && y <= 8) {
|
||||
if (mouseEvent.getType() == MouseEvent.Type.Pressed || mouseEvent.getType() == MouseEvent.Type.Dragged) {
|
||||
int sliderX = x - sliderLeftpad;
|
||||
int percentage = (int) Math.round((((double) sliderX / sliderWidth) * 100));
|
||||
|
||||
game.getConfiguration().setSoundVolume(percentage);
|
||||
log.info("Sound volume was changed to: {}% ", percentage);
|
||||
|
||||
screenRenderer.render(game);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package cz.jzitnik.game.ui;
|
||||
|
||||
public enum Window {
|
||||
WORLD, INVENTORY, CRAFTING_TABLE, CHEST, FURNACE, ESC, SAVE_EXIT, SAVED
|
||||
WORLD, INVENTORY, CRAFTING_TABLE, CHEST, FURNACE, ESC, SAVE_EXIT, SAVED, OPTIONS
|
||||
}
|
||||
|
@ -81,6 +81,7 @@ public class ScreenRenderer {
|
||||
.stream().filter(i -> i.getBlockId().equals("furnace")).toList().getFirst().getData()).render(game,
|
||||
main, terminal, spriteList);
|
||||
case ESC -> game.getGameStates().dependencies.escape.render(main, terminal);
|
||||
case OPTIONS -> game.getGameStates().dependencies.options.render(main, terminal);
|
||||
case SAVE_EXIT -> game.getGameStates().dependencies.escape.renderSave(main, terminal);
|
||||
case SAVED -> game.getGameStates().dependencies.escape.renderSaved(main, terminal);
|
||||
case WORLD -> {
|
||||
|
121
src/main/java/cz/jzitnik/tui/utils/Menu.java
Normal file
121
src/main/java/cz/jzitnik/tui/utils/Menu.java
Normal file
@ -0,0 +1,121 @@
|
||||
package cz.jzitnik.tui.utils;
|
||||
|
||||
|
||||
import org.jline.terminal.MouseEvent;
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.sprites.ui.Font;
|
||||
import cz.jzitnik.game.sprites.ui.Font.*;
|
||||
import cz.jzitnik.tui.ScreenRenderer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class Menu {
|
||||
private Game game;
|
||||
private String headingText;
|
||||
private String[] buttonLabels;
|
||||
private boolean[] buttonsHover;
|
||||
private int btnWidth;
|
||||
private int leftPad;
|
||||
private int mainTextHeight;
|
||||
private int buttonHeight;
|
||||
private int textButtonMargin;
|
||||
private ButtonClickHandler buttonClickHandler;
|
||||
|
||||
public Menu(Game game, String headingText, String[] buttonLabels, ButtonClickHandler buttonClickHandler) {
|
||||
this.game = game;
|
||||
this.headingText = headingText;
|
||||
this.buttonLabels = buttonLabels;
|
||||
this.buttonsHover = new boolean[buttonLabels.length];
|
||||
this.buttonClickHandler = buttonClickHandler;
|
||||
}
|
||||
|
||||
public void render(StringBuilder buffer, Terminal terminal) {
|
||||
var font = game.getGameStates().dependencies.font;
|
||||
var width = terminal.getWidth();
|
||||
var height = terminal.getHeight();
|
||||
|
||||
var heading = font.line(terminal, headingText, Size.LARGE, Align.CENTER);
|
||||
buffer.append(heading.getData());
|
||||
mainTextHeight = heading.getHeight();
|
||||
|
||||
textButtonMargin = (height < 600) ? 15 : (height < 800) ? 30 : 50;
|
||||
buffer.append("\n".repeat(textButtonMargin));
|
||||
|
||||
for (int i = 0; i < buttonLabels.length; i++) {
|
||||
renderButton(buffer, buttonLabels[i], width, font, terminal, buttonsHover[i]);
|
||||
buffer.append("\n".repeat(textButtonMargin / 2));
|
||||
}
|
||||
}
|
||||
|
||||
public void handleMouse(MouseEvent mouseEvent, Terminal terminal, ScreenRenderer screenRenderer) {
|
||||
int x = mouseEvent.getX();
|
||||
int y = mouseEvent.getY();
|
||||
var type = mouseEvent.getType();
|
||||
|
||||
int buttonx = x - leftPad;
|
||||
int buttony = y - (mainTextHeight + textButtonMargin);
|
||||
int margin = textButtonMargin / 2;
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
if (buttonx > 0 && buttonx <= btnWidth) {
|
||||
for (int i = 0; i < buttonLabels.length; i++) {
|
||||
int top = i * (buttonHeight + margin);
|
||||
int bottom = top + buttonHeight;
|
||||
|
||||
if (buttony > top && buttony < bottom) {
|
||||
if (type == MouseEvent.Type.Pressed) {
|
||||
buttonClickHandler.onClick(i, screenRenderer);
|
||||
return;
|
||||
}
|
||||
if (!buttonsHover[i]) {
|
||||
buttonsHover[i] = true;
|
||||
changed = true;
|
||||
}
|
||||
} else if (buttonsHover[i]) {
|
||||
buttonsHover[i] = false;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
screenRenderer.render(game);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderButton(StringBuilder buffer, String txt, int width, Font font, Terminal terminal, boolean selected) {
|
||||
btnWidth = Math.min(350, (int) (width * (3.0 / 4)));
|
||||
leftPad = (width - btnWidth) / 2;
|
||||
log.debug("Button width: {}px ", btnWidth);
|
||||
|
||||
final String color = selected ? "[48;2;70;70;70m" : "[48;2;116;115;113m";
|
||||
var text = font.line(terminal, txt, Size.SMALL, Align.CENTER, new Custom.Width(btnWidth - 4), new Background(color));
|
||||
buttonHeight = text.getHeight() + 4;
|
||||
var lines = text.getData().split("\n");
|
||||
|
||||
for (int y = 0; y < buttonHeight; y++) {
|
||||
buffer.append(" ".repeat(leftPad));
|
||||
if (y < 2 || y >= buttonHeight - 2) {
|
||||
buffer.append("\033").append(color).append(" ".repeat(btnWidth));
|
||||
} else {
|
||||
buffer.append("\033").append(color).append(" ".repeat(2));
|
||||
buffer.append(lines[y - 2]);
|
||||
buffer.append("\033").append(color).append(" ".repeat(2));
|
||||
}
|
||||
buffer.append("\033[0m\n");
|
||||
}
|
||||
}
|
||||
|
||||
public interface ButtonClickHandler {
|
||||
void onClick(int index, ScreenRenderer screenRenderer);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
for (int i = 0; i < buttonsHover.length; i++) {
|
||||
buttonsHover[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@
|
||||
<!-- Max history (how many days to keep) -->
|
||||
<maxHistory>30</maxHistory>
|
||||
<!-- Total size cap -->
|
||||
<totalSizeCap>20MB</totalSizeCap>
|
||||
<totalSizeCap>30MB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
|
Loading…
x
Reference in New Issue
Block a user