Allow registration of raw key handler
This commit is contained in:
@@ -9,6 +9,7 @@ import net.minecraft.util.ReportedException;
|
||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.gameevent.InputEvent;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -19,6 +20,7 @@ import java.util.concurrent.Callable;
|
||||
public class KeyBindingRegistry {
|
||||
private Map<String, KeyBinding> keyBindings = new HashMap<String, KeyBinding>();
|
||||
private Multimap<KeyBinding, Runnable> keyBindingHandlers = ArrayListMultimap.create();
|
||||
private Multimap<Integer, Runnable> rawHandlers = ArrayListMultimap.create();
|
||||
|
||||
public void registerKeyBinding(String name, int keyCode, Runnable whenPressed) {
|
||||
KeyBinding keyBinding = keyBindings.get(name);
|
||||
@@ -30,12 +32,21 @@ public class KeyBindingRegistry {
|
||||
keyBindingHandlers.put(keyBinding, whenPressed);
|
||||
}
|
||||
|
||||
public void registerRaw(int keyCode, Runnable whenPressed) {
|
||||
rawHandlers.put(keyCode, whenPressed);
|
||||
}
|
||||
|
||||
public Map<String, KeyBinding> getKeyBindings() {
|
||||
return Collections.unmodifiableMap(keyBindings);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onKeyInput(InputEvent.KeyInputEvent event) {
|
||||
handleKeyBindings();
|
||||
handleRaw();
|
||||
}
|
||||
|
||||
public void handleKeyBindings() {
|
||||
for (Map.Entry<KeyBinding, Collection<Runnable>> entry : keyBindingHandlers.asMap().entrySet()) {
|
||||
while (entry.getKey().isPressed()) {
|
||||
for (final Runnable runnable : entry.getValue()) {
|
||||
@@ -57,4 +68,24 @@ public class KeyBindingRegistry {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleRaw() {
|
||||
int keyCode = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter() + 256 : Keyboard.getEventKey();
|
||||
for (final Runnable runnable : rawHandlers.get(keyCode)) {
|
||||
try {
|
||||
runnable.run();
|
||||
} catch (Throwable cause) {
|
||||
CrashReport crashReport = CrashReport.makeCrashReport(cause, "Handling Raw Key Binding");
|
||||
CrashReportCategory category = crashReport.makeCategory("Key Binding");
|
||||
category.addCrashSection("Key Code", keyCode);
|
||||
category.addCrashSectionCallable("Handler", new Callable() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
return runnable;
|
||||
}
|
||||
});
|
||||
throw new ReportedException(crashReport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.replaymod.extras;
|
||||
|
||||
import com.replaymod.core.KeyBindingRegistry;
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import com.replaymod.replay.events.ReplayOpenEvent;
|
||||
import com.replaymod.replay.gui.overlay.GuiReplayOverlay;
|
||||
@@ -66,7 +67,8 @@ public class HotkeyButtons implements Extra {
|
||||
}
|
||||
}.setLayout(panelLayout = new GridLayout().setSpacingX(5).setSpacingY(5).setColumns(1));
|
||||
|
||||
for (final KeyBinding keyBinding : mod.getKeyBindingRegistry().getKeyBindings().values()) {
|
||||
final KeyBindingRegistry keyBindingRegistry = mod.getKeyBindingRegistry();
|
||||
for (final KeyBinding keyBinding : keyBindingRegistry.getKeyBindings().values()) {
|
||||
String keyName = "???";
|
||||
try {
|
||||
keyName = Keyboard.getKeyName(keyBinding.getKeyCode());
|
||||
@@ -79,7 +81,7 @@ public class HotkeyButtons implements Extra {
|
||||
@Override
|
||||
public void run() {
|
||||
keyBinding.pressTime++;
|
||||
FMLCommonHandler.instance().fireKeyInput();
|
||||
keyBindingRegistry.handleKeyBindings();
|
||||
}
|
||||
}),
|
||||
new GuiLabel().setI18nText(keyBinding.getKeyDescription())
|
||||
|
||||
Reference in New Issue
Block a user