mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add DropdownValueResolver
This commit is contained in:
parent
af9d9f561d
commit
69ebc4dc89
@ -1,6 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TriInspector;
|
||||
using TriInspector;
|
||||
using TriInspector.Drawers;
|
||||
using TriInspector.Elements;
|
||||
using TriInspector.Resolvers;
|
||||
@ -11,22 +9,15 @@ namespace TriInspector.Drawers
|
||||
{
|
||||
public class DropdownDrawer<T> : TriAttributeDrawer<DropdownAttribute>
|
||||
{
|
||||
private ValueResolver<IEnumerable<TriDropdownItem<T>>> _itemsResolver;
|
||||
private ValueResolver<IEnumerable<T>> _valuesResolver;
|
||||
private DropdownValuesResolver<T> _valuesResolver;
|
||||
|
||||
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
|
||||
{
|
||||
_valuesResolver = ValueResolver.Resolve<IEnumerable<T>>(propertyDefinition, Attribute.Values);
|
||||
_valuesResolver = DropdownValuesResolver<T>.Resolve(propertyDefinition, Attribute.Values);
|
||||
|
||||
if (_valuesResolver.TryGetErrorString(out _))
|
||||
if (_valuesResolver.TryGetErrorString(out var error))
|
||||
{
|
||||
_itemsResolver =
|
||||
ValueResolver.Resolve<IEnumerable<TriDropdownItem<T>>>(propertyDefinition, Attribute.Values);
|
||||
|
||||
if (_itemsResolver.TryGetErrorString(out var itemResolverError))
|
||||
{
|
||||
return itemResolverError;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
return TriExtensionInitializationResult.Ok;
|
||||
@ -34,30 +25,7 @@ namespace TriInspector.Drawers
|
||||
|
||||
public override TriElement CreateElement(TriProperty property, TriElement next)
|
||||
{
|
||||
return new TriDropdownElement(property, GetDropdownItems);
|
||||
}
|
||||
|
||||
private IEnumerable<ITriDropdownItem> GetDropdownItems(TriProperty property)
|
||||
{
|
||||
if (_valuesResolver != null)
|
||||
{
|
||||
var values = _valuesResolver.GetValue(property, Enumerable.Empty<T>());
|
||||
|
||||
foreach (var value in values)
|
||||
{
|
||||
yield return new TriDropdownItem {Text = $"{value}", Value = value,};
|
||||
}
|
||||
}
|
||||
|
||||
if (_itemsResolver != null)
|
||||
{
|
||||
var values = _itemsResolver.GetValue(property, Enumerable.Empty<TriDropdownItem<T>>());
|
||||
|
||||
foreach (var value in values)
|
||||
{
|
||||
yield return value;
|
||||
}
|
||||
}
|
||||
return new TriDropdownElement(property, _valuesResolver.GetDropdownItems);
|
||||
}
|
||||
}
|
||||
}
|
62
Editor/Resolvers/DropdownValuesResolver.cs
Normal file
62
Editor/Resolvers/DropdownValuesResolver.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace TriInspector.Resolvers
|
||||
{
|
||||
public class DropdownValuesResolver<T>
|
||||
{
|
||||
[CanBeNull] private ValueResolver<IEnumerable<TriDropdownItem<T>>> _itemsResolver;
|
||||
[CanBeNull] private ValueResolver<IEnumerable<T>> _valuesResolver;
|
||||
|
||||
[PublicAPI]
|
||||
public static DropdownValuesResolver<T> Resolve(TriPropertyDefinition propertyDefinition, string expression)
|
||||
{
|
||||
var valuesResolver = ValueResolver.Resolve<IEnumerable<T>>(propertyDefinition, expression);
|
||||
if (!valuesResolver.TryGetErrorString(out _))
|
||||
{
|
||||
return new DropdownValuesResolver<T>
|
||||
{
|
||||
_valuesResolver = valuesResolver,
|
||||
};
|
||||
}
|
||||
|
||||
var itemsResolver = ValueResolver.Resolve<IEnumerable<TriDropdownItem<T>>>(propertyDefinition, expression);
|
||||
|
||||
return new DropdownValuesResolver<T>
|
||||
{
|
||||
_itemsResolver = itemsResolver,
|
||||
};
|
||||
}
|
||||
|
||||
[PublicAPI]
|
||||
public bool TryGetErrorString(out string error)
|
||||
{
|
||||
return ValueResolver.TryGetErrorString(_valuesResolver, _itemsResolver, out error);
|
||||
}
|
||||
|
||||
[PublicAPI]
|
||||
public IEnumerable<ITriDropdownItem> GetDropdownItems(TriProperty property)
|
||||
{
|
||||
if (_valuesResolver != null)
|
||||
{
|
||||
var values = _valuesResolver.GetValue(property, Enumerable.Empty<T>());
|
||||
|
||||
foreach (var value in values)
|
||||
{
|
||||
yield return new TriDropdownItem {Text = $"{value}", Value = value,};
|
||||
}
|
||||
}
|
||||
|
||||
if (_itemsResolver != null)
|
||||
{
|
||||
var values = _itemsResolver.GetValue(property, Enumerable.Empty<TriDropdownItem<T>>());
|
||||
|
||||
foreach (var value in values)
|
||||
{
|
||||
yield return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Resolvers/DropdownValuesResolver.cs.meta
Normal file
3
Editor/Resolvers/DropdownValuesResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16fa8c4711a44bdea6facb3bc68822d9
|
||||
timeCreated: 1680713564
|
Loading…
Reference in New Issue
Block a user