Skip to content

Commit

Permalink
Starting to add in joystick and keyboard rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanceewing committed Mar 9, 2024
1 parent 3676f76 commit b3545c1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
73 changes: 33 additions & 40 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.KeyboardType;
import com.agifans.agile.ui.ViewportManager;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
Expand Down Expand Up @@ -70,8 +71,8 @@ public class GameScreen implements Screen {
private ViewportManager viewportManager;

// Touchpad
private Stage portraitStage;
private Stage landscapeStage;
private Stage portraitTouchpadStage;
private Stage landscapeTouchpadStage;
private Touchpad portraitTouchpad;
private Touchpad landscapeTouchpad;

Expand Down Expand Up @@ -118,20 +119,20 @@ public GameScreen(Agile agile, AgileRunner agileRunner, DialogHandler dialogHand
viewportManager = ViewportManager.getInstance();

// Create a Stage and add TouchPad
portraitStage = new Stage(viewportManager.getPortraitViewport(), batch);
portraitStage.addActor(portraitTouchpad);
landscapeStage = new Stage(viewportManager.getLandscapeViewport(), batch);
landscapeStage.addActor(landscapeTouchpad);
portraitTouchpadStage = new Stage(viewportManager.getPortraitViewport(), batch);
portraitTouchpadStage.addActor(portraitTouchpad);
landscapeTouchpadStage = new Stage(viewportManager.getLandscapeViewport(), batch);
landscapeTouchpadStage.addActor(landscapeTouchpad);

// Create and register an input processor for keys, etc.
gameScreenInputProcessor = new GameScreenInputProcessor(this, dialogHandler);
portraitInputProcessor = new InputMultiplexer();
portraitInputProcessor.addProcessor(agileRunner.userInput);
portraitInputProcessor.addProcessor(portraitStage);
portraitInputProcessor.addProcessor(portraitTouchpadStage);
portraitInputProcessor.addProcessor(gameScreenInputProcessor);
landscapeInputProcessor = new InputMultiplexer();
landscapeInputProcessor.addProcessor(agileRunner.userInput);
landscapeInputProcessor.addProcessor(landscapeStage);
landscapeInputProcessor.addProcessor(landscapeTouchpadStage);
landscapeInputProcessor.addProcessor(gameScreenInputProcessor);
}

Expand Down Expand Up @@ -256,8 +257,8 @@ public void render(float delta) {
}

private void draw(float delta) {
// Get the KeyboardType currently being used by the MachineScreenProcessor.
//KeyboardType keyboardType = machineInputProcessor.getKeyboardType();
// Get the KeyboardType currently being used by the GameScreenInputProcessor.
KeyboardType keyboardType = gameScreenInputProcessor.getKeyboardType();

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Expand All @@ -282,19 +283,10 @@ private void draw(float delta) {
batch.enableBlending();
batch.begin();

// TODO: Implement joystick being active.
//if (keyboardType.equals(KeyboardType.JOYSTICK)) {
// if (viewportManager.isPortrait()) {
// // batch.draw(keyboardType.getTexture(KeyboardType.LEFT), 0, 0);
// batch.draw(keyboardType.getTexture(KeyboardType.RIGHT), viewportManager.getWidth() - 135, 0);
// } else {
// // batch.draw(keyboardType.getTexture(KeyboardType.LEFT), 0, 0, 201, 201);
// batch.draw(keyboardType.getTexture(KeyboardType.RIGHT), viewportManager.getWidth() - 135, 0);
// }
//} else if (keyboardType.isRendered()) {
// batch.setColor(c.r, c.g, c.b, keyboardType.getOpacity());
// batch.draw(keyboardType.getTexture(), 0, keyboardType.getRenderOffset());
//} else if (keyboardType.equals(KeyboardType.OFF)) {
if (keyboardType.isRendered()) {
batch.setColor(c.r, c.g, c.b, keyboardType.getOpacity());
batch.draw(keyboardType.getTexture(), 0, keyboardType.getRenderOffset());
} else if (keyboardType.equals(KeyboardType.OFF)) {
// The keyboard and joystick icons are rendered only when an input type isn't
// showing.
batch.setColor(c.r, c.g, c.b, 0.5f);
Expand All @@ -321,25 +313,26 @@ private void draw(float delta) {
batch.draw(keyboardIcon, 0, 0);

}
//}
}
batch.end();

// if (keyboardType.equals(KeyboardType.JOYSTICK)) {
// float joyX = 0;
// float joyY = 0;
// if (viewportManager.isPortrait()) {
// portraitStage.act(delta);
// portraitStage.draw();
// joyX = portraitTouchpad.getKnobPercentX();
// joyY = portraitTouchpad.getKnobPercentY();
// } else {
// landscapeStage.act(delta);
// landscapeStage.draw();
// joyX = landscapeTouchpad.getKnobPercentX();
// joyY = landscapeTouchpad.getKnobPercentY();
// }
// machine.getJoystick().touchPad(joyX, joyY);
// }
if (gameScreenInputProcessor.isJoystickActive()) {
float joyX = 0;
float joyY = 0;
if (viewportManager.isPortrait()) {
portraitTouchpadStage.act(delta);
portraitTouchpadStage.draw();
joyX = portraitTouchpad.getKnobPercentX();
joyY = portraitTouchpad.getKnobPercentY();
} else {
landscapeTouchpadStage.act(delta);
landscapeTouchpadStage.draw();
joyX = landscapeTouchpad.getKnobPercentX();
joyY = landscapeTouchpad.getKnobPercentY();
}
// TODO: Apply movement
//machine.getJoystick().touchPad(joyX, joyY);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class GameScreenInputProcessor extends InputAdapter {
*/
private KeyboardType keyboardType;

/**
* Whether the joystick is active or not.
*/
private boolean joystickActive;

/**
* Invoked by AGILE whenever it would like to show a dialog, such as when it
* needs the user to confirm an action, or to choose a file.
Expand Down Expand Up @@ -110,6 +115,8 @@ public GameScreenInputProcessor(GameScreen gameScreen, DialogHandler dialogHandl
* @return whether the input was processed
*/
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println("touchDown: screenX=" + screenX + ", screenY=" + screenY);

// Convert the screen coordinates to world coordinates.
Vector2 touchXY = viewportManager.unproject(screenX, screenY);

Expand Down Expand Up @@ -191,6 +198,8 @@ private void updateAGIMouse(Vector2 touchXY, int button, boolean buttonDown) {
* @return whether the input was processed
*/
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
System.out.println("touchUp: screenX=" + screenX + ", screenY=" + screenY);

// Convert the screen coordinates to world coordinates.
Vector2 touchXY = viewportManager.unproject(screenX, screenY);

Expand Down Expand Up @@ -288,7 +297,7 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) {
}

if (joystickClicked) {
keyboardType = KeyboardType.JOYSTICK;
joystickActive = !joystickActive;
}

if (fullScreenClicked) {
Expand Down Expand Up @@ -347,6 +356,8 @@ public boolean mouseMoved (int screenX, int screenY) {
* @return whether the input was processed
*/
public boolean touchDragged(int screenX, int screenY, int pointer) {
System.out.println("touchDragged: screenX=" + screenX + ", screenY=" + screenY);

// Convert the screen coordinates to world coordinates.
Vector2 touchXY = viewportManager.unproject(screenX, screenY);

Expand Down Expand Up @@ -390,7 +401,7 @@ public boolean touchDragged(int screenX, int screenY, int pointer) {
* @param height The new screen height.
*/
public void resize(int width, int height) {
if (keyboardType.isRendered() && !keyboardType.equals(KeyboardType.JOYSTICK)) {
if (keyboardType.isRendered()) {
// Switch keyboard layout based on the orientation.
keyboardType = (height > width ? KeyboardType.PORTRAIT : KeyboardType.LANDSCAPE);
}
Expand All @@ -404,4 +415,13 @@ public void resize(int width, int height) {
public KeyboardType getKeyboardType() {
return keyboardType;
}

/**
* Returns whether the joystick is active or not.
*
* @return whether the joystick is active or not.
*/
public boolean isJoystickActive() {
return joystickActive;
}
}

0 comments on commit b3545c1

Please sign in to comment.