1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 06:37:50 +08:00
osu-lazer/osu.Game.Tournament/Screens/Ladder/Components/DrawableMatchTeam.cs

208 lines
7.0 KiB
C#
Raw Normal View History

2019-03-04 12:24:19 +08: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.
using System;
using osu.Framework.Allocation;
2019-03-02 12:40:43 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2018-09-21 18:58:47 +08:00
using osu.Game.Graphics.UserInterface;
2018-08-26 00:24:19 +08:00
using osu.Game.Tournament.Components;
2019-06-18 13:51:48 +08:00
using osu.Game.Tournament.Models;
using osu.Game.Tournament.Screens.Editors;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
namespace osu.Game.Tournament.Screens.Ladder.Components
{
public class DrawableMatchTeam : DrawableTournamentTeam, IHasContextMenu
{
2019-06-18 13:57:05 +08:00
private readonly TournamentMatch match;
2018-09-25 02:55:24 +08:00
private readonly bool losers;
private OsuSpriteText scoreText;
private Box background;
private readonly Bindable<int?> score = new Bindable<int?>();
private readonly BindableBool completed = new BindableBool();
private Color4 colourWinner;
private Color4 colourNormal;
private readonly Func<bool> isWinner;
private LadderEditorScreen ladderEditor;
2018-11-11 09:13:17 +08:00
[Resolved]
private LadderInfo ladderInfo { get; set; }
private void setCurrent()
{
//todo: tournamentgamebase?
if (ladderInfo.CurrentMatch.Value != null)
ladderInfo.CurrentMatch.Value.Current.Value = false;
2019-06-18 13:57:05 +08:00
ladderInfo.CurrentMatch.Value = match;
2018-11-11 09:13:17 +08:00
ladderInfo.CurrentMatch.Value.Current.Value = true;
}
2018-09-22 04:52:25 +08:00
[Resolved(CanBeNull = true)]
2018-11-06 13:49:20 +08:00
private LadderEditorInfo editorInfo { get; set; }
2018-09-22 04:52:25 +08:00
2019-06-18 13:57:05 +08:00
public DrawableMatchTeam(TournamentTeam team, TournamentMatch match, bool losers)
: base(team)
{
2019-06-18 13:57:05 +08:00
this.match = match;
2018-09-25 02:55:24 +08:00
this.losers = losers;
Size = new Vector2(150, 40);
Masking = true;
CornerRadius = 5;
Flag.Scale = new Vector2(0.9f);
Flag.Anchor = Flag.Origin = Anchor.CentreLeft;
AcronymText.Anchor = AcronymText.Origin = Anchor.CentreLeft;
AcronymText.Padding = new MarginPadding { Left = 50 };
2019-03-04 11:06:41 +08:00
AcronymText.Font = OsuFont.GetFont(size: 24);
2019-06-18 13:57:05 +08:00
if (match != null)
{
2019-06-18 13:57:05 +08:00
isWinner = () => match.Winner == Team;
2019-06-18 13:57:05 +08:00
completed.BindTo(match.Completed);
2019-03-02 12:40:43 +08:00
if (team != null)
2019-06-18 13:57:05 +08:00
score.BindTo(team == match.Team1.Value ? match.Team1Score : match.Team2Score);
}
}
[BackgroundDependencyLoader(true)]
private void load(OsuColour colours, LadderEditorScreen ladderEditor)
{
this.ladderEditor = ladderEditor;
2018-09-25 02:55:24 +08:00
colourWinner = losers ? colours.YellowDarker : colours.BlueDarker;
colourNormal = OsuColour.Gray(0.2f);
InternalChildren = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
new Container
{
Padding = new MarginPadding(5),
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
AcronymText,
Flag,
new Container
{
Masking = true,
CornerRadius = 5,
Width = 0.3f,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
Colour = OsuColour.Gray(0.1f),
Alpha = 0.8f,
RelativeSizeAxes = Axes.Both,
},
scoreText = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-03-04 11:06:41 +08:00
Font = OsuFont.GetFont(size: 20),
}
}
}
}
}
};
completed.BindValueChanged(_ => updateWinStyle());
score.BindValueChanged(val =>
{
2019-03-02 12:40:43 +08:00
scoreText.Text = val.NewValue?.ToString() ?? string.Empty;
updateWinStyle();
}, true);
}
2018-11-07 00:20:32 +08:00
protected override bool OnClick(ClickEvent e)
{
if (Team == null || editorInfo != null) return false;
2019-06-18 13:57:05 +08:00
if (!match.Current.Value)
2018-11-07 00:20:32 +08:00
{
2018-11-11 09:13:17 +08:00
setCurrent();
2018-11-07 00:20:32 +08:00
return true;
}
if (e.Button == MouseButton.Left)
{
if (score.Value == null)
{
2019-06-18 13:57:05 +08:00
match.StartMatch();
}
2019-06-18 13:57:05 +08:00
else if (!match.Completed.Value)
score.Value++;
}
else
{
2019-06-18 13:57:05 +08:00
if (match.Progression.Value?.Completed.Value == true)
2018-09-25 01:31:48 +08:00
// don't allow changing scores if the match has a progression. can cause large data loss
return false;
2019-06-18 13:57:05 +08:00
if (match.Completed.Value && match.Winner != Team)
2018-09-25 02:14:30 +08:00
// don't allow changing scores from the non-winner
return false;
if (score.Value > 0)
score.Value--;
else
2019-06-18 13:57:05 +08:00
match.CancelMatchStart();
}
return false;
}
private void updateWinStyle()
{
2019-03-02 12:40:43 +08:00
bool winner = completed.Value && isWinner?.Invoke() == true;
background.FadeColour(winner ? colourWinner : colourNormal, winner ? 500 : 0, Easing.OutQuint);
2019-05-15 12:10:58 +08:00
scoreText.Font = AcronymText.Font = OsuFont.GetFont(weight: winner ? FontWeight.Bold : FontWeight.Regular);
}
2018-09-22 04:52:25 +08:00
public MenuItem[] ContextMenuItems
{
2018-09-22 04:52:25 +08:00
get
{
if (editorInfo == null)
2018-09-22 04:52:25 +08:00
return new MenuItem[0];
return new MenuItem[]
{
2018-11-11 09:13:17 +08:00
new OsuMenuItem("Set as current", MenuItemType.Standard, setCurrent),
2019-06-18 13:57:05 +08:00
new OsuMenuItem("Join with", MenuItemType.Standard, () => ladderEditor.BeginJoin(match, false)),
new OsuMenuItem("Join with (loser)", MenuItemType.Standard, () => ladderEditor.BeginJoin(match, true)),
new OsuMenuItem("Remove", MenuItemType.Destructive, () => ladderEditor.Remove(match)),
2018-09-22 04:52:25 +08:00
};
}
}
}
}