1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 15:27:26 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/Components/LadderManager.cs

171 lines
5.5 KiB
C#
Raw Normal View History

2018-09-16 04:35:51 +08:00
using System;
using System.Collections.Generic;
2018-09-16 04:35:51 +08:00
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-09-16 04:35:51 +08:00
using osu.Framework.Graphics.Lines;
using osu.Framework.Input.States;
using osu.Game.Graphics.Cursor;
using osu.Game.Tournament.Components;
2018-09-16 04:35:51 +08:00
using OpenTK;
using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class LadderManager : CompositeDrawable
{
public readonly List<TournamentTeam> Teams;
2018-09-16 04:35:51 +08:00
private readonly Container<DrawableMatchPairing> pairingsContainer;
private readonly Container<Path> paths;
public LadderManager(LadderInfo info, List<TournamentTeam> teams)
{
Teams = teams;
RelativeSizeAxes = Axes.Both;
2018-09-16 04:35:51 +08:00
InternalChild = new OsuContextMenuContainer
{
2018-09-16 04:35:51 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
pairingsContainer = new Container<DrawableMatchPairing> { RelativeSizeAxes = Axes.Both },
paths = new Container<Path> { RelativeSizeAxes = Axes.Both },
}
};
2018-09-16 04:35:51 +08:00
foreach (var pair in info.Progressions)
info.Pairings.Single(p => p.ID == pair.Item1).Progression.Value = info.Pairings.Single(p => p.ID == pair.Item2);
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));
protected override bool OnClick(InputState state)
{
2018-09-16 04:35:51 +08:00
addPairing(new MatchPairing
{
Position = new Point((int)state.Mouse.Position.X, (int)state.Mouse.Position.Y)
});
return true;
}
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)
{
var progression = pairingsContainer.Single(p => p.Pairing == pairing.Pairing.Progression.Value);
const float line_width = 2;
var path = new Path
{
BypassAutoSizeAxes = Axes.Both,
PathWidth = line_width,
};
paths.Add(path);
Vector2 getCenteredVector(Vector2 top, Vector2 bottom) => new Vector2(top.X, top.Y + (bottom.Y - top.Y) / 2);
2018-09-17 22:47:01 +08:00
const float padding = 10;
2018-09-16 04:35:51 +08:00
2018-09-17 22:47:01 +08:00
var q1 = pairing.ScreenSpaceDrawQuad;
var q2 = progression.ScreenSpaceDrawQuad;
2018-09-16 04:35:51 +08:00
2018-09-17 22:47:01 +08:00
bool progressionToRight = q2.TopLeft.X > q1.TopLeft.X;
2018-09-16 04:35:51 +08:00
2018-09-17 22:47:01 +08:00
if (!progressionToRight)
2018-09-16 04:35:51 +08:00
{
2018-09-17 22:47:01 +08:00
var temp = q2;
q2 = q1;
q1 = temp;
2018-09-16 04:35:51 +08:00
}
2018-09-17 22:47:01 +08:00
var c1 = getCenteredVector(q1.TopRight, q1.BottomRight) + new Vector2(padding, 0);
var c2 = getCenteredVector(q2.TopLeft, q2.BottomLeft) - new Vector2(padding, 0);
var p1 = c1;
var p2 = p1 + new Vector2(padding, 0);
if (p2.X > c2.X)
{
c2 = getCenteredVector(q2.TopRight, q2.BottomRight) + new Vector2(padding, 0);
p2.X = c2.X + padding;
}
var p3 = new Vector2(p2.X, c2.Y);
var p4 = new Vector2(c2.X, p3.Y);
path.Positions = new[] { p1, p2, p3, p4 }.Select(p => path.ToLocalSpace(p)).ToList();
2018-09-16 04:35:51 +08:00
}
}
}
public void JoinRequest(MatchPairing pairing)
{
AddInternal(new JoinRequestHandler(pairing, handleProgression));
}
private bool handleProgression(JoinRequestHandler handler, InputState state)
{
var found = pairingsContainer.FirstOrDefault(d => d.ReceiveMouseInputAt(state.Mouse.NativeState.Position));
if (found != null)
{
handler.Source.Progression.Value = found.Pairing;
return true;
}
return false;
}
private class JoinRequestHandler : CompositeDrawable
{
public readonly MatchPairing Source;
private readonly Func<JoinRequestHandler, InputState, bool> onClick;
public JoinRequestHandler(MatchPairing source, Func<JoinRequestHandler, InputState, bool> onClick)
{
Source = source;
this.onClick = onClick;
RelativeSizeAxes = Axes.Both;
}
protected override bool OnClick(InputState state)
{
if (onClick(this, state))
Expire();
return true;
}
}
}
}