From 60d244d2d60ae4d1fcc1e68997be7874c1f34671 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 15 May 2019 18:08:19 +0900 Subject: [PATCH] Clean up events and states --- .../Visual/TestCaseChangelogOverlay.cs | 2 +- osu.Game/Overlays/Changelog/BadgeDisplay.cs | 8 ++-- .../Overlays/Changelog/ChangelogContent.cs | 15 ++----- .../Changelog/ChangelogContentGroup.cs | 17 +++----- osu.Game/Overlays/ChangelogOverlay.cs | 39 +++++++------------ 5 files changed, 27 insertions(+), 54 deletions(-) diff --git a/osu.Game.Tests/Visual/TestCaseChangelogOverlay.cs b/osu.Game.Tests/Visual/TestCaseChangelogOverlay.cs index 851cf17cfc..89b4fd2d18 100644 --- a/osu.Game.Tests/Visual/TestCaseChangelogOverlay.cs +++ b/osu.Game.Tests/Visual/TestCaseChangelogOverlay.cs @@ -24,7 +24,7 @@ namespace osu.Game.Tests.Visual AddStep(@"Show with Lazer 2018.712.0", () => { - changelog.FetchAndShowBuild(new APIChangelogBuild + changelog.ShowBuild(new APIChangelogBuild { Version = "2018.712.0", UpdateStream = new APIUpdateStream { Name = "lazer" }, diff --git a/osu.Game/Overlays/Changelog/BadgeDisplay.cs b/osu.Game/Overlays/Changelog/BadgeDisplay.cs index 441909132a..8c58decf49 100644 --- a/osu.Game/Overlays/Changelog/BadgeDisplay.cs +++ b/osu.Game/Overlays/Changelog/BadgeDisplay.cs @@ -17,9 +17,7 @@ namespace osu.Game.Overlays.Changelog private const float vertical_padding = 20; private const float horizontal_padding = 85; - public delegate void SelectionHandler(APIChangelogBuild releaseStream, EventArgs args); - - public event SelectionHandler Selected; + public event Action Selected; private readonly FillFlowContainer badgesContainer; private long selectedStreamId = -1; @@ -46,6 +44,8 @@ namespace osu.Game.Overlays.Changelog public void Populate(List streams) { + SelectNone(); + foreach (APIUpdateStream updateStream in streams) { var streamBadge = new StreamBadge(updateStream); @@ -92,7 +92,7 @@ namespace osu.Game.Overlays.Changelog protected virtual void OnSelected(StreamBadge source) { - Selected?.Invoke(source.Stream.LatestBuild, EventArgs.Empty); + Selected?.Invoke(source.Stream.LatestBuild); } protected override bool OnHover(HoverEvent e) diff --git a/osu.Game/Overlays/Changelog/ChangelogContent.cs b/osu.Game/Overlays/Changelog/ChangelogContent.cs index fdc9fac404..45fb98add0 100644 --- a/osu.Game/Overlays/Changelog/ChangelogContent.cs +++ b/osu.Game/Overlays/Changelog/ChangelogContent.cs @@ -15,9 +15,7 @@ namespace osu.Game.Overlays.Changelog { private ChangelogContentGroup changelogContentGroup; - public delegate void BuildSelectedEventHandler(APIChangelogBuild build, EventArgs args); - - public event BuildSelectedEventHandler BuildSelected; + public event Action BuildSelected; public ChangelogContent() { @@ -48,7 +46,7 @@ namespace osu.Game.Overlays.Changelog } changelogContentGroup = new ChangelogContentGroup(build, true); - changelogContentGroup.BuildSelected += OnBuildSelected; + changelogContentGroup.BuildSelected += b => BuildSelected?.Invoke(b); changelogContentGroup.GenerateText(build.ChangelogEntries); Add(changelogContentGroup); currentDate = build.CreatedAt.Date; @@ -64,7 +62,7 @@ namespace osu.Game.Overlays.Changelog }); changelogContentGroup = new ChangelogContentGroup(build, false); - changelogContentGroup.BuildSelected += OnBuildSelected; + changelogContentGroup.BuildSelected += b => BuildSelected?.Invoke(b); changelogContentGroup.GenerateText(build.ChangelogEntries); Add(changelogContentGroup); } @@ -77,12 +75,7 @@ namespace osu.Game.Overlays.Changelog changelogContentGroup.GenerateText(changelogBuild.ChangelogEntries); changelogContentGroup.UpdateChevronTooltips(changelogBuild.Versions.Previous?.DisplayVersion, changelogBuild.Versions.Next?.DisplayVersion); - changelogContentGroup.BuildSelected += OnBuildSelected; - } - - protected virtual void OnBuildSelected(APIChangelogBuild build, EventArgs args) - { - BuildSelected?.Invoke(build, EventArgs.Empty); + changelogContentGroup.BuildSelected += b => BuildSelected?.Invoke(b); } } } diff --git a/osu.Game/Overlays/Changelog/ChangelogContentGroup.cs b/osu.Game/Overlays/Changelog/ChangelogContentGroup.cs index 190720df04..1b03ade7e0 100644 --- a/osu.Game/Overlays/Changelog/ChangelogContentGroup.cs +++ b/osu.Game/Overlays/Changelog/ChangelogContentGroup.cs @@ -23,9 +23,7 @@ namespace osu.Game.Overlays.Changelog private readonly SortedDictionary> categories = new SortedDictionary>(); - public delegate void BuildSelectedEventHandler(APIChangelogBuild build, EventArgs args); - - public event BuildSelectedEventHandler BuildSelected; + public event Action BuildSelected; public readonly FillFlowContainer ChangelogEntries; @@ -53,7 +51,7 @@ namespace osu.Game.Overlays.Changelog Size = new Vector2(24), Action = () => { - OnBuildSelected(build.Versions.Previous); + BuildSelected?.Invoke(build.Versions.Previous); chevronPrevious.IsEnabled = false; }, }, @@ -88,7 +86,7 @@ namespace osu.Game.Overlays.Changelog Size = new Vector2(24), Action = () => { - OnBuildSelected(build.Versions.Next); + BuildSelected?.Invoke(build.Versions.Next); chevronNext.IsEnabled = false; }, }, @@ -141,7 +139,7 @@ namespace osu.Game.Overlays.Changelog Origin = Anchor.TopCentre, AutoSizeAxes = Axes.Both, Margin = new MarginPadding { Top = 20 }, - Action = () => OnBuildSelected(build), + Action = () => BuildSelected?.Invoke(build), Child = new FillFlowContainer { Direction = FillDirection.Horizontal, @@ -179,7 +177,7 @@ namespace osu.Game.Overlays.Changelog clickableBuildText.FadeTo(0.5f, 500); Scheduler.AddDelayed(() => { - clickableBuildText.Action = () => OnBuildSelected(build); + clickableBuildText.Action = () => BuildSelected?.Invoke(build); clickableBuildText.FadeIn(500); }, 2000); }; @@ -200,11 +198,6 @@ namespace osu.Game.Overlays.Changelog } } - protected virtual void OnBuildSelected(APIChangelogBuild build) - { - BuildSelected?.Invoke(build, EventArgs.Empty); - } - public void GenerateText(List changelogEntries) { // sort entries by category diff --git a/osu.Game/Overlays/ChangelogOverlay.cs b/osu.Game/Overlays/ChangelogOverlay.cs index a571879192..2bf5f0336c 100644 --- a/osu.Game/Overlays/ChangelogOverlay.cs +++ b/osu.Game/Overlays/ChangelogOverlay.cs @@ -13,7 +13,6 @@ using osu.Game.Input.Bindings; using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests.Responses; using osu.Game.Overlays.Changelog; -using System; using osuTK; using osuTK.Graphics; @@ -23,7 +22,7 @@ namespace osu.Game.Overlays { private ChangelogHeader header; - private BadgeDisplay badgeDisplay; + private BadgeDisplay badges; private ChangelogContent listing; private ChangelogContent content; @@ -32,7 +31,6 @@ namespace osu.Game.Overlays private SampleChannel sampleBack; - private bool isAtListing; private float savedScrollPosition; // receive input outside our bounds so we can trigger a close event on ourselves. @@ -66,7 +64,7 @@ namespace osu.Game.Overlays Children = new Drawable[] { header = new ChangelogHeader(), - badgeDisplay = new BadgeDisplay(), + badges = new BadgeDisplay(), listing = new ChangelogContent(), content = new ChangelogContent() }, @@ -75,9 +73,10 @@ namespace osu.Game.Overlays }; header.ListingSelected += ShowListing; - badgeDisplay.Selected += onBuildSelected; - listing.BuildSelected += onBuildSelected; - content.BuildSelected += onBuildSelected; + + badges.Selected += ShowBuild; + listing.BuildSelected += ShowBuild; + content.BuildSelected += ShowBuild; sampleBack = audio.Sample.Get(@"UI/generic-select-soft"); // @"UI/screen-back" feels non-fitting here } @@ -127,38 +126,29 @@ namespace osu.Game.Overlays return false; } - private void onBuildSelected(APIChangelogBuild build, EventArgs e) => FetchAndShowBuild(build); - private void fetchListing() { header.ShowListing(); - if (isAtListing) - return; - - isAtListing = true; var req = new GetChangelogRequest(); - badgeDisplay.SelectNone(); req.Success += res => { listing.ShowListing(res.Builds); - badgeDisplay.Populate(res.Streams); + badges.Populate(res.Streams); }; API.Queue(req); } + private bool isAtListing; + public void ShowListing() { + isAtListing = true; header.ShowListing(); - if (isAtListing) - return; - - isAtListing = true; content.Hide(); - listing.Show(); - badgeDisplay.SelectNone(); + badges.SelectNone(); listing.Show(); scroll.ScrollTo(savedScrollPosition); } @@ -169,9 +159,7 @@ namespace osu.Game.Overlays /// Must contain at least and /// . If and /// are specified, the header will instantly display them. - /// Whether to update badges. Should be set to false in case - /// the function is called by selecting a badge, to avoid an infinite loop. - public void FetchAndShowBuild(APIChangelogBuild build, bool updateBadges = true) + public void ShowBuild(APIChangelogBuild build) { var req = new GetChangelogBuildRequest(build.UpdateStream.Name, build.Version); @@ -180,8 +168,7 @@ namespace osu.Game.Overlays else req.Success += res => header.ShowBuild(res.UpdateStream.DisplayName, res.DisplayVersion); - if (updateBadges) - badgeDisplay.SelectUpdateStream(build.UpdateStream.Name); + badges.SelectUpdateStream(build.UpdateStream.Name); req.Success += apiChangelog => {