1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:57:52 +08:00

Convert BadgeDisplay to use bindable

This commit is contained in:
Dean Herbert 2019-05-17 11:43:36 +09:00
parent e94b9feebd
commit e606c73329
4 changed files with 36 additions and 62 deletions

View File

@ -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<APIChangelogBuild> Selected;
public readonly Bindable<APIUpdateStream> Current = new Bindable<APIUpdateStream>();
private readonly FillFlowContainer<StreamBadge> 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<APIUpdateStream> 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);
}

View File

@ -1,4 +1,4 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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

View File

@ -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()

View File

@ -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
/// <see cref="APIChangelogBuild.DisplayVersion"/> are specified, the header will instantly display them.</param>
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();