File size: 2,232 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;

namespace Unity.PlasticSCM.Editor.UI.UIElements
{
    internal class LoadingSpinner : VisualElement
    {
        internal LoadingSpinner()
        {
            mStarted = false;

            // add child elements to set up centered spinner rotation
            mSpinner = new VisualElement();
            Add(mSpinner);

            mSpinner.style.backgroundImage = Images.GetImage(Images.Name.Loading);
            mSpinner.style.position = Position.Absolute;
            mSpinner.style.width = 16;
            mSpinner.style.height = 16;
            mSpinner.style.left = -8;
            mSpinner.style.top = -8;

            style.position = Position.Relative;
            style.width = 16;
            style.height = 16;
            style.left = 8;
            style.top = 8;
        }

        internal void Dispose()
        {
            if (mStarted)
                EditorApplication.update -= UpdateProgress;
        }

        internal void Start()
        {
            if (mStarted)
                return;

            mRotation = 0;
            mLastRotationTime = EditorApplication.timeSinceStartup;

            EditorApplication.update += UpdateProgress;

            mStarted = true;
        }

        internal void Stop()
        {
            if (!mStarted)
                return;

            EditorApplication.update -= UpdateProgress;

            mStarted = false;
        }

        void UpdateProgress()
        {
            double currentTime = EditorApplication.timeSinceStartup;
            double deltaTime = currentTime - mLastRotationTime;

#if UNITY_2021_2_OR_NEWER
            mSpinner.transform.rotation = Quaternion.Euler(0, 0, mRotation);
#else
            transform.rotation = Quaternion.Euler(0, 0, mRotation);
#endif

            mRotation += (int)(ROTATION_SPEED * deltaTime);
            mRotation = mRotation % 360;
            if (mRotation < 0) mRotation += 360;

            mLastRotationTime = currentTime;
        }

        int mRotation;
        double mLastRotationTime;
        bool mStarted;
        VisualElement mSpinner;

        const int ROTATION_SPEED = 360; // Euler degrees per second
    }
}