1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 17:35:03 +08:00
osu-lazer/osu.Game/Screens/Multi/Match/Components/ReadyButton.cs

101 lines
2.7 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.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Online.Multiplayer;
2018-12-14 13:20:03 +08:00
using osuTK;
namespace osu.Game.Screens.Multi.Match.Components
{
2018-12-14 13:20:03 +08:00
public class ReadyButton : HeaderButton
{
2019-02-11 18:11:34 +08:00
public readonly Bindable<BeatmapInfo> Beatmap = new Bindable<BeatmapInfo>();
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()
{
2018-12-14 13:20:03 +08:00
RelativeSizeAxes = Axes.Y;
Size = new Vector2(200, 1);
Text = "Start";
}
[BackgroundDependencyLoader]
private void load()
{
beatmaps.ItemAdded += beatmapAdded;
beatmaps.ItemRemoved += beatmapRemoved;
Beatmap.BindValueChanged(b => updateBeatmap(b.NewValue), true);
}
private void updateBeatmap(BeatmapInfo beatmap)
{
hasBeatmap = false;
if (beatmap?.OnlineBeatmapID == null)
return;
hasBeatmap = beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == beatmap.OnlineBeatmapID) != null;
}
private void beatmapAdded(BeatmapSetInfo model)
{
if (model.Beatmaps.Any(b => b.OnlineBeatmapID == Beatmap.Value.OnlineBeatmapID))
Schedule(() => hasBeatmap = true);
}
private void beatmapRemoved(BeatmapSetInfo model)
{
if (Beatmap.Value == null)
return;
if (model.OnlineBeatmapSetID == Beatmap.Value.BeatmapSet.OnlineBeatmapSetID)
Schedule(() => hasBeatmap = false);
}
protected override void Update()
{
base.Update();
updateEnabledState();
}
private void updateEnabledState()
{
if (gameBeatmap.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;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2018-12-18 17:09:54 +08:00
if (beatmaps != null)
beatmaps.ItemAdded -= beatmapAdded;
}
}
}