2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia.client;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-04-25 15:37:17 +02:00
|
|
|
import java.util.HashMap;
|
2020-04-15 15:45:57 +02:00
|
|
|
import java.util.HashSet;
|
2024-04-12 01:50:03 +02:00
|
|
|
import java.util.Locale;
|
2020-04-25 15:37:17 +02:00
|
|
|
import java.util.Map;
|
2020-04-15 15:45:57 +02:00
|
|
|
import java.util.Set;
|
2024-04-12 01:50:03 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
import org.lwjgl.glfw.GLFW;
|
|
|
|
|
2023-08-22 17:25:36 +02:00
|
|
|
import com.minelittlepony.unicopia.USounds;
|
2022-12-27 20:40:24 +01:00
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
2020-10-09 14:37:49 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.AbilityDispatcher;
|
2020-05-06 15:55:25 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.AbilitySlot;
|
2021-12-31 14:31:23 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.ActivationType;
|
2020-10-09 14:37:49 +02:00
|
|
|
import com.minelittlepony.unicopia.client.gui.UHud;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-06-26 11:44:47 +02:00
|
|
|
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
2020-01-16 12:35:46 +01:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
2021-08-04 15:38:03 +02:00
|
|
|
import net.minecraft.client.option.KeyBinding;
|
2024-04-08 23:13:08 +02:00
|
|
|
import net.minecraft.client.option.StickyKeyBinding;
|
2020-10-09 14:37:49 +02:00
|
|
|
import net.minecraft.client.sound.PositionedSoundInstance;
|
2024-04-08 23:13:08 +02:00
|
|
|
import net.minecraft.client.util.InputUtil;
|
2021-12-31 14:31:23 +01:00
|
|
|
import net.minecraft.text.Text;
|
2020-10-09 14:37:49 +02:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-09-22 18:13:20 +02:00
|
|
|
public class KeyBindingsHandler {
|
2020-10-09 14:37:49 +02:00
|
|
|
private static final String KEY_CATEGORY = "unicopia.category.name";
|
2020-04-25 15:37:17 +02:00
|
|
|
|
2020-09-22 18:13:20 +02:00
|
|
|
public static final KeyBindingsHandler INSTANCE = new KeyBindingsHandler();
|
|
|
|
|
2020-09-25 13:06:41 +02:00
|
|
|
static void bootstrap() {}
|
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
private final Map<Binding, AbilitySlot> keys = new HashMap<>();
|
|
|
|
private final Map<AbilitySlot, Binding> reverse = new HashMap<>();
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2024-04-12 01:50:03 +02:00
|
|
|
private final Binding pageDown = new Binding(create(GLFW.GLFW_KEY_PAGE_DOWN, "hud_page_dn"));
|
|
|
|
private final Binding pageUp = new Binding(create(GLFW.GLFW_KEY_PAGE_UP, "hud_page_up"));
|
2020-10-09 14:37:49 +02:00
|
|
|
|
2024-04-12 01:50:03 +02:00
|
|
|
private final KeyBinding singleTapModifier = createSticky(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_tap");
|
|
|
|
private final KeyBinding doubleTapModifier = createSticky(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_double_tap");
|
|
|
|
private final KeyBinding tripleTapModifier = createSticky(InputUtil.UNKNOWN_KEY.getCode(), "ability_modifier_triple_tap");
|
2024-04-08 23:13:08 +02:00
|
|
|
|
2020-04-15 15:45:57 +02:00
|
|
|
private final Set<KeyBinding> pressed = new HashSet<>();
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
public KeyBindingsHandler() {
|
2020-09-25 13:06:41 +02:00
|
|
|
addKeybind(GLFW.GLFW_KEY_R, AbilitySlot.PRIMARY);
|
2020-10-02 21:44:33 +02:00
|
|
|
addKeybind(GLFW.GLFW_KEY_G, AbilitySlot.SECONDARY);
|
|
|
|
addKeybind(GLFW.GLFW_KEY_V, AbilitySlot.TERTIARY);
|
2020-04-25 15:37:17 +02:00
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
public Binding getBinding(AbilitySlot slot) {
|
2020-09-22 18:13:20 +02:00
|
|
|
return reverse.get(slot);
|
|
|
|
}
|
|
|
|
|
2024-04-08 23:13:08 +02:00
|
|
|
public boolean isToggleMode() {
|
|
|
|
return Unicopia.getConfig().toggleAbilityKeys.get();
|
|
|
|
}
|
|
|
|
|
2024-04-12 01:50:03 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
public void addKeybind(int code, AbilitySlot slot) {
|
2024-04-12 01:50:03 +02:00
|
|
|
Binding binding = new Binding(createSticky(code, slot.name().toLowerCase(Locale.ROOT)));
|
2020-09-22 18:13:20 +02:00
|
|
|
reverse.put(slot, binding);
|
|
|
|
keys.put(binding, slot);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
2024-04-12 01:50:03 +02:00
|
|
|
KeyBinding create(int code, String name) {
|
|
|
|
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));
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 15:37:17 +02:00
|
|
|
public void tick(MinecraftClient client) {
|
2020-01-16 12:35:46 +01:00
|
|
|
if (client.currentScreen != null
|
|
|
|
|| client.player == null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-15 18:12:00 +02:00
|
|
|
Pony iplayer = Pony.of(client.player);
|
2020-10-09 14:37:49 +02:00
|
|
|
AbilityDispatcher abilities = iplayer.getAbilities();
|
2023-06-02 21:20:30 +02:00
|
|
|
int maxPage = abilities.getMaxPage();
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2023-06-02 21:20:30 +02:00
|
|
|
int page = MathHelper.clamp(Unicopia.getConfig().hudPage.get(), 0, maxPage);
|
2020-10-09 14:37:49 +02:00
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
if (page > 0 && pageDown.getState() == PressedState.PRESSED) {
|
2020-10-09 14:37:49 +02:00
|
|
|
changePage(client, maxPage, -1);
|
2021-12-31 14:31:23 +01:00
|
|
|
} else if (page < maxPage && pageUp.getState() == PressedState.PRESSED) {
|
2020-10-09 14:37:49 +02:00
|
|
|
changePage(client, maxPage, 1);
|
2024-04-22 00:07:10 +02:00
|
|
|
} else if (!client.player.isSpectator()) {
|
2021-12-31 14:31:23 +01:00
|
|
|
for (Binding i : keys.keySet()) {
|
2020-10-09 14:37:49 +02:00
|
|
|
AbilitySlot slot = keys.get(i);
|
2022-03-26 20:34:15 +01:00
|
|
|
if (slot == AbilitySlot.PRIMARY && client.options.sneakKey.isPressed() && abilities.isFilled(AbilitySlot.PASSIVE)) {
|
2022-01-02 17:07:28 +01:00
|
|
|
slot = AbilitySlot.PASSIVE;
|
|
|
|
}
|
|
|
|
if (slot == AbilitySlot.PRIMARY && !abilities.isFilled(slot)) {
|
2020-10-09 14:37:49 +02:00
|
|
|
slot = AbilitySlot.PASSIVE;
|
|
|
|
}
|
2020-05-06 15:55:25 +02:00
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
PressedState state = i.getState();
|
|
|
|
|
2020-10-09 14:37:49 +02:00
|
|
|
if (state != PressedState.UNCHANGED) {
|
|
|
|
if (state == PressedState.PRESSED) {
|
2023-05-29 15:13:52 +02:00
|
|
|
abilities.activate(slot, page).map(a -> a.getName(iplayer)).ifPresent(UHud.INSTANCE::setMessage);
|
2020-10-09 14:37:49 +02:00
|
|
|
} else {
|
2021-12-31 14:31:23 +01:00
|
|
|
abilities.clear(slot, ActivationType.NONE, page);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ActivationType type = i.getType();
|
2024-04-12 01:50:03 +02:00
|
|
|
if (type.isResult()) {
|
2021-12-31 14:31:23 +01:00
|
|
|
abilities.clear(slot, type, page);
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 14:37:49 +02:00
|
|
|
|
|
|
|
private void changePage(MinecraftClient client, long max, int sigma) {
|
2022-12-27 20:40:24 +01:00
|
|
|
int page = Unicopia.getConfig().hudPage.get();
|
2020-10-09 14:37:49 +02:00
|
|
|
page += sigma;
|
2022-12-27 20:40:24 +01:00
|
|
|
Unicopia.getConfig().hudPage.set(page);
|
2024-04-08 23:13:08 +02:00
|
|
|
Unicopia.getConfig().save();
|
2023-08-22 17:25:36 +02:00
|
|
|
client.getSoundManager().play(PositionedSoundInstance.master(USounds.Vanilla.UI_BUTTON_CLICK, 1.75F + (0.25F * sigma)));
|
2022-06-25 00:19:55 +02:00
|
|
|
UHud.INSTANCE.setMessage(Text.translatable("gui.unicopia.page_num", page + 1, max + 1));
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
public class Binding {
|
|
|
|
private final KeyBinding binding;
|
|
|
|
|
|
|
|
private long nextPhaseTime;
|
|
|
|
|
2024-04-12 01:50:03 +02:00
|
|
|
private final AtomicReference<ActivationType> type = new AtomicReference<>(ActivationType.NONE);
|
2021-12-31 14:31:23 +01:00
|
|
|
|
|
|
|
Binding(KeyBinding binding) {
|
|
|
|
this.binding = binding;
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
public Text getLabel() {
|
|
|
|
return binding.getBoundKeyLocalizedText();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PressedState getState() {
|
|
|
|
PressedState state = getNewState();
|
|
|
|
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
|
|
|
|
if (state == PressedState.PRESSED) {
|
|
|
|
nextPhaseTime = now + 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == PressedState.RELEASED && now < nextPhaseTime + 10) {
|
|
|
|
nextPhaseTime = now + 200;
|
2024-04-12 01:50:03 +02:00
|
|
|
type.set(type.get().getNext());
|
2021-12-31 14:31:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActivationType getType() {
|
2024-04-12 01:50:03 +02:00
|
|
|
if (binding.isPressed()) {
|
|
|
|
ActivationType t = getForcedActivationType();
|
|
|
|
if (t.isResult()) {
|
2024-04-08 23:13:08 +02:00
|
|
|
KeyBinding.untoggleStickyKeys();
|
2024-04-12 01:50:03 +02:00
|
|
|
return t;
|
2024-04-08 23:13:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 01:50:03 +02:00
|
|
|
if (!isToggleMode() && System.currentTimeMillis() > nextPhaseTime - 70) {
|
|
|
|
return type.getAndSet(ActivationType.NONE);
|
2021-12-31 14:31:23 +01:00
|
|
|
}
|
2024-04-12 01:50:03 +02:00
|
|
|
|
2021-12-31 14:31:23 +01:00
|
|
|
return ActivationType.NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private PressedState getNewState() {
|
|
|
|
if (binding.isPressed()) {
|
|
|
|
return pressed.add(binding) ? PressedState.PRESSED : PressedState.UNCHANGED;
|
|
|
|
} else if (pressed.remove(binding)) {
|
|
|
|
return PressedState.RELEASED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PressedState.UNCHANGED;
|
|
|
|
}
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum PressedState {
|
|
|
|
UNCHANGED,
|
|
|
|
PRESSED,
|
2021-12-31 14:31:23 +01:00
|
|
|
RELEASED
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|