osu/osu.Game.Tournament/Screens/Editors/LadderEditorScreen.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
5.3 KiB
C#
Raw Normal View History

2019-03-04 04:24:19 +00: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.
2022-06-17 07:37:17 +00:00
#nullable disable
using System;
using System.Drawing;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Graphics.UserInterface;
2019-06-18 05:51:48 +00:00
using osu.Game.Tournament.Models;
using osu.Game.Tournament.Screens.Ladder;
using osu.Game.Tournament.Screens.Ladder.Components;
using osuTK;
using osuTK.Graphics;
2019-06-18 05:51:48 +00:00
namespace osu.Game.Tournament.Screens.Editors
{
[Cached]
public class LadderEditorScreen : LadderScreen, IHasContextMenu
{
[Cached]
private LadderEditorInfo editorInfo = new LadderEditorInfo();
private WarningBox rightClickMessage;
protected override bool DrawLoserPaths => true;
[BackgroundDependencyLoader]
private void load()
{
2019-06-13 08:04:57 +00:00
Content.Add(new LadderEditorSettings
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(5)
});
AddInternal(rightClickMessage = new WarningBox("Right click to place and link matches"));
2022-06-24 12:25:23 +00:00
LadderInfo.Matches.CollectionChanged += (_, _) => updateMessage();
updateMessage();
}
private void updateMessage()
{
rightClickMessage.Alpha = LadderInfo.Matches.Count > 0 ? 0 : 1;
}
2019-06-18 05:57:05 +00:00
public void BeginJoin(TournamentMatch match, bool losers)
{
2019-06-18 05:57:05 +00:00
ScrollContent.Add(new JoinVisualiser(MatchesContainer, match, losers, UpdateLayout));
}
public MenuItem[] ContextMenuItems
{
get
{
if (editorInfo == null)
2019-11-28 13:41:29 +00:00
return Array.Empty<MenuItem>();
return new MenuItem[]
{
new OsuMenuItem("Create new match", MenuItemType.Highlighted, () =>
{
2019-06-18 05:57:05 +00:00
var pos = MatchesContainer.ToLocalSpace(GetContainingInputManager().CurrentState.Mouse.Position);
LadderInfo.Matches.Add(new TournamentMatch { Position = { Value = new Point((int)pos.X, (int)pos.Y) } });
}),
2019-02-06 09:36:15 +00:00
new OsuMenuItem("Reset teams", MenuItemType.Destructive, () =>
{
2019-06-18 05:57:05 +00:00
foreach (var p in MatchesContainer)
p.Match.Reset();
2019-02-06 09:36:15 +00:00
})
};
}
}
2019-06-18 05:57:05 +00:00
public void Remove(TournamentMatch match)
{
2019-06-18 05:57:05 +00:00
MatchesContainer.FirstOrDefault(p => p.Match == match)?.Remove();
}
2019-06-13 08:04:57 +00:00
private class JoinVisualiser : CompositeDrawable
{
2019-06-18 05:57:05 +00:00
private readonly Container<DrawableTournamentMatch> matchesContainer;
public readonly TournamentMatch Source;
private readonly bool losers;
private readonly Action complete;
private ProgressionPath path;
2019-06-18 05:57:05 +00:00
public JoinVisualiser(Container<DrawableTournamentMatch> matchesContainer, TournamentMatch source, bool losers, Action complete)
{
2019-06-18 05:57:05 +00:00
this.matchesContainer = matchesContainer;
RelativeSizeAxes = Axes.Both;
Source = source;
this.losers = losers;
this.complete = complete;
if (losers)
Source.LosersProgression.Value = null;
else
Source.Progression.Value = null;
}
2019-06-18 05:57:05 +00:00
private DrawableTournamentMatch findTarget(InputState state)
{
2019-06-18 05:57:05 +00:00
return matchesContainer.FirstOrDefault(d => d.ReceivePositionalInputAt(state.Mouse.Position));
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos)
{
return true;
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
var found = findTarget(e.CurrentState);
if (found == path?.Destination)
return false;
path?.Expire();
path = null;
if (found == null)
return false;
2019-06-18 05:57:05 +00:00
AddInternal(path = new ProgressionPath(matchesContainer.First(c => c.Match == Source), found)
{
Colour = Color4.Yellow,
});
return base.OnMouseMove(e);
}
protected override bool OnClick(ClickEvent e)
{
var found = findTarget(e.CurrentState);
if (found != null)
{
2019-06-18 05:57:05 +00:00
if (found.Match != Source)
{
if (losers)
2019-06-18 05:57:05 +00:00
Source.LosersProgression.Value = found.Match;
else
2019-06-18 05:57:05 +00:00
Source.Progression.Value = found.Match;
}
complete?.Invoke();
Expire();
return true;
}
return false;
}
}
}
}