mirror of
https://github.com/RHeavenStudio/HeavenStudio.git
synced 2025-06-12 12:47:38 +02:00
Editor: Tempo finder tweaks
This commit is contained in:
@ -1,21 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System.Linq;
|
||||
|
||||
namespace RhythmHeavenMania.Editor
|
||||
{
|
||||
public class BPMText : MonoBehaviour
|
||||
{
|
||||
public const int maxPressTimes = 50;
|
||||
|
||||
[SerializeField] private TMP_Text BPM;
|
||||
[SerializeField] private TMP_Text BPMRounded;
|
||||
|
||||
private List<float> pressTimes = new List<float>();
|
||||
|
||||
public void ChangeText(float timePressed)
|
||||
{
|
||||
float thisBPM = 60 / timePressed; // BPM = 60/t
|
||||
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);
|
||||
|
||||
var averageTime = pressTimes.GetRange(1, pressTimes.Count - 1).Average();
|
||||
|
||||
float thisBPM = 60 / averageTime; // BPM = 60/t
|
||||
BPM.text = $"{thisBPM}";
|
||||
BPMRounded.text = $"{Mathf.RoundToInt(thisBPM)}";
|
||||
}
|
||||
public void ResetText()
|
||||
{
|
||||
pressTimes.Clear();
|
||||
|
||||
BPM.text = "---";
|
||||
BPMRounded.text = "---";
|
||||
}
|
||||
|
Reference in New Issue
Block a user