Esempio n. 1
0
        public void HandleKeyEventForContext(Event evt, object context)
        {
            if (evt == null || !evt.isKey || evt.keyCode == KeyCode.None)
            {
                return;
            }

            if (evt.type == EventType.KeyUp)
            {
                m_KeysDown.Remove(evt.keyCode);
                ShortcutEntry shortcutEntry;
                if (m_ActiveClutches.TryGetValue(evt.keyCode, out shortcutEntry))
                {
                    m_ActiveClutches.Remove(evt.keyCode);
                    var args = new ShortcutArguments
                    {
                        context = context as EditorWindow,
                        state   = ShortcutState.End,
                    };
                    shortcutEntry.action(args);
                }

                return;
            }

            if (m_KeysDown.Contains(evt.keyCode))
            {
                evt.Use();
                return;
            }
            m_KeysDown.Add(evt.keyCode);

            var keyCodeCombination = new KeyCombination(evt);

            m_KeyCombinationSequence.Add(keyCodeCombination);

            s_Context[1] = priorityContext?.GetType() ?? context?.GetType();
            m_Directory.FindShortcutEntries(m_KeyCombinationSequence, s_Context, m_Entries);
            if (priorityContext != null && m_Entries.Count(entry => entry.context == null) < m_Entries.Count)
            {
                m_Entries.RemoveAll(entry => entry.context == null);
            }

            switch (m_Entries.Count)
            {
            case 0:
                Reset();
                break;

            case 1:
                var shortcutEntry = m_Entries.Single();
                if (ShortcutFullyMatchesKeyCombination(shortcutEntry))
                {
                    if (evt.keyCode != m_KeyCombinationSequence.Last().keyCode)
                    {
                        break;
                    }

                    var args = new ShortcutArguments();
                    args.context = context as EditorWindow;
                    switch (shortcutEntry.type)
                    {
                    case ShortcutType.Action:
                        args.state = ShortcutState.End;
                        shortcutEntry.action(args);
                        evt.Use();
                        Reset();
                        break;

                    case ShortcutType.Clutch:
                        if (!m_ActiveClutches.ContainsKey(evt.keyCode))
                        {
                            m_ActiveClutches.Add(evt.keyCode, shortcutEntry);
                            args.state = ShortcutState.Begin;
                            shortcutEntry.action(args);
                            evt.Use();
                            Reset();
                        }
                        break;
                    }
                }
                break;

            default:
                if (HasConflicts(m_Entries, m_KeyCombinationSequence))
                {
                    m_ConflictResolver.ResolveConflict(m_KeyCombinationSequence, m_Entries);
                    Reset();
                }
                break;
            }
        }
Esempio n. 2
0
        internal void Init(IConflictResolver conflictResolver, IEnumerable <KeyCombination> keyCombinationSequence, IEnumerable <ShortcutEntry> entries, GUIView previouslyFocusedView)
        {
            m_PreviouslyFocusedView = previouslyFocusedView;
            m_ConflictResolver      = conflictResolver;
            m_Entries = entries.ToList();

            var multiColumnHeader = new MultiColumnHeader(m_MulticolumnHeaderState);

            multiColumnHeader.ResizeToFit();
            m_ConflictListView = new ConflictListView(m_TreeViewState, multiColumnHeader, m_Entries);


            m_Header = string.Format(L10n.Tr("The binding \"{0}\" conflicts with multiple commands."), KeyCombination.SequenceToString(keyCombinationSequence));
        }
        void MigrateUserSpecifiedPrefKeys()
        {
            // If migration already happened then don't do anything
            if (EditorPrefs.GetBool(k_ProfileMigratedEditorPrefKey, false))
            {
                return;
            }

            EditorPrefs.SetBool(k_ProfileMigratedEditorPrefKey, true);

            // Find shortcut entries that might need to be migrated
            var allShortcuts = new List <ShortcutEntry>();

            directory.GetAllShortcuts(allShortcuts);

            // Find existing or create migrated profile and make it active so we can amend it
            var originalActiveProfile         = profileManager.activeProfile;
            var migratedProfile               = profileManager.GetProfileById(k_MigratedProfileId);
            var migratedProfileAlreadyExisted = migratedProfile != null;

            if (!migratedProfileAlreadyExisted)
            {
                migratedProfile = profileManager.CreateProfile(k_MigratedProfileId);
            }
            profileManager.activeProfile = migratedProfile;

            var migratedProfileModified = false;

            var tempKeyCombinations          = new KeyCombination[1];
            var methodsWithFormerlyPrefKeyAs = EditorAssemblies.GetAllMethodsWithAttribute <FormerlyPrefKeyAsAttribute>();

            foreach (var method in methodsWithFormerlyPrefKeyAs)
            {
                var shortcutAttr = Attribute.GetCustomAttribute(method, typeof(ShortcutAttribute), true) as ShortcutAttribute;
                if (shortcutAttr == null)
                {
                    continue;
                }

                var entry = allShortcuts.Find(e => string.Equals(e.identifier.path, shortcutAttr.identifier));
                if (entry == null)
                {
                    continue;
                }

                // Ignore former PrefKey if it is overriden in existing migrated profile
                if (entry.overridden)
                {
                    continue;
                }

                // Parse default pref key value from FormerlyPrefKeyAs attribute
                var    prefKeyAttr            = (FormerlyPrefKeyAsAttribute)Attribute.GetCustomAttribute(method, typeof(FormerlyPrefKeyAsAttribute));
                var    editorPrefDefaultValue = $"{prefKeyAttr.name};{prefKeyAttr.defaultValue}";
                string name;
                Event  keyboardEvent;
                string shortcut;
                if (!TryParseUniquePrefKeyString(editorPrefDefaultValue, out name, out keyboardEvent, out shortcut))
                {
                    continue;
                }
                var prefKeyDefaultKeyCombination = KeyCombination.FromPrefKeyKeyboardEvent(keyboardEvent);

                // Parse current pref key value (falling back on default pref key value)
                if (!TryParseUniquePrefKeyString(EditorPrefs.GetString(prefKeyAttr.name, editorPrefDefaultValue), out name, out keyboardEvent, out shortcut))
                {
                    continue;
                }
                var prefKeyCurrentKeyCombination = KeyCombination.FromPrefKeyKeyboardEvent(keyboardEvent);

                // Only migrate pref keys that the user actually overwrote
                if (prefKeyCurrentKeyCombination.Equals(prefKeyDefaultKeyCombination))
                {
                    continue;
                }

                string invalidBindingMessage;
                tempKeyCombinations[0] = prefKeyCurrentKeyCombination;
                if (!bindingValidator.IsBindingValid(tempKeyCombinations, out invalidBindingMessage))
                {
                    Debug.LogWarning($"Could not migrate existing binding for shortcut \"{entry.identifier.path}\" with invalid binding.\n{invalidBindingMessage}.");
                    continue;
                }

                profileManager.ModifyShortcutEntry(entry.identifier, new List <KeyCombination> {
                    prefKeyCurrentKeyCombination
                });

                migratedProfileModified = true;
            }

            // Delete migrated profile if it was created and not modified
            if (!migratedProfileAlreadyExisted && !migratedProfileModified)
            {
                profileManager.DeleteProfile(migratedProfile);
            }

            // Restore original active profile unless last loaded profile was null and the migrated profile was created
            if (originalActiveProfile != null || migratedProfileAlreadyExisted)
            {
                profileManager.activeProfile = originalActiveProfile;
            }
        }
Esempio n. 4
0
        public void HandleKeyEvent(Event evt, IContextManager contextManager)
        {
            if (evt == null || !evt.isKey || evt.keyCode == KeyCode.None)
            {
                return;
            }

            if (evt.type == EventType.KeyUp)
            {
                KeyValuePair <ShortcutEntry, object> clutchKeyValuePair;
                if (m_ActiveClutches.TryGetValue(evt.keyCode, out clutchKeyValuePair))
                {
                    var clutchContext = m_ActiveClutches[evt.keyCode].Value;

                    m_ActiveClutches.Remove(evt.keyCode);
                    var args = new ShortcutArguments
                    {
                        context = clutchContext,
                        stage   = ShortcutStage.End
                    };
                    invokingAction?.Invoke(clutchKeyValuePair.Key, args);
                    clutchKeyValuePair.Key.action(args);
                }
                return;
            }

            // Use the event and return if the key is currently used in an active clutch
            if (m_ActiveClutches.ContainsKey(evt.keyCode))
            {
                evt.Use();
                return;
            }

            var keyCodeCombination = KeyCombination.FromKeyboardInput(evt);

            m_KeyCombinationSequence.Add(keyCodeCombination);

            // Ignore event if sequence is empty
            if (m_KeyCombinationSequence.Count == 0)
            {
                return;
            }

            m_Directory.FindShortcutEntries(m_KeyCombinationSequence, contextManager, m_Entries);
            IEnumerable <ShortcutEntry> entries = m_Entries;

            // Deal ONLY with prioritycontext
            if (entries.Count() > 1 && contextManager.HasAnyPriorityContext())
            {
                m_CurrentContextManager = contextManager;
                entries = m_Entries.FindAll(CurrentContextManagerHasPriorityContextFor);
                if (!entries.Any())
                {
                    entries = m_Entries;
                }
            }

            switch (entries.Count())
            {
            case 0:
                Reset();
                break;

            case 1:
                var shortcutEntry = entries.Single();
                if (ShortcutFullyMatchesKeyCombination(shortcutEntry))
                {
                    if (evt.keyCode != m_KeyCombinationSequence.Last().keyCode)
                    {
                        break;
                    }

                    var args = new ShortcutArguments();
                    args.context = contextManager.GetContextInstanceOfType(shortcutEntry.context);
                    switch (shortcutEntry.type)
                    {
                    case ShortcutType.Action:
                        args.stage = ShortcutStage.End;
                        invokingAction?.Invoke(shortcutEntry, args);
                        shortcutEntry.action(args);
                        evt.Use();
                        Reset();
                        break;

                    case ShortcutType.Clutch:
                        if (!m_ActiveClutches.ContainsKey(evt.keyCode))
                        {
                            m_ActiveClutches.Add(evt.keyCode, new KeyValuePair <ShortcutEntry, object>(shortcutEntry, args.context));
                            args.stage = ShortcutStage.Begin;
                            invokingAction?.Invoke(shortcutEntry, args);
                            shortcutEntry.action(args);
                            evt.Use();
                            Reset();
                        }
                        break;

                    case ShortcutType.Menu:
                        args.stage = ShortcutStage.End;
                        invokingAction?.Invoke(shortcutEntry, args);
                        shortcutEntry.action(args);
                        evt.Use();
                        Reset();
                        break;
                    }
                }
                break;

            default:
                if (HasConflicts(entries, m_KeyCombinationSequence))
                {
                    m_ConflictResolver.ResolveConflict(m_KeyCombinationSequence, entries);
                    evt.Use();
                    Reset();
                }
                break;
            }
        }
Esempio n. 5
0
        public void HandleKeyEvent(Event evt, IContextManager contextManager)
        {
            if (evt == null || !evt.isKey || evt.keyCode == KeyCode.None)
            {
                return;
            }

            if (evt.type == EventType.KeyUp)
            {
                m_KeysDown.Remove(evt.keyCode);
                ShortcutEntry shortcutEntry;
                if (m_ActiveClutches.TryGetValue(evt.keyCode, out shortcutEntry))
                {
                    m_ActiveClutches.Remove(evt.keyCode);
                    var args = new ShortcutArguments
                    {
                        context = contextManager.GetContextInstanceOfType(shortcutEntry.context),
                        state   = ShortcutState.End,
                    };
                    shortcutEntry.action(args);
                }
                return;
            }

            if (m_KeysDown.Contains(evt.keyCode))
            {
                evt.Use();
                return;
            }
            m_KeysDown.Add(evt.keyCode);

            var keyCodeCombination = new KeyCombination(evt);

            m_KeyCombinationSequence.Add(keyCodeCombination);

            // Ignore event if sequence is empty
            if (m_KeyCombinationSequence.Count == 0)
            {
                return;
            }

            m_Directory.FindShortcutEntries(m_KeyCombinationSequence, contextManager, m_Entries);

            if (m_Entries.Count > 1 && contextManager.priorityContext != null)
            {
                var entry = m_Entries.FirstOrDefault(a => a.context == contextManager.priorityContext.GetType());
                if (entry != null)
                {
                    m_Entries.Clear();
                    m_Entries.Add(entry);
                }
            }

            switch (m_Entries.Count)
            {
            case 0:
                Reset();
                break;

            case 1:
                var shortcutEntry = m_Entries.Single();
                if (ShortcutFullyMatchesKeyCombination(shortcutEntry))
                {
                    if (evt.keyCode != m_KeyCombinationSequence.Last().keyCode)
                    {
                        break;
                    }

                    var args = new ShortcutArguments();
                    args.context = contextManager.GetContextInstanceOfType(shortcutEntry.context);
                    switch (shortcutEntry.type)
                    {
                    case ShortcutType.Action:
                        args.state = ShortcutState.End;
                        shortcutEntry.action(args);
                        evt.Use();
                        Reset();
                        break;

                    case ShortcutType.Clutch:
                        if (!m_ActiveClutches.ContainsKey(evt.keyCode))
                        {
                            m_ActiveClutches.Add(evt.keyCode, shortcutEntry);
                            args.state = ShortcutState.Begin;
                            shortcutEntry.action(args);
                            evt.Use();
                            Reset();
                        }
                        break;
                    }
                }
                break;

            default:
                if (HasConflicts(m_Entries, m_KeyCombinationSequence))
                {
                    m_ConflictResolver.ResolveConflict(m_KeyCombinationSequence, m_Entries);
                    Reset();
                }
                break;
            }
        }
Esempio n. 6
0
        public void HandleKeyEvent(Event evt, IContextManager contextManager)
        {
            if (evt == null || !evt.isKey || evt.keyCode == KeyCode.None)
            {
                return;
            }

            if (evt.type == EventType.KeyUp)
            {
                m_KeysDown.Remove(evt.keyCode);
                Tuple <ShortcutEntry, object> clutchTuple;
                if (m_ActiveClutches.TryGetValue(evt.keyCode, out clutchTuple))
                {
                    var clutchContext = m_ActiveClutches[evt.keyCode].Item2;

                    m_ActiveClutches.Remove(evt.keyCode);
                    var args = new ShortcutArguments
                    {
                        context = clutchContext,
                        state   = ShortcutState.End
                    };
                    clutchTuple.Item1.action(args);
                }
                return;
            }

            if (m_KeysDown.Contains(evt.keyCode))
            {
                evt.Use();
                return;
            }
            m_KeysDown.Add(evt.keyCode);

            var keyCodeCombination = new KeyCombination(evt);

            m_KeyCombinationSequence.Add(keyCodeCombination);

            // Ignore event if sequence is empty
            if (m_KeyCombinationSequence.Count == 0)
            {
                return;
            }

            m_Directory.FindShortcutEntries(m_KeyCombinationSequence, contextManager, m_Entries);

            // Deal ONLY with prioritycontext
            if (m_Entries.Count > 1 && contextManager.HasAnyPriorityContext())
            {
                var entry = m_Entries.FindAll(a => contextManager.HasPriorityContextOfType(a.context));
                if (entry.Any())
                {
                    m_Entries.Clear();
                    m_Entries.AddRange(entry);
                }
            }

            switch (m_Entries.Count)
            {
            case 0:
                Reset();
                break;

            case 1:
                var shortcutEntry = m_Entries.Single();
                if (ShortcutFullyMatchesKeyCombination(shortcutEntry))
                {
                    if (evt.keyCode != m_KeyCombinationSequence.Last().keyCode)
                    {
                        break;
                    }

                    var args = new ShortcutArguments();
                    args.context = contextManager.GetContextInstanceOfType(shortcutEntry.context);
                    switch (shortcutEntry.type)
                    {
                    case ShortcutType.Action:
                        args.state = ShortcutState.End;
                        shortcutEntry.action(args);
                        evt.Use();
                        Reset();
                        break;

                    case ShortcutType.Clutch:
                        if (!m_ActiveClutches.ContainsKey(evt.keyCode))
                        {
                            m_ActiveClutches.Add(evt.keyCode, new Tuple <ShortcutEntry, object>(shortcutEntry, args.context));
                            args.state = ShortcutState.Begin;
                            shortcutEntry.action(args);
                            evt.Use();
                            Reset();
                        }
                        break;

                    case ShortcutType.Menu:
                        args.state = ShortcutState.End;
                        EditorApplication.ExecuteMenuItem(shortcutEntry.identifier.path);
                        evt.Use();
                        Reset();
                        break;
                    }
                }
                break;

            default:
                if (HasConflicts(m_Entries, m_KeyCombinationSequence))
                {
                    m_ConflictResolver.ResolveConflict(m_KeyCombinationSequence, m_Entries);
                    Reset();
                }
                break;
            }
        }
Esempio n. 7
0
 public override string ToString() => KeyCombination.SequenceToString(keyCombinationSequence);
Esempio n. 8
0
 public ShortcutBinding(KeyCombination keyCombination)
     : this(new[] { keyCombination })
 {
 }