Skip to content

Commit

Permalink
Changed joystick icon to switch the placement of the joystick touchpad.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanceewing committed Mar 19, 2024
1 parent 97f36f5 commit 0505ba6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
30 changes: 27 additions & 3 deletions core/src/main/java/com/agifans/agile/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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];
}
}
}

0 comments on commit 0505ba6

Please sign in to comment.