2019-01-24 08:43:03 +00: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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-01-29 19:32:45 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-01-09 04:43:44 +00:00
|
|
|
|
using osu.Framework.Utils;
|
2019-02-12 04:04:46 +00:00
|
|
|
|
using osu.Game.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-01-29 19:32:45 +00:00
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-12-04 11:21:53 +00:00
|
|
|
|
namespace osu.Game.Rulesets.Osu.Skinning.Default
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
public class SpinnerSpmCounter : Container
|
|
|
|
|
{
|
2021-01-29 19:32:45 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private DrawableHitObject drawableSpinner { get; set; }
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
private readonly OsuSpriteText spmText;
|
|
|
|
|
|
|
|
|
|
public SpinnerSpmCounter()
|
|
|
|
|
{
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
spmText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Text = @"0",
|
2019-02-22 10:42:09 +00:00
|
|
|
|
Font = OsuFont.Numeric.With(size: 24)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
|
|
|
|
new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Text = @"SPINS PER MINUTE",
|
2019-02-22 10:42:09 +00:00
|
|
|
|
Font = OsuFont.Numeric.With(size: 12),
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Y = 30
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 19:32:45 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
drawableSpinner.HitObjectApplied += resetState;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
private double spm;
|
|
|
|
|
|
|
|
|
|
public double SpinsPerMinute
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => spm;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
if (value == spm) return;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
spm = value;
|
|
|
|
|
spmText.Text = Math.Truncate(value).ToString(@"#0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct RotationRecord
|
|
|
|
|
{
|
|
|
|
|
public float Rotation;
|
|
|
|
|
public double Time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Queue<RotationRecord> records = new Queue<RotationRecord>();
|
|
|
|
|
private const double spm_count_duration = 595; // not using hundreds to avoid frame rounding issues
|
|
|
|
|
|
|
|
|
|
public void SetRotation(float currentRotation)
|
|
|
|
|
{
|
2019-12-18 00:03:12 +00:00
|
|
|
|
// Never calculate SPM by same time of record to avoid 0 / 0 = NaN or X / 0 = Infinity result.
|
|
|
|
|
if (Precision.AlmostEquals(0, Time.Elapsed))
|
|
|
|
|
return;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
// If we've gone back in time, it's fine to work with a fresh set of records for now
|
|
|
|
|
if (records.Count > 0 && Time.Current < records.Last().Time)
|
|
|
|
|
records.Clear();
|
|
|
|
|
|
|
|
|
|
if (records.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var record = records.Peek();
|
|
|
|
|
while (records.Count > 0 && Time.Current - records.Peek().Time > spm_count_duration)
|
|
|
|
|
record = records.Dequeue();
|
2019-12-18 00:03:12 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
SpinsPerMinute = (currentRotation - record.Rotation) / (Time.Current - record.Time) * 1000 * 60 / 360;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
records.Enqueue(new RotationRecord { Rotation = currentRotation, Time = Time.Current });
|
|
|
|
|
}
|
2021-01-29 19:32:45 +00:00
|
|
|
|
|
|
|
|
|
private void resetState(DrawableHitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
SpinsPerMinute = 0;
|
|
|
|
|
records.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
if (drawableSpinner != null)
|
|
|
|
|
drawableSpinner.HitObjectApplied -= resetState;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|