Ejemplo n.º 1
0
    public static void RemoveButtonDelegate(string inputName, float axisAsButtonThreshold, ButtonDelegate buttonDelegate)
    {
        if (isApplicationQuitting || (instance == null))
        {
            return;
        }

        ButtonDelegateKey key = new ButtonDelegateKey(inputName, axisAsButtonThreshold);

        if (dictButtonDelegates.ContainsKey(key))
        {
            ButtonDelegateEntry dictEntry = dictButtonDelegates[key];
            dictEntry.buttonDelegate -= buttonDelegate;

            if (dictEntry.buttonDelegate == null)
            {
                dictButtonDelegates.Remove(key);
            }
        }
    }
Ejemplo n.º 2
0
    public static void AddButtonDelegate(string inputName, float axisAsButtonThreshold, ButtonDelegate buttonDelegate)
    {
        if (isApplicationQuitting)
        {
            return;
        }

        if (instance == null)
        {
            InstantiateInstance();
        }

        ButtonDelegateKey key = new ButtonDelegateKey(inputName, axisAsButtonThreshold);

        if (!dictButtonDelegates.ContainsKey(key))
        {
            ButtonDelegateEntry entry = new ButtonDelegateEntry(buttonDelegate);
            dictButtonDelegates.Add(key, entry);
        }
        else
        {
            dictButtonDelegates[key].buttonDelegate += buttonDelegate;
        }
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        if (!isApplicationQuitting && this.isInstance)
        {
            //Iterate through the dictionaries and only check the inputs that have delegates assigned to them.

            Dictionary <string, AxisDelegate> .KeyCollection axisKeys = dictAxisDelegates.Keys;
            if (axisKeys.Count > 0)
            {
                foreach (string key in axisKeys)
                {
                    dictAxisDelegates[key](Input.GetAxis(key));
                }
            }

            Dictionary <Axis2DDelegateKey, Axis2DDelegate> .KeyCollection axis2DKeys = dictAxis2DDelegates.Keys;
            if (axis2DKeys.Count > 0)
            {
                foreach (Axis2DDelegateKey key in axis2DKeys)
                {
                    dictAxis2DDelegates[key](Input.GetAxis(key.inputHorizontal), Input.GetAxis(key.inputVertical));
                }
            }

            Dictionary <ButtonDelegateKey, ButtonDelegateEntry> .KeyCollection buttonKeys = dictButtonDelegates.Keys;
            if (buttonKeys.Count > 0)
            {
                foreach (ButtonDelegateKey key in buttonKeys)
                {
                    ButtonDelegateEntry entry = dictButtonDelegates[key];

                    float value = Input.GetAxis(key.inputName);
                    bool  state = false;

                    //If we have a negative threshold for a button, then we want to measure the axis going in the other direction.
                    if (key.axisAsButtonThreshold >= 0)
                    {
                        state = value >= key.axisAsButtonThreshold;
                    }
                    else
                    {
                        state = value <= key.axisAsButtonThreshold;
                    }

                    //Track the Pressed/Released states ourselves since we're detecting axis thresholds.
                    // Not sure if I should be sending Held and NotHeld events at the same time as I send the Pressed and Released
                    // events (respectively), but it's easier for people to use that way.
                    bool pressed = false, held = false, released = false, notHeld = false;
                    if (state && !entry.lastState)
                    {
                        pressed = held = true;
                    }
                    else if (state && entry.lastState)
                    {
                        held = true;
                    }
                    else if (!state && entry.lastState)
                    {
                        released = notHeld = true;
                    }
                    else if (!state && !entry.lastState)
                    {
                        notHeld = true;
                    }

                    entry.buttonDelegate(new ButtonState(pressed, held, released, notHeld));

                    entry.lastState = state;
                }
            }
        }
        // End Update
    }