1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 07:42:57 +08:00

Reduce generic-ness

This commit is contained in:
Dean Herbert 2018-12-22 15:20:35 +09:00
parent 23259b295c
commit 787e65c3c5
4 changed files with 46 additions and 74 deletions

View File

@ -18,14 +18,14 @@ using osuTK.Graphics;
namespace osu.Game.Online.Leaderboards
{
public abstract class Leaderboard<TScope, TScoreModel> : Container
public abstract class Leaderboard<TScope, ScoreInfo> : Container
{
private const double fade_duration = 300;
private readonly ScrollContainer scrollContainer;
private readonly Container placeholderContainer;
private FillFlowContainer<LeaderboardScore<TScoreModel>> scrollFlow;
private FillFlowContainer<LeaderboardScore> scrollFlow;
private readonly LoadingAnimation loading;
@ -33,9 +33,9 @@ namespace osu.Game.Online.Leaderboards
private bool scoresLoadedOnce;
private IEnumerable<TScoreModel> scores;
private IEnumerable<ScoreInfo> scores;
public IEnumerable<TScoreModel> Scores
public IEnumerable<ScoreInfo> Scores
{
get { return scores; }
set
@ -55,13 +55,13 @@ namespace osu.Game.Online.Leaderboards
// ensure placeholder is hidden when displaying scores
PlaceholderState = PlaceholderState.Successful;
var flow = scrollFlow = new FillFlowContainer<LeaderboardScore<TScoreModel>>
var flow = scrollFlow = new FillFlowContainer<LeaderboardScore>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0f, 5f),
Padding = new MarginPadding { Top = 10, Bottom = 5 },
ChildrenEnumerable = scores.Select((s, index) => CreateScoreVisualiser(s, index + 1))
ChildrenEnumerable = scores.Select((s, index) => CreateDrawableScore(s, index + 1))
};
// schedule because we may not be loaded yet (LoadComponentAsync complains).
@ -240,7 +240,7 @@ namespace osu.Game.Online.Leaderboards
});
}
protected abstract APIRequest FetchScores(Action<IEnumerable<TScoreModel>> scoresCallback);
protected abstract APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback);
private Placeholder currentPlaceholder;
@ -295,6 +295,6 @@ namespace osu.Game.Online.Leaderboards
}
}
protected abstract LeaderboardScore<TScoreModel> CreateScoreVisualiser(TScoreModel model, int index);
protected abstract LeaderboardScore CreateDrawableScore(ScoreInfo model, int index);
}
}

View File

@ -13,7 +13,6 @@ using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
using osu.Game.Scoring;
using osu.Game.Users;
@ -22,15 +21,12 @@ using osuTK.Graphics;
namespace osu.Game.Online.Leaderboards
{
public static class LeaderboardScore
{
public const float HEIGHT = 60;
}
public abstract class LeaderboardScore<TScoreModel> : OsuClickableContainer
public class LeaderboardScore : OsuClickableContainer
{
public readonly int RankPosition;
public const float HEIGHT = 60;
private const float corner_radius = 5;
private const float edge_margin = 5;
private const float background_alpha = 0.25f;
@ -38,7 +34,7 @@ namespace osu.Game.Online.Leaderboards
protected Container RankContainer { get; private set; }
private readonly TScoreModel score;
private readonly ScoreInfo score;
private Box background;
private Container content;
@ -51,21 +47,27 @@ namespace osu.Game.Online.Leaderboards
private List<ScoreComponentLabel> statisticsLabels;
protected LeaderboardScore(TScoreModel score, int rank)
public LeaderboardScore(ScoreInfo score, int rank)
{
this.score = score;
RankPosition = rank;
RelativeSizeAxes = Axes.X;
Height = LeaderboardScore.HEIGHT;
Height = HEIGHT;
}
protected virtual IEnumerable<LeaderboardScoreStatistic> GetStatistics(ScoreInfo model) => new[]
{
new LeaderboardScoreStatistic(FontAwesome.fa_link, "Max Combo", model.MaxCombo.ToString()),
new LeaderboardScoreStatistic(FontAwesome.fa_crosshairs, "Accuracy", string.Format(model.Accuracy % 1 == 0 ? @"{0:P0}" : @"{0:P2}", model.Accuracy))
};
[BackgroundDependencyLoader]
private void load()
{
var user = GetUser(score);
var user = score.User;
statisticsLabels = GetStatistics(score).Select(s => new ScoreComponentLabel(s.icon, s.value, s.name)).ToList();
statisticsLabels = GetStatistics(score).Select(s => new ScoreComponentLabel(s)).ToList();
Children = new Drawable[]
{
@ -129,13 +131,13 @@ namespace osu.Game.Online.Leaderboards
})
{
RelativeSizeAxes = Axes.None,
Size = new Vector2(LeaderboardScore.HEIGHT - edge_margin * 2, LeaderboardScore.HEIGHT - edge_margin * 2),
Size = new Vector2(HEIGHT - edge_margin * 2, HEIGHT - edge_margin * 2),
},
new Container
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Position = new Vector2(LeaderboardScore.HEIGHT - edge_margin, 0f),
Position = new Vector2(HEIGHT - edge_margin, 0f),
Children = new Drawable[]
{
nameLabel = new OsuSpriteText
@ -191,13 +193,13 @@ namespace osu.Game.Online.Leaderboards
Spacing = new Vector2(5f, 0f),
Children = new Drawable[]
{
scoreLabel = new GlowingSpriteText(GetTotalScore(score).ToString(@"N0"), @"Venera", 23, Color4.White, OsuColour.FromHex(@"83ccfa")),
scoreLabel = new GlowingSpriteText(score.TotalScore.ToString(@"N0"), @"Venera", 23, Color4.White, OsuColour.FromHex(@"83ccfa")),
RankContainer = new Container
{
Size = new Vector2(40f, 20f),
Children = new[]
{
scoreRank = new DrawableRank(GetRank(score))
scoreRank = new DrawableRank(score.Rank)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
@ -213,7 +215,7 @@ namespace osu.Game.Online.Leaderboards
Origin = Anchor.BottomRight,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
ChildrenEnumerable = GetMods(score).Select(mod => new ModIcon(mod) { Scale = new Vector2(0.375f) })
ChildrenEnumerable = score.Mods.Select(mod => new ModIcon(mod) { Scale = new Vector2(0.375f) })
},
},
},
@ -271,16 +273,6 @@ namespace osu.Game.Online.Leaderboards
base.OnHoverLost(e);
}
protected abstract User GetUser(TScoreModel model);
protected abstract IEnumerable<Mod> GetMods(TScoreModel model);
protected abstract IEnumerable<(FontAwesome icon, string value, string name)> GetStatistics(TScoreModel model);
protected abstract int GetTotalScore(TScoreModel model);
protected abstract ScoreRank GetRank(TScoreModel model);
private class GlowingSpriteText : Container
{
public GlowingSpriteText(string text, string font, int textSize, Color4 textColour, Color4 glowColour)
@ -339,9 +331,9 @@ namespace osu.Game.Online.Leaderboards
public string TooltipText => name;
public ScoreComponentLabel(FontAwesome icon, string value, string name)
public ScoreComponentLabel(LeaderboardScoreStatistic statistic)
{
this.name = name;
name = statistic.Name;
AutoSizeAxes = Axes.Both;
Child = content = new FillFlowContainer
@ -373,11 +365,11 @@ namespace osu.Game.Online.Leaderboards
Origin = Anchor.Centre,
Size = new Vector2(icon_size - 6),
Colour = OsuColour.FromHex(@"a4edff"),
Icon = icon,
Icon = statistic.Icon,
},
},
},
new GlowingSpriteText(value, @"Exo2.0-Bold", 17, Color4.White, OsuColour.FromHex(@"83ccfa"))
new GlowingSpriteText(statistic.Value, @"Exo2.0-Bold", 17, Color4.White, OsuColour.FromHex(@"83ccfa"))
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
@ -386,5 +378,19 @@ namespace osu.Game.Online.Leaderboards
};
}
}
public class LeaderboardScoreStatistic
{
public FontAwesome Icon;
public string Value;
public string Name;
public LeaderboardScoreStatistic(FontAwesome icon, string name, string value)
{
Icon = icon;
Name = name;
Value = value;
}
}
}
}

View File

@ -79,7 +79,7 @@ namespace osu.Game.Screens.Select.Leaderboards
return req;
}
protected override LeaderboardScore<ScoreInfo> CreateScoreVisualiser(ScoreInfo model, int index) => new BeatmapLeaderboardScore(model, index)
protected override LeaderboardScore CreateDrawableScore(ScoreInfo model, int index) => new LeaderboardScore(model, index)
{
Action = () => ScoreSelected?.Invoke(model)
};

View File

@ -1,34 +0,0 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using osu.Game.Graphics;
using osu.Game.Online.Leaderboards;
using osu.Game.Rulesets.Mods;
using osu.Game.Scoring;
using osu.Game.Users;
namespace osu.Game.Screens.Select.Leaderboards
{
public class BeatmapLeaderboardScore : LeaderboardScore<ScoreInfo>
{
public BeatmapLeaderboardScore(ScoreInfo score, int rank)
: base(score, rank)
{
}
protected override User GetUser(ScoreInfo model) => model.User;
protected override IEnumerable<Mod> GetMods(ScoreInfo model) => model.Mods;
protected override IEnumerable<(FontAwesome icon, string value, string name)> GetStatistics(ScoreInfo model) => new[]
{
(FontAwesome.fa_link, model.MaxCombo.ToString(), "Max Combo"),
(FontAwesome.fa_crosshairs, string.Format(model.Accuracy % 1 == 0 ? @"{0:P0}" : @"{0:P2}", model.Accuracy), "Accuracy")
};
protected override int GetTotalScore(ScoreInfo model) => model.TotalScore;
protected override ScoreRank GetRank(ScoreInfo model) => model.Rank;
}
}