From 3b2abe80751bf73a0e9dc2446f5d8d47d312fd73 Mon Sep 17 00:00:00 2001 From: giglio Date: Mon, 18 Mar 2024 00:12:18 +0100 Subject: [PATCH 1/8] ADD - added mapping for controls in options menu --- .../com/gdx/game/common/UtilityClass.java | 21 ++ .../gdx/game/component/InputComponent.java | 7 +- .../entities/player/PlayerInputComponent.java | 53 ++--- .../java/com/gdx/game/screen/BaseScreen.java | 4 + .../java/com/gdx/game/screen/GameScreen.java | 13 + .../com/gdx/game/screen/OptionScreen.java | 225 +++++++++++++++++- core/src/main/resources/settings/keys.json | 30 +++ 7 files changed, 318 insertions(+), 35 deletions(-) create mode 100644 core/src/main/java/com/gdx/game/common/UtilityClass.java create mode 100644 core/src/main/resources/settings/keys.json diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java new file mode 100644 index 00000000..a713c537 --- /dev/null +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -0,0 +1,21 @@ +package com.gdx.game.common; + +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +public class UtilityClass { + + + public static Set getKeysByValue(Map map, E value) { + Set keys = new HashSet(); + for (Map.Entry entry : map.entrySet()) { + if (Objects.equals(value, entry.getValue())) { + keys.add(entry.getKey()); + } + } + return keys; + } + +} diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 399e1b9c..12a1f9c1 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -1,8 +1,11 @@ package com.gdx.game.component; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.utils.Json; import com.gdx.game.entities.Entity; +import com.gdx.game.quest.QuestGraph; import java.util.HashMap; import java.util.Map; @@ -13,7 +16,7 @@ public abstract class InputComponent extends ComponentSubject implements Compone protected Entity.State currentState = null; protected Json json; - protected enum Keys { + public enum Keys { LEFT, RIGHT, UP, DOWN, QUIT, INTERACT, OPTION } @@ -51,4 +54,6 @@ public boolean scrolled(float amountX, float amountY) { return false; } + public static Map playerControls = new HashMap<>(); + } diff --git a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java index 9721af06..4a9d7429 100644 --- a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java +++ b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java @@ -92,30 +92,31 @@ public void update(Entity entity, float delta) { @Override public boolean keyDown(int keycode) { - switch (keycode) { - case Input.Keys.DOWN: - case Input.Keys.S: + + if(!playerControls.containsKey(keycode)){ + return false; + } + + switch (playerControls.get(keycode)) { + case DOWN: this.downPressed(); break; - case Input.Keys.UP: - case Input.Keys.W: + case UP: this.upPressed(); break; - case Input.Keys.LEFT: - case Input.Keys.A: + case LEFT: this.leftPressed(); break; - case Input.Keys.RIGHT: - case Input.Keys.D: + case RIGHT: this.rightPressed(); break; - case Input.Keys.E: + case INTERACT: this.interactPressed(); break; - case Input.Keys.O: + case OPTION: this.optionPressed(); break; - case Input.Keys.ESCAPE: + case QUIT: this.quitPressed(); break; default: @@ -126,35 +127,33 @@ public boolean keyDown(int keycode) { @Override public boolean keyUp(int keycode) { - switch (keycode) { - case Input.Keys.DOWN: - case Input.Keys.S: + + if(!playerControls.containsKey(keycode)){ + return false; + } + + switch (playerControls.get(keycode)) { + case DOWN: this.downReleased(); break; - case Input.Keys.UP: - case Input.Keys.W: + case UP: this.upReleased(); break; - case Input.Keys.LEFT: - case Input.Keys.A: + case LEFT: this.leftReleased(); break; - case Input.Keys.RIGHT: - case Input.Keys.D: + case RIGHT: this.rightReleased(); break; - case Input.Keys.E: + case INTERACT: this.interactReleased(); break; - case Input.Keys.O: + case OPTION: this.optionReleased(); break; - case Input.Keys.ESCAPE: + case QUIT: this.quitReleased(); break; - case Input.Keys.BACKSPACE: - debug = !debug; - break; default: break; } diff --git a/core/src/main/java/com/gdx/game/screen/BaseScreen.java b/core/src/main/java/com/gdx/game/screen/BaseScreen.java index b7e672d1..1219c09b 100644 --- a/core/src/main/java/com/gdx/game/screen/BaseScreen.java +++ b/core/src/main/java/com/gdx/game/screen/BaseScreen.java @@ -7,8 +7,10 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.TextField; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.viewport.Viewport; @@ -22,6 +24,8 @@ import java.util.ArrayList; import java.util.List; +import static com.gdx.game.manager.ResourceManager.skin; + public class BaseScreen implements Screen, AudioSubject { protected final GdxGame gdxGame; protected ResourceManager resourceManager; diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index 40088950..b37e388a 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -15,6 +15,7 @@ import com.gdx.game.camera.CameraStyles; import com.gdx.game.component.Component; import com.gdx.game.component.ComponentObserver; +import com.gdx.game.component.InputComponent; import com.gdx.game.entities.Entity; import com.gdx.game.entities.EntityFactory; import com.gdx.game.entities.player.PlayerHUD; @@ -28,6 +29,9 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.HashMap; + +import static com.gdx.game.component.InputComponent.playerControls; public class GameScreen extends BaseScreen implements ComponentObserver { @@ -92,6 +96,15 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { player = EntityFactory.getInstance().getEntity(ProfileManager.getInstance().getProperty("playerCharacter", EntityFactory.EntityType.class)); player.registerObserver(this); + //initialize controls + Json jsonObject = new Json(); + HashMap jsonMap = + jsonObject.fromJson(HashMap.class, Gdx.files.local("settings/keys.json")); + + for(var entry : jsonMap.entrySet()){ + playerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); + } + mapManager.setPlayer(player); mapManager.setCamera(camera); diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 1f859480..1c6eb23b 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -1,21 +1,40 @@ package com.gdx.game.screen; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputAdapter; +import com.badlogic.gdx.InputProcessor; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.*; +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; +import com.badlogic.gdx.utils.Json; import com.crashinvaders.vfx.VfxManager; import com.crashinvaders.vfx.effects.GaussianBlurEffect; import com.gdx.game.GdxGame; import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; +import com.gdx.game.component.InputComponent; +import com.gdx.game.entities.player.PlayerInputComponent; import com.gdx.game.manager.ResourceManager; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static com.gdx.game.common.UtilityClass.getKeysByValue; +import static com.gdx.game.manager.ResourceManager.skin; public class OptionScreen extends BaseScreen { @@ -35,6 +54,8 @@ public class OptionScreen extends BaseScreen { private VfxManager vfxManager; private GaussianBlurEffect vfxEffect; + private Map playerControlsNew = new HashMap<>(); + public OptionScreen(GdxGame gdxGame, BaseScreen previousScreen, ResourceManager resourceManager) { super(gdxGame, resourceManager); this.previousScreen = previousScreen; @@ -69,23 +90,213 @@ private void handleControlButton() { createButton("Control",0, optionTable.getHeight()/10, optionTable); Actor controlButton = optionTable.getCells().get(0).getActor(); + controlButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { controlClickListener = true; + controlTable = createTable(); + handleControlSettings(); handleControlBackButton(); } }); } + private void handleControlSettings() { + + Json jsonObject = new Json(); + playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal("settings/keys.json")); + + Label downLabel = new Label(InputComponent.Keys.DOWN.name(), skin); + TextField downText = new TextField("S", skin); + Label upLabel = new Label(InputComponent.Keys.UP.name(), skin); + TextField upText = new TextField("W", skin); + Label leftLabel = new Label(InputComponent.Keys.LEFT.name(), skin); + TextField leftText = new TextField("A", skin); + Label rightLabel = new Label(InputComponent.Keys.RIGHT.name(), skin); + TextField rightText = new TextField("D", skin); + Label interactLabel = new Label(InputComponent.Keys.INTERACT.name(), skin); + TextField interactText = new TextField("E", skin); + Label optionLabel = new Label(InputComponent.Keys.OPTION.name(), skin); + TextField optionText = new TextField("O", skin); + Label quitLabel = new Label(InputComponent.Keys.QUIT.name(), skin); + TextField quitText = new TextField("ENTER", skin); + + downText.setMaxLength(1); + upText.setMaxLength(1); + leftText.setMaxLength(1); + rightText.setMaxLength(1); + interactText.setMaxLength(1); + optionText.setMaxLength(1); + quitText.setMaxLength(1); + + downText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.DOWN.name()); + downText.setMaxLength(Input.Keys.toString(keycode).length()); + downText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + upText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.UP.name()); + upText.setMaxLength(Input.Keys.toString(keycode).length()); + upText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + leftText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.LEFT.name()); + leftText.setMaxLength(Input.Keys.toString(keycode).length()); + leftText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + rightText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.RIGHT.name()); + rightText.setMaxLength(Input.Keys.toString(keycode).length()); + rightText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + interactText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.INTERACT.name()); + interactText.setMaxLength(Input.Keys.toString(keycode).length()); + interactText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + optionText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.OPTION.name()); + optionText.setMaxLength(Input.Keys.toString(keycode).length()); + optionText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + quitText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.QUIT.name()); + quitText.setMaxLength(Input.Keys.toString(keycode).length()); + quitText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + controlTable.add(downLabel); + controlTable.add(downText); + controlTable.row(); + controlTable.add(upLabel); + controlTable.add(upText); + controlTable.row(); + controlTable.add(leftLabel); + controlTable.add(leftText); + controlTable.row(); + controlTable.add(rightLabel); + controlTable.add(rightText); + controlTable.row(); + controlTable.add(interactLabel); + controlTable.add(interactText); + controlTable.row(); + controlTable.add(optionLabel); + controlTable.add(optionText); + controlTable.row(); + controlTable.add(quitLabel); + controlTable.add(quitText); + controlTable.row(); + + } + private void handleControlBackButton() { createButton("Back",0, controlTable.getHeight()/5, controlTable); - Actor backButton = controlTable.getCells().get(0).getActor(); + Actor backButton = controlTable.getCells().get(controlTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { + + Json json = new Json(); + + File f = Gdx.files.local("settings/keys.json").file(); + try { + f.createNewFile(); + + FileWriter fw = new FileWriter(f); + + fw.write(json.prettyPrint(playerControlsNew)); + fw.close(); + + } catch (IOException e) { + throw new RuntimeException(e); + } + controlClickListener = false; } }); @@ -107,16 +318,16 @@ public void clicked(InputEvent even, float x, float y) { } private void handleMusicSettings() { - Label musicLabel = new Label("MUSIC", resourceManager.skin); + Label musicLabel = new Label("MUSIC", skin); musicLabel.setAlignment(Align.left); - Slider musicSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider musicSlider = new Slider(0, 1, 0.01f, false, skin); musicSlider.setValue(gdxGame.getPreferenceManager().getMusicVolume()); musicSlider.addListener(event -> { gdxGame.getPreferenceManager().setMusicVolume(musicSlider.getValue()); AudioManager.getInstance().getCurrentMusic().setVolume(gdxGame.getPreferenceManager().getMusicVolume()); return false; }); - CheckBox musicCheckbox = new CheckBox("Enable Music", resourceManager.skin); + CheckBox musicCheckbox = new CheckBox("Enable Music", skin); musicCheckbox.setChecked(gdxGame.getPreferenceManager().isMusicEnabled()); musicCheckbox.addListener(event -> { gdxGame.getPreferenceManager().setMusicEnabled(musicCheckbox.isChecked()); @@ -124,15 +335,15 @@ private void handleMusicSettings() { return false; }); - Label soundLabel = new Label("SOUND", resourceManager.skin); + Label soundLabel = new Label("SOUND", skin); soundLabel.setAlignment(Align.left); - Slider soundSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider soundSlider = new Slider(0, 1, 0.01f, false, skin); soundSlider.setValue(gdxGame.getPreferenceManager().getSoundVolume()); soundSlider.addListener(event -> { gdxGame.getPreferenceManager().setSoundVolume(soundSlider.getValue()); return false; }); - CheckBox soundCheckbox = new CheckBox("Enable Sound", resourceManager.skin); + CheckBox soundCheckbox = new CheckBox("Enable Sound", skin); soundCheckbox.setChecked(gdxGame.getPreferenceManager().isSoundEffectsEnabled()); soundCheckbox.addListener(event -> { boolean enabled = soundCheckbox.isChecked(); diff --git a/core/src/main/resources/settings/keys.json b/core/src/main/resources/settings/keys.json new file mode 100644 index 00000000..72db42ac --- /dev/null +++ b/core/src/main/resources/settings/keys.json @@ -0,0 +1,30 @@ +{ + "33":{ + "class":"java.lang.String", + "value":"INTERACT" + }, + "44":{ + "class":"java.lang.String", + "value":"OPTION" + }, + "111":{ + "class":"java.lang.String", + "value":"QUIT" + }, + "47":{ + "class":"java.lang.String", + "value":"DOWN" + }, + "29":{ + "class":"java.lang.String", + "value":"LEFT" + }, + "51":{ + "class":"java.lang.String", + "value":"UP" + }, + "32":{ + "class":"java.lang.String", + "value":"RIGHT" + } +} \ No newline at end of file From 55b541bd80b4e84f336605e1f1fe599652ff338f Mon Sep 17 00:00:00 2001 From: giglio Date: Mon, 18 Mar 2024 00:12:18 +0100 Subject: [PATCH 2/8] ADD - added mapping for controls in options menu --- .../com/gdx/game/common/UtilityClass.java | 21 ++ .../gdx/game/component/InputComponent.java | 7 +- .../entities/player/PlayerInputComponent.java | 53 ++--- .../java/com/gdx/game/screen/BaseScreen.java | 4 + .../java/com/gdx/game/screen/GameScreen.java | 13 + .../com/gdx/game/screen/OptionScreen.java | 225 +++++++++++++++++- core/src/main/resources/settings/keys.json | 30 +++ 7 files changed, 318 insertions(+), 35 deletions(-) create mode 100644 core/src/main/java/com/gdx/game/common/UtilityClass.java create mode 100644 core/src/main/resources/settings/keys.json diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java new file mode 100644 index 00000000..a713c537 --- /dev/null +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -0,0 +1,21 @@ +package com.gdx.game.common; + +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +public class UtilityClass { + + + public static Set getKeysByValue(Map map, E value) { + Set keys = new HashSet(); + for (Map.Entry entry : map.entrySet()) { + if (Objects.equals(value, entry.getValue())) { + keys.add(entry.getKey()); + } + } + return keys; + } + +} diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 399e1b9c..12a1f9c1 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -1,8 +1,11 @@ package com.gdx.game.component; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.utils.Json; import com.gdx.game.entities.Entity; +import com.gdx.game.quest.QuestGraph; import java.util.HashMap; import java.util.Map; @@ -13,7 +16,7 @@ public abstract class InputComponent extends ComponentSubject implements Compone protected Entity.State currentState = null; protected Json json; - protected enum Keys { + public enum Keys { LEFT, RIGHT, UP, DOWN, QUIT, INTERACT, OPTION } @@ -51,4 +54,6 @@ public boolean scrolled(float amountX, float amountY) { return false; } + public static Map playerControls = new HashMap<>(); + } diff --git a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java index 9721af06..4a9d7429 100644 --- a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java +++ b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java @@ -92,30 +92,31 @@ public void update(Entity entity, float delta) { @Override public boolean keyDown(int keycode) { - switch (keycode) { - case Input.Keys.DOWN: - case Input.Keys.S: + + if(!playerControls.containsKey(keycode)){ + return false; + } + + switch (playerControls.get(keycode)) { + case DOWN: this.downPressed(); break; - case Input.Keys.UP: - case Input.Keys.W: + case UP: this.upPressed(); break; - case Input.Keys.LEFT: - case Input.Keys.A: + case LEFT: this.leftPressed(); break; - case Input.Keys.RIGHT: - case Input.Keys.D: + case RIGHT: this.rightPressed(); break; - case Input.Keys.E: + case INTERACT: this.interactPressed(); break; - case Input.Keys.O: + case OPTION: this.optionPressed(); break; - case Input.Keys.ESCAPE: + case QUIT: this.quitPressed(); break; default: @@ -126,35 +127,33 @@ public boolean keyDown(int keycode) { @Override public boolean keyUp(int keycode) { - switch (keycode) { - case Input.Keys.DOWN: - case Input.Keys.S: + + if(!playerControls.containsKey(keycode)){ + return false; + } + + switch (playerControls.get(keycode)) { + case DOWN: this.downReleased(); break; - case Input.Keys.UP: - case Input.Keys.W: + case UP: this.upReleased(); break; - case Input.Keys.LEFT: - case Input.Keys.A: + case LEFT: this.leftReleased(); break; - case Input.Keys.RIGHT: - case Input.Keys.D: + case RIGHT: this.rightReleased(); break; - case Input.Keys.E: + case INTERACT: this.interactReleased(); break; - case Input.Keys.O: + case OPTION: this.optionReleased(); break; - case Input.Keys.ESCAPE: + case QUIT: this.quitReleased(); break; - case Input.Keys.BACKSPACE: - debug = !debug; - break; default: break; } diff --git a/core/src/main/java/com/gdx/game/screen/BaseScreen.java b/core/src/main/java/com/gdx/game/screen/BaseScreen.java index b7e672d1..1219c09b 100644 --- a/core/src/main/java/com/gdx/game/screen/BaseScreen.java +++ b/core/src/main/java/com/gdx/game/screen/BaseScreen.java @@ -7,8 +7,10 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.TextField; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.viewport.Viewport; @@ -22,6 +24,8 @@ import java.util.ArrayList; import java.util.List; +import static com.gdx.game.manager.ResourceManager.skin; + public class BaseScreen implements Screen, AudioSubject { protected final GdxGame gdxGame; protected ResourceManager resourceManager; diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index 40088950..b37e388a 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -15,6 +15,7 @@ import com.gdx.game.camera.CameraStyles; import com.gdx.game.component.Component; import com.gdx.game.component.ComponentObserver; +import com.gdx.game.component.InputComponent; import com.gdx.game.entities.Entity; import com.gdx.game.entities.EntityFactory; import com.gdx.game.entities.player.PlayerHUD; @@ -28,6 +29,9 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.HashMap; + +import static com.gdx.game.component.InputComponent.playerControls; public class GameScreen extends BaseScreen implements ComponentObserver { @@ -92,6 +96,15 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { player = EntityFactory.getInstance().getEntity(ProfileManager.getInstance().getProperty("playerCharacter", EntityFactory.EntityType.class)); player.registerObserver(this); + //initialize controls + Json jsonObject = new Json(); + HashMap jsonMap = + jsonObject.fromJson(HashMap.class, Gdx.files.local("settings/keys.json")); + + for(var entry : jsonMap.entrySet()){ + playerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); + } + mapManager.setPlayer(player); mapManager.setCamera(camera); diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 1f859480..1c6eb23b 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -1,21 +1,40 @@ package com.gdx.game.screen; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.InputAdapter; +import com.badlogic.gdx.InputProcessor; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.*; +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; +import com.badlogic.gdx.utils.Json; import com.crashinvaders.vfx.VfxManager; import com.crashinvaders.vfx.effects.GaussianBlurEffect; import com.gdx.game.GdxGame; import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; +import com.gdx.game.component.InputComponent; +import com.gdx.game.entities.player.PlayerInputComponent; import com.gdx.game.manager.ResourceManager; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static com.gdx.game.common.UtilityClass.getKeysByValue; +import static com.gdx.game.manager.ResourceManager.skin; public class OptionScreen extends BaseScreen { @@ -35,6 +54,8 @@ public class OptionScreen extends BaseScreen { private VfxManager vfxManager; private GaussianBlurEffect vfxEffect; + private Map playerControlsNew = new HashMap<>(); + public OptionScreen(GdxGame gdxGame, BaseScreen previousScreen, ResourceManager resourceManager) { super(gdxGame, resourceManager); this.previousScreen = previousScreen; @@ -69,23 +90,213 @@ private void handleControlButton() { createButton("Control",0, optionTable.getHeight()/10, optionTable); Actor controlButton = optionTable.getCells().get(0).getActor(); + controlButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { controlClickListener = true; + controlTable = createTable(); + handleControlSettings(); handleControlBackButton(); } }); } + private void handleControlSettings() { + + Json jsonObject = new Json(); + playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal("settings/keys.json")); + + Label downLabel = new Label(InputComponent.Keys.DOWN.name(), skin); + TextField downText = new TextField("S", skin); + Label upLabel = new Label(InputComponent.Keys.UP.name(), skin); + TextField upText = new TextField("W", skin); + Label leftLabel = new Label(InputComponent.Keys.LEFT.name(), skin); + TextField leftText = new TextField("A", skin); + Label rightLabel = new Label(InputComponent.Keys.RIGHT.name(), skin); + TextField rightText = new TextField("D", skin); + Label interactLabel = new Label(InputComponent.Keys.INTERACT.name(), skin); + TextField interactText = new TextField("E", skin); + Label optionLabel = new Label(InputComponent.Keys.OPTION.name(), skin); + TextField optionText = new TextField("O", skin); + Label quitLabel = new Label(InputComponent.Keys.QUIT.name(), skin); + TextField quitText = new TextField("ENTER", skin); + + downText.setMaxLength(1); + upText.setMaxLength(1); + leftText.setMaxLength(1); + rightText.setMaxLength(1); + interactText.setMaxLength(1); + optionText.setMaxLength(1); + quitText.setMaxLength(1); + + downText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.DOWN.name()); + downText.setMaxLength(Input.Keys.toString(keycode).length()); + downText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + upText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.UP.name()); + upText.setMaxLength(Input.Keys.toString(keycode).length()); + upText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + leftText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.LEFT.name()); + leftText.setMaxLength(Input.Keys.toString(keycode).length()); + leftText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + rightText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.RIGHT.name()); + rightText.setMaxLength(Input.Keys.toString(keycode).length()); + rightText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + interactText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.INTERACT.name()); + interactText.setMaxLength(Input.Keys.toString(keycode).length()); + interactText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + optionText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.OPTION.name()); + optionText.setMaxLength(Input.Keys.toString(keycode).length()); + optionText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + quitText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) + .stream().findFirst().toString(); + playerControlsNew.remove(keyToRemove); + + playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.QUIT.name()); + quitText.setMaxLength(Input.Keys.toString(keycode).length()); + quitText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + controlTable.add(downLabel); + controlTable.add(downText); + controlTable.row(); + controlTable.add(upLabel); + controlTable.add(upText); + controlTable.row(); + controlTable.add(leftLabel); + controlTable.add(leftText); + controlTable.row(); + controlTable.add(rightLabel); + controlTable.add(rightText); + controlTable.row(); + controlTable.add(interactLabel); + controlTable.add(interactText); + controlTable.row(); + controlTable.add(optionLabel); + controlTable.add(optionText); + controlTable.row(); + controlTable.add(quitLabel); + controlTable.add(quitText); + controlTable.row(); + + } + private void handleControlBackButton() { createButton("Back",0, controlTable.getHeight()/5, controlTable); - Actor backButton = controlTable.getCells().get(0).getActor(); + Actor backButton = controlTable.getCells().get(controlTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { + + Json json = new Json(); + + File f = Gdx.files.local("settings/keys.json").file(); + try { + f.createNewFile(); + + FileWriter fw = new FileWriter(f); + + fw.write(json.prettyPrint(playerControlsNew)); + fw.close(); + + } catch (IOException e) { + throw new RuntimeException(e); + } + controlClickListener = false; } }); @@ -107,16 +318,16 @@ public void clicked(InputEvent even, float x, float y) { } private void handleMusicSettings() { - Label musicLabel = new Label("MUSIC", resourceManager.skin); + Label musicLabel = new Label("MUSIC", skin); musicLabel.setAlignment(Align.left); - Slider musicSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider musicSlider = new Slider(0, 1, 0.01f, false, skin); musicSlider.setValue(gdxGame.getPreferenceManager().getMusicVolume()); musicSlider.addListener(event -> { gdxGame.getPreferenceManager().setMusicVolume(musicSlider.getValue()); AudioManager.getInstance().getCurrentMusic().setVolume(gdxGame.getPreferenceManager().getMusicVolume()); return false; }); - CheckBox musicCheckbox = new CheckBox("Enable Music", resourceManager.skin); + CheckBox musicCheckbox = new CheckBox("Enable Music", skin); musicCheckbox.setChecked(gdxGame.getPreferenceManager().isMusicEnabled()); musicCheckbox.addListener(event -> { gdxGame.getPreferenceManager().setMusicEnabled(musicCheckbox.isChecked()); @@ -124,15 +335,15 @@ private void handleMusicSettings() { return false; }); - Label soundLabel = new Label("SOUND", resourceManager.skin); + Label soundLabel = new Label("SOUND", skin); soundLabel.setAlignment(Align.left); - Slider soundSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider soundSlider = new Slider(0, 1, 0.01f, false, skin); soundSlider.setValue(gdxGame.getPreferenceManager().getSoundVolume()); soundSlider.addListener(event -> { gdxGame.getPreferenceManager().setSoundVolume(soundSlider.getValue()); return false; }); - CheckBox soundCheckbox = new CheckBox("Enable Sound", resourceManager.skin); + CheckBox soundCheckbox = new CheckBox("Enable Sound", skin); soundCheckbox.setChecked(gdxGame.getPreferenceManager().isSoundEffectsEnabled()); soundCheckbox.addListener(event -> { boolean enabled = soundCheckbox.isChecked(); diff --git a/core/src/main/resources/settings/keys.json b/core/src/main/resources/settings/keys.json new file mode 100644 index 00000000..72db42ac --- /dev/null +++ b/core/src/main/resources/settings/keys.json @@ -0,0 +1,30 @@ +{ + "33":{ + "class":"java.lang.String", + "value":"INTERACT" + }, + "44":{ + "class":"java.lang.String", + "value":"OPTION" + }, + "111":{ + "class":"java.lang.String", + "value":"QUIT" + }, + "47":{ + "class":"java.lang.String", + "value":"DOWN" + }, + "29":{ + "class":"java.lang.String", + "value":"LEFT" + }, + "51":{ + "class":"java.lang.String", + "value":"UP" + }, + "32":{ + "class":"java.lang.String", + "value":"RIGHT" + } +} \ No newline at end of file From 62b6b0a971b3cdfa4544ae096389ae75b66fb6fb Mon Sep 17 00:00:00 2001 From: giglio Date: Tue, 19 Mar 2024 23:54:01 +0100 Subject: [PATCH 3/8] FIX - fixed crashes when use back button + better implementation of control settings --- .../com/gdx/game/common/UtilityClass.java | 33 ++++++- .../gdx/game/component/InputComponent.java | 3 - .../com/gdx/game/dialog/ConversationUI.java | 6 +- .../java/com/gdx/game/screen/BaseScreen.java | 4 - .../com/gdx/game/screen/OptionScreen.java | 96 ++++++++----------- core/src/main/resources/settings/keys.json | 56 +++++------ 6 files changed, 99 insertions(+), 99 deletions(-) diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java index a713c537..cd050cbd 100644 --- a/core/src/main/java/com/gdx/game/common/UtilityClass.java +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -1,15 +1,12 @@ package com.gdx.game.common; -import java.util.HashSet; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.util.*; public class UtilityClass { public static Set getKeysByValue(Map map, E value) { - Set keys = new HashSet(); + Set keys = new HashSet<>(); for (Map.Entry entry : map.entrySet()) { if (Objects.equals(value, entry.getValue())) { keys.add(entry.getKey()); @@ -18,4 +15,30 @@ public static Set getKeysByValue(Map map, E value) { return keys; } + public static Optional getFirstKeyByValue(Map map, E value){ + return getKeysByValue(map, value).stream().findFirst(); + } + + public static HashMap mapInverter(HashMap hashMap){ + + HashMap newHashMap = new HashMap<>(); + + for(Map.Entry entry : hashMap.entrySet()){ + newHashMap.put(entry.getValue(), entry.getKey()); + } + + return newHashMap; + } + + public static Map mapInverter(Map hashMap){ + + Map newMap = new HashMap<>(); + + for(Map.Entry entry : hashMap.entrySet()){ + newMap.put(entry.getValue(), entry.getKey()); + } + + return newMap; + } + } diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 12a1f9c1..463974b7 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -1,11 +1,8 @@ package com.gdx.game.component; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.utils.Json; import com.gdx.game.entities.Entity; -import com.gdx.game.quest.QuestGraph; import java.util.HashMap; import java.util.Map; diff --git a/core/src/main/java/com/gdx/game/dialog/ConversationUI.java b/core/src/main/java/com/gdx/game/dialog/ConversationUI.java index f51b1e48..670bd037 100644 --- a/core/src/main/java/com/gdx/game/dialog/ConversationUI.java +++ b/core/src/main/java/com/gdx/game/dialog/ConversationUI.java @@ -2,11 +2,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.scenes.scene2d.InputEvent; -import com.badlogic.gdx.scenes.scene2d.ui.Label; -import com.badlogic.gdx.scenes.scene2d.ui.List; -import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; -import com.badlogic.gdx.scenes.scene2d.ui.TextButton; -import com.badlogic.gdx.scenes.scene2d.ui.Window; +import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Array; diff --git a/core/src/main/java/com/gdx/game/screen/BaseScreen.java b/core/src/main/java/com/gdx/game/screen/BaseScreen.java index 1219c09b..b7e672d1 100644 --- a/core/src/main/java/com/gdx/game/screen/BaseScreen.java +++ b/core/src/main/java/com/gdx/game/screen/BaseScreen.java @@ -7,10 +7,8 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; -import com.badlogic.gdx.scenes.scene2d.ui.TextField; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.viewport.Viewport; @@ -24,8 +22,6 @@ import java.util.ArrayList; import java.util.List; -import static com.gdx.game.manager.ResourceManager.skin; - public class BaseScreen implements Screen, AudioSubject { protected final GdxGame gdxGame; protected ResourceManager resourceManager; diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 1c6eb23b..adb8b56b 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -2,8 +2,6 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.InputAdapter; -import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.scenes.scene2d.Actor; @@ -11,7 +9,6 @@ import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.*; -import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Json; @@ -21,19 +18,15 @@ import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; import com.gdx.game.component.InputComponent; -import com.gdx.game.entities.player.PlayerInputComponent; import com.gdx.game.manager.ResourceManager; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.Optional; -import static com.gdx.game.common.UtilityClass.getKeysByValue; +import static com.gdx.game.common.UtilityClass.getFirstKeyByValue; +import static com.gdx.game.common.UtilityClass.mapInverter; import static com.gdx.game.manager.ResourceManager.skin; public class OptionScreen extends BaseScreen { @@ -108,20 +101,31 @@ private void handleControlSettings() { Json jsonObject = new Json(); playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal("settings/keys.json")); - Label downLabel = new Label(InputComponent.Keys.DOWN.name(), skin); - TextField downText = new TextField("S", skin); + playerControlsNew = mapInverter(playerControlsNew); + Label upLabel = new Label(InputComponent.Keys.UP.name(), skin); - TextField upText = new TextField("W", skin); + TextField upText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.UP.name()))), skin); + Label downLabel = new Label(InputComponent.Keys.DOWN.name(), skin); + TextField downText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.DOWN.name()))), skin); Label leftLabel = new Label(InputComponent.Keys.LEFT.name(), skin); - TextField leftText = new TextField("A", skin); + TextField leftText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.LEFT.name()))), skin); Label rightLabel = new Label(InputComponent.Keys.RIGHT.name(), skin); - TextField rightText = new TextField("D", skin); + TextField rightText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.RIGHT.name()))), skin); Label interactLabel = new Label(InputComponent.Keys.INTERACT.name(), skin); - TextField interactText = new TextField("E", skin); + TextField interactText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.INTERACT.name()))), skin); Label optionLabel = new Label(InputComponent.Keys.OPTION.name(), skin); - TextField optionText = new TextField("O", skin); + TextField optionText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.OPTION.name()))), skin); Label quitLabel = new Label(InputComponent.Keys.QUIT.name(), skin); - TextField quitText = new TextField("ENTER", skin); + TextField quitText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.QUIT.name()))), skin); + + playerControlsNew = mapInverter(playerControlsNew); downText.setMaxLength(1); upText.setMaxLength(1); @@ -136,9 +140,9 @@ private void handleControlSettings() { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.DOWN.name()); + + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.DOWN.name()); downText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -153,9 +157,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.UP.name()); + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.UP.name()); upText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -170,9 +173,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.LEFT.name()); + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.LEFT.name()); leftText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -187,9 +189,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.RIGHT.name()); + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.RIGHT.name()); rightText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -204,9 +205,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.INTERACT.name()); + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.INTERACT.name()); interactText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -221,9 +221,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.OPTION.name()); + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.OPTION.name()); optionText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -238,9 +237,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - String keyToRemove = getKeysByValue(playerControlsNew, InputComponent.Keys.DOWN.name()) - .stream().findFirst().toString(); - playerControlsNew.remove(keyToRemove); + Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.QUIT.name()); + keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.QUIT.name()); quitText.setMaxLength(Input.Keys.toString(keycode).length()); @@ -250,12 +248,12 @@ public boolean keyDown(InputEvent event, int keycode) { } }); - controlTable.add(downLabel); - controlTable.add(downText); - controlTable.row(); controlTable.add(upLabel); controlTable.add(upText); controlTable.row(); + controlTable.add(downLabel); + controlTable.add(downText); + controlTable.row(); controlTable.add(leftLabel); controlTable.add(leftText); controlTable.row(); @@ -284,18 +282,8 @@ public void clicked(InputEvent even, float x, float y) { Json json = new Json(); - File f = Gdx.files.local("settings/keys.json").file(); - try { - f.createNewFile(); - - FileWriter fw = new FileWriter(f); - - fw.write(json.prettyPrint(playerControlsNew)); - fw.close(); - - } catch (IOException e) { - throw new RuntimeException(e); - } + FileHandle commandsFile = Gdx.files.local("core/src/main/resources/settings/keys.json"); + commandsFile.writeString(json.prettyPrint(playerControlsNew), false); controlClickListener = false; } diff --git a/core/src/main/resources/settings/keys.json b/core/src/main/resources/settings/keys.json index 72db42ac..ecf5b10a 100644 --- a/core/src/main/resources/settings/keys.json +++ b/core/src/main/resources/settings/keys.json @@ -1,30 +1,30 @@ { - "33":{ - "class":"java.lang.String", - "value":"INTERACT" - }, - "44":{ - "class":"java.lang.String", - "value":"OPTION" - }, - "111":{ - "class":"java.lang.String", - "value":"QUIT" - }, - "47":{ - "class":"java.lang.String", - "value":"DOWN" - }, - "29":{ - "class":"java.lang.String", - "value":"LEFT" - }, - "51":{ - "class":"java.lang.String", - "value":"UP" - }, - "32":{ - "class":"java.lang.String", - "value":"RIGHT" - } +33: { + class: java.lang.String + value: INTERACT +} +44: { + class: java.lang.String + value: OPTION +} +22: { + class: java.lang.String + value: RIGHT +} +111: { + class: java.lang.String + value: QUIT +} +19: { + class: java.lang.String + value: UP +} +20: { + class: java.lang.String + value: DOWN +} +21: { + class: java.lang.String + value: LEFT +} } \ No newline at end of file From 0f17427fffa47478b250b7e95d15c86243426445 Mon Sep 17 00:00:00 2001 From: giglio Date: Wed, 20 Mar 2024 00:12:37 +0100 Subject: [PATCH 4/8] ADDED - defaultControls map --- .../gdx/game/common/DefaultControlsMap.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 core/src/main/java/com/gdx/game/common/DefaultControlsMap.java diff --git a/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java b/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java new file mode 100644 index 00000000..b1eca8ae --- /dev/null +++ b/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java @@ -0,0 +1,21 @@ +package com.gdx.game.common; + +import com.gdx.game.component.InputComponent; + +import java.util.HashMap; + +public class DefaultControlsMap { + + public static final HashMap defaultControls = new HashMap<>(); + + static { + defaultControls.put("33", InputComponent.Keys.INTERACT.name()); + defaultControls.put("44", InputComponent.Keys.OPTION.name()); + defaultControls.put("22", InputComponent.Keys.RIGHT.name()); + defaultControls.put("111", InputComponent.Keys.QUIT.name()); + defaultControls.put("19", InputComponent.Keys.UP.name()); + defaultControls.put("20", InputComponent.Keys.DOWN.name()); + defaultControls.put("21", InputComponent.Keys.LEFT.name()); + } + +} From e5e11726d993cfb6c26e164cce351636b38de7fc Mon Sep 17 00:00:00 2001 From: giglio Date: Wed, 20 Mar 2024 22:50:40 +0100 Subject: [PATCH 5/8] ADDED - defaultControls map if keys json map do not exist + some code refactoring --- .../java/com/gdx/game/common/Constats.java | 3 ++ .../gdx/game/common/DefaultControlsMap.java | 16 ++++----- .../java/com/gdx/game/screen/GameScreen.java | 34 +++++++++++++------ .../com/gdx/game/screen/OptionScreen.java | 14 ++++++-- core/src/main/resources/settings/keys.json | 30 ---------------- 5 files changed, 46 insertions(+), 51 deletions(-) delete mode 100644 core/src/main/resources/settings/keys.json diff --git a/core/src/main/java/com/gdx/game/common/Constats.java b/core/src/main/java/com/gdx/game/common/Constats.java index f64fc27a..5ac7438a 100644 --- a/core/src/main/java/com/gdx/game/common/Constats.java +++ b/core/src/main/java/com/gdx/game/common/Constats.java @@ -6,4 +6,7 @@ public class Constats { public static final String COURTESY_PHRASES_PATH = "conversations/conversation_courtesy.json"; + public static final String PARTIAL_CONTROLS_SETTINGS_PATH = "settings/keys.json"; + public static final String FULL_CONTROLS_SETTINGS_PATH = "core/src/main/resources/settings/keys.json"; + } diff --git a/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java b/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java index b1eca8ae..ba879aee 100644 --- a/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java +++ b/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java @@ -6,16 +6,16 @@ public class DefaultControlsMap { - public static final HashMap defaultControls = new HashMap<>(); + public static final HashMap DEFAULT_CONTROLS = new HashMap<>(); static { - defaultControls.put("33", InputComponent.Keys.INTERACT.name()); - defaultControls.put("44", InputComponent.Keys.OPTION.name()); - defaultControls.put("22", InputComponent.Keys.RIGHT.name()); - defaultControls.put("111", InputComponent.Keys.QUIT.name()); - defaultControls.put("19", InputComponent.Keys.UP.name()); - defaultControls.put("20", InputComponent.Keys.DOWN.name()); - defaultControls.put("21", InputComponent.Keys.LEFT.name()); + DEFAULT_CONTROLS.put("33", InputComponent.Keys.INTERACT.name()); + DEFAULT_CONTROLS.put("43", InputComponent.Keys.OPTION.name()); + DEFAULT_CONTROLS.put("32", InputComponent.Keys.RIGHT.name()); + DEFAULT_CONTROLS.put("111", InputComponent.Keys.QUIT.name()); + DEFAULT_CONTROLS.put("51", InputComponent.Keys.UP.name()); + DEFAULT_CONTROLS.put("47", InputComponent.Keys.DOWN.name()); + DEFAULT_CONTROLS.put("29", InputComponent.Keys.LEFT.name()); } } diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index b37e388a..7f77c446 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -2,13 +2,14 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputMultiplexer; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; -import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.ScreenUtils; +import com.badlogic.gdx.utils.SerializationException; import com.gdx.game.GdxGame; import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; @@ -31,6 +32,9 @@ import java.util.ArrayList; import java.util.HashMap; +import static com.gdx.game.common.Constats.FULL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS; import static com.gdx.game.component.InputComponent.playerControls; public class GameScreen extends BaseScreen implements ComponentObserver { @@ -60,14 +64,13 @@ public enum GameState { protected MapManager mapManager; protected OrthographicCamera camera; protected OrthographicCamera hudCamera; - private Stage gameStage = new Stage(); - private Json json; - private GdxGame game; - private InputMultiplexer multiplexer; + private final Json json; + private final GdxGame game; + private final InputMultiplexer multiplexer; - private Entity player; - private PlayerHUD playerHUD; + private final Entity player; + private final PlayerHUD playerHUD; private float startX; private float startY; @@ -97,10 +100,20 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { player.registerObserver(this); //initialize controls - Json jsonObject = new Json(); - HashMap jsonMap = - jsonObject.fromJson(HashMap.class, Gdx.files.local("settings/keys.json")); + HashMap jsonMap; + try { + jsonMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); + }catch (SerializationException se){ + + // if I can not read the file it doesn't exist, so use the default controls binding and save it + jsonMap = DEFAULT_CONTROLS; + + FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); + commandsFile.writeString(json.prettyPrint(jsonMap), false); + } + + // map player controls get by json into game readable controls for(var entry : jsonMap.entrySet()){ playerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); } @@ -249,7 +262,6 @@ public static GameState getGameState() { public static void setGameState(GameState state) { switch (state) { - case RUNNING -> gameState = GameState.RUNNING; case LOADING -> { ProfileManager.getInstance().loadProfile(); gameState = GameState.RUNNING; diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index adb8b56b..f8250d70 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -12,6 +12,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Json; +import com.badlogic.gdx.utils.SerializationException; import com.crashinvaders.vfx.VfxManager; import com.crashinvaders.vfx.effects.GaussianBlurEffect; import com.gdx.game.GdxGame; @@ -25,6 +26,9 @@ import java.util.Map; import java.util.Optional; +import static com.gdx.game.common.Constats.FULL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS; import static com.gdx.game.common.UtilityClass.getFirstKeyByValue; import static com.gdx.game.common.UtilityClass.mapInverter; import static com.gdx.game.manager.ResourceManager.skin; @@ -99,7 +103,13 @@ public void clicked(InputEvent even, float x, float y) { private void handleControlSettings() { Json jsonObject = new Json(); - playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal("settings/keys.json")); + + try { + playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal(PARTIAL_CONTROLS_SETTINGS_PATH)); + }catch (SerializationException se){ + playerControlsNew = DEFAULT_CONTROLS; + } + playerControlsNew = mapInverter(playerControlsNew); @@ -282,7 +292,7 @@ public void clicked(InputEvent even, float x, float y) { Json json = new Json(); - FileHandle commandsFile = Gdx.files.local("core/src/main/resources/settings/keys.json"); + FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); commandsFile.writeString(json.prettyPrint(playerControlsNew), false); controlClickListener = false; diff --git a/core/src/main/resources/settings/keys.json b/core/src/main/resources/settings/keys.json deleted file mode 100644 index ecf5b10a..00000000 --- a/core/src/main/resources/settings/keys.json +++ /dev/null @@ -1,30 +0,0 @@ -{ -33: { - class: java.lang.String - value: INTERACT -} -44: { - class: java.lang.String - value: OPTION -} -22: { - class: java.lang.String - value: RIGHT -} -111: { - class: java.lang.String - value: QUIT -} -19: { - class: java.lang.String - value: UP -} -20: { - class: java.lang.String - value: DOWN -} -21: { - class: java.lang.String - value: LEFT -} -} \ No newline at end of file From 356ae9ec434eb682b5d846045b113ffed2ed3c44 Mon Sep 17 00:00:00 2001 From: giglio Date: Thu, 21 Mar 2024 00:04:53 +0100 Subject: [PATCH 6/8] FIX - controls saving error handling + removed control options while running game --- .../java/com/gdx/game/screen/GameScreen.java | 6 +++++ .../com/gdx/game/screen/OptionScreen.java | 25 +++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index 7f77c446..dab73d86 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -104,7 +104,13 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { try { jsonMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); + + if (DEFAULT_CONTROLS.size() != jsonMap.size()){ + throw new SerializationException("Not valid control map"); + } + }catch (SerializationException se){ + LOGGER.error(se.getMessage()); // if I can not read the file it doesn't exist, so use the default controls binding and save it jsonMap = DEFAULT_CONTROLS; diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index f8250d70..1eb644e1 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -20,6 +20,8 @@ import com.gdx.game.audio.AudioObserver; import com.gdx.game.component.InputComponent; import com.gdx.game.manager.ResourceManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; @@ -32,6 +34,7 @@ import static com.gdx.game.common.UtilityClass.getFirstKeyByValue; import static com.gdx.game.common.UtilityClass.mapInverter; import static com.gdx.game.manager.ResourceManager.skin; +import static com.gdx.game.screen.GameScreen.GameState.PAUSED; public class OptionScreen extends BaseScreen { @@ -53,6 +56,8 @@ public class OptionScreen extends BaseScreen { private Map playerControlsNew = new HashMap<>(); + private static final Logger LOGGER = LoggerFactory.getLogger(GameScreen.class); + public OptionScreen(GdxGame gdxGame, BaseScreen previousScreen, ResourceManager resourceManager) { super(gdxGame, resourceManager); this.previousScreen = previousScreen; @@ -77,8 +82,10 @@ private void loadContents() { vfxManager.addEffect(vfxEffect); } + GameScreen.GameState gameState = GameScreen.getGameState(); + optionTable = createTable(); - handleControlButton(); + if (gameState == null) handleControlButton(); handleMusicButton(); handleBackButton(); } @@ -86,7 +93,7 @@ private void loadContents() { private void handleControlButton() { createButton("Control",0, optionTable.getHeight()/10, optionTable); - Actor controlButton = optionTable.getCells().get(0).getActor(); + Actor controlButton = optionTable.getCells().get(optionTable.getCells().size - 1).getActor(); controlButton.addListener(new ClickListener() { @Override @@ -106,7 +113,15 @@ private void handleControlSettings() { try { playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal(PARTIAL_CONTROLS_SETTINGS_PATH)); + + if (DEFAULT_CONTROLS.size() != playerControlsNew.size()){ + throw new SerializationException("Not valid control map"); + } + }catch (SerializationException se){ + + LOGGER.error(se.getMessage()); + playerControlsNew = DEFAULT_CONTROLS; } @@ -303,7 +318,7 @@ public void clicked(InputEvent even, float x, float y) { private void handleMusicButton() { createButton("Music",0, optionTable.getHeight()/15, optionTable); - Actor musicButton = optionTable.getCells().get(1).getActor(); + Actor musicButton = optionTable.getCells().get(optionTable.getCells().size - 1).getActor(); musicButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { @@ -368,7 +383,7 @@ private void handleMusicSettings() { private void handleMusicBackButton() { createButton("Back",0, musicTable.getHeight()/20, musicTable); - Actor backButton = musicTable.getCells().get(6).getActor(); + Actor backButton = musicTable.getCells().get(musicTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { @@ -380,7 +395,7 @@ public void clicked(InputEvent even, float x, float y) { private void handleBackButton() { createButton("Back",0, optionTable.getHeight()/5, optionTable); - Actor backButton = optionTable.getCells().get(2).getActor(); + Actor backButton = optionTable.getCells().get(optionTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { From 3f66f724b164c36c63b1d348c16d74fb81203d19 Mon Sep 17 00:00:00 2001 From: hudescottes Date: Thu, 21 Mar 2024 00:42:01 +0100 Subject: [PATCH 7/8] Adjust imports and small typo fixes --- .gitignore | 1 + .../java/com/gdx/game/common/UtilityClass.java | 7 ++++++- .../com/gdx/game/component/InputComponent.java | 3 --- .../java/com/gdx/game/dialog/ConversationUI.java | 6 +++++- .../java/com/gdx/game/screen/GameScreen.java | 16 ++++++++-------- .../java/com/gdx/game/screen/OptionScreen.java | 8 ++++++-- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 03eff3c8..f5c8a719 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ lib/ *.hprof .DS_Store *.sav +/**/settings/keys.json ### STS ### .apt_generated diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java index cd050cbd..2d712d38 100644 --- a/core/src/main/java/com/gdx/game/common/UtilityClass.java +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -1,6 +1,11 @@ package com.gdx.game.common; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; public class UtilityClass { diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 12a1f9c1..463974b7 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -1,11 +1,8 @@ package com.gdx.game.component; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.utils.Json; import com.gdx.game.entities.Entity; -import com.gdx.game.quest.QuestGraph; import java.util.HashMap; import java.util.Map; diff --git a/core/src/main/java/com/gdx/game/dialog/ConversationUI.java b/core/src/main/java/com/gdx/game/dialog/ConversationUI.java index 670bd037..f51b1e48 100644 --- a/core/src/main/java/com/gdx/game/dialog/ConversationUI.java +++ b/core/src/main/java/com/gdx/game/dialog/ConversationUI.java @@ -2,7 +2,11 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.scenes.scene2d.InputEvent; -import com.badlogic.gdx.scenes.scene2d.ui.*; +import com.badlogic.gdx.scenes.scene2d.ui.Label; +import com.badlogic.gdx.scenes.scene2d.ui.List; +import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; +import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.Window; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Array; diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index dab73d86..0f1a7265 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -100,27 +100,27 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { player.registerObserver(this); //initialize controls - HashMap jsonMap; + HashMap controlMap; try { - jsonMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); + controlMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); - if (DEFAULT_CONTROLS.size() != jsonMap.size()){ + if (DEFAULT_CONTROLS.size() != controlMap.size()){ throw new SerializationException("Not valid control map"); } - }catch (SerializationException se){ + } catch (SerializationException se){ LOGGER.error(se.getMessage()); - // if I can not read the file it doesn't exist, so use the default controls binding and save it - jsonMap = DEFAULT_CONTROLS; + // if I can not read the file, so use the default controls binding and save it + controlMap = DEFAULT_CONTROLS; FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); - commandsFile.writeString(json.prettyPrint(jsonMap), false); + commandsFile.writeString(json.prettyPrint(controlMap), false); } // map player controls get by json into game readable controls - for(var entry : jsonMap.entrySet()){ + for (var entry : controlMap.entrySet()){ playerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); } diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 1eb644e1..3be75738 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -8,7 +8,12 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.scenes.scene2d.ui.*; +import com.badlogic.gdx.scenes.scene2d.ui.CheckBox; +import com.badlogic.gdx.scenes.scene2d.ui.Image; +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.TextField; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Json; @@ -34,7 +39,6 @@ import static com.gdx.game.common.UtilityClass.getFirstKeyByValue; import static com.gdx.game.common.UtilityClass.mapInverter; import static com.gdx.game.manager.ResourceManager.skin; -import static com.gdx.game.screen.GameScreen.GameState.PAUSED; public class OptionScreen extends BaseScreen { From 515d808e5d45495e57c058582327d7fd4e10f6e8 Mon Sep 17 00:00:00 2001 From: giglio Date: Thu, 21 Mar 2024 22:58:00 +0100 Subject: [PATCH 8/8] FIX - restored control settings in pause menu + fixed previous bug that force to restart the game for apply controls changes --- .../com/gdx/game/common/UtilityClass.java | 19 ----- .../gdx/game/component/InputComponent.java | 37 ++++++++- .../java/com/gdx/game/screen/GameScreen.java | 31 ++++--- .../com/gdx/game/screen/OptionScreen.java | 81 +++++++------------ 4 files changed, 81 insertions(+), 87 deletions(-) diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java index 2d712d38..fc92c3eb 100644 --- a/core/src/main/java/com/gdx/game/common/UtilityClass.java +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -1,29 +1,10 @@ package com.gdx.game.common; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; public class UtilityClass { - - public static Set getKeysByValue(Map map, E value) { - Set keys = new HashSet<>(); - for (Map.Entry entry : map.entrySet()) { - if (Objects.equals(value, entry.getValue())) { - keys.add(entry.getKey()); - } - } - return keys; - } - - public static Optional getFirstKeyByValue(Map map, E value){ - return getKeysByValue(map, value).stream().findFirst(); - } - public static HashMap mapInverter(HashMap hashMap){ HashMap newHashMap = new HashMap<>(); diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 463974b7..6d20f265 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -7,6 +7,8 @@ import java.util.HashMap; import java.util.Map; +import static com.gdx.game.common.UtilityClass.mapInverter; + public abstract class InputComponent extends ComponentSubject implements Component, InputProcessor { protected Entity.Direction currentDirection = null; @@ -51,6 +53,39 @@ public boolean scrolled(float amountX, float amountY) { return false; } - public static Map playerControls = new HashMap<>(); + public static HashMap playerControls = new HashMap<>(); + + public static void setPlayerControlMapFromJsonControlsMap(HashMap jsonMap){ + + HashMap newPlayerControls = new HashMap<>(); + + for (var entry : jsonMap.entrySet()) { + newPlayerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); + } + + playerControls = newPlayerControls; + + } + + public static HashMap mapJsonControlsToPlayerControl(HashMap playerControls){ + + HashMap result = new HashMap<>(); + + for (var entry : playerControls.entrySet()) { + result.put(entry.getKey().toString(), entry.getValue().toString()); + } + + return result; + } + + public static HashMap changeValueFromJsonControlsMap(HashMap jsonMap, + Keys keyValue, + Integer keyCode){ + jsonMap = mapInverter(jsonMap); + jsonMap.put(keyValue.name(), String.valueOf(keyCode)); + jsonMap = mapInverter(jsonMap); + + return jsonMap; + } } diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index 0f1a7265..ab9eddf9 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -16,7 +16,6 @@ import com.gdx.game.camera.CameraStyles; import com.gdx.game.component.Component; import com.gdx.game.component.ComponentObserver; -import com.gdx.game.component.InputComponent; import com.gdx.game.entities.Entity; import com.gdx.game.entities.EntityFactory; import com.gdx.game.entities.player.PlayerHUD; @@ -36,6 +35,7 @@ import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH; import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS; import static com.gdx.game.component.InputComponent.playerControls; +import static com.gdx.game.component.InputComponent.setPlayerControlMapFromJsonControlsMap; public class GameScreen extends BaseScreen implements ComponentObserver { @@ -102,26 +102,23 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { //initialize controls HashMap controlMap; - try { - controlMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); + if(playerControls.isEmpty()){ + try { + controlMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); - if (DEFAULT_CONTROLS.size() != controlMap.size()){ - throw new SerializationException("Not valid control map"); - } - - } catch (SerializationException se){ - LOGGER.error(se.getMessage()); + if (DEFAULT_CONTROLS.size() != controlMap.size()){ + throw new SerializationException("Not valid control map"); + } + } catch (SerializationException se) {LOGGER.error(se.getMessage()); - // if I can not read the file, so use the default controls binding and save it - controlMap = DEFAULT_CONTROLS; + // if I can not read the file , so use the default controls binding and save it + controlMap = DEFAULT_CONTROLS; - FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); - commandsFile.writeString(json.prettyPrint(controlMap), false); - } + FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); + commandsFile.writeString(json.prettyPrint(controlMap), false); + } - // map player controls get by json into game readable controls - for (var entry : controlMap.entrySet()){ - playerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); + setPlayerControlMapFromJsonControlsMap(controlMap); } mapManager.setPlayer(player); diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 3be75738..73aaa6de 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -8,12 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.scenes.scene2d.ui.CheckBox; -import com.badlogic.gdx.scenes.scene2d.ui.Image; -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.TextField; +import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Json; @@ -24,20 +19,19 @@ import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; import com.gdx.game.component.InputComponent; +import com.gdx.game.manager.PreferenceManager; import com.gdx.game.manager.ResourceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; -import java.util.Map; -import java.util.Optional; import static com.gdx.game.common.Constats.FULL_CONTROLS_SETTINGS_PATH; import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH; import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS; -import static com.gdx.game.common.UtilityClass.getFirstKeyByValue; import static com.gdx.game.common.UtilityClass.mapInverter; +import static com.gdx.game.component.InputComponent.*; import static com.gdx.game.manager.ResourceManager.skin; public class OptionScreen extends BaseScreen { @@ -45,11 +39,11 @@ public class OptionScreen extends BaseScreen { private Table optionTable; private Table musicTable; private Table controlTable; - private Stage optionStage = new Stage(); - private Stage musicStage = new Stage(); - private Stage controlStage = new Stage(); - private Stage backgroundStage = new Stage(); - private BaseScreen previousScreen; + private final Stage optionStage = new Stage(); + private final Stage musicStage = new Stage(); + private final Stage controlStage = new Stage(); + private final Stage backgroundStage = new Stage(); + private final BaseScreen previousScreen; private Image previousScreenAsImg; private boolean musicClickListener; private boolean controlClickListener; @@ -58,7 +52,7 @@ public class OptionScreen extends BaseScreen { private VfxManager vfxManager; private GaussianBlurEffect vfxEffect; - private Map playerControlsNew = new HashMap<>(); + private HashMap playerControlsNew = mapJsonControlsToPlayerControl(playerControls); private static final Logger LOGGER = LoggerFactory.getLogger(GameScreen.class); @@ -86,10 +80,8 @@ private void loadContents() { vfxManager.addEffect(vfxEffect); } - GameScreen.GameState gameState = GameScreen.getGameState(); - optionTable = createTable(); - if (gameState == null) handleControlButton(); + handleControlButton(); handleMusicButton(); handleBackButton(); } @@ -115,18 +107,20 @@ private void handleControlSettings() { Json jsonObject = new Json(); - try { - playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal(PARTIAL_CONTROLS_SETTINGS_PATH)); + if (playerControls.isEmpty()){ + try { + playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal(PARTIAL_CONTROLS_SETTINGS_PATH)); - if (DEFAULT_CONTROLS.size() != playerControlsNew.size()){ - throw new SerializationException("Not valid control map"); - } + if (DEFAULT_CONTROLS.size() != playerControlsNew.size()) { + throw new SerializationException("Not valid control map"); + } - }catch (SerializationException se){ + } catch (SerializationException se) { - LOGGER.error(se.getMessage()); + LOGGER.error(se.getMessage()); - playerControlsNew = DEFAULT_CONTROLS; + playerControlsNew = DEFAULT_CONTROLS; + } } @@ -169,11 +163,8 @@ private void handleControlSettings() { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.DOWN.name()); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.DOWN, keycode); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); - - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.DOWN.name()); downText.setMaxLength(Input.Keys.toString(keycode).length()); downText.setText(Input.Keys.toString(keycode)); @@ -186,10 +177,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.UP.name()); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.UP, keycode); - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.UP.name()); upText.setMaxLength(Input.Keys.toString(keycode).length()); upText.setText(Input.Keys.toString(keycode)); @@ -202,10 +191,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.LEFT.name()); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.LEFT, keycode); - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.LEFT.name()); leftText.setMaxLength(Input.Keys.toString(keycode).length()); leftText.setText(Input.Keys.toString(keycode)); @@ -218,10 +205,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.RIGHT.name()); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.RIGHT, keycode); - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.RIGHT.name()); rightText.setMaxLength(Input.Keys.toString(keycode).length()); rightText.setText(Input.Keys.toString(keycode)); @@ -234,10 +219,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.INTERACT.name()); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.INTERACT, keycode); - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.INTERACT.name()); interactText.setMaxLength(Input.Keys.toString(keycode).length()); interactText.setText(Input.Keys.toString(keycode)); @@ -250,10 +233,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.OPTION.name()); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.OPTION, keycode); - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.OPTION.name()); optionText.setMaxLength(Input.Keys.toString(keycode).length()); optionText.setText(Input.Keys.toString(keycode)); @@ -266,10 +247,8 @@ public boolean keyDown(InputEvent event, int keycode) { @Override public boolean keyDown(InputEvent event, int keycode) { - Optional keyToRemove = getFirstKeyByValue(playerControlsNew, InputComponent.Keys.QUIT.name()); - keyToRemove.ifPresent(s -> playerControlsNew.remove(s)); + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.QUIT, keycode); - playerControlsNew.put(String.valueOf(keycode), InputComponent.Keys.QUIT.name()); quitText.setMaxLength(Input.Keys.toString(keycode).length()); quitText.setText(Input.Keys.toString(keycode)); @@ -314,6 +293,8 @@ public void clicked(InputEvent even, float x, float y) { FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); commandsFile.writeString(json.prettyPrint(playerControlsNew), false); + setPlayerControlMapFromJsonControlsMap(playerControlsNew); + controlClickListener = false; } }); @@ -338,10 +319,10 @@ private void handleMusicSettings() { Label musicLabel = new Label("MUSIC", skin); musicLabel.setAlignment(Align.left); Slider musicSlider = new Slider(0, 1, 0.01f, false, skin); - musicSlider.setValue(gdxGame.getPreferenceManager().getMusicVolume()); + musicSlider.setValue(PreferenceManager.getMusicVolume()); musicSlider.addListener(event -> { gdxGame.getPreferenceManager().setMusicVolume(musicSlider.getValue()); - AudioManager.getInstance().getCurrentMusic().setVolume(gdxGame.getPreferenceManager().getMusicVolume()); + AudioManager.getInstance().getCurrentMusic().setVolume(PreferenceManager.getMusicVolume()); return false; }); CheckBox musicCheckbox = new CheckBox("Enable Music", skin);