You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

  1. using UnityEngine;
  2. using UnityEngine.Experimental.UIElements;
  3. namespace UnityEditor.PackageManager.UI
  4. {
  5. #if !UNITY_2018_3_OR_NEWER
  6. internal class LoadingSpinnerFactory : UxmlFactory<LoadingSpinner>
  7. {
  8. protected override LoadingSpinner DoCreate(IUxmlAttributes bag, CreationContext cc)
  9. {
  10. return new LoadingSpinner();
  11. }
  12. }
  13. #endif
  14. internal class LoadingSpinner : VisualElement
  15. {
  16. #if UNITY_2018_3_OR_NEWER
  17. internal new class UxmlFactory : UxmlFactory<LoadingSpinner, UxmlTraits>
  18. {
  19. }
  20. // This works around an issue with UXML instantiation
  21. // See https://fogbugz.unity3d.com/f/cases/1046459/
  22. internal new class UxmlTraits : VisualElement.UxmlTraits
  23. {
  24. public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
  25. {
  26. base.Init(ve, bag, cc);
  27. UIUtils.SetElementDisplay(ve, false);
  28. }
  29. }
  30. #endif
  31. public bool InvertColor { get; set; }
  32. public bool Started { get; private set; }
  33. private int rotation;
  34. public LoadingSpinner()
  35. {
  36. InvertColor = false;
  37. Started = false;
  38. UIUtils.SetElementDisplay(this, false);
  39. }
  40. private void UpdateProgress()
  41. {
  42. if (parent == null)
  43. return;
  44. parent.transform.rotation = Quaternion.Euler(0, 0, rotation);
  45. rotation += 3;
  46. if (rotation > 360)
  47. rotation -= 360;
  48. }
  49. public void Start()
  50. {
  51. if (Started)
  52. return;
  53. // Weird hack to make sure loading spinner doesn't generate an error every frame.
  54. // Cannot put in constructor as it give really strange result.
  55. if (parent != null && parent.parent != null)
  56. parent.parent.clippingOptions = ClippingOptions.ClipAndCacheContents;
  57. rotation = 0;
  58. EditorApplication.update += UpdateProgress;
  59. Started = true;
  60. UIUtils.SetElementDisplay(this, true);
  61. }
  62. public void Stop()
  63. {
  64. if (!Started)
  65. return;
  66. EditorApplication.update -= UpdateProgress;
  67. Started = false;
  68. UIUtils.SetElementDisplay(this, false);
  69. }
  70. }
  71. }