Add score display

This commit is contained in:
Dean Herbert
2018-11-15 21:28:42 +09:00
parent 54b87e9c93
commit 46e163ec5e
10 changed files with 277 additions and 38 deletions

View File

@ -0,0 +1,230 @@
// 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;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input.Events;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Tournament.Components;
using osu.Game.Tournament.Screens.Ladder.Components;
using osu.Game.Tournament.Screens.Showcase;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
namespace osu.Game.Tournament.Screens.Gameplay.Components
{
public class MatchHeader : Container
{
[BackgroundDependencyLoader]
private void load(LadderInfo ladder, TextureStore textures)
{
RelativeSizeAxes = Axes.X;
Height = 100;
Children = new Drawable[]
{
new TournamentLogo(),
new RoundDisplay
{
Y = 10,
Anchor = Anchor.BottomCentre,
Origin = Anchor.TopCentre,
},
new TeamScoreDisplay(TeamColour.Red)
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
},
new TeamScoreDisplay(TeamColour.Blue)
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
};
}
private class TeamScoreDisplay : CompositeDrawable
{
private readonly TeamColour teamColour;
private readonly Color4 red = new Color4(129, 68, 65, 255);
private readonly Color4 blue = new Color4(41, 91, 97, 255);
private readonly Bindable<MatchPairing> currentMatch = new Bindable<MatchPairing>();
private readonly Bindable<TournamentTeam> currentTeam = new Bindable<TournamentTeam>();
private readonly Bindable<int?> currentTeamScore = new Bindable<int?>();
public TeamScoreDisplay(TeamColour teamColour)
{
this.teamColour = teamColour;
RelativeSizeAxes = Axes.Y;
Width = 300;
}
[BackgroundDependencyLoader]
private void load(LadderInfo ladder)
{
currentMatch.BindValueChanged(matchChanged);
currentMatch.BindTo(ladder.CurrentMatch);
}
private void matchChanged(MatchPairing match)
{
currentTeamScore.UnbindBindings();
currentTeamScore.BindTo(teamColour == TeamColour.Red ? match.Team1Score : match.Team2Score);
currentTeam.UnbindBindings();
currentTeam.BindTo(teamColour == TeamColour.Red ? match.Team1 : match.Team2);
// team may change to same team, which means score is not in a good state.
// thus we handle this manually.
teamChanged(currentTeam.Value);
}
protected override bool OnMouseDown(MouseDownEvent e)
{
switch (e.Button)
{
case MouseButton.Left:
if (currentTeamScore.Value < currentMatch.Value.PointsToWin)
currentTeamScore.Value++;
return true;
case MouseButton.Right:
if (currentTeamScore.Value > 0)
currentTeamScore.Value--;
return true;
}
return base.OnMouseDown(e);
}
private void teamChanged(TournamentTeam team)
{
var colour = teamColour == TeamColour.Red ? red : blue;
var flip = teamColour != TeamColour.Red;
InternalChildren = new Drawable[]
{
new TeamDisplay(team, colour, flip),
new TeamScore(currentTeamScore, flip, currentMatch.Value.PointsToWin)
{
Colour = colour
}
};
}
}
private class TeamScore : CompositeDrawable
{
private readonly Bindable<int?> currentTeamScore = new Bindable<int?>();
private readonly StarCounter counter;
public TeamScore(Bindable<int?> score, bool flip, int count)
{
var anchor = flip ? Anchor.CentreRight : Anchor.CentreLeft;
Anchor = anchor;
Origin = anchor;
InternalChild = counter = new StarCounter(count)
{
Anchor = anchor,
X = (flip ? -1 : 1) * 90,
Y = 5,
Scale = flip ? new Vector2(-1, 1) : Vector2.One,
};
currentTeamScore.BindValueChanged(scoreChanged);
currentTeamScore.BindTo(score);
}
private void scoreChanged(int? score) => counter.CountStars = score ?? 0;
}
private class TeamDisplay : DrawableTournamentTeam
{
public TeamDisplay(TournamentTeam team, Color4 colour, bool flip)
: base(team)
{
RelativeSizeAxes = Axes.Both;
var anchor = flip ? Anchor.CentreRight : Anchor.CentreLeft;
Anchor = Origin = anchor;
Flag.Anchor = Flag.Origin = anchor;
Flag.RelativeSizeAxes = Axes.None;
Flag.Size = new Vector2(60, 40);
Flag.Margin = new MarginPadding(20);
InternalChild = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
Flag,
new OsuSpriteText
{
Text = team?.FullName.ToUpper() ?? "???",
X = (flip ? -1 : 1) * 90,
Y = -10,
TextSize = 20,
Colour = colour,
Font = "Aquatico-Regular",
Origin = anchor,
Anchor = anchor,
},
}
};
}
}
private class RoundDisplay : CompositeDrawable
{
private readonly Bindable<MatchPairing> currentMatch = new Bindable<MatchPairing>();
public RoundDisplay()
{
CornerRadius = 10;
Masking = true;
Width = 200;
Height = 20;
}
[BackgroundDependencyLoader]
private void load(LadderInfo ladder)
{
currentMatch.BindValueChanged(matchChanged);
currentMatch.BindTo(ladder.CurrentMatch);
}
private void matchChanged(MatchPairing match)
{
InternalChildren = new Drawable[]
{
new Box
{
Colour = new Color4(95, 41, 60, 255),
RelativeSizeAxes = Axes.Both,
},
new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Colour = Color4.White,
Text = match.Grouping.Value?.Name.Value ?? "Unknown Grouping",
Font = "Aquatico-Regular",
TextSize = 18,
},
};
}
}
}
}

View File

@ -0,0 +1,144 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterface;
using osu.Game.Tournament.IPC;
using osu.Game.Tournament.Screens.Ladder.Components;
using OpenTK.Graphics;
namespace osu.Game.Tournament.Screens.Gameplay.Components
{
public class MatchScoreDisplay : CompositeDrawable
{
private readonly Color4 red = new Color4(186, 0, 18, 255);
private readonly Color4 blue = new Color4(17, 136, 170, 255);
private const float bar_height = 20;
private readonly Bindable<MatchPairing> currentMatch = new Bindable<MatchPairing>();
private readonly BindableInt score1 = new BindableInt();
private readonly BindableInt score2 = new BindableInt();
private readonly MatchScoreCounter score1Text;
private readonly MatchScoreCounter score2Text;
private readonly Circle score1Bar;
private readonly Circle score2Bar;
public MatchScoreDisplay()
{
RelativeSizeAxes = Axes.X;
InternalChildren = new Drawable[]
{
score1Bar = new Circle
{
Name = "top bar red",
RelativeSizeAxes = Axes.X,
Height = bar_height,
Width = 0,
Colour = red,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopRight
},
score1Text = new MatchScoreCounter
{
Colour = red,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
},
score2Bar = new Circle
{
Name = "top bar blue",
RelativeSizeAxes = Axes.X,
Height = bar_height,
Width = 0,
Colour = blue,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopLeft
},
score2Text = new MatchScoreCounter
{
Colour = blue,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
},
};
}
[BackgroundDependencyLoader]
private void load(LadderInfo ladder, MatchIPCInfo ipc)
{
currentMatch.BindTo(ladder.CurrentMatch);
score1.BindValueChanged(_ => updateScores());
score1.BindTo(ipc.Score1);
score2.BindValueChanged(_ => updateScores());
score2.BindTo(ipc.Score2);
}
private void updateScores()
{
score1Text.Current.Value = score1.Value;
score2Text.Current.Value = score2.Value;
var winningText = score1.Value > score2.Value ? score1Text : score2Text;
var losingText = score1.Value <= score2.Value ? score1Text : score2Text;
winningText.Winning = true;
losingText.Winning = false;
var winningBar = score1.Value > score2.Value ? score1Bar : score2Bar;
var losingBar = score1.Value <= score2.Value ? score1Bar : score2Bar;
var diff = Math.Max(score1.Value, score2.Value) - Math.Min(score1.Value, score2.Value);
losingBar.ResizeWidthTo(0, 400, Easing.OutQuint);
winningBar.ResizeWidthTo((float)Math.Pow(diff / 1000000f, 0.5), 400, Easing.OutQuint);
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
score1Text.X = -Math.Max(score1Text.DrawWidth / 2, score1Bar.DrawWidth);
score2Text.X = Math.Max(score2Text.DrawWidth / 2, score2Bar.DrawWidth);
}
private class MatchScoreCounter : ScoreCounter
{
public MatchScoreCounter()
{
Margin = new MarginPadding { Top = bar_height + 5, Horizontal = 10 };
Winning = false;
DisplayedCountSpriteText.FixedWidth = false;
}
public bool Winning
{
set
{
if (value)
{
DisplayedCountSpriteText.Font = "Aquatico-Regular";
DisplayedCountSpriteText.TextSize = 60;
}
else
{
DisplayedCountSpriteText.Font = "Aquatico-Light";
DisplayedCountSpriteText.TextSize = 40;
}
}
}
}
}
}