110 lines
2.7 KiB
C#
110 lines
2.7 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;
|
|
Label recentTime;
|
|
Label recentDiff;
|
|
|
|
VisualElement pbCompare;
|
|
VisualElement currentLap;
|
|
VisualElement sectors;
|
|
|
|
void OnEnable() {
|
|
Init();
|
|
}
|
|
|
|
void Init() {
|
|
uiDocument = GetComponent<UIDocument>();
|
|
|
|
pbCompare = uiDocument.rootVisualElement.Q<VisualElement>("pbCompare");
|
|
currentLap = uiDocument.rootVisualElement.Q<VisualElement>("currentLap");
|
|
sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors");
|
|
|
|
recentTime = uiDocument.rootVisualElement.Q<Label>("recentTime");
|
|
recentDiff = uiDocument.rootVisualElement.Q<Label>("recentDiff");
|
|
timer = uiDocument.rootVisualElement.Q<Label>("timer");
|
|
pb = uiDocument.rootVisualElement.Q<Label>("pb");
|
|
pb.text = "-";
|
|
|
|
|
|
pbCompare.AddToClassList("hidden");
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (timer == null) {
|
|
Debug.LogWarning("no 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);
|
|
}
|
|
|
|
// if (timing.recentLap != null) ShowRecentLap()
|
|
}
|
|
|
|
// yadad yadad
|
|
void ShowRecentLap() {
|
|
currentLap.AddToClassList("hidden");
|
|
pbCompare.RemoveFromClassList("hidden");
|
|
|
|
recentTime.text = timing.recentLap.ToString();
|
|
// recentDiff = yadad
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|