示例#1
0
        public static bool Unbind(CShortCut shortCut)
        {
            int index = IndexOf(shortCut);

            if (index != -1)
            {
                m_bindings.RemoveAt(index);
                return(true);
            }

            return(false);
        }
示例#2
0
        // TODO: better lookup
        private static int IndexOf(CShortCut shortCut)
        {
            for (int i = 0; i < m_bindings.Count; ++i)
            {
                if (m_bindings[i].shortCut.Equals(shortCut))
                {
                    return(i);
                }
            }

            return(-1);
        }
示例#3
0
        public static bool FindBinding(CShortCut shortCut, out CBinding result)
        {
            for (int i = 0; i < m_bindings.Count; ++i)
            {
                if (m_bindings[i].shortCut.Equals(shortCut))
                {
                    result = m_bindings[i];
                    return(true);
                }
            }

            result = default(CBinding);
            return(false);
        }
示例#4
0
        public static bool TryParse(string token, out CShortCut shortCut)
        {
            string[] tokens = token.Split('+');

            CModifiers modifiers = 0;

            if (tokens.Length > 1)
            {
                for (int i = 0; i < tokens.Length - 1; ++i)
                {
                    string name = tokens[i].ToLower();
                    if (name == "ctrl")
                    {
                        modifiers |= CModifiers.Control;
                    }
                    else if (name == "shift")
                    {
                        modifiers |= CModifiers.Shift;
                    }
                    else if (name == "alt")
                    {
                        modifiers |= CModifiers.Alt;
                    }
                    else if (name == "cmd" || name == "command")
                    {
                        modifiers |= CModifiers.Command;
                    }
                    else
                    {
                        shortCut = default(CShortCut);
                        return(false);
                    }
                }
            }

            string  keyName = tokens[tokens.Length - 1].ToLower();
            KeyCode key;

            if (!CBindings.TryParse(keyName, out key))
            {
                shortCut = default(CShortCut);
                return(false);
            }

            shortCut = new CShortCut(key, modifiers);
            return(true);
        }
示例#5
0
        public static void Bind(CShortCut shortCut, string cmdKeyDown, string cmdKeyUp)
        {
            if (cmdKeyDown == null)
            {
                throw new NullReferenceException("Command is null");
            }

            int index = IndexOf(shortCut);

            if (index != -1)
            {
                CBinding existing = m_bindings[index];
                existing.cmdKeyDown = cmdKeyDown;
                existing.cmdKeyUp   = cmdKeyUp;
                m_bindings[index]   = existing;
            }
            else
            {
                m_bindings.Add(new CBinding(shortCut, cmdKeyDown, cmdKeyUp));
            }
        }
示例#6
0
        bool Execute(string key)
        {
            string token = key.ToLower();

            CShortCut shortCut;

            if (!CShortCut.TryParse(token, out shortCut))
            {
                PrintError("Invalid shortcut: {0}", token);
                return(false);
            }

            CBindings.Unbind(shortCut); // TODO: unbind 'operation' commands

            PostNotification(
                CCommandNotifications.CBindingsChanged,
                CCommandNotifications.KeyManualMode, this.IsManualMode
                );

            return(true);
        }
        private bool IsValidModifiers(CShortCut shortCut)
        {
            if (shortCut.IsShift ^ (GetKey(KeyCode.LeftShift) || GetKey(KeyCode.RightShift)))
            {
                return(false);
            }
            if (shortCut.IsControl ^ (GetKey(KeyCode.LeftControl) || GetKey(KeyCode.RightControl)))
            {
                return(false);
            }
            if (shortCut.IsAlt ^ (GetKey(KeyCode.LeftAlt) || GetKey(KeyCode.RightAlt)))
            {
                return(false);
            }
            if (shortCut.IsCommand ^ (GetKey(KeyCode.LeftCommand) || GetKey(KeyCode.RightCommand)))
            {
                return(false);
            }

            return(true);
        }
示例#8
0
        public static bool TryParse(string token, out CShortCut shortCut)
        {
            string[] tokens = token.Split('+');

            CModifiers modifiers = 0;
            if (tokens.Length > 1)
            {
                for (int i = 0; i < tokens.Length - 1; ++i)
                {
                    string name = tokens[i].ToLower();
                    if (name == "ctrl")
                        modifiers |= CModifiers.Control;
                    else if (name == "shift")
                        modifiers |= CModifiers.Shift;
                    else if (name == "alt")
                        modifiers |= CModifiers.Alt;
                    else if (name == "cmd" || name == "command")
                        modifiers |= CModifiers.Command;
                    else
                    {
                        shortCut = default(CShortCut);
                        return false;
                    }
                }
            }

            string keyName = tokens[tokens.Length - 1].ToLower();
            KeyCode key;
            if (!CBindings.TryParse(keyName, out key))
            {
                shortCut = default(CShortCut);
                return false;
            }

            shortCut = new CShortCut(key, modifiers);
            return true;
        }
示例#9
0
 public CBinding(CShortCut shortCut, string cmdKeyUp, string cmdKeyDown)
 {
     m_shortCut   = shortCut;
     m_cmdKeyDown = cmdKeyUp;
     m_cmdKeyUp   = cmdKeyDown;
 }
示例#10
0
 public CBinding(CShortCut shortCut, string cmdKeyUp, string cmdKeyDown)
 {
     m_shortCut = shortCut;
     m_cmdKeyDown = cmdKeyUp;
     m_cmdKeyUp = cmdKeyDown;
 }
示例#11
0
 public bool Equals(CShortCut other)
 {
     return(key == other.key && modifiers == other.modifiers);
 }
示例#12
0
        public static bool FindBinding(CShortCut shortCut, out CBinding result)
        {
            for (int i = 0; i < m_bindings.Count; ++i)
            {
                if (m_bindings[i].shortCut.Equals(shortCut))
                {
                    result = m_bindings[i];
                    return true;
                }
            }

            result = default(CBinding);
            return false;
        }
示例#13
0
        public static void Bind(CShortCut shortCut, string cmdKeyDown, string cmdKeyUp)
        {
            if (cmdKeyDown == null)
            {
                throw new NullReferenceException("Command is null");
            }

            int index = IndexOf(shortCut);
            if (index != -1)
            {
                CBinding existing = m_bindings[index];
                existing.cmdKeyDown = cmdKeyDown;
                existing.cmdKeyUp = cmdKeyUp;
                m_bindings[index] = existing;
            }
            else
            {
                m_bindings.Add(new CBinding(shortCut, cmdKeyDown, cmdKeyUp));
            }
        }
示例#14
0
 public bool Equals(CShortCut other)
 {
     return key == other.key && modifiers == other.modifiers;
 }
示例#15
0
        private bool SceneUpDownHandler(KeyCode key, CModifiers modifiers)
        {
            CBinding binding;
            CShortCut shortCut = new CShortCut(key, modifiers);
            if (CBindings.FindBinding(shortCut, out binding) && binding.cmdKeyUp != null)
            {
                return ExecCommand(binding.cmdKeyUp);
            }

            return false;
        }
示例#16
0
        public static bool Unbind(CShortCut shortCut)
        {
            int index = IndexOf(shortCut);
            if (index != -1)
            {
                m_bindings.RemoveAt(index);
                return true;
            }

            return false;
        }
示例#17
0
        // TODO: better lookup
        private static int IndexOf(CShortCut shortCut)
        {
            for (int i = 0; i < m_bindings.Count; ++i)
            {
                if (m_bindings[i].shortCut.Equals(shortCut))
                {
                    return i;
                }
            }

            return -1;
        }
示例#18
0
        private bool IsValidModifiers(CShortCut shortCut)
        {
            if (shortCut.IsShift ^ (GetKey(KeyCode.LeftShift) || GetKey(KeyCode.RightShift))) return false;
            if (shortCut.IsControl ^ (GetKey(KeyCode.LeftControl) || GetKey(KeyCode.RightControl))) return false;
            if (shortCut.IsAlt ^ (GetKey(KeyCode.LeftAlt) || GetKey(KeyCode.RightAlt))) return false;
            if (shortCut.IsCommand ^ (GetKey(KeyCode.LeftCommand) || GetKey(KeyCode.RightCommand))) return false;

            return true;
        }
示例#19
0
        bool Execute(string stroke, string command = null) // TODO: refactor me!
        {
            string name = stroke.ToLower();

            if (!string.IsNullOrEmpty(command))
            {
                CShortCut shortCut;
                if (!CShortCut.TryParse(stroke, out shortCut))
                {
                    PrintError("Invalid shortcut: {0}", name);
                    return(false);
                }

                string keyUpCommand = null;

                char keyDownOp = command[0];
                if (keyDownOp == '+' || keyDownOp == '-')
                {
                    if (command.Length == 1)
                    {
                        PrintError("Identifier expected!");
                        return(false);
                    }

                    string identifier = command.Substring(1);

                    // register operation command
                    CCommand keyDownCmd = CRegistery.FindCommand(command);
                    if (keyDownCmd == null)
                    {
                        keyDownCmd = new COperationCommand(keyDownOp, identifier);
                        CRegistery.Register(keyDownCmd);
                        keyDownCmd.SetFlag(CCommandFlags.System, true);
                    }

                    // register opposite operation command
                    char keyUpOp = OppositeOperation(keyDownOp);
                    keyUpCommand = keyUpOp + identifier;
                    CCommand keyUpCmd = CRegistery.FindCommand(keyUpCommand);
                    if (keyUpCmd == null)
                    {
                        keyUpCmd = new COperationCommand(keyUpOp, identifier);
                        CRegistery.Register(keyUpCmd);
                        keyUpCmd.SetFlag(CCommandFlags.System, true);
                    }
                }

                CBindings.Bind(shortCut, StringUtils.UnArg(command), keyUpCommand != null ? StringUtils.UnArg(keyUpCommand) : null);

                PostNotification(
                    CCommandNotifications.CBindingsChanged,
                    CCommandNotifications.KeyManualMode, this.IsManualMode
                    );

                return(true);
            }

            IList <CBinding> bindings = CBindings.List(name);

            if (bindings.Count > 0)
            {
                foreach (CBinding b in bindings)
                {
                    PrintIndent(ToString(b));
                }
            }
            else
            {
                PrintIndent("No bindings");
            }

            return(true);
        }