using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace Lightbug.CharacterControllerPro.Implementation { /// /// This struct contains all the button states, which are updated frame by frame. /// [System.Serializable] public struct BoolAction { /// /// The action current value. /// public bool value; /// /// Returns true if the value transitioned from false to true (e.g. a button press). /// public bool Started { get; private set; } /// /// Returns true if the value transitioned from true to false (e.g. a button release). /// public bool Canceled { get; private set; } /// /// Elapsed time since the last "Started" flag. /// public float StartedElapsedTime { get; private set; } /// /// Elapsed time since the last "Canceled" flag. /// public float CanceledElapsedTime { get; private set; } /// /// The elapsed time since this action was set to true. /// public float ActiveTime { get; private set; } /// /// The elapsed time since this action was set to false. /// public float InactiveTime { get; private set; } /// /// The last "ActiveTime" value registered by this action (on Canceled). /// public float LastActiveTime { get; private set; } /// /// The last "InactiveTime" value registered by this action (on Started). /// public float LastInactiveTime { get; private set; } bool previousValue; bool previousStarted; bool previousCanceled; /// /// Initialize the values. /// public void Initialize() { StartedElapsedTime = Mathf.Infinity; CanceledElapsedTime = Mathf.Infinity; value = false; previousValue = false; previousStarted = false; previousCanceled = false; } /// /// Resets the action. /// public void Reset() { Started = false; Canceled = false; } /// /// Updates the fields based on the current button state. /// public void Update(float dt) { Started |= !previousValue && value; Canceled |= previousValue && !value; StartedElapsedTime += dt; CanceledElapsedTime += dt; if (Started) { StartedElapsedTime = 0f; if (!previousStarted) { LastActiveTime = 0f; LastInactiveTime = InactiveTime; } } if (Canceled) { CanceledElapsedTime = 0f; if (!previousCanceled) { LastActiveTime = ActiveTime; LastInactiveTime = 0f; } } if (value) { ActiveTime += dt; InactiveTime = 0f; } else { ActiveTime = 0f; InactiveTime += dt; } previousValue = value; previousStarted = Started; previousCanceled = Canceled; } } // ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── // EDITOR ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── // ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── #if UNITY_EDITOR [CustomPropertyDrawer(typeof(BoolAction))] public class BoolActionEditor : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); SerializedProperty value = property.FindPropertyRelative("value"); Rect fieldRect = position; fieldRect.height = EditorGUIUtility.singleLineHeight; fieldRect.width = 100; EditorGUI.LabelField(fieldRect, label); fieldRect.x += 110; EditorGUI.PropertyField(fieldRect, value, GUIContent.none); EditorGUI.EndProperty(); } } #endif }