1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 04:02:59 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/LadderManager.cs

260 lines
9.3 KiB
C#
Raw Normal View History

2018-09-25 09:27:54 +08:00
// 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;
2018-09-16 04:35:51 +08:00
using System.Linq;
2018-09-22 04:52:25 +08:00
using osu.Framework.Allocation;
2018-09-24 23:57:44 +08:00
using osu.Framework.Caching;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-09-21 18:58:47 +08:00
using osu.Framework.Graphics.Cursor;
2018-09-16 04:35:51 +08:00
using osu.Framework.Graphics.Lines;
2018-09-21 18:58:47 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Graphics;
2018-09-21 18:58:47 +08:00
using osu.Game.Graphics.UserInterface;
2018-09-21 17:51:37 +08:00
using osu.Game.Tournament.Screens.Ladder.Components;
using OpenTK;
using OpenTK.Graphics;
using SixLabors.Primitives;
2018-09-21 17:51:37 +08:00
namespace osu.Game.Tournament.Screens.Ladder
{
2018-11-07 00:20:32 +08:00
[Cached]
public class LadderManager : TournamentScreen, IHasContextMenu
{
2018-11-08 05:29:04 +08:00
private Container<DrawableMatchPairing> pairingsContainer;
private Container<Path> paths;
private Container headings;
2018-11-08 05:29:04 +08:00
private ScrollableContainer scrollContent;
2018-11-11 09:13:17 +08:00
[Resolved]
private LadderEditorInfo editorInfo { get; set;}
2018-09-22 04:52:25 +08:00
[Resolved]
private LadderInfo ladderInfo { get; set; }
2018-11-08 05:29:04 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2018-11-08 05:29:04 +08:00
normalPathColour = colours.BlueDarker.Darken(2);
losersPathColour = colours.YellowDarker.Darken(2);
RelativeSizeAxes = Axes.Both;
2018-09-21 18:58:47 +08:00
InternalChild = new Container
{
2018-09-16 04:35:51 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
scrollContent = new ScrollableContainer
2018-09-24 15:34:46 +08:00
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
paths = new Container<Path> { RelativeSizeAxes = Axes.Both },
2018-09-25 01:31:48 +08:00
headings = new Container { RelativeSizeAxes = Axes.Both },
2018-09-24 15:34:46 +08:00
pairingsContainer = new Container<DrawableMatchPairing> { RelativeSizeAxes = Axes.Both },
}
},
2018-09-22 04:52:25 +08:00
new LadderEditorSettings
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(5)
}
2018-09-16 04:35:51 +08:00
}
};
foreach (var pairing in ladderInfo.Pairings)
addPairing(pairing);
2018-09-24 22:30:37 +08:00
2018-09-24 23:57:44 +08:00
// todo: fix this
Scheduler.AddDelayed(() => layout.Invalidate(), 100, true);
}
private void updateInfo()
{
ladderInfo.Pairings = pairingsContainer.Select(p => p.Pairing).ToList();
foreach (var g in ladderInfo.Groupings)
g.Pairings = ladderInfo.Pairings.Where(p => p.Grouping.Value == g).Select(p => p.ID).ToList();
2018-09-24 22:30:37 +08:00
ladderInfo.Progressions = ladderInfo.Pairings.Where(p => p.Progression.Value != null).Select(p => new TournamentProgression(p.ID, p.Progression.Value.ID)).Concat(
ladderInfo.Pairings.Where(p => p.LosersProgression.Value != null).Select(p => new TournamentProgression(p.ID, p.LosersProgression.Value.ID, true)))
.ToList();
}
private void addPairing(MatchPairing pairing)
{
pairingsContainer.Add(new DrawableMatchPairing(pairing));
updateInfo();
}
2018-09-22 04:52:25 +08:00
public MenuItem[] ContextMenuItems
{
2018-09-22 04:52:25 +08:00
get
{
2018-09-22 04:52:25 +08:00
if (!editorInfo.EditingEnabled)
return new MenuItem[0];
return new MenuItem[]
{
new OsuMenuItem("Create new match", MenuItemType.Highlighted, () =>
{
2018-09-25 01:31:48 +08:00
var pos = pairingsContainer.ToLocalSpace(GetContainingInputManager().CurrentState.Mouse.Position);
2018-09-22 04:52:25 +08:00
addPairing(new MatchPairing { Position = new Point((int)pos.X, (int)pos.Y) });
}),
};
}
}
2018-09-16 04:35:51 +08:00
2018-09-24 23:57:44 +08:00
private Cached layout = new Cached();
2018-09-16 04:35:51 +08:00
protected override void Update()
{
base.Update();
2018-09-24 23:57:44 +08:00
if (!layout.IsValid)
updateLayout();
}
private Color4 normalPathColour;
private Color4 losersPathColour;
2018-09-24 23:57:44 +08:00
private void updateLayout()
{
2018-09-16 04:35:51 +08:00
paths.Clear();
2018-09-24 23:57:44 +08:00
headings.Clear();
2018-09-16 04:35:51 +08:00
2018-09-22 05:36:45 +08:00
int id = 1;
2018-09-16 04:35:51 +08:00
foreach (var pairing in pairingsContainer.OrderBy(d => d.Y).ThenBy(d => d.X))
{
pairing.Pairing.ID = id++;
if (pairing.Pairing.Progression.Value != null)
2018-09-22 04:52:25 +08:00
{
var dest = pairingsContainer.FirstOrDefault(p => p.Pairing == pairing.Pairing.Progression.Value);
if (dest == null)
// clean up outdated progressions.
pairing.Pairing.Progression.Value = null;
else
paths.Add(new ProgressionPath(pairing, dest) { Colour = pairing.Pairing.Losers ? losersPathColour : normalPathColour });
2018-09-22 04:52:25 +08:00
}
2018-09-16 04:35:51 +08:00
}
2018-09-24 23:57:44 +08:00
foreach (var group in ladderInfo.Groupings)
2018-09-24 23:57:44 +08:00
{
2018-09-25 01:31:48 +08:00
var topPairing = pairingsContainer.Where(p => !p.Pairing.Losers && p.Pairing.Grouping.Value == group).OrderBy(p => p.Y).FirstOrDefault();
2018-09-24 23:57:44 +08:00
if (topPairing == null) continue;
headings.Add(new DrawableTournamentGrouping(group)
{
Position = headings.ToLocalSpace((topPairing.ScreenSpaceDrawQuad.TopLeft + topPairing.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
foreach (var group in ladderInfo.Groupings)
2018-09-25 01:31:48 +08:00
{
var topPairing = pairingsContainer.Where(p => p.Pairing.Losers && p.Pairing.Grouping.Value == group).OrderBy(p => p.Y).FirstOrDefault();
if (topPairing == null) continue;
headings.Add(new DrawableTournamentGrouping(group, true)
{
Position = headings.ToLocalSpace((topPairing.ScreenSpaceDrawQuad.TopLeft + topPairing.ScreenSpaceDrawQuad.TopRight) / 2),
Margin = new MarginPadding { Bottom = 10 },
Origin = Anchor.BottomCentre,
});
}
2018-09-24 23:57:44 +08:00
layout.Validate();
updateInfo();
2018-09-16 04:35:51 +08:00
}
public void RequestJoin(MatchPairing pairing, bool losers) => scrollContent.Add(new JoinRequestHandler(pairingsContainer, pairing, losers, updateInfo));
// todo: remove after ppy/osu-framework#1980 is merged.
public override bool HandlePositionalInput => true;
public void Remove(MatchPairing pairing) => pairingsContainer.FirstOrDefault(p => p.Pairing == pairing)?.Remove();
2018-09-16 04:35:51 +08:00
private class JoinRequestHandler : CompositeDrawable
{
2018-09-19 01:00:16 +08:00
private readonly Container<DrawableMatchPairing> pairingsContainer;
2018-09-16 04:35:51 +08:00
public readonly MatchPairing Source;
2018-09-25 01:31:48 +08:00
private readonly bool losers;
private readonly Action complete;
2018-09-16 04:35:51 +08:00
2018-09-21 17:51:37 +08:00
private ProgressionPath path;
public JoinRequestHandler(Container<DrawableMatchPairing> pairingsContainer, MatchPairing source, bool losers, Action complete)
2018-09-16 04:35:51 +08:00
{
2018-09-19 01:00:16 +08:00
this.pairingsContainer = pairingsContainer;
2018-09-16 04:35:51 +08:00
RelativeSizeAxes = Axes.Both;
2018-09-21 17:51:37 +08:00
Source = source;
2018-09-25 01:31:48 +08:00
this.losers = losers;
this.complete = complete;
2018-09-25 01:31:48 +08:00
if (losers)
Source.LosersProgression.Value = null;
else
Source.Progression.Value = null;
2018-09-21 17:51:37 +08:00
}
private DrawableMatchPairing findTarget(InputState state) => pairingsContainer.FirstOrDefault(d => d.ReceivePositionalInputAt(state.Mouse.Position));
2018-09-21 17:51:37 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
protected override bool OnMouseMove(MouseMoveEvent e)
2018-09-21 17:51:37 +08:00
{
var found = findTarget(e.CurrentState);
2018-09-21 17:51:37 +08:00
if (found == path?.Destination)
return false;
path?.Expire();
path = null;
if (found == null)
return false;
AddInternal(path = new ProgressionPath(pairingsContainer.First(c => c.Pairing == Source), found)
{
Colour = Color4.Yellow,
});
2018-09-21 17:51:37 +08:00
return base.OnMouseMove(e);
2018-09-16 04:35:51 +08:00
}
protected override bool OnClick(ClickEvent e)
2018-09-16 04:35:51 +08:00
{
var found = findTarget(e.CurrentState);
2018-09-19 01:00:16 +08:00
if (found != null)
{
2018-09-21 18:58:47 +08:00
if (found.Pairing != Source)
2018-09-25 01:31:48 +08:00
{
if (losers)
Source.LosersProgression.Value = found.Pairing;
else
Source.Progression.Value = found.Pairing;
}
complete?.Invoke();
2018-09-16 04:35:51 +08:00
Expire();
2018-09-19 01:00:16 +08:00
return true;
}
2018-09-16 04:35:51 +08:00
2018-09-19 01:00:16 +08:00
return false;
2018-09-16 04:35:51 +08:00
}
}
}
}