Skip to content

Commit 0e12191

Browse files
committed
chore: legacy custom made imgui controls.
1 parent 72a7908 commit 0e12191

13 files changed

+442
-1
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using UnityEditor;
4+
5+
namespace StansAssets.Plugins.Editor
6+
{
7+
public static class IMGUIReorderablList
8+
{
9+
static readonly Dictionary<int, bool> s_GlobalFoldoutItemsState = new Dictionary<int, bool>();
10+
11+
public delegate string ItemName<T>(T item);
12+
13+
public delegate void ItemContent<T>(T item);
14+
15+
public delegate void OnItemAdd();
16+
17+
public static void Draw<T>(IList<T> list, ItemName<T> itemName, ItemContent<T> itemContent = null, OnItemAdd onItemAdd = null, ItemContent<T> buttonsContentOverride = null, ItemContent<T> itemStartUI = null)
18+
{
19+
if (itemContent != null)
20+
DrawFoldout(list, itemName, itemContent, buttonsContentOverride, itemStartUI);
21+
else
22+
DrawLabel(list, itemName, buttonsContentOverride, itemStartUI);
23+
24+
if (onItemAdd != null)
25+
using (new IMGUIBeginVertical())
26+
{
27+
GUILayout.Space(-7);
28+
using (new IMGUIBeginHorizontal())
29+
{
30+
EditorGUILayout.Space();
31+
var add = GUILayout.Button("+", EditorStyles.miniButton, GUILayout.Width(24));
32+
if (add)
33+
{
34+
onItemAdd();
35+
return;
36+
}
37+
38+
GUILayout.Space(5);
39+
}
40+
}
41+
}
42+
43+
static void DrawFoldout<T>(IList<T> list, ItemName<T> itemName, ItemContent<T> itemContent, ItemContent<T> buttonsContentOverride = null, ItemContent<T> itemStartUI = null)
44+
{
45+
var indentLevel = EditorGUI.indentLevel;
46+
47+
var space = 10;
48+
if (indentLevel >= 1) space += EditorGUI.indentLevel * 10;
49+
50+
EditorGUI.indentLevel = 0;
51+
52+
for (var i = 0; i < list.Count; i++)
53+
{
54+
var item = list[i];
55+
56+
using (new IMGUIBeginHorizontal())
57+
{
58+
GUILayout.Space(space);
59+
using (new IMGUIBeginVertical(PluginsEditorSkin.BoxStyle))
60+
{
61+
var foldState = GetFoldoutState(item);
62+
using (new IMGUIBeginHorizontal())
63+
{
64+
itemStartUI?.Invoke(item);
65+
foldState = EditorGUILayout.Foldout(foldState, itemName(item), true);
66+
67+
SetFoldoutState(item, foldState);
68+
69+
if (buttonsContentOverride != null)
70+
{
71+
buttonsContentOverride.Invoke(item);
72+
}
73+
else
74+
{
75+
var itemWasRemoved = DrawButtons(item, list);
76+
if (itemWasRemoved) return;
77+
}
78+
}
79+
80+
if (foldState)
81+
using (new IMGUIIndentLevel(1))
82+
{
83+
EditorGUILayout.Space();
84+
itemContent(item);
85+
EditorGUILayout.Space();
86+
}
87+
}
88+
89+
GUILayout.Space(5);
90+
}
91+
}
92+
93+
EditorGUI.indentLevel = indentLevel;
94+
}
95+
96+
static void DrawLabel<T>(IList<T> list, ItemName<T> itemName, ItemContent<T> buttonsContentOverride = null, ItemContent<T> itemStartUI = null)
97+
{
98+
var indentLevel = EditorGUI.indentLevel;
99+
100+
var space = 10;
101+
if (indentLevel >= 1) space += EditorGUI.indentLevel * 10;
102+
103+
EditorGUI.indentLevel = 0;
104+
105+
foreach (var item in list)
106+
using (new IMGUIBeginHorizontal())
107+
{
108+
GUILayout.Space(space);
109+
using (new IMGUIBeginVertical(PluginsEditorSkin.BoxStyle))
110+
{
111+
using (new IMGUIBeginHorizontal())
112+
{
113+
itemStartUI?.Invoke(item);
114+
115+
EditorGUILayout.SelectableLabel(itemName(item), GUILayout.Height(16));
116+
117+
if (buttonsContentOverride != null)
118+
{
119+
buttonsContentOverride.Invoke(item);
120+
}
121+
else
122+
{
123+
var itemWasRemoved = DrawButtons(item, list);
124+
if (itemWasRemoved) return;
125+
}
126+
}
127+
}
128+
}
129+
130+
EditorGUI.indentLevel = indentLevel;
131+
}
132+
133+
static bool GetFoldoutState(object item)
134+
{
135+
if (item == null) return false;
136+
return s_GlobalFoldoutItemsState.ContainsKey(item.GetHashCode())
137+
&& s_GlobalFoldoutItemsState[item.GetHashCode()];
138+
}
139+
140+
static void SetFoldoutState(object item, bool value)
141+
{
142+
if (item == null) return;
143+
s_GlobalFoldoutItemsState[item.GetHashCode()] = value;
144+
}
145+
146+
static bool DrawButtons<T>(T currentObject, IList<T> objectsList)
147+
{
148+
var objectIndex = objectsList.IndexOf(currentObject);
149+
if (objectIndex == 0) GUI.enabled = false;
150+
151+
var up = GUILayout.Button("↑", EditorStyles.miniButtonLeft, GUILayout.Width(20));
152+
if (up)
153+
{
154+
var c = currentObject;
155+
objectsList[objectIndex] = objectsList[objectIndex - 1];
156+
objectsList[objectIndex - 1] = c;
157+
}
158+
159+
GUI.enabled = objectIndex < objectsList.Count - 1;
160+
161+
var down = GUILayout.Button("↓", EditorStyles.miniButtonMid, GUILayout.Width(20));
162+
if (down)
163+
{
164+
var c = currentObject;
165+
objectsList[objectIndex] = objectsList[objectIndex + 1];
166+
objectsList[objectIndex + 1] = c;
167+
}
168+
169+
GUI.enabled = true;
170+
var r = GUILayout.Button("-", EditorStyles.miniButtonRight, GUILayout.Width(20));
171+
if (r) objectsList.Remove(currentObject);
172+
173+
return r;
174+
}
175+
}
176+
}

com.stansassets.plugins-dev-kit/Editor/IMGUI/Controls/IMGUIReorderablList.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace StansAssets.Plugins.Editor
5+
{
6+
public class IMGUIBeginArea : IDisposable
7+
{
8+
public IMGUIBeginArea(Rect area)
9+
{
10+
GUILayout.BeginArea(area);
11+
}
12+
13+
public IMGUIBeginArea(Rect area, string content)
14+
{
15+
GUILayout.BeginArea(area, content);
16+
}
17+
18+
public IMGUIBeginArea(Rect area, string content, string style)
19+
{
20+
GUILayout.BeginArea(area, content, style);
21+
}
22+
23+
public void Dispose()
24+
{
25+
GUILayout.EndArea();
26+
}
27+
}
28+
}

com.stansassets.plugins-dev-kit/Editor/IMGUI/Disposables/IMGUIBeginArea.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnityEngine;
2+
using System;
3+
4+
namespace StansAssets.Plugins.Editor
5+
{
6+
public class IMGUIEnable : IDisposable
7+
{
8+
bool PreviousState { get; }
9+
10+
public IMGUIEnable(bool newState)
11+
{
12+
PreviousState = GUI.enabled;
13+
GUI.enabled = newState;
14+
}
15+
16+
public void Dispose()
17+
{
18+
GUI.enabled = PreviousState;
19+
}
20+
}
21+
}

com.stansassets.plugins-dev-kit/Editor/IMGUI/Disposables/IMGUIEnable.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEditor;
4+
5+
namespace StansAssets.Plugins.Editor
6+
{
7+
[Serializable]
8+
public class IMGUIHorizontalSpace : IDisposable
9+
{
10+
public IMGUIHorizontalSpace(int space)
11+
{
12+
EditorGUILayout.BeginHorizontal();
13+
GUILayout.Space(space);
14+
EditorGUILayout.BeginVertical();
15+
}
16+
17+
public void Dispose()
18+
{
19+
EditorGUILayout.EndVertical();
20+
EditorGUILayout.EndHorizontal();
21+
}
22+
}
23+
}

com.stansassets.plugins-dev-kit/Editor/IMGUI/Disposables/IMGUIHorizontalSpace.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEditor;
4+
using UnityEditor.AnimatedValues;
5+
6+
namespace StansAssets.Plugins.Editor
7+
{
8+
[Serializable]
9+
public class IMGUIAnimatedFoldoutBlock
10+
{
11+
[SerializeField]
12+
GUIContent m_Header;
13+
[SerializeField]
14+
AnimBool m_ShowExtraFields = new AnimBool(false);
15+
16+
public IMGUIAnimatedFoldoutBlock(GUIContent header)
17+
{
18+
if (header.image != null) header.text = " " + header.text;
19+
m_Header = header;
20+
}
21+
22+
protected virtual void OnAfterHeaderGUI() { }
23+
24+
public void OnGUI(Action onContentRender)
25+
{
26+
using (new IMGUIBeginHorizontal())
27+
{
28+
m_ShowExtraFields.target = EditorGUILayout.Foldout(m_ShowExtraFields.target, m_Header, true);
29+
}
30+
31+
using (new IMGUIHorizontalSpace(15))
32+
{
33+
if (EditorGUILayout.BeginFadeGroup(m_ShowExtraFields.faded)) onContentRender.Invoke();
34+
EditorGUILayout.EndFadeGroup();
35+
}
36+
}
37+
}
38+
}

com.stansassets.plugins-dev-kit/Editor/IMGUI/Layout/IMGUIAnimatedFoldoutBlock.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)