This commit is contained in:
ntr committed 2023-08-04 16:00:10 +10:00
1 parent d97efd0e88
commit c61fd84f90
4 files changed
+57 -50

No files matched your search

+41 -19
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.IO; using System.IO;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements;
public enum NoticeCategory { public enum NoticeCategory {
Initial, Initial,
@@ -12,27 +13,42 @@ public enum NoticeCategory {
Fastest, Fastest,
} }
// public record TimingNotice( public class TimingNotice {
// double Seconds, public double Seconds;
// NoticeCategory Category, NoticeCategory Category = NoticeCategory.Initial;
// ) {
// 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}"),
// };
// string colourClass = Category Switch { public TimingNotice Compare(double comparison) {
// DiffType.Slower => "yellow", double s = Math.Abs(comparison - Seconds);
// DiffType.Faster => "green", NoticeCategory cat = comparison switch {
// DiffType.Fastest => "purple", double c when Seconds < c => NoticeCategory.Faster,
// }; _ => NoticeCategory.Slower,
};
// l.AddToClassList(colourClass); return new TimingNotice {
// return l; 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 { public enum DiffColour {
Green, Green,
@@ -127,3 +143,9 @@ static class LapStorageService {
.MinBy(l => l.TotalTime); .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 {}
}
+7 -7
View File
@@ -129,17 +129,17 @@ public class Timing: MonoBehaviour {
return LapStorageService.PB(mapName); 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 { Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch {
SectorTime fastestSector => s.Seconds - fastestSector.Seconds, SectorTime fastestSector => (new TimingNotice { Seconds = s.Seconds }).Compare(fastestSector.Seconds),
null => -1d, 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 { public TimingNotice PBLapDiff(Lap lap) => PB() switch {
Lap pb => lap.TotalTime - pb.TotalTime, Lap pb => (new TimingNotice { Seconds = lap.TotalTime }).Compare(pb.TotalTime),
null => -1d, null => new TimingNotice { Seconds = lap.TotalTime },
}; };
} }
+3 -1
View File
@@ -7,7 +7,9 @@
<ui:VisualElement name="notice" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0);"> <ui:VisualElement name="notice" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0);">
<ui:VisualElement name="noticeLap" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); flex-direction: row; justify-content: space-between; align-items: auto; width: auto; align-self: stretch; flex-shrink: 0;"> <ui:VisualElement name="noticeLap" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); flex-direction: row; justify-content: space-between; align-items: auto; width: auto; align-self: stretch; flex-shrink: 0;">
<ui:Label tabindex="-1" text="1:24.248" display-tooltip-when-elided="true" name="noticeTime" style="align-self: flex-end; align-items: auto;" /> <ui:Label tabindex="-1" text="1:24.248" display-tooltip-when-elided="true" name="noticeTime" style="align-self: flex-end; align-items: auto;" />
<ui:Label tabindex="-1" text="-0.999" display-tooltip-when-elided="true" name="noticeDiff" /> <ui:VisualElement name="noticeDiff" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); -unity-text-align: upper-right;">
<ui:Label tabindex="-1" text="-0.999" display-tooltip-when-elided="true" />
</ui:VisualElement>
</ui:VisualElement> </ui:VisualElement>
<ui:VisualElement name="noticeSectors" class="sectors" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-direction: row; -unity-text-align: upper-left;"> <ui:VisualElement name="noticeSectors" class="sectors" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-direction: row; -unity-text-align: upper-left;">
<ui:Label tabindex="-1" text="+2.1" display-tooltip-when-elided="true" class="yellow" /> <ui:Label tabindex="-1" text="+2.1" display-tooltip-when-elided="true" class="yellow" />
+6 -23
View File
@@ -19,8 +19,8 @@ public class TimingUI : MonoBehaviour {
VisualElement notice; VisualElement notice;
VisualElement noticeSectors; VisualElement noticeSectors;
VisualElement noticeDiff;
Label noticeTime; Label noticeTime;
Label noticeDiff;
void OnEnable() { void OnEnable() {
Init(); Init();
@@ -37,7 +37,7 @@ public class TimingUI : MonoBehaviour {
timerSectors = uiDocument.rootVisualElement.Q<VisualElement>("timerSectors"); timerSectors = uiDocument.rootVisualElement.Q<VisualElement>("timerSectors");
noticeTime = uiDocument.rootVisualElement.Q<Label>("noticeTime"); noticeTime = uiDocument.rootVisualElement.Q<Label>("noticeTime");
noticeDiff = uiDocument.rootVisualElement.Q<Label>("noticeDiff"); noticeDiff = uiDocument.rootVisualElement.Q<VisualElement>("noticeDiff");
timerTime = uiDocument.rootVisualElement.Q<Label>("timerTime"); timerTime = uiDocument.rootVisualElement.Q<Label>("timerTime");
timerPb = uiDocument.rootVisualElement.Q<Label>("timerPb"); timerPb = uiDocument.rootVisualElement.Q<Label>("timerPb");
@@ -73,7 +73,7 @@ public class TimingUI : MonoBehaviour {
foreach (Sector sector in timing.sectors) { foreach (Sector sector in timing.sectors) {
Label l = timing.sectorTimes Label l = timing.sectorTimes
.Where(s => s.Sector == sector.sector) .Where(s => s.Sector == sector.sector)
.Select(s => SectorLabel(s)) .Select(s => timing.PBSectorDiff(s).ToLabel())
.DefaultIfEmpty(new Label("-")) .DefaultIfEmpty(new Label("-"))
.Single(); .Single();
@@ -90,11 +90,12 @@ public class TimingUI : MonoBehaviour {
// should show the diff to the PB // should show the diff to the PB
// or - if there is only 1 lap // or - if there is only 1 lap
noticeDiff.text = lap.ToString(); noticeDiff.Clear();
noticeDiff.Add(timing.PBLapDiff(lap).ToLabel());
noticeSectors.Clear(); noticeSectors.Clear();
foreach (SectorTime sector in lap.Sectors) { foreach (SectorTime sector in lap.Sectors) {
Label l = SectorLabel(sector); Label l = timing.PBSectorDiff(sector).ToLabel();
noticeSectors.Add(l); noticeSectors.Add(l);
} }
} }
@@ -103,22 +104,4 @@ public class TimingUI : MonoBehaviour {
timer.RemoveFromClassList("hidden"); timer.RemoveFromClassList("hidden");
notice.AddToClassList("hidden"); notice.AddToClassList("hidden");
} }
Label SectorLabel(SectorTime s) {
double diff = timing.PBSectorDiff(s);
string sign = diff switch {
< 0 => "",
_ => "+",
};
DiffColour colour = diff switch {
< 0 => DiffColour.Green,
_ => DiffColour.Yellow,
};
Label l = new Label($"{sign}{diff:0.000}");
string colourClass = colour.ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l;
}
} }