chore(settings): "properly" implement settings

This commit is contained in:
2025-10-02 10:58:56 +07:00
parent b42cd6d263
commit bc3dee458c
4 changed files with 140 additions and 112 deletions

View File

@ -94,6 +94,9 @@ public class Game extends ApplicationAdapter {
@Override
public void dispose() {
// Save settings
Settings.saveSettings();
// Dispose resources
introScene.dispose();
mainMenuScene.dispose();
Assets.dispose();

View File

@ -0,0 +1,65 @@
package org.vibecoders.moongazer;
import com.badlogic.gdx.Input;
import java.util.HashMap;
import org.slf4j.Logger;
public class Settings {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(Settings.class);
private static float masterVolume = 1.0f;
private static float musicVolume = 1.0f;
private static float sfxVolume = 1.0f;
public static HashMap<String, Integer> keybinds = new HashMap<>() {
{
put("p1_left", Input.Keys.LEFT);
put("p1_right", Input.Keys.RIGHT);
put("p2_left", Input.Keys.A);
put("p2_right", Input.Keys.D);
}
};
public static int getKeybind(String action) {
return keybinds.getOrDefault(action, Input.Keys.UNKNOWN);
}
public static float getMasterVolume() {
return masterVolume;
}
public static float getMusicVolume() {
return musicVolume;
}
public static float getSfxVolume() {
return sfxVolume;
}
public static void setKeybind(String action, int keycode) {
keybinds.put(action, keycode);
}
public static void setMasterVolume(float volume) {
masterVolume = Math.max(0, Math.min(1, volume));
}
public static void setMusicVolume(float volume) {
musicVolume = Math.max(0, Math.min(1, volume));
}
public static void setSfxVolume(float volume) {
sfxVolume = Math.max(0, Math.min(1, volume));
}
public static void saveSettings() {
log.debug("Saving settings: Master Volume = {}, Music Volume = {}, SFX Volume = {}", masterVolume, musicVolume, sfxVolume);
for (java.util.Map.Entry<String, Integer> entry : keybinds.entrySet()) {
log.debug("Keybind: {} = {}", entry.getKey(), Input.Keys.toString(entry.getValue()));
}
}
public static void loadSettings() {
}
}

View File

@ -31,7 +31,7 @@ public class Intro extends Scene {
Assets.loadAll();
// Create scenes
game.mainMenuScene = new MainMenu(game);
game.settingsScene = new Settings(game);
game.settingsScene = new SettingsScene(game);
game.gameScenes.add(game.mainMenuScene);
game.gameScenes.add(game.settingsScene);
}

View File

@ -14,66 +14,53 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import static org.vibecoders.moongazer.Constants.*;
import org.vibecoders.moongazer.Game;
import org.vibecoders.moongazer.State;
import org.vibecoders.moongazer.Settings;
import org.vibecoders.moongazer.managers.Assets;
import org.vibecoders.moongazer.ui.UIImageButton;
import static org.vibecoders.moongazer.Constants.WINDOW_HEIGHT;
import static org.vibecoders.moongazer.Constants.WINDOW_WIDTH;
import java.util.HashMap;
public class Settings extends Scene {
import static org.vibecoders.moongazer.Constants.*;
public class SettingsScene extends Scene {
private Table mainPanel;
private boolean isEditingKeybind = false;
private TextButton currentEditingButton = null;
private String currentKeybindAction = "";
private java.util.Map<String, Integer> keybinds = new java.util.HashMap<>();
private java.util.Map<String, TextButton> keybindButtons = new java.util.HashMap<>();
private HashMap<String, TextButton> keybindButtons = new HashMap<>();
public Settings(Game game) {
public SettingsScene(Game game) {
super(game);
initializeDefaultKeybinds();
setupUI();
initUI();
}
private void initializeDefaultKeybinds() {
keybinds.put("player1_left", Input.Keys.LEFT);
keybinds.put("player1_right", Input.Keys.RIGHT);
keybinds.put("player2_left", Input.Keys.A);
keybinds.put("player2_right", Input.Keys.D);
}
private void setupUI() {
private void initUI() {
root.setFillParent(true);
root.clear();
BitmapFont font = Assets.getFont("ui", 24);
Label.LabelStyle labelStyle = new Label.LabelStyle(font, Color.WHITE);
// 1. Create main panel
mainPanel = new Table();
mainPanel.setSize(800, 600);
mainPanel.setPosition(
(Gdx.graphics.getWidth() - 800) / 2f,
(Gdx.graphics.getHeight() - 600) / 2f
);
(Gdx.graphics.getHeight() - 600) / 2f);
// 2. Blurred rectangle background for rows
TextureRegionDrawable rowBgDrawable = new TextureRegionDrawable(Assets.getWhiteTexture());
Color bgColor = new Color(0.2f, 0.2f, 0.2f, 0.3f);
Drawable tintedBg = rowBgDrawable.tint(bgColor);
// 3. Add title "Settings"
Label titleLabel = new Label("Settings", new Label.LabelStyle(font, Color.WHITE));
mainPanel.add(titleLabel).colspan(2).padTop(60).padBottom(40);
mainPanel.row();
// 4. Add volume sliders rows (WIP)
String[] volumeLabels = {"Master Volume", "Music Volume", "SFX Volume"};
// TODO: Volume Sliders
String[] volumeLabels = { "Master Volume", "Music Volume", "SFX Volume" };
for (String volumeLabel : volumeLabels) {
Table volumeRow = new Table();
volumeRow.setBackground(tintedBg);
@ -85,15 +72,13 @@ public class Settings extends Scene {
mainPanel.row();
}
// 5. Keybinds Section
Table keybindsSection = new Table();
keybindsSection.setBackground(tintedBg);
// Keybinds title
keybindsSection.add(new Label("Keybinds", labelStyle)).colspan(2).expandX().left().padLeft(40).padTop(15).padBottom(10);
keybindsSection.add(new Label("Keybinds", labelStyle)).colspan(2).expandX().left().padLeft(40).padTop(15)
.padBottom(10);
keybindsSection.row();
// Normal keybinds
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.fontColor = Color.BLACK;
@ -101,50 +86,43 @@ public class Settings extends Scene {
Color buttonColor = new Color(0.7f, 0.7f, 0.7f, 1.0f);
textButtonStyle.up = keybindBgDrawable.tint(buttonColor);
// Editing keybinds
TextButton.TextButtonStyle editingButtonStyle = new TextButton.TextButtonStyle();
editingButtonStyle.font = font;
editingButtonStyle.fontColor = Color.WHITE;
Color editingColor = new Color(0.3f, 0.3f, 0.8f, 1.0f);
editingButtonStyle.up = keybindBgDrawable.tint(editingColor);
// Player 1
keybindsSection.add(new Label(" Player 1", labelStyle)).colspan(2).left().padLeft(60).padBottom(8);
keybindsSection.row();
// Move Left
keybindsSection.add(new Label(" Move Left", labelStyle)).left().padLeft(80);
TextButton player1LeftButton = new TextButton(getKeyName(keybinds.get("player1_left")), textButtonStyle);
setupKeybindButton(player1LeftButton, "player1_left", textButtonStyle, editingButtonStyle);
keybindButtons.put("player1_left", player1LeftButton);
TextButton player1LeftButton = new TextButton(getKeyName(Settings.keybinds.get("p1_left")), textButtonStyle);
setupKeybindButton(player1LeftButton, "p1_left", textButtonStyle, editingButtonStyle);
keybindButtons.put("p1_left", player1LeftButton);
keybindsSection.add(player1LeftButton).width(150).right().padRight(40).padBottom(5);
keybindsSection.row();
// Move Right
keybindsSection.add(new Label(" Move Right", labelStyle)).left().padLeft(80);
TextButton player1RightButton = new TextButton(getKeyName(keybinds.get("player1_right")), textButtonStyle);
setupKeybindButton(player1RightButton, "player1_right", textButtonStyle, editingButtonStyle);
keybindButtons.put("player1_right", player1RightButton);
TextButton player1RightButton = new TextButton(getKeyName(Settings.keybinds.get("p1_right")), textButtonStyle);
setupKeybindButton(player1RightButton, "p1_right", textButtonStyle, editingButtonStyle);
keybindButtons.put("p1_right", player1RightButton);
keybindsSection.add(player1RightButton).width(150).right().padRight(40).padBottom(10);
keybindsSection.row();
// Player 2
keybindsSection.add(new Label(" Player 2", labelStyle)).colspan(2).left().padLeft(60).padTop(5).padBottom(8);
keybindsSection.row();
// Player 2 Move Left
keybindsSection.add(new Label(" Move Left", labelStyle)).left().padLeft(80);
TextButton player2LeftButton = new TextButton(getKeyName(keybinds.get("player2_left")), textButtonStyle);
setupKeybindButton(player2LeftButton, "player2_left", textButtonStyle, editingButtonStyle);
keybindButtons.put("player2_left", player2LeftButton);
TextButton player2LeftButton = new TextButton(getKeyName(Settings.keybinds.get("p2_left")), textButtonStyle);
setupKeybindButton(player2LeftButton, "p2_left", textButtonStyle, editingButtonStyle);
keybindButtons.put("p2_left", player2LeftButton);
keybindsSection.add(player2LeftButton).width(150).right().padRight(40).padBottom(5);
keybindsSection.row();
// Player 2 Move Right
keybindsSection.add(new Label(" Move Right", labelStyle)).left().padLeft(80);
TextButton player2RightButton = new TextButton(getKeyName(keybinds.get("player2_right")), textButtonStyle);
setupKeybindButton(player2RightButton, "player2_right", textButtonStyle, editingButtonStyle);
keybindButtons.put("player2_right", player2RightButton);
TextButton player2RightButton = new TextButton(getKeyName(Settings.keybinds.get("p2_right")), textButtonStyle);
setupKeybindButton(player2RightButton, "p2_right", textButtonStyle, editingButtonStyle);
keybindButtons.put("p2_right", player2RightButton);
keybindsSection.add(player2RightButton).width(150).right().padRight(40).padBottom(15);
keybindsSection.row();
@ -170,46 +148,55 @@ public class Settings extends Scene {
game.stage.addListener(new InputListener() {
@Override
public boolean keyDown(InputEvent event, int keycode) {
if (isEditingKeybind && currentEditingButton != null) {
// ESC not allowed, cancel editing
if (isEditingKeybind) {
if (currentEditingButton != null) {
// ESC not allowed, cancel editing
if (keycode == Input.Keys.ESCAPE) {
cancelKeybindEdit();
return true;
}
// Check if key is already used
if (isKeybindAlreadyUsed(keycode, currentKeybindAction)) {
// Error_msg
currentEditingButton.setText("Key in use!");
// Delay
new Thread(() -> {
try {
Thread.sleep(1000);
Gdx.app.postRunnable(() -> {
if (isEditingKeybind && currentEditingButton != null) {
currentEditingButton.setText("Press Key...");
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}).start();
return true;
}
// Update keybind
Settings.keybinds.put(currentKeybindAction, keycode);
currentEditingButton.setText(getKeyName(keycode));
finishKeybindEdit();
return true;
}
} else {
if (keycode == Input.Keys.ESCAPE) {
cancelKeybindEdit();
game.transition = new Transition(game, SettingsScene.this, game.mainMenuScene,
State.MAIN_MENU, 350);
return true;
}
// Check if key is already used
if (isKeybindAlreadyUsed(keycode, currentKeybindAction)) {
//Error_msg
currentEditingButton.setText("Key in use!");
// Delay
new Thread(() -> {
try {
Thread.sleep(1000);
Gdx.app.postRunnable(() -> {
if (isEditingKeybind && currentEditingButton != null) {
currentEditingButton.setText("Press Key...");
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}).start();
return true;
}
// Update keybind
keybinds.put(currentKeybindAction, keycode);
currentEditingButton.setText(getKeyName(keycode));
finishKeybindEdit();
return true;
}
return false;
}
});
}
private void setupKeybindButton(TextButton button, String action, TextButton.TextButtonStyle normalStyle, TextButton.TextButtonStyle editingStyle) {
private void setupKeybindButton(TextButton button, String action, TextButton.TextButtonStyle normalStyle,
TextButton.TextButtonStyle editingStyle) {
button.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
@ -251,43 +238,18 @@ public class Settings extends Scene {
private void cancelKeybindEdit() {
if (currentEditingButton != null) {
// Reset text
currentEditingButton.setText(getKeyName(keybinds.get(currentKeybindAction)));
currentEditingButton.setText(getKeyName(Settings.getKeybind(currentKeybindAction)));
}
finishKeybindEdit();
}
private String getKeyName(int keycode) {
switch (keycode) {
case Input.Keys.LEFT:
return "Left Arrow";
case Input.Keys.RIGHT:
return "Right Arrow";
case Input.Keys.UP:
return "Up Arrow";
case Input.Keys.DOWN:
return "Down Arrow";
case Input.Keys.SPACE:
return "Space_Bar";
case Input.Keys.SHIFT_LEFT:
return "Left_Shift";
case Input.Keys.SHIFT_RIGHT:
return "Right_Shift";
case Input.Keys.CONTROL_LEFT:
return "Left_Ctrl";
case Input.Keys.CONTROL_RIGHT:
return "Right_Ctrl";
case Input.Keys.ALT_LEFT:
return "Left_Alt";
case Input.Keys.ALT_RIGHT:
return "Right_Alt";
default:
String keyName = Input.Keys.toString(keycode);
return keyName != null ? keyName.toUpperCase() : "Unknown";
}
String keyName = Input.Keys.toString(keycode);
return keyName != null ? keyName.toUpperCase() : "Unknown";
}
private boolean isKeybindAlreadyUsed(int keycode, String currentAction) {
for (java.util.Map.Entry<String, Integer> entry : keybinds.entrySet()) {
for (java.util.Map.Entry<String, Integer> entry : Settings.keybinds.entrySet()) {
if (!entry.getKey().equals(currentAction) && entry.getValue().equals(keycode)) {
return true;
}
@ -297,9 +259,7 @@ public class Settings extends Scene {
@Override
public void render(SpriteBatch batch) {
if (game.mainMenuScene != null) {
game.mainMenuScene.render(batch);
}
game.mainMenuScene.render(batch);
batch.setColor(0, 0, 0, 0.8f);
batch.draw(Assets.getWhiteTexture(), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
batch.setColor(Color.WHITE);