Files
spacerace/Assets/Src/UI/TimingUI.cs
T
2023-08-01 18:04:18 +10:00

87 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
public class TimingUI : MonoBehaviour {
UIDocument uiDocument;
[SerializeField]
ShipControls ship;
[SerializeField]
Timing timing;
Label timer;
Label pb;
VisualElement sectors;
VisualElement timeSheet;
void OnEnable() {
Init();
}
void Init() {
uiDocument = GetComponent<UIDocument>();
timer = uiDocument.rootVisualElement.Q<Label>("timer");
pb = uiDocument.rootVisualElement.Q<Label>("pb");
sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors");
pb.text = "-";
}
void FixedUpdate() {
if (timer == null) {
Debug.LogWarning("no speed or timer");
Init();
return;
}
timer.text = $"{TimeSpan.FromSeconds(timing.lapTime):m\\:ss\\.fff}";
// clearly this is not ideal but idk atm
UpdateUI();
}
public void UpdateUI() {
if (sectors == null)
Debug.LogWarning("null sectors");
Lap pbLap = timing.PB();
if (pbLap != null)
pb.text = $"{pbLap.ToString()}";
sectors.Clear();
foreach (Sector sector in timing.sectors) {
Label l = timing.sectorTimes
.Where(s => s.Sector == sector.sector)
.Select(s => SectorLabel(s))
.DefaultIfEmpty(new Label("-"))
.Single();
sectors.Add(l);
}
}
Label SectorLabel(SectorTime s) {
double diff = timing.PBSectorDiff(s);
string sign = diff switch {
< 0 => "",
_ => "+",
};
SectorColour colour = diff switch {
< 0 => SectorColour.Green,
_ => SectorColour.Yellow,
};
Label l = new Label($"{sign}{diff:0.000}");
string colourClass = colour.ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l;
}
}