Use IStateful and improve collapse logic

This commit is contained in:
Drew DeVault 2016-10-26 12:49:16 -04:00
parent 55e5ec6fae
commit 9c27c33e18
2 changed files with 34 additions and 46 deletions

View File

@ -17,11 +17,18 @@ using OpenTK.Graphics;
using osu.Game.Beatmaps.IO; using osu.Game.Beatmaps.IO;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework;
namespace osu.Game.GameModes.Play namespace osu.Game.GameModes.Play
{ {
class BeatmapGroup : Container class BeatmapGroup : Container, IStateful<BeatmapGroup.GroupState>
{ {
public enum GroupState
{
Collapsed,
Expanded,
}
private const float collapsedAlpha = 0.5f; private const float collapsedAlpha = 0.5f;
private const float collapsedWidth = 0.8f; private const float collapsedWidth = 0.8f;
@ -35,21 +42,20 @@ namespace osu.Game.GameModes.Play
} }
} }
public Action<BeatmapSetInfo> SetSelected; public Action<BeatmapGroup, BeatmapInfo> BeatmapSelected;
public Action<BeatmapSetInfo, BeatmapInfo> BeatmapSelected;
public BeatmapSetInfo BeatmapSet; public BeatmapSetInfo BeatmapSet;
private BeatmapSetBox setBox; private BeatmapSetHeader setBox;
private FlowContainer topContainer;
private FlowContainer difficulties; private FlowContainer difficulties;
private bool collapsed; private bool collapsed;
public bool Collapsed public GroupState State
{ {
get { return collapsed; } get { return collapsed ? GroupState.Collapsed : GroupState.Expanded; }
set set
{ {
if (collapsed == value) bool val = value == GroupState.Collapsed;
if (collapsed == val)
return; return;
collapsed = value; collapsed = val;
ClearTransformations(); ClearTransformations();
const float uncollapsedAlpha = 1; const float uncollapsedAlpha = 1;
FadeTo(collapsed ? collapsedAlpha : uncollapsedAlpha, 250); FadeTo(collapsed ? collapsedAlpha : uncollapsedAlpha, 250);
@ -78,14 +84,14 @@ namespace osu.Game.GameModes.Play
float difficultyWidth = 1; float difficultyWidth = 1;
Children = new[] Children = new[]
{ {
topContainer = new FlowContainer new FlowContainer
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
Direction = FlowDirection.VerticalOnly, Direction = FlowDirection.VerticalOnly,
Children = new Drawable[] Children = new Drawable[]
{ {
setBox = new BeatmapSetBox(beatmapSet) setBox = new BeatmapSetHeader(beatmapSet)
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Width = collapsedWidth, Width = collapsedWidth,
@ -127,19 +133,19 @@ namespace osu.Game.GameModes.Play
{ {
foreach (BeatmapPanel panel in difficulties.Children) foreach (BeatmapPanel panel in difficulties.Children)
panel.Selected = panel.Beatmap == map; panel.Selected = panel.Beatmap == map;
BeatmapSelected?.Invoke(BeatmapSet, map); BeatmapSelected?.Invoke(this, map);
} }
protected override bool OnClick(InputState state) protected override bool OnClick(InputState state)
{ {
SetSelected?.Invoke(BeatmapSet); BeatmapSelected?.Invoke(this, selectedBeatmap);
return true; return true;
} }
} }
class BeatmapSetBox : Container class BeatmapSetHeader : Container
{ {
public BeatmapSetBox(BeatmapSetInfo beatmapSet) public BeatmapSetHeader(BeatmapSetInfo beatmapSet)
{ {
AutoSizeAxes = Axes.Y; AutoSizeAxes = Axes.Y;
Masking = true; Masking = true;

View File

@ -2,13 +2,10 @@
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE //Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System; using System;
using System.Collections.Generic;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.IO;
using osu.Game.GameModes.Backgrounds; using osu.Game.GameModes.Backgrounds;
using osu.Framework; using osu.Framework;
using osu.Game.Database; using osu.Game.Database;
@ -16,10 +13,8 @@ using osu.Framework.Graphics.Primitives;
using System.Linq; using System.Linq;
using OpenTK; using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using osu.Framework.Graphics.Textures;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Diagnostics;
namespace osu.Game.GameModes.Play namespace osu.Game.GameModes.Play
{ {
@ -27,7 +22,7 @@ namespace osu.Game.GameModes.Play
{ {
private Bindable<PlayMode> playMode; private Bindable<PlayMode> playMode;
private BeatmapDatabase beatmaps; private BeatmapDatabase beatmaps;
private BeatmapSetInfo selectedBeatmapSet; private BeatmapGroup selectedBeatmapGroup;
private BeatmapInfo selectedBeatmap; private BeatmapInfo selectedBeatmap;
// TODO: use currently selected track as bg // TODO: use currently selected track as bg
protected override BackgroundMode CreateBackground() => new BackgroundModeCustom(@"Backgrounds/bg4"); protected override BackgroundMode CreateBackground() => new BackgroundModeCustom(@"Backgrounds/bg4");
@ -141,29 +136,20 @@ namespace osu.Game.GameModes.Play
{ {
} }
private void selectBeatmapSet(BeatmapSetInfo beatmapSet) private void selectBeatmapSet(BeatmapGroup group)
{ {
if (selectedBeatmapSet == beatmapSet) if (selectedBeatmapGroup == group)
return; return;
selectedBeatmapSet = beatmapSet; selectedBeatmapGroup.State = BeatmapGroup.GroupState.Collapsed;
foreach (var child in setList.Children) selectedBeatmapGroup = group;
{ selectedBeatmapGroup.State = BeatmapGroup.GroupState.Expanded;
var childGroup = child as BeatmapGroup;
if (childGroup.BeatmapSet == beatmapSet)
{
childGroup.Collapsed = false;
selectedBeatmap = childGroup.SelectedBeatmap;
}
else
childGroup.Collapsed = true;
}
} }
private void selectBeatmap(BeatmapSetInfo set, BeatmapInfo beatmap) private void selectBeatmap(BeatmapGroup group, BeatmapInfo beatmap)
{ {
if (selectedBeatmap == beatmap) if (selectedBeatmap == beatmap)
return; return;
selectBeatmapSet(set); selectBeatmapSet(group);
selectedBeatmap = beatmap; selectedBeatmap = beatmap;
} }
@ -173,19 +159,15 @@ namespace osu.Game.GameModes.Play
beatmapSet.Beatmaps.ForEach(b => beatmaps.GetChildren(b)); beatmapSet.Beatmaps.ForEach(b => beatmaps.GetChildren(b));
beatmapSet.Beatmaps = beatmapSet.Beatmaps.OrderBy(b => b.BaseDifficulty.OverallDifficulty) beatmapSet.Beatmaps = beatmapSet.Beatmaps.OrderBy(b => b.BaseDifficulty.OverallDifficulty)
.ToList(); .ToList();
Scheduler.Add(() => Schedule(() =>
{ {
var group = new BeatmapGroup(beatmapSet) var group = new BeatmapGroup(beatmapSet) { BeatmapSelected = selectBeatmap };
{
SetSelected = selectBeatmapSet,
BeatmapSelected = selectBeatmap,
};
setList.Add(group); setList.Add(group);
if (setList.Children.Count() == 1) if (setList.Children.Count() == 1)
{ {
selectedBeatmapSet = group.BeatmapSet; selectedBeatmapGroup = group;
selectedBeatmap = group.SelectedBeatmap; selectedBeatmap = group.SelectedBeatmap;
group.Collapsed = false; group.State = BeatmapGroup.GroupState.Expanded;
} }
}); });
} }