HeavenStudio/Assets/Scripts/LevelEditor/BPMText.cs

61 lines
1.6 KiB
C#
Raw Normal View History

using System;
2022-02-25 01:02:05 +00:00
using System.Collections.Generic;
using UnityEngine;
using TMPro;
2022-02-25 04:30:02 +00:00
using System.Linq;
2022-02-25 01:02:05 +00:00
namespace HeavenStudio.Editor
2022-02-25 01:02:05 +00:00
{
public class BPMText : MonoBehaviour
{
2022-02-25 04:30:02 +00:00
public const int maxPressTimes = 50;
2022-02-25 01:02:05 +00:00
[SerializeField] private TMP_Text BPM;
[SerializeField] private TMP_Text BPMRounded;
2022-02-25 04:30:02 +00:00
private List<double> pressTimes = new();
2022-02-25 04:30:02 +00:00
public void ChangeText(double timePressed)
2022-02-25 01:02:05 +00:00
{
2022-02-25 04:30:02 +00:00
pressTimes.Add(timePressed);
// First press isn't good to work with.
if (pressTimes.Count < 2) return;
// Limit the number of press times stored.
if (pressTimes.Count > maxPressTimes)
pressTimes.RemoveAt(0);
double averageTime = pressTimes.GetRange(1, pressTimes.Count - 1).Average();
2022-02-25 04:30:02 +00:00
double thisBPM = 60 / averageTime;
BPM.text = $"{thisBPM:0.000}";
BPMRounded.text = $"{(int)Math.Round(thisBPM)}";
2022-02-25 01:02:05 +00:00
}
2022-02-25 01:02:05 +00:00
public void ResetText()
{
2022-02-25 04:30:02 +00:00
pressTimes.Clear();
2022-02-25 01:02:05 +00:00
BPM.text = "---";
BPMRounded.text = "---";
}
public void ClearSamples()
{
if (pressTimes.Count < 2) return;
if (pressTimes.Count > maxPressTimes)
pressTimes.RemoveAt(0);
double averageTime = pressTimes.GetRange(1, pressTimes.Count - 1).Average();
double thisBPM = 60 / averageTime;
BPM.text = $"<color=\"yellow\">{thisBPM:0.000}";
BPMRounded.text = $"<color=\"yellow\">{(int)Math.Round(thisBPM)}";
pressTimes.Clear();
}
2022-02-25 01:02:05 +00:00
}
}