Files
Client/src/main/java/de/fullrisk/citrusDev/UI/Module_KeyStrokes.java
T

130 lines
5.0 KiB
Java

package de.fullrisk.citrusDev.UI;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.UIComponents;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.UIContainers;
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.hud.Hud;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import de.fullrisk.citrusDev.Config;
public class Module_KeyStrokes {
private static FlowLayout keyContainer;
private static FlowLayout topRow;
private static FlowLayout bottomRow;
private static FlowLayout wBox, aBox, sBox, dBox;
private static LabelComponent wLabel, aLabel, sLabel, dLabel;
static boolean toggle = true;
private static boolean shown = false;
public static boolean isEnabled() {
return toggle;
}
public static void setEnabled(boolean value) {
toggle = value;
}
private static final Config CONFIG = Config.get();
private static final Surface GLASS_PANEL = Surface.blur(5f, 15f)
.and(Surface.flat(0x40FFFFFF));
private static final int KEY_SIZE = 16;
private static final int PRESSED_COLOR = 0xFF64de4b;
private static final int IDLE_COLOR = 0x40FFFFFF;
public static void setPosition(int x, int y) {
if (keyContainer != null) {
keyContainer.positioning(Positioning.relative(x, y));
}
CONFIG.keyX = x;
CONFIG.keyY = y;
CONFIG.save();
}
public static void register() {
Hud.add(Identifier.fromNamespaceAndPath("citrus-dev", "keystrokes_display"), () -> {
if (keyContainer == null) {
wLabel = UIComponents.label(Component.literal("W"));
aLabel = UIComponents.label(Component.literal("A"));
sLabel = UIComponents.label(Component.literal("S"));
dLabel = UIComponents.label(Component.literal("D"));
wBox = makeKeyBox(wLabel);
aBox = makeKeyBox(aLabel);
sBox = makeKeyBox(sLabel);
dBox = makeKeyBox(dLabel);
topRow = UIContainers.horizontalFlow(Sizing.content(), Sizing.content());
topRow.gap(2);
topRow.horizontalAlignment(HorizontalAlignment.CENTER);
topRow.child(UIContainers.horizontalFlow(Sizing.fixed(KEY_SIZE), Sizing.fixed(KEY_SIZE))); // spacer
topRow.child(wBox);
topRow.child(UIContainers.horizontalFlow(Sizing.fixed(KEY_SIZE), Sizing.fixed(KEY_SIZE))); // spacer
bottomRow = UIContainers.horizontalFlow(Sizing.content(), Sizing.content());
bottomRow.gap(2);
bottomRow.horizontalAlignment(HorizontalAlignment.CENTER);
bottomRow.child(aBox);
bottomRow.child(sBox);
bottomRow.child(dBox);
keyContainer = UIContainers.verticalFlow(Sizing.content(), Sizing.content());
keyContainer.gap(2);
keyContainer.verticalAlignment(VerticalAlignment.CENTER);
keyContainer.horizontalAlignment(HorizontalAlignment.CENTER);
keyContainer.padding(Insets.of(0));
keyContainer.positioning(Positioning.relative(CONFIG.keyX, CONFIG.keyY));
}
return keyContainer;
});
}
private static FlowLayout makeKeyBox(LabelComponent label) {
FlowLayout box = UIContainers.verticalFlow(Sizing.fixed(KEY_SIZE), Sizing.fixed(KEY_SIZE));
box.horizontalAlignment(HorizontalAlignment.CENTER);
box.verticalAlignment(VerticalAlignment.CENTER);
box.surface(Surface.flat(IDLE_COLOR));
box.child(label);
return box;
}
public static void tick() {
if (keyContainer == null) return;
if (toggle && !shown) {
keyContainer.clearChildren(); // <-- safety net against duplicate adds
keyContainer.surface(GLASS_PANEL);
keyContainer.padding(Insets.of(Config.get().fpsP));
keyContainer.child(topRow);
keyContainer.child(bottomRow);
shown = true;
} else if (!toggle && shown) {
keyContainer.clearChildren();
keyContainer.surface(Surface.flat(0));
keyContainer.padding(Insets.of(0));
shown = false;
}
if (!toggle) return;
Minecraft client = Minecraft.getInstance();
KeyMapping up = client.options.keyUp;
KeyMapping left = client.options.keyLeft;
KeyMapping down = client.options.keyDown;
KeyMapping right = client.options.keyRight;
wBox.surface(Surface.flat(up.isDown() ? PRESSED_COLOR : IDLE_COLOR));
aBox.surface(Surface.flat(left.isDown() ? PRESSED_COLOR : IDLE_COLOR));
sBox.surface(Surface.flat(down.isDown() ? PRESSED_COLOR : IDLE_COLOR));
dBox.surface(Surface.flat(right.isDown() ? PRESSED_COLOR : IDLE_COLOR));
}
}