Skip to content

Commit

Permalink
Experimental code for keyboard and touchpad placement.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanceewing committed Mar 16, 2024
1 parent dfcd0e3 commit fb3e72e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
37 changes: 26 additions & 11 deletions core/src/main/java/com/agifans/agile/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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"));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()) {
Expand All @@ -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) {
Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -379,7 +393,7 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) {
}

if (joystickClicked) {
joystickActive = !joystickActive;
joystickActive = !joystickActive;
}

if (fullScreenClicked) {
Expand Down Expand Up @@ -409,7 +423,11 @@ public void no() {
}
});
}
}

if (joystickActive && agiMouseUpdated) {
joystickPosition = touchXY;
}
//}

return true;
}
Expand Down Expand Up @@ -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;
}
}
12 changes: 6 additions & 6 deletions core/src/main/java/com/agifans/agile/ui/KeyboardType.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum KeyboardType {
},
"png/landscape_keyboard_lowercase.png",
0.4f,
0,
150,
0,
5,
1911,
Expand All @@ -38,7 +38,7 @@ public enum KeyboardType {
},
"png/landscape_keyboard_uppercase.png",
0.4f,
0,
150,
0,
5,
1911,
Expand All @@ -54,7 +54,7 @@ public enum KeyboardType {
},
"png/landscape_keyboard_punc_numbers.png",
0.4f,
0,
150,
0,
5,
1911,
Expand All @@ -70,7 +70,7 @@ public enum KeyboardType {
},
"png/portrait_keyboard_lowercase.png",
0.6f,
0,
150,
0,
1,
-1,
Expand All @@ -86,7 +86,7 @@ public enum KeyboardType {
},
"png/portrait_keyboard_uppercase.png",
0.6f,
0,
150,
0,
1,
-1,
Expand All @@ -102,7 +102,7 @@ public enum KeyboardType {
},
"png/portrait_keyboard_punc_numbers.png",
0.6f,
0,
150,
0,
1,
-1,
Expand Down

0 comments on commit fb3e72e

Please sign in to comment.