From 0505ba664fe8fb32cc7ed602a43c157f7d39dcbc Mon Sep 17 00:00:00 2001 From: Lance Ewing Date: Tue, 19 Mar 2024 23:44:57 +0000 Subject: [PATCH] Changed joystick icon to switch the placement of the joystick touchpad. --- .../java/com/agifans/agile/GameScreen.java | 30 +++++++++-- .../agile/ui/GameScreenInputProcessor.java | 54 ++++++++++++++----- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/agifans/agile/GameScreen.java b/core/src/main/java/com/agifans/agile/GameScreen.java index 2af13a0..ed44521 100644 --- a/core/src/main/java/com/agifans/agile/GameScreen.java +++ b/core/src/main/java/com/agifans/agile/GameScreen.java @@ -3,6 +3,7 @@ import com.agifans.agile.config.AppConfigItem; import com.agifans.agile.ui.DialogHandler; import com.agifans.agile.ui.GameScreenInputProcessor; +import com.agifans.agile.ui.GameScreenInputProcessor.JoystickAlignment; import com.agifans.agile.ui.KeyboardType; import com.agifans.agile.ui.ViewportManager; import com.badlogic.gdx.Input.Keys; @@ -301,7 +302,7 @@ private void draw(float delta) { batch.end(); // The joystick touch pad is updated and rendered via the Stage. - if (gameScreenInputProcessor.isJoystickActive()) { + if (!gameScreenInputProcessor.getJoystickAlignment().equals(JoystickAlignment.OFF)) { float joyX = 0; float joyY = 0; if (viewportManager.isPortrait()) { @@ -311,7 +312,19 @@ private void draw(float delta) { int midBetweenKeybAndPic = ((agiScreenBase + 900) / 2); portraitTouchpad.setSize(joyWidth, joyWidth); portraitTouchpad.setY(midBetweenKeybAndPic - (joyWidth / 2)); - portraitTouchpad.setX(1080 - joyWidth - 20); + switch (gameScreenInputProcessor.getJoystickAlignment()) { + case OFF: + break; + case RIGHT: + portraitTouchpad.setX(1080 - joyWidth - 20); + break; + case MIDDLE: + portraitTouchpad.setX(viewportManager.getWidth() - viewportManager.getWidth() / 2 - (joyWidth / 2)); + break; + case LEFT: + portraitTouchpad.setX(20); + break; + } portraitTouchpadStage.act(delta); portraitTouchpadStage.draw(); joyX = portraitTouchpad.getKnobPercentX(); @@ -321,7 +334,18 @@ private void draw(float delta) { int joyWidth = 96; landscapeTouchpad.setSize(joyWidth, joyWidth); landscapeTouchpad.setY(viewportManager.getHeight() - (viewportManager.getHeight() / 2) - (joyWidth / 2)); - landscapeTouchpad.setX(1920 - joyWidth - 16); + switch (gameScreenInputProcessor.getJoystickAlignment()) { + case OFF: + break; + case RIGHT: + landscapeTouchpad.setX(1920 - joyWidth - 16); + break; + case MIDDLE: + break; + case LEFT: + landscapeTouchpad.setX(16); + break; + } landscapeTouchpadStage.act(delta); landscapeTouchpadStage.draw(); joyX = landscapeTouchpad.getKnobPercentX(); diff --git a/core/src/main/java/com/agifans/agile/ui/GameScreenInputProcessor.java b/core/src/main/java/com/agifans/agile/ui/GameScreenInputProcessor.java index 61d24dd..4a76242 100644 --- a/core/src/main/java/com/agifans/agile/ui/GameScreenInputProcessor.java +++ b/core/src/main/java/com/agifans/agile/ui/GameScreenInputProcessor.java @@ -26,9 +26,10 @@ public class GameScreenInputProcessor extends InputAdapter { private KeyboardType keyboardType; /** - * Whether the joystick is active or not. + * The current alignment of the joystick on screen, if active, otherwise + * set to the OFF value. */ - private boolean joystickActive; + private JoystickAlignment joystickAlignment = JoystickAlignment.OFF; /** * Invoked by AGILE whenever it would like to show a dialog, such as when it @@ -358,7 +359,11 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) { } if (joystickClicked) { - joystickActive = !joystickActive; + // Rotate the joystick screen alignment. + joystickAlignment = joystickAlignment.rotateValue(); + if (!viewportManager.isPortrait() && joystickAlignment.equals(JoystickAlignment.MIDDLE)) { + joystickAlignment = joystickAlignment.rotateValue(); + } } if (fullScreenClicked) { @@ -367,14 +372,11 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) { if (screenWidthBeforeFullScreen > screenHeightBeforeFullScreen) { keyboardType = KeyboardType.OFF; } - Gdx.graphics.setWindowedMode(screenWidthBeforeFullScreen, screenHeightBeforeFullScreen); + switchOutOfFullScreen(); } else { keyboardType = KeyboardType.OFF; - Graphics.DisplayMode currentMode = Gdx.graphics.getDisplayMode(); - screenWidthBeforeFullScreen = Gdx.graphics.getWidth(); - screenHeightBeforeFullScreen = Gdx.graphics.getHeight(); - Gdx.graphics.setFullscreenMode(currentMode); + switchIntoFullScreen(); } } @@ -396,6 +398,25 @@ public void no() { return true; } + /** + * Switches to full screen mode, storing the width and height beforehand so + * that it can be restored when switching back. + */ + private void switchIntoFullScreen() { + Graphics.DisplayMode currentMode = Gdx.graphics.getDisplayMode(); + screenWidthBeforeFullScreen = Gdx.graphics.getWidth(); + screenHeightBeforeFullScreen = Gdx.graphics.getHeight(); + Gdx.graphics.setFullscreenMode(currentMode); + } + + /** + * Switches out of full screen mode back to the windowed mode, restoring the + * saved width and height. + */ + private void switchOutOfFullScreen() { + Gdx.graphics.setWindowedMode(screenWidthBeforeFullScreen, screenHeightBeforeFullScreen); + } + /** * Called whenever the mouse moves. * @@ -486,11 +507,20 @@ public KeyboardType getKeyboardType() { } /** - * Returns whether the joystick is active or not. + * Gets the current joystick screen alignment, i.e. where to place it on the + * screen (left aligned, middle aligned, right aligned, or turned off) * - * @return whether the joystick is active or not. + * @return The current joystick screen alignment. */ - public boolean isJoystickActive() { - return joystickActive; + public JoystickAlignment getJoystickAlignment() { + return joystickAlignment; + } + + public static enum JoystickAlignment { + OFF, RIGHT, MIDDLE, LEFT; + + JoystickAlignment rotateValue() { + return values()[(ordinal() + 1) % 4]; + } } }