Optimize GUIColorDrawer

This commit is contained in:
VladV 2023-05-01 16:17:43 +04:00
parent 9d0f5faeab
commit a292fece70

View File

@ -1,4 +1,5 @@
using TriInspector;
using JetBrains.Annotations;
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Resolvers;
using UnityEngine;
@ -9,21 +10,27 @@ namespace TriInspector.Drawers
{
public class GUIColorDrawer : TriAttributeDrawer<GUIColorAttribute>
{
[CanBeNull] private ValueResolver<Color> _colorResolver;
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
if (!string.IsNullOrEmpty(Attribute.GetColor))
{
_colorResolver = ValueResolver.Resolve<Color>(propertyDefinition, Attribute.GetColor);
}
if (_colorResolver != null && _colorResolver.TryGetErrorString(out var error))
{
return error;
}
return TriExtensionInitializationResult.Ok;
}
public override void OnGUI(Rect position, TriProperty property, TriElement next)
{
var oldColor = GUI.color;
var newColor = Color.white;
if (string.IsNullOrEmpty(Attribute.GetColor))
{
newColor = Attribute.Color;
}
else
{
var colorResolver = ValueResolver.Resolve<Color>(property.Definition, Attribute.GetColor ?? "");
newColor = colorResolver.GetValue(property, Color.white);
}
var newColor = _colorResolver?.GetValue(property, Color.white) ?? Attribute.Color;
GUI.color = newColor;
GUI.contentColor = newColor;