mirror of
https://github.com/ppy/osu.git
synced 2025-03-19 01:17:19 +08:00
Refactor tables to avoid code duplication
This commit is contained in:
parent
c546df8a80
commit
800bda7e81
@ -29,6 +29,7 @@ namespace osu.Game.Tests.Visual.Online
|
||||
typeof(ScoresTable),
|
||||
typeof(CountriesTable),
|
||||
typeof(TableRowBackground),
|
||||
typeof(UserBasedTable),
|
||||
typeof(RankingsTable<>)
|
||||
};
|
||||
|
||||
|
@ -3,12 +3,10 @@
|
||||
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Users.Drawables;
|
||||
using osuTK;
|
||||
using System;
|
||||
using osu.Game.Users;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Graphics;
|
||||
|
||||
namespace osu.Game.Overlays.Rankings.Tables
|
||||
{
|
||||
@ -29,31 +27,16 @@ namespace osu.Game.Overlays.Rankings.Tables
|
||||
new TableColumn("Avg. Perf.", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
};
|
||||
|
||||
protected override Drawable[] CreateContent(int index, CountryStatistics item) => new Drawable[]
|
||||
protected override Country GetCountry(CountryStatistics item) => item.Country;
|
||||
|
||||
protected override Drawable CreateFlagContent(CountryStatistics item) => new OsuSpriteText
|
||||
{
|
||||
Font = OsuFont.GetFont(size: TEXT_SIZE),
|
||||
Text = $@"{item.Country.FullName}",
|
||||
};
|
||||
|
||||
protected override Drawable[] CreateAdditionalContent(CountryStatistics item) => new[]
|
||||
{
|
||||
new OsuSpriteText
|
||||
{
|
||||
Text = $"#{index + 1}",
|
||||
Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold)
|
||||
},
|
||||
new FillFlowContainer
|
||||
{
|
||||
AutoSizeAxes = Axes.Both,
|
||||
Direction = FillDirection.Horizontal,
|
||||
Spacing = new Vector2(7, 0),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new UpdateableFlag(item.Country)
|
||||
{
|
||||
Size = new Vector2(20, 13),
|
||||
ShowPlaceholderOnNull = false,
|
||||
},
|
||||
new RowText
|
||||
{
|
||||
Text = $@"{item.Country.FullName}",
|
||||
}
|
||||
}
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.ActiveUsers:N0}",
|
||||
|
@ -3,91 +3,28 @@
|
||||
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using System.Collections.Generic;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Users.Drawables;
|
||||
using osuTK;
|
||||
using osu.Game.Users;
|
||||
|
||||
namespace osu.Game.Overlays.Rankings.Tables
|
||||
{
|
||||
public class PerformanceTable : RankingsTable<UserStatistics>
|
||||
public class PerformanceTable : UserBasedTable
|
||||
{
|
||||
public PerformanceTable(int page = 1)
|
||||
: base(page)
|
||||
{
|
||||
}
|
||||
|
||||
protected override TableColumn[] CreateAdditionalHeaders() => new[]
|
||||
protected override TableColumn[] CreateUniqueHeaders() => new[]
|
||||
{
|
||||
new TableColumn("Accuracy", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Play Count", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Performance", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("SS", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("S", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("A", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
};
|
||||
|
||||
protected override Drawable[] CreateContent(int index, UserStatistics item)
|
||||
protected override Drawable[] CreateUniqueContent(UserStatistics item) => new[]
|
||||
{
|
||||
var content = new List<Drawable>
|
||||
new RowText
|
||||
{
|
||||
new OsuSpriteText
|
||||
{
|
||||
Text = $"#{index + 1}",
|
||||
Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold)
|
||||
},
|
||||
};
|
||||
|
||||
var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE)) { AutoSizeAxes = Axes.Both };
|
||||
username.AddUserLink(item.User);
|
||||
|
||||
content.AddRange(new Drawable[]
|
||||
{
|
||||
new FillFlowContainer
|
||||
{
|
||||
AutoSizeAxes = Axes.Both,
|
||||
Direction = FillDirection.Horizontal,
|
||||
Spacing = new Vector2(7, 0),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new UpdateableFlag(item.User.Country)
|
||||
{
|
||||
Size = new Vector2(20, 13),
|
||||
ShowPlaceholderOnNull = false,
|
||||
},
|
||||
username
|
||||
}
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.Accuracy:F2}%",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.PlayCount:N0}",
|
||||
},
|
||||
new RowText
|
||||
{
|
||||
Text = $@"{item.PP:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.SS + item.GradesCount.SSPlus:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.S + item.GradesCount.SPlus:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.A:N0}",
|
||||
},
|
||||
});
|
||||
|
||||
return content.ToArray();
|
||||
}
|
||||
Text = $@"{item.PP:N0}",
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,9 @@ using osu.Framework.Extensions;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Framework.Extensions.IEnumerableExtensions;
|
||||
using osu.Game.Users;
|
||||
using osu.Game.Users.Drawables;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Overlays.Rankings.Tables
|
||||
{
|
||||
@ -54,10 +57,12 @@ namespace osu.Game.Overlays.Rankings.Tables
|
||||
value.ForEach(_ => backgroundFlow.Add(new TableRowBackground()));
|
||||
|
||||
Columns = mainHeaders.Concat(CreateAdditionalHeaders()).ToArray();
|
||||
Content = value.Select((s, i) => CreateContent((page - 1) * items_per_page + i, s)).ToArray().ToRectangular();
|
||||
Content = value.Select((s, i) => createContent((page - 1) * items_per_page + i, s)).ToArray().ToRectangular();
|
||||
}
|
||||
}
|
||||
|
||||
private Drawable[] createContent(int index, TModel item) => new Drawable[] { createIndexDrawable(index), createMainContent(item) }.Concat(CreateAdditionalContent(item)).ToArray();
|
||||
|
||||
private static TableColumn[] mainHeaders => new[]
|
||||
{
|
||||
new TableColumn(string.Empty, Anchor.Centre, new Dimension(GridSizeMode.Absolute, 50)), // place
|
||||
@ -66,10 +71,36 @@ namespace osu.Game.Overlays.Rankings.Tables
|
||||
|
||||
protected abstract TableColumn[] CreateAdditionalHeaders();
|
||||
|
||||
protected abstract Drawable[] CreateContent(int index, TModel item);
|
||||
protected abstract Drawable[] CreateAdditionalContent(TModel item);
|
||||
|
||||
protected override Drawable CreateHeader(int index, TableColumn column) => new HeaderText(column?.Header ?? string.Empty, HighlightedColumn());
|
||||
|
||||
protected abstract Country GetCountry(TModel item);
|
||||
|
||||
protected abstract Drawable CreateFlagContent(TModel item);
|
||||
|
||||
private OsuSpriteText createIndexDrawable(int index) => new OsuSpriteText
|
||||
{
|
||||
Text = $"#{index + 1}",
|
||||
Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold)
|
||||
};
|
||||
|
||||
private FillFlowContainer createMainContent(TModel item) => new FillFlowContainer
|
||||
{
|
||||
AutoSizeAxes = Axes.Both,
|
||||
Direction = FillDirection.Horizontal,
|
||||
Spacing = new Vector2(7, 0),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new UpdateableFlag(GetCountry(item))
|
||||
{
|
||||
Size = new Vector2(20, 13),
|
||||
ShowPlaceholderOnNull = false,
|
||||
},
|
||||
CreateFlagContent(item)
|
||||
}
|
||||
};
|
||||
|
||||
protected virtual string HighlightedColumn() => @"Performance";
|
||||
|
||||
private class HeaderText : OsuSpriteText
|
||||
|
@ -3,97 +3,34 @@
|
||||
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Users.Drawables;
|
||||
using osuTK;
|
||||
using System.Collections.Generic;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osu.Game.Users;
|
||||
|
||||
namespace osu.Game.Overlays.Rankings.Tables
|
||||
{
|
||||
public class ScoresTable : RankingsTable<UserStatistics>
|
||||
public class ScoresTable : UserBasedTable
|
||||
{
|
||||
public ScoresTable(int page = 1)
|
||||
: base(page)
|
||||
{
|
||||
}
|
||||
|
||||
protected override TableColumn[] CreateAdditionalHeaders() => new[]
|
||||
protected override TableColumn[] CreateUniqueHeaders() => new[]
|
||||
{
|
||||
new TableColumn("Accuracy", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Play Count", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Total Score", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Ranked Score", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("SS", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("S", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("A", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Ranked Score", Anchor.Centre, new Dimension(GridSizeMode.AutoSize))
|
||||
};
|
||||
|
||||
protected override Drawable[] CreateContent(int index, UserStatistics item)
|
||||
protected override Drawable[] CreateUniqueContent(UserStatistics item) => new[]
|
||||
{
|
||||
var content = new List<Drawable>
|
||||
new ColoredRowText
|
||||
{
|
||||
new OsuSpriteText
|
||||
{
|
||||
Text = $"#{index + 1}",
|
||||
Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold)
|
||||
},
|
||||
};
|
||||
|
||||
var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE)) { AutoSizeAxes = Axes.Both };
|
||||
username.AddUserLink(item.User);
|
||||
|
||||
content.AddRange(new Drawable[]
|
||||
Text = $@"{item.TotalScore:N0}",
|
||||
},
|
||||
new RowText
|
||||
{
|
||||
new FillFlowContainer
|
||||
{
|
||||
AutoSizeAxes = Axes.Both,
|
||||
Direction = FillDirection.Horizontal,
|
||||
Spacing = new Vector2(7, 0),
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new UpdateableFlag(item.User.Country)
|
||||
{
|
||||
Size = new Vector2(20, 13),
|
||||
ShowPlaceholderOnNull = false,
|
||||
},
|
||||
username
|
||||
}
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.Accuracy:F2}%",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.PlayCount:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.TotalScore:N0}",
|
||||
},
|
||||
new RowText
|
||||
{
|
||||
Text = $@"{item.RankedScore:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.SS + item.GradesCount.SSPlus:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.S + item.GradesCount.SPlus:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.A:N0}",
|
||||
},
|
||||
});
|
||||
|
||||
return content.ToArray();
|
||||
}
|
||||
Text = $@"{item.RankedScore:N0}",
|
||||
}
|
||||
};
|
||||
|
||||
protected override string HighlightedColumn() => @"Ranked Score";
|
||||
}
|
||||
|
70
osu.Game/Overlays/Rankings/Tables/UserBasedTable.cs
Normal file
70
osu.Game/Overlays/Rankings/Tables/UserBasedTable.cs
Normal file
@ -0,0 +1,70 @@
|
||||
// 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.
|
||||
|
||||
using System.Linq;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osu.Game.Users;
|
||||
|
||||
namespace osu.Game.Overlays.Rankings.Tables
|
||||
{
|
||||
public abstract class UserBasedTable : RankingsTable<UserStatistics>
|
||||
{
|
||||
protected UserBasedTable(int page)
|
||||
: base(page)
|
||||
{
|
||||
}
|
||||
|
||||
protected override TableColumn[] CreateAdditionalHeaders() => new[]
|
||||
{
|
||||
new TableColumn("Accuracy", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("Play Count", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
}.Concat(CreateUniqueHeaders()).Concat(new[]
|
||||
{
|
||||
new TableColumn("SS", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("S", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
new TableColumn("A", Anchor.Centre, new Dimension(GridSizeMode.AutoSize)),
|
||||
}).ToArray();
|
||||
|
||||
protected override Country GetCountry(UserStatistics item) => item.User.Country;
|
||||
|
||||
protected override Drawable CreateFlagContent(UserStatistics item)
|
||||
{
|
||||
var username = new LinkFlowContainer(t => t.Font = OsuFont.GetFont(size: TEXT_SIZE)) { AutoSizeAxes = Axes.Both };
|
||||
username.AddUserLink(item.User);
|
||||
return username;
|
||||
}
|
||||
|
||||
protected override Drawable[] CreateAdditionalContent(UserStatistics item) => new[]
|
||||
{
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.Accuracy:F2}%",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.PlayCount:N0}",
|
||||
},
|
||||
}.Concat(CreateUniqueContent(item)).Concat(new[]
|
||||
{
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.SS + item.GradesCount.SSPlus:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.S + item.GradesCount.SPlus:N0}",
|
||||
},
|
||||
new ColoredRowText
|
||||
{
|
||||
Text = $@"{item.GradesCount.A:N0}",
|
||||
}
|
||||
}).ToArray();
|
||||
|
||||
protected abstract TableColumn[] CreateUniqueHeaders();
|
||||
|
||||
protected abstract Drawable[] CreateUniqueContent(UserStatistics item);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user