Add a way to register key bindings triggered every render tick (fixes #31)

This commit is contained in:
johni0702
2016-11-24 21:31:21 +01:00
parent a817c7367a
commit 50b53fc6a3
2 changed files with 41 additions and 18 deletions

View File

@@ -9,6 +9,7 @@ import net.minecraft.util.ReportedException;
import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent; import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import java.util.Collection; import java.util.Collection;
@@ -20,16 +21,25 @@ import java.util.concurrent.Callable;
public class KeyBindingRegistry { public class KeyBindingRegistry {
private Map<String, KeyBinding> keyBindings = new HashMap<String, KeyBinding>(); private Map<String, KeyBinding> keyBindings = new HashMap<String, KeyBinding>();
private Multimap<KeyBinding, Runnable> keyBindingHandlers = ArrayListMultimap.create(); private Multimap<KeyBinding, Runnable> keyBindingHandlers = ArrayListMultimap.create();
private Multimap<KeyBinding, Runnable> repeatedKeyBindingHandlers = ArrayListMultimap.create();
private Multimap<Integer, Runnable> rawHandlers = ArrayListMultimap.create(); private Multimap<Integer, Runnable> rawHandlers = ArrayListMultimap.create();
public void registerKeyBinding(String name, int keyCode, Runnable whenPressed) { public void registerKeyBinding(String name, int keyCode, Runnable whenPressed) {
keyBindingHandlers.put(registerKeyBinding(name, keyCode), whenPressed);
}
public void registerRepeatedKeyBinding(String name, int keyCode, Runnable whenPressed) {
repeatedKeyBindingHandlers.put(registerKeyBinding(name, keyCode), whenPressed);
}
private KeyBinding registerKeyBinding(String name, int keyCode) {
KeyBinding keyBinding = keyBindings.get(name); KeyBinding keyBinding = keyBindings.get(name);
if (keyBinding == null) { if (keyBinding == null) {
keyBinding = new KeyBinding(name, keyCode, "replaymod.title"); keyBinding = new KeyBinding(name, keyCode, "replaymod.title");
keyBindings.put(name, keyBinding); keyBindings.put(name, keyBinding);
ClientRegistry.registerKeyBinding(keyBinding); ClientRegistry.registerKeyBinding(keyBinding);
} }
keyBindingHandlers.put(keyBinding, whenPressed); return keyBinding;
} }
public void registerRaw(int keyCode, Runnable whenPressed) { public void registerRaw(int keyCode, Runnable whenPressed) {
@@ -46,28 +56,41 @@ public class KeyBindingRegistry {
handleRaw(); handleRaw();
} }
@SubscribeEvent
public void onTick(TickEvent.RenderTickEvent event) {
if (event.phase != TickEvent.Phase.START) return;
handleRepeatedKeyBindings();
}
public void handleRepeatedKeyBindings() {
for (Map.Entry<KeyBinding, Collection<Runnable>> entry : repeatedKeyBindingHandlers.asMap().entrySet()) {
if (entry.getKey().isKeyDown()) {
invokeKeyBindingHandlers(entry.getKey(), entry.getValue());
}
}
}
public void handleKeyBindings() { public void handleKeyBindings() {
for (Map.Entry<KeyBinding, Collection<Runnable>> entry : keyBindingHandlers.asMap().entrySet()) { for (Map.Entry<KeyBinding, Collection<Runnable>> entry : keyBindingHandlers.asMap().entrySet()) {
while (entry.getKey().isPressed()) { while (entry.getKey().isPressed()) {
for (final Runnable runnable : entry.getValue()) { invokeKeyBindingHandlers(entry.getKey(), entry.getValue());
}
}
}
private void invokeKeyBindingHandlers(KeyBinding keyBinding, Collection<Runnable> handlers) {
for (final Runnable runnable : handlers) {
try { try {
runnable.run(); runnable.run();
} catch (Throwable cause) { } catch (Throwable cause) {
CrashReport crashReport = CrashReport.makeCrashReport(cause, "Handling Key Binding"); CrashReport crashReport = CrashReport.makeCrashReport(cause, "Handling Key Binding");
CrashReportCategory category = crashReport.makeCategory("Key Binding"); CrashReportCategory category = crashReport.makeCategory("Key Binding");
category.addCrashSection("Key Binding", entry.getKey()); category.addCrashSection("Key Binding", keyBinding);
category.addCrashSectionCallable("Handler", new Callable() { category.addCrashSectionCallable("Handler", runnable::toString);
@Override
public Object call() throws Exception {
return runnable;
}
});
throw new ReportedException(crashReport); throw new ReportedException(crashReport);
} }
} }
} }
}
}
public void handleRaw() { public void handleRaw() {
int keyCode = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter() + 256 : Keyboard.getEventKey(); int keyCode = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter() + 256 : Keyboard.getEventKey();

View File

@@ -431,7 +431,7 @@ public class GuiPathing {
}); });
}); });
core.getKeyBindingRegistry().registerKeyBinding("replaymod.input.synctimeline", Keyboard.KEY_V, () -> { core.getKeyBindingRegistry().registerRepeatedKeyBinding("replaymod.input.synctimeline", Keyboard.KEY_V, () -> {
// Current replay time // Current replay time
int time = replayHandler.getReplaySender().currentTimeStamp(); int time = replayHandler.getReplaySender().currentTimeStamp();
// Position of the cursor // Position of the cursor