From e606c7332931f3af3a9d58d85c882c6f62603ffe Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 17 May 2019 11:43:36 +0900 Subject: [PATCH] Convert BadgeDisplay to use bindable --- osu.Game/Overlays/Changelog/BadgeDisplay.cs | 66 +++++-------------- .../Overlays/Changelog/ChangelogHeader.cs | 11 +--- osu.Game/Overlays/Changelog/StreamBadge.cs | 6 +- osu.Game/Overlays/ChangelogOverlay.cs | 15 ++++- 4 files changed, 36 insertions(+), 62 deletions(-) diff --git a/osu.Game/Overlays/Changelog/BadgeDisplay.cs b/osu.Game/Overlays/Changelog/BadgeDisplay.cs index 8c58decf49..e04624cfcb 100644 --- a/osu.Game/Overlays/Changelog/BadgeDisplay.cs +++ b/osu.Game/Overlays/Changelog/BadgeDisplay.cs @@ -6,8 +6,8 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; using osu.Framework.Input.Events; using osu.Game.Online.API.Requests.Responses; -using System; using System.Collections.Generic; +using osu.Framework.Bindables; using osuTK.Graphics; namespace osu.Game.Overlays.Changelog @@ -17,10 +17,9 @@ namespace osu.Game.Overlays.Changelog private const float vertical_padding = 20; private const float horizontal_padding = 85; - public event Action Selected; + public readonly Bindable Current = new Bindable(); private readonly FillFlowContainer badgesContainer; - private long selectedStreamId = -1; public BadgeDisplay() { @@ -40,23 +39,34 @@ namespace osu.Game.Overlays.Changelog Padding = new MarginPadding { Vertical = vertical_padding, Horizontal = horizontal_padding }, }, }; + + Current.ValueChanged += e => + { + foreach (StreamBadge streamBadge in badgesContainer) + { + if (!IsHovered || e.NewValue.Id == streamBadge.Stream.Id) + streamBadge.Activate(); + else + streamBadge.Deactivate(); + } + }; } public void Populate(List streams) { - SelectNone(); + Current.Value = null; foreach (APIUpdateStream updateStream in streams) { var streamBadge = new StreamBadge(updateStream); - streamBadge.Selected += onBadgeSelected; + streamBadge.Selected += () => Current.Value = updateStream; badgesContainer.Add(streamBadge); } } public void SelectNone() { - selectedStreamId = -1; + Current.Value = null; if (badgesContainer != null) { @@ -70,45 +80,10 @@ namespace osu.Game.Overlays.Changelog } } - public void SelectUpdateStream(string updateStream) - { - foreach (StreamBadge streamBadge in badgesContainer) - { - if (streamBadge.Stream.Name == updateStream) - { - selectedStreamId = streamBadge.Stream.Id; - streamBadge.Activate(); - } - else - streamBadge.Deactivate(); - } - } - - private void onBadgeSelected(StreamBadge source, EventArgs args) - { - selectedStreamId = source.Stream.Id; - OnSelected(source); - } - - protected virtual void OnSelected(StreamBadge source) - { - Selected?.Invoke(source.Stream.LatestBuild); - } - protected override bool OnHover(HoverEvent e) { foreach (StreamBadge streamBadge in badgesContainer.Children) - { - if (selectedStreamId >= 0) - { - if (selectedStreamId != streamBadge.Stream.Id) - streamBadge.Deactivate(); - else - streamBadge.EnableDim(); - } - else - streamBadge.Deactivate(); - } + streamBadge.EnableDim(); return base.OnHover(e); } @@ -116,12 +91,7 @@ namespace osu.Game.Overlays.Changelog protected override void OnHoverLost(HoverLostEvent e) { foreach (StreamBadge streamBadge in badgesContainer.Children) - { - if (selectedStreamId < 0) - streamBadge.Activate(); - else if (streamBadge.Stream.Id == selectedStreamId) - streamBadge.DisableDim(); - } + streamBadge.DisableDim(); base.OnHoverLost(e); } diff --git a/osu.Game/Overlays/Changelog/ChangelogHeader.cs b/osu.Game/Overlays/Changelog/ChangelogHeader.cs index 7502ef1773..94046d5762 100644 --- a/osu.Game/Overlays/Changelog/ChangelogHeader.cs +++ b/osu.Game/Overlays/Changelog/ChangelogHeader.cs @@ -1,4 +1,4 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; @@ -81,18 +81,13 @@ namespace osu.Game.Overlays.Changelog Anchor = Anchor.Centre, Origin = Anchor.Centre, }, - - // this box has 2 functions: - // - ensures the circle doesn't disappear on the X and Y edges - // - gets rid of the white "contamination" on the circle (due to smoothing) - // (https://i.imgur.com/SMuvWBZ.png) new Box { RelativeSizeAxes = Axes.Both, + Colour = colours.Violet, Alpha = 0, AlwaysPresent = true, - Colour = colours.Violet, - } + }, } }, new FillFlowContainer diff --git a/osu.Game/Overlays/Changelog/StreamBadge.cs b/osu.Game/Overlays/Changelog/StreamBadge.cs index 721689c75d..2be6aba926 100644 --- a/osu.Game/Overlays/Changelog/StreamBadge.cs +++ b/osu.Game/Overlays/Changelog/StreamBadge.cs @@ -22,9 +22,7 @@ namespace osu.Game.Overlays.Changelog private const float badge_width = 100; private const float transition_duration = 100; - public delegate void SelectedHandler(StreamBadge source, EventArgs args); - - public event SelectedHandler Selected; + public event Action Selected; private bool isActivated; @@ -90,7 +88,7 @@ namespace osu.Game.Overlays.Changelog this.FadeIn(transition_duration); lineBadge.Uncollapse(); if (!withoutFiringUpdates) - Selected?.Invoke(this, EventArgs.Empty); + Selected?.Invoke(); } public void Deactivate() diff --git a/osu.Game/Overlays/ChangelogOverlay.cs b/osu.Game/Overlays/ChangelogOverlay.cs index ef049035c3..71393141bd 100644 --- a/osu.Game/Overlays/ChangelogOverlay.cs +++ b/osu.Game/Overlays/ChangelogOverlay.cs @@ -70,7 +70,8 @@ namespace osu.Game.Overlays header.ListingSelected += ShowListing; - badges.Selected += ShowBuild; + // todo: better + badges.Current.ValueChanged += e => ShowBuild(e.NewValue.LatestBuild); listing.BuildSelected += ShowBuild; content.BuildSelected += ShowBuild; @@ -129,6 +130,10 @@ namespace osu.Game.Overlays var req = new GetChangelogRequest(); req.Success += res => { + // remap streams to builds to ensure model equality + res.Builds.ForEach(b => b.UpdateStream = res.Streams.Find(s => s.Id == b.UpdateStream.Id)); + res.Streams.ForEach(s => s.LatestBuild.UpdateStream = res.Streams.Find(s2 => s2.Id == s.LatestBuild.UpdateStream.Id)); + listing.ShowListing(res.Builds); badges.Populate(res.Streams); }; @@ -157,8 +162,14 @@ namespace osu.Game.Overlays /// are specified, the header will instantly display them. public void ShowBuild(APIChangelogBuild build) { + if (build == null) + { + ShowListing(); + return; + } + header.ShowBuild(build.UpdateStream.DisplayName, build.DisplayVersion); - badges.SelectUpdateStream(build.UpdateStream.Name); + badges.Current.Value = build.UpdateStream; listing.Hide();