Replace KeyInfo with String

- KeyInfo was leftover code from toying around with various implementations. String is sufficient in current implementation.
This commit is contained in:
cryy
2025-04-22 20:16:08 +02:00
parent 041eeb29aa
commit bb5c6976c0
3 changed files with 36 additions and 63 deletions

View File

@@ -1,16 +0,0 @@
package eu.midnightdust.midnightcontrols.client.gui.virtualkeyboard;
public record KeyInfo(String keySymbol, String displayText, double widthFactor) {
// Convenience constructor for standard width keys
KeyInfo(String keySymbol, double widthFactor) {
this(keySymbol, keySymbol, widthFactor);
}
KeyInfo(String keySymbol) {
this(keySymbol, 1.0);
}
KeyInfo(String keySymbol, String displayText) {
this(keySymbol, displayText, 1.0);
}
}

View File

@@ -8,52 +8,52 @@ public class KeyboardLayout {
public static KeyboardLayout QWERTY = new KeyboardLayout(createQwertyLetterLayout(), createSymbolLayout()); public static KeyboardLayout QWERTY = new KeyboardLayout(createQwertyLetterLayout(), createSymbolLayout());
private final List<List<KeyInfo>> letters; private final List<List<String>> letters;
private final List<List<KeyInfo>> symbols; private final List<List<String>> symbols;
private KeyboardLayout(List<List<KeyInfo>> letters, List<List<KeyInfo>> symbols) { private KeyboardLayout(List<List<String>> letters, List<List<String>> symbols) {
this.letters = letters; this.letters = letters;
this.symbols = symbols; this.symbols = symbols;
} }
public List<List<KeyInfo>> getLetters() { public List<List<String>> getLetters() {
return letters; return letters;
} }
public List<List<KeyInfo>> getSymbols() { public List<List<String>> getSymbols() {
return symbols; return symbols;
} }
private static List<List<KeyInfo>> createQwertyLetterLayout() { private static List<List<String>> createQwertyLetterLayout() {
List<List<KeyInfo>> letters = new ArrayList<>(); List<List<String>> letters = new ArrayList<>();
letters.add(Arrays.asList( letters.add(Arrays.asList(
new KeyInfo("q"), new KeyInfo("w"), new KeyInfo("e"), new KeyInfo("r"), new KeyInfo("t"), "q", "w", "e", "r", "t",
new KeyInfo("y"), new KeyInfo("u"), new KeyInfo("i"), new KeyInfo("o"), new KeyInfo("p") "y", "u", "i", "o", "p"
)); ));
letters.add(Arrays.asList( letters.add(Arrays.asList(
new KeyInfo("a"), new KeyInfo("s"), new KeyInfo("d"), new KeyInfo("f"), new KeyInfo("g"), "a", "s", "d", "f", "g",
new KeyInfo("h"), new KeyInfo("j"), new KeyInfo("k"), new KeyInfo("l") "h", "j", "k", "l"
)); ));
letters.add(Arrays.asList( letters.add(Arrays.asList(
new KeyInfo("z"), new KeyInfo("x"), new KeyInfo("c"), new KeyInfo("v"), "z", "x", "c", "v",
new KeyInfo("b"), new KeyInfo("n"), new KeyInfo("m") "b", "n", "m"
)); ));
return letters; return letters;
} }
private static List<List<KeyInfo>> createSymbolLayout() { private static List<List<String>> createSymbolLayout() {
List<List<KeyInfo>> symbols = new ArrayList<>(); List<List<String>> symbols = new ArrayList<>();
symbols.add(Arrays.asList( symbols.add(Arrays.asList(
new KeyInfo("1"), new KeyInfo("2"), new KeyInfo("3"), new KeyInfo("4"), new KeyInfo("5"), "1", "2", "3", "4", "5",
new KeyInfo("6"), new KeyInfo("7"), new KeyInfo("8"), new KeyInfo("9"), new KeyInfo("0") "6", "7", "8", "9", "0"
)); ));
symbols.add(Arrays.asList( symbols.add(Arrays.asList(
new KeyInfo("@"), new KeyInfo("#"), new KeyInfo("$"), new KeyInfo("%"), new KeyInfo("&"), "@", "#", "$", "%", "&",
new KeyInfo("*"), new KeyInfo("-"), new KeyInfo("+"), new KeyInfo("("), new KeyInfo(")") "*", "-", "+", "(", ")"
)); ));
symbols.add(Arrays.asList( symbols.add(Arrays.asList(
new KeyInfo("!"), new KeyInfo("\""), new KeyInfo("'"), new KeyInfo(":"), new KeyInfo(";"), "!", "\"", "'", ":", ";",
new KeyInfo(","), new KeyInfo("."), new KeyInfo("?"), new KeyInfo("/") ",", ".", "?", "/"
)); ));
return symbols; return symbols;
} }

View File

@@ -74,7 +74,6 @@ public class VirtualKeyboardScreen extends SpruceScreen {
@Override @Override
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
this.renderBackground(drawContext, mouseX, mouseY, delta); this.renderBackground(drawContext, mouseX, mouseY, delta);
drawContext.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 15, 0xFFFFFF);
super.render(drawContext, mouseX, mouseY, delta); super.render(drawContext, mouseX, mouseY, delta);
} }
@@ -96,10 +95,10 @@ public class VirtualKeyboardScreen extends SpruceScreen {
this.remove(this.keyboardContainer); this.remove(this.keyboardContainer);
} }
var keys = getActiveKeyLayout(); var layoutKeys = getActiveKeyLayout();
var keyboardContainer = createKeyboardContainer(keys); var keyboardContainer = createKeyboardContainer(layoutKeys);
addLetterRows(keyboardContainer, keys); addLayoutRows(keyboardContainer, layoutKeys);
addFunctionKeys(keyboardContainer); addFunctionKeys(keyboardContainer);
addBottomRow(keyboardContainer); addBottomRow(keyboardContainer);
@@ -107,7 +106,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
this.addDrawableChild(this.keyboardContainer); this.addDrawableChild(this.keyboardContainer);
} }
private SpruceContainerWidget createKeyboardContainer(List<List<KeyInfo>> layoutKeys) { private SpruceContainerWidget createKeyboardContainer(List<List<String>> layoutKeys) {
int containerWidth = this.width; int containerWidth = this.width;
int totalKeyboardHeight = calculateKeyboardHeight(layoutKeys); int totalKeyboardHeight = calculateKeyboardHeight(layoutKeys);
int keyboardY = this.bufferDisplayArea.getY() + this.bufferDisplayArea.getHeight() + VERTICAL_SPACING * 2; int keyboardY = this.bufferDisplayArea.getY() + this.bufferDisplayArea.getHeight() + VERTICAL_SPACING * 2;
@@ -141,44 +140,42 @@ public class VirtualKeyboardScreen extends SpruceScreen {
return bufferDisplay; return bufferDisplay;
} }
private int calculateKeyboardHeight(List<List<KeyInfo>> keyRows) { private int calculateKeyboardHeight(List<List<String>> keyRows) {
return keyRows.size() * (KEY_HEIGHT + VERTICAL_SPACING) + return keyRows.size() * (KEY_HEIGHT + VERTICAL_SPACING) +
(KEY_HEIGHT + VERTICAL_SPACING) + // space for bottom row (KEY_HEIGHT + VERTICAL_SPACING) + // space for bottom row
CONTAINER_PADDING * 2; // top and bottom padding CONTAINER_PADDING * 2; // top and bottom padding
} }
private void addLetterRows(SpruceContainerWidget container, List<List<KeyInfo>> keyRows) { private void addLayoutRows(SpruceContainerWidget container, List<List<String>> keyLayoutRows) {
int currentY = CONTAINER_PADDING; int currentY = CONTAINER_PADDING;
for (List<KeyInfo> row : keyRows) { for (List<String> row : keyLayoutRows) {
int rowWidth = calculateRowWidth(row); int rowWidth = calculateRowWidth(row);
// center row // center row
int currentX = (container.getWidth() - rowWidth) / 2; int currentX = (container.getWidth() - rowWidth) / 2;
for (KeyInfo keyInfo : row) { for (String key : row) {
int keyWidth = (int) (STANDARD_KEY_WIDTH * keyInfo.widthFactor()); String displayText = (this.capsMode && !this.symbolMode) ? key.toUpperCase() : key;
String displayText = getKeyDisplayText(keyInfo);
container.addChild( container.addChild(
new SpruceButtonWidget( new SpruceButtonWidget(
Position.of(currentX, currentY), Position.of(currentX, currentY),
keyWidth, STANDARD_KEY_WIDTH,
KEY_HEIGHT, KEY_HEIGHT,
Text.literal(displayText), Text.literal(displayText),
btn -> handleKeyPress(displayText) btn -> handleKeyPress(displayText)
) )
); );
currentX += keyWidth + HORIZONTAL_SPACING; currentX += STANDARD_KEY_WIDTH + HORIZONTAL_SPACING;
} }
currentY += KEY_HEIGHT + VERTICAL_SPACING; currentY += KEY_HEIGHT + VERTICAL_SPACING;
} }
} }
private int calculateRowWidth(List<KeyInfo> row) { private int calculateRowWidth(List<String> row) {
int rowWidth = 0; int rowWidth = 0;
for (int i = 0; i < row.size(); i++) { for (int i = 0; i < row.size(); i++) {
rowWidth += (int) (STANDARD_KEY_WIDTH * row.get(i).widthFactor());
if (i < row.size() - 1) { if (i < row.size() - 1) {
rowWidth += HORIZONTAL_SPACING; rowWidth += HORIZONTAL_SPACING;
} }
@@ -187,7 +184,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
} }
private void addFunctionKeys(SpruceContainerWidget container) { private void addFunctionKeys(SpruceContainerWidget container) {
List<KeyInfo> firstRow = getActiveKeyLayout().get(0); List<String> firstRow = getActiveKeyLayout().get(0);
int firstRowWidth = calculateRowWidth(firstRow); int firstRowWidth = calculateRowWidth(firstRow);
// position backspace at the right of the first row // position backspace at the right of the first row
@@ -207,7 +204,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
if (this.newLineSupport) { if (this.newLineSupport) {
// position newline at the right of the second row // position newline at the right of the second row
List<KeyInfo> secondRow = getActiveKeyLayout().get(1); List<String> secondRow = getActiveKeyLayout().get(1);
int newlineWidth = (int) (STANDARD_KEY_WIDTH * 1.5); int newlineWidth = (int) (STANDARD_KEY_WIDTH * 1.5);
int secondRowWidth = calculateRowWidth(secondRow); int secondRowWidth = calculateRowWidth(secondRow);
int newlineX = (container.getWidth() + secondRowWidth) / 2 + HORIZONTAL_SPACING; int newlineX = (container.getWidth() + secondRowWidth) / 2 + HORIZONTAL_SPACING;
@@ -284,15 +281,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
} }
} }
private String getKeyDisplayText(KeyInfo keyInfo) { private List<List<String>> getActiveKeyLayout() {
if(this.capsMode && !this.symbolMode) {
return keyInfo.displayText().toUpperCase();
}
return keyInfo.displayText();
}
private List<List<KeyInfo>> getActiveKeyLayout() {
return this.symbolMode ? this.layout.getSymbols() : this.layout.getLetters(); return this.symbolMode ? this.layout.getSymbols() : this.layout.getLetters();
} }