/// <summary> /// Gets the input normalized. /// </summary> /// <returns>The input normalized.</returns> /// <param name="sensitivity">Sensitivity.</param> /// <param name="dreadzone">Dreadzone.</param> /// <param name="gravity">Gravity.</param> internal float GetInputGenerated(IDevice device, float sensitivity, float dreadzone, float gravity) { if (_actionsList.Count > 1) { return(0); } //if key,mouse if (__currentInputAction.getCode(device) < InputCode.MAX_KEY_CODE) { return(GetGenericAnalogValue(device, sensitivity, dreadzone, gravity)); } else { if (InputEx.GetInputAnalog(__currentInputAction, device) < 0f) { return(-GetGenericAnalogValue(device, sensitivity, dreadzone, gravity)); } return(GetGenericAnalogValue(device, sensitivity, dreadzone, gravity)); } }
internal bool GetInputHold(IDevice device) { if (_actionsList.Count > 1 || __currentInputAction.type != InputActionType.SINGLE) /*Debug.LogWarning("You found need of GetInputHold with combos. Fork code on github");*/ { return(false); } else { return(InputEx.GetInputHold(__currentInputAction, device)); } }
/// <summary> /// Gets the interpolated analog value. /// </summary> /// <returns>The interpolated analog value.</returns> /// <param name="sensitivity">Sensitivity.</param> /// <param name="dreadzone">Dreadzone.</param> /// <param name="gravity">Gravity.</param> internal float GetGenericAnalogValue(IDevice device, float sensitivity, float dreadzone, float gravity) { if (InputEx.GetInputHold(__currentInputAction, device)) { _isActive = true; _timeDelta += Time.deltaTime * sensitivity; //timeDelta need to go from 0 to 1 (which mean from 0 to 100% of range difference) _analogValue = Mathf.Lerp(0, 1, Mathf.Clamp01(_timeDelta)); //Debug.Log("hold"+_analogValue); } else //on KeyUp reset _timeDelta { if (InputEx.GetInputUp(__currentInputAction, device)) { _isActive = false; _timeDelta = 0f; //reset // Debug.Log("UP g="+gravity); //if gravity is not set => drop _analogValue to 0 immidiately if (!(gravity > 0)) { _analogValue = 0; } return(_analogValue); } //effect of gravity if (_analogValue != 0) { _timeDelta += Time.deltaTime * gravity; _analogValue = Mathf.Lerp(_analogValue, 0, Mathf.Clamp01(_timeDelta)); // Debug.Log("gravity"); } } if (_analogValue < dreadzone) { _analogValue = 0f; } return(_analogValue); }
internal static InputAction GetAction(IDevice device) { bool isPlaying = Application.isPlaying; float time = isPlaying ? Time.time : Time.realtimeSinceStartup; //prioterize joysticks vs keys/mouse if (device != null) { if ((_code = device.GetInputCode()) != 0) { Debug.Log("Get Input Just from set device" + device.Index + " " + InputCode.toEnumString(_code) + " frame: " + Time.frameCount); return(processInputCode(_code, time, device)); } } else if (Devices.Count > 0) { foreach (IDevice deviceAvailable in Devices) { //test only // if(device.Index==3) if ((_code = deviceAvailable.GetInputCode()) != 0) { Debug.Log("Get Input Joy" + deviceAvailable.Index + " " + InputCode.toEnumString(_code) + " frame: " + Time.frameCount); return(processInputCode(_code, time, device)); } } } if (isPlaying) { _code = InputEx.GetKeyboardInput(); } else { _code = InputEx.GetGUIKeyboardInput(); } return(processInputCode(_code, time, device)); }
/// <summary> /// Gets the analog value. /// </summary> /// <returns> for analog input return values -1f to 1f or inverted depending of device</returns> internal float GetAnalogValue(IDevice device, float sensitivity, float dreadzone, float gravity) { //if key,mouse, joy button if (__currentInputAction.getCode(device) < InputCode.MAX_KEY_CODE || InputCode.toAxis(__currentInputAction.getCode(device)) == JoystickAxis.None) { if (InputEx.GetInputHold(__currentInputAction, device) || InputEx.GetInputDown(__currentInputAction, device)) { return(2f * (GetGenericAnalogValue(device, sensitivity, dreadzone, gravity) - 0.5f)); } else { return(0f); } } else { return(InputEx.GetInputAnalog(__currentInputAction, device)); } }
public static bool GetInputDigital(int code, ButtonState buttonState, IDevice Device) { if (code < InputCode.MAX_KEY_CODE) { if (buttonState == ButtonState.Down) { return(InputEx.IGetKeyDown((KeyCode)code)); } if (buttonState == ButtonState.Up) { return(InputEx.IGetKeyUp((KeyCode)code)); } if (buttonState == ButtonState.Hold) { return(InputEx.IGetKey((KeyCode)code)); } return(false); } else { lock (syncRoot) { if (Device != null) { return(Device.GetInputDigital(code, buttonState)); } else { foreach (IDevice device in Devices) { if (device.GetInputDigital(code, buttonState)) { return(true); } } return(false); } } } }
// private static int GetJoystickInput () // { // // // int numAxis = 8; // float axisValue = 0f; // string axisName; // KeyCode code; // // if (_frameCountPrev == Time.frameCount) // return 0; // // // _frameCountPrev = Time.frameCount; // // // // // _numJoysticks = Input.GetJoystickNames ().Length; // // // //check for joysticks clicks // for (int i=0; i<_numJoysticks; i++) { // // for (int k=0; k < 1; k++) { // axisName=i.ToString () + k.ToString (); // axisValue = Input.GetAxisRaw(axisName);// + 1f;//index-of joystick, k-ord number of axis // // Debug.Log("axisValue :"+axisValue); // //// if(axisValue>0) //// return InputCode.toCode((Joysticks)i,(JoystickAxis)k,JoystickPosition.Positive); //// else if(axisValue<0) //// return InputCode.toCode((Joysticks)i,(JoystickAxis)k,JoystickPosition.Negative); // } // // // //Let Unity handle JoystickButtons // for (int j=0; j<_numJoystickButtons; j++) { // // code = (KeyCode)Enum.Parse (typeof(KeyCode), "Joystick" + (i + 1) + "Button" + j); // // //Debug.Log("Joystick"+i+"Button"+j+" code:"+code); // // if (Input.GetKeyDown (code)) { // return InputCode.toCode ((Joysticks)i, JoystickAxis.None, j); // // // InputCode.toCode(code); // } // } // } // // // return 0; // // // } /// <summary> /// Gets the GUI keyboard input. /// first found key or mouse button pressed would be returned /// used for mapping states to current input /// </summary> /// <returns>The GUI keyboard input.</returns> private static int GetGUIKeyboardInput() { KeyCode keyCode; int numKeys = _keys.Length; if (_frameCountEditorPrev == _frameCountEditor) { return(0); } else { _frameCountEditorPrev = _frameCountEditor; } for (int i = 0; i < numKeys; i++) { //check for mouse clicks if (i < _numMouseButtons) { keyCode = (KeyCode)Enum.Parse(typeof(KeyCode), "Mouse" + i); if (InputEx.GetMouseButtonDown(keyCode)) { return((int)keyCode); } } keyCode = _keys [i]; // if (InputEx.GetKeyDown(keyCode)) { return((int)keyCode); } } return(0); }
internal bool GetInputDown(IDevice device, bool atOnce = false) { if (__currentInputAction.type == InputActionType.SINGLE && _actionsList.Count == 1) { return(InputEx.GetInputDown(__currentInputAction, device)); } else { if (atOnce) { int len = actions.Length; for (int i = 0; i < len; i++) { //LONG is counted as HOLD in this mode if (actions [i].type == InputActionType.LONG) { if (!InputEx.GetInputHold(actions [i], device)) { return(false); } } else if (!InputEx.GetInputDown(actions [i], device)) { return(false); } } return(true); } else // Double,Long are also count as combinations handled by InputEx.GetAction { return(GetCombinationInput(device)); } } }
/// <summary> /// Processes the input code into InputAction /// </summary> /// <returns>InputAction (single,double,long) or null.</returns> /// <param name="code">Code.</param> /// <param name="time">Time.</param> internal static InputAction processInputCode(int code, float time, IDevice device) { InputAction action = null; // Debug.Log ("process "+code); if (code != 0) //=KeyCode.None { if (_lastCode == 0) //=KeyCode.None //save key event and time needed for double check { _lastCode = code; _lastCodeTime = time; Debug.Log("Last code " + InputCode.toEnumString(_lastCode) + " at time:" + _lastCodeTime); // Debug.Log("Take time "+_lastCodeTime); } else { //if new pressed key is different then the last if (_lastCode != code) { //consturct string from lastCode action = new InputAction(_lastCode, InputActionType.SINGLE, device); //take new pressed code as lastCode _lastCode = code; Debug.Log("Single " + time + ":" + _lastCodeTime + " " + InputActionType.SINGLE); } else { if (time - _lastCodeTime < InputAction.DOUBLE_CLICK_SENSITIVITY) { action = new InputAction(_lastCode, InputActionType.DOUBLE, device); _lastCode = 0; //KeyCode.None; Debug.Log("Double " + time + ":" + _lastCodeTime + "<" + InputActionType.DOUBLE); } } } } else { if (_lastCode != 0) //=KeyCode.None //if key is still down and time longer then default long time click => display long click { if (InputEx.GetInputHold(_lastCode, device) || (!Application.isPlaying && InputEx.GetKeyDown(_lastCode))) { if (time - _lastCodeTime >= InputAction.LONG_CLICK_SENSITIVITY) { action = new InputAction(_lastCode, InputActionType.LONG, device); _lastCode = 0; //KeyCode.None; Debug.Log("Long " + (time - _lastCodeTime) + " <" + InputActionType.LONG); } } else //time wating for double click activity passed => display last code { if (time - _lastCodeTime >= InputAction.DOUBLE_CLICK_SENSITIVITY) { action = new InputAction(_lastCode, InputActionType.SINGLE, device); _lastCode = 0; //KeyCode.None; Debug.Log("Single after wating Double time pass " + (time - _lastCodeTime) + " " + InputActionType.SINGLE); } } } } return(action); }
/// <summary> /// Check if InputActin happened( SINGLE,DOUBLE,LONG) /// </summary> /// <returns>true/false</returns> /// <param name="action">InputAction to be compared with input</param> internal static bool GetAction(InputAction action, IDevice device) { if (action.type == InputActionType.SINGLE) { if (InputEx.GetInputDigital(action, ButtonState.Down, device)) { Debug.Log("Single <" + InputActionType.SINGLE); //action.startTime = Time.time; _lastCode = action.getCode(device); return(true); } return(false); } if (action.type == InputActionType.DOUBLE) { if (InputEx.GetInputDigital(action, ButtonState.Down, device)) { if (_lastCode != action.getCode(device)) //first click { _lastCode = action.getCode(device); action.startTime = Time.time; Debug.Log("First Click" + Time.time + ":" + action.startTime + " going for " + InputActionType.DOUBLE); return(false); } else //InputEx.LastCode==_pointer.Current.code //second click //check time diffrence if less then { if (Time.time - action.startTime < InputAction.DOUBLE_CLICK_SENSITIVITY) { _lastCode = 0; //??? action.startTime = 0; Debug.Log("Double " + Time.time + ":" + action.startTime + "<" + InputActionType.DOUBLE); return(true); } // Debug.Log("Lost Double " + Time.time + ":" + action.startTime + "<" + InputActionType.DOUBLE); } } return(false); } if (action.type == InputActionType.LONG) { if (InputEx.GetInputDigital(action, ButtonState.Hold, device)) //if hold { if (_lastCode != action.getCode(device)) { _lastCode = action.getCode(device); action.startTime = Time.time; return(false); } else //InputEx.LastCode==_pointer.Current.getCode(device) //hold //check time diffrence if less then { if (Time.time - action.startTime >= InputAction.LONG_CLICK_SENSITIVITY) { _lastCode = 0; //KeyCode.None; action.startTime = 0; Debug.Log("Long " + (Time.time - action.startTime) + " " + InputActionType.LONG); return(true); } } } return(false); } return(false); }
//TODO this with corutine to compare performace internal bool GetCombinationInput(IDevice device) { if (InputEx.GetAction(__currentInputAction, device)) //and// if code and type are ok go in // UnityEngine.Debug.Log ("CODE:" + _pointer.Current.codeString); //save time when action happened if not saved or reseted { if (__actionHappenTime == 0) { __actionHappenTime = Time.time; } //check if time from one action to the other is less then InputAction.COMBINATION_CLICK_SENSITIVITY if (Time.time < __actionHappenTime + InputAction.COMBINATION_CLICK_SENSITIVITY) { //get the time when current action of combination happened __actionHappenTime = Time.time; __currentIndex++; //just move to next if possible => combination happend or reset if couldn't if (!(__currentIndex < actions.Length)) { __currentIndex = 0; return(true); } } else //reset cos time has passed for next action { __currentIndex = 0; __actionHappenTime = 0; InputEx.LastCode = 0; // UnityEngine.Debug.Log ("Reset Time Cos Time Passed (Too late):" + Time.time + " Time Allowed:" + (__actionHappenTime + InputAction.COMBINATION_CLICK_SENSITIVITY)); } //UnityEngine.Debug.Log("CodeAfter:"+_pointer.Current.codeString); return(false); } //UnityEngine.Debug.Log("CodeAfter New Between Code or not same type:"+_pointer.Current.codeString); //combination stated but never continue in allowed time InputAction.COMBINATION_CLICK_SENSITIVITY if (__actionHappenTime > 0 && Time.time > __actionHappenTime + InputAction.COMBINATION_CLICK_SENSITIVITY) { //UnityEngine.Debug.Log ("Reset in Idle " + Time.time + " Time Allowed:" + (__actionHappenTime + InputAction.COMBINATION_CLICK_SENSITIVITY)); __currentIndex = 0; __actionHappenTime = 0; InputEx.LastCode = 0; return(false); } //TODO check current type and time waiting for type of // time passed while waiting for double/long action to happen => don't reset we aren't idle if (Time.time > __currentInputAction.startTime + InputAction.COMBINATION_CLICK_SENSITIVITY && InputEx.LastCode == __currentInputAction.getCode(device)) // or waiting for double/long action to happen => don't reset we aren't idle //UnityEngine.Debug.Log ("Reset in cos time waiting for double/long passed" + Time.time + " Time Allowed:" + (_pointer.Current.startTime + InputAction.COMBINATION_CLICK_SENSITIVITY)); { __currentIndex = 0; __actionHappenTime = 0; InputEx.LastCode = 0; return(false); } //key happend that isn't expected inbetween combination sequence if (InputEx.anyKeyDown && InputEx.LastCode != __currentInputAction.getCode(device) && __actionHappenTime > 0) { // UnityEngine.Debug.Log("Last Code:"+InputEx.LastCode+" current"+_pointer.Current.codeString); // UnityEngine.Debug.Log("Reset cos some other key is pressed" + InputEx.anyKeyDown + " Unity anykey:" + Input.anyKeyDown); __currentIndex = 0; __actionHappenTime = 0; InputEx.LastCode = 0; return(false); } return(false); }