private static void OnGUISettings() { if (HQ.Settings == null) { return; } EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); { EditorGUILayout.BeginVertical(GUILayoutOptionPool.ExpandWidthTrue); GUILayout.Label("Bind a MenuItem with a custom hotkey.", GeneralStyles.SmallLabel); if (GUILayout.Button("Help", GUILayoutOptionPool.Width(50F)) == true) { Application.OpenURL("https://docs.unity3d.com/ScriptReference/MenuItem.html"); } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(GUILayoutOptionPool.Width(130F)); if (EditorApplication.isCompiling == true) { using (BgColorContentRestorer.Get(GeneralStyles.HighlightResultButton)) { GUILayout.Button("Compiling...", GeneralStyles.BigButton); } } else { EditorGUI.BeginDisabledGroup(NGHotkeys.DetectDiff(true) == false); if (GUILayout.Button("Save", GeneralStyles.BigButton) == true) { NGHotkeys.Generate(); HQ.InvalidateSettings(HQ.Settings, true); } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); Rect r2 = GUILayoutUtility.GetLastRect(); r2.x = 2F; r2.width += 13F; r2.yMin = r2.yMax - 1F; EditorGUI.DrawRect(r2, Color.gray); CustomHotkeysSettings settings = HQ.Settings.Get <CustomHotkeysSettings>(); NGHotkeys.scrollPosition = EditorGUILayout.BeginScrollView(NGHotkeys.scrollPosition); { for (int i = 0; i < NGHotkeys.shortcuts.Count; i++) { EditorGUILayout.BeginHorizontal(); { MenuItemShortcut shortcut = NGHotkeys.shortcuts[i]; CustomHotkeysSettings.MethodHotkey hotkey = null; for (int j = 0; j < settings.hotkeys.Count; j++) { if (settings.hotkeys[j].staticMethod == [email protected] + '.' + shortcut.staticMethod) { hotkey = settings.hotkeys[j]; break; } } Utility.content.text = shortcut.name; Rect r = GUILayoutUtility.GetRect(Utility.content, GUI.skin.label, GUILayoutOptionPool.Width(170F)); EditorGUI.PrefixLabel(r, Utility.content); if (hotkey != null && string.IsNullOrEmpty(hotkey.bind) == false) { float w = r.width; r.x -= 1F; r.width = 1F; EditorGUI.DrawRect(r, Color.cyan); r.x += 1F; r.width = w; } bool hasChanged = false; EditorGUI.BeginChangeCheck(); r.x += r.width; r.width = 50F; string bind = EditorGUI.TextField(r, hotkey != null ? hotkey.bind : string.Empty); if (EditorGUI.EndChangeCheck() == true) { hasChanged = true; } r.x += r.width; r.width = 50F; EditorGUI.BeginChangeCheck(); GUI.Toggle(r, bind.Contains("%"), "Ctrl", GeneralStyles.ToolbarToggle); if (EditorGUI.EndChangeCheck() == true) { GUI.FocusControl(null); hasChanged = true; int n = bind.IndexOf('%'); if (n != -1) { bind = bind.Remove(n, 1); } else { bind = '%' + bind; } } r.x += r.width; EditorGUI.BeginChangeCheck(); GUI.Toggle(r, bind.Contains("#"), "Shift", GeneralStyles.ToolbarToggle); if (EditorGUI.EndChangeCheck() == true) { GUI.FocusControl(null); hasChanged = true; int n = bind.IndexOf('#'); if (n != -1) { bind = bind.Remove(n, 1); } else { bind = '#' + bind; } } r.x += r.width; EditorGUI.BeginChangeCheck(); GUI.Toggle(r, bind.Contains("&"), "Alt", GeneralStyles.ToolbarToggle); if (EditorGUI.EndChangeCheck() == true) { GUI.FocusControl(null); hasChanged = true; int n = bind.IndexOf('&'); if (n != -1) { bind = bind.Remove(n, 1); } else { bind = '&' + bind; } } if (hasChanged == true) { if (string.IsNullOrEmpty(bind) == true) { if (hotkey != null) { settings.hotkeys.Remove(hotkey); } } else { if (hotkey == null) { settings.hotkeys.Add(new CustomHotkeysSettings.MethodHotkey { staticMethod = [email protected] + '.' + shortcut.staticMethod, bind = bind }); } else { hotkey.bind = bind; } } } GUILayout.FlexibleSpace(); } EditorGUILayout.EndHorizontal(); GUILayout.Space(5F); } } EditorGUILayout.EndScrollView(); }
private static bool DetectDiff(bool silent) { if (HQ.Settings == null || NGHotkeys.isGenerating == true) { return(false); } CustomHotkeysSettings settings = HQ.Settings.Get <CustomHotkeysSettings>(); bool isDifferent = false; Type type = Utility.GetType("NGToolsEditor", "CustomHotkeys"); if (type != null) { MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static); if (methods.Length != settings.hotkeys.Count) { isDifferent = true; } else { MenuItem[][] attributes = new MenuItem[methods.Length][]; for (int i = 0; i < methods.Length; i++) { attributes[i] = methods[i].GetCustomAttributes(typeof(MenuItem), false) as MenuItem[]; } for (int i = 0; i < settings.hotkeys.Count; i++) { CustomHotkeysSettings.MethodHotkey binding = settings.hotkeys[i]; for (int j = 0; j < NGHotkeys.shortcuts.Count; j++) { MenuItemShortcut shortcut = NGHotkeys.shortcuts[j]; if ([email protected] + '.' + shortcut.staticMethod == binding.staticMethod) { for (int k = 0; k < attributes.Length; k++) { for (int l = 0; l < attributes[k].Length; l++) { string s = Constants.MenuItemPath + NGHotkeys.SubMenuItemPath + shortcut.name + " _"; if (attributes[k][l].menuItem.StartsWith(s) == true) { if (attributes[k][l].menuItem.Substring(s.Length) != binding.bind) { isDifferent = true; goto quadrupleBreaks; } goto doubleBreaks; } } } doubleBreaks: break; } } } } } else if (settings.hotkeys.Count > 0) { isDifferent = true; } quadrupleBreaks: if (isDifferent == true && silent == false) { EditorApplication.delayCall += () => { if (EditorUtility.DisplayDialog(NGHotkeys.Title, "The current hotkeys bindings do not match your settings.\nThis might happen after an update of NG Tools.\n\nDo you want to restore your setup?", LC.G("Yes"), LC.G("No")) == true) { NGHotkeys.isGenerating = true; NGHotkeys.Generate(); HQ.InvalidateSettings(HQ.Settings, true); } }; } return(isDifferent); }
public void OnGUI() { if (HQ.Settings == null) { this.so = null; GUILayout.Label(LC.G("ConsoleSettings_NullTarget")); return; } try { if (this.so == null || this.so.targetObject == null || this.so.targetObject != HQ.Settings.Get(this.typeSetting) as ScriptableObject) { this.so = new SerializedObject(HQ.Settings.Get(this.typeSetting) as ScriptableObject); } else { this.so.Update(); } } catch (Exception ex) { InternalNGDebug.LogException("Setting " + this.typeSetting + " is failing. (" + HQ.Settings.Get(this.typeSetting) + ")", ex); return; } GUILayout.BeginHorizontal(); { GUILayout.FlexibleSpace(); if (GUILayout.Button("Reset", GUILayoutOptionPool.ExpandWidthFalse) == true && ((Event.current.modifiers & Constants.ByPassPromptModifier) != 0 || EditorUtility.DisplayDialog(NGSettingsWindow.Title, LC.G("ConsoleSettings_ResetConfirm"), LC.G("Yes"), LC.G("No")) == true)) { if (this.typeSetting.IsSubclassOf(typeof(ScriptableObject)) == true) { this.so = null; // Delete the current settings. HQ.Settings.Clear(this.typeSetting); // Then regenerate it to ensure it exists and is called from OnGUI context. HQ.Settings.Get(this.typeSetting); } else { object settings = Activator.CreateInstance(this.fieldInfo.FieldType); this.fieldInfo.SetValue(HQ.Settings, settings); } HQ.InvalidateSettings(); InternalEditorUtility.RepaintAllViews(); return; } } GUILayout.EndHorizontal(); if (this.typeSetting.IsSubclassOf(typeof(ScriptableObject)) == true) { SerializedProperty iterator = this.so.GetIterator(); iterator.NextVisible(true); EditorGUI.BeginChangeCheck(); while (iterator.NextVisible(false) == true) { EditorGUILayout.PropertyField(iterator, true); } if (EditorGUI.EndChangeCheck() == true) { this.so.ApplyModifiedProperties(); HQ.InvalidateSettings(); } } else { SerializedProperty iterator = this.so.FindProperty(this.fieldInfo.Name); SerializedProperty end = iterator.GetEndProperty(); bool enterChildren = true; EditorGUI.BeginChangeCheck(); while (iterator.NextVisible(enterChildren) == true && SerializedProperty.EqualContents(iterator, end) == false) { EditorGUILayout.PropertyField(iterator, true); enterChildren = false; } if (EditorGUI.EndChangeCheck() == true) { this.so.ApplyModifiedProperties(); HQ.InvalidateSettings(); } } }