-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEditingMode.cs
More file actions
150 lines (143 loc) · 7.72 KB
/
EditingMode.cs
File metadata and controls
150 lines (143 loc) · 7.72 KB
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
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace AlweStats {
public static class EditingMode {
private static GameObject resetObj = null;
private static bool isEditing = false;
private static List<GameObject> templateObjs = new();
private static List<Block> blockObjs = new();
private static Vector3 lastMousePos = Vector3.zero;
private static string currentlyDragging = "";
private static float lastScrollPos = 0f;
public static void Start(List<Block> blocks) {
foreach (GameObject go in templateObjs) {
UnityEngine.Object.Destroy(go);
}
templateObjs.Clear();
blockObjs.Clear();
blockObjs.AddRange(blocks);
foreach (Block b in blocks) {
GameObject templateObj = UnityEngine.Object.Instantiate(b.GetGameObject(), b.GetTransform());
templateObj.name = $"{b.GetName()}Template";
templateObj.transform.SetParent(b.GetParent());
templateObj.GetComponent<VerticalLayoutGroup>().padding = new RectOffset(0, 0, 0, 0);
UnityEngine.Object.Destroy(templateObj.GetComponent<ContentSizeFitter>());
templateObj.GetComponent<Image>().color = new Color(0.25f, 0.25f, 0.25f, 1f);
Text templateText = templateObj.GetComponentInChildren<Text>();
templateText.text = b.GetName();
templateText.color = Color.white;
templateText.alignment = TextAnchor.MiddleCenter;
templateText.resizeTextForBestFit = true;
templateObj.SetActive(false);
templateObjs.Add(templateObj);
}
}
public static void Update() {
if (!isEditing) return;
Vector3 mousePos = Input.mousePosition;
float scrollPos = Input.mouseScrollDelta.y;
if (lastMousePos == Vector3.zero) lastMousePos = mousePos;
if (lastScrollPos == 0f) lastScrollPos = scrollPos;
if (Input.GetKey(KeyCode.LeftControl)) {
foreach (GameObject g in templateObjs) {
RectTransform currentRect = g.GetComponent<RectTransform>();
if (RectTransformUtility.RectangleContainsScreenPoint(currentRect, mousePos)) {
float scaledValueX = (float) Math.Round(Mathf.Abs(currentRect.sizeDelta.x + (scrollPos * 2)), 2);
float scaledValueY = (float) Math.Round(Mathf.Abs(currentRect.sizeDelta.y + (scrollPos * 2)), 2);
currentRect.sizeDelta = new Vector2(scaledValueX, scaledValueY);
g.GetComponentInChildren<Text>().fontSize += (int) Math.Round(scrollPos / 2);
}
}
}
if (Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.LeftControl)) {
if (currentlyDragging != "") {
Transform current = Hud.instance.m_rootObject.transform.Find(currentlyDragging);
if (current) {
RectTransform currentRect = current.GetComponent<RectTransform>();
currentRect.position = mousePos;
}
} else {
foreach (GameObject g in templateObjs) {
RectTransform currentRect = g.GetComponent<RectTransform>();
if (RectTransformUtility.RectangleContainsScreenPoint(currentRect, mousePos)) {
currentRect.position = mousePos;
currentlyDragging = g.name;
break;
}
}
}
} else currentlyDragging = "";
lastMousePos = mousePos;
lastScrollPos = scrollPos;
}
public static void OnPress() {
isEditing = !isEditing;
if (templateObjs == null) return;
if (isEditing) {
//Debug.Log($"{Main.Plugin_Name} - Editing mode : ON !");
foreach (GameObject g in templateObjs) {
Transform original = g.transform.parent.Find(g.name.Replace("Template", ""));
RectTransform originalRect = original.GetComponent<RectTransform>();
RectTransform templateRect = g.GetComponent<RectTransform>();
templateRect.pivot = templateRect.anchorMin = templateRect.anchorMax = originalRect.pivot;
templateRect.sizeDelta = originalRect.sizeDelta;
templateRect.anchoredPosition = originalRect.anchoredPosition;
templateRect.position = originalRect.position;
if (original.gameObject.activeSelf) g.SetActive(true);
}
} else if (!isEditing) {
//Debug.Log($"{Main.Plugin_Name} - Editing mode : OFF !");
foreach (GameObject g in templateObjs) {
Transform original = g.transform.parent.Find(g.name.Replace("Template", ""));
RectTransform originalRect = original.GetComponent<RectTransform>();
RectTransform templateRect = g.GetComponent<RectTransform>();
Text templateText = g.GetComponentInChildren<Text>();
Text originalText = original.GetComponentInChildren<Text>();
originalText.fontSize = templateText.fontSize;
originalRect.position = templateRect.position;
g.SetActive(false);
}
}
}
public static void Destroy(List<Block> blocks) {
foreach (Block b in blocks) {
b.SetSize(b.GetText().fontSize);
b.SetPosition(b.GetRect().pivot);
b.SetMargin(b.GetRect().anchoredPosition);
UnityEngine.Object.Destroy(b.GetGameObject());
}
Main.config.Save();
Debug.Log($"The config file of {Main.Plugin_Name} was saved successfully !");
}
public static void Reset() {
isEditing = true;
OnPress();
foreach (Block b in blockObjs) {
b.SetPosition(b.GetConfigValue<string>(b.GetName(), "Position").DefaultValue.ToString());
b.SetMargin(b.GetConfigValue<string>(b.GetName(), "Margin").DefaultValue.ToString());
}
Main.config.Save();
//Debug.Log($"The config file of {Main.Plugin_Name} was saved successfully !");
}
public static void AddButton() {
if (resetObj == null) {
GameObject originalObj = Menu.instance.m_menuDialog.Find("MenuEntries/Continue").gameObject;
resetObj = UnityEngine.Object.Instantiate(originalObj, originalObj.transform);
resetObj.name = "ResetAlweStats";
resetObj.transform.SetParent(originalObj.transform.parent);
resetObj.transform.localPosition = new Vector3(0, originalObj.transform.localPosition.y - 40f, 0f);
TextMeshProUGUI textComp = resetObj.GetComponentInChildren<TextMeshProUGUI>();
textComp.text = Localization.instance.Localize("$alwe_reset");
textComp.ForceMeshUpdate();
resetObj.transform.Find("LeftKnot").localPosition = new Vector3(-textComp.bounds.size.x, 0f, 0f);
resetObj.transform.Find("RightKnot").localPosition = new Vector3(textComp.bounds.size.x, 0f, 0f);
resetObj.GetComponent<Button>().onClick.RemoveAllListeners();
resetObj.GetComponent<Button>().onClick.AddListener(Reset);
resetObj.SetActive(true);
}
}
}
}