-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerDisplay.cs
More file actions
172 lines (137 loc) · 5.12 KB
/
TimerDisplay.cs
File metadata and controls
172 lines (137 loc) · 5.12 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
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
namespace TimerMod;
public class TimerDisplay
{
private GameObject canvas;
private Text? text;
private Text? pbText;
private Font? theFont;
private bool timerVisible = true;
private ConfigEntry<int> timerTextSize;
private ConfigEntry<int> pbTextSize;
private ConfigEntry<Vector2> timerTextPos;
private ConfigEntry<Vector2> pbTextPos;
public TimerDisplay()
{
ConfigFile config = TimerMod.config;
timerTextSize = config.Bind("UI", "Timer Text Size", 50, "");
pbTextSize = config.Bind("UI", "Pb Text Size", 25, "");
timerTextPos = config.Bind("UI", "Timer Text Position", new Vector2(0.03f, 0.04f), "");
pbTextPos = config.Bind("UI", "Pb Text Position", new Vector2(0.03f, 0.09f), "");
timerTextSize.SettingChanged += (_, _) => {setAllConfigs();};
pbTextSize.SettingChanged += (_, _) => {setAllConfigs();};
timerTextPos.SettingChanged += (_, _) => {setAllConfigs();};
pbTextPos.SettingChanged += (_, _) => {setAllConfigs();};
canvas = new GameObject("TimerModCanvas");
UnityEngine.Object.DontDestroyOnLoad(canvas);
}
public void setup()
{
var allFonts = Resources.FindObjectsOfTypeAll<Font>();
theFont = allFonts.FirstOrDefault(x => x.name == "TrajanPro-Regular");
setupCanvas();
text = createText("TimerText", "0:00.00", timerTextSize.Value, timerTextPos.Value);
pbText = createText("TimerPBText", "PB: 0:00.00", pbTextSize.Value, pbTextPos.Value);
}
private void setAllConfigs()
{
if (text == null || pbText == null)
return;
text.fontSize = timerTextSize.Value;
pbText.fontSize = pbTextSize.Value;
updateTextPos(text, timerTextPos.Value);
updateTextPos(pbText, pbTextPos.Value);
}
private void setupCanvas()
{
var canvasC = canvas.AddComponent<UnityEngine.Canvas>();
canvasC.renderMode = RenderMode.ScreenSpaceOverlay;
// canvasC.worldCamera = GameCameras.SilentInstance.hudCamera;
canvasC.planeDistance = 20;
canvas.layer = LayerMask.NameToLayer("UI");
CanvasScaler scaler = canvas.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(1920f, 1080f);
canvas.AddComponent<GraphicRaycaster>();
RectTransform canvasRect = canvas.GetComponent<RectTransform>();
canvasRect.anchorMin = new Vector2(0, 0);
canvasRect.anchorMax = new Vector2(1, 1);
canvasRect.sizeDelta = new Vector2(0, 0);
}
public Text createText(string name, string defaultText, int fontSize, Vector2 position)
{
GameObject textObject = new GameObject(name);
textObject.layer = LayerMask.NameToLayer("UI");
textObject.AddComponent<CanvasRenderer>();
RectTransform textTransform = textObject.AddComponent<RectTransform>();
textTransform.localPosition = Vector3.zero;
CanvasGroup group = textObject.AddComponent<CanvasGroup>();
group.interactable = false;
group.blocksRaycasts = false;
Text text = textObject.AddComponent<Text>();
text.text = defaultText;
text.font = theFont;
text.fontSize = fontSize;
text.fontStyle = FontStyle.Normal;
textObject.transform.SetParent(canvas.transform, false);
updateTextPos(text, position);
return text;
}
private void updateTextPos(Text t, Vector2 position)
{
RectTransform transform = t.gameObject.GetComponent<RectTransform>();
// 4 combinations, there probably exists a better way but it is alright
if (position.y > 0.5f)
{
if (position.x > 0.5f)
{
t.alignment = TextAnchor.UpperRight;
transform.anchorMin = new Vector2(0f, 0f);
transform.anchorMax = position;
}
else
{
t.alignment = TextAnchor.UpperLeft;
transform.anchorMin = new Vector2(position.x, 0f);
transform.anchorMax = new Vector2(1f, position.y);
}
}
else
{
if (position.x > 0.5f)
{
t.alignment = TextAnchor.LowerRight;
transform.anchorMin = new Vector2(0f, position.y);
transform.anchorMax = new Vector2(position.x, 1f);
}
else
{
t.alignment = TextAnchor.LowerLeft;
transform.anchorMin = position;
transform.anchorMax = new Vector2(1f, 1f);
}
}
}
public void toggleVisibility()
{
canvas.SetActive(timerVisible);
timerVisible = !timerVisible;
}
public void setTime(string time)
{
if (text == null)
return;
text.text = time;
}
public void setPbTime(string time)
{
if (pbText == null)
return;
pbText.text = "PB: " + time;
}
}