Move TriDropdownElement to core

This commit is contained in:
VladV 2022-07-14 18:34:09 +03:00
parent 5c459096f7
commit a17cfcf39d
3 changed files with 88 additions and 79 deletions

View File

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
using TriInspector.Resolvers;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriAttributeDrawer(typeof(DropdownDrawer<>), TriDrawerOrder.Decorator)]
@ -36,7 +34,7 @@ namespace TriInspector.Drawers
public override TriElement CreateElement(TriProperty property, TriElement next)
{
return new DropdownElement(property, GetDropdownItems);
return new TriDropdownElement(property, GetDropdownItems);
}
private IEnumerable<ITriDropdownItem> GetDropdownItems(TriProperty property)
@ -61,79 +59,5 @@ namespace TriInspector.Drawers
}
}
}
private class DropdownElement : TriElement
{
private readonly TriProperty _property;
private readonly Func<TriProperty, IEnumerable<ITriDropdownItem>> _valuesGetter;
private string _currentText;
public DropdownElement(TriProperty property, Func<TriProperty, IEnumerable<ITriDropdownItem>> valuesGetter)
{
_property = property;
_valuesGetter = valuesGetter;
}
protected override void OnAttachToPanel()
{
base.OnAttachToPanel();
_property.ValueChanged += OnValueChanged;
RefreshCurrentText();
}
protected override void OnDetachFromPanel()
{
_property.ValueChanged -= OnValueChanged;
base.OnDetachFromPanel();
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var controlId = GUIUtility.GetControlID(FocusType.Passive);
position = EditorGUI.PrefixLabel(position, controlId, _property.DisplayNameContent);
if (GUI.Button(position, _currentText, EditorStyles.popup))
{
ShowDropdown(position);
}
}
private void OnValueChanged(TriProperty property)
{
RefreshCurrentText();
}
private void RefreshCurrentText()
{
var items = _valuesGetter.Invoke(_property);
_currentText = items
.FirstOrDefault(it => _property.Comparer.Equals(it.Value, _property.Value))
?.Text ?? "";
}
private void ShowDropdown(Rect position)
{
var items = _valuesGetter.Invoke(_property);
var menu = new GenericMenu();
foreach (var item in items)
{
var isOn = _property.Comparer.Equals(item.Value, _property.Value);
menu.AddItem(new GUIContent(item.Text), isOn, _property.SetValue, item.Value);
}
menu.DropDown(position);
}
}
}
}

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace TriInspector.Elements
{
public class TriDropdownElement : TriElement
{
private readonly TriProperty _property;
private readonly Func<TriProperty, IEnumerable<ITriDropdownItem>> _valuesGetter;
private string _currentText;
public TriDropdownElement(TriProperty property, Func<TriProperty, IEnumerable<ITriDropdownItem>> valuesGetter)
{
_property = property;
_valuesGetter = valuesGetter;
}
protected override void OnAttachToPanel()
{
base.OnAttachToPanel();
_property.ValueChanged += OnValueChanged;
RefreshCurrentText();
}
protected override void OnDetachFromPanel()
{
_property.ValueChanged -= OnValueChanged;
base.OnDetachFromPanel();
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var controlId = GUIUtility.GetControlID(FocusType.Passive);
position = EditorGUI.PrefixLabel(position, controlId, _property.DisplayNameContent);
if (GUI.Button(position, _currentText, EditorStyles.popup))
{
ShowDropdown(position);
}
}
private void OnValueChanged(TriProperty property)
{
RefreshCurrentText();
}
private void RefreshCurrentText()
{
var items = _valuesGetter.Invoke(_property);
_currentText = items
.FirstOrDefault(it => _property.Comparer.Equals(it.Value, _property.Value))
?.Text ?? "";
}
private void ShowDropdown(Rect position)
{
var items = _valuesGetter.Invoke(_property);
var menu = new GenericMenu();
foreach (var item in items)
{
var isOn = _property.Comparer.Equals(item.Value, _property.Value);
menu.AddItem(new GUIContent(item.Text), isOn, _property.SetValue, item.Value);
}
menu.DropDown(position);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0bfe47c1836e47c48920fb74982c89cb
timeCreated: 1657812638