#if UNITY_2019_4_OR_NEWER using com.zibra.liquid.Foundation.Editor; using JetBrains.Annotations; using UnityEngine; using UnityEngine.UIElements; namespace com.zibra.liquid.Foundation.UIElements { /// /// The LoadingSpinner control. let's you place buttons group with the labels or images /// internal sealed class LoadingSpinner : VisualElement { [UsedImplicitly] internal new class UxmlFactory : UxmlFactory { } internal new class UxmlTraits : BindableElement.UxmlTraits {} bool m_IsActive; int m_RotationAngle; readonly IVisualElementScheduledItem m_ScheduledUpdate; const long k_RotationUpdateInterval = 1L; const int k_RotationAngleDelta = 10; /// /// Loading Spinner control Uss class name /// public const string UssClassName = "zibraai-loading-spinner"; /// /// Creates LoadingSpinner control /// public LoadingSpinner() { AddToClassList(UssClassName); UIToolkitEditorUtility.ApplyStyleForInternalControl(this, nameof(LoadingSpinner)); m_IsActive = false; // add child elements to set up centered spinner rotation var innerElement = new VisualElement(); innerElement.AddToClassList("image"); Add(innerElement); m_ScheduledUpdate = schedule.Execute(UpdateProgress).Every(k_RotationUpdateInterval); m_ScheduledUpdate.Pause(); RegisterCallback(OnAttachToPanelEventHandler, TrickleDown.TrickleDown); RegisterCallback(OnDetachFromPanelEventHandler, TrickleDown.TrickleDown); } void OnAttachToPanelEventHandler(AttachToPanelEvent e) { Activate(); } void OnDetachFromPanelEventHandler(DetachFromPanelEvent e) { Deactivate(); } void UpdateProgress() { transform.rotation = Quaternion.Euler(0, 0, m_RotationAngle); m_RotationAngle += k_RotationAngleDelta; if (m_RotationAngle > 360) m_RotationAngle -= 360; } void Activate() { if (m_IsActive) return; m_RotationAngle = 0; m_ScheduledUpdate.Resume(); m_IsActive = true; } void Deactivate() { if (!m_IsActive) return; m_ScheduledUpdate.Pause(); m_IsActive = false; } } } #endif