This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
forked from flarfo/Muck-SaveUtility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIManager.cs
437 lines (345 loc) · 20 KB
/
UIManager.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using System.Collections.Generic;
using System.IO;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
namespace SaveUtility
{
[HarmonyPatch]
class UIManager
{
private static bool uiUpdated = false;
private static bool pauseUIUpdated = false;
private static bool canSave = true;
private static bool serverHost = false;
private static float time = 0.0f;
public static Texture backgroundImage;
internal static GameObject selectionGUI;
internal static GameObject buttonExtraGUI;
internal static GameObject verifyDelete;
internal static GameObject backgroundDarkGUI;
internal static Dictionary<string, SaveButton> saveButtons = new Dictionary<string, SaveButton>();
internal static string curEditSave;
public static bool useAutoSave = false;
[HarmonyPatch(typeof(MenuUI), "JoinLobby")]
static void Postfix(MenuUI __instance)
{
if (!uiUpdated && serverHost)
{
LoadManager.worldSaves = SaveSystem.GetAllSaves();
Vector2 uiSize = new Vector2(500, 750);
//gets lobby UI parent
GameObject.Find("LobbyDetails").GetComponent<RectTransform>().sizeDelta = new Vector2(250, 408);
//creates selection UI parent with size/layer/transform
selectionGUI = new GameObject("selectionGUI", new[] { typeof(RectTransform),
typeof(CanvasRenderer), typeof(RawImage) });
selectionGUI.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
selectionGUI.layer = 5;
selectionGUI.transform.parent = __instance.lobbyUi.transform;
//sets UI background image
selectionGUI.GetComponent<RectTransform>().sizeDelta = uiSize;
selectionGUI.GetComponent<RawImage>().texture = backgroundImage;
selectionGUI.GetComponent<RawImage>().color = new Color32(137, 104, 61, 255);
//maybe only make dark when delete button is clicked, "Are you sure?" "Delete" "Cancel"
//creates extra button UI for when button is right clicked
//maybe reposition pop-up GUI for deleting/rename to be to the side of normal GUI
buttonExtraGUI = new GameObject("buttonExtraGUI", new[] { typeof(RectTransform),
typeof(CanvasRenderer), typeof(RawImage), typeof(VerticalLayoutGroup) });
buttonExtraGUI.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
buttonExtraGUI.layer = 5;
buttonExtraGUI.transform.parent = __instance.lobbyUi.transform;
buttonExtraGUI.GetComponent<RectTransform>().sizeDelta = new Vector2(400, 250);
buttonExtraGUI.GetComponent<RawImage>().texture = backgroundImage;
buttonExtraGUI.GetComponent<RawImage>().color = new Color32(137, 104, 61, 255);
//FOR RENAMINING, LOOK AT JOIN BUTTON CHILDREN
buttonExtraGUI.GetComponent<VerticalLayoutGroup>().padding = new RectOffset(15, 15, 15, 15);
buttonExtraGUI.GetComponent<VerticalLayoutGroup>().spacing = 2;
buttonExtraGUI.GetComponent<VerticalLayoutGroup>().childAlignment = TextAnchor.UpperCenter;
buttonExtraGUI.GetComponent<VerticalLayoutGroup>().childControlWidth = true;
GameObject saveText = new GameObject("saveText", new[] { typeof(TextMeshProUGUI) } );
saveText.transform.parent = buttonExtraGUI.transform;
saveText.GetComponent<TextMeshProUGUI>().fontSize = 35;
saveText.GetComponent<TextMeshProUGUI>().fontStyle = FontStyles.Bold;
saveText.GetComponent<TextMeshProUGUI>().color = new Color(0, 0, 0, 1);
saveText.GetComponent<TextMeshProUGUI>().alignment = TextAlignmentOptions.Center;
Button deleteButton = UnityEngine.Object.Instantiate(__instance.startBtn, buttonExtraGUI.transform).GetComponent<Button>();
deleteButton.onClick = new Button.ButtonClickedEvent();
deleteButton.onClick.AddListener(delegate ()
{
verifyDelete.SetActive(true);
backgroundDarkGUI.SetActive(true);
});
deleteButton.GetComponentInChildren<TextMeshProUGUI>().text = "Delete";
Button closeButton = UnityEngine.Object.Instantiate(__instance.startBtn, buttonExtraGUI.transform).GetComponent<Button>();
closeButton.onClick = new Button.ButtonClickedEvent();
closeButton.onClick.AddListener(delegate ()
{
buttonExtraGUI.SetActive(false);
});
closeButton.GetComponentInChildren<TextMeshProUGUI>().text = "Close";
/*GameObject rename = new GameObject("rename", new[] { typeof(RectTransform),
typeof(CanvasRenderer), typeof(VerticalLayoutGroup) });
rename.transform.parent = buttonExtraGUI.transform;
Button renameButton = UnityEngine.Object.Instantiate(__instance.startBtn, rename.transform).GetComponent<Button>();
renameButton.onClick = new Button.ButtonClickedEvent();
renameButton.onClick.AddListener(delegate ()
{
Debug.Log("Save Renamed!");
//create text input field
//saveButtons[curEditSave].UpdateText(text);
buttonExtraGUI.SetActive(false);
backgroundDarkGUI.SetActive(false);
});
renameButton.GetComponentInChildren<TextMeshProUGUI>().text = "Rename";
GameObject renameInput = new GameObject("renameInput", new[] { typeof(RectTransform),
typeof(CanvasRenderer), typeof(Image), typeof(TMP_InputField) });
renameInput.transform.parent = rename.transform;
GameObject textArea = new GameObject("textAra", new[] { typeof(RectTransform), typeof(RectMask2D) });
textArea.transform.parent = renameInput.transform;*/
buttonExtraGUI.SetActive(false);
backgroundDarkGUI = new GameObject("backgroundDark", new[] { typeof(RectTransform),
typeof(CanvasRenderer), typeof(RawImage) });
backgroundDarkGUI.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
backgroundDarkGUI.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width, Screen.height);
backgroundDarkGUI.GetComponent<RawImage>().color = new Color32(0, 0, 0, 150);
backgroundDarkGUI.layer = 5;
backgroundDarkGUI.transform.parent = __instance.lobbyUi.transform;
backgroundDarkGUI.SetActive(false);
verifyDelete = new GameObject("verifyDelete", new[] { typeof(RectTransform),
typeof(CanvasRenderer), typeof(RawImage), typeof(VerticalLayoutGroup) });
verifyDelete.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
verifyDelete.layer = 5;
verifyDelete.transform.parent = __instance.lobbyUi.transform;
verifyDelete.GetComponent<RectTransform>().sizeDelta = new Vector2(400, 250);
verifyDelete.GetComponent<RawImage>().texture = backgroundImage;
verifyDelete.GetComponent<RawImage>().color = new Color32(137, 104, 61, 255);
verifyDelete.GetComponent<VerticalLayoutGroup>().padding = new RectOffset(15, 15, 15, 15);
verifyDelete.GetComponent<VerticalLayoutGroup>().spacing = 2;
verifyDelete.GetComponent<VerticalLayoutGroup>().childAlignment = TextAnchor.UpperCenter;
verifyDelete.GetComponent<VerticalLayoutGroup>().childControlWidth = true;
GameObject deleteText = new GameObject("deleteText", new[] { typeof(TextMeshProUGUI) });
deleteText.transform.parent = verifyDelete.transform;
deleteText.GetComponent<TextMeshProUGUI>().text = "Are you sure?";
deleteText.GetComponent<TextMeshProUGUI>().fontSize = 35;
deleteText.GetComponent<TextMeshProUGUI>().fontStyle = FontStyles.Bold;
deleteText.GetComponent<TextMeshProUGUI>().color = new Color(0, 0, 0, 1);
deleteText.GetComponent<TextMeshProUGUI>().alignment = TextAlignmentOptions.Center;
Button yesButton = UnityEngine.Object.Instantiate(__instance.startBtn, verifyDelete.transform).GetComponent<Button>();
yesButton.onClick = new Button.ButtonClickedEvent();
yesButton.onClick.AddListener(delegate ()
{
Debug.Log("Save Deleted!");
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves", curEditSave)))
{
File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves", curEditSave));
}
GameObject.Destroy(saveButtons[curEditSave].gameObject);
saveButtons.Remove(curEditSave);
if (LoadManager.currentSave == curEditSave)
{
LoadManager.currentSave = "";
LoadManager.hasSaveLoaded = false;
}
buttonExtraGUI.SetActive(false);
backgroundDarkGUI.SetActive(false);
verifyDelete.SetActive(false);
});
yesButton.GetComponentInChildren<TextMeshProUGUI>().text = "Yes";
Button noButton = UnityEngine.Object.Instantiate(__instance.startBtn, verifyDelete.transform).GetComponent<Button>();
noButton.onClick = new Button.ButtonClickedEvent();
noButton.onClick.AddListener(delegate ()
{
buttonExtraGUI.SetActive(false);
backgroundDarkGUI.SetActive(false);
verifyDelete.SetActive(false);
});
noButton.GetComponentInChildren<TextMeshProUGUI>().text = "No";
verifyDelete.SetActive(false);
//creates mask object, stops scroll from being fully rendered
GameObject maskRect = new GameObject("maskRect", new[] { typeof(RectTransform), typeof(CanvasRenderer),
typeof(RawImage), typeof(Mask) });
maskRect.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
maskRect.transform.parent = selectionGUI.transform;
maskRect.GetComponent<Mask>().rectTransform.sizeDelta = uiSize;
maskRect.GetComponent<Mask>().showMaskGraphic = false;
//creates scroll object with scrollrect
GameObject scrollGUI = new GameObject("scrollGUI", new[] { typeof(RectTransform), typeof(CanvasRenderer),
typeof(ScrollRect) });
scrollGUI.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
scrollGUI.transform.parent = maskRect.transform;
scrollGUI.GetComponent<ScrollRect>().GetComponent<RectTransform>().sizeDelta = uiSize;
scrollGUI.GetComponent<ScrollRect>().horizontal = false;
scrollGUI.GetComponent<ScrollRect>().movementType = ScrollRect.MovementType.Elastic;
//creates scrollContent (array of buttons parented to scrollGUI)
GameObject scrollContent = new GameObject("scrollContent", new[] { typeof(RectTransform), typeof(CanvasRenderer),
typeof(VerticalLayoutGroup)});
scrollContent.GetComponent<RectTransform>().position = new Vector3(Screen.width / 2, Screen.height / 2, 0);
scrollContent.transform.parent = scrollGUI.transform;
scrollContent.GetComponent<RectTransform>().sizeDelta = uiSize;
scrollContent.GetComponent<VerticalLayoutGroup>().padding = new RectOffset(15, 15, 15, 15);
scrollContent.GetComponent<VerticalLayoutGroup>().spacing = 2;
scrollContent.GetComponent<VerticalLayoutGroup>().childAlignment = TextAnchor.UpperCenter;
scrollContent.GetComponent<VerticalLayoutGroup>().childControlWidth = true;
//updates scrollGUI content variables
scrollGUI.GetComponent<ScrollRect>().content = scrollContent.GetComponent<RectTransform>();
//iterate through worldsaves, create new button for each world save
if (LoadManager.worldSaves.Length > 0)
{
for (int i = 0; i < LoadManager.worldSaves.Length; i++)
{
if (i > 8)
{
//extends scrollcontent UI for each button above 8 so as to not compress buttons together
scrollContent.GetComponent<RectTransform>().sizeDelta = new Vector2(500, 750+(i*30));
}
//create a new world save button
Button newButton = UnityEngine.Object.Instantiate(__instance.startBtn, scrollContent.transform).GetComponent<Button>();
newButton.onClick = new Button.ButtonClickedEvent();
newButton.gameObject.AddComponent<SaveButton>();
newButton.GetComponentInChildren<TextMeshProUGUI>().text = LoadManager.worldSaves[i];
}
//creates none button that allows players to deselect saves
Button noneButton = UnityEngine.Object.Instantiate(__instance.startBtn, scrollContent.transform
.transform).GetComponent<Button>();
noneButton.GetComponentInChildren<TextMeshProUGUI>().text = "None";
noneButton.onClick = new Button.ButtonClickedEvent();
noneButton.onClick.AddListener(delegate ()
{
LoadManager.currentSave = "";
LoadManager.hasSaveLoaded = false;
selectionGUI.SetActive(false);
});
}
else
{
//if there are no saves, creates no saves button
Button noSaveButton = UnityEngine.Object.Instantiate(__instance.startBtn, scrollContent
.transform).GetComponent<Button>();
noSaveButton.GetComponentInChildren<TextMeshProUGUI>().text = "No Saves :(";
noSaveButton.onClick = new Button.ButtonClickedEvent();
noSaveButton.onClick.AddListener(delegate ()
{
selectionGUI.SetActive(false);
});
}
selectionGUI.SetActive(false);
//adds load save button, which displays the selection GUI when clicked
Button component = UnityEngine.Object.Instantiate(__instance.startBtn,
GameObject.Find("LobbyDetails").transform).GetComponent<Button>();
component.GetComponentInChildren<TextMeshProUGUI>().text = "Load Save";
component.onClick = new Button.ButtonClickedEvent();
component.onClick.AddListener(delegate ()
{
Debug.Log("LOAD SAVE BUTTON CLICKED");
selectionGUI.SetActive(!selectionGUI.activeSelf);
});
uiUpdated = true;
}
}
[HarmonyPatch(typeof(OtherInput), "Pause")]
[HarmonyPostfix]
static void UpdatePauseUI()
{
if (!pauseUIUpdated)
{
Button saveButton = UnityEngine.Object.Instantiate(GameObject.Find("ResumeBtn"), GameObject.Find("PauseUI").transform).GetComponent<Button>();
saveButton.gameObject.transform.SetSiblingIndex(2);
saveButton.GetComponentInChildren<TextMeshProUGUI>().text = "Save";
saveButton.onClick = new Button.ButtonClickedEvent();
saveButton.onClick.AddListener(delegate ()
{
Debug.Log("SAVE BUTTON CLICKED");
if (canSave)
{
World.Save();
canSave = false;
time = 0f;
}
});
pauseUIUpdated = true;
}
}
[HarmonyPatch(typeof(OtherInput), "Update")]
[HarmonyPostfix]
static void SaveTimer()
{
time += Time.deltaTime;
if (time >= 60f)
{
canSave = true;
}
}
[HarmonyPatch(typeof(MenuUI), "StartLobby")]
[HarmonyPrefix]
static void SetHost()
{
serverHost = true;
}
[HarmonyPatch(typeof(MenuUI), "Start")]
static void Prefix()
{
saveButtons.Clear();
canSave = true;
time = 0f;
pauseUIUpdated = false;
uiUpdated = false;
serverHost = false;
}
[HarmonyPatch(typeof(Settings), "Start")]
[HarmonyPostfix]
static void UpdateSettings(Settings __instance)
{
if (PlayerPrefs.HasKey("autosave"))
{
useAutoSave = Convert.ToBoolean(PlayerPrefs.GetInt("autosave"));
}
GameObject autoSave = GameObject.Instantiate(__instance.tutorial.gameObject, __instance.tutorial.transform.parent);
autoSave.GetComponentInChildren<TextMeshProUGUI>().text = "Auto Save";
autoSave.GetComponentInChildren<MyBoolSetting>().SetSetting(useAutoSave);
autoSave.GetComponentInChildren<Button>().onClick = new Button.ButtonClickedEvent();
autoSave.GetComponentInChildren<Button>().onClick.AddListener(delegate ()
{
useAutoSave = !useAutoSave;
autoSave.GetComponentInChildren<MyBoolSetting>().SetSetting(useAutoSave);
PlayerPrefs.SetInt("autosave", Convert.ToInt32(useAutoSave));
});
}
}
public class SaveButton : MonoBehaviour, IPointerClickHandler
{
void Start()
{
UIManager.saveButtons.Add(GetComponentInChildren<TextMeshProUGUI>().text, this);
}
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
LoadManager.currentSave = GetComponentInChildren<TextMeshProUGUI>().text;
LoadManager.hasSaveLoaded = true;
UIManager.selectionGUI.SetActive(false);
}
else if (eventData.button == PointerEventData.InputButton.Right)
{
UIManager.buttonExtraGUI.transform.position = new Vector3(eventData.position.x+200, eventData.position.y-125);
UIManager.buttonExtraGUI.SetActive(true);
UIManager.curEditSave = GetComponentInChildren<TextMeshProUGUI>().text;
if (UIManager.curEditSave.Length > 12)
{
UIManager.buttonExtraGUI.GetComponentInChildren<TextMeshProUGUI>().text = GetComponentInChildren<TextMeshProUGUI>().text.Substring(0,12) + "...";
}
else
{
UIManager.buttonExtraGUI.GetComponentInChildren<TextMeshProUGUI>().text = GetComponentInChildren<TextMeshProUGUI>().text;
}
}
}
public void UpdateText(string text)
{
GetComponentInChildren<TextMeshProUGUI>().text = text;
}
}
}