osu/osu.Game/Overlays/BeatmapSet/BeatmapPicker.cs

340 lines
11 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.
2018-04-13 09:19:50 +00:00
using System;
using System.Linq;
using osu.Framework;
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-04-13 09:19:50 +00:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays.BeatmapSet
{
public class BeatmapPicker : Container
{
private const float tile_icon_padding = 7;
private const float tile_spacing = 2;
private readonly OsuSpriteText version, starRating;
private readonly Statistic plays, favourites;
public readonly DifficultiesContainer Difficulties;
2018-04-13 09:19:50 +00:00
public readonly Bindable<BeatmapInfo> Beatmap = new Bindable<BeatmapInfo>();
private BeatmapSetInfo beatmapSet;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
public BeatmapSetInfo BeatmapSet
{
get => beatmapSet;
2018-04-13 09:19:50 +00:00
set
{
if (value == beatmapSet) return;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
beatmapSet = value;
updateDisplay();
}
}
2018-04-13 09:19:50 +00:00
public BeatmapPicker()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Children = new Drawable[]
{
new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
Difficulties = new DifficultiesContainer
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding { Left = -(tile_icon_padding + tile_spacing / 2), Bottom = 10 },
2018-04-13 09:19:50 +00:00
OnLostHover = () =>
{
showBeatmap(Beatmap.Value);
starRating.FadeOut(100);
},
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(5f),
Children = new[]
{
version = new OsuSpriteText
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Font = OsuFont.GetFont(size: 17, weight: FontWeight.Bold)
2018-04-13 09:19:50 +00:00
},
starRating = new OsuSpriteText
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Font = OsuFont.GetFont(size: 11, weight: FontWeight.Bold),
2018-04-13 09:19:50 +00:00
Text = "Star Difficulty",
Alpha = 0,
Margin = new MarginPadding { Bottom = 1 },
},
},
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10f),
Margin = new MarginPadding { Top = 5 },
Children = new[]
{
2019-04-02 10:55:24 +00:00
plays = new Statistic(FontAwesome.Solid.PlayCircle),
favourites = new Statistic(FontAwesome.Solid.Heart),
2018-04-13 09:19:50 +00:00
},
},
},
},
};
Beatmap.ValueChanged += b =>
{
showBeatmap(b.NewValue);
2018-04-13 09:19:50 +00:00
updateDifficultyButtons();
};
}
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
starRating.Colour = colours.Yellow;
updateDisplay();
2018-04-13 09:19:50 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
ruleset.ValueChanged += r => updateDisplay();
2018-04-13 09:19:50 +00:00
// done here so everything can bind in intialization and get the first trigger
Beatmap.TriggerChange();
}
2019-10-31 08:13:00 +00:00
private void updateDisplay()
{
Difficulties.Clear();
if (BeatmapSet != null)
{
Difficulties.ChildrenEnumerable = BeatmapSet.Beatmaps.Where(b => b.Ruleset.Equals(ruleset.Value)).OrderBy(b => b.StarDifficulty).Select(b => new DifficultySelectorButton(b)
{
State = DifficultySelectorState.NotSelected,
OnHovered = beatmap =>
{
showBeatmap(beatmap);
starRating.Text = beatmap.StarDifficulty.ToString("Star Difficulty 0.##");
starRating.FadeIn(100);
},
OnClicked = beatmap => { Beatmap.Value = beatmap; },
});
}
starRating.FadeOut(100);
Beatmap.Value = Difficulties.FirstOrDefault()?.Beatmap;
plays.Value = BeatmapSet?.OnlineInfo.PlayCount ?? 0;
favourites.Value = BeatmapSet?.OnlineInfo.FavouriteCount ?? 0;
updateDifficultyButtons();
}
private void showBeatmap(BeatmapInfo beatmap)
{
version.Text = beatmap?.Version;
}
2018-04-13 09:19:50 +00:00
private void updateDifficultyButtons()
{
Difficulties.Children.ToList().ForEach(diff => diff.State = diff.Beatmap == Beatmap.Value ? DifficultySelectorState.Selected : DifficultySelectorState.NotSelected);
2018-04-13 09:19:50 +00:00
}
public class DifficultiesContainer : FillFlowContainer<DifficultySelectorButton>
2018-04-13 09:19:50 +00:00
{
public Action OnLostHover;
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 09:19:50 +00:00
{
2018-10-02 03:02:47 +00:00
base.OnHoverLost(e);
2018-04-13 09:19:50 +00:00
OnLostHover?.Invoke();
}
}
public class DifficultySelectorButton : OsuClickableContainer, IStateful<DifficultySelectorState>
2018-04-13 09:19:50 +00:00
{
private const float transition_duration = 100;
private const float size = 54;
private const float background_size = size - 2;
2018-04-13 09:19:50 +00:00
private readonly Container background;
private readonly Box backgroundBox;
2018-04-13 09:19:50 +00:00
private readonly DifficultyIcon icon;
public readonly BeatmapInfo Beatmap;
public Action<BeatmapInfo> OnHovered;
public Action<BeatmapInfo> OnClicked;
public event Action<DifficultySelectorState> StateChanged;
private DifficultySelectorState state;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
public DifficultySelectorState State
{
get => state;
2018-04-13 09:19:50 +00:00
set
{
if (value == state) return;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
state = value;
StateChanged?.Invoke(State);
if (value == DifficultySelectorState.Selected)
fadeIn();
else
fadeOut();
}
}
public DifficultySelectorButton(BeatmapInfo beatmap)
{
Beatmap = beatmap;
Size = new Vector2(size);
Margin = new MarginPadding { Horizontal = tile_spacing / 2 };
Children = new Drawable[]
{
background = new Container
2018-04-13 09:19:50 +00:00
{
Size = new Vector2(background_size),
2018-04-13 09:19:50 +00:00
Masking = true,
CornerRadius = 4,
Child = backgroundBox = new Box
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Alpha = 0.5f
}
2018-04-13 09:19:50 +00:00
},
2019-08-15 03:11:54 +00:00
icon = new DifficultyIcon(beatmap, shouldShowTooltip: false)
2018-04-13 09:19:50 +00:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(size - tile_icon_padding * 2),
Margin = new MarginPadding { Bottom = 1 },
},
};
}
2018-10-02 03:02:47 +00:00
protected override bool OnHover(HoverEvent e)
2018-04-13 09:19:50 +00:00
{
fadeIn();
OnHovered?.Invoke(Beatmap);
2018-10-02 03:02:47 +00:00
return base.OnHover(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 09:19:50 +00:00
{
if (State == DifficultySelectorState.NotSelected)
fadeOut();
2018-10-02 03:02:47 +00:00
base.OnHoverLost(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnClick(ClickEvent e)
2018-04-13 09:19:50 +00:00
{
OnClicked?.Invoke(Beatmap);
2018-10-02 03:02:47 +00:00
return base.OnClick(e);
2018-04-13 09:19:50 +00:00
}
private void fadeIn()
{
background.FadeIn(transition_duration);
2018-04-13 09:19:50 +00:00
icon.FadeIn(transition_duration);
}
private void fadeOut()
{
background.FadeOut();
2018-04-13 09:19:50 +00:00
icon.FadeTo(0.7f, transition_duration);
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
backgroundBox.Colour = colourProvider.Background6;
}
2018-04-13 09:19:50 +00:00
}
private class Statistic : FillFlowContainer
{
private readonly OsuSpriteText text;
private int value;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
public int Value
{
get => value;
2018-04-13 09:19:50 +00:00
set
{
this.value = value;
text.Text = Value.ToString(@"N0");
}
}
public Statistic(IconUsage icon)
2018-04-13 09:19:50 +00:00
{
AutoSizeAxes = Axes.Both;
Direction = FillDirection.Horizontal;
Spacing = new Vector2(2f);
Children = new Drawable[]
{
new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = icon,
Shadow = true,
Size = new Vector2(12),
2018-04-13 09:19:50 +00:00
},
text = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold, italics: true),
2018-04-13 09:19:50 +00:00
},
};
}
}
public enum DifficultySelectorState
2018-04-13 09:19:50 +00:00
{
Selected,
NotSelected,
}
}
}