A game about forced loneliness, made by TACStudios
1using UnityEngine;
2using UnityEditor;
3using UnityEngine.UIElements;
4
5namespace Unity.PlasticSCM.Editor.UI.UIElements
6{
7 internal class LoadingSpinner : VisualElement
8 {
9 internal LoadingSpinner()
10 {
11 mStarted = false;
12
13 // add child elements to set up centered spinner rotation
14 mSpinner = new VisualElement();
15 Add(mSpinner);
16
17 mSpinner.style.backgroundImage = Images.GetImage(Images.Name.Loading);
18 mSpinner.style.position = Position.Absolute;
19 mSpinner.style.width = 16;
20 mSpinner.style.height = 16;
21 mSpinner.style.left = -8;
22 mSpinner.style.top = -8;
23
24 style.position = Position.Relative;
25 style.width = 16;
26 style.height = 16;
27 style.left = 8;
28 style.top = 8;
29 }
30
31 internal void Dispose()
32 {
33 if (mStarted)
34 EditorApplication.update -= UpdateProgress;
35 }
36
37 internal void Start()
38 {
39 if (mStarted)
40 return;
41
42 mRotation = 0;
43 mLastRotationTime = EditorApplication.timeSinceStartup;
44
45 EditorApplication.update += UpdateProgress;
46
47 mStarted = true;
48 }
49
50 internal void Stop()
51 {
52 if (!mStarted)
53 return;
54
55 EditorApplication.update -= UpdateProgress;
56
57 mStarted = false;
58 }
59
60 void UpdateProgress()
61 {
62 double currentTime = EditorApplication.timeSinceStartup;
63 double deltaTime = currentTime - mLastRotationTime;
64
65 mSpinner.transform.rotation = Quaternion.Euler(0, 0, mRotation);
66
67 mRotation += (int)(ROTATION_SPEED * deltaTime);
68 mRotation = mRotation % 360;
69 if (mRotation < 0) mRotation += 360;
70
71 mLastRotationTime = currentTime;
72 }
73
74 int mRotation;
75 double mLastRotationTime;
76 bool mStarted;
77 VisualElement mSpinner;
78
79 const int ROTATION_SPEED = 360; // Euler degrees per second
80 }
81}