|
7 | 7 |
|
8 | 8 | namespace Toolbox.Editor |
9 | 9 | { |
| 10 | + using Toolbox.Attributes; |
10 | 11 | using Toolbox.Editor.Drawers; |
11 | 12 |
|
12 | 13 | /// <summary> |
@@ -83,6 +84,12 @@ internal class ToolboxPropertyHandler |
83 | 84 | /// </summary> |
84 | 85 | private bool hasToolboxConditionDrawer; |
85 | 86 |
|
| 87 | + /// <summary> |
| 88 | + /// Temporary solution to cache all attributes responsible for overriding label. |
| 89 | + /// This will be replaced with validation attributes. |
| 90 | + /// </summary> |
| 91 | + private ILabelProcessorAttribute labelProcessorAttribute; |
| 92 | + |
86 | 93 |
|
87 | 94 | /// <summary> |
88 | 95 | /// Constructor prepares all property-related data for custom drawing. |
@@ -166,19 +173,28 @@ private void CheckIfPropertyHasPropertyDrawer(Type type) |
166 | 173 |
|
167 | 174 | private void HandleNewAttribute(PropertyAttribute attribute) |
168 | 175 | { |
169 | | - if (!isChild) |
| 176 | + //NOTE: setting tooltip and labels is valid only for parent or single properties |
| 177 | + //it's a bit ugly but, it's the only semi-acceptable way to support built-in TooltipAttribute |
| 178 | + //TODO: move these attributes to validation drawers |
| 179 | + switch (attribute) |
170 | 180 | { |
171 | | - //NOTE: setting tooltip and labels is valid only for parent or single properties |
172 | | - //it's a bit ugly but, it's the only semi-acceptable way to support built-in TooltipAttribute |
173 | | - switch (attribute) |
174 | | - { |
175 | | - case TooltipAttribute a: |
176 | | - label.tooltip = a.tooltip; |
177 | | - return; |
178 | | - case NewLabelAttribute a: |
179 | | - label.text = a.NewLabel; |
180 | | - return; |
181 | | - } |
| 181 | + case TooltipAttribute a: |
| 182 | + label.tooltip = a.tooltip; |
| 183 | + return; |
| 184 | + case NewLabelAttribute a: |
| 185 | + if (!isChild) |
| 186 | + { |
| 187 | + labelProcessorAttribute = a; |
| 188 | + } |
| 189 | + |
| 190 | + return; |
| 191 | + case LabelByChildAttribute a: |
| 192 | + if (!isArray) |
| 193 | + { |
| 194 | + labelProcessorAttribute = a; |
| 195 | + } |
| 196 | + |
| 197 | + return; |
182 | 198 | } |
183 | 199 |
|
184 | 200 | var attributeType = attribute.GetType(); |
@@ -269,6 +285,8 @@ private bool TryAssignConditionAttribute(ToolboxConditionAttribute attribute) |
269 | 285 |
|
270 | 286 | private void DrawProperty(SerializedProperty property, GUIContent label) |
271 | 287 | { |
| 288 | + ProcessLabel(property, label); |
| 289 | + |
272 | 290 | //get toolbox drawer for the property or draw it in the default way |
273 | 291 | if (hasToolboxPropertyDrawer && (!hasBuiltInPropertyDrawer || isArray)) |
274 | 292 | { |
@@ -360,6 +378,25 @@ private PropertyCondition Validate(SerializedProperty property) |
360 | 378 | return ToolboxDrawerModule.GetConditionDrawer(conditionAttribute)?.OnGuiValidate(property, conditionAttribute) ?? PropertyCondition.Valid; |
361 | 379 | } |
362 | 380 |
|
| 381 | + //TODO: replace this method with validation attributes |
| 382 | + private void ProcessLabel(SerializedProperty property, GUIContent label) |
| 383 | + { |
| 384 | + if (labelProcessorAttribute == null) |
| 385 | + { |
| 386 | + return; |
| 387 | + } |
| 388 | + |
| 389 | + switch (labelProcessorAttribute) |
| 390 | + { |
| 391 | + case NewLabelAttribute a: |
| 392 | + label.text = a.NewLabel; |
| 393 | + return; |
| 394 | + case LabelByChildAttribute a: |
| 395 | + PropertyUtility.OverrideLabelByChild(label, property, a.ChildName); |
| 396 | + return; |
| 397 | + } |
| 398 | + } |
| 399 | + |
363 | 400 |
|
364 | 401 | /// <summary> |
365 | 402 | /// Draw property using built-in layout system and cached <see cref="ToolboxAttributeDrawer"/>s. |
|
0 commit comments