129 lines
3.1 KiB
C#
Raw Normal View History

2020-12-20 10:56:15 +00:00
using OWML.Utils;
using QSB.Utility;
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;
private Enum _currentReason;
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 oldScene, OWScene newScene)
2020-11-26 13:09:34 +00:00
{
_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;
2021-08-28 12:55:23 +01:00
if (_canvas != null && _canvas.enabled)
2020-11-25 09:54:52 +00:00
{
Canvas.willRenderCanvases -= OnWillRenderCanvases;
}
}
public static void Start(TimeSyncType type, Enum reason) =>
QSBCore.UnityEvents.RunWhen(() => Instance._isSetUp, () => Instance.StartTimeSync(type, reason));
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
private void StartTimeSync(TimeSyncType type, Enum reason)
2020-11-25 09:54:52 +00:00
{
if (!QSBSceneManager.IsInUniverse)
{
DebugLog.ToConsole("Error - Tried to start time sync UI when not in universe!", OWML.Common.MessageType.Error);
return;
}
2020-11-26 19:46:09 +00:00
_currentType = type;
_currentReason = reason;
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;
2021-07-29 23:44:59 +01:00
var milliseconds = totalSeconds % 1 * 1000;
2020-11-26 19:46:09 +00:00
var text = "";
switch (_currentType)
{
case TimeSyncType.Fastforwarding:
switch ((FastForwardReason)_currentReason)
{
case FastForwardReason.TooFarBehind:
text = $"{minutes:D2}:{seconds:D2}.{milliseconds:000}"
+ Environment.NewLine
+ "Fast-forwarding to match server time...";
break;
}
2021-08-22 17:17:46 +01:00
2020-11-26 19:46:09 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-11-26 19:46:09 +00:00
case TimeSyncType.Pausing:
switch ((PauseReason)_currentReason)
{
case PauseReason.ServerNotStarted:
text = "Waiting for server to start...";
break;
case PauseReason.TooFarAhead:
text = "Pausing to match server time...";
break;
case PauseReason.WaitingForAllPlayersToBeReady:
text = "Waiting for start of loop...";
break;
case PauseReason.WaitingForAllPlayersToDie:
text = "Waiting for end of loop...";
break;
}
2021-08-22 17:17:46 +01:00
2021-05-09 19:55:19 +01:00
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
}
}
}