|
| 1 | +using System; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +internal abstract class BaseExposedPropertyDrawer : PropertyDrawer |
| 6 | +{ |
| 7 | + protected enum ExposedPropertyMode |
| 8 | + { |
| 9 | + DefaultValue, |
| 10 | + Named, |
| 11 | + NamedGUID |
| 12 | + } |
| 13 | + |
| 14 | + protected enum OverrideState |
| 15 | + { |
| 16 | + DefaultValue, |
| 17 | + MissingOverride, |
| 18 | + Overridden |
| 19 | + } |
| 20 | + |
| 21 | + private static float kDriveWidgetWidth = 18f; |
| 22 | + |
| 23 | + private static GUIStyle kDropDownStyle = null; |
| 24 | + |
| 25 | + private static Color kMissingOverrideColor = new Color(1f, 0.11f, 0.11f, 1f); |
| 26 | + |
| 27 | + protected readonly GUIContent ExposePropertyContent = EditorGUIUtility.TextContent("Expose Property"); |
| 28 | + |
| 29 | + protected readonly GUIContent UnexposePropertyContent = EditorGUIUtility.TextContent("Unexpose Property"); |
| 30 | + |
| 31 | + protected readonly GUIContent NotFoundOn = EditorGUIUtility.TextContent("not found on"); |
| 32 | + |
| 33 | + protected readonly GUIContent OverridenByContent = EditorGUIUtility.TextContent("Overriden by "); |
| 34 | + |
| 35 | + private GUIContent m_ModifiedLabel = new GUIContent(); |
| 36 | + |
| 37 | + public BaseExposedPropertyDrawer() |
| 38 | + { |
| 39 | + if (BaseExposedPropertyDrawer.kDropDownStyle == null) |
| 40 | + { |
| 41 | + BaseExposedPropertyDrawer.kDropDownStyle = new GUIStyle("ShurikenDropdown"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static BaseExposedPropertyDrawer.ExposedPropertyMode GetExposedPropertyMode(string propertyName) |
| 46 | + { |
| 47 | + BaseExposedPropertyDrawer.ExposedPropertyMode result; |
| 48 | + GUID gUID; |
| 49 | + if (string.IsNullOrEmpty(propertyName)) |
| 50 | + { |
| 51 | + result = BaseExposedPropertyDrawer.ExposedPropertyMode.DefaultValue; |
| 52 | + } |
| 53 | + else if (GUID.TryParse(propertyName, out gUID)) |
| 54 | + { |
| 55 | + result = BaseExposedPropertyDrawer.ExposedPropertyMode.NamedGUID; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + result = BaseExposedPropertyDrawer.ExposedPropertyMode.Named; |
| 60 | + } |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + protected IExposedPropertyTable GetExposedPropertyTable(SerializedProperty property) |
| 65 | + { |
| 66 | + UnityEngine.Object context = property.serializedObject.context; |
| 67 | + return context as IExposedPropertyTable; |
| 68 | + } |
| 69 | + |
| 70 | + protected abstract void OnRenderProperty(Rect position, PropertyName exposedPropertyNameString, UnityEngine.Object currentReferenceValue, SerializedProperty exposedPropertyDefault, SerializedProperty exposedPropertyName, BaseExposedPropertyDrawer.ExposedPropertyMode mode, IExposedPropertyTable exposedProperties); |
| 71 | + |
| 72 | + public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) |
| 73 | + { |
| 74 | + SerializedProperty serializedProperty = prop.FindPropertyRelative("defaultValue"); |
| 75 | + SerializedProperty serializedProperty2 = prop.FindPropertyRelative("exposedName"); |
| 76 | + string text = serializedProperty2.stringValue; |
| 77 | + BaseExposedPropertyDrawer.ExposedPropertyMode exposedPropertyMode = BaseExposedPropertyDrawer.GetExposedPropertyMode(text); |
| 78 | + Rect rect = position; |
| 79 | + rect.xMax -= BaseExposedPropertyDrawer.kDriveWidgetWidth; |
| 80 | + Rect position2 = position; |
| 81 | + position2.x = rect.xMax; |
| 82 | + position2.width = BaseExposedPropertyDrawer.kDriveWidgetWidth; |
| 83 | + IExposedPropertyTable exposedPropertyTable = this.GetExposedPropertyTable(prop); |
| 84 | + bool flag = exposedPropertyTable != null; |
| 85 | + PropertyName propertyName = new PropertyName(text); |
| 86 | + BaseExposedPropertyDrawer.OverrideState overrideState = BaseExposedPropertyDrawer.OverrideState.DefaultValue; |
| 87 | + UnityEngine.Object currentReferenceValue = this.Resolve(propertyName, exposedPropertyTable, serializedProperty.objectReferenceValue, out overrideState); |
| 88 | + Color color = GUI.color; |
| 89 | + bool boldDefaultFont = EditorGUIUtility.GetBoldDefaultFont(); |
| 90 | + Rect position3 = this.DrawLabel(flag, overrideState, label, position, exposedPropertyTable, text, serializedProperty2, serializedProperty); |
| 91 | + EditorGUI.BeginChangeCheck(); |
| 92 | + if (exposedPropertyMode == BaseExposedPropertyDrawer.ExposedPropertyMode.DefaultValue || exposedPropertyMode == BaseExposedPropertyDrawer.ExposedPropertyMode.NamedGUID) |
| 93 | + { |
| 94 | + this.OnRenderProperty(position3, propertyName, currentReferenceValue, serializedProperty, serializedProperty2, exposedPropertyMode, exposedPropertyTable); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + position3.width /= 2f; |
| 99 | + EditorGUI.BeginChangeCheck(); |
| 100 | + text = EditorGUI.TextField(position3, text); |
| 101 | + if (EditorGUI.EndChangeCheck()) |
| 102 | + { |
| 103 | + serializedProperty2.stringValue = text; |
| 104 | + } |
| 105 | + position3.x += position3.width; |
| 106 | + this.OnRenderProperty(position3, new PropertyName(text), currentReferenceValue, serializedProperty, serializedProperty2, exposedPropertyMode, exposedPropertyTable); |
| 107 | + } |
| 108 | + EditorGUI.EndDisabledGroup(); |
| 109 | + GUI.color = color; |
| 110 | + EditorGUIUtility.SetBoldDefaultFont(boldDefaultFont); |
| 111 | + if (flag && GUI.Button(position2, GUIContent.none, BaseExposedPropertyDrawer.kDropDownStyle)) |
| 112 | + { |
| 113 | + GenericMenu genericMenu = new GenericMenu(); |
| 114 | + this.PopulateContextMenu(genericMenu, overrideState, exposedPropertyTable, serializedProperty2, serializedProperty); |
| 115 | + genericMenu.ShowAsContext(); |
| 116 | + Event.current.Use(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private Rect DrawLabel(bool showContextMenu, BaseExposedPropertyDrawer.OverrideState currentOverrideState, GUIContent label, Rect position, IExposedPropertyTable exposedPropertyTable, string exposedNameStr, SerializedProperty exposedName, SerializedProperty defaultValue) |
| 121 | + { |
| 122 | + if (showContextMenu) |
| 123 | + { |
| 124 | + position.xMax -= BaseExposedPropertyDrawer.kDriveWidgetWidth; |
| 125 | + } |
| 126 | + EditorGUIUtility.SetBoldDefaultFont(currentOverrideState != BaseExposedPropertyDrawer.OverrideState.DefaultValue); |
| 127 | + this.m_ModifiedLabel.text = label.text; |
| 128 | + this.m_ModifiedLabel.tooltip = label.tooltip; |
| 129 | + this.m_ModifiedLabel.image = label.image; |
| 130 | + if (!string.IsNullOrEmpty(this.m_ModifiedLabel.tooltip)) |
| 131 | + { |
| 132 | + GUIContent expr_78 = this.m_ModifiedLabel; |
| 133 | + expr_78.tooltip += "\n"; |
| 134 | + } |
| 135 | + if (currentOverrideState == BaseExposedPropertyDrawer.OverrideState.MissingOverride) |
| 136 | + { |
| 137 | + GUI.color = BaseExposedPropertyDrawer.kMissingOverrideColor; |
| 138 | + GUIContent expr_A6 = this.m_ModifiedLabel; |
| 139 | + string tooltip = expr_A6.tooltip; |
| 140 | + expr_A6.tooltip = string.Concat(new string[] |
| 141 | + { |
| 142 | + tooltip, |
| 143 | + label.text, |
| 144 | + " ", |
| 145 | + this.NotFoundOn.text, |
| 146 | + " ", |
| 147 | + exposedPropertyTable.ToString(), |
| 148 | + "." |
| 149 | + }); |
| 150 | + } |
| 151 | + else if (currentOverrideState == BaseExposedPropertyDrawer.OverrideState.Overridden && exposedPropertyTable != null) |
| 152 | + { |
| 153 | + GUIContent expr_115 = this.m_ModifiedLabel; |
| 154 | + expr_115.tooltip = expr_115.tooltip + this.OverridenByContent.text + exposedPropertyTable.ToString() + "."; |
| 155 | + } |
| 156 | + Rect result = EditorGUI.PrefixLabel(position, this.m_ModifiedLabel); |
| 157 | + if (exposedPropertyTable != null && Event.current.type == EventType.ContextClick) |
| 158 | + { |
| 159 | + if (position.Contains(Event.current.mousePosition)) |
| 160 | + { |
| 161 | + GenericMenu genericMenu = new GenericMenu(); |
| 162 | + this.PopulateContextMenu(genericMenu, (!string.IsNullOrEmpty(exposedNameStr)) ? BaseExposedPropertyDrawer.OverrideState.Overridden : BaseExposedPropertyDrawer.OverrideState.DefaultValue, exposedPropertyTable, exposedName, defaultValue); |
| 163 | + genericMenu.ShowAsContext(); |
| 164 | + } |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + protected UnityEngine.Object Resolve(PropertyName exposedPropertyName, IExposedPropertyTable exposedPropertyTable, UnityEngine.Object defaultValue, out BaseExposedPropertyDrawer.OverrideState currentOverrideState) |
| 170 | + { |
| 171 | + UnityEngine.Object @object = null; |
| 172 | + bool flag = false; |
| 173 | + bool flag2 = !PropertyName.IsNullOrEmpty(exposedPropertyName); |
| 174 | + currentOverrideState = BaseExposedPropertyDrawer.OverrideState.DefaultValue; |
| 175 | + if (exposedPropertyTable != null) |
| 176 | + { |
| 177 | + @object = exposedPropertyTable.GetReferenceValue(exposedPropertyName, out flag); |
| 178 | + if (flag) |
| 179 | + { |
| 180 | + currentOverrideState = BaseExposedPropertyDrawer.OverrideState.Overridden; |
| 181 | + } |
| 182 | + else if (flag2) |
| 183 | + { |
| 184 | + currentOverrideState = BaseExposedPropertyDrawer.OverrideState.MissingOverride; |
| 185 | + } |
| 186 | + } |
| 187 | + return (currentOverrideState != BaseExposedPropertyDrawer.OverrideState.Overridden) ? defaultValue : @object; |
| 188 | + } |
| 189 | + |
| 190 | + protected abstract void PopulateContextMenu(GenericMenu menu, BaseExposedPropertyDrawer.OverrideState overrideState, IExposedPropertyTable exposedPropertyTable, SerializedProperty exposedName, SerializedProperty defaultValue); |
| 191 | +} |
0 commit comments