123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package me.hammerle.snuviengine.api;
- import java.util.HashMap;
- public final class Keys {
- private final HashMap<Integer, Key> bindings = new HashMap<>();
- private Key rebind = null;
- protected Keys() {
- }
- public Key register(int key) {
- if(bindings.containsKey(key)) {
- return null;
- }
- Key binding = new Key(key);
- bindings.put(key, binding);
- return binding;
- }
- public void rebindOnNextKeyPress(Key binding) {
- rebind = binding;
- if(binding != null) {
- binding.setRebinding(true);
- }
- }
- public void rebind(Key binding, int key) {
- if(bindings.get(binding.getKey()) == binding) {
- bindings.remove(binding.getKey());
- }
- binding.setKey(key);
- bindings.put(key, binding);
- }
- protected void press(int key) {
- if(isRebinding()) {
- rebindMarkedBinding(key);
- return;
- }
- Key binding = bindings.get(key);
- if(binding != null) {
- binding.press();
- }
- }
- private boolean isRebinding() {
- return rebind != null;
- }
- private void rebindMarkedBinding(int key) {
- rebind.setRebinding(false);
- if(!bindings.containsKey(key)) {
- rebind(rebind, key);
- }
- rebind = null;
- }
- protected void release(int key) {
- Key binding = bindings.get(key);
- if(binding != null) {
- binding.release();
- }
- }
- protected void tick() {
- bindings.values().forEach(binding -> binding.tick());
- }
- }
|