A game about forced loneliness, made by TACStudios
at master 5.3 kB view raw
1using UnityEditor; 2using UnityEngine.UIElements; 3 4using Codice.Client.Common.Authentication; 5using PlasticGui; 6 7namespace Unity.PlasticSCM.Editor.UI.UIElements 8{ 9 class ProgressControlsForDialogs : 10 VisualElement, 11 IProgressControls, 12 IAuthenticationProgressControls 13 { 14 internal class Data 15 { 16 internal bool IsWaitingAsyncResult; 17 internal float ProgressPercent; 18 internal string ProgressMessage; 19 20 internal MessageType StatusType; 21 internal string StatusMessage; 22 23 internal void CopyInto(Data other) 24 { 25 other.IsWaitingAsyncResult = IsWaitingAsyncResult; 26 other.ProgressPercent = ProgressPercent; 27 other.ProgressMessage = ProgressMessage; 28 other.StatusType = StatusType; 29 other.StatusMessage = StatusMessage; 30 } 31 } 32 33 internal Data ProgressData { get { return mData; } } 34 35 internal void ForcedUpdateProgress() 36 { 37 if (mData.IsWaitingAsyncResult) 38 { 39 mUndefinedProgress.Show(); 40 mPercentageLabel.Show(); 41 mLoadingSpinner.Start(); 42 EditorApplication.update += UpdatePercent; 43 } 44 else 45 { 46 mUndefinedProgress.Collapse(); 47 mPercentageLabel.Collapse(); 48 mLoadingSpinner.Stop(); 49 EditorApplication.update -= UpdatePercent; 50 } 51 52 mStatusLabel.text = mData.StatusMessage; 53 mProgressLabel.text = mData.ProgressMessage; 54 } 55 56 internal void UpdatePercent() 57 { 58 if (mData.ProgressPercent >= 0) 59 mPercentageLabel.text = string.Format("({0}%)", (int)(mData.ProgressPercent * 100)); 60 else 61 mPercentageLabel.text = ""; 62 } 63 64 internal ProgressControlsForDialogs( 65 VisualElement[] actionControls) 66 { 67 mActionControls = actionControls; 68 69 InitializeLayoutAndStyles(); 70 71 BuildComponents(); 72 } 73 74 internal void EnableActionControls(bool enable) 75 { 76 if (mActionControls != null) 77 foreach (var control in mActionControls) 78 if (control != null) 79 control.SetEnabled(enable); 80 } 81 82 void IProgressControls.HideProgress() 83 { 84 InternalHideProgress(); 85 } 86 87 void IAuthenticationProgressControls.HideProgress() 88 { 89 InternalHideProgress(); 90 } 91 92 void InternalHideProgress() 93 { 94 EnableActionControls(true); 95 96 mData.IsWaitingAsyncResult = false; 97 mData.ProgressMessage = string.Empty; 98 ForcedUpdateProgress(); 99 } 100 101 void IProgressControls.ShowProgress(string message) 102 { 103 InternalShowProgress(message); 104 } 105 106 void IAuthenticationProgressControls.ShowProgress(string message) 107 { 108 InternalShowProgress(message); 109 } 110 111 void InternalShowProgress(string message) 112 { 113 EnableActionControls(false); 114 115 CleanStatusMessage(mData); 116 117 mData.IsWaitingAsyncResult = true; 118 mData.ProgressPercent = -1f; 119 mData.ProgressMessage = message; 120 ForcedUpdateProgress(); 121 } 122 123 void IProgressControls.ShowError(string message) 124 { 125 mData.StatusMessage = message; 126 mData.StatusType = MessageType.Error; 127 ForcedUpdateProgress(); 128 } 129 130 void IProgressControls.ShowNotification(string message) 131 { 132 mData.StatusMessage = message; 133 mData.StatusType = MessageType.Info; 134 ForcedUpdateProgress(); 135 } 136 137 void IProgressControls.ShowSuccess(string message) 138 { 139 mData.StatusMessage = message; 140 mData.StatusType = MessageType.Info; 141 ForcedUpdateProgress(); 142 } 143 144 void IProgressControls.ShowWarning(string message) 145 { 146 mData.StatusMessage = message; 147 mData.StatusType = MessageType.Warning; 148 ForcedUpdateProgress(); 149 } 150 151 void BuildComponents() 152 { 153 mUndefinedProgress = this.Q<VisualElement>("UndefinedProgress"); 154 mProgressLabel = this.Q<Label>("Progress"); 155 mStatusLabel = this.Q<Label>("Status"); 156 mPercentageLabel = this.Q<Label>("Percentage"); 157 158 mLoadingSpinner = new LoadingSpinner(); 159 mUndefinedProgress.Add(mLoadingSpinner); 160 } 161 162 void InitializeLayoutAndStyles() 163 { 164 this.LoadLayout(typeof(ProgressControlsForDialogs).Name); 165 166 this.LoadStyle(typeof(ProgressControlsForDialogs).Name); 167 } 168 169 static void CleanStatusMessage(Data data) 170 { 171 data.StatusMessage = string.Empty; 172 data.StatusType = MessageType.None; 173 } 174 175 Data mData = new Data(); 176 VisualElement mUndefinedProgress; 177 Label mProgressLabel; 178 Label mStatusLabel; 179 Label mPercentageLabel; 180 VisualElement[] mActionControls; 181 182 LoadingSpinner mLoadingSpinner; 183 } 184}