mirror of
https://github.com/TeamMidnightDust/MidnightControls.git
synced 2025-12-13 23:25:10 +01:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -8,52 +8,52 @@ public class KeyboardLayout {
|
||||
|
||||
public static KeyboardLayout QWERTY = new KeyboardLayout(createQwertyLetterLayout(), createSymbolLayout());
|
||||
|
||||
private final List<List<KeyInfo>> letters;
|
||||
private final List<List<KeyInfo>> symbols;
|
||||
private final List<List<String>> letters;
|
||||
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.symbols = symbols;
|
||||
}
|
||||
|
||||
public List<List<KeyInfo>> getLetters() {
|
||||
public List<List<String>> getLetters() {
|
||||
return letters;
|
||||
}
|
||||
|
||||
public List<List<KeyInfo>> getSymbols() {
|
||||
public List<List<String>> getSymbols() {
|
||||
return symbols;
|
||||
}
|
||||
|
||||
private static List<List<KeyInfo>> createQwertyLetterLayout() {
|
||||
List<List<KeyInfo>> letters = new ArrayList<>();
|
||||
private static List<List<String>> createQwertyLetterLayout() {
|
||||
List<List<String>> letters = new ArrayList<>();
|
||||
letters.add(Arrays.asList(
|
||||
new KeyInfo("q"), new KeyInfo("w"), new KeyInfo("e"), new KeyInfo("r"), new KeyInfo("t"),
|
||||
new KeyInfo("y"), new KeyInfo("u"), new KeyInfo("i"), new KeyInfo("o"), new KeyInfo("p")
|
||||
"q", "w", "e", "r", "t",
|
||||
"y", "u", "i", "o", "p"
|
||||
));
|
||||
letters.add(Arrays.asList(
|
||||
new KeyInfo("a"), new KeyInfo("s"), new KeyInfo("d"), new KeyInfo("f"), new KeyInfo("g"),
|
||||
new KeyInfo("h"), new KeyInfo("j"), new KeyInfo("k"), new KeyInfo("l")
|
||||
"a", "s", "d", "f", "g",
|
||||
"h", "j", "k", "l"
|
||||
));
|
||||
letters.add(Arrays.asList(
|
||||
new KeyInfo("z"), new KeyInfo("x"), new KeyInfo("c"), new KeyInfo("v"),
|
||||
new KeyInfo("b"), new KeyInfo("n"), new KeyInfo("m")
|
||||
"z", "x", "c", "v",
|
||||
"b", "n", "m"
|
||||
));
|
||||
return letters;
|
||||
}
|
||||
|
||||
private static List<List<KeyInfo>> createSymbolLayout() {
|
||||
List<List<KeyInfo>> symbols = new ArrayList<>();
|
||||
private static List<List<String>> createSymbolLayout() {
|
||||
List<List<String>> symbols = new ArrayList<>();
|
||||
symbols.add(Arrays.asList(
|
||||
new KeyInfo("1"), new KeyInfo("2"), new KeyInfo("3"), new KeyInfo("4"), new KeyInfo("5"),
|
||||
new KeyInfo("6"), new KeyInfo("7"), new KeyInfo("8"), new KeyInfo("9"), new KeyInfo("0")
|
||||
"1", "2", "3", "4", "5",
|
||||
"6", "7", "8", "9", "0"
|
||||
));
|
||||
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(
|
||||
new KeyInfo("!"), new KeyInfo("\""), new KeyInfo("'"), new KeyInfo(":"), new KeyInfo(";"),
|
||||
new KeyInfo(","), new KeyInfo("."), new KeyInfo("?"), new KeyInfo("/")
|
||||
"!", "\"", "'", ":", ";",
|
||||
",", ".", "?", "/"
|
||||
));
|
||||
return symbols;
|
||||
}
|
||||
|
||||
@@ -74,7 +74,6 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
@Override
|
||||
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
|
||||
this.renderBackground(drawContext, mouseX, mouseY, delta);
|
||||
drawContext.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 15, 0xFFFFFF);
|
||||
super.render(drawContext, mouseX, mouseY, delta);
|
||||
}
|
||||
|
||||
@@ -96,10 +95,10 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
this.remove(this.keyboardContainer);
|
||||
}
|
||||
|
||||
var keys = getActiveKeyLayout();
|
||||
var keyboardContainer = createKeyboardContainer(keys);
|
||||
var layoutKeys = getActiveKeyLayout();
|
||||
var keyboardContainer = createKeyboardContainer(layoutKeys);
|
||||
|
||||
addLetterRows(keyboardContainer, keys);
|
||||
addLayoutRows(keyboardContainer, layoutKeys);
|
||||
addFunctionKeys(keyboardContainer);
|
||||
addBottomRow(keyboardContainer);
|
||||
|
||||
@@ -107,7 +106,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
this.addDrawableChild(this.keyboardContainer);
|
||||
}
|
||||
|
||||
private SpruceContainerWidget createKeyboardContainer(List<List<KeyInfo>> layoutKeys) {
|
||||
private SpruceContainerWidget createKeyboardContainer(List<List<String>> layoutKeys) {
|
||||
int containerWidth = this.width;
|
||||
int totalKeyboardHeight = calculateKeyboardHeight(layoutKeys);
|
||||
int keyboardY = this.bufferDisplayArea.getY() + this.bufferDisplayArea.getHeight() + VERTICAL_SPACING * 2;
|
||||
@@ -141,44 +140,42 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
return bufferDisplay;
|
||||
}
|
||||
|
||||
private int calculateKeyboardHeight(List<List<KeyInfo>> keyRows) {
|
||||
private int calculateKeyboardHeight(List<List<String>> keyRows) {
|
||||
return keyRows.size() * (KEY_HEIGHT + VERTICAL_SPACING) +
|
||||
(KEY_HEIGHT + VERTICAL_SPACING) + // space for bottom row
|
||||
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;
|
||||
|
||||
for (List<KeyInfo> row : keyRows) {
|
||||
for (List<String> row : keyLayoutRows) {
|
||||
int rowWidth = calculateRowWidth(row);
|
||||
// center row
|
||||
int currentX = (container.getWidth() - rowWidth) / 2;
|
||||
|
||||
for (KeyInfo keyInfo : row) {
|
||||
int keyWidth = (int) (STANDARD_KEY_WIDTH * keyInfo.widthFactor());
|
||||
String displayText = getKeyDisplayText(keyInfo);
|
||||
for (String key : row) {
|
||||
String displayText = (this.capsMode && !this.symbolMode) ? key.toUpperCase() : key;
|
||||
container.addChild(
|
||||
new SpruceButtonWidget(
|
||||
Position.of(currentX, currentY),
|
||||
keyWidth,
|
||||
STANDARD_KEY_WIDTH,
|
||||
KEY_HEIGHT,
|
||||
Text.literal(displayText),
|
||||
btn -> handleKeyPress(displayText)
|
||||
)
|
||||
);
|
||||
|
||||
currentX += keyWidth + HORIZONTAL_SPACING;
|
||||
currentX += STANDARD_KEY_WIDTH + HORIZONTAL_SPACING;
|
||||
}
|
||||
|
||||
currentY += KEY_HEIGHT + VERTICAL_SPACING;
|
||||
}
|
||||
}
|
||||
|
||||
private int calculateRowWidth(List<KeyInfo> row) {
|
||||
private int calculateRowWidth(List<String> row) {
|
||||
int rowWidth = 0;
|
||||
for (int i = 0; i < row.size(); i++) {
|
||||
rowWidth += (int) (STANDARD_KEY_WIDTH * row.get(i).widthFactor());
|
||||
if (i < row.size() - 1) {
|
||||
rowWidth += HORIZONTAL_SPACING;
|
||||
}
|
||||
@@ -187,7 +184,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
}
|
||||
|
||||
private void addFunctionKeys(SpruceContainerWidget container) {
|
||||
List<KeyInfo> firstRow = getActiveKeyLayout().get(0);
|
||||
List<String> firstRow = getActiveKeyLayout().get(0);
|
||||
int firstRowWidth = calculateRowWidth(firstRow);
|
||||
|
||||
// position backspace at the right of the first row
|
||||
@@ -207,7 +204,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
|
||||
if (this.newLineSupport) {
|
||||
// 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 secondRowWidth = calculateRowWidth(secondRow);
|
||||
int newlineX = (container.getWidth() + secondRowWidth) / 2 + HORIZONTAL_SPACING;
|
||||
@@ -284,15 +281,7 @@ public class VirtualKeyboardScreen extends SpruceScreen {
|
||||
}
|
||||
}
|
||||
|
||||
private String getKeyDisplayText(KeyInfo keyInfo) {
|
||||
if(this.capsMode && !this.symbolMode) {
|
||||
return keyInfo.displayText().toUpperCase();
|
||||
}
|
||||
|
||||
return keyInfo.displayText();
|
||||
}
|
||||
|
||||
private List<List<KeyInfo>> getActiveKeyLayout() {
|
||||
private List<List<String>> getActiveKeyLayout() {
|
||||
return this.symbolMode ? this.layout.getSymbols() : this.layout.getLetters();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user