108 lines
2.8 KiB
C#
108 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class TimingUI : MonoBehaviour {
|
|
[SerializeField]
|
|
ShipControls ship;
|
|
|
|
Timing timing;
|
|
UIDocument uiDocument;
|
|
|
|
VisualElement timer;
|
|
VisualElement timerSectors;
|
|
Label timerTime;
|
|
Label timerPb;
|
|
|
|
VisualElement notice;
|
|
VisualElement noticeSectors;
|
|
VisualElement noticeDiff;
|
|
Label noticeTime;
|
|
|
|
void OnEnable() {
|
|
Init();
|
|
}
|
|
|
|
void Init() {
|
|
uiDocument = GetComponent<UIDocument>();
|
|
timing = GetComponent<Timing>();
|
|
|
|
notice = uiDocument.rootVisualElement.Q<VisualElement>("notice");
|
|
noticeSectors = uiDocument.rootVisualElement.Q<VisualElement>("noticeSectors");
|
|
|
|
timer = uiDocument.rootVisualElement.Q<VisualElement>("timer");
|
|
timerSectors = uiDocument.rootVisualElement.Q<VisualElement>("timerSectors");
|
|
|
|
noticeTime = uiDocument.rootVisualElement.Q<Label>("noticeTime");
|
|
noticeDiff = uiDocument.rootVisualElement.Q<VisualElement>("noticeDiff");
|
|
|
|
timerTime = uiDocument.rootVisualElement.Q<Label>("timerTime");
|
|
timerPb = uiDocument.rootVisualElement.Q<Label>("timerPb");
|
|
|
|
timerPb.text = "-";
|
|
|
|
notice.AddToClassList("hidden");
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (timer == null) {
|
|
Debug.LogWarning("no timer");
|
|
Init();
|
|
return;
|
|
}
|
|
|
|
timerTime.text = $"{TimeSpan.FromSeconds(timing.lapTime):m\\:ss\\.f}";
|
|
|
|
// clearly this is not ideal but idk atm
|
|
UpdateUI();
|
|
}
|
|
|
|
public void UpdateUI() {
|
|
if (timerSectors == null)
|
|
Debug.LogWarning("null timerSectors");
|
|
|
|
Lap timerPbLap = timing.PB();
|
|
if (timerPbLap != null)
|
|
timerPb.text = $"{timerPbLap.ToString()}";
|
|
|
|
timerSectors.Clear();
|
|
|
|
foreach (Sector sector in timing.sectors) {
|
|
Label l = timing.sectorTimes
|
|
.Where(s => s.Sector == sector.sector)
|
|
.Select(s => timing.PBSectorDiff(s).ToLabel())
|
|
.DefaultIfEmpty(new Label("-"))
|
|
.Single();
|
|
|
|
timerSectors.Add(l);
|
|
}
|
|
}
|
|
|
|
public void ShowRecentLap(Lap lap) {
|
|
timer.AddToClassList("hidden");
|
|
notice.RemoveFromClassList("hidden");
|
|
|
|
// fine
|
|
noticeTime.text = lap.ToString();
|
|
|
|
// should show the diff to the PB
|
|
// or - if there is only 1 lap
|
|
noticeDiff.Clear();
|
|
noticeDiff.Add(timing.PBLapDiff(lap).ToLabel());
|
|
|
|
noticeSectors.Clear();
|
|
foreach (SectorTime sector in lap.Sectors) {
|
|
Label l = timing.PBSectorDiff(sector).ToLabel();
|
|
noticeSectors.Add(l);
|
|
}
|
|
}
|
|
|
|
public void HideRecentLap() {
|
|
timer.RemoveFromClassList("hidden");
|
|
notice.AddToClassList("hidden");
|
|
}
|
|
}
|