Пример #1
0
        public void TestNotAdded()
        {
            var am    = new ActionMap <int, int>();
            int value = 0;

            am.OnAdd(1, x => value = x);
            am.Add(2, 2);
            Assert.Equal(0, value);
        }
Пример #2
0
Файл: Toy.cs Проект: smelch/BVB
        public override ActionMap GetActionMenu()
        {
            var map = new ActionMap();

            if (Abilities.Count > 0)
                map.Add(Keys.Q, () => Abilities[0].Execute());

            if (Abilities.Count > 1)
                map.Add(Keys.W, () => Abilities[1].Execute());

            if (Abilities.Count > 2)
                map.Add(Keys.E, () => Abilities[2].Execute());

            if (Abilities.Count > 3)
                map.Add(Keys.R, () => Abilities[3].Execute());

            return map;
        }
Пример #3
0
        public void TestMultipleActions()
        {
            var am = new ActionMap <int, int>();
            int value1 = 0, value2 = 0;

            am.OnAdd(1, x => value1 = x);
            am.OnAdd(1, x => value2 = x);
            am.Add(1, 2);
            Assert.Equal(2, value1);
            Assert.Equal(2, value2);
        }
Пример #4
0
        public void Subscribe <T, TInput>(Action <TInput> action)
        {
            // TODO make some attributes to filter action bus types such as MaxSubsubscribers

            Type type = typeof(T);

            if (m_actionMap.ContainsKey(type))
            {
                m_actionMap[type].Add(action);
            }
            ActionList actionList = new ActionList();

            actionList.Add(action);
            m_actionMap.Add(type, actionList);
        }
Пример #5
0
        /// <summary>
        /// Read all from an Action XElement
        /// </summary>
        /// <param name="action">The action XElement</param>
        /// <returns>True if successfull</returns>
        private bool ReadAction(XElement action)
        {
            /* A variety exists here...
             *            <action  name="v_eject_cinematic"  onPress="0"  onHold="1"  onRelease="1"  keyboard="ralt+l"  xboxpad="shoulderl+shoulderr+y"  joystick=" "  />
             *            <action  name="v_exit"  onPress="1"  onHold="0"  onRelease="1"  multiTap="1"  multiTapBlock="1"  pressTriggerThreshold="1.0"  releaseTriggerThreshold="-1"  releaseTriggerDelay="0"  keyboard="f"  xboxpad="y"  UILabel="@ui_CIExit"  UIDescription="@ui_CIExitDesc"  >
             *                    <joystick  ActivationMode="press"  input=" "  />
             *            </action>
             *            <action  name="v_throttle_100"  onPress="1"  xboxpad=" "  joystick=" "  UILabel="@ui_CIThrottleMax"  UIDescription="@ui_CIThrottleMaxDesc"  >
             *                    <xboxpad  multiTap="2"  input="thumbl_up"  />
             *                    <keyboard  multiTap="2"  input=" "  />
             *            </action>
             *
             * <action  name="v_target_deselect_component"  ActivationMode="delayed_press"  pressTriggerThreshold="0.75"  joystick=""  >
             *                    <keyboard  >
             *                            <inputdata  input="lbracket"  />
             *                            <inputdata  input="rbracket"  />
             *                    </keyboard>
             *            </action>
             *
             */
            log.Debug("DProfileReader.ReadAction - Entry");
            // a complete actionmap arrives here
            bool retVal = true;

            string name    = (string)action.Attribute("name");
            string uiLabel = (string)action.Attribute("UILabel");

            if (string.IsNullOrEmpty(uiLabel))
            {
                uiLabel = name;                   // subst if not found in Action node
            }
            SCUiText.Instance.Add(name, uiLabel); // Register item for translation

            // prep all kinds
            var jAC = new ProfileAction( )
            {
                Name = name, UILabel = uiLabel, DevID = Act.DevTag(JoystickCls.DeviceClass)
            };
            var kAC = new ProfileAction( )
            {
                Name = name, UILabel = uiLabel, DevID = Act.DevTag(KeyboardCls.DeviceClass)
            };
            var mAC = new ProfileAction( )
            {
                Name = name, UILabel = uiLabel, DevID = Act.DevTag(MouseCls.DeviceClass)
            };
            var xAC = new ProfileAction( )
            {
                Name = name, UILabel = uiLabel, DevID = Act.DevTag(GamepadCls.DeviceClass)
            };

            // process element items
            JInput(ref jAC, action, (string)action.Attribute(JoystickCls.DeviceClass));
            KInput(ref kAC, action, (string)action.Attribute(KeyboardCls.DeviceClass));
            MInput(ref mAC, action, (string)action.Attribute(MouseCls.DeviceClass));
            XInput(ref xAC, action, (string)action.Attribute(GamepadCls.DeviceClass));

            // then nested ones - they may or may not exist from the initial scan
            foreach (XElement l1action in action.Elements( ))
            {
                // comes with the name of the device class
                switch (l1action.Name.LocalName)
                {
                case JoystickCls.DeviceClass: {
                    JInput(ref jAC, l1action, (string)l1action.Attribute("input")); // may have attributed ones
                    foreach (XElement l2action in l1action.Elements( ))
                    {
                        if (l2action.Name.LocalName == "inputdata")
                        {
                            JInput(ref jAC, l2action, (string)l2action.Attribute("input")); // or in the inputdata nesting
                        }
                    }
                }
                break;

                case KeyboardCls.DeviceClass: {
                    KInput(ref kAC, l1action, (string)l1action.Attribute("input")); // may have attributed ones
                    foreach (XElement l2action in l1action.Elements( ))
                    {
                        if (l2action.Name.LocalName == "inputdata")
                        {
                            KInput(ref kAC, l2action, (string)l2action.Attribute("input")); // or in the inputdata nesting
                        }
                    }
                }
                break;

                case MouseCls.DeviceClass: {
                    MInput(ref mAC, l1action, (string)l1action.Attribute("input")); // may have attributed ones
                    foreach (XElement l2action in l1action.Elements( ))
                    {
                        if (l2action.Name.LocalName == "inputdata")
                        {
                            MInput(ref mAC, l2action, (string)l2action.Attribute("input")); // or in the inputdata nesting
                        }
                    }
                }
                break;

                case GamepadCls.DeviceClass: {
                    XInput(ref xAC, l1action, (string)l1action.Attribute("input")); // may have attributed ones
                    foreach (XElement l2action in l1action.Elements( ))
                    {
                        if (l2action.Name.LocalName == "inputdata")
                        {
                            XInput(ref xAC, l2action, (string)l2action.Attribute("input")); // or in the inputdata nesting
                        }
                    }
                }
                break;

                default: break;
                }
            }

            if (!string.IsNullOrEmpty(jAC.DefBinding))
            {
                m_currentMap.Add(jAC);                                         // finally add it to the current map if it was bound
            }
            if (!string.IsNullOrEmpty(kAC.DefBinding))
            {
                m_currentMap.Add(kAC);                                         // finally add it to the current map if it was bound
            }
            if (!string.IsNullOrEmpty(mAC.DefBinding))
            {
                m_currentMap.Add(mAC);                                         // finally add it to the current map if it was bound
            }
            if (!string.IsNullOrEmpty(xAC.DefBinding))
            {
                m_currentMap.Add(xAC);                                         // finally add it to the current map if it was bound
            }
            return(retVal);
        }
Пример #6
0
        /// <summary>
        /// Assumes to be in an action element
        /// retrieves the attributes and collects the various control=binding pairs
        /// </summary>
        /// <param name="xr">An XML reader @ StartElement</param>
        private void CollectActions(Dictionary <string, string> attr)
        {
            //first find an ActivationMode if there is - applies to all actions
            string actModeName = ActivationMode.Default.Name;
            string multiTap    = "0";

            // this can be an Activation Mode OR a multitap
            // if there is an activationMode the multiTap remains 0
            // if no ActivationMode is given, multitap is 1 or may be 2...
            if (attr.ContainsKey("ActivationMode"))
            {
                actModeName = attr["ActivationMode"];
                multiTap    = ActivationModes.Instance.MultiTapFor(actModeName).ToString( ); // given by the already collected items
            }
            else
            {
                // name remains default - we handle multiTaps only here
                multiTap = "1"; // default if not changed in the action to may be 2 or so..
                if (attr.ContainsKey("multiTap"))
                {
                    multiTap = attr["multiTap"];
                }
            }
            ActivationMode actMode = new ActivationMode(actModeName, int.Parse(multiTap)); // should be a valid ActivationMode for this action

            // we collect actions for each input ie for K,J,X and M
            if (attr.ContainsKey("joystick"))
            {
                ProfileAction ac = new ProfileAction( );
                ac.name              = attr["name"];
                ac.devID             = "J";
                ac.defBinding        = attr["joystick"];
                ac.defActivationMode = actMode;
                if (ac.defBinding == " ")
                {
                    ac.defBinding = Joystick.JoystickCls.BlendedInput;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
                else if (!String.IsNullOrEmpty(ac.defBinding))
                {
                    ac.defBinding = "js1_" + ac.defBinding;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
            }

            if (attr.ContainsKey("keyboard"))
            {
                ProfileAction ac = new ProfileAction( );
                ac.name              = attr["name"];
                ac.devID             = "K";
                ac.defBinding        = attr["keyboard"];
                ac.defActivationMode = actMode;
                if (ac.defBinding == " ")
                {
                    ac.defBinding = Keyboard.KeyboardCls.BlendedInput;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
                else if (!String.IsNullOrEmpty(ac.defBinding))
                {
                    ac.defBinding = "kb1_" + ac.defBinding;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
            }

            if (attr.ContainsKey("mouse"))   // 20151220BM: add mouse device (from AC 2.0 defaultProfile usage)
            {
                ProfileAction ac = new ProfileAction( );
                ac.name              = attr["name"];
                ac.devID             = "M";
                ac.defBinding        = attr["mouse"];
                ac.defActivationMode = actMode;
                if (ac.defBinding == " ")
                {
                    ac.defBinding = Mouse.MouseCls.BlendedInput;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
                else if (!String.IsNullOrEmpty(ac.defBinding))
                {
                    ac.defBinding = "mo1_" + ac.defBinding;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
            }

            if (attr.ContainsKey("xboxpad"))
            {
                ProfileAction ac = new ProfileAction( );
                ac.name              = attr["name"];
                ac.devID             = "X";
                ac.defBinding        = attr["xboxpad"];
                ac.defActivationMode = actMode;
                if (ac.defBinding == " ")
                {
                    ac.defBinding = Gamepad.GamepadCls.BlendedInput;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
                else if (!String.IsNullOrEmpty(ac.defBinding))
                {
                    ac.defBinding = "xi1_" + ac.defBinding;
                    m_currentMap.Add(ac); // finally add it to the current map if it was bound
                }
            }
            if (attr.ContainsKey("ps3pad"))
            {
                // ignore
            }
        }
Пример #7
0
        //METHODS
        #region Methods
        public static ActionMap ReadFile(string filepath)
        {
            StreamReader sr = new StreamReader(filepath);
            String line;
            String[] tokens;
            ActionKey actionKey;
            ActionMap map = new ActionMap();
            Type actionType;
            String actionKeyString;
            int linecounter = 0;

            while (!sr.EndOfStream)
            {
                line = sr.ReadLine();
                linecounter++;
                tokens = line.Split('/');
                //tokens[0] = entry-type
                //tokens[1] = parameters
                actionKeyString = EncodeEntryType(tokens[0],linecounter);

                tokens = tokens[1].Split(':');

                //verify action format is valid
                switch (actionKeyString[0].ToString())
                {
                    case ActionKey.EntryType.ASSOCIATION:
                        actionKeyString += EncodeParametersAssociation(tokens, linecounter);
                        break;
                    case ActionKey.EntryType.ASSOCIATIONCLASS:
                        actionKeyString += EncodeParametersAssociationClass(tokens, linecounter);
                        break;
                    case ActionKey.EntryType.GENERALIZATION:
                        actionKeyString += EncodeParametersGeneralization(tokens, linecounter);
                        break;
                    case ActionKey.EntryType.REALIZATION:
                        actionKeyString += EncodeParametersRealization(tokens, linecounter);
                        break;
                }

                if (Regex.IsMatch(tokens[tokens.Length - 1], @"^[A-Za-z0-9]*$"))
                {
                    actionKey = new ActionKey(actionKeyString);

                    //add the value-pair if no equal action-key exists
                    if (map.GetAction(actionKey) == null)
                    {
                        //create/validate action object
                        actionType = Type.GetType(ActionKey.ACTIONNAMESPACE + tokens[tokens.Length - 1]);
                        //add the map entry
                        map.Add(actionKey, (AbstractAction)Activator.CreateInstance(actionType));
                    }
                    else
                    {
                        throw new InvalidOperationException("An equivalent ActionKey already exists in the ActionMap at line: " + linecounter);
                    }
                }
                else
                {
                    throw new ArgumentException("The action-name contains some invalid characters at line: " + linecounter);
                }
            }

            return map;
        }
Пример #8
0
 /// <summary>
 /// Assumes to be in an action element
 /// retrieves the attributes and collects the various control=binding pairs
 /// </summary>
 /// <param name="xr">An XML reader @ StartElement</param>
 private void CollectActions(XmlReader xr, Dictionary <string, string> attr)
 {
     // we collect actions for each input ie for K,J,X and P
     if (attr.ContainsKey("joystick"))
     {
         Action ac = new Action( );
         ac.name       = attr["name"];
         ac.input      = "J";
         ac.defBinding = attr["joystick"];
         if (ac.defBinding == " ")
         {
             ac.defBinding = JoystickCls.BlendedInput;
         }
         if (!String.IsNullOrEmpty(ac.defBinding))
         {
             m_currentMap.Add(ac);                                      // finally add it to the current map if it was bound
         }
     }
     if (attr.ContainsKey("keyboard"))
     {
         Action ac = new Action( );
         ac.name       = attr["name"];
         ac.input      = "K";
         ac.defBinding = attr["keyboard"];
         if (ac.defBinding == " ")
         {
             ac.defBinding = KeyboardCls.BlendedInput;
         }
         if (!String.IsNullOrEmpty(ac.defBinding))
         {
             m_currentMap.Add(ac);                                      // finally add it to the current map if it was bound
         }
     }
     if (attr.ContainsKey("xboxpad"))
     {
         Action ac = new Action( );
         ac.name       = attr["name"];
         ac.input      = "X";
         ac.defBinding = attr["xboxpad"];
         if (ac.defBinding == " ")
         {
             ac.defBinding = GamepadCls.BlendedInput;
         }
         if (!String.IsNullOrEmpty(ac.defBinding))
         {
             m_currentMap.Add(ac);                                      // finally add it to the current map if it was bound
         }
     }
     if (attr.ContainsKey("ps3pad"))
     {
         Action ac = new Action( );
         ac.name       = attr["name"];
         ac.input      = "P";
         ac.defBinding = attr["ps3pad"];
         if (ac.defBinding == " ")
         {
             ac.defBinding = GamepadCls.BlendedInput;
         }
         if (!String.IsNullOrEmpty(ac.defBinding))
         {
             m_currentMap.Add(ac);                                      // finally add it to the current map if it was bound
         }
     }
 }