protected virtual void Handle(nvEvent.UnregisterInputType gameEvent) { InputType.KeyState onKeyState = InputType.KeyState.None; if (gameEvent.onPress) { onKeyState |= InputType.KeyState.OnPress; } if (gameEvent.onHold) { onKeyState |= InputType.KeyState.OnHold; } if (gameEvent.onRelease) { onKeyState |= InputType.KeyState.OnRelease; } if (onKeyState == InputType.KeyState.None) { return; } if (gameEvent.gameAction) { Unregister(gameInputActions, gameEvent.key, gameEvent.action, onKeyState); } if (gameEvent.systemAction) { Unregister(systemInputActions, gameEvent.key, gameEvent.action, onKeyState); } if (gameEvent.debugAction) { Unregister(debugInputActions, gameEvent.key, gameEvent.action, onKeyState); } }
protected virtual void Handle(nvEvent.RegisterInputType gameEvent) { InputType.KeyState onKeyState = InputType.KeyState.None; if (gameEvent.onPress) { onKeyState |= InputType.KeyState.OnPress; } if (gameEvent.onHold) { onKeyState |= InputType.KeyState.OnHold; } if (gameEvent.onRelease) { onKeyState |= InputType.KeyState.OnRelease; } if (onKeyState == InputType.KeyState.None) { return; } InputType.TouchableObject touchable = null; if (gameEvent.touchableObject != null) { touchable = new InputType.TouchableObject(gameEvent.touchableObject, gameEvent.touchableObjectCamera); } if (gameEvent.gameAction) { Register(gameInputActions, gameEvent.key, gameEvent.action, onKeyState, touchable); } if (gameEvent.systemAction) { Register(systemInputActions, gameEvent.key, gameEvent.action, onKeyState, touchable); } if (gameEvent.debugAction) { Register(debugInputActions, gameEvent.key, gameEvent.action, onKeyState, touchable); } }
/// <summary> /// Register a key to a callback. Example: /// GameInput.Register( KeyCode.Mouse0, TryPick ); /// </summary> public virtual void Register(List <InputType> inputActions, KeyCode key, UnityAction action, InputType.KeyState keystate = InputType.KeyState.OnPress, InputType.TouchableObject optionalTouchable = null) { //unregister first to prevent double-adding of event handlers Unregister(inputActions, key, action, keystate); for (int i = 0; i < inputActions.Count; ++i) { if (inputActions[i].key == key) { foreach (var pair in inputActions[i].keyEvents) { if ((pair.onKeyState & keystate) != InputType.KeyState.None) { pair.events.RemoveListener(action); pair.events.AddListener(action); return; } } //if we reach this point, the pair doesn't exist InputType.KeyEvent newPair = new InputType.KeyEvent(); newPair.onKeyState = keystate; newPair.events.AddListener(action); inputActions[i].keyEvents.Add(newPair); break; } } //if we reach this point, the action doesn't exist InputType newAction = new InputType(); InputType.KeyEvent newPairForNewAction = new InputType.KeyEvent(); newPairForNewAction.onKeyState = keystate; newPairForNewAction.events = new UnityEvent(); newPairForNewAction.events.AddListener(action); newAction.keyEvents.Add(newPairForNewAction); newAction.key = key; newAction.optionalTouchable = optionalTouchable; inputActions.Add(newAction); }
/// <summary> /// Removes a key from the list of callbacks. Example: /// GameInput.Unregister( KeyCode.Mouse0, TryPick ); /// </summary> public virtual void Unregister(List <InputType> inputActions, KeyCode key, UnityAction action, InputType.KeyState keystate = InputType.KeyState.OnPress) { for (int i = 0; i < inputActions.Count; ++i) { if (inputActions[i].key == key) { for (int j = 0; j < inputActions[i].keyEvents.Count; ++j) { if ((inputActions[i].keyEvents[j].onKeyState & keystate) != InputType.KeyState.None) { inputActions[i].keyEvents[j].events.RemoveListener(action); //normally this would be bad, but we're exiting the list iteration right after removing the element(s) if (inputActions[i].keyEvents[j].events.GetPersistentEventCount() <= 0) { inputActions[i].keyEvents.RemoveAt(j); } if (inputActions[i].keyEvents.Count <= 0) { inputActions.RemoveAt(i); } return; } } //matching keyEvents not found return; } } }