-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenu.cs
196 lines (157 loc) · 5.14 KB
/
Menu.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Menu.cs
=======
Description: Implements UI logic. Wrapper for Unity IMGUI.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace EasyCompany
{
// Represents a menu tab and all elements it contains.
internal class MenuTab
{
public string name;
public UnityEngine.Vector2 scrollPos = new UnityEngine.Vector2(0, 0);
public List<BaseMenuTabItem> items;
public MenuTab(string name, List<BaseMenuTabItem> items)
{
this.name = name;
this.items = items;
}
}
// Parent virtual class for elements inside a tab
internal class BaseMenuTabItem
{
// Label associated with the tab item
protected string label;
public BaseMenuTabItem(string label)
{
this.label = label;
}
// Draw the tab item. Implement in child classes
public virtual void Draw()
{
throw new NotImplementedException("Must use child class");
}
}
// Horizontal container
internal class HContainerMenuTabItem : BaseMenuTabItem
{
private List<BaseMenuTabItem> items;
// Only need this for player menu, which will always have player name as label.
public HContainerMenuTabItem(string label, List<BaseMenuTabItem> items) : base(label)
{
this.items = items;
}
public override void Draw()
{
GUILayout.BeginHorizontal();
// Draw label
GUILayout.Label(label);
// Draw items
foreach (var item in items) { item.Draw(); }
GUILayout.EndHorizontal();
}
}
// Buttons
internal class ButtonMenuTabItem : BaseMenuTabItem
{
// Called once when clicking the button
private Action onClick;
public ButtonMenuTabItem(String label, Action onClick) : base(label)
{
this.onClick = onClick;
}
public override void Draw()
{
if (GUILayout.Button(this.label))
{
onClick();
}
}
}
// Toggle radio button
internal class ToggleMenuTabItem : BaseMenuTabItem
{
// Called to get the value of the toggle button
private Func<bool> getValueFn;
// Called (once) when the value has changed. Must set the new value.
private Action<bool> setValueFn;
public ToggleMenuTabItem(String label, Func<bool> getValueFn, Action<bool> setValueFn) : base(label)
{
this.getValueFn = getValueFn;
this.setValueFn = setValueFn;
}
public override void Draw()
{
bool curVal = getValueFn();
bool newVal = GUILayout.Toggle(curVal, label);
// Call setValueFn if toggle was clicked.
if (curVal != newVal)
{
setValueFn(newVal);
}
}
}
// Interface class for the UI
internal class Menu
{
// Title label
private String title;
// Size/position
private Rect rect;
// Tabs
private int selectedTabIdx = 0;
private List<MenuTab> tabs = new List<MenuTab>();
public Menu(string title, Rect rect)
{
this.title = title;
this.rect = rect;
}
// Inserts a new tab
public void AddTab(MenuTab tab)
{
// Tab names must be unique
Debug.Assert(tabs.All(t => t.name != tab.name));
tabs.Add(tab);
}
// Draws the UI.
public void Draw()
{
// Background
rect = GUILayout.Window(1024, rect, DrawWindow, title);
}
// Replace the contents of a tab by name
public void UpdateTab(String name, List<BaseMenuTabItem> newItems)
{
var tab = tabs.FirstOrDefault(t => t.name == name);
// Tab should exist
Debug.Assert(tab != null);
tab.items = newItems;
}
// UI window draw function, called by IMGUI
private void DrawWindow(int windowID)
{
GUILayout.BeginVertical();
// Draw tabs
selectedTabIdx = GUILayout.Toolbar(selectedTabIdx, tabs.Select(t => t.name).ToArray<String>());
// Draw current tab
MenuTab curTab = tabs[selectedTabIdx];
curTab.scrollPos = GUILayout.BeginScrollView(curTab.scrollPos);
curTab.items.ForEach(item => item.Draw());
GUILayout.EndScrollView();
GUILayout.EndVertical();
// Drag window (call here so this only happens if no control is clicked)
// FIXME: Alternatively could limit drag area to title bar since it's a little annoying to misclick and
// accidentally drag the window.
// see https://docs.unity3d.com/ScriptReference/GUI.DragWindow.html
// FIXME: If loading into a game while dragging, window will disappear!
GUI.DragWindow();
}
}
}