Example #1
0
 public KeyEvent(params KeyCode[] keyCodes)
 {
     keys = new KeyCodeWithModifier[keyCodes.Length];
     for (int i = 0; i < keyCodes.Length; i++)
     {
         keys[i] = new KeyCodeWithModifier(keyCodes[i], EventModifiers.None);
     }
     defaultKeys = keys.ToList().ToArray();
 }
        static bool PreferenceKeyItem(ref KeyCodeWithModifier keyPref, int index = -1)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label("Key:");

            if (keyPref.keyCode != KeyCode.None && !UniqueKey(ref keyPref, index))
            {
                KeyEventField(index, ref keyPref, constants.redTextBox);
            }
            else
            {
                KeyEventField(index, ref keyPref, GUI.skin.textField);
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Modifiers:");
            GUILayout.BeginVertical();

            EventModifiers modifier = keyPref.modifiers;

            bool modifier_command = (modifier & EventModifiers.Command) != 0;
            bool modifier_alt     = (modifier & EventModifiers.Alt) != 0;
            bool modifier_shift   = (modifier & EventModifiers.Shift) != 0;
            bool modifier_control = (modifier & EventModifiers.Control) != 0;

            if (Application.platform == RuntimePlatform.OSXEditor)
            {
                modifier_command = GUILayout.Toggle(modifier_command, "Command");
            }
            modifier_control = GUILayout.Toggle(modifier_control, "Control");
            modifier_shift   = GUILayout.Toggle(modifier_shift, "Shift");
            modifier_alt     = GUILayout.Toggle(modifier_alt, "Alt");

            var key_is_no_longer_valid = (index != -1 &&
                                          (GUILayout.Button("Remove") ||
                                           keyPref.keyCode == KeyCode.None));

            GUILayout.EndVertical();
            GUILayout.EndHorizontal();

            if (GUI.changed && key_is_no_longer_valid)
            {
                keyPref.modifiers = (modifier_command ? EventModifiers.Command : 0)
                                    | (modifier_alt ? EventModifiers.Alt : 0)
                                    | (modifier_shift ? EventModifiers.Shift : 0)
                                    | (modifier_control ? EventModifiers.Control : 0);
            }
            return(key_is_no_longer_valid);
        }
        static bool UniqueKey(ref KeyCodeWithModifier keyPref, int index)
        {
            for (int i = 0; i < keyArray.Length; i++)
            {
                var allKeys = keyArray[i].keyEvent.keys;
                for (int j = 0; j < allKeys.Length; j++)
                {
                    if (i == m_SelectedKeyIndex && j == index)
                    {
                        continue;
                    }

                    if (allKeys[j].keyCode == keyPref.keyCode &&
                        allKeys[j].modifiers == keyPref.modifiers)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
        public static void PreferenceWindow()
        {
            if (constants == null)
            {
                constants = new LocalStyles();
            }
            if (keyArray == null)
            {
                keyArray = GetKeys();
                ReadKeys();
            }

            int id = GUIUtility.GetControlID(s_KeysControlHash, FocusType.Keyboard);

            KeyPref prevKey          = null;
            KeyPref nextKey          = null;
            bool    foundSelectedKey = false;


            var width = Mathf.Min(Mathf.Max(170f, EditorGUIUtility.currentViewWidth - 600), 400);

            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical(GUILayout.MaxWidth(width));
            GUILayout.Label("Actions", constants.settingsBoxTitle, GUILayout.ExpandWidth(true));
            Rect selectedRect = default(Rect);

            keyNamesScrollPos = GUILayout.BeginScrollView(keyNamesScrollPos);            //, constants.settingsBox);
            {
                for (int i = 0; i < keyArray.Length; i++)
                {
                    var keyPref = keyArray[i];
                    if (!foundSelectedKey)
                    {
                        if (keyPref == m_SelectedKey)
                        {
                            foundSelectedKey = true;
                        }
                        else
                        {
                            prevKey = keyPref;
                        }
                    }
                    else
                    {
                        if (nextKey == null)
                        {
                            nextKey = keyPref;
                        }
                    }

                    EditorGUI.BeginChangeCheck();
                    if (GUILayout.Toggle(keyPref == m_SelectedKey, keyPref.name, constants.keysElement))
                    {
                        if (m_SelectedKey != keyPref)
                        {
                            checkBounds = true;
                        }
                        m_SelectedKeyIndex = i;
                        m_SelectedKey      = keyPref;
                        newKey             = new KeyCodeWithModifier();
                        if (Event.current.type == EventType.Repaint)
                        {
                            selectedRect = GUILayoutUtility.GetLastRect();
                        }
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        GUIUtility.keyboardControl = id;
                    }
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();
            if (Event.current.type == EventType.Repaint && checkBounds)
            {
                checkBounds = false;
                Rect scrollViewRect = GUILayoutUtility.GetLastRect();
                scrollViewRect.position  = Vector2.zero;
                scrollViewRect.position += keyNamesScrollPos;
                scrollViewRect.yMax     -= 34;

                if (selectedRect.yMax > scrollViewRect.yMax)
                {
                    keyNamesScrollPos.y = selectedRect.yMax - scrollViewRect.height;
                    HandleUtility.Repaint();
                }
                if (selectedRect.yMin < scrollViewRect.yMin)
                {
                    keyNamesScrollPos.y = selectedRect.yMin;
                    HandleUtility.Repaint();
                }
                if (keyNamesScrollPos.y < 0)
                {
                    keyNamesScrollPos.y = 0;
                }
            }

            GUILayout.Space(10.0f);

            GUILayout.BeginVertical();
            keySettingsScrollPos = GUILayout.BeginScrollView(keySettingsScrollPos);

            if (m_SelectedKey != null)
            {
                GUI.changed = false;

                var allKeys = m_SelectedKey.keyEvent.keys;

                for (int i = 0; i < allKeys.Length; i++)
                {
                    PreferenceKey(ref allKeys, i);
                }

                PreferenceKeyItem(ref newKey);
                if (newKey.keyCode != KeyCode.None)
                {
                    ArrayUtility.Add(ref allKeys, newKey);
                }

                m_SelectedKey.keyEvent.keys = allKeys;


                if (GUI.changed)
                {
                    StoreKeys();
                }
                else
                {
                    if (GUIUtility.keyboardControl == id && Event.current.type == EventType.KeyDown)
                    {
                        switch (Event.current.keyCode)
                        {
                        case KeyCode.UpArrow:
                            if (prevKey != null && prevKey != m_SelectedKey)
                            {
                                m_SelectedKey = prevKey;
                                checkBounds   = true;
                                //m_ValidKeyChange = true;
                            }
                            Event.current.Use();
                            break;

                        case KeyCode.DownArrow:
                            if (nextKey != null && nextKey != m_SelectedKey)
                            {
                                m_SelectedKey = nextKey;
                                checkBounds   = true;
                                //m_ValidKeyChange = true;
                            }
                            Event.current.Use();
                            break;
                        }
                    }
                }
            }

            GUILayout.EndScrollView();
            GUILayout.EndVertical();
            GUILayout.Space(10f);

            GUILayout.EndHorizontal();
            GUILayout.Space(5f);

            if (GUILayout.Button("Use Defaults", GUILayout.Width(120)))
            {
                RevertKeys();
                StoreKeys();
            }
        }
        internal static void KeyEventField(int index, ref KeyCodeWithModifier key, GUIStyle style, params GUILayoutOption[] options)
        {
            Rect r = GUILayoutUtility.GetRect(defaultKeyContent, style, options);

            KeyEventField(r, index, ref key, style);
        }
 internal static void KeyEventField(Rect position, int index, ref KeyCodeWithModifier key, GUIStyle style)
 {
     DoKeyEventField(position, index, ref key, style);
 }
        static void DoKeyEventField(Rect position, int index, ref KeyCodeWithModifier key, GUIStyle style)
        {
            int   id  = GUIUtility.GetControlID(s_KeyEventFieldHash + index, FocusType.Passive, position);
            Event evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.MouseDown:
            {
                // If the mouse is inside the button, we say that we're the hot control
                if (position.Contains(evt.mousePosition))
                {
                    GUIUtility.hotControl = id;
                    evt.Use();
                    if (bKeyEventActive == id)
                    // cancel
                    {
                        bKeyEventActive = -1;
                    }
                    else
                    {
                        bKeyEventActive = id;
                    }
                }
                return;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == id)
                {
                    GUIUtility.hotControl = id;

                    // If we got the mousedown, the mouseup is ours as well
                    // (no matter if the click was in the button or not)
                    evt.Use();
                }
                return;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl == id)
                {
                    evt.Use();
                }
                break;
            }

            case EventType.Repaint:
            {
                if (bKeyEventActive == id)
                {
                    style.Draw(position, defaultKeyContent, id);
                }
                else
                {
                    string str = KeyEvent.CodeToString(key.keyCode);
                    style.Draw(position, new GUIContent(str), id);
                }
                break;
            }

            case EventType.KeyDown:
            {
                if ((GUIUtility.hotControl == id) && bKeyEventActive == id)
                {
                    // ignore presses of just modifier keys
                    if (evt.character == '\0')
                    {
                        if (evt.alt &&
                            (evt.keyCode == KeyCode.AltGr || evt.keyCode == KeyCode.LeftAlt || evt.keyCode == KeyCode.RightAlt) ||
                            evt.control && (evt.keyCode == KeyCode.LeftControl || evt.keyCode == KeyCode.RightControl) ||
                            evt.command &&
                            (evt.keyCode == KeyCode.LeftApple || evt.keyCode == KeyCode.RightApple || evt.keyCode == KeyCode.LeftWindows ||
                             evt.keyCode == KeyCode.RightWindows) ||
                            evt.shift &&
                            (evt.keyCode == KeyCode.LeftShift || evt.keyCode == KeyCode.RightShift || (int)evt.keyCode == 0))
                        {
                            return;
                        }
                    }
                    bKeyEventActive = -1;
                    GUI.changed     = true;
                    key.keyCode     = evt.keyCode;
                    key.modifiers   = (evt.command ? EventModifiers.Command : 0)
                                      | (evt.alt     ? EventModifiers.Alt : 0)
                                      | (evt.shift   ? EventModifiers.Shift : 0)
                                      | (evt.control ? EventModifiers.Control : 0);
                    GUIUtility.hotControl = 0;
                    evt.Use();
                    return;
                }
                break;
            }
            }
        }
Example #8
0
 public KeyEvent(KeyCode code, EventModifiers modifier)
 {
     keys = new KeyCodeWithModifier[1] {
         new KeyCodeWithModifier(code, modifier, false)
     }; defaultKeys = keys.ToList().ToArray();
 }
Example #9
0
 public KeyEvent(KeyCode code, bool hold)
 {
     keys = new KeyCodeWithModifier[1] {
         new KeyCodeWithModifier(code, EventModifiers.None, hold)
     }; defaultKeys = keys.ToList().ToArray();
 }