-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitLayout.cs
More file actions
256 lines (222 loc) · 9.16 KB
/
Copy pathSplitLayout.cs
File metadata and controls
256 lines (222 loc) · 9.16 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
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
using System.Collections.Generic;
using BepInEx.Logging;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace MoveControllerAxisSplit
{
internal enum Axis
{
X,
Z
}
internal sealed class SplitLayout
{
private sealed class TextState
{
internal Text Text;
internal bool Enabled;
}
private readonly ManualLogSource _logger;
private readonly MoveControllerBridge _bridge;
private readonly List<TextState> _originalTextStates = new List<TextState>();
private Button _sourceButton;
private Button _zButton;
private GameObject _xLabel;
private Vector2 _originalSizeDelta;
private Vector2 _originalAnchoredPosition;
internal bool IsApplied
{
get { return _sourceButton != null; }
}
internal bool IsAlive
{
get { return _sourceButton != null && _sourceButton.gameObject != null; }
}
internal SplitLayout(ManualLogSource logger, MoveControllerBridge bridge)
{
_logger = logger;
_bridge = bridge;
}
internal bool Apply(Button sourceButton, float gap)
{
if (sourceButton == null || IsApplied)
return false;
RectTransform sourceRect = sourceButton.transform as RectTransform;
if (sourceRect == null)
return false;
try
{
_sourceButton = sourceButton;
_originalSizeDelta = sourceRect.sizeDelta;
_originalAnchoredPosition = sourceRect.anchoredPosition;
CaptureAndHideOriginalText(sourceButton);
GameObject clone = Object.Instantiate(sourceButton.gameObject) as GameObject;
if (clone == null)
throw new UnityException("Could not clone the Move XZ control.");
clone.name = "MoveZ";
clone.transform.SetParent(sourceRect.parent, false);
_zButton = clone.GetComponent<Button>();
if (_zButton == null)
throw new UnityException("The cloned Move Z control has no Button component.");
RectTransform zRect = clone.transform as RectTransform;
CopyRectTransform(sourceRect, zRect);
RemoveEventTriggers(sourceButton.gameObject);
RemoveEventTriggers(clone);
ClearButtonClick(sourceButton);
ClearButtonClick(_zButton);
HideAllText(_zButton);
SplitVertically(sourceRect, zRect, gap);
_xLabel = CreateLabel(sourceButton, "\u2190 Move X \u2192");
CreateLabel(_zButton, "\u2190 Move Z \u2192");
if (!_bridge.BindAxisButton(sourceButton, Axis.X) ||
!_bridge.BindAxisButton(_zButton, Axis.Z))
{
throw new UnityException("Could not bind one or more split controls.");
}
Canvas.ForceUpdateCanvases();
return true;
}
catch (System.Exception ex)
{
_logger.LogError("Could not split the Move XZ control: " + ex.Message);
RestoreOriginal();
return false;
}
}
internal void RestoreOriginal()
{
if (_sourceButton == null)
{
ForgetDestroyedLayout();
return;
}
try
{
if (_zButton != null)
Object.DestroyImmediate(_zButton.gameObject);
if (_xLabel != null)
Object.DestroyImmediate(_xLabel);
RectTransform sourceRect = _sourceButton.transform as RectTransform;
if (sourceRect != null)
{
sourceRect.sizeDelta = _originalSizeDelta;
sourceRect.anchoredPosition = _originalAnchoredPosition;
}
RestoreOriginalText();
RemoveEventTriggers(_sourceButton.gameObject);
ClearButtonClick(_sourceButton);
_bridge.BindOriginalXZ(_sourceButton);
Canvas.ForceUpdateCanvases();
}
catch (System.Exception ex)
{
_logger.LogWarning("Could not fully restore the original Move XZ control: " + ex.Message);
}
finally
{
ForgetDestroyedLayout();
}
}
internal void ForgetDestroyedLayout()
{
_sourceButton = null;
_zButton = null;
_xLabel = null;
_originalTextStates.Clear();
}
private void CaptureAndHideOriginalText(Button button)
{
_originalTextStates.Clear();
Text[] texts = button.GetComponentsInChildren<Text>(true);
for (int i = 0; i < texts.Length; i++)
{
TextState state = new TextState();
state.Text = texts[i];
state.Enabled = texts[i].enabled;
_originalTextStates.Add(state);
texts[i].enabled = false;
}
}
private void RestoreOriginalText()
{
for (int i = 0; i < _originalTextStates.Count; i++)
{
TextState state = _originalTextStates[i];
if (state.Text != null)
state.Text.enabled = state.Enabled;
}
}
private static void HideAllText(Button button)
{
Text[] texts = button.GetComponentsInChildren<Text>(true);
for (int i = 0; i < texts.Length; i++)
texts[i].enabled = false;
}
private static GameObject CreateLabel(Button button, string label)
{
Text template = null;
Text[] texts = button.GetComponentsInChildren<Text>(true);
if (texts.Length > 0)
template = texts[0];
GameObject labelObject = new GameObject("AxisSplitLabel", typeof(RectTransform), typeof(Text));
RectTransform rect = labelObject.GetComponent<RectTransform>();
rect.SetParent(button.transform, false);
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.offsetMin = new Vector2(2f, 1f);
rect.offsetMax = new Vector2(-2f, -1f);
rect.localScale = Vector3.one;
Text text = labelObject.GetComponent<Text>();
text.text = label;
text.alignment = TextAnchor.MiddleCenter;
text.raycastTarget = false;
text.color = template != null ? template.color : Color.black;
text.font = template != null ? template.font : Resources.GetBuiltinResource<Font>("Arial.ttf");
text.fontStyle = template != null ? template.fontStyle : FontStyle.Normal;
text.fontSize = template != null ? template.fontSize : 12;
text.resizeTextForBestFit = true;
text.resizeTextMinSize = 7;
text.resizeTextMaxSize = Mathf.Max(10, text.fontSize);
return labelObject;
}
private static void SplitVertically(RectTransform xRect, RectTransform zRect, float gap)
{
float originalHeight = xRect.rect.height;
if (originalHeight <= 1f)
originalHeight = Mathf.Abs(xRect.sizeDelta.y);
float splitHeight = Mathf.Max(8f, (originalHeight - gap) * 0.5f);
float pivotY = xRect.pivot.y;
float originalTop = xRect.anchoredPosition.y + ((1f - pivotY) * originalHeight);
float originalBottom = xRect.anchoredPosition.y - (pivotY * originalHeight);
xRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, splitHeight);
zRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, splitHeight);
Vector2 xPosition = xRect.anchoredPosition;
xPosition.y = originalTop - ((1f - pivotY) * splitHeight);
xRect.anchoredPosition = xPosition;
Vector2 zPosition = zRect.anchoredPosition;
zPosition.y = originalBottom + (pivotY * splitHeight);
zRect.anchoredPosition = zPosition;
}
private static void CopyRectTransform(RectTransform source, RectTransform target)
{
target.anchorMin = source.anchorMin;
target.anchorMax = source.anchorMax;
target.pivot = source.pivot;
target.sizeDelta = source.sizeDelta;
target.anchoredPosition = source.anchoredPosition;
target.localScale = source.localScale;
target.localRotation = source.localRotation;
}
private static void RemoveEventTriggers(GameObject gameObject)
{
EventTrigger[] triggers = gameObject.GetComponents<EventTrigger>();
for (int i = 0; i < triggers.Length; i++)
Object.DestroyImmediate(triggers[i]);
}
private static void ClearButtonClick(Button button)
{
button.onClick = new Button.ButtonClickedEvent();
}
}
}