From fb3e72ea4cb513b6021ebeecfd367c7c333e4951 Mon Sep 17 00:00:00 2001 From: Lance Ewing Date: Sat, 16 Mar 2024 22:23:40 +0000 Subject: [PATCH] Experimental code for keyboard and touchpad placement. --- .../java/com/agifans/agile/GameScreen.java | 37 +++++++++++++------ .../agile/ui/GameScreenInputProcessor.java | 37 ++++++++++++++++--- .../com/agifans/agile/ui/KeyboardType.java | 12 +++--- 3 files changed, 64 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/com/agifans/agile/GameScreen.java b/core/src/main/java/com/agifans/agile/GameScreen.java index 2f5274a..107fc3a 100644 --- a/core/src/main/java/com/agifans/agile/GameScreen.java +++ b/core/src/main/java/com/agifans/agile/GameScreen.java @@ -17,6 +17,7 @@ import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Touchpad; @@ -115,8 +116,8 @@ public GameScreen(Agile agile, AgileRunner agileRunner, DialogHandler dialogHand fullScreenIcon = new Texture("png/full_screen.png"); // Create the portrait and landscape joystick touchpads. - portraitTouchpad = createTouchpad(300); - landscapeTouchpad = createTouchpad(200); + portraitTouchpad = createTouchpad(200, 1080 - 200 - 15, 1920); + landscapeTouchpad = createTouchpad(150, 1920 - 150 - 15, 815); viewportManager = ViewportManager.getInstance(); @@ -138,7 +139,7 @@ public GameScreen(Agile agile, AgileRunner agileRunner, DialogHandler dialogHand landscapeInputProcessor.addProcessor(gameScreenInputProcessor); } - protected Touchpad createTouchpad(int size) { + protected Touchpad createTouchpad(int size, int x, int y) { Skin touchpadSkin = new Skin(); touchpadSkin.add("touchBackground", new Texture("png/touchBackground.png")); touchpadSkin.add("touchKnob", new Texture("png/touchKnob.png")); @@ -148,7 +149,7 @@ protected Touchpad createTouchpad(int size) { touchpadStyle.background = touchBackground; touchpadStyle.knob = touchKnob; Touchpad touchpad = new Touchpad(10, touchpadStyle); - touchpad.setBounds(15, 15, size, size); + touchpad.setBounds(x, y, size, size); return touchpad; } @@ -247,11 +248,6 @@ public void render(float delta) { if ((lastLogTime == 0) || (renderStartTime - lastLogTime > 10000000000L)) { lastLogTime = renderStartTime; - // Gdx.app.log("RenderTime", String.format( - // "[%d] avgDrawTime: %d avgRenderTime: %d maxFrameDuration: %d delta: %f fps: - // %d", - // drawCount, avgDrawTime, avgRenderTime, maxFrameDuration, delta, - // Gdx.graphics.getFramesPerSecond())); } // Trigger tick. @@ -288,7 +284,9 @@ private void draw(float delta) { 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)) { + } + + //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); @@ -315,18 +313,35 @@ private void draw(float delta) { batch.draw(keyboardIcon, 0, 0); } - } + //} + batch.end(); if (gameScreenInputProcessor.isJoystickActive()) { + Vector2 joystickPosition = gameScreenInputProcessor.getJoystickPosition(); float joyX = 0; float joyY = 0; if (viewportManager.isPortrait()) { + //if (joystickPosition != null) { + // portraitTouchpad.setX(joystickPosition.x); + // portraitTouchpad.setY(joystickPosition.y); + //} else { + portraitTouchpad.setY(0); + //} + portraitTouchpad.setY(viewportManager.getHeight() - 1050); portraitTouchpadStage.act(delta); portraitTouchpadStage.draw(); joyX = portraitTouchpad.getKnobPercentX(); joyY = portraitTouchpad.getKnobPercentY(); } else { + //landscapeTouchpad.setY(0); + //if (joystickPosition != null) { + // landscapeTouchpad.setX(joystickPosition.x); + // landscapeTouchpad.setY(joystickPosition.y); + //} else { + landscapeTouchpad.setY(0); + //} + landscapeTouchpad.setY(viewportManager.getHeight() - (viewportManager.getHeight() / 2) - 75); 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 9fffd4b..1d9f78d 100644 --- a/core/src/main/java/com/agifans/agile/ui/GameScreenInputProcessor.java +++ b/core/src/main/java/com/agifans/agile/ui/GameScreenInputProcessor.java @@ -31,6 +31,11 @@ public class GameScreenInputProcessor extends InputAdapter { */ private boolean joystickActive; + /** + * The position at which to draw the joystick when it is active. + */ + private Vector2 joystickPosition; + /** * 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. @@ -238,8 +243,10 @@ private void processCapsLockToggle() { * @param touchXY * @param button * @param buttonDown + * + * @return true if AGI mouse state was updated; otherwise false. */ - private void updateAGIMouse(Vector2 touchXY, int button, boolean buttonDown) { + private boolean updateAGIMouse(Vector2 touchXY, int button, boolean buttonDown) { int agiX = 0; int agiY = 0; if (viewportManager.isPortrait()) { @@ -258,6 +265,9 @@ private void updateAGIMouse(Vector2 touchXY, int button, boolean buttonDown) { float agiStart = (1920 / 2) - (agiWidth / 2); agiX = Math.round((touchXY.x - agiStart) / agiRatio); } + + boolean agiMouseUpdated = false; + if ((agiX >= 0) && (agiX <= 159) && (agiY >= 0) && (agiY < 199)) { // Only if it is within the AGI Screen do we set the mouse vars. if (buttonDown) { @@ -267,9 +277,12 @@ private void updateAGIMouse(Vector2 touchXY, int button, boolean buttonDown) { } gameScreen.getAgileRunner().getVariableData().setMouseX(agiX); gameScreen.getAgileRunner().getVariableData().setMouseY(agiY); + agiMouseUpdated = true; } agiMouseButton = (buttonDown? button + 1 : 0); + + return agiMouseUpdated; } /** @@ -286,7 +299,7 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) { Vector2 touchXY = viewportManager.unproject(screenX, screenY); // Update AGI mouse variables (click/touch released, so clear all to 0) - updateAGIMouse(touchXY, button, false); + boolean agiMouseUpdated = updateAGIMouse(touchXY, button, false); // Update the touch info for this pointer. TouchInfo touchInfo = null; @@ -318,7 +331,8 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) { keyboardType = KeyboardType.OFF; } - } else { + } + //else { // TODO: Need to handle the magic numbers in this block in a better way. boolean keyboardClicked = false; boolean joystickClicked = false; @@ -379,7 +393,7 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) { } if (joystickClicked) { - joystickActive = !joystickActive; + joystickActive = !joystickActive; } if (fullScreenClicked) { @@ -409,7 +423,11 @@ public void no() { } }); } - } + + if (joystickActive && agiMouseUpdated) { + joystickPosition = touchXY; + } + //} return true; } @@ -504,4 +522,13 @@ public KeyboardType getKeyboardType() { public boolean isJoystickActive() { return joystickActive; } + + /** + * Gets the position of the joystick when it is active. + * + * @return The position of the joystick when it is active. + */ + public Vector2 getJoystickPosition() { + return joystickPosition; + } } diff --git a/core/src/main/java/com/agifans/agile/ui/KeyboardType.java b/core/src/main/java/com/agifans/agile/ui/KeyboardType.java index b1db247..43ff29a 100644 --- a/core/src/main/java/com/agifans/agile/ui/KeyboardType.java +++ b/core/src/main/java/com/agifans/agile/ui/KeyboardType.java @@ -22,7 +22,7 @@ public enum KeyboardType { }, "png/landscape_keyboard_lowercase.png", 0.4f, - 0, + 150, 0, 5, 1911, @@ -38,7 +38,7 @@ public enum KeyboardType { }, "png/landscape_keyboard_uppercase.png", 0.4f, - 0, + 150, 0, 5, 1911, @@ -54,7 +54,7 @@ public enum KeyboardType { }, "png/landscape_keyboard_punc_numbers.png", 0.4f, - 0, + 150, 0, 5, 1911, @@ -70,7 +70,7 @@ public enum KeyboardType { }, "png/portrait_keyboard_lowercase.png", 0.6f, - 0, + 150, 0, 1, -1, @@ -86,7 +86,7 @@ public enum KeyboardType { }, "png/portrait_keyboard_uppercase.png", 0.6f, - 0, + 150, 0, 1, -1, @@ -102,7 +102,7 @@ public enum KeyboardType { }, "png/portrait_keyboard_punc_numbers.png", 0.6f, - 0, + 150, 0, 1, -1,