1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 09:05:59 +08:00
osu-lazer/osu.Game/Beatmaps/Drawable/BeatmapGroup.cs

154 lines
5.2 KiB
C#
Raw Normal View History

2016-10-14 03:10:00 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2016-10-28 18:55:48 +08:00
using System.Collections.Generic;
2016-10-27 10:55:55 +08:00
using System.Linq;
2016-11-01 22:24:14 +08:00
using System.Threading;
using System.Threading.Tasks;
2016-10-27 10:55:55 +08:00
using osu.Framework;
2016-11-01 22:24:14 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
2016-10-14 03:10:00 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
2016-10-27 10:55:55 +08:00
using osu.Game.Database;
using OpenTK;
namespace osu.Game.Beatmaps.Drawable
2016-10-14 03:10:00 +08:00
{
class BeatmapGroup : Container, IStateful<BeatmapGroupState>
2016-10-14 03:10:00 +08:00
{
public BeatmapPanel SelectedPanel;
2016-10-14 04:55:15 +08:00
2016-10-27 12:41:30 +08:00
/// <summary>
/// Fires when one of our difficulties was selected. Will fire on first expand.
/// </summary>
public Action<BeatmapGroup, BeatmapInfo> SelectionChanged;
2016-10-27 12:41:30 +08:00
private BeatmapSetInfo beatmapSet;
2016-10-27 10:55:55 +08:00
private BeatmapSetHeader header;
2016-10-14 04:25:41 +08:00
private FlowContainer difficulties;
2016-10-27 12:41:30 +08:00
private BeatmapGroupState state;
2016-10-27 12:41:30 +08:00
public List<BeatmapPanel> BeatmapPanels;
2016-10-28 18:55:48 +08:00
public BeatmapGroupState State
2016-10-14 03:10:00 +08:00
{
get { return state; }
2016-10-14 03:10:00 +08:00
set
{
state = value;
switch (state)
{
case BeatmapGroupState.Expanded:
FadeTo(1, 250);
2016-11-01 22:24:14 +08:00
//if (!difficulties.Children.All(d => IsLoaded))
// Task.WhenAll(difficulties.Children.Select(d => d.Preload(Game))).ContinueWith(t => difficulties.Show());
//else
difficulties.Show();
header.State = PanelSelectedState.Selected;
break;
case BeatmapGroupState.Collapsed:
2016-11-05 19:00:14 +08:00
FadeTo(0.8f, 250);
header.State = PanelSelectedState.NotSelected;
difficulties.Hide();
break;
}
2016-10-14 03:10:00 +08:00
}
}
2016-11-05 19:00:14 +08:00
public BeatmapGroup(BeatmapSetInfo beatmapSet, WorkingBeatmap working)
2016-10-14 03:10:00 +08:00
{
this.beatmapSet = beatmapSet;
Alpha = 0;
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
2016-10-14 05:27:08 +08:00
Children = new[]
2016-10-14 03:10:00 +08:00
{
new FlowContainer
2016-10-14 04:55:15 +08:00
{
2016-10-14 08:48:36 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2016-10-14 04:55:15 +08:00
Direction = FlowDirection.VerticalOnly,
2016-10-27 10:55:55 +08:00
Children = new Framework.Graphics.Drawable[]
2016-10-21 01:55:04 +08:00
{
2016-11-05 19:00:14 +08:00
header = new BeatmapSetHeader(beatmapSet, working)
2016-10-21 01:55:04 +08:00
{
GainedSelection = headerGainedSelection,
2016-10-21 01:55:04 +08:00
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
2016-10-26 22:52:04 +08:00
},
difficulties = new FlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding { Top = 5 },
Padding = new MarginPadding { Left = 75 },
Spacing = new Vector2(0, 5),
Direction = FlowDirection.VerticalOnly,
2016-10-21 01:55:04 +08:00
}
}
2016-10-14 04:55:15 +08:00
}
2016-10-14 03:10:00 +08:00
};
}
2016-11-01 22:24:14 +08:00
protected override void Load(BaseGame game)
{
base.Load(game);
2016-11-01 22:24:14 +08:00
BeatmapPanels = beatmapSet.Beatmaps.Select(b => new BeatmapPanel(b)
{
GainedSelection = panelGainedSelection,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
RelativeSizeAxes = Axes.X,
}).ToList();
//for the time being, let's completely load the difficulty panels in the background.
//this likely won't scale so well, but allows us to completely async the loading flow.
Task.WhenAll(BeatmapPanels.Select(panel => panel.Preload(game, p => difficulties.Add(panel)))).Wait();
}
private void headerGainedSelection(BeatmapSetHeader panel)
2016-10-26 22:52:04 +08:00
{
State = BeatmapGroupState.Expanded;
2016-10-28 22:40:12 +08:00
//we want to make sure one of our children is selected in the case none have been selected yet.
if (SelectedPanel == null)
BeatmapPanels.First().State = PanelSelectedState.Selected;
else
SelectionChanged?.Invoke(this, SelectedPanel.Beatmap);
2016-10-26 22:52:04 +08:00
}
private void panelGainedSelection(BeatmapPanel panel)
2016-10-14 03:10:00 +08:00
{
try
{
if (SelectedPanel == panel) return;
if (SelectedPanel != null)
SelectedPanel.State = PanelSelectedState.NotSelected;
SelectedPanel = panel;
}
finally
{
State = BeatmapGroupState.Expanded;
SelectionChanged?.Invoke(this, panel.Beatmap);
}
2016-10-14 03:10:00 +08:00
}
}
public enum BeatmapGroupState
{
Collapsed,
Expanded,
2016-10-14 03:10:00 +08:00
}
}