1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 10:57:51 +08:00
osu-lazer/osu.Game/Screens/Multi/Match/Components/ReadyButton.cs

111 lines
3.5 KiB
C#
Raw Normal View History

// 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 System;
using System.Linq;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
2020-02-20 17:39:10 +08:00
using osu.Game.Graphics;
2020-02-14 19:14:25 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Multiplayer;
namespace osu.Game.Screens.Multi.Match.Components
{
2020-02-20 17:39:10 +08:00
public class ReadyButton : TriangleButton
{
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
2019-02-05 18:00:01 +08:00
[Resolved(typeof(Room), nameof(Room.EndDate))]
private Bindable<DateTimeOffset> endDate { get; set; }
[Resolved]
2019-02-01 14:42:15 +08:00
private IBindable<WorkingBeatmap> gameBeatmap { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
private bool hasBeatmap;
2019-02-05 18:00:01 +08:00
public ReadyButton()
{
Text = "Start";
}
2020-05-19 15:44:22 +08:00
private IBindable<WeakReference<BeatmapSetInfo>> managerAdded;
private IBindable<WeakReference<BeatmapSetInfo>> managerRemoved;
[BackgroundDependencyLoader]
2020-02-20 17:39:10 +08:00
private void load(OsuColour colours)
{
2020-05-19 15:44:22 +08:00
managerAdded = beatmaps.ItemAdded.GetBoundCopy();
managerAdded.BindValueChanged(beatmapAdded);
managerRemoved = beatmaps.ItemRemoved.GetBoundCopy();
managerRemoved.BindValueChanged(beatmapRemoved);
SelectedItem.BindValueChanged(item => updateSelectedItem(item.NewValue), true);
2020-02-20 17:39:10 +08:00
BackgroundColour = colours.Green;
Triangles.ColourDark = colours.Green;
Triangles.ColourLight = colours.GreenLight;
}
private void updateSelectedItem(PlaylistItem item)
{
hasBeatmap = false;
int? beatmapId = SelectedItem.Value?.Beatmap.Value?.OnlineBeatmapID;
if (beatmapId == null)
return;
hasBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == beatmapId) != null;
}
2020-05-19 15:44:22 +08:00
private void beatmapAdded(ValueChangedEvent<WeakReference<BeatmapSetInfo>> weakSet)
{
2020-05-19 15:44:22 +08:00
if (weakSet.NewValue.TryGetTarget(out var set))
{
int? beatmapId = SelectedItem.Value?.Beatmap.Value?.OnlineBeatmapID;
if (beatmapId == null)
return;
2020-05-19 15:44:22 +08:00
if (set.Beatmaps.Any(b => b.OnlineBeatmapID == beatmapId))
Schedule(() => hasBeatmap = true);
}
}
2020-05-19 15:44:22 +08:00
private void beatmapRemoved(ValueChangedEvent<WeakReference<BeatmapSetInfo>> weakSet)
{
2020-05-19 15:44:22 +08:00
if (weakSet.NewValue.TryGetTarget(out var set))
{
int? beatmapId = SelectedItem.Value?.Beatmap.Value?.OnlineBeatmapID;
if (beatmapId == null)
return;
2020-05-19 15:44:22 +08:00
if (set.Beatmaps.Any(b => b.OnlineBeatmapID == beatmapId))
Schedule(() => hasBeatmap = false);
}
}
protected override void Update()
{
base.Update();
updateEnabledState();
}
private void updateEnabledState()
{
if (gameBeatmap.Value == null || SelectedItem.Value == null)
{
Enabled.Value = false;
return;
}
2019-02-21 17:56:34 +08:00
bool hasEnoughTime = DateTimeOffset.UtcNow.AddSeconds(30).AddMilliseconds(gameBeatmap.Value.Track.Length) < endDate.Value;
Enabled.Value = hasBeatmap && hasEnoughTime;
}
}
}