100 lines
2.3 KiB
C#
Raw Normal View History

2020-12-20 10:56:15 +00:00
using OWML.Utils;
2020-11-26 13:09:34 +00:00
using System;
using UnityEngine;
2020-11-25 09:54:52 +00:00
using UnityEngine.UI;
namespace QSB.TimeSync
{
2020-12-03 08:28:05 +00:00
internal class TimeSyncUI : MonoBehaviour
2020-11-25 09:54:52 +00:00
{
2020-11-26 19:46:09 +00:00
public static TimeSyncUI Instance;
2020-11-25 09:54:52 +00:00
2020-12-19 19:06:13 +00:00
public static float TargetTime;
2020-11-25 09:54:52 +00:00
private Canvas _canvas;
private Text _text;
2020-11-26 11:43:35 +00:00
private float _startTime;
2020-11-26 13:09:34 +00:00
private bool _isSetUp;
2020-11-26 19:46:09 +00:00
private TimeSyncType _currentType;
2020-11-25 09:54:52 +00:00
2020-12-14 21:41:56 +01:00
public void Awake()
2020-11-25 09:54:52 +00:00
{
Instance = this;
enabled = false;
2020-11-26 13:09:34 +00:00
QSBSceneManager.OnUniverseSceneLoaded += OnUniverseSceneLoad;
}
private void OnUniverseSceneLoad(OWScene scene)
{
_isSetUp = true;
var obj = Resources.FindObjectsOfTypeAll<SleepTimerUI>()[0];
_canvas = obj.GetValue<Canvas>("_canvas");
_text = obj.GetValue<Text>("_text");
2020-11-25 09:54:52 +00:00
_canvas.enabled = false;
}
2020-12-14 21:41:56 +01:00
public void OnDestroy()
2020-11-25 09:54:52 +00:00
{
2020-11-26 13:09:34 +00:00
QSBSceneManager.OnUniverseSceneLoaded -= OnUniverseSceneLoad;
if (_canvas != null & _canvas.enabled)
2020-11-25 09:54:52 +00:00
{
Canvas.willRenderCanvases -= OnWillRenderCanvases;
}
}
2020-12-14 21:41:56 +01:00
public static void Start(TimeSyncType type) =>
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(() => Instance._isSetUp, () => Instance.StartTimeSync(type));
2020-12-03 08:28:05 +00:00
2020-12-14 21:41:56 +01:00
public static void Stop() =>
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(() => Instance._isSetUp, () => Instance.EndTimeSync());
2020-11-25 09:54:52 +00:00
2020-11-26 19:46:09 +00:00
private void StartTimeSync(TimeSyncType type)
2020-11-25 09:54:52 +00:00
{
2020-11-26 19:46:09 +00:00
_currentType = type;
2020-11-26 11:43:35 +00:00
_startTime = Time.timeSinceLevelLoad;
2020-11-25 09:54:52 +00:00
enabled = true;
_canvas.enabled = true;
Canvas.willRenderCanvases += OnWillRenderCanvases;
}
2020-11-26 19:46:09 +00:00
private void EndTimeSync()
2020-11-25 09:54:52 +00:00
{
2020-11-26 19:46:09 +00:00
_currentType = TimeSyncType.None;
2020-11-25 09:54:52 +00:00
enabled = false;
_canvas.enabled = false;
Canvas.willRenderCanvases -= OnWillRenderCanvases;
}
private void OnWillRenderCanvases()
{
2020-11-26 13:09:34 +00:00
if (!_isSetUp)
{
return;
}
2021-06-18 22:38:32 +01:00
2020-12-19 19:06:13 +00:00
var totalSeconds = Mathf.Max(TargetTime - Time.timeSinceLevelLoad, 0f);
2020-11-25 09:54:52 +00:00
var minutes = Mathf.FloorToInt(totalSeconds / 60f);
var seconds = Mathf.FloorToInt(totalSeconds) % 60;
2020-11-26 19:46:09 +00:00
var text = "";
switch (_currentType)
{
case TimeSyncType.Fastforwarding:
2020-12-14 21:41:56 +01:00
text = $"{minutes:D2}:{seconds:D2}"
2020-11-26 19:46:09 +00:00
+ Environment.NewLine
+ "Fast-forwarding to match server time...";
break;
2020-12-03 08:28:05 +00:00
2020-11-26 19:46:09 +00:00
case TimeSyncType.Pausing:
text = "Pausing to match server time...";
break;
2021-05-09 19:55:19 +01:00
case TimeSyncType.WaitForServerLoop:
text = "Waiting for server...";
break;
2020-11-26 19:46:09 +00:00
}
2021-06-18 22:38:32 +01:00
2020-11-26 19:46:09 +00:00
_text.text = text;
2020-11-25 09:54:52 +00:00
}
}
}