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;
|
2020-04-25 15:37:17 +02:00
|
|
|
import java.util.Map;
|
2020-04-15 15:45:57 +02:00
|
|
|
import java.util.Set;
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
import org.lwjgl.glfw.GLFW;
|
|
|
|
|
2020-10-11 13:14:11 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.Ability;
|
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;
|
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;
|
2020-10-09 14:37:49 +02:00
|
|
|
import net.minecraft.client.sound.PositionedSoundInstance;
|
|
|
|
import net.minecraft.sound.SoundEvents;
|
2020-10-11 13:14:11 +02:00
|
|
|
import net.minecraft.text.TranslatableText;
|
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() {}
|
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
private final Map<KeyBinding, AbilitySlot> keys = new HashMap<>();
|
2020-09-22 18:13:20 +02:00
|
|
|
private final Map<AbilitySlot, KeyBinding> reverse = new HashMap<>();
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-10-09 14:37:49 +02:00
|
|
|
private final KeyBinding pageDown = register(GLFW.GLFW_KEY_PAGE_DOWN, "hud_page_dn");
|
|
|
|
private final KeyBinding pageUp = register(GLFW.GLFW_KEY_PAGE_UP, "hud_page_up");
|
|
|
|
|
|
|
|
public long page = 0;
|
|
|
|
|
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
|
|
|
|
2020-09-22 18:13:20 +02:00
|
|
|
public KeyBinding getBinding(AbilitySlot slot) {
|
|
|
|
return reverse.get(slot);
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:55:25 +02:00
|
|
|
public void addKeybind(int code, AbilitySlot slot) {
|
2020-10-09 14:37:49 +02:00
|
|
|
KeyBinding binding = register(code, slot.name().toLowerCase());
|
2020-09-22 18:13:20 +02:00
|
|
|
reverse.put(slot, binding);
|
|
|
|
keys.put(binding, slot);
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|
|
|
|
|
2020-10-09 14:37:49 +02:00
|
|
|
static KeyBinding register(int code, String name) {
|
|
|
|
return KeyBindingHelper.registerKeyBinding(new KeyBinding("key.unicopia." + name, code, KEY_CATEGORY));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
long maxPage = abilities.getMaxPage();
|
2020-01-16 12:35:46 +01:00
|
|
|
|
2020-10-09 14:37:49 +02:00
|
|
|
page = MathHelper.clamp(page, 0, maxPage);
|
|
|
|
|
|
|
|
if (page > 0 && checkPressed(pageDown) == PressedState.PRESSED) {
|
|
|
|
changePage(client, maxPage, -1);
|
|
|
|
} else if (page < maxPage && checkPressed(pageUp) == PressedState.PRESSED) {
|
|
|
|
changePage(client, maxPage, 1);
|
|
|
|
} else {
|
|
|
|
for (KeyBinding i : keys.keySet()) {
|
|
|
|
AbilitySlot slot = keys.get(i);
|
|
|
|
if (slot == AbilitySlot.PRIMARY && client.options.keySneak.isPressed()) {
|
|
|
|
slot = AbilitySlot.PASSIVE;
|
|
|
|
}
|
2020-05-06 15:55:25 +02:00
|
|
|
|
2020-10-09 14:37:49 +02:00
|
|
|
PressedState state = checkPressed(i);
|
|
|
|
if (state != PressedState.UNCHANGED) {
|
|
|
|
if (state == PressedState.PRESSED) {
|
2020-10-11 13:14:11 +02:00
|
|
|
abilities.activate(slot, page).map(Ability::getName).ifPresent(UHud.INSTANCE::setMessage);
|
2020-10-09 14:37:49 +02:00
|
|
|
} else {
|
|
|
|
abilities.clear(slot);
|
|
|
|
}
|
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) {
|
|
|
|
page += sigma;
|
|
|
|
client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.75F + (0.25F * sigma)));
|
2020-10-11 13:14:11 +02:00
|
|
|
UHud.INSTANCE.setMessage(new TranslatableText("gui.unicopia.page_num", page, max));
|
2020-10-09 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private PressedState checkPressed(KeyBinding binding) {
|
|
|
|
if (binding.isPressed()) {
|
|
|
|
return pressed.add(binding) ? PressedState.PRESSED : PressedState.UNCHANGED;
|
|
|
|
} else if (pressed.remove(binding)) {
|
|
|
|
return PressedState.UNPRESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PressedState.UNCHANGED;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum PressedState {
|
|
|
|
UNCHANGED,
|
|
|
|
PRESSED,
|
|
|
|
UNPRESSED
|
|
|
|
}
|
2020-01-16 12:35:46 +01:00
|
|
|
}
|