1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 09:07:25 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/HitErrorMeters/BarHitErrorMeter.cs

269 lines
9.1 KiB
C#
Raw Normal View History

2019-08-20 01:44:06 +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.Linq;
2019-08-30 17:46:42 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
2019-08-20 01:44:06 +08:00
using osu.Framework.Graphics;
2019-08-30 17:46:42 +08:00
using osu.Framework.Graphics.Colour;
2019-08-20 01:44:06 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
2019-08-30 17:46:42 +08:00
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Scoring;
2019-08-30 17:46:42 +08:00
using osuTK;
using osuTK.Graphics;
2019-08-20 01:44:06 +08:00
2019-08-30 17:46:42 +08:00
namespace osu.Game.Screens.Play.HUD.HitErrorMeters
2019-08-20 01:44:06 +08:00
{
2019-08-30 14:32:47 +08:00
public class BarHitErrorMeter : HitErrorMeter
2019-08-20 01:44:06 +08:00
{
private readonly Anchor alignment;
2019-08-20 14:03:31 +08:00
private const int arrow_move_duration = 400;
private const int judgement_line_width = 6;
2019-08-20 01:44:06 +08:00
private const int bar_height = 200;
private const int bar_width = 2;
private const int spacing = 2;
2019-08-20 01:44:06 +08:00
2019-08-30 17:50:38 +08:00
private const float chevron_size = 8;
private SpriteIcon arrow;
private Container colourBarsEarly;
private Container colourBarsLate;
private Container judgementsContainer;
private double maxHitWindow;
2019-08-20 01:44:06 +08:00
2019-08-30 14:51:36 +08:00
public BarHitErrorMeter(HitWindows hitWindows, bool rightAligned = false)
: base(hitWindows)
2019-08-20 01:44:06 +08:00
{
alignment = rightAligned ? Anchor.x0 : Anchor.x2;
2019-08-20 01:44:06 +08:00
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
InternalChild = new FillFlowContainer
2019-08-20 01:44:06 +08:00
{
AutoSizeAxes = Axes.X,
Height = bar_height,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(spacing, 0),
Margin = new MarginPadding(2),
2019-08-20 01:44:06 +08:00
Children = new Drawable[]
{
judgementsContainer = new Container
{
Anchor = Anchor.y1 | alignment,
Origin = Anchor.y1 | alignment,
2019-08-20 01:44:06 +08:00
Width = judgement_line_width,
RelativeSizeAxes = Axes.Y,
},
colourBars = new Container
2019-08-20 01:44:06 +08:00
{
Width = bar_width,
RelativeSizeAxes = Axes.Y,
Anchor = Anchor.y1 | alignment,
Origin = Anchor.y1 | alignment,
Children = new Drawable[]
{
colourBarsEarly = new Container
{
Anchor = Anchor.y1 | alignment,
Origin = alignment,
RelativeSizeAxes = Axes.Both,
Height = 0.5f,
Scale = new Vector2(1, -1),
},
colourBarsLate = new Container
{
Anchor = Anchor.y1 | alignment,
Origin = alignment,
RelativeSizeAxes = Axes.Both,
Height = 0.5f,
},
}
2019-08-20 01:44:06 +08:00
},
new Container
{
Anchor = Anchor.y1 | alignment,
Origin = Anchor.y1 | alignment,
2019-08-30 17:50:38 +08:00
Width = chevron_size,
2019-08-20 01:44:06 +08:00
RelativeSizeAxes = Axes.Y,
Child = arrow = new SpriteIcon
{
Anchor = Anchor.TopCentre,
2019-08-20 01:44:06 +08:00
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Y,
Y = 0.5f,
Icon = alignment == Anchor.x2 ? FontAwesome.Solid.ChevronRight : FontAwesome.Solid.ChevronLeft,
2019-08-30 17:50:38 +08:00
Size = new Vector2(chevron_size),
2019-08-20 01:44:06 +08:00
}
},
}
};
2019-08-20 01:44:06 +08:00
createColourBars(colours);
2019-08-20 01:44:06 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
colourBars.Height = 0;
colourBars.ResizeHeightTo(1, 800, Easing.OutQuint);
2019-08-30 17:50:38 +08:00
arrow.Alpha = 0;
arrow.Delay(200).FadeInFromZero(600);
}
private void createColourBars(OsuColour colours)
2019-08-20 13:00:09 +08:00
{
var windows = HitWindows.GetAllAvailableHalfWindows().ToArray();
maxHitWindow = windows.First().length;
for (var i = 0; i < windows.Length; i++)
{
var (result, length) = windows[i];
colourBarsEarly.Add(createColourBar(result, (float)(length / maxHitWindow), i == 0));
colourBarsLate.Add(createColourBar(result, (float)(length / maxHitWindow), i == 0));
}
// a little nub to mark the centre point.
var centre = createColourBar(windows.Last().result, 0.01f);
centre.Anchor = centre.Origin = Anchor.y1 | alignment;
centre.Width = 1.5f;
colourBars.Add(centre);
Color4 getColour(HitResult result)
{
switch (result)
{
case HitResult.Meh:
return colours.Yellow;
case HitResult.Ok:
return colours.Green;
case HitResult.Good:
return colours.GreenLight;
case HitResult.Great:
return colours.Blue;
default:
return colours.BlueLight;
}
}
Drawable createColourBar(HitResult result, float height, bool first = false)
{
var colour = getColour(result);
if (first)
{
// the first bar needs gradient rendering.
const float gradient_start = 0.8f;
return new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = getColour(result),
Height = height * gradient_start
},
new Box
{
RelativeSizeAxes = Axes.Both,
RelativePositionAxes = Axes.Both,
Colour = ColourInfo.GradientVertical(colour, colour.Opacity(0)),
Y = gradient_start,
Height = height * (1 - gradient_start)
},
}
};
}
return new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colour,
Height = height
};
}
}
2019-08-20 13:00:09 +08:00
private double floatingAverage;
private Container colourBars;
public override void OnNewJudgement(JudgementResult judgement)
2019-08-20 01:44:06 +08:00
{
if (!judgement.IsHit)
2019-08-20 01:44:06 +08:00
return;
judgementsContainer.Add(new JudgementLine
{
Y = getRelativeJudgementPosition(judgement.TimeOffset),
Anchor = alignment == Anchor.x2 ? Anchor.x0 : Anchor.x2,
Origin = alignment == Anchor.x2 ? Anchor.x0 : Anchor.x2,
});
2019-08-20 01:44:06 +08:00
arrow.MoveToY(
getRelativeJudgementPosition(floatingAverage = floatingAverage * 0.9 + judgement.TimeOffset * 0.1)
, arrow_move_duration, Easing.Out);
2019-08-20 01:44:06 +08:00
}
private float getRelativeJudgementPosition(double value) => (float)((value / maxHitWindow) + 1) / 2;
2019-08-20 01:44:06 +08:00
public class JudgementLine : CompositeDrawable
2019-08-20 01:44:06 +08:00
{
2019-08-30 17:50:38 +08:00
private const int judgement_fade_duration = 10000;
public JudgementLine()
{
RelativeSizeAxes = Axes.X;
RelativePositionAxes = Axes.Y;
2019-08-30 17:50:38 +08:00
Height = 3;
InternalChild = new CircularContainer
{
Masking = true,
RelativeSizeAxes = Axes.Both,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
}
};
}
2019-08-20 01:44:06 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
2019-08-20 01:44:06 +08:00
Width = 0;
2019-08-30 17:50:38 +08:00
this.ResizeWidthTo(1, 200, Easing.OutElasticHalf);
this.FadeTo(0.8f, 150).Then().FadeOut(judgement_fade_duration, Easing.OutQuint).Expire();
}
2019-08-20 01:44:06 +08:00
}
}
}