1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 08:47:51 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/Components/DrawableMatchPairing.cs

203 lines
6.7 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-09-22 04:52:25 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
2018-08-26 00:30:33 +08:00
using OpenTK;
2018-09-22 04:52:25 +08:00
using OpenTK.Graphics;
using OpenTK.Input;
using SixLabors.Primitives;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class DrawableMatchPairing : CompositeDrawable
{
2018-08-26 17:37:46 +08:00
public readonly MatchPairing Pairing;
private readonly FillFlowContainer<DrawableMatchTeam> flow;
2018-09-22 04:52:25 +08:00
private readonly Drawable selectionBox;
private Bindable<MatchPairing> globalSelection;
[Resolved(CanBeNull = true)]
private LadderEditorInfo editorInfo { get; set; } = null;
public DrawableMatchPairing(MatchPairing pairing)
{
Pairing = pairing;
Position = new Vector2(pairing.Position.X, pairing.Position.Y);
AutoSizeAxes = Axes.Both;
Margin = new MarginPadding(5);
2018-09-22 04:52:25 +08:00
InternalChildren = new[]
{
2018-09-22 04:52:25 +08:00
selectionBox = new Container
{
CornerRadius = 5,
Masking = true,
Scale = new Vector2(1.05f),
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0,
Colour = Color4.YellowGreen,
Child = new Box { RelativeSizeAxes = Axes.Both }
},
flow = new FillFlowContainer<DrawableMatchTeam>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(2)
2018-09-16 04:35:51 +08:00
}
};
pairing.Team1.BindValueChanged(_ => updateTeams());
pairing.Team2.BindValueChanged(_ => updateTeams());
pairing.Team1Score.BindValueChanged(_ => updateWinConditions());
pairing.Team2Score.BindValueChanged(_ => updateWinConditions());
pairing.Grouping.BindValueChanged(_ => updateWinConditions());
pairing.Completed.BindValueChanged(_ => updateProgression());
2018-09-16 04:35:51 +08:00
pairing.Progression.BindValueChanged(_ => updateProgression());
2018-09-25 01:31:48 +08:00
pairing.LosersProgression.BindValueChanged(_ => updateProgression());
2018-09-25 02:55:24 +08:00
pairing.Losers.BindValueChanged(_ => updateTeams());
updateTeams();
}
2018-09-22 04:52:25 +08:00
private bool selected;
public bool Selected
{
get => selected;
set
{
if (value == selected) return;
selected = value;
if (selected)
{
selectionBox.Show();
editorInfo.Selected.Value = Pairing;
}
else
selectionBox.Hide();
}
}
2018-08-26 17:37:46 +08:00
private void updateProgression()
{
2018-09-16 04:35:51 +08:00
var progression = Pairing.Progression?.Value;
2018-08-26 17:37:46 +08:00
2018-09-25 01:31:48 +08:00
if (progression != null)
{
bool progressionAbove = progression.ID < Pairing.ID;
2018-08-26 17:37:46 +08:00
2018-09-25 02:14:30 +08:00
var destinationForWinner = progressionAbove || progression.Team1.Value != null && progression.Team1.Value != Pairing.Team1.Value && progression.Team1.Value != Pairing.Team2.Value ? progression.Team2 : progression.Team1;
2018-09-25 01:31:48 +08:00
destinationForWinner.Value = Pairing.Winner;
}
2018-08-26 17:37:46 +08:00
2018-09-25 01:31:48 +08:00
if ((progression = Pairing.LosersProgression?.Value) != null)
{
bool progressionAbove = progression.ID < Pairing.ID;
2018-09-25 02:14:30 +08:00
var destinationForLoser = progressionAbove || progression.Team1.Value != null && progression.Team1.Value != Pairing.Team1.Value && progression.Team1.Value != Pairing.Team2.Value ? progression.Team2 : progression.Team1;
2018-09-25 01:31:48 +08:00
destinationForLoser.Value = Pairing.Loser;
}
2018-08-26 17:37:46 +08:00
}
private void updateWinConditions()
{
2018-09-25 09:27:54 +08:00
if (Pairing.Grouping.Value == null) return;
var instaWinAmount = Pairing.Grouping.Value.BestOf / 2;
2018-09-24 01:16:59 +08:00
Pairing.Completed.Value = Pairing.Grouping.Value.BestOf > 0 && (Pairing.Team1Score + Pairing.Team2Score >= Pairing.Grouping.Value.BestOf || Pairing.Team1Score > instaWinAmount || Pairing.Team2Score > instaWinAmount);
}
2018-08-26 17:37:46 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
updateTeams();
2018-09-22 04:52:25 +08:00
if (editorInfo != null)
{
globalSelection = editorInfo.Selected.GetBoundCopy();
globalSelection.BindValueChanged(s =>
{
if (s != Pairing) Selected = false;
});
}
2018-08-26 17:37:46 +08:00
}
private void updateTeams()
{
2018-08-26 17:37:46 +08:00
if (LoadState != LoadState.Loaded)
return;
// todo: teams may need to be bindable for transitions at a later point.
if (Pairing.Team1.Value == null || Pairing.Team2.Value == null)
Pairing.CancelMatchStart();
flow.Children = new[]
{
2018-09-25 02:55:24 +08:00
new DrawableMatchTeam(Pairing.Team1, Pairing, Pairing.Losers),
new DrawableMatchTeam(Pairing.Team2, Pairing, Pairing.Losers)
};
2018-08-26 17:37:46 +08:00
2018-08-27 16:08:00 +08:00
SchedulerAfterChildren.Add(() => Scheduler.Add(updateProgression));
updateWinConditions();
}
protected override bool OnMouseDown(MouseDownEvent e) => e.Button == MouseButton.Left && editorInfo.EditingEnabled;
protected override bool OnDragStart(DragStartEvent e) => editorInfo.EditingEnabled;
protected override bool OnKeyDown(KeyDownEvent e)
2018-09-22 04:52:25 +08:00
{
if (Selected && editorInfo.EditingEnabled && e.Key == Key.Delete)
2018-09-22 04:52:25 +08:00
{
Remove();
return true;
}
return base.OnKeyDown(e);
2018-09-22 04:52:25 +08:00
}
protected override bool OnClick(ClickEvent e)
2018-09-22 04:52:25 +08:00
{
if (!editorInfo.EditingEnabled)
return false;
Selected = true;
return true;
}
protected override bool OnDrag(DragEvent e)
{
if (base.OnDrag(e)) return true;
2018-09-22 04:52:25 +08:00
Selected = true;
this.MoveToOffset(e.Delta);
var pos = Position;
Pairing.Position = new Point((int)pos.X, (int)pos.Y);
return true;
}
2018-09-21 18:58:47 +08:00
public void Remove()
{
2018-09-22 04:52:25 +08:00
Selected = false;
2018-09-21 18:58:47 +08:00
Pairing.Progression.Value = null;
2018-09-25 01:31:48 +08:00
Pairing.LosersProgression.Value = null;
2018-09-22 04:52:25 +08:00
2018-09-21 18:58:47 +08:00
Expire();
}
}
}