-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUI.cs
336 lines (297 loc) · 12 KB
/
UI.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using Modding;
using UnityEngine;
using UnityEngine.UI;
namespace HKTimer.UI {
// AaaaAAAaAaaAAA
public class UIManager : MonoBehaviour {
private bool uiOpen = false;
private GameObject menu;
public TriggerManager tm { get; set; }
public HKTimer hktimer { get; set; }
public Timer timer { get; set; }
public GameManager gm { get; set; }
private static readonly Vector2 MIDDLE = new Vector2(0.5f, 0.5f);
public UIOption<ToggleOption> showTimer { get; private set; }
public UIButton resetPb { get; private set; }
public UIOption<TriggerTypeOption> triggerType { get; private set; }
public UIButton saveTriggers { get; private set; }
public UIButton loadTriggers { get; private set; }
public UIButton reloadSettings { get; private set; }
public UIElement[] elements { get; private set; }
public GameObject cursorDisplay { get; private set; }
private int cursor;
public UIManager Initialize(TriggerManager tm, HKTimer hktimer, Timer timer) {
this.tm = tm;
this.hktimer = hktimer;
this.timer = timer;
this.gm = GameManager.instance;
return this;
}
public void InitDisplay() {
if(menu != null) {
GameObject.DestroyImmediate(menu);
}
CanvasUtil.CreateFonts();
menu = CanvasUtil.CreateCanvas(UnityEngine.RenderMode.ScreenSpaceOverlay, 100);
this.showTimer = new UIOption<ToggleOption>(
CanvasUtil.CreateTextPanel(
menu, "Show Timer", 30, TextAnchor.MiddleLeft,
new CanvasUtil.RectData(new Vector2(300, 60), new Vector2(-150, 180), MIDDLE, MIDDLE)
),
CanvasUtil.CreateTextPanel(
menu, "", 30, TextAnchor.MiddleRight,
new CanvasUtil.RectData(new Vector2(300, 60), new Vector2(150, 180), MIDDLE, MIDDLE)
),
new Vector2(-330, 180),
ToggleOption.values,
(v) => this.timer.ShowDisplay(v.enabled),
1
);
this.resetPb = new UIButton(
CanvasUtil.CreateTextPanel(
menu, "Reset PB", 30, TextAnchor.MiddleCenter,
new CanvasUtil.RectData(new Vector2(360, 60), new Vector2(0, 120), MIDDLE, MIDDLE)
),
new Vector2(-210, 120),
() => {
if(this.tm != null) this.tm.ResetPB();
}
);
this.triggerType = new UIOption<TriggerTypeOption>(
CanvasUtil.CreateTextPanel(
menu, "Trigger Type", 30, TextAnchor.MiddleLeft,
new CanvasUtil.RectData(new Vector2(300, 60), new Vector2(-150, 0), MIDDLE, MIDDLE)
),
CanvasUtil.CreateTextPanel(
menu, "", 30, TextAnchor.MiddleRight,
new CanvasUtil.RectData(new Vector2(300, 60), new Vector2(150, 0), MIDDLE, MIDDLE)
),
new Vector2(-330, 0),
TriggerTypeOption.values,
(e) => this.tm.triggerPlaceType = e.variant
);
this.saveTriggers = new UIButton(
CanvasUtil.CreateTextPanel(
menu, "Save Triggers", 30, TextAnchor.MiddleCenter,
new CanvasUtil.RectData(new Vector2(360, 60), new Vector2(0, -60), MIDDLE, MIDDLE)
),
new Vector2(-210, -60),
() => {
if(this.tm != null) this.tm.SaveTriggers();
}
);
this.loadTriggers = new UIButton(
CanvasUtil.CreateTextPanel(
menu, "Load Triggers", 30, TextAnchor.MiddleCenter,
new CanvasUtil.RectData(new Vector2(360, 60), new Vector2(0, -120), MIDDLE, MIDDLE)
),
new Vector2(-210, -120),
() => {
if(this.tm != null) this.tm.LoadTriggers();
}
);
this.reloadSettings = new UIButton(
CanvasUtil.CreateTextPanel(
menu, "Reload Settings", 30, TextAnchor.MiddleCenter,
new CanvasUtil.RectData(new Vector2(360, 60), new Vector2(0, -240), MIDDLE, MIDDLE)
),
new Vector2(-210, -240),
() => {
if(this.hktimer != null) this.hktimer.ReloadSettings();
}
);
this.cursorDisplay = CanvasUtil.CreateTextPanel(
menu, ">", 50, TextAnchor.MiddleCenter,
new CanvasUtil.RectData(new Vector2(60, 60), new Vector2(0, 0), MIDDLE, MIDDLE)
);
this.elements = new UIElement[]{
this.showTimer,
this.resetPb,
this.triggerType,
this.saveTriggers,
this.loadTriggers,
this.reloadSettings
};
this.SetShown(false);
this.uiOpen = false;
GameObject.DontDestroyOnLoad(this.menu);
}
public void SetShown(bool show) {
foreach(var e in this.elements) {
e.SetShown(show);
}
this.cursorDisplay.SetActive(show);
if(show) GameObject.DontDestroyOnLoad(this.cursorDisplay);
}
private void UpdateCursor() {
this.cursorDisplay
.GetComponent<RectTransform>()
.anchoredPosition = this.elements[this.cursor].cursorPos;
}
public void Update() {
if(StringInputManager.GetKeyDown(HKTimer.settings.open_ui)) {
if(this.uiOpen) {
this.uiOpen = false;
this.SetShown(false);
if(this.gm.hero_ctrl != null) this.gm.hero_ctrl.RegainControl();
} else {
this.uiOpen = true;
this.SetShown(true);
this.cursor = 0;
this.UpdateCursor();
if(this.gm.hero_ctrl != null) this.gm.hero_ctrl.RelinquishControl();
}
} else if(this.uiOpen) {
var inputHandler = this.gm.inputHandler;
if(inputHandler.inputActions.left.WasPressed) {
this.elements[this.cursor].Left();
} else if(inputHandler.inputActions.right.WasPressed) {
this.elements[this.cursor].Right();
} else if(inputHandler.inputActions.up.WasPressed) {
if(this.cursor == 0) {
this.cursor = elements.Length;
}
this.cursor -= 1;
this.UpdateCursor();
} else if(inputHandler.inputActions.down.WasPressed) {
this.cursor += 1;
if(this.cursor == elements.Length) {
this.cursor = 0;
}
this.UpdateCursor();
} else if(inputHandler.inputActions.menuSubmit.WasPressed) {
this.elements[this.cursor].Accept();
}
}
}
}
public class ToggleOption {
public static readonly ToggleOption[] values = {
new ToggleOption(false),
new ToggleOption(true)
};
public bool enabled { get; set; }
public ToggleOption(bool enabled) => this.enabled = enabled;
public override string ToString() {
return enabled ? "On" : "Off";
}
}
public class TriggerTypeOption {
public static readonly TriggerTypeOption[] values = {
new TriggerTypeOption(TriggerManager.TriggerPlaceType.Collision),
new TriggerTypeOption(TriggerManager.TriggerPlaceType.Movement),
new TriggerTypeOption(TriggerManager.TriggerPlaceType.Scene)
};
public TriggerManager.TriggerPlaceType variant { get; set; }
public TriggerTypeOption(TriggerManager.TriggerPlaceType variant) => this.variant = variant;
public override string ToString() {
return this.variant switch {
TriggerManager.TriggerPlaceType.Collision => "Collision",
TriggerManager.TriggerPlaceType.Movement => "Movement",
TriggerManager.TriggerPlaceType.Scene => "Scene",
_ => "unknown"
};
}
}
public abstract class UIElement {
public abstract Vector2 cursorPos { get; protected set; }
public abstract void SetShown(bool show);
public virtual void Left() { }
public virtual void Right() { }
public virtual void Accept() { }
}
public class UIOption<E> : UIElement {
private GameObject nameEl;
private GameObject valEl;
private Action<E> onUpdate;
public Text nameText { get; private set; }
public Text valText { get; private set; }
private int valueInd = 0;
public E value { get => values[valueInd]; }
public override Vector2 cursorPos { get; protected set; }
private readonly E[] values;
public UIOption(
GameObject nameEl,
GameObject valEl,
Vector2 cursorAnchor,
E[] values
) : this(nameEl, valEl, cursorAnchor, values, default, 0) { }
public UIOption(
GameObject nameEl,
GameObject valEl,
Vector2 cursorAnchor,
E[] values,
Action<E> onUpdate
) : this(nameEl, valEl, cursorAnchor, values, onUpdate, 0) { }
public UIOption(
GameObject nameEl,
GameObject valEl,
Vector2 cursorAnchor,
E[] values,
Action<E> onUpdate,
int defaultInd
) {
this.values = values;
this.valueInd = defaultInd;
this.nameEl = nameEl;
this.cursorPos = cursorAnchor;
this.valEl = valEl;
this.onUpdate = onUpdate;
this.nameText = nameEl.GetComponent<Text>();
this.valText = valEl.GetComponent<Text>();
this.UpdateText();
}
private void UpdateText() {
this.valText.text = this.value.ToString();
}
public override void Left() {
if(valueInd == 0) {
valueInd = values.Length;
}
valueInd -= 1;
if(this.onUpdate != null) this.onUpdate(this.value);
this.UpdateText();
base.Left();
}
public override void Right() {
valueInd += 1;
if(valueInd == values.Length) {
valueInd = 0;
}
if(this.onUpdate != null) this.onUpdate(this.value);
this.UpdateText();
base.Right();
}
public override void SetShown(bool show) {
this.nameEl.SetActive(show);
this.valEl.SetActive(show);
if(show) {
GameObject.DontDestroyOnLoad(this.nameEl);
GameObject.DontDestroyOnLoad(this.valEl);
}
}
}
public class UIButton : UIElement {
private GameObject el;
public Text text { get; private set; }
public override Vector2 cursorPos { get; protected set; }
private Action onClick;
public UIButton(GameObject el, Vector2 cursorAnchor, Action onClick) {
this.el = el;
this.onClick = onClick;
this.cursorPos = cursorAnchor;
this.text = el.GetComponent<Text>();
GameObject.DontDestroyOnLoad(this.el);
}
public override void Accept() {
this.onClick();
base.Accept();
}
public override void SetShown(bool show) {
this.el.SetActive(show);
if(show) GameObject.DontDestroyOnLoad(this.el);
}
}
}