2023-11-24 22:59:34 +00:00
|
|
|
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
|
|
|
|
2023-11-24 22:59:34 +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
|
|
|
|
2023-11-24 22:59:34 +00:00
|
|
|
private List<double> pressTimes = new();
|
2022-02-25 04:30:02 +00:00
|
|
|
|
2023-11-24 22:59:34 +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);
|
|
|
|
|
2023-11-24 22:59:34 +00:00
|
|
|
double averageTime = pressTimes.GetRange(1, pressTimes.Count - 1).Average();
|
2022-02-25 04:30:02 +00:00
|
|
|
|
2023-11-24 22:59:34 +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
|
|
|
}
|
2023-11-24 22:59:34 +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 = "---";
|
|
|
|
}
|
2023-11-24 22:59:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|