chore(ui/slider): refactor
This commit is contained in:
@ -5,11 +5,9 @@ import com.badlogic.gdx.Input;
|
|||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
||||||
import com.badlogic.gdx.scenes.scene2d.InputListener;
|
import com.badlogic.gdx.scenes.scene2d.InputListener;
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
|
|
||||||
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;
|
||||||
@ -20,12 +18,10 @@ import org.vibecoders.moongazer.Settings;
|
|||||||
import org.vibecoders.moongazer.managers.Assets;
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
import org.vibecoders.moongazer.ui.UICloseButton;
|
import org.vibecoders.moongazer.ui.UICloseButton;
|
||||||
import org.vibecoders.moongazer.ui.UITextButton;
|
import org.vibecoders.moongazer.ui.UITextButton;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import org.vibecoders.moongazer.ui.UISlider;
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import static com.badlogic.gdx.scenes.scene2d.ui.Slider.*;
|
|
||||||
import static org.vibecoders.moongazer.Constants.*;
|
import static org.vibecoders.moongazer.Constants.*;
|
||||||
|
|
||||||
public class SettingsScene extends Scene {
|
public class SettingsScene extends Scene {
|
||||||
@ -33,7 +29,9 @@ public class SettingsScene extends Scene {
|
|||||||
private UITextButton currentEditingButton = null;
|
private UITextButton currentEditingButton = null;
|
||||||
private String currentKeybindAction = "";
|
private String currentKeybindAction = "";
|
||||||
private HashMap<String, UITextButton> keybindButtons = new HashMap<>();
|
private HashMap<String, UITextButton> keybindButtons = new HashMap<>();
|
||||||
private Slider musicSlider;
|
private UISlider masterVolSlider;
|
||||||
|
private UISlider musicSlider;
|
||||||
|
private UISlider sfxSlider;
|
||||||
|
|
||||||
public SettingsScene(Game game) {
|
public SettingsScene(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
@ -52,30 +50,35 @@ public class SettingsScene extends Scene {
|
|||||||
mainPanel.add(title).colspan(2).padTop(60).padBottom(40);
|
mainPanel.add(title).colspan(2).padTop(60).padBottom(40);
|
||||||
mainPanel.row();
|
mainPanel.row();
|
||||||
|
|
||||||
|
|
||||||
// slider placeholders
|
|
||||||
Texture sliderBgTexture = Assets.getAsset("textures/ui/UI_SliderBg2.png", Texture.class);
|
|
||||||
Texture sliderKnobTexture = Assets.getAsset("textures/ui/UI_SliderKnob.png", Texture.class);
|
|
||||||
Texture sliderKnobOverTexture = Assets.getAsset("textures/ui/UI_SliderBg.png", Texture.class);
|
|
||||||
|
|
||||||
SliderStyle sliderStyle = new SliderStyle();
|
|
||||||
sliderStyle.background = new TextureRegionDrawable(new TextureRegion(sliderBgTexture));
|
|
||||||
sliderStyle.knob = new TextureRegionDrawable(new TextureRegion(sliderKnobTexture));
|
|
||||||
sliderStyle.knobBefore = new TextureRegionDrawable(new TextureRegion(sliderKnobOverTexture));
|
|
||||||
sliderStyle.knobAfter = new TextureRegionDrawable(new TextureRegion(sliderBgTexture));
|
|
||||||
|
|
||||||
// Volume settings
|
// Volume settings
|
||||||
TextureRegionDrawable bg = new TextureRegionDrawable(Assets.getWhiteTexture());
|
TextureRegionDrawable bg = new TextureRegionDrawable(Assets.getWhiteTexture());
|
||||||
var tintedBg = bg.tint(new Color(0.2f, 0.2f, 0.2f, 0.3f));
|
var tintedBg = bg.tint(new Color(0.2f, 0.2f, 0.2f, 0.3f));
|
||||||
|
|
||||||
String[] volumes = {"Master Volume", "Music Volume", "SFX Volume"};
|
String[] volumes = { "Master Volume", "Music Volume", "SFX Volume" };
|
||||||
|
// TODO: implement audio manager
|
||||||
for (String volume : volumes) {
|
for (String volume : volumes) {
|
||||||
Table row = new Table();
|
Table row = new Table();
|
||||||
row.setBackground(tintedBg);
|
row.setBackground(tintedBg);
|
||||||
row.add(new Label(volume, labelStyle)).expandX().left().padLeft(40).pad(15);
|
row.add(new Label(volume, labelStyle)).expandX().left().padLeft(40).pad(15);
|
||||||
musicSlider = new Slider(0f, 1f, 0.01f, false, sliderStyle);
|
if (volume.equals("Master Volume")) {
|
||||||
musicSlider.setValue(0.5f);
|
masterVolSlider = new UISlider();
|
||||||
row.add(musicSlider).width(300).right().padRight(40);
|
masterVolSlider.onChanged(() -> {
|
||||||
|
Settings.setMasterVolume(masterVolSlider.getValue());
|
||||||
|
});
|
||||||
|
row.add(masterVolSlider.slider).width(300).right().padRight(40);
|
||||||
|
} else if (volume.equals("Music Volume")) {
|
||||||
|
musicSlider = new UISlider();
|
||||||
|
musicSlider.onChanged(() -> {
|
||||||
|
Settings.setMusicVolume(musicSlider.getValue());
|
||||||
|
});
|
||||||
|
row.add(musicSlider.slider).width(300).right().padRight(40);
|
||||||
|
} else if (volume.equals("SFX Volume")) {
|
||||||
|
sfxSlider = new UISlider();
|
||||||
|
sfxSlider.onChanged(() -> {
|
||||||
|
Settings.setSfxVolume(sfxSlider.getValue());
|
||||||
|
});
|
||||||
|
row.add(sfxSlider.slider).width(300).right().padRight(40);
|
||||||
|
}
|
||||||
mainPanel.add(row).width(700).height(60).padBottom(5);
|
mainPanel.add(row).width(700).height(60).padBottom(5);
|
||||||
mainPanel.row();
|
mainPanel.row();
|
||||||
}
|
}
|
||||||
@ -88,10 +91,10 @@ public class SettingsScene extends Scene {
|
|||||||
section.row();
|
section.row();
|
||||||
|
|
||||||
// Player keybinds
|
// Player keybinds
|
||||||
String[] players = {"Player 1", "Player 2"};
|
String[] players = { "Player 1", "Player 2" };
|
||||||
String[] prefixes = {"p1", "p2"};
|
String[] prefixes = { "p1", "p2" };
|
||||||
String[] actions = {"_left", "_right"};
|
String[] actions = { "_left", "_right" };
|
||||||
String[] labels = {" Move Left", " Move Right"};
|
String[] labels = { " Move Left", " Move Right" };
|
||||||
|
|
||||||
for (int p = 0; p < players.length; p++) {
|
for (int p = 0; p < players.length; p++) {
|
||||||
section.add(new Label(" " + players[p], labelStyle)).colspan(2).left()
|
section.add(new Label(" " + players[p], labelStyle)).colspan(2).left()
|
||||||
@ -108,7 +111,7 @@ public class SettingsScene extends Scene {
|
|||||||
isEditingKeybind = true;
|
isEditingKeybind = true;
|
||||||
currentEditingButton = button;
|
currentEditingButton = button;
|
||||||
currentKeybindAction = action;
|
currentKeybindAction = action;
|
||||||
((TextButton)(button.button)).setText("Press Key...");
|
((TextButton) (button.button)).setText("Press Key...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -143,7 +146,8 @@ public class SettingsScene extends Scene {
|
|||||||
if (isEditingKeybind && currentEditingButton != null) {
|
if (isEditingKeybind && currentEditingButton != null) {
|
||||||
if (keycode == Input.Keys.ESCAPE) {
|
if (keycode == Input.Keys.ESCAPE) {
|
||||||
if (currentEditingButton != null) {
|
if (currentEditingButton != null) {
|
||||||
((TextButton)(currentEditingButton.button)).setText(getKeyName(Settings.getKeybind(currentKeybindAction)));
|
((TextButton) (currentEditingButton.button))
|
||||||
|
.setText(getKeyName(Settings.getKeybind(currentKeybindAction)));
|
||||||
}
|
}
|
||||||
isEditingKeybind = false;
|
isEditingKeybind = false;
|
||||||
currentEditingButton = null;
|
currentEditingButton = null;
|
||||||
@ -151,13 +155,13 @@ public class SettingsScene extends Scene {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (isKeybindAlreadyUsed(keycode, currentKeybindAction)) {
|
if (isKeybindAlreadyUsed(keycode, currentKeybindAction)) {
|
||||||
((TextButton)(currentEditingButton.button)).setText("Key in use!");
|
((TextButton) (currentEditingButton.button)).setText("Key in use!");
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
Gdx.app.postRunnable(() -> {
|
Gdx.app.postRunnable(() -> {
|
||||||
if (isEditingKeybind && currentEditingButton != null) {
|
if (isEditingKeybind && currentEditingButton != null) {
|
||||||
((TextButton)(currentEditingButton.button)).setText("Press Key...");
|
((TextButton) (currentEditingButton.button)).setText("Press Key...");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@ -167,7 +171,7 @@ public class SettingsScene extends Scene {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Settings.keybinds.put(currentKeybindAction, keycode);
|
Settings.keybinds.put(currentKeybindAction, keycode);
|
||||||
((TextButton)(currentEditingButton.button)).setText(getKeyName(keycode));
|
((TextButton) (currentEditingButton.button)).setText(getKeyName(keycode));
|
||||||
isEditingKeybind = false;
|
isEditingKeybind = false;
|
||||||
currentEditingButton = null;
|
currentEditingButton = null;
|
||||||
currentKeybindAction = "";
|
currentKeybindAction = "";
|
||||||
|
|||||||
61
app/src/main/java/org/vibecoders/moongazer/ui/UISlider.java
Normal file
61
app/src/main/java/org/vibecoders/moongazer/ui/UISlider.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package org.vibecoders.moongazer.ui;
|
||||||
|
|
||||||
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.EventListener;
|
||||||
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
|
||||||
|
|
||||||
|
public class UISlider {
|
||||||
|
public Slider slider;
|
||||||
|
|
||||||
|
public UISlider() {
|
||||||
|
Texture sliderBgTexture = Assets.getAsset("textures/ui/UI_SliderBg2.png", Texture.class);
|
||||||
|
Texture sliderKnobTexture = Assets.getAsset("textures/ui/UI_SliderKnob.png", Texture.class);
|
||||||
|
Texture sliderKnobOverTexture = Assets.getAsset("textures/ui/UI_SliderBg.png", Texture.class);
|
||||||
|
|
||||||
|
SliderStyle sliderStyle = new SliderStyle();
|
||||||
|
sliderStyle.background = new TextureRegionDrawable(new TextureRegion(sliderBgTexture));
|
||||||
|
sliderStyle.knob = new TextureRegionDrawable(new TextureRegion(sliderKnobTexture));
|
||||||
|
sliderStyle.knobBefore = new TextureRegionDrawable(new TextureRegion(sliderKnobOverTexture));
|
||||||
|
sliderStyle.knobAfter = new TextureRegionDrawable(new TextureRegion(sliderBgTexture));
|
||||||
|
|
||||||
|
slider = new Slider(0f, 1f, 0.01f, false, sliderStyle);
|
||||||
|
slider.setValue(1f);
|
||||||
|
slider.setProgrammaticChangeEvents(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(float value) {
|
||||||
|
slider.setValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getValue() {
|
||||||
|
return slider.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSize(float width, float height) {
|
||||||
|
slider.setSize(width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition(float x, float y) {
|
||||||
|
slider.setPosition(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(EventListener listener) {
|
||||||
|
slider.addListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onChanged(Runnable action) {
|
||||||
|
slider.addListener(new ChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void changed(ChangeEvent event, Actor actor) {
|
||||||
|
action.run();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user