/*
         * Axes
         */

        public static AxisDelegate CreateAxisDelegate(SPInputId axis, Joystick joystick = Joystick.All, bool invert = false)
        {
            var inputId = SPInputDirect.GetInputName(axis, joystick);

            if (axis.IsAxis())
            {
                if (invert)
                {
                    return(() => - UnityEngine.Input.GetAxisRaw(inputId));
                }
                else
                {
                    return(() => UnityEngine.Input.GetAxisRaw(inputId));
                }
            }
            else
            {
                if (invert)
                {
                    return(() => UnityEngine.Input.GetButton(inputId) ? -1f : 0f);
                }
                else
                {
                    return(() => UnityEngine.Input.GetButton(inputId) ? 1f : 0f);
                }
            }
        }
        /*
         * Triggers
         */

        public static AxisDelegate CreateTriggerDelegate(SPInputId axis, Joystick joystick = Joystick.All, AxleValueConsideration axisConsideration = AxleValueConsideration.Positive)
        {
            var inputId = SPInputDirect.GetInputName(axis, joystick);

            if (axis.IsAxis())
            {
                switch (axisConsideration)
                {
                case AxleValueConsideration.Positive:
                    return(() => UnityEngine.Mathf.Clamp01(UnityEngine.Input.GetAxisRaw(inputId)));

                case AxleValueConsideration.Negative:
                    return(() => - UnityEngine.Mathf.Clamp(UnityEngine.Input.GetAxisRaw(inputId), -1f, 0f));

                case AxleValueConsideration.Absolute:
                    return(() => Math.Abs(UnityEngine.Input.GetAxisRaw(inputId)));

                default:
                    return(null);
                }
            }
            else
            {
                return(() => UnityEngine.Input.GetButton(inputId) ? 1f : 0f);
            }
        }
        public static ButtonDelegate CreateAxleButtonDelegate(SPInputId axis, AxleValueConsideration consideration, Joystick joystick = Joystick.All, float axleButtonDeadZone = InputUtil.DEFAULT_AXLEBTNDEADZONE)
        {
            var inputId = SPInputDirect.GetInputName(axis, joystick);

            if (axis.IsAxis())
            {
                switch (consideration)
                {
                case AxleValueConsideration.Positive:
                    return(() => UnityEngine.Input.GetAxisRaw(inputId) > axleButtonDeadZone);

                case AxleValueConsideration.Negative:
                    return(() => UnityEngine.Input.GetAxisRaw(inputId) < -axleButtonDeadZone);

                case AxleValueConsideration.Absolute:
                    return(() => Math.Abs(UnityEngine.Input.GetAxisRaw(inputId)) > axleButtonDeadZone);

                default:
                    return(null);
                }
            }
            else
            {
                return(() => UnityEngine.Input.GetButton(inputId));
            }
        }
        /*
         * Long Trigers
         */

        /// <summary>
        /// Some controller's triggers register -1 as inactive, and 1 as active. This creates a TriggerDelegate that normalizes this value to 0->1.
        /// </summary>
        /// <param name="axis"></param>
        /// <param name="joystick"></param>
        /// <returns></returns>
        public static AxisDelegate CreateLongTriggerDelegate(SPInputId axis, Joystick joystick = Joystick.All)
        {
            var inputId = SPInputDirect.GetInputName(axis, joystick);

            if (axis.IsAxis())
            {
                return(() => (UnityEngine.Input.GetAxisRaw(inputId) + 1f) / 2f);
            }
            else
            {
                return(() => UnityEngine.Input.GetButton(inputId) ? 1f : 0f);
            }
        }
        /*
         * Buttons
         */

        public static ButtonDelegate CreateButtonDelegate(SPInputId button, Joystick joystick = Joystick.All)
        {
            var inputId = SPInputDirect.GetInputName(button, joystick);

            if (button.IsButton())
            {
                return(() => UnityEngine.Input.GetButton(inputId));
            }
            else
            {
                return(() => UnityEngine.Input.GetAxisRaw(inputId) > InputUtil.DEFAULT_AXLEBTNDEADZONE);
            }
        }
示例#6
0
        private System.Collections.IEnumerator WorkRoutine(float delay)
        {
            yield return(WaitForDuration.Seconds(delay, SPTime.Real));

            while (_state == State.Running)
            {
                if (UnityEngine.Input.GetKeyDown(this.CancelKey))
                {
                    this.Cancel();
                    yield break;
                }
                if (this.CancelDelegate != null && this.CancelDelegate())
                {
                    this.Cancel();
                    yield break;
                }

                if (this.CustomPollingCallback != null)
                {
                    InputToken t;
                    if (this.CustomPollingCallback(this, out t))
                    {
                        this.InputResult = t;
                        goto Complete;
                    }
                }

                if (this.PollFromStandardSPInputs)
                {
                    if (this.PollButtons)
                    {
                        SPInputId btn;
                        if (SPInputDirect.TryPollButton(out btn, this.ButtonPollingState, this.Joystick))
                        {
                            this.InputResult = InputToken.CreateButton(btn);
                            goto Complete;
                        }
                    }

                    if (this.PollJoyAxes || this.PollMouseAxes)
                    {
                        SPInputId axis;
                        float     value;
                        if (SPInputDirect.TryPollAxis(out axis, out value, this.Joystick, this.PollMouseAxes, this.AxisPollingDeadZone) && TestConsideration(value, this.AxisConsideration, this.AxisPollingDeadZone))
                        {
                            if ((this.PollJoyAxes && axis.IsJoyAxis()) || (this.PollMouseAxes && axis.IsMouseAxis()))
                            {
                                this.InputResult = InputToken.CreateAxleButton(axis, this.AxisConsideration, this.AxisPollingDeadZone);
                                goto Complete;
                            }
                        }
                    }
                }

                if (this.PollKeyboard)
                {
                    UnityEngine.KeyCode key;
                    if (SPInputDirect.TryPollKey(out key, this.ButtonPollingState))
                    {
                        this.InputResult = InputToken.CreateButton(key);
                        goto Complete;
                    }
                }

                yield return(null);
            }

Complete:
            _state   = State.Complete;
            _routine = null;
            this.SignalOnComplete();
        }
        private System.Collections.IEnumerator WorkRoutine(float delay)
        {
            yield return(WaitForDuration.Seconds(delay, SPTime.Real));

            SPInputId positiveBtn = SPInputId.Unknown;

            UnityEngine.KeyCode positiveKey = UnityEngine.KeyCode.None;
            float t = float.NegativeInfinity;

            while (_state == State.Running)
            {
                if (UnityEngine.Input.GetKeyDown(this.CancelKey))
                {
                    this.Cancel();
                    yield break;
                }
                if (this.CancelDelegate != null && this.CancelDelegate())
                {
                    this.Cancel();
                    yield break;
                }

                if (this.CustomPollingCallback != null)
                {
                    InputToken token;
                    if (this.CustomPollingCallback(this, out token))
                    {
                        this.InputResult = token;
                        goto Complete;
                    }
                }

                if (this.PollFromStandardSPInputs)
                {
                    if (this.PollJoyAxes || this.PollMouseAxes)
                    {
                        SPInputId axis;
                        float     value;
                        if (SPInputDirect.TryPollAxis(out axis, out value, this.Joystick, this.PollMouseAxes, this.AxisPollingDeadZone) && TestConsideration(value, this.AxisConsideration, this.AxisPollingDeadZone))
                        {
                            if ((this.PollJoyAxes && axis.IsJoyAxis()) || (this.PollMouseAxes && axis.IsMouseAxis()))
                            {
                                if (this.PollAsTrigger)
                                {
                                    this.InputResult = InputToken.CreateTrigger(axis);
                                }
                                else
                                {
                                    this.InputResult = InputToken.CreateAxis(axis, value < 0f);
                                }
                                goto Complete;
                            }
                        }
                    }

                    if (this.PollButtons)
                    {
                        SPInputId btn;
                        if (SPInputDirect.TryPollButton(out btn, this.ButtonPollingState, this.Joystick))
                        {
                            if (this.PollAsTrigger)
                            {
                                this.InputResult = InputToken.CreateTrigger(btn, AxleValueConsideration.Positive);
                                goto Complete;
                            }
                            if (positiveBtn != SPInputId.Unknown)
                            {
                                this.InputResult = InputToken.CreateEmulatedAxis(positiveBtn, btn);
                                goto Complete;
                            }
                            else
                            {
                                positiveKey = UnityEngine.KeyCode.None;
                                positiveBtn = btn;
                                t           = UnityEngine.Time.realtimeSinceStartup;
                                goto Restart;
                            }
                        }
                    }
                }

                if (this.PollKeyboard)
                {
                    UnityEngine.KeyCode key;
                    if (SPInputDirect.TryPollKey(out key, this.ButtonPollingState))
                    {
                        if (this.PollAsTrigger)
                        {
                            this.InputResult = InputToken.CreateTrigger(key);
                            goto Complete;
                        }
                        if (positiveKey != UnityEngine.KeyCode.None)
                        {
                            this.InputResult = InputToken.CreateEmulatedAxis(positiveKey, key);
                            goto Complete;
                        }
                        else
                        {
                            positiveBtn = SPInputId.Unknown;
                            positiveKey = key;
                            t           = UnityEngine.Time.realtimeSinceStartup;
                            goto Restart;
                        }
                    }
                }

Restart:
                yield return(null);

                if (UnityEngine.Time.realtimeSinceStartup - t > this.ButtonPressMonitorDuration)
                {
                    positiveBtn = SPInputId.Unknown;
                    positiveKey = UnityEngine.KeyCode.None;
                }
            }

Complete:
            _state   = State.Complete;
            _routine = null;
            this.SignalOnComplete();
        }
        public bool PollButton(ButtonState state = ButtonState.Down, Joystick joystick = Joystick.All)
        {
            switch (this.Type)
            {
            case InputType.Unknown:
                return(false);

            case InputType.Joystick:
            {
                switch (this.Mode)
                {
                case InputMode.Axis:
                {
                    var d = SPInputFactory.CreateAxleButtonDelegate((SPInputId)this.Value, AxleValueConsideration.Absolute, joystick);
                    return(d != null?d() : false);
                }

                case InputMode.Trigger:
                {
                    var d = SPInputFactory.CreateAxleButtonDelegate((SPInputId)this.Value, (AxleValueConsideration)this.AltValue, joystick);
                    return(d != null?d() : false);
                }

                case InputMode.LongTrigger:
                {
                    var d = SPInputFactory.CreateAxleButtonDelegate(SPInputFactory.CreateLongTriggerDelegate((SPInputId)this.Value, joystick), AxleValueConsideration.Positive);
                    return(d != null?d() : false);
                }

                case InputMode.Button:
                    return(SPInputDirect.GetButton((SPInputId)this.Value, state, joystick));

                case InputMode.AxleButton:
                {
                    var d = SPInputFactory.CreateAxleButtonDelegate((SPInputId)this.Value, (AxleValueConsideration)this.AltValue, joystick, this.DeadZone);
                    return(d != null?d() : false);
                }
                }
            }
            break;

            case InputType.Keyboard:
            {
                switch (this.Mode)
                {
                case InputMode.Axis:
                {
                    KeyCode p = (KeyCode)Value;
                    KeyCode n = (KeyCode)AltValue;
                    //return () => Input.GetKey(p) || Input.GetKey(n);
                    return(SPInputDirect.GetKey(p, state) || SPInputDirect.GetKey(n, state));
                }

                case InputMode.Trigger:
                case InputMode.LongTrigger:
                case InputMode.Button:
                case InputMode.AxleButton:
                    return(SPInputDirect.GetKey((KeyCode)this.Value, state));
                }
            }
            break;

            case InputType.Custom:
            {
                if (_buttonDelegate is ButtonDelegateFactory)
                {
                    var d = (_buttonDelegate as ButtonDelegateFactory)(joystick);
                    return(d != null?d() : false);
                }
                else if (_buttonDelegate is ButtonDelegate)
                {
                    var d = _buttonDelegate as ButtonDelegate;
                    return(d != null?d() : false);
                }
                else if (_axisDelegate is AxisDelegateFactory)
                {
                    var d = SPInputFactory.CreateAxleButtonDelegate((_axisDelegate as AxisDelegateFactory)(joystick), AxleValueConsideration.Absolute);
                    return(d != null?d() : false);
                }
                else if (_axisDelegate is AxisDelegate)
                {
                    var d = SPInputFactory.CreateAxleButtonDelegate(_axisDelegate as AxisDelegate, AxleValueConsideration.Absolute);
                    return(d != null?d() : false);
                }
                else
                {
                    return(false);
                }
            }
            }
            return(false);
        }
        public float PollAxis(Joystick joystick = Joystick.All)
        {
            switch (this.Type)
            {
            case InputType.Unknown:
                return(0f);

            case InputType.Joystick:
            {
                switch (this.Mode)
                {
                case InputMode.Axis:
                {
                    if (float.IsNaN(this.DeadZone))
                    {
                        var d = SPInputFactory.CreateAxisDelegate((SPInputId)this.Value, (SPInputId)this.AltValue, joystick);
                        return(d != null?d() : 0f);
                    }
                    else
                    {
                        //return SPInputFactory.CreateAxisDelegate((SPInputId)this.Value, joystick, this.AltValue != 0);
                        float v = SPInputDirect.GetAxis((SPInputId)this.Value, joystick);
                        if (this.AltValue != 0)
                        {
                            v = -v;
                        }
                        return(v);
                    }
                }

                case InputMode.Trigger:
                {
                    var d = SPInputFactory.CreateTriggerDelegate((SPInputId)this.Value, joystick, (AxleValueConsideration)this.AltValue);
                    return(d != null?d() : 0f);
                }

                case InputMode.LongTrigger:
                {
                    var d = SPInputFactory.CreateLongTriggerDelegate((SPInputId)this.Value, joystick);
                    return(d != null?d() : 0f);
                }

                case InputMode.Button:
                    //return SPInputFactory.CreateTriggerDelegate(SPInputFactory.CreateButtonDelegate((SPInputId)this.Value, joystick));
                    return(SPInputDirect.GetButton((SPInputId)this.Value, joystick) ? 1f : 0f);

                case InputMode.AxleButton:
                {
                    var d = SPInputFactory.CreateTriggerDelegate(SPInputFactory.CreateAxleButtonDelegate((SPInputId)this.Value, (AxleValueConsideration)this.AltValue, joystick, this.DeadZone));
                    return(d != null?d() : 0f);
                }
                }
            }
            break;

            case InputType.Keyboard:
            {
                switch (this.Mode)
                {
                case InputMode.Axis:
                {
                    //return SPInputFactory.CreateAxisDelegate((KeyCode)this.Value, (KeyCode)this.AltValue);
                    if (Input.GetKey((KeyCode)this.Value))
                    {
                        return(1f);
                    }
                    else if (Input.GetKey((KeyCode)this.AltValue))
                    {
                        return(-1f);
                    }
                    else
                    {
                        return(0f);
                    }
                }

                case InputMode.Trigger:
                case InputMode.LongTrigger:
                case InputMode.Button:
                case InputMode.AxleButton:
                    //return SPInputFactory.CreateTriggerDelegate((KeyCode)this.Value);
                    return(Input.GetKey((KeyCode)this.Value) ? 1f : 0f);
                }
            }
            break;

            case InputType.Custom:
            {
                if (_axisDelegate is AxisDelegateFactory)
                {
                    var d = (_axisDelegate as AxisDelegateFactory)(joystick);
                    return(d != null?d() : 0f);
                }
                else if (_axisDelegate is AxisDelegate)
                {
                    var d = _axisDelegate as AxisDelegate;
                    return(d != null?d() : 0f);
                }
                else if (_buttonDelegate is ButtonDelegateFactory)
                {
                    var d = SPInputFactory.CreateTriggerDelegate((_buttonDelegate as ButtonDelegateFactory)(joystick));
                    return(d != null?d() : 0f);
                }
                else if (_buttonDelegate is ButtonDelegate)
                {
                    var d = SPInputFactory.CreateTriggerDelegate(_buttonDelegate as ButtonDelegate);
                    return(d != null?d() : 0f);
                }
                else
                {
                    return(0f);
                }
            }
            }
            return(0f);
        }