CollabToolbarButton.cs
10.6 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Collaboration;
using UnityEditor.Connect;
using UnityEditor.Web;
using UnityEngine;
namespace UnityEditor
{
internal class CollabToolbarButton : SubToolbar, IDisposable
{
// Must match s_CollabIcon array
enum CollabToolbarState
{
NeedToEnableCollab,
UpToDate,
Conflict,
OperationError,
ServerHasChanges,
FilesToPush,
InProgress,
Disabled,
Offline
}
private class CollabToolbarContent
{
readonly string m_iconName;
readonly string m_toolTip;
readonly CollabToolbarState m_state;
static Dictionary<CollabToolbarContent, GUIContent> m_CollabIcons;
public CollabToolbarState RegisteredForState
{
get { return m_state; }
}
public GUIContent GuiContent
{
get
{
if (m_CollabIcons == null)
{
m_CollabIcons = new Dictionary<CollabToolbarContent, GUIContent>();
}
if (!m_CollabIcons.ContainsKey(this))
{
m_CollabIcons.Add(this, EditorGUIUtility.TrTextContentWithIcon("Collab", m_toolTip, m_iconName));
}
return m_CollabIcons[this];
}
}
public CollabToolbarContent(CollabToolbarState state, string iconName, string toolTip)
{
m_state = state;
m_iconName = iconName;
m_toolTip = toolTip;
}
}
CollabToolbarContent[] m_toolbarContents;
CollabToolbarState m_CollabToolbarState = CollabToolbarState.UpToDate;
const float kCollabButtonWidth = 78.0f;
ButtonWithAnimatedIconRotation m_CollabButton;
string m_DynamicTooltip;
static bool m_ShowCollabTooltip = false;
private GUIContent currentCollabContent
{
get
{
CollabToolbarContent toolbarContent =
m_toolbarContents.FirstOrDefault(c => c.RegisteredForState.Equals(m_CollabToolbarState));
GUIContent content = new GUIContent(toolbarContent == null? m_toolbarContents.First().GuiContent : toolbarContent.GuiContent);
if (!m_ShowCollabTooltip)
{
content.tooltip = null;
}
else if (m_DynamicTooltip != "")
{
content.tooltip = m_DynamicTooltip;
}
if (Collab.instance.AreTestsRunning())
{
content.text = "CTF";
}
return content;
}
}
public CollabToolbarButton()
{
m_toolbarContents = new[]
{
new CollabToolbarContent(CollabToolbarState.NeedToEnableCollab, "CollabNew", " You need to enable collab."),
new CollabToolbarContent(CollabToolbarState.UpToDate, "Collab", " You are up to date."),
new CollabToolbarContent(CollabToolbarState.Conflict, "CollabConflict", " Please fix your conflicts prior to publishing."),
new CollabToolbarContent(CollabToolbarState.OperationError, "CollabError", " Last operation failed. Please retry later."),
new CollabToolbarContent(CollabToolbarState.ServerHasChanges, "CollabPull", " Please update, there are server changes."),
new CollabToolbarContent(CollabToolbarState.FilesToPush, "CollabPush", " You have files to publish."),
new CollabToolbarContent(CollabToolbarState.InProgress, "CollabProgress", " Operation in progress."),
new CollabToolbarContent(CollabToolbarState.Disabled, "CollabNew", " Collab is disabled."),
new CollabToolbarContent(CollabToolbarState.Offline, "CollabNew", " Please check your network connection.")
};
Collab.instance.StateChanged += OnCollabStateChanged;
UnityConnect.instance.StateChanged += OnUnityConnectStateChanged;
UnityConnect.instance.UserStateChanged += OnUnityConnectUserStateChanged;
}
void OnUnityConnectUserStateChanged(UserInfo state)
{
UpdateCollabToolbarState();
}
void OnUnityConnectStateChanged(ConnectInfo state)
{
UpdateCollabToolbarState();
}
public override void OnGUI(Rect rect)
{
DoCollabDropDown(rect);
}
Rect GUIToScreenRect(Rect guiRect)
{
Vector2 screenPoint = GUIUtility.GUIToScreenPoint(new Vector2(guiRect.x, guiRect.y));
guiRect.x = screenPoint.x;
guiRect.y = screenPoint.y;
return guiRect;
}
void ShowPopup(Rect rect)
{
// window should be centered on the button
ReserveRight(kCollabButtonWidth / 2, ref rect);
ReserveBottom(5, ref rect);
// calculate screen rect before saving assets since it might open the AssetSaveDialog window
var screenRect = GUIToScreenRect(rect);
// save all the assets
AssetDatabase.SaveAssets();
if (Collab.ShowToolbarAtPosition != null && Collab.ShowToolbarAtPosition(screenRect))
{
GUIUtility.ExitGUI();
}
}
void DoCollabDropDown(Rect rect)
{
UpdateCollabToolbarState();
GUIStyle collabButtonStyle = "OffsetDropDown";
bool showPopup = Toolbar.requestShowCollabToolbar;
Toolbar.requestShowCollabToolbar = false;
bool enable = !EditorApplication.isPlaying;
using (new EditorGUI.DisabledScope(!enable))
{
bool animate = m_CollabToolbarState == CollabToolbarState.InProgress;
EditorGUIUtility.SetIconSize(new Vector2(12, 12));
if (GetCollabButton().OnGUI(rect, currentCollabContent, animate, collabButtonStyle))
{
showPopup = true;
}
EditorGUIUtility.SetIconSize(Vector2.zero);
}
if (m_CollabToolbarState == CollabToolbarState.Disabled)
return;
if (showPopup)
{
ShowPopup(rect);
}
}
public void OnCollabStateChanged(CollabInfo info)
{
UpdateCollabToolbarState();
}
public void UpdateCollabToolbarState()
{
var currentCollabState = CollabToolbarState.UpToDate;
bool networkAvailable = UnityConnect.instance.connectInfo.online && UnityConnect.instance.connectInfo.loggedIn;
m_DynamicTooltip = "";
if (UnityConnect.instance.isDisableCollabWindow)
{
currentCollabState = CollabToolbarState.Disabled;
}
else if (networkAvailable)
{
Collab collab = Collab.instance;
CollabInfo currentInfo = collab.collabInfo;
UnityErrorInfo errInfo;
bool error = false;
if (collab.GetError((UnityConnect.UnityErrorFilter.ByContext | UnityConnect.UnityErrorFilter.ByChild), out errInfo))
{
error = (errInfo.priority <= (int)UnityConnect.UnityErrorPriority.Error);
m_DynamicTooltip = errInfo.shortMsg;
}
if (!currentInfo.ready)
{
currentCollabState = CollabToolbarState.InProgress;
}
else if (error)
{
currentCollabState = CollabToolbarState.OperationError;
}
else if (currentInfo.inProgress)
{
currentCollabState = CollabToolbarState.InProgress;
}
else
{
bool collabEnable = Collab.instance.IsCollabEnabledForCurrentProject();
if (UnityConnect.instance.projectInfo.projectBound == false || !collabEnable)
{
currentCollabState = CollabToolbarState.NeedToEnableCollab;
}
else if (currentInfo.update)
{
currentCollabState = CollabToolbarState.ServerHasChanges;
}
else if (currentInfo.conflict)
{
currentCollabState = CollabToolbarState.Conflict;
}
else if (currentInfo.publish)
{
currentCollabState = CollabToolbarState.FilesToPush;
}
}
}
else
{
currentCollabState = CollabToolbarState.Offline;
}
if (Collab.IsToolbarVisible != null)
{
if (currentCollabState != m_CollabToolbarState ||
Collab.IsToolbarVisible() == m_ShowCollabTooltip)
{
m_CollabToolbarState = currentCollabState;
m_ShowCollabTooltip = !Collab.IsToolbarVisible();
Toolbar.RepaintToolbar();
}
}
}
void ReserveRight(float width, ref Rect pos)
{
pos.x += width;
}
void ReserveBottom(float height, ref Rect pos)
{
pos.y += height;
}
ButtonWithAnimatedIconRotation GetCollabButton()
{
if (m_CollabButton == null)
{
const int repaintsPerSecond = 20;
const float animSpeed = 500f;
const bool mouseDownButton = true;
m_CollabButton = new ButtonWithAnimatedIconRotation(() => (float)EditorApplication.timeSinceStartup * animSpeed, Toolbar.RepaintToolbar, repaintsPerSecond, mouseDownButton);
}
return m_CollabButton;
}
public void Dispose()
{
Collab.instance.StateChanged -= OnCollabStateChanged;
UnityConnect.instance.StateChanged -= OnUnityConnectStateChanged;
UnityConnect.instance.UserStateChanged -= OnUnityConnectUserStateChanged;
if (m_CollabButton != null)
m_CollabButton.Clear();
}
}
} // namespace