Skip to content

Commit af9d9f5

Browse files
committed
Actualize and set dropdown value in OnGUI loop
1 parent 78d032d commit af9d9f5

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

Editor/Elements/TriDropdownElement.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,43 @@ public class TriDropdownElement : TriElement
1111
private readonly TriProperty _property;
1212
private readonly Func<TriProperty, IEnumerable<ITriDropdownItem>> _valuesGetter;
1313

14+
private object _currentValue;
1415
private string _currentText;
1516

17+
private bool _hasNextValue;
18+
private object _nextValue;
19+
1620
public TriDropdownElement(TriProperty property, Func<TriProperty, IEnumerable<ITriDropdownItem>> valuesGetter)
1721
{
1822
_property = property;
1923
_valuesGetter = valuesGetter;
2024
}
2125

22-
protected override void OnAttachToPanel()
23-
{
24-
base.OnAttachToPanel();
25-
26-
_property.ValueChanged += OnValueChanged;
27-
28-
RefreshCurrentText();
29-
}
30-
31-
protected override void OnDetachFromPanel()
32-
{
33-
_property.ValueChanged -= OnValueChanged;
34-
35-
base.OnDetachFromPanel();
36-
}
37-
3826
public override float GetHeight(float width)
3927
{
4028
return EditorGUIUtility.singleLineHeight;
4129
}
4230

4331
public override void OnGUI(Rect position)
4432
{
33+
if (_hasNextValue)
34+
{
35+
var nextValue = _nextValue;
36+
_hasNextValue = false;
37+
_nextValue = null;
38+
39+
_property.SetValue(nextValue);
40+
GUI.changed = true;
41+
}
42+
43+
if (!_property.Comparer.Equals(_currentValue, _property.Value))
44+
{
45+
_currentValue = _property.Value;
46+
_currentText = _valuesGetter.Invoke(_property)
47+
.FirstOrDefault(it => _property.Comparer.Equals(it.Value, _property.Value))
48+
?.Text ?? "";
49+
}
50+
4551
var controlId = GUIUtility.GetControlID(FocusType.Passive);
4652
position = EditorGUI.PrefixLabel(position, controlId, _property.DisplayNameContent);
4753

@@ -51,20 +57,6 @@ public override void OnGUI(Rect position)
5157
}
5258
}
5359

54-
private void OnValueChanged(TriProperty property)
55-
{
56-
RefreshCurrentText();
57-
}
58-
59-
private void RefreshCurrentText()
60-
{
61-
var items = _valuesGetter.Invoke(_property);
62-
63-
_currentText = items
64-
.FirstOrDefault(it => _property.Comparer.Equals(it.Value, _property.Value))
65-
?.Text ?? "";
66-
}
67-
6860
private void ShowDropdown(Rect position)
6961
{
7062
var items = _valuesGetter.Invoke(_property);
@@ -73,10 +65,17 @@ private void ShowDropdown(Rect position)
7365
foreach (var item in items)
7466
{
7567
var isOn = _property.Comparer.Equals(item.Value, _property.Value);
76-
menu.AddItem(new GUIContent(item.Text), isOn, _property.SetValue, item.Value);
68+
menu.AddItem(new GUIContent(item.Text), isOn, ChangeValue, item.Value);
7769
}
7870

7971
menu.DropDown(position);
72+
73+
void ChangeValue(object v)
74+
{
75+
_nextValue = v;
76+
_hasNextValue = true;
77+
_property.PropertyTree.RequestRepaint();
78+
}
8079
}
8180
}
8281
}

0 commit comments

Comments
 (0)