Tidy up judgement line logic (and fix it displaying at the wrong place)

This commit is contained in:
Dean Herbert 2019-08-30 16:40:39 +09:00
parent 5f3e638499
commit a73d672c2f
2 changed files with 63 additions and 58 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
@ -8,42 +9,46 @@ using osu.Game.Rulesets.Judgements;
using osuTK.Graphics; using osuTK.Graphics;
using osuTK; using osuTK;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using System.Collections.Generic;
using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Colour;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using System.Linq;
namespace osu.Game.Screens.Play.HitErrorDisplay namespace osu.Game.Screens.Play.HitErrorDisplay
{ {
public class BarHitErrorMeter : HitErrorMeter public class BarHitErrorMeter : HitErrorMeter
{ {
/// <summary> private readonly bool rightAligned;
/// The amount of <see cref="JudgementResult"/> which will be stored to calculate arrow position.
/// </summary>
private const int stored_judgements_amount = 5;
private const int judgement_fade_duration = 10000; private const int judgement_fade_duration = 10000;
private const int arrow_move_duration = 500;
private const int arrow_move_duration = 400;
private const int judgement_line_width = 8; private const int judgement_line_width = 8;
private const int bar_height = 200; private const int bar_height = 200;
private const int bar_width = 3; private const int bar_width = 3;
private const int spacing = 3; private const int spacing = 3;
private readonly SpriteIcon arrow; private readonly SpriteIcon arrow;
private readonly FillFlowContainer<Box> bar; private readonly FillFlowContainer<Box> bar;
private readonly Container judgementsContainer; private readonly Container judgementsContainer;
private readonly List<double> judgementOffsets = new List<double>();
private readonly double maxHitWindows; private readonly double maxHitWindow;
public BarHitErrorMeter(HitWindows hitWindows, bool rightAligned = false) public BarHitErrorMeter(HitWindows hitWindows, bool rightAligned = false)
: base(hitWindows) : base(hitWindows)
{ {
maxHitWindows = HitWindows.Meh == 0 ? HitWindows.Good : HitWindows.Meh; this.rightAligned = rightAligned;
maxHitWindow = Math.Max(Math.Max(HitWindows.Meh, HitWindows.Ok), HitWindows.Good);
AutoSizeAxes = Axes.Both; AutoSizeAxes = Axes.Both;
AddInternal(new FillFlowContainer AddInternal(new FillFlowContainer
{ {
AutoSizeAxes = Axes.X, AutoSizeAxes = Axes.X,
@ -75,9 +80,10 @@ namespace osu.Game.Screens.Play.HitErrorDisplay
RelativeSizeAxes = Axes.Y, RelativeSizeAxes = Axes.Y,
Child = arrow = new SpriteIcon Child = arrow = new SpriteIcon
{ {
Anchor = Anchor.Centre, Anchor = Anchor.TopCentre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
RelativePositionAxes = Axes.Y, RelativePositionAxes = Axes.Y,
Y = 0.5f,
Icon = rightAligned ? FontAwesome.Solid.ChevronRight : FontAwesome.Solid.ChevronLeft, Icon = rightAligned ? FontAwesome.Solid.ChevronRight : FontAwesome.Solid.ChevronLeft,
Size = new Vector2(8), Size = new Vector2(8),
} }
@ -93,24 +99,20 @@ namespace osu.Game.Screens.Play.HitErrorDisplay
{ {
bar.AddRange(new[] bar.AddRange(new[]
{ {
createColoredPiece(ColourInfo.GradientVertical(colours.Yellow.Opacity(0), colours.Yellow), createColoredPiece(ColourInfo.GradientVertical(colours.Yellow.Opacity(0), colours.Yellow), (maxHitWindow - HitWindows.Good) / (maxHitWindow * 2)),
(maxHitWindows - HitWindows.Good) / (maxHitWindows * 2)), createColoredPiece(colours.Green, (HitWindows.Good - HitWindows.Great) / (maxHitWindow * 2)),
createColoredPiece(colours.Green, (HitWindows.Good - HitWindows.Great) / (maxHitWindows * 2)), createColoredPiece(colours.BlueLight, HitWindows.Great / maxHitWindow),
createColoredPiece(colours.BlueLight, HitWindows.Great / maxHitWindows), createColoredPiece(colours.Green, (HitWindows.Good - HitWindows.Great) / (maxHitWindow * 2)),
createColoredPiece(colours.Green, (HitWindows.Good - HitWindows.Great) / (maxHitWindows * 2)), createColoredPiece(ColourInfo.GradientVertical(colours.Yellow, colours.Yellow.Opacity(0)), (maxHitWindow - HitWindows.Good) / (maxHitWindow * 2))
createColoredPiece(ColourInfo.GradientVertical(colours.Yellow, colours.Yellow.Opacity(0)),
(maxHitWindows - HitWindows.Good) / (maxHitWindows * 2))
}); });
} }
else else
{ {
bar.AddRange(new[] bar.AddRange(new[]
{ {
createColoredPiece(ColourInfo.GradientVertical(colours.Green.Opacity(0), colours.Green), createColoredPiece(ColourInfo.GradientVertical(colours.Green.Opacity(0), colours.Green), (HitWindows.Good - HitWindows.Great) / (maxHitWindow * 2)),
(HitWindows.Good - HitWindows.Great) / (maxHitWindows * 2)), createColoredPiece(colours.BlueLight, HitWindows.Great / maxHitWindow),
createColoredPiece(colours.BlueLight, HitWindows.Great / maxHitWindows), createColoredPiece(ColourInfo.GradientVertical(colours.Green, colours.Green.Opacity(0)), (HitWindows.Good - HitWindows.Great) / (maxHitWindow * 2)),
createColoredPiece(ColourInfo.GradientVertical(colours.Green, colours.Green.Opacity(0)),
(HitWindows.Good - HitWindows.Great) / (maxHitWindows * 2)),
}); });
} }
} }
@ -122,51 +124,54 @@ namespace osu.Game.Screens.Play.HitErrorDisplay
Height = (float)height Height = (float)height
}; };
public override void OnNewJudgement(JudgementResult newJudgement) private double floatingAverage;
public override void OnNewJudgement(JudgementResult judgement)
{ {
if (!newJudgement.IsHit) if (!judgement.IsHit)
return; return;
var judgementLine = CreateJudgementLine(newJudgement); judgementsContainer.Add(new JudgementLine
{
Y = getRelativeJudgementPosition(judgement.TimeOffset),
Anchor = rightAligned ? Anchor.TopLeft : Anchor.TopRight,
Origin = rightAligned ? Anchor.TopLeft : Anchor.TopRight,
});
judgementsContainer.Add(judgementLine); arrow.MoveToY(getRelativeJudgementPosition(floatingAverage = floatingAverage * 0.9 + judgement.TimeOffset * 0.1)
, arrow_move_duration, Easing.Out);
judgementLine.FadeOut(judgement_fade_duration, Easing.OutQuint).Expire();
arrow.MoveToY(calculateArrowPosition(newJudgement), arrow_move_duration, Easing.OutQuint);
} }
protected virtual Container CreateJudgementLine(JudgementResult judgement) => new CircularContainer private float getRelativeJudgementPosition(double value) => (float)((value / maxHitWindow) + 1) / 2;
public class JudgementLine : CompositeDrawable
{
public JudgementLine()
{
RelativeSizeAxes = Axes.X;
RelativePositionAxes = Axes.Y;
Height = 2;
InternalChild = new CircularContainer
{ {
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Masking = true, Masking = true,
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.Both,
Height = 2,
RelativePositionAxes = Axes.Y,
Y = getRelativeJudgementPosition(judgement.TimeOffset),
Child = new Box Child = new Box
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Colour = Color4.White, Colour = Color4.White,
} }
}; };
}
private float getRelativeJudgementPosition(double value) => (float)(value / maxHitWindows); protected override void LoadComplete()
private float calculateArrowPosition(JudgementResult newJudgement)
{ {
judgementOffsets.Add(newJudgement.TimeOffset); base.LoadComplete();
if (judgementOffsets.Count < stored_judgements_amount) Width = 0;
return getRelativeJudgementPosition(judgementOffsets.Average()); this.ResizeWidthTo(1, 150, Easing.OutElasticHalf);
this.FadeTo(0.8f, 150).Then().FadeOut(judgement_fade_duration, Easing.OutQuint).Expire();
double sum = 0; }
for (int i = judgementOffsets.Count - stored_judgements_amount; i < judgementOffsets.Count; i++)
sum += judgementOffsets[i];
return getRelativeJudgementPosition(sum / stored_judgements_amount);
} }
} }
} }

View File

@ -16,6 +16,6 @@ namespace osu.Game.Screens.Play.HitErrorDisplay
HitWindows = hitWindows; HitWindows = hitWindows;
} }
public abstract void OnNewJudgement(JudgementResult newJudgement); public abstract void OnNewJudgement(JudgementResult judgement);
} }
} }