Make the VR keys respect the toggle option and display a visual feedback when they are being used

This commit is contained in:
Sollace 2024-04-12 00:50:03 +01:00
parent 408c9d11c0
commit 3489c47d63
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 53 additions and 35 deletions

View file

@ -18,6 +18,10 @@ public enum ActivationType {
return ordinal(); return ordinal();
} }
public boolean isResult() {
return this != NONE;
}
public static ActivationType of(int id) { public static ActivationType of(int id) {
return VALUES[MathHelper.clamp(id, 0, VALUES.length)]; return VALUES[MathHelper.clamp(id, 0, VALUES.length)];
} }

View file

@ -2,8 +2,10 @@ package com.minelittlepony.unicopia.client;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
@ -34,12 +36,12 @@ public class KeyBindingsHandler {
private final Map<Binding, AbilitySlot> keys = new HashMap<>(); private final Map<Binding, AbilitySlot> keys = new HashMap<>();
private final Map<AbilitySlot, Binding> reverse = new HashMap<>(); private final Map<AbilitySlot, Binding> reverse = new HashMap<>();
private final Binding pageDown = register(GLFW.GLFW_KEY_PAGE_DOWN, "hud_page_dn"); private final Binding pageDown = new Binding(create(GLFW.GLFW_KEY_PAGE_DOWN, "hud_page_dn"));
private final Binding pageUp = register(GLFW.GLFW_KEY_PAGE_UP, "hud_page_up"); private final Binding pageUp = new Binding(create(GLFW.GLFW_KEY_PAGE_UP, "hud_page_up"));
private final Binding singleTapModifier = register(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_tap"); private final KeyBinding singleTapModifier = createSticky(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_tap");
private final Binding doubleTapModifier = register(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_double_tap"); private final KeyBinding doubleTapModifier = createSticky(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_double_tap");
private final Binding tripleTapModifier = register(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_triple_tap"); private final KeyBinding tripleTapModifier = createSticky(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_triple_tap");
private final Set<KeyBinding> pressed = new HashSet<>(); private final Set<KeyBinding> pressed = new HashSet<>();
@ -57,14 +59,34 @@ public class KeyBindingsHandler {
return Unicopia.getConfig().toggleAbilityKeys.get(); return Unicopia.getConfig().toggleAbilityKeys.get();
} }
public ActivationType getForcedActivationType() {
if (singleTapModifier.isPressed()) {
return ActivationType.TAP;
}
if (doubleTapModifier.isPressed()) {
return ActivationType.DOUBLE_TAP;
}
if (tripleTapModifier.isPressed()) {
return ActivationType.TRIPLE_TAP;
}
return ActivationType.NONE;
}
public void addKeybind(int code, AbilitySlot slot) { public void addKeybind(int code, AbilitySlot slot) {
Binding binding = new Binding(KeyBindingHelper.registerKeyBinding(new StickyKeyBinding("key.unicopia." + slot.name().toLowerCase(), code, KEY_CATEGORY, this::isToggleMode))); Binding binding = new Binding(createSticky(code, slot.name().toLowerCase(Locale.ROOT)));
reverse.put(slot, binding); reverse.put(slot, binding);
keys.put(binding, slot); keys.put(binding, slot);
} }
Binding register(int code, String name) { KeyBinding create(int code, String name) {
return new Binding(KeyBindingHelper.registerKeyBinding(new KeyBinding("key.unicopia." + name, code, KEY_CATEGORY))); return KeyBindingHelper.registerKeyBinding(new KeyBinding("key.unicopia." + name, code, KEY_CATEGORY));
}
KeyBinding createSticky(int code, String name) {
return KeyBindingHelper.registerKeyBinding(new StickyKeyBinding("key.unicopia." + name, code, KEY_CATEGORY, this::isToggleMode));
} }
public void tick(MinecraftClient client) { public void tick(MinecraftClient client) {
@ -102,7 +124,7 @@ public class KeyBindingsHandler {
} }
} else { } else {
ActivationType type = i.getType(); ActivationType type = i.getType();
if (type != ActivationType.NONE) { if (type.isResult()) {
abilities.clear(slot, type, page); abilities.clear(slot, type, page);
} }
} }
@ -124,7 +146,7 @@ public class KeyBindingsHandler {
private long nextPhaseTime; private long nextPhaseTime;
private ActivationType type = ActivationType.NONE; private final AtomicReference<ActivationType> type = new AtomicReference<>(ActivationType.NONE);
Binding(KeyBinding binding) { Binding(KeyBinding binding) {
this.binding = binding; this.binding = binding;
@ -145,40 +167,25 @@ public class KeyBindingsHandler {
if (state == PressedState.RELEASED && now < nextPhaseTime + 10) { if (state == PressedState.RELEASED && now < nextPhaseTime + 10) {
nextPhaseTime = now + 200; nextPhaseTime = now + 200;
type = type.getNext(); type.set(type.get().getNext());
} }
return state; return state;
} }
public ActivationType getType() { public ActivationType getType() {
if (binding.isPressed() && binding instanceof StickyKeyBinding) { if (binding.isPressed()) {
if (singleTapModifier.binding.isPressed()) { ActivationType t = getForcedActivationType();
if (t.isResult()) {
KeyBinding.untoggleStickyKeys(); KeyBinding.untoggleStickyKeys();
return ActivationType.TAP;
}
if (doubleTapModifier.binding.isPressed()) {
KeyBinding.untoggleStickyKeys();
return ActivationType.DOUBLE_TAP;
}
if (tripleTapModifier.binding.isPressed()) {
KeyBinding.untoggleStickyKeys();
return ActivationType.TRIPLE_TAP;
}
if (isToggleMode()) {
return ActivationType.NONE;
}
}
long now = System.currentTimeMillis();
if (type != ActivationType.NONE && now > nextPhaseTime - 70) {
ActivationType t = type;
type = ActivationType.NONE;
return t; return t;
} }
}
if (!isToggleMode() && System.currentTimeMillis() > nextPhaseTime - 70) {
return type.getAndSet(ActivationType.NONE);
}
return ActivationType.NONE; return ActivationType.NONE;
} }

View file

@ -3,12 +3,14 @@ package com.minelittlepony.unicopia.client.gui;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.ability.AbilityDispatcher; import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.ability.AbilitySlot; import com.minelittlepony.unicopia.ability.AbilitySlot;
import com.minelittlepony.unicopia.ability.ActivationType;
import com.minelittlepony.unicopia.client.KeyBindingsHandler; import com.minelittlepony.unicopia.client.KeyBindingsHandler;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text; import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
class Slot { class Slot {
@ -134,7 +136,7 @@ class Slot {
return; return;
} }
Text label = KeyBindingsHandler.INSTANCE.getBinding(aSlot).getLabel(); MutableText label = KeyBindingsHandler.INSTANCE.getBinding(aSlot).getLabel().copy().formatted(Formatting.BOLD);
MatrixStack matrices = context.getMatrices(); MatrixStack matrices = context.getMatrices();
matrices.push(); matrices.push();
@ -150,6 +152,11 @@ class Slot {
matrices.translate(x, getY() + labelY, 0); matrices.translate(x, getY() + labelY, 0);
matrices.scale(0.5F, 0.5F, 0.5F); matrices.scale(0.5F, 0.5F, 0.5F);
ActivationType activation = KeyBindingsHandler.INSTANCE.getForcedActivationType();
if (activation.isResult()) {
label = label.append("+T" + activation.getTapCount());
}
context.drawText(uHud.font, label, 0, 0, 0xFFFFFF, true); context.drawText(uHud.font, label, 0, 0, 0xFFFFFF, true);
matrices.pop(); matrices.pop();