KeyHandler.java 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package me.hammerle.snuviengine.api;
  2. import java.util.HashMap;
  3. public final class KeyHandler
  4. {
  5. private final static HashMap<Integer, KeyBinding> BINDINGS = new HashMap<>();
  6. public static KeyBinding register(int key) throws KeyDuplicateException
  7. {
  8. KeyBinding binding = new KeyBinding();
  9. if(BINDINGS.putIfAbsent(key, binding) != null)
  10. {
  11. throw new KeyDuplicateException("the key '" + key + "' has already been registered");
  12. }
  13. return binding;
  14. }
  15. protected static void onKeyDownEvent(int key)
  16. {
  17. KeyBinding binding = BINDINGS.get(key);
  18. if(binding != null)
  19. {
  20. binding.onKeyDownEvent();
  21. }
  22. }
  23. protected static void onKeyUpEvent(int key)
  24. {
  25. KeyBinding binding = BINDINGS.get(key);
  26. if(binding != null)
  27. {
  28. binding.onKeyUpEvent();
  29. }
  30. }
  31. protected static void tick()
  32. {
  33. BINDINGS.values().forEach(binding -> binding.tick());
  34. }
  35. }