A game about forced loneliness, made by TACStudios
at master 138 lines 5.2 kB view raw
1using System; 2using UnityEditor.AnimatedValues; 3using UnityEngine; 4 5namespace UnityEditor.Rendering 6{ 7 /// <summary> 8 /// Global accessor to advanced properties 9 /// </summary> 10 public static class AdvancedProperties 11 { 12 static class Keys 13 { 14 // TODO Deprecate this key in U7: Advanced properties were formerly called additional properties 15 internal const string showAllAdditionalProperties = "General.ShowAllAdditionalProperties"; 16 internal const string advancedPropertiesMigrated = "General.LocalAdditionalPropertiesMigratedToGlobal"; 17 //END TODO 18 19 internal const string showAdvancedProperties = "General.ShowAdvancedProperties"; 20 } 21 22 // TODO Deprecate this in U7: Advanced properties were formerly called additional properties 23 static AdvancedProperties() 24 { 25 // Migrate from the previous global state 26 UpdateShowAdvancedProperties(Keys.showAllAdditionalProperties, 27 EditorPrefs.HasKey(Keys.showAllAdditionalProperties) && 28 EditorPrefs.GetBool(Keys.showAllAdditionalProperties)); 29 } 30 31 internal static void UpdateShowAdvancedProperties(string key, bool previousState) 32 { 33 if (previousState) 34 { 35 if (!EditorPrefs.HasKey(Keys.advancedPropertiesMigrated) || !EditorPrefs.GetBool(Keys.advancedPropertiesMigrated)) 36 { 37 // Before we were storing a global state and a per editor state. 38 // So if the user had at least 1 editor with show additional, we need to show advanced properties everywhere. 39 enabled = true; 40 EditorPrefs.SetBool(Keys.advancedPropertiesMigrated, true); 41 } 42 } 43 44 if (EditorPrefs.HasKey(key)) 45 EditorPrefs.DeleteKey(key); 46 } 47 // END TODO 48 49 /// <summary> 50 /// Global event when the advanced preferences have changed 51 /// </summary> 52 public static event Action<bool> advancedPreferenceChanged; 53 54 private static bool? s_ShowAdvanced; 55 56 /// <summary> 57 /// If the show advanced properties is enabled 58 /// </summary> 59 public static bool enabled 60 { 61 get 62 { 63 s_ShowAdvanced ??= EditorPrefs.GetBool(Keys.showAdvancedProperties, false); 64 return s_ShowAdvanced.Value; 65 } 66 set 67 { 68 if (s_ShowAdvanced != value) 69 { 70 s_ShowAdvanced = value; 71 EditorPrefs.SetBool(Keys.showAdvancedProperties, value); 72 advancedPreferenceChanged?.Invoke(value); 73 } 74 } 75 } 76 77 /// <summary> 78 /// Adds an entry to toggle Advanced Properties 79 /// </summary> 80 /// <param name="menu">The menu where to add the Advanced Properties entry.</param> 81 /// <param name="hasMoreOptions">If the option is checked</param> 82 /// <param name="toggleMoreOptions">The toggle action</param> 83 public static void AddAdvancedPropertiesBoolMenuItem(this GenericMenu menu, Func<bool> hasMoreOptions, Action toggleMoreOptions) 84 { 85 menu.AddItem(EditorGUIUtility.TrTextContent("Advanced Properties"), hasMoreOptions.Invoke(), () => toggleMoreOptions.Invoke()); 86 } 87 88 /// <summary> 89 /// Adds an entry to toggle Advanced Properties 90 /// </summary> 91 /// <param name="menu">The menu where to add the Advanced Properties entry.</param> 92 public static void AddAdvancedPropertiesBoolMenuItem(this GenericMenu menu) 93 { 94 AddAdvancedPropertiesBoolMenuItem(menu, 95 () => AdvancedProperties.enabled, 96 () => AdvancedProperties.enabled = !AdvancedProperties.enabled); 97 } 98 99 internal static AnimFloat s_AnimFloat = new(0) 100 { 101 speed = 0.2f 102 }; 103 104 internal static void ResetHighlight() 105 { 106 s_AnimFloat.value = 1.0f; 107 s_AnimFloat.target = 0.0f; 108 } 109 110 internal static bool IsHighlightActive() => s_AnimFloat.isAnimating; 111 112 /// <summary> 113 /// Starts the Advanced Properties highlight 114 /// </summary> 115 /// <param name="animation">The animation of the highlight. If null, the global animation value is used.</param> 116 /// <returns>Tru, if the advanced properties is enabled</returns> 117 public static bool BeginGroup(AnimFloat animation = null) 118 { 119 var oldColor = GUI.color; 120 121 animation ??= s_AnimFloat; 122 123 GUI.color = Color.Lerp(CoreEditorStyles.backgroundColor * oldColor, CoreEditorStyles.backgroundHighlightColor, animation.value); 124 EditorGUILayout.BeginVertical(CoreEditorStyles.additionalPropertiesHighlightStyle); 125 GUI.color = oldColor; 126 127 return AdvancedProperties.enabled; 128 } 129 130 /// <summary> 131 /// Ends the scope of highlight of advanced properties 132 /// </summary> 133 public static void EndGroup() 134 { 135 EditorGUILayout.EndVertical(); 136 } 137 } 138}