Skip to content

Commit 9235e22

Browse files
committed
Improved toolbar capabilities
1 parent f90d805 commit 9235e22

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/ToolbarView.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ protected enum ElementType
1919
Button,
2020
Toggle,
2121
DropDownButton,
22+
Separator,
23+
Custom,
24+
FlexibleSpace,
2225
}
2326

2427
protected class ToolbarButtonData
@@ -29,6 +32,8 @@ protected class ToolbarButtonData
2932
public bool visible = true;
3033
public Action buttonCallback;
3134
public Action< bool > toggleCallback;
35+
public int size;
36+
public Action customDrawFunction;
3237
}
3338

3439
List< ToolbarButtonData > leftButtonDatas = new List< ToolbarButtonData >();
@@ -66,6 +71,32 @@ protected ToolbarButtonData AddButton(GUIContent content, Action callback, bool
6671
return data;
6772
}
6873

74+
protected void AddSeparator(int sizeInPixels = 10, bool left = true)
75+
{
76+
var data = new ToolbarButtonData{
77+
type = ElementType.Separator,
78+
size = sizeInPixels,
79+
};
80+
((left) ? leftButtonDatas : rightButtonDatas).Add(data);
81+
}
82+
83+
protected void AddCustom(Action imguiDrawFunction, bool left = true)
84+
{
85+
if (imguiDrawFunction == null)
86+
throw new ArgumentException("imguiDrawFunction can't be null");
87+
88+
var data = new ToolbarButtonData{
89+
type = ElementType.Custom,
90+
customDrawFunction = imguiDrawFunction,
91+
};
92+
((left) ? leftButtonDatas : rightButtonDatas).Add(data);
93+
}
94+
95+
protected void AddFlexibleSpace(bool left = true)
96+
{
97+
((left) ? leftButtonDatas : rightButtonDatas).Add(new ToolbarButtonData{ type = ElementType.FlexibleSpace });
98+
}
99+
69100
protected ToolbarButtonData AddToggle(string name, bool defaultValue, Action< bool > callback, bool left = true)
70101
=> AddToggle(new GUIContent(name), defaultValue, callback, left);
71102

@@ -112,7 +143,7 @@ protected void RemoveButton(string name, bool left)
112143
protected void HideButton(string name)
113144
{
114145
leftButtonDatas.Concat(rightButtonDatas).All(b => {
115-
if (b.content.text == name)
146+
if (b?.content?.text == name)
116147
b.visible = false;
117148
return true;
118149
});
@@ -125,7 +156,7 @@ protected void HideButton(string name)
125156
protected void ShowButton(string name)
126157
{
127158
leftButtonDatas.Concat(rightButtonDatas).All(b => {
128-
if (b.content.text == name)
159+
if (b?.content?.text == name)
129160
b.visible = true;
130161
return true;
131162
});
@@ -174,6 +205,16 @@ void DrawImGUIButtonList(List< ToolbarButtonData > buttons)
174205
if (EditorGUILayout.DropdownButton(button.content, FocusType.Passive, EditorStyles.toolbarDropDown))
175206
button.buttonCallback();
176207
break;
208+
case ElementType.Separator:
209+
EditorGUILayout.Separator();
210+
EditorGUILayout.Space(button.size);
211+
break;
212+
case ElementType.Custom:
213+
button.customDrawFunction();
214+
break;
215+
case ElementType.FlexibleSpace:
216+
GUILayout.FlexibleSpace();
217+
break;
177218
}
178219
}
179220
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
### Added
1010
- Renamable nodes
11+
- Added an API in the toolbar view to add separators and custom UI fields.
1112

1213
## [1.1.2]
1314

0 commit comments

Comments
 (0)