Skip to content

Commit

Permalink
Updated virtual keyboard rendering to scale the height based on avail…
Browse files Browse the repository at this point in the history
…able space.
  • Loading branch information
lanceewing committed Apr 30, 2024
1 parent 1f02a6b commit c71d769
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/com/agifans/agile/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private void draw(float delta) {
float cameraXOffset = 0;
float cameraYOffset = 0;
float sidePaddingWidth = 0;
if (viewportManager.isLandscape()) {
if (viewportManager.doesScreenFitWidth()) {
// Override default screen centering logic to allow for narrower screens, so
// that the joystick can be rendered as a decent size.
float agiScreenWidth = (viewportManager.getHeight() * 1.32f);
Expand Down Expand Up @@ -324,7 +324,11 @@ private void draw(float delta) {
if (keyboardType.isRendered() || viewportManager.isPortrait()) {
if (keyboardType.getTexture() != null) {
batch.setColor(c.r, c.g, c.b, keyboardType.getOpacity());
batch.draw(keyboardType.getTexture(), 0, keyboardType.getRenderOffset());
batch.draw(
keyboardType.getTexture(),
0, keyboardType.getRenderOffset(),
keyboardType.getTexture().getWidth(),
keyboardType.getHeight());
}
}

Expand Down
63 changes: 31 additions & 32 deletions core/src/main/java/com/agifans/agile/ui/KeyboardType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public enum KeyboardType {
"png/landscape_keyboard_lowercase.png",
0.3f,
110,
0,
250,
1421,
0
Expand All @@ -39,7 +38,6 @@ public enum KeyboardType {
"png/landscape_keyboard_uppercase.png",
0.3f,
110,
0,
250,
1421,
0
Expand All @@ -55,7 +53,6 @@ public enum KeyboardType {
"png/landscape_keyboard_punc_numbers.png",
0.3f,
110,
0,
250,
1421,
0
Expand All @@ -71,7 +68,6 @@ public enum KeyboardType {
"png/portrait_keyboard_lowercase.png",
0.6f,
135,
0,
1,
-1,
0
Expand All @@ -87,7 +83,6 @@ public enum KeyboardType {
"png/portrait_keyboard_uppercase.png",
0.6f,
135,
0,
1,
-1,
0
Expand All @@ -103,19 +98,13 @@ public enum KeyboardType {
"png/portrait_keyboard_punc_numbers.png",
0.6f,
135,
0,
1,
-1,
0
),
MOBILE_ON_SCREEN,
OFF;

/**
* The vertical size of the keys in this KeyboardType.
*/
private float vertKeySize;

/**
* The horizontal size of the keys in this KeyboardType.
*/
Expand All @@ -141,11 +130,6 @@ public enum KeyboardType {
*/
private int renderOffset;

/**
* The Y value above which the keyboard will be closed.
*/
private int closeHeight;

/**
* The X value at which the keyboard starts in the keyboard image.
*/
Expand All @@ -169,16 +153,14 @@ public enum KeyboardType {
* @param opacity The opacity of this KeyboardType.
* @param renderOffset Offset from the bottom of the screen that the keyboard
* is rendered at.
* @param closeBuffer Buffer over the keyboard above which a tap or click
* will close the keyboard.
* @param xStart The X value at which the keyboard starts in the
* keyboard image.
* @param activeWidth The width of the active part of the keyboard image, or
* -1 to deduce from texture width and xStart.
* @param yStart The Y value at which the keyboard starts in the
* keyboard image.
*/
KeyboardType(Integer[][] keyMap, String keyboardImage, float opacity, int renderOffset, int closeBuffer,
KeyboardType(Integer[][] keyMap, String keyboardImage, float opacity, int renderOffset,
int xStart, int activeWidth, int yStart) {
this.keyMap = keyMap;
this.texture = new Texture(keyboardImage);
Expand All @@ -187,12 +169,9 @@ public enum KeyboardType {

activeWidth = (activeWidth == -1 ? this.texture.getWidth() - this.xStart : activeWidth);

this.vertKeySize = ((float) (((float) this.texture.getHeight()) - (float) this.yStart)
/ (float) this.keyMap.length);
this.horizKeySize = ((float) activeWidth / (float) this.keyMap[0].length);
this.opacity = opacity;
this.renderOffset = renderOffset;
this.closeHeight = this.texture.getHeight() + renderOffset + closeBuffer;
this.activeWidth = activeWidth;
}

Expand All @@ -215,7 +194,10 @@ public enum KeyboardType {
*/
public Integer getKeyCode(float x, float y) {
Integer keyCode = null;
int keyRow = (int) ((texture.getHeight() - (y - yStart) + renderOffset) / vertKeySize);
float height = getHeight();
float vertKeySize = ((float) (((float) height) - (float) this.yStart) / (float) this.keyMap.length);

int keyRow = (int) ((getHeight() - (y - yStart) + renderOffset) / vertKeySize);

if (keyRow >= keyMap.length) {
keyRow = keyMap.length - 1;
Expand All @@ -239,6 +221,30 @@ public Integer getKeyCode(float x, float y) {

return keyCode;
}

/**
* Gets the height of the keyboard.
*
* @return The current height of the keyboard.
*/
public float getHeight() {
if (isLandscape()) {
return texture.getHeight();
} else {
ViewportManager viewportManager = ViewportManager.getInstance();
int keyboardHeight = viewportManager.getScreenBase() - getRenderOffset();
return Math.max(Math.min(keyboardHeight, texture.getHeight()), 365);
}
}

/**
* Gets the Y value for the top of this KeyboardType.
*
* @return The Y value for the top of this KeyboardType.
*/
public float getTop() {
return (getRenderOffset() + getHeight());
}

/**
* Tests if the given X/Y position is within the bounds of this KeyboardTypes
Expand All @@ -252,7 +258,7 @@ public Integer getKeyCode(float x, float y) {
*/
public boolean isInKeyboard(float x, float y) {
if (isRendered()) {
boolean isInYBounds = (y < (texture.getHeight() + renderOffset) && (y > renderOffset));
boolean isInYBounds = (y < (getHeight() + renderOffset) && (y > renderOffset));
boolean isInXBounds = ((x >= xStart) && (x < (xStart + activeWidth)));
return isInYBounds && isInXBounds;

Expand All @@ -261,7 +267,7 @@ public boolean isInKeyboard(float x, float y) {
return false;
}
}

/**
* @return The Texture holding the keyboard image for this KeyboardType.
*/
Expand Down Expand Up @@ -310,13 +316,6 @@ public int getRenderOffset() {
return renderOffset;
}

/**
* @return The height above which the keyboard will close.
*/
public int getCloseHeight() {
return closeHeight;
}

/**
* Disposes of the libGDX Texture for all KeyboardTypes.
*/
Expand Down
17 changes: 16 additions & 1 deletion core/src/main/java/com/agifans/agile/ui/ViewportManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ public static ViewportManager getInstance() {
* @param height The new screen height.
*/
public void update(int width, int height) {
portrait = (height > (width / 1.32f));
// 1.13 when icons are no longer overlapping
// 1.00 square
// 0.80 where keyboard top matches screen base
//portrait = (height > (width / 1.32f));
//portrait = (height > (width / 1.00f));

portrait = (height > (width / 0.80f));

getCurrentViewport().update(width, height, true);
}

Expand Down Expand Up @@ -102,6 +109,14 @@ public boolean isLandscape() {
return !portrait;
}

public boolean doesScreenFitWidth() {
return !(getHeight() > (getWidth() / 1.32f));
}

public int getScreenBase() {
return (int)(getHeight() - (getWidth() / 1.32));
}

/**
* Gets the current Camera to use when rendering UI components.
*
Expand Down

0 comments on commit c71d769

Please sign in to comment.