1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 05:07:50 +08:00
osu-lazer/osu.Game/Beatmaps/Drawables/BeatmapGroup.cs

139 lines
4.8 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework;
using osu.Framework.Graphics;
namespace osu.Game.Beatmaps.Drawables
{
public class BeatmapGroup : IStateful<BeatmapGroupState>
{
2017-09-04 08:10:04 +08:00
public event Action<BeatmapGroupState> StateChanged;
public BeatmapPanel SelectedPanel;
/// <summary>
/// Fires when one of our difficulties was selected. Will fire on first expand.
/// </summary>
public Action<BeatmapGroup, BeatmapPanel> SelectionChanged;
/// <summary>
/// Fires when one of our difficulties is clicked when already selected. Should start playing the map.
/// </summary>
public Action<BeatmapInfo> StartRequested;
public Action<BeatmapSetInfo> DeleteRequested;
2017-08-30 19:41:41 +08:00
public Action<BeatmapSetInfo> RestoreHiddenRequested;
2017-09-04 06:59:32 +08:00
public Action<BeatmapInfo> HideDifficultyRequested;
2017-10-06 23:59:14 +08:00
public Action<BeatmapInfo> EditRequested;
public BeatmapSetHeader Header;
2016-10-27 12:41:30 +08:00
2017-01-18 08:18:15 +08:00
public List<BeatmapPanel> BeatmapPanels;
public BeatmapSetInfo BeatmapSet;
private BeatmapGroupState state;
public BeatmapGroupState State
{
get { return state; }
set
{
2017-09-04 08:10:04 +08:00
state = value;
switch (value)
{
case BeatmapGroupState.Expanded:
Header.State = PanelSelectedState.Selected;
foreach (BeatmapPanel panel in BeatmapPanels)
panel.State = panel == SelectedPanel ? PanelSelectedState.Selected : PanelSelectedState.NotSelected;
break;
case BeatmapGroupState.Collapsed:
Header.State = PanelSelectedState.NotSelected;
foreach (BeatmapPanel panel in BeatmapPanels)
panel.State = PanelSelectedState.Hidden;
break;
case BeatmapGroupState.Hidden:
Header.State = PanelSelectedState.Hidden;
foreach (BeatmapPanel panel in BeatmapPanels)
panel.State = PanelSelectedState.Hidden;
break;
}
2017-09-04 08:10:04 +08:00
StateChanged?.Invoke(state);
}
}
public BeatmapGroup(BeatmapSetInfo beatmapSet, BeatmapManager manager)
{
if (beatmapSet == null)
throw new ArgumentNullException(nameof(beatmapSet));
if (manager == null)
throw new ArgumentNullException(nameof(manager));
BeatmapSet = beatmapSet;
WorkingBeatmap beatmap = manager.GetWorkingBeatmap(BeatmapSet.Beatmaps.FirstOrDefault());
Header = new BeatmapSetHeader(beatmap)
{
GainedSelection = headerGainedSelection,
2017-08-30 19:41:41 +08:00
DeleteRequested = b => DeleteRequested(b),
RestoreHiddenRequested = b => RestoreHiddenRequested(b),
RelativeSizeAxes = Axes.X,
};
BeatmapPanels = BeatmapSet.Beatmaps.Where(b => !b.Hidden).OrderBy(b => b.StarDifficulty).Select(b => new BeatmapPanel(b)
2016-11-01 22:24:14 +08:00
{
Alpha = 0,
2016-11-01 22:24:14 +08:00
GainedSelection = panelGainedSelection,
2017-09-04 06:59:32 +08:00
HideRequested = p => HideDifficultyRequested?.Invoke(p),
2017-10-06 23:59:14 +08:00
StartRequested = p => StartRequested?.Invoke(p.Beatmap),
EditRequested = p => EditRequested?.Invoke(p.Beatmap),
2016-11-01 22:24:14 +08:00
RelativeSizeAxes = Axes.X,
2017-01-18 08:18:15 +08:00
}).ToList();
2017-01-30 12:35:40 +08:00
Header.AddDifficultyIcons(BeatmapPanels);
2016-11-01 22:24:14 +08:00
}
private void headerGainedSelection(BeatmapSetHeader panel)
{
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;
}
private void panelGainedSelection(BeatmapPanel panel)
{
try
{
if (SelectedPanel == panel) return;
if (SelectedPanel != null)
SelectedPanel.State = PanelSelectedState.NotSelected;
SelectedPanel = panel;
}
finally
{
State = BeatmapGroupState.Expanded;
SelectionChanged?.Invoke(this, SelectedPanel);
}
}
}
public enum BeatmapGroupState
{
Collapsed,
Expanded,
Hidden,
}
}