Editor: Tempo finder tweaks

This commit is contained in:
Jenny Crowe 2022-02-24 21:30:02 -07:00
parent 3b7d328c05
commit 68bb30ae2a
7 changed files with 90 additions and 20 deletions

View file

@ -0,0 +1,20 @@
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using RhythmHeavenMania;
namespace RhythmHeavenMania.Editor
{
[CustomEditor(typeof(TempoFinderButton))]
public class TempoFinderButtonEditor : ButtonEditor
{
public override void OnInspectorGUI()
{
TempoFinderButton targetButton = (TempoFinderButton)target;
targetButton.tempoFinder = (TempoFinder)EditorGUILayout.ObjectField("Tempo Finder", targetButton.tempoFinder, typeof(TempoFinder), true);
base.OnInspectorGUI();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 733c524d1df74784e96f0c2731c167c0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -636,7 +636,7 @@ MonoBehaviour:
m_GameObject: {fileID: 44197994}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Script: {fileID: 11500000, guid: 9557e460670800e458d7bb141135de55, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
@ -670,19 +670,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 44197997}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1265865542}
m_TargetAssemblyTypeName: RhythmHeavenMania.Editor.TempoFinder, Assembly-CSharp
m_MethodName: TapBPM
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_Calls: []
tempoFinder: {fileID: 1265865542}
--- !u!114 &44197997
MonoBehaviour:
m_ObjectHideFlags: 0
@ -6951,7 +6940,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 704039020}
m_Direction: 0
m_Value: 0
m_Size: 0.033
m_Size: 0.033000004
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
@ -13019,7 +13008,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
tempoFinder: {fileID: 1265865540}
BPMText: {fileID: 565525424}
bpmText: {fileID: 565525426}
--- !u!1 &1271528576
GameObject:
m_ObjectHideFlags: 0

View file

@ -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 = "---";
}

View file

@ -9,7 +9,7 @@ namespace RhythmHeavenMania.Editor
[SerializeField] private GameObject tempoFinder;
private bool pressed;
private float timePressed;
[SerializeField] private GameObject BPMText;
[SerializeField] private BPMText bpmText;
private void Awake()
{
pressed = false;
@ -20,7 +20,7 @@ namespace RhythmHeavenMania.Editor
if(tempoFinder.activeSelf) {
tempoFinder.SetActive(false);
timePressed = 0;
BPMText.GetComponent<BPMText>().ResetText();
bpmText.ResetText();
} else {
tempoFinder.SetActive(true);
}
@ -35,7 +35,7 @@ namespace RhythmHeavenMania.Editor
if(pressed)
{
pressed = false;
BPMText.GetComponent<BPMText>().ChangeText(timePressed);
bpmText.ChangeText(timePressed);
timePressed = 0;
}
}

View file

@ -0,0 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace RhythmHeavenMania.Editor
{
public class TempoFinderButton : Button, IPointerDownHandler
{
public TempoFinder tempoFinder;
public override void OnPointerDown(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
tempoFinder.TapBPM();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9557e460670800e458d7bb141135de55
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: