mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Move TriDropdownElement to core
This commit is contained in:
parent
5c459096f7
commit
a17cfcf39d
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
82
Editor/Elements/TriDropdownElement.cs
Normal file
82
Editor/Elements/TriDropdownElement.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Elements/TriDropdownElement.cs.meta
Normal file
3
Editor/Elements/TriDropdownElement.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bfe47c1836e47c48920fb74982c89cb
|
||||
timeCreated: 1657812638
|
Loading…
Reference in New Issue
Block a user