chore(scene/settings): further simplify
Wow.
This commit is contained in:
@ -11,132 +11,104 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
|||||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
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 com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
|
||||||
|
|
||||||
import org.vibecoders.moongazer.Game;
|
import org.vibecoders.moongazer.Game;
|
||||||
import org.vibecoders.moongazer.State;
|
import org.vibecoders.moongazer.State;
|
||||||
import org.vibecoders.moongazer.Settings;
|
import org.vibecoders.moongazer.Settings;
|
||||||
import org.vibecoders.moongazer.managers.Assets;
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
import org.vibecoders.moongazer.ui.UIImageButton;
|
import org.vibecoders.moongazer.ui.UIImageButton;
|
||||||
|
import org.vibecoders.moongazer.ui.UITextButton;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import static org.vibecoders.moongazer.Constants.*;
|
import static org.vibecoders.moongazer.Constants.*;
|
||||||
|
|
||||||
public class SettingsScene extends Scene {
|
public class SettingsScene extends Scene {
|
||||||
|
|
||||||
private Table mainPanel;
|
|
||||||
private boolean isEditingKeybind = false;
|
private boolean isEditingKeybind = false;
|
||||||
private TextButton currentEditingButton = null;
|
private UITextButton currentEditingButton = null;
|
||||||
private String currentKeybindAction = "";
|
private String currentKeybindAction = "";
|
||||||
|
private HashMap<String, UITextButton> keybindButtons = new HashMap<>();
|
||||||
private HashMap<String, TextButton> keybindButtons = new HashMap<>();
|
|
||||||
|
|
||||||
public SettingsScene(Game game) {
|
public SettingsScene(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
initUI();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initUI() {
|
|
||||||
root.setFillParent(true);
|
root.setFillParent(true);
|
||||||
root.clear();
|
|
||||||
|
|
||||||
BitmapFont font = Assets.getFont("ui", 24);
|
BitmapFont font = Assets.getFont("ui", 24);
|
||||||
Label.LabelStyle labelStyle = new Label.LabelStyle(font, Color.WHITE);
|
Label.LabelStyle labelStyle = new Label.LabelStyle(font, Color.WHITE);
|
||||||
|
|
||||||
mainPanel = new Table();
|
// Main panel
|
||||||
|
Table mainPanel = new Table();
|
||||||
mainPanel.setSize(800, 600);
|
mainPanel.setSize(800, 600);
|
||||||
mainPanel.setPosition(
|
mainPanel.setPosition((Gdx.graphics.getWidth() - 800) / 2f,
|
||||||
(Gdx.graphics.getWidth() - 800) / 2f,
|
|
||||||
(Gdx.graphics.getHeight() - 600) / 2f);
|
(Gdx.graphics.getHeight() - 600) / 2f);
|
||||||
|
|
||||||
TextureRegionDrawable rowBgDrawable = new TextureRegionDrawable(Assets.getWhiteTexture());
|
Label title = new Label("Settings", labelStyle);
|
||||||
Color bgColor = new Color(0.2f, 0.2f, 0.2f, 0.3f);
|
mainPanel.add(title).colspan(2).padTop(60).padBottom(40);
|
||||||
Drawable tintedBg = rowBgDrawable.tint(bgColor);
|
|
||||||
|
|
||||||
Label titleLabel = new Label("Settings", new Label.LabelStyle(font, Color.WHITE));
|
|
||||||
mainPanel.add(titleLabel).colspan(2).padTop(60).padBottom(40);
|
|
||||||
mainPanel.row();
|
mainPanel.row();
|
||||||
|
|
||||||
// TODO: Volume Sliders
|
// Volume settings
|
||||||
String[] volumeLabels = { "Master Volume", "Music Volume", "SFX Volume" };
|
TextureRegionDrawable bg = new TextureRegionDrawable(Assets.getWhiteTexture());
|
||||||
for (String volumeLabel : volumeLabels) {
|
var tintedBg = bg.tint(new Color(0.2f, 0.2f, 0.2f, 0.3f));
|
||||||
Table volumeRow = new Table();
|
|
||||||
volumeRow.setBackground(tintedBg);
|
|
||||||
|
|
||||||
volumeRow.add(new Label(volumeLabel, labelStyle)).expandX().left().padLeft(40).padTop(15).padBottom(15);
|
String[] volumes = {"Master Volume", "Music Volume", "SFX Volume"};
|
||||||
volumeRow.add(new Label("[Slider Placeholder]", labelStyle)).right().padRight(40).padTop(15).padBottom(15);
|
for (String volume : volumes) {
|
||||||
|
Table row = new Table();
|
||||||
mainPanel.add(volumeRow).width(700).height(60).padBottom(5);
|
row.setBackground(tintedBg);
|
||||||
|
row.add(new Label(volume, labelStyle)).expandX().left().padLeft(40).pad(15);
|
||||||
|
row.add(new Label("[Slider Placeholder]", labelStyle)).right().padRight(40);
|
||||||
|
mainPanel.add(row).width(700).height(60).padBottom(5);
|
||||||
mainPanel.row();
|
mainPanel.row();
|
||||||
}
|
}
|
||||||
|
|
||||||
Table keybindsSection = new Table();
|
// Keybind settings
|
||||||
keybindsSection.setBackground(tintedBg);
|
Table section = new Table();
|
||||||
|
section.setBackground(tintedBg);
|
||||||
|
section.add(new Label("Keybinds", labelStyle)).colspan(2).expandX().left()
|
||||||
|
.padLeft(20).padTop(15).padBottom(10);
|
||||||
|
section.row();
|
||||||
|
|
||||||
keybindsSection.add(new Label("Keybinds", labelStyle)).colspan(2).expandX().left().padLeft(40).padTop(15)
|
// Player keybinds
|
||||||
.padBottom(10);
|
String[] players = {"Player 1", "Player 2"};
|
||||||
keybindsSection.row();
|
String[] prefixes = {"p1", "p2"};
|
||||||
|
String[] actions = {"_left", "_right"};
|
||||||
|
String[] labels = {" Move Left", " Move Right"};
|
||||||
|
|
||||||
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
|
for (int p = 0; p < players.length; p++) {
|
||||||
textButtonStyle.font = font;
|
section.add(new Label(" " + players[p], labelStyle)).colspan(2).left()
|
||||||
textButtonStyle.fontColor = Color.BLACK;
|
.padLeft(60);
|
||||||
TextureRegionDrawable keybindBgDrawable = new TextureRegionDrawable(Assets.getWhiteTexture());
|
section.row();
|
||||||
Color buttonColor = new Color(0.7f, 0.7f, 0.7f, 1.0f);
|
for (int a = 0; a < actions.length; a++) {
|
||||||
textButtonStyle.up = keybindBgDrawable.tint(buttonColor);
|
section.add(new Label(labels[a], labelStyle)).left().padLeft(80);
|
||||||
|
String action = prefixes[p] + actions[a];
|
||||||
|
UITextButton button = new UITextButton(getKeyName(Settings.getKeybind(action)), font);
|
||||||
|
button.button.addListener(new ClickListener() {
|
||||||
|
@Override
|
||||||
|
public void clicked(InputEvent event, float x, float y) {
|
||||||
|
if (!isEditingKeybind) {
|
||||||
|
isEditingKeybind = true;
|
||||||
|
currentEditingButton = button;
|
||||||
|
currentKeybindAction = action;
|
||||||
|
((TextButton)(button.button)).setText("Press Key...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
keybindButtons.put(action, button);
|
||||||
|
section.add(button.button).width(240).height(60).right().padRight(40);
|
||||||
|
section.row();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TextButton.TextButtonStyle editingButtonStyle = new TextButton.TextButtonStyle();
|
mainPanel.add(section).width(700).padBottom(20);
|
||||||
editingButtonStyle.font = font;
|
|
||||||
editingButtonStyle.fontColor = Color.WHITE;
|
|
||||||
Color editingColor = new Color(0.3f, 0.3f, 0.8f, 1.0f);
|
|
||||||
editingButtonStyle.up = keybindBgDrawable.tint(editingColor);
|
|
||||||
|
|
||||||
keybindsSection.add(new Label(" Player 1", labelStyle)).colspan(2).left().padLeft(60).padBottom(8);
|
|
||||||
keybindsSection.row();
|
|
||||||
|
|
||||||
keybindsSection.add(new Label(" Move Left", labelStyle)).left().padLeft(80);
|
|
||||||
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();
|
|
||||||
|
|
||||||
keybindsSection.add(new Label(" Move Right", labelStyle)).left().padLeft(80);
|
|
||||||
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();
|
|
||||||
|
|
||||||
keybindsSection.add(new Label(" Player 2", labelStyle)).colspan(2).left().padLeft(60).padTop(5).padBottom(8);
|
|
||||||
keybindsSection.row();
|
|
||||||
|
|
||||||
keybindsSection.add(new Label(" Move Left", labelStyle)).left().padLeft(80);
|
|
||||||
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();
|
|
||||||
|
|
||||||
keybindsSection.add(new Label(" Move Right", labelStyle)).left().padLeft(80);
|
|
||||||
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();
|
|
||||||
|
|
||||||
mainPanel.add(keybindsSection).width(700).padBottom(20);
|
|
||||||
mainPanel.row();
|
mainPanel.row();
|
||||||
|
|
||||||
// Close button
|
// Back button
|
||||||
UIImageButton backButton = new UIImageButton("textures/ui/UI_Gcg_Icon_Close.png");
|
UIImageButton backButton = new UIImageButton("textures/ui/UI_Gcg_Icon_Close.png");
|
||||||
backButton.setSize(40, 40);
|
backButton.setSize(40, 40);
|
||||||
backButton.setPosition(Gdx.graphics.getWidth() - 80, Gdx.graphics.getHeight() - 80);
|
backButton.setPosition(Gdx.graphics.getWidth() - 80, Gdx.graphics.getHeight() - 80);
|
||||||
backButton.getActor().setName("backButton");
|
|
||||||
backButton.onClick(() -> {
|
backButton.onClick(() -> {
|
||||||
if (game.transition == null && !isEditingKeybind) {
|
if (game.transition == null && !isEditingKeybind) {
|
||||||
game.transition = new Transition(game, this, game.mainMenuScene, State.MAIN_MENU, 350);
|
game.transition = new Transition(game, this, game.mainMenuScene,
|
||||||
|
State.MAIN_MENU, 350);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -144,117 +116,60 @@ public class SettingsScene extends Scene {
|
|||||||
root.addActor(backButton.getActor());
|
root.addActor(backButton.getActor());
|
||||||
game.stage.addActor(root);
|
game.stage.addActor(root);
|
||||||
|
|
||||||
// Listener for key input when editing keybind
|
// Key listener
|
||||||
game.stage.addListener(new InputListener() {
|
root.addListener(new InputListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean keyDown(InputEvent event, int keycode) {
|
public boolean keyDown(InputEvent event, int keycode) {
|
||||||
if (isEditingKeybind) {
|
if (isEditingKeybind && currentEditingButton != null) {
|
||||||
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) {
|
if (keycode == Input.Keys.ESCAPE) {
|
||||||
game.transition = new Transition(game, SettingsScene.this, game.mainMenuScene,
|
if (currentEditingButton != null) {
|
||||||
State.MAIN_MENU, 350);
|
((TextButton)(currentEditingButton.button)).setText(getKeyName(Settings.getKeybind(currentKeybindAction)));
|
||||||
|
}
|
||||||
|
isEditingKeybind = false;
|
||||||
|
currentEditingButton = null;
|
||||||
|
currentKeybindAction = "";
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (isKeybindAlreadyUsed(keycode, currentKeybindAction)) {
|
||||||
|
((TextButton)(currentEditingButton.button)).setText("Key in use!");
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
Gdx.app.postRunnable(() -> {
|
||||||
|
if (isEditingKeybind && currentEditingButton != null) {
|
||||||
|
((TextButton)(currentEditingButton.button)).setText("Press Key...");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Settings.keybinds.put(currentKeybindAction, keycode);
|
||||||
|
((TextButton)(currentEditingButton.button)).setText(getKeyName(keycode));
|
||||||
|
isEditingKeybind = false;
|
||||||
|
currentEditingButton = null;
|
||||||
|
currentKeybindAction = "";
|
||||||
|
return true;
|
||||||
|
} else if (keycode == Input.Keys.ESCAPE) {
|
||||||
|
backButton.click();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|
||||||
if (!isEditingKeybind) {
|
|
||||||
startKeybindEdit(button, action, editingStyle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startKeybindEdit(TextButton button, String action, TextButton.TextButtonStyle editingStyle) {
|
|
||||||
isEditingKeybind = true;
|
|
||||||
currentEditingButton = button;
|
|
||||||
currentKeybindAction = action;
|
|
||||||
|
|
||||||
button.setStyle(editingStyle);
|
|
||||||
button.setText("Press Key...");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void finishKeybindEdit() {
|
|
||||||
if (currentEditingButton != null) {
|
|
||||||
// Return to normal
|
|
||||||
TextButton.TextButtonStyle normalStyle = new TextButton.TextButtonStyle();
|
|
||||||
BitmapFont font = Assets.getFont("ui", 24);
|
|
||||||
normalStyle.font = font;
|
|
||||||
normalStyle.fontColor = Color.BLACK;
|
|
||||||
TextureRegionDrawable keybindBgDrawable = new TextureRegionDrawable(Assets.getWhiteTexture());
|
|
||||||
Color buttonColor = new Color(0.7f, 0.7f, 0.7f, 1.0f);
|
|
||||||
normalStyle.up = keybindBgDrawable.tint(buttonColor);
|
|
||||||
|
|
||||||
currentEditingButton.setStyle(normalStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
isEditingKeybind = false;
|
|
||||||
currentEditingButton = null;
|
|
||||||
currentKeybindAction = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cancelKeybindEdit() {
|
|
||||||
if (currentEditingButton != null) {
|
|
||||||
// Reset text
|
|
||||||
currentEditingButton.setText(getKeyName(Settings.getKeybind(currentKeybindAction)));
|
|
||||||
}
|
|
||||||
finishKeybindEdit();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getKeyName(int keycode) {
|
private String getKeyName(int keycode) {
|
||||||
String keyName = Input.Keys.toString(keycode);
|
String keyName = Input.Keys.toString(keycode);
|
||||||
return keyName != null ? keyName.toUpperCase() : "Unknown";
|
return keyName != null ? keyName.toUpperCase() : "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isKeybindAlreadyUsed(int keycode, String currentAction) {
|
private boolean isKeybindAlreadyUsed(int keycode, String currentAction) {
|
||||||
for (java.util.Map.Entry<String, Integer> entry : Settings.keybinds.entrySet()) {
|
return Settings.keybinds.entrySet().stream()
|
||||||
if (!entry.getKey().equals(currentAction) && entry.getValue().equals(keycode)) {
|
.anyMatch(entry -> !entry.getKey().equals(currentAction) &&
|
||||||
return true;
|
entry.getValue().equals(keycode));
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user