osu/osu.Game/Screens/Play/HUD/HitErrorMeters/ColourHitErrorMeter.cs

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

200 lines
6.3 KiB
C#
Raw Normal View History

2022-09-05 02:49:48 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-12-21 10:41:50 +00:00
// See the LICENCE file in the repository root for full licence text.
2020-01-24 02:52:32 +00:00
using System.Collections.Generic;
2019-12-21 13:08:28 +00:00
using System.Linq;
using osu.Framework.Allocation;
2022-09-02 21:07:30 +00:00
using osu.Framework.Bindables;
2019-12-21 10:41:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
2019-12-21 10:41:50 +00:00
using osu.Framework.Graphics.Shapes;
2022-09-02 21:07:30 +00:00
using osu.Game.Configuration;
2019-12-21 10:41:50 +00:00
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Scoring;
2019-12-21 10:41:50 +00:00
using osuTK;
2019-12-21 11:52:53 +00:00
using osuTK.Graphics;
2019-12-21 10:41:50 +00:00
namespace osu.Game.Screens.Play.HUD.HitErrorMeters
{
[Cached]
2019-12-21 10:41:50 +00:00
public partial class ColourHitErrorMeter : HitErrorMeter
{
private const int animation_duration = 200;
private const int drawable_judgement_size = 8;
2019-12-21 11:30:41 +00:00
[SettingSource("Judgement count", "The number of displayed judgements")]
public BindableNumber<int> JudgementCount { get; } = new BindableNumber<int>(20)
2022-09-02 21:07:30 +00:00
{
MinValue = 1,
MaxValue = 50,
2022-09-02 21:07:30 +00:00
};
[SettingSource("Judgement spacing", "The space between each displayed judgement")]
public BindableNumber<float> JudgementSpacing { get; } = new BindableNumber<float>(2)
2022-09-02 21:07:30 +00:00
{
MinValue = 0,
2022-09-02 23:27:22 +00:00
MaxValue = 10,
2022-09-02 21:07:30 +00:00
};
[SettingSource("Judgement shape", "The shape of each displayed judgement")]
public Bindable<ShapeStyle> JudgementShape { get; } = new Bindable<ShapeStyle>();
private readonly JudgementFlow judgementsFlow;
public ColourHitErrorMeter()
2019-12-21 10:41:50 +00:00
{
2019-12-21 11:30:41 +00:00
AutoSizeAxes = Axes.Both;
InternalChild = judgementsFlow = new JudgementFlow
{
JudgementShape = { BindTarget = JudgementShape },
JudgementSpacing = { BindTarget = JudgementSpacing },
JudgementCount = { BindTarget = JudgementCount }
};
2019-12-21 13:08:28 +00:00
}
protected override void OnNewJudgement(JudgementResult judgement)
{
if (!judgement.Type.IsScorable() || judgement.Type.IsBonus())
return;
judgementsFlow.Push(GetColourForHitResult(judgement.Type));
}
2019-12-21 13:08:28 +00:00
2021-09-20 14:22:36 +00:00
public override void Clear() => judgementsFlow.Clear();
private partial class JudgementFlow : FillFlowContainer<HitErrorShape>
2019-12-21 13:08:28 +00:00
{
2020-01-24 02:52:32 +00:00
public override IEnumerable<Drawable> FlowingChildren => base.FlowingChildren.Reverse();
public readonly Bindable<ShapeStyle> JudgementShape = new Bindable<ShapeStyle>();
public readonly Bindable<float> JudgementSpacing = new Bindable<float>();
public readonly Bindable<int> JudgementCount = new Bindable<int>();
2019-12-22 00:06:57 +00:00
2022-09-02 23:27:22 +00:00
public JudgementFlow()
2019-12-21 13:08:28 +00:00
{
2022-09-02 23:27:22 +00:00
Width = drawable_judgement_size;
2019-12-22 00:06:57 +00:00
Direction = FillDirection.Vertical;
LayoutDuration = animation_duration;
LayoutEasing = Easing.OutQuint;
2019-12-21 13:08:28 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
JudgementCount.BindValueChanged(_ =>
{
removeExtraJudgements();
updateMetrics();
});
JudgementSpacing.BindValueChanged(_ => updateMetrics(), true);
}
private readonly DrawablePool<HitErrorShape> judgementLinePool = new DrawablePool<HitErrorShape>(50);
public void Push(Color4 colour)
2019-12-21 13:08:28 +00:00
{
judgementLinePool.Get(shape =>
{
shape.Colour = colour;
Add(shape);
2019-12-21 13:08:28 +00:00
removeExtraJudgements();
});
}
private void removeExtraJudgements()
{
var remainingChildren = Children.Where(c => !c.IsRemoved);
while (remainingChildren.Count() > JudgementCount.Value)
remainingChildren.First().Remove();
}
private void updateMetrics()
{
Height = JudgementCount.Value * (drawable_judgement_size + JudgementSpacing.Value) - JudgementSpacing.Value;
Spacing = new Vector2(0, JudgementSpacing.Value);
2019-12-21 13:08:28 +00:00
}
2019-12-21 11:30:41 +00:00
}
public partial class HitErrorShape : PoolableDrawable
2019-12-21 11:30:41 +00:00
{
2019-12-22 00:30:17 +00:00
public bool IsRemoved { get; private set; }
public readonly Bindable<ShapeStyle> Shape = new Bindable<ShapeStyle>();
[Resolved]
private ColourHitErrorMeter hitErrorMeter { get; set; } = null!;
private Container content = null!;
public HitErrorShape()
{
Size = new Vector2(drawable_judgement_size);
}
protected override void LoadComplete()
{
base.LoadComplete();
InternalChild = content = new Container
{
RelativeSizeAxes = Axes.Both,
};
Shape.BindTo(hitErrorMeter.JudgementShape);
Shape.BindValueChanged(shape =>
{
switch (shape.NewValue)
{
case ShapeStyle.Circle:
content.Child = new Circle { RelativeSizeAxes = Axes.Both };
break;
case ShapeStyle.Square:
content.Child = new Box { RelativeSizeAxes = Axes.Both };
break;
}
}, true);
}
protected override void PrepareForUse()
{
base.PrepareForUse();
this.FadeInFromZero(animation_duration, Easing.OutQuint)
// On pool re-use, start flow animation from (0,0).
.MoveTo(Vector2.Zero);
content.MoveToY(-DrawSize.Y)
.MoveToY(0, animation_duration, Easing.OutQuint);
}
2019-12-22 00:30:17 +00:00
protected override void FreeAfterUse()
{
base.FreeAfterUse();
IsRemoved = false;
}
2019-12-22 00:30:17 +00:00
public void Remove()
{
IsRemoved = true;
this.FadeOut(animation_duration, Easing.OutQuint)
.Expire();
2019-12-22 00:30:17 +00:00
}
2019-12-21 10:41:50 +00:00
}
public enum ShapeStyle
{
Circle,
Square
}
2019-12-21 10:41:50 +00:00
}
}