Beispiel #1
0
        /// <summary>
        ///   Checks if the given string represents a valid buttton/key for the given input device.
        /// </summary>
        /// <param name="dev">
        ///   The input device.
        /// </param>
        /// <param name="but">
        ///   The button/key string.
        /// </param>
        /// <returns>
        ///   True if the given string represents a valid button/key for the given input device, otherwise false.
        /// </returns>
        public static bool IsButton(InputDevice dev, string but)
        {
            if (dev is InputDevice.Keyboard)
            {
                return(KeyboardManager.IsKey(but));
            }
            else if (dev is InputDevice.Mouse)
            {
                return(MouseManager.IsButton(but));
            }
            else if (dev is InputDevice.Joystick)
            {
                return(JoystickManager.IsButton(but));
            }

            return(false);
        }
Beispiel #2
0
        /// <summary>
        ///   Loads data from an xml element.
        /// </summary>
        /// <param name="ele">
        ///   The element to load data from.
        /// </param>
        /// <returns>
        ///   True if loaded successfully and false otherwise.
        /// </returns>
        public override bool LoadFromXml(XmlElement ele)
        {
            if (ele is null)
            {
                return(Logger.LogReturn("Failed loading InputMap: Null xml element.", false, LogType.Error));
            }

            // Type
            {
                string loname = ele.Name.ToLower();
                Type = loname == "button" ? InputType.Button : (loname == "axis" ? InputType.Axis : (InputType)(-1));

                if (Type < 0)
                {
                    return(Logger.LogReturn("Failed loading InputMap: Invalid input type.", false, LogType.Error));
                }
            }

            // Device
            {
                if (!ele.HasAttribute(nameof(Device)))
                {
                    return(Logger.LogReturn("Failed loading InputMap: No Device attribute.", false, LogType.Error));
                }
                if (!Enum.TryParse(ele.GetAttribute(nameof(Device)), true, out InputDevice dev))
                {
                    return(Logger.LogReturn("Failed loading InputMap: Invalid Device attribute.", false, LogType.Error));
                }

                Device = dev;
            }

            // Value
            {
                if (!ele.HasAttribute(nameof(Value)) && !ele.HasAttribute("Positive") &&
                    !ele.HasAttribute(nameof(Negative)))
                {
                    return(Logger.LogReturn("Failed loading InputMap: No Positive and Negative or Value attributes.", false, LogType.Error));
                }

                string val = ele.GetAttribute(nameof(Value)),
                       neg = ele.GetAttribute(nameof(Negative));

                if (string.IsNullOrWhiteSpace(val))
                {
                    val = ele.GetAttribute("Positive");
                }

                if (string.IsNullOrWhiteSpace(val))
                {
                    val = null;
                }
                if (string.IsNullOrWhiteSpace(neg))
                {
                    neg = null;
                }

                if (val is null && neg is null)
                {
                    return(Logger.LogReturn("Failed loading InputMap: Invalid Positive, Negative and/or Value attributes.", false, LogType.Error));
                }

                if (Device is InputDevice.Keyboard)
                {
                    if (val is not null && !KeyboardManager.IsKey(val))
                    {
                        return(Logger.LogReturn("Failed loading InputMap: Invalid Positive or Value attribute.", false, LogType.Error));
                    }
                    if (neg is not null && !KeyboardManager.IsKey(neg))
                    {
                        return(Logger.LogReturn("Failed loading InputMap: Invalid Negative attribute.", false, LogType.Error));
                    }
                }
                else if (Device is InputDevice.Mouse)
                {
                    if (Type is InputType.Axis)
                    {
                        if (!MouseManager.IsAxis(val))
                        {
                            return(Logger.LogReturn("Failed loading InputMap: Unable to parse mouse axis.", false, LogType.Error));
                        }
                    }
                    else if (Type is InputType.Button)
                    {
                        if (val is not null && !MouseManager.IsButton(val))
                        {
                            return(Logger.LogReturn("Failed loading InputMap: Invalid Positive or Value attribute.", false, LogType.Error));
                        }
                        if (neg is not null && !MouseManager.IsButton(neg))
                        {
                            return(Logger.LogReturn("Failed loading InputMap: Invalid Negative attribute.", false, LogType.Error));
                        }
                    }
                }
                else if (Device is InputDevice.Joystick)
                {
                    if (Type is InputType.Axis)
                    {
                        if (!JoystickManager.IsAxis(val))
                        {
                            return(Logger.LogReturn("Failed loading InputMap: Unable to parse joystick axis.", false, LogType.Error));
                        }
                    }
                    else if (Type is InputType.Button)
                    {
                        if (val is not null && !JoystickManager.IsButton(val))
                        {
                            return(Logger.LogReturn("Failed loading InputMap: Invalid Positive or Value attribute.", false, LogType.Error));
                        }
                        if (neg is not null && !JoystickManager.IsButton(neg))
                        {
                            return(Logger.LogReturn("Failed loading InputMap: Invalid Negative attribute.", false, LogType.Error));
                        }
                    }
                }

                Value    = val ?? string.Empty;
                Negative = neg ?? string.Empty;
            }

            // Invert
            {
                if (ele.HasAttribute(nameof(Invert)))
                {
                    string invert = ele.GetAttribute(nameof(Invert));

                    if (string.IsNullOrWhiteSpace(invert))
                    {
                        return(Logger.LogReturn("Failed loading InputMap: Invalid Invert attribute.", false, LogType.Error));
                    }
                    if (!bool.TryParse(invert, out bool i))
                    {
                        return(Logger.LogReturn("Failed loading InputMap: Unable to parse Invert attribute.", false, LogType.Error));
                    }

                    Invert = i;
                }
            }

            return(true);
        }
Beispiel #3
0
 /// <summary>
 ///   Copy constructor.
 /// </summary>
 public MouseManager(MouseManager mm)
 {
     m_last    = new MouseState(mm.m_last);
     m_current = new MouseState(mm.m_current);
 }