Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 3d61011

Browse files
committed
Move Time Scale Widget into separate class
And change "pause" to "lock" behaviour. Added patch to implement locking feature.
1 parent 5285239 commit 3d61011

File tree

2 files changed

+126
-68
lines changed

2 files changed

+126
-68
lines changed

src/UI/UIManager.cs

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityExplorer.CSConsole;
66
using UnityExplorer.Inspectors;
77
using UnityExplorer.UI.Panels;
8+
using UnityExplorer.UI.Widgets;
89
using UnityExplorer.UI.Widgets.AutoComplete;
910
using UniverseLib;
1011
using UniverseLib.Input;
@@ -54,10 +55,7 @@ public enum VerticalAnchor
5455
private static readonly Vector2 NAVBAR_DIMENSIONS = new(1020f, 35f);
5556

5657
private static ButtonRef closeBtn;
57-
private static ButtonRef pauseBtn;
58-
private static InputFieldRef timeInput;
59-
private static bool pauseButtonPausing;
60-
private static float lastTimeScale;
58+
private static TimeScaleWidget timeScaleWidget;
6159

6260
private static int lastScreenWidth;
6361
private static int lastScreenHeight;
@@ -141,20 +139,7 @@ public static void Update()
141139
UniverseLib.Config.ConfigManager.Force_Unlock_Mouse = !UniverseLib.Config.ConfigManager.Force_Unlock_Mouse;
142140

143141
// update the timescale value
144-
if (!timeInput.Component.isFocused && lastTimeScale != Time.timeScale)
145-
{
146-
if (pauseButtonPausing && Time.timeScale != 0.0f)
147-
{
148-
pauseButtonPausing = false;
149-
OnPauseButtonToggled();
150-
}
151-
152-
if (!pauseButtonPausing)
153-
{
154-
timeInput.Text = Time.timeScale.ToString("F2");
155-
lastTimeScale = Time.timeScale;
156-
}
157-
}
142+
timeScaleWidget.Update();
158143

159144
// check screen dimension change
160145
Display display = DisplayManager.ActiveDisplay;
@@ -232,41 +217,7 @@ private static void Master_Toggle_OnValueChanged(KeyCode val)
232217
closeBtn.ButtonText.text = val.ToString();
233218
}
234219

235-
// Time controls
236-
237-
private static void OnTimeInputEndEdit(string val)
238-
{
239-
if (pauseButtonPausing)
240-
return;
241-
242-
if (float.TryParse(val, out float f))
243-
{
244-
Time.timeScale = f;
245-
lastTimeScale = f;
246-
}
247-
248-
timeInput.Text = Time.timeScale.ToString("F2");
249-
}
250-
251-
private static void OnPauseButtonClicked()
252-
{
253-
pauseButtonPausing = !pauseButtonPausing;
254-
255-
Time.timeScale = pauseButtonPausing ? 0f : lastTimeScale;
256-
257-
OnPauseButtonToggled();
258-
}
259-
260-
private static void OnPauseButtonToggled()
261-
{
262-
timeInput.Component.text = Time.timeScale.ToString("F2");
263-
timeInput.Component.readOnly = pauseButtonPausing;
264-
timeInput.Component.textComponent.color = pauseButtonPausing ? Color.grey : Color.white;
265-
266-
Color color = pauseButtonPausing ? new Color(0.3f, 0.3f, 0.2f) : new Color(0.2f, 0.2f, 0.2f);
267-
RuntimeHelper.SetColorBlock(pauseBtn.Component, color, color * 1.2f, color * 0.7f);
268-
pauseBtn.ButtonText.text = pauseButtonPausing ? "►" : "||";
269-
}
220+
270221

271222
// UI Construction
272223

@@ -298,26 +249,17 @@ private static void CreateTopNavBar()
298249
UIFactory.SetLayoutElement(NavbarTabButtonHolder, minHeight: 25, flexibleHeight: 999, flexibleWidth: 999);
299250
UIFactory.SetLayoutGroup<HorizontalLayoutGroup>(NavbarTabButtonHolder, false, true, true, true, 4, 2, 2, 2, 2);
300251

301-
// Time controls
302-
303-
Text timeLabel = UIFactory.CreateLabel(navbarPanel, "TimeLabel", "Time:", TextAnchor.MiddleRight, Color.grey);
304-
UIFactory.SetLayoutElement(timeLabel.gameObject, minHeight: 25, minWidth: 50);
305-
306-
timeInput = UIFactory.CreateInputField(navbarPanel, "TimeInput", "timeScale");
307-
UIFactory.SetLayoutElement(timeInput.Component.gameObject, minHeight: 25, minWidth: 40);
308-
timeInput.Component.GetOnEndEdit().AddListener(OnTimeInputEndEdit);
309-
310-
timeInput.Text = string.Empty;
311-
timeInput.Text = Time.timeScale.ToString();
252+
// Time scale widget
253+
timeScaleWidget = new(navbarPanel);
312254

313-
pauseBtn = UIFactory.CreateButton(navbarPanel, "PauseButton", "||", new Color(0.2f, 0.2f, 0.2f));
314-
UIFactory.SetLayoutElement(pauseBtn.Component.gameObject, minHeight: 25, minWidth: 25);
315-
pauseBtn.OnClick += OnPauseButtonClicked;
255+
//spacer
256+
GameObject spacer = UIFactory.CreateUIObject("Spacer", navbarPanel);
257+
UIFactory.SetLayoutElement(spacer, minWidth: 15);
316258

317259
// Hide menu button
318260

319261
closeBtn = UIFactory.CreateButton(navbarPanel, "CloseButton", ConfigManager.Master_Toggle.Value.ToString());
320-
UIFactory.SetLayoutElement(closeBtn.Component.gameObject, minHeight: 25, minWidth: 80, flexibleWidth: 0);
262+
UIFactory.SetLayoutElement(closeBtn.Component.gameObject, minHeight: 25, minWidth: 60, flexibleWidth: 0);
321263
RuntimeHelper.SetColorBlock(closeBtn.Component, new Color(0.63f, 0.32f, 0.31f),
322264
new Color(0.81f, 0.25f, 0.2f), new Color(0.6f, 0.18f, 0.16f));
323265

src/UI/Widgets/TimeScaleWidget.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using HarmonyLib;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using UnityEngine;
8+
using UnityEngine.UI;
9+
using UniverseLib;
10+
using UniverseLib.UI;
11+
using UniverseLib.UI.Models;
12+
using UniverseLib.Utility;
13+
14+
namespace UnityExplorer.UI.Widgets
15+
{
16+
internal class TimeScaleWidget
17+
{
18+
public TimeScaleWidget(GameObject parent)
19+
{
20+
Instance = this;
21+
22+
ConstructUI(parent);
23+
24+
InitPatch();
25+
}
26+
27+
static TimeScaleWidget Instance;
28+
29+
ButtonRef lockBtn;
30+
bool locked;
31+
InputFieldRef timeInput;
32+
float desiredTime;
33+
bool settingTimeScale;
34+
35+
public void Update()
36+
{
37+
// Fallback in case Time.timeScale patch failed for whatever reason
38+
if (locked)
39+
Time.timeScale = desiredTime;
40+
41+
if (!timeInput.Component.isFocused)
42+
timeInput.Text = Time.timeScale.ToString("F2");
43+
}
44+
45+
void SetTimeScale(float time)
46+
{
47+
settingTimeScale = true;
48+
Time.timeScale = time;
49+
settingTimeScale = false;
50+
}
51+
52+
// UI event listeners
53+
54+
void OnTimeInputEndEdit(string val)
55+
{
56+
if (float.TryParse(val, out float f))
57+
{
58+
SetTimeScale(f);
59+
desiredTime = f;
60+
}
61+
}
62+
63+
void OnPauseButtonClicked()
64+
{
65+
OnTimeInputEndEdit(timeInput.Text);
66+
67+
locked = !locked;
68+
69+
Color color = locked ? new Color(0.3f, 0.3f, 0.2f) : new Color(0.2f, 0.2f, 0.2f);
70+
RuntimeHelper.SetColorBlock(lockBtn.Component, color, color * 1.2f, color * 0.7f);
71+
lockBtn.ButtonText.text = locked ? "Unlock" : "Lock";
72+
}
73+
74+
// UI Construction
75+
76+
void ConstructUI(GameObject parent)
77+
{
78+
Text timeLabel = UIFactory.CreateLabel(parent, "TimeLabel", "Time:", TextAnchor.MiddleRight, Color.grey);
79+
UIFactory.SetLayoutElement(timeLabel.gameObject, minHeight: 25, minWidth: 35);
80+
81+
timeInput = UIFactory.CreateInputField(parent, "TimeInput", "timeScale");
82+
UIFactory.SetLayoutElement(timeInput.Component.gameObject, minHeight: 25, minWidth: 40);
83+
timeInput.Component.GetOnEndEdit().AddListener(OnTimeInputEndEdit);
84+
85+
timeInput.Text = string.Empty;
86+
timeInput.Text = Time.timeScale.ToString();
87+
88+
lockBtn = UIFactory.CreateButton(parent, "PauseButton", "Lock", new Color(0.2f, 0.2f, 0.2f));
89+
UIFactory.SetLayoutElement(lockBtn.Component.gameObject, minHeight: 25, minWidth: 50);
90+
lockBtn.OnClick += OnPauseButtonClicked;
91+
}
92+
93+
// Only allow Time.timeScale to be set if the user hasn't "locked" it or if we are setting the value internally.
94+
95+
static void InitPatch()
96+
{
97+
98+
try
99+
{
100+
MethodInfo target = AccessTools.Method(typeof(Time), nameof(Time.timeScale));
101+
#if CPP
102+
if (UnhollowerBaseLib.UnhollowerUtils.GetIl2CppMethodInfoPointerFieldForGeneratedMethod(target) == null)
103+
return;
104+
#endif
105+
ExplorerCore.Harmony.Patch(target,
106+
prefix: new(AccessTools.Method(typeof(TimeScaleWidget), nameof(Prefix_Time_set_timeScale))));
107+
}
108+
catch { }
109+
}
110+
111+
static bool Prefix_Time_set_timeScale()
112+
{
113+
return !Instance.locked || Instance.settingTimeScale;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)