1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 22:07:29 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/LadderManager.cs

187 lines
6.3 KiB
C#
Raw Normal View History

2018-09-22 04:52:25 +08:00
using System;
using System.Collections.Generic;
2018-09-16 04:35:51 +08:00
using System.Linq;
2018-09-22 04:52:25 +08:00
using osu.Framework.Allocation;
using osu.Framework.Configuration;
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.States;
2018-09-21 18:58:47 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Tournament.Components;
2018-09-21 17:51:37 +08:00
using osu.Game.Tournament.Screens.Ladder.Components;
using SixLabors.Primitives;
2018-09-21 17:51:37 +08:00
namespace osu.Game.Tournament.Screens.Ladder
{
2018-09-22 04:52:25 +08:00
public class LadderEditorInfo
{
public readonly BindableBool EditingEnabled = new BindableBool();
public List<TournamentTeam> Teams = new List<TournamentTeam>();
public readonly Bindable<MatchPairing> Selected = new Bindable<MatchPairing>();
}
2018-09-21 18:58:47 +08:00
public class LadderManager : CompositeDrawable, IHasContextMenu
{
public readonly List<TournamentTeam> Teams;
2018-09-16 04:35:51 +08:00
private readonly Container<DrawableMatchPairing> pairingsContainer;
private readonly Container<Path> paths;
2018-09-22 04:52:25 +08:00
[Cached]
private readonly LadderEditorInfo editorInfo = new LadderEditorInfo();
public LadderManager(LadderInfo info, List<TournamentTeam> teams)
{
2018-09-22 04:52:25 +08:00
editorInfo.Teams = Teams = teams;
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[]
{
paths = new Container<Path> { RelativeSizeAxes = Axes.Both },
2018-09-21 21:24:21 +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
}
};
2018-09-16 04:35:51 +08:00
foreach (var pair in info.Progressions)
2018-09-22 04:52:25 +08:00
{
var src = info.Pairings.FirstOrDefault(p => p.ID == pair.Item1);
var dest = info.Pairings.FirstOrDefault(p => p.ID == pair.Item2);
if (src == null) throw new InvalidOperationException();
if (dest != null)
src.Progression.Value = dest;
}
2018-09-16 04:35:51 +08:00
foreach (var pairing in info.Pairings)
addPairing(pairing);
}
2018-09-16 04:35:51 +08:00
public LadderInfo CreateInfo()
{
2018-09-16 04:35:51 +08:00
var pairings = pairingsContainer.Select(p => p.Pairing).ToList();
return new LadderInfo
{
Pairings = pairings,
Progressions = pairings
.Where(p => p.Progression.Value != null)
.Select(p => (p.ID, p.Progression.Value.ID))
.ToList()
};
}
2018-09-16 04:35:51 +08:00
private void addPairing(MatchPairing pairing) => pairingsContainer.Add(new DrawableMatchPairing(pairing));
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, () =>
{
var pos = ToLocalSpace(GetContainingInputManager().CurrentState.Mouse.Position);
addPairing(new MatchPairing { Position = new Point((int)pos.X, (int)pos.Y) });
}),
};
}
}
2018-09-16 04:35:51 +08:00
protected override void Update()
{
base.Update();
paths.Clear();
int id = 0;
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));
}
2018-09-16 04:35:51 +08:00
}
}
2018-09-21 18:58:47 +08:00
public void RequestJoin(MatchPairing pairing) => AddInternal(new JoinRequestHandler(pairingsContainer, pairing));
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-21 17:51:37 +08:00
private ProgressionPath path;
2018-09-19 01:00:16 +08:00
public JoinRequestHandler(Container<DrawableMatchPairing> pairingsContainer, MatchPairing source)
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;
Source.Progression.Value = null;
}
private DrawableMatchPairing findTarget(InputState state) => pairingsContainer.FirstOrDefault(d => d.ReceiveMouseInputAt(state.Mouse.NativeState.Position));
protected override bool OnMouseMove(InputState state)
{
var found = findTarget(state);
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) { Alpha = 0.4f });
return base.OnMouseMove(state);
2018-09-16 04:35:51 +08:00
}
protected override bool OnClick(InputState state)
{
2018-09-21 17:51:37 +08:00
var found = findTarget(state);
2018-09-19 01:00:16 +08:00
if (found != null)
{
2018-09-21 18:58:47 +08:00
if (found.Pairing != Source)
Source.Progression.Value = found.Pairing;
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
}
}
2018-09-21 18:58:47 +08:00
public void Remove(MatchPairing pairing) => pairingsContainer.FirstOrDefault(p => p.Pairing == pairing)?.Remove();
}
}