feat(ui): Options tab
This commit is contained in:
parent
10b81d018c
commit
1d29972087
@ -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 -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,8 @@ 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;
|
||||
import cz.jzitnik.tui.utils.Menu;
|
||||
|
||||
@Slf4j
|
||||
public class Escape {
|
||||
private Game game;
|
||||
private Menu menu;
|
||||
@ -36,7 +35,7 @@ public class Escape {
|
||||
game.setWindow(Window.WORLD);
|
||||
break;
|
||||
case 1:
|
||||
// Options logic placeholder
|
||||
game.setWindow(Window.OPTIONS);
|
||||
break;
|
||||
case 2:
|
||||
game.setWindow(Window.SAVE_EXIT);
|
||||
|
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 -> {
|
||||
|
@ -1,4 +1,5 @@
|
||||
package cz.jzitnik.game.ui;
|
||||
package cz.jzitnik.tui.utils;
|
||||
|
||||
|
||||
import org.jline.terminal.MouseEvent;
|
||||
import org.jline.terminal.Terminal;
|
||||
@ -108,7 +109,7 @@ public class Menu {
|
||||
}
|
||||
}
|
||||
|
||||
interface ButtonClickHandler {
|
||||
public interface ButtonClickHandler {
|
||||
void onClick(int index, ScreenRenderer screenRenderer);
|
||||
}
|
||||
|
@ -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