osu/osu.Game.Tournament/Screens/Ladder/LadderScreen.cs

180 lines
6.4 KiB
C#
Raw Normal View History

2019-03-04 04:24:19 +00:00
// 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.Collections.Specialized;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Caching;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
2018-11-17 05:59:37 +00:00
using osu.Framework.Platform;
using osu.Game.Graphics;
using osu.Game.Tournament.Components;
2019-06-18 05:51:48 +00:00
using osu.Game.Tournament.Models;
using osu.Game.Tournament.Screens.Editors;
using osu.Game.Tournament.Screens.Ladder.Components;
using osuTK.Graphics;
namespace osu.Game.Tournament.Screens.Ladder
{
2018-11-17 05:59:37 +00:00
public class LadderScreen : TournamentScreen, IProvideVideo
{
2019-06-18 05:57:05 +00:00
protected Container<DrawableTournamentMatch> MatchesContainer;
private Container<Path> paths;
private Container headings;
2019-06-13 07:11:15 +00:00
protected LadderDragContainer ScrollContent;
2019-06-13 08:04:57 +00:00
protected Container Content;
[BackgroundDependencyLoader]
2018-11-17 05:59:37 +00:00
private void load(OsuColour colours, Storage storage)
{
normalPathColour = colours.BlueDarker.Darken(2);
losersPathColour = colours.YellowDarker.Darken(2);
RelativeSizeAxes = Axes.Both;
2019-06-13 08:04:57 +00:00
InternalChild = Content = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new TourneyVideo("ladder")
2018-11-17 05:59:37 +00:00
{
RelativeSizeAxes = Axes.Both,
Loop = true,
},
2019-06-13 07:11:15 +00:00
ScrollContent = new LadderDragContainer
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
paths = new Container<Path> { RelativeSizeAxes = Axes.Both },
headings = new Container { RelativeSizeAxes = Axes.Both },
2019-06-18 05:57:05 +00:00
MatchesContainer = new Container<DrawableTournamentMatch> { RelativeSizeAxes = Axes.Both },
}
},
}
};
2019-06-18 05:57:05 +00:00
void addMatch(TournamentMatch match) =>
MatchesContainer.Add(new DrawableTournamentMatch(match, this is LadderEditorScreen)
{
Changed = () => layout.Invalidate()
});
2019-06-18 05:57:05 +00:00
foreach (var match in LadderInfo.Matches)
addMatch(match);
LadderInfo.Rounds.CollectionChanged += (_, __) => layout.Invalidate();
LadderInfo.Matches.CollectionChanged += (_, args) =>
{
switch (args.Action)
2019-11-11 11:53:22 +00:00
{
case NotifyCollectionChangedAction.Add:
foreach (var p in args.NewItems.Cast<TournamentMatch>())
addMatch(p);
break;
case NotifyCollectionChangedAction.Remove:
foreach (var p in args.OldItems.Cast<TournamentMatch>())
{
foreach (var d in MatchesContainer.Where(d => d.Match == p))
d.Expire();
}
break;
2019-11-11 11:53:22 +00:00
}
layout.Invalidate();
};
}
private readonly Cached layout = new Cached();
protected override void Update()
{
base.Update();
if (!layout.IsValid)
UpdateLayout();
}
private Color4 normalPathColour;
private Color4 losersPathColour;
protected virtual bool DrawLoserPaths => false;
protected virtual void UpdateLayout()
{
paths.Clear();
headings.Clear();
int id = 1;
2019-06-18 05:57:05 +00:00
foreach (var match in MatchesContainer.OrderBy(d => d.Y).ThenBy(d => d.X))
{
2019-06-18 05:57:05 +00:00
match.Match.ID = id++;
2019-06-18 05:57:05 +00:00
if (match.Match.Progression.Value != null)
{
2019-06-18 05:57:05 +00:00
var dest = MatchesContainer.FirstOrDefault(p => p.Match == match.Match.Progression.Value);
if (dest == null)
// clean up outdated progressions.
2019-06-18 05:57:05 +00:00
match.Match.Progression.Value = null;
else
2019-06-18 05:57:05 +00:00
paths.Add(new ProgressionPath(match, dest) { Colour = match.Match.Losers.Value ? losersPathColour : normalPathColour });
}
if (DrawLoserPaths)
{
2019-06-18 05:57:05 +00:00
if (match.Match.LosersProgression.Value != null)
{
2019-06-18 05:57:05 +00:00
var dest = MatchesContainer.FirstOrDefault(p => p.Match == match.Match.LosersProgression.Value);
if (dest == null)
// clean up outdated progressions.
2019-06-18 05:57:05 +00:00
match.Match.LosersProgression.Value = null;
else
2019-06-18 05:57:05 +00:00
paths.Add(new ProgressionPath(match, dest) { Colour = losersPathColour.Opacity(0.1f) });
}
}
}
2019-06-18 05:44:15 +00:00
foreach (var round in LadderInfo.Rounds)
{
2019-06-18 05:57:05 +00:00
var topMatch = MatchesContainer.Where(p => !p.Match.Losers.Value && p.Match.Round.Value == round).OrderBy(p => p.Y).FirstOrDefault();
2019-06-18 05:57:05 +00:00
if (topMatch == null) continue;
2019-06-18 05:44:15 +00:00
headings.Add(new DrawableTournamentRound(round)
{
2019-06-18 05:57:05 +00:00
Position = headings.ToLocalSpace((topMatch.ScreenSpaceDrawQuad.TopLeft + topMatch.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
2019-06-18 05:44:15 +00:00
foreach (var round in LadderInfo.Rounds)
{
2019-06-18 05:57:05 +00:00
var topMatch = MatchesContainer.Where(p => p.Match.Losers.Value && p.Match.Round.Value == round).OrderBy(p => p.Y).FirstOrDefault();
2019-06-18 05:57:05 +00:00
if (topMatch == null) continue;
2019-06-18 05:44:15 +00:00
headings.Add(new DrawableTournamentRound(round, true)
{
2019-06-18 05:57:05 +00:00
Position = headings.ToLocalSpace((topMatch.ScreenSpaceDrawQuad.TopLeft + topMatch.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
layout.Validate();
}
}
}