diff --git a/Assets/Editor/TempoFinderButtonEditor.cs b/Assets/Editor/TempoFinderButtonEditor.cs new file mode 100644 index 00000000..5bf20960 --- /dev/null +++ b/Assets/Editor/TempoFinderButtonEditor.cs @@ -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(); + } + } +} diff --git a/Assets/Editor/TempoFinderButtonEditor.cs.meta b/Assets/Editor/TempoFinderButtonEditor.cs.meta new file mode 100644 index 00000000..29dd9197 --- /dev/null +++ b/Assets/Editor/TempoFinderButtonEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 733c524d1df74784e96f0c2731c167c0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Editor.unity b/Assets/Scenes/Editor.unity index af131a99..064a71dc 100644 --- a/Assets/Scenes/Editor.unity +++ b/Assets/Scenes/Editor.unity @@ -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 diff --git a/Assets/Scripts/LevelEditor/BPMText.cs b/Assets/Scripts/LevelEditor/BPMText.cs index 472078f3..01e19776 100644 --- a/Assets/Scripts/LevelEditor/BPMText.cs +++ b/Assets/Scripts/LevelEditor/BPMText.cs @@ -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 pressTimes = new List(); + 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 = "---"; } diff --git a/Assets/Scripts/LevelEditor/TempoFinder.cs b/Assets/Scripts/LevelEditor/TempoFinder.cs index 050b75c0..9fe4f09b 100644 --- a/Assets/Scripts/LevelEditor/TempoFinder.cs +++ b/Assets/Scripts/LevelEditor/TempoFinder.cs @@ -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().ResetText(); + bpmText.ResetText(); } else { tempoFinder.SetActive(true); } @@ -35,7 +35,7 @@ namespace RhythmHeavenMania.Editor if(pressed) { pressed = false; - BPMText.GetComponent().ChangeText(timePressed); + bpmText.ChangeText(timePressed); timePressed = 0; } } diff --git a/Assets/Scripts/LevelEditor/TempoFinderButton.cs b/Assets/Scripts/LevelEditor/TempoFinderButton.cs new file mode 100644 index 00000000..de2b3ef7 --- /dev/null +++ b/Assets/Scripts/LevelEditor/TempoFinderButton.cs @@ -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(); + } + } + } +} diff --git a/Assets/Scripts/LevelEditor/TempoFinderButton.cs.meta b/Assets/Scripts/LevelEditor/TempoFinderButton.cs.meta new file mode 100644 index 00000000..8e609385 --- /dev/null +++ b/Assets/Scripts/LevelEditor/TempoFinderButton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9557e460670800e458d7bb141135de55 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: