public ControllerState ApplyMap(IController sourceController)
        {
            var state = sourceController.GetState();
            var ret   = new ControllerState();

            foreach (var item in ButtonMaps)
            {
                if (state.Buttons.Count > (int)item.Source)
                {
                    if (item.Target != Button.Unmapped)
                    {
                        ret.Buttons.EnsureSize((int)(item.Target + 1));
                        ret.Buttons[(int)item.Target] = state.Buttons[(int)item.Source];
                    }
                }
            }

            foreach (var item in AxisMaps)
            {
                if (state.Axes.Count > (int)item.Source)
                {
                    if (item.Target != Axis.Unmapped)
                    {
                        ret.Axes.EnsureSize((int)(item.Target + 1));
                        double axisVal = state.Axes[(int)item.Source];
                        if (sourceController.IsAxisTrigger((int)item.Source) && !item.IsTrigger)
                        {
                            // scale from range [0.0-1.0] to range [-1.0;1.0]
                            axisVal = 2.0 * (axisVal - 0.5);
                        }
                        else if (!sourceController.IsAxisTrigger((int)item.Source) && item.IsTrigger)
                        {
                            // scale from range [-1.0;1.0] to range [0.0-1.0]
                            axisVal = (axisVal + 1.0) / 2.0;
                        }
                        ret.Axes[(int)item.Target] = axisVal;
                    }
                }
            }

            foreach (var item in ButtonToAxisMaps)
            {
                if (state.Buttons.Count > (int)item.Source)
                {
                    if (item.Target != Axis.Unmapped && state.Buttons[(int)item.Source])
                    {
                        ret.Axes.EnsureSize((int)(item.Target + 1));
                        ret.Axes[(int)item.Target] = item.AxisValue;
                    }
                }
            }

            foreach (var item in AxisToButtonMaps)
            {
                if (state.Axes.Count > (int)item.Source)
                {
                    if (item.Target != Button.Unmapped)
                    {
                        ret.Buttons.EnsureSize((int)(item.Target + 1));
                        double axisVal = state.Axes[(int)item.Source];
                        if (item.Mode == AxisToButtonMapMode.WhenAboveThreshold && axisVal > item.Threshold)
                        {
                            ret.Buttons[(int)item.Target] = true;
                        }
                        else if (item.Mode == AxisToButtonMapMode.WhenBelowThreshold && axisVal < item.Threshold)
                        {
                            ret.Buttons[(int)item.Target] = true;
                        }
                        else
                        {
                            ret.Buttons[(int)item.Target] = false;
                        }
                    }
                }
            }


            return(ret);
        }
Exemple #2
0
 protected bool Equals(ControllerState other)
 {
     return(Axes.SequenceEqual(other.Axes) && Buttons.SequenceEqual(other.Buttons) && Hats.SequenceEqual(other.Hats));
 }