1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 10:07:28 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/LadderScreen.cs

180 lines
6.5 KiB
C#
Raw Normal View History

2019-03-04 12:24:19 +08: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 13:59:37 +08:00
using osu.Framework.Platform;
using osu.Game.Graphics;
using osu.Game.Tournament.Components;
2019-06-18 13:51:48 +08: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 13:59:37 +08:00
public class LadderScreen : TournamentScreen, IProvideVideo
{
2019-06-18 13:57:05 +08:00
protected Container<DrawableTournamentMatch> MatchesContainer;
private Container<Path> paths;
private Container headings;
2019-06-13 15:11:15 +08:00
protected LadderDragContainer ScrollContent;
2019-06-13 16:04:57 +08:00
protected Container Content;
[BackgroundDependencyLoader]
2018-11-17 13:59:37 +08:00
private void load(OsuColour colours, Storage storage)
{
normalPathColour = colours.BlueDarker.Darken(2);
losersPathColour = colours.YellowDarker.Darken(2);
RelativeSizeAxes = Axes.Both;
2019-06-13 16:04:57 +08:00
InternalChild = Content = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
2020-03-03 18:10:29 +08:00
new TourneyVideo(storage.GetStream(@"videos/ladder.m4v"))
2018-11-17 13:59:37 +08:00
{
RelativeSizeAxes = Axes.Both,
Loop = true,
},
2019-06-13 15:11:15 +08: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 13:57:05 +08:00
MatchesContainer = new Container<DrawableTournamentMatch> { RelativeSizeAxes = Axes.Both },
}
},
}
};
2019-06-18 13:57:05 +08:00
void addMatch(TournamentMatch match) =>
MatchesContainer.Add(new DrawableTournamentMatch(match, this is LadderEditorScreen)
{
Changed = () => layout.Invalidate()
});
2019-06-18 13:57:05 +08:00
foreach (var match in LadderInfo.Matches)
addMatch(match);
LadderInfo.Rounds.CollectionChanged += (_, __) => layout.Invalidate();
LadderInfo.Matches.CollectionChanged += (_, args) =>
{
switch (args.Action)
2019-11-11 19:53:22 +08: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 19:53:22 +08: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 13:57:05 +08:00
foreach (var match in MatchesContainer.OrderBy(d => d.Y).ThenBy(d => d.X))
{
2019-06-18 13:57:05 +08:00
match.Match.ID = id++;
2019-06-18 13:57:05 +08:00
if (match.Match.Progression.Value != null)
{
2019-06-18 13:57:05 +08:00
var dest = MatchesContainer.FirstOrDefault(p => p.Match == match.Match.Progression.Value);
if (dest == null)
// clean up outdated progressions.
2019-06-18 13:57:05 +08:00
match.Match.Progression.Value = null;
else
2019-06-18 13:57:05 +08:00
paths.Add(new ProgressionPath(match, dest) { Colour = match.Match.Losers.Value ? losersPathColour : normalPathColour });
}
if (DrawLoserPaths)
{
2019-06-18 13:57:05 +08:00
if (match.Match.LosersProgression.Value != null)
{
2019-06-18 13:57:05 +08:00
var dest = MatchesContainer.FirstOrDefault(p => p.Match == match.Match.LosersProgression.Value);
if (dest == null)
// clean up outdated progressions.
2019-06-18 13:57:05 +08:00
match.Match.LosersProgression.Value = null;
else
2019-06-18 13:57:05 +08:00
paths.Add(new ProgressionPath(match, dest) { Colour = losersPathColour.Opacity(0.1f) });
}
}
}
2019-06-18 13:44:15 +08:00
foreach (var round in LadderInfo.Rounds)
{
2019-06-18 13:57:05 +08:00
var topMatch = MatchesContainer.Where(p => !p.Match.Losers.Value && p.Match.Round.Value == round).OrderBy(p => p.Y).FirstOrDefault();
2019-06-18 13:57:05 +08:00
if (topMatch == null) continue;
2019-06-18 13:44:15 +08:00
headings.Add(new DrawableTournamentRound(round)
{
2019-06-18 13:57:05 +08:00
Position = headings.ToLocalSpace((topMatch.ScreenSpaceDrawQuad.TopLeft + topMatch.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
2019-06-18 13:44:15 +08:00
foreach (var round in LadderInfo.Rounds)
{
2019-06-18 13:57:05 +08:00
var topMatch = MatchesContainer.Where(p => p.Match.Losers.Value && p.Match.Round.Value == round).OrderBy(p => p.Y).FirstOrDefault();
2019-06-18 13:57:05 +08:00
if (topMatch == null) continue;
2019-06-18 13:44:15 +08:00
headings.Add(new DrawableTournamentRound(round, true)
{
2019-06-18 13:57:05 +08:00
Position = headings.ToLocalSpace((topMatch.ScreenSpaceDrawQuad.TopLeft + topMatch.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
layout.Validate();
}
}
}