From c61fd84f9024b62a1857b027350feed31d8366cb Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 4 Aug 2023 16:00:10 +1000 Subject: [PATCH] pog --- Assets/Src/Laps.cs | 60 ++++++++++++++++++++++++++------------- Assets/Src/Timing.cs | 14 ++++----- Assets/Src/UI/Timing.uxml | 4 ++- Assets/Src/UI/TimingUI.cs | 29 ++++--------------- 4 files changed, 57 insertions(+), 50 deletions(-) diff --git a/Assets/Src/Laps.cs b/Assets/Src/Laps.cs index b7930d8..e50ef20 100644 --- a/Assets/Src/Laps.cs +++ b/Assets/Src/Laps.cs @@ -4,6 +4,7 @@ using System.Linq; using System.IO; using UnityEngine; +using UnityEngine.UIElements; public enum NoticeCategory { Initial, @@ -12,27 +13,42 @@ public enum NoticeCategory { Fastest, } -// public record TimingNotice( -// double Seconds, -// NoticeCategory Category, -// ) { -// Label ToLabel() { -// Label l = Category switch { -// DiffType.Slower => new Label($"+{diff:0.000}"), -// DiffType.Faster => new Label($"-{diff:0.000}"), -// DiffType.Fastest => new Label($"-{diff:0.000}"), -// }; +public class TimingNotice { + public double Seconds; + NoticeCategory Category = NoticeCategory.Initial; -// string colourClass = Category Switch { -// DiffType.Slower => "yellow", -// DiffType.Faster => "green", -// DiffType.Fastest => "purple", -// }; + public TimingNotice Compare(double comparison) { + double s = Math.Abs(comparison - Seconds); + NoticeCategory cat = comparison switch { + double c when Seconds < c => NoticeCategory.Faster, + _ => NoticeCategory.Slower, + }; -// l.AddToClassList(colourClass); -// return l; -// } -// } + return new TimingNotice { + Seconds = s, + Category = cat, + }; + } + + public Label ToLabel() { + Label l = Category switch { + NoticeCategory.Slower => new Label($"+{Seconds:0.000}"), + NoticeCategory.Faster => new Label($"-{Seconds:0.000}"), + NoticeCategory.Fastest => new Label($"-{Seconds:0.000}"), + NoticeCategory.Initial => new Label($"{TimeSpan.FromSeconds(Seconds):m\\:ss\\.fff}"), + }; + + string colourClass = Category switch { + NoticeCategory.Slower => "yellow", + NoticeCategory.Faster => "green", + NoticeCategory.Fastest => "purple", + NoticeCategory.Initial => "", + }; + + l.AddToClassList(colourClass); + return l; + } +} public enum DiffColour { Green, @@ -126,4 +142,10 @@ static class LapStorageService { return lapsByMap[mapName].Laps .MinBy(l => l.TotalTime); } +} + +// Add this magic code, you will be able to use record and init. +// I only tested in editor, not sure whether it would work on other platforms. +namespace System.Runtime.CompilerServices { + class IsExternalInit {} } \ No newline at end of file diff --git a/Assets/Src/Timing.cs b/Assets/Src/Timing.cs index 3e1b76b..d804295 100644 --- a/Assets/Src/Timing.cs +++ b/Assets/Src/Timing.cs @@ -129,17 +129,17 @@ public class Timing: MonoBehaviour { return LapStorageService.PB(mapName); } - public double PBSectorDiff(SectorTime s) => PB() switch { + public TimingNotice PBSectorDiff(SectorTime s) => PB() switch { Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch { - SectorTime fastestSector => s.Seconds - fastestSector.Seconds, - null => -1d, + SectorTime fastestSector => (new TimingNotice { Seconds = s.Seconds }).Compare(fastestSector.Seconds), + null => new TimingNotice { Seconds = s.Seconds }, // this one should never happen }, - null => -1d, + null => new TimingNotice { Seconds = s.Seconds }, // there is no pb }; - public double PBLapDiff(Lap lap) => PB() switch { - Lap pb => lap.TotalTime - pb.TotalTime, - null => -1d, + public TimingNotice PBLapDiff(Lap lap) => PB() switch { + Lap pb => (new TimingNotice { Seconds = lap.TotalTime }).Compare(pb.TotalTime), + null => new TimingNotice { Seconds = lap.TotalTime }, }; } diff --git a/Assets/Src/UI/Timing.uxml b/Assets/Src/UI/Timing.uxml index 23840d4..0c21146 100644 --- a/Assets/Src/UI/Timing.uxml +++ b/Assets/Src/UI/Timing.uxml @@ -7,7 +7,9 @@ - + + + diff --git a/Assets/Src/UI/TimingUI.cs b/Assets/Src/UI/TimingUI.cs index 925f5c2..f5be9b9 100644 --- a/Assets/Src/UI/TimingUI.cs +++ b/Assets/Src/UI/TimingUI.cs @@ -19,8 +19,8 @@ public class TimingUI : MonoBehaviour { VisualElement notice; VisualElement noticeSectors; + VisualElement noticeDiff; Label noticeTime; - Label noticeDiff; void OnEnable() { Init(); @@ -37,7 +37,7 @@ public class TimingUI : MonoBehaviour { timerSectors = uiDocument.rootVisualElement.Q("timerSectors"); noticeTime = uiDocument.rootVisualElement.Q