A game about forced loneliness, made by TACStudios
1using System;
2
3using UnityEditor;
4using UnityEngine;
5
6using Codice.Client.Common;
7using Codice.Client.Common.WebApi;
8using Codice.CM.Common;
9using PlasticGui;
10using PlasticGui.WorkspaceWindow.Home.Repositories;
11using Unity.PlasticSCM.Editor.UI;
12using Unity.PlasticSCM.Editor.UI.Progress;
13using Unity.PlasticSCM.Editor.Views.CreateWorkspace.Dialogs;
14
15namespace Unity.PlasticSCM.Editor.Views.CreateWorkspace
16{
17 internal static class DrawCreateWorkspace
18 {
19 internal static void ForState(
20 Action<string> selectRepositoryAction,
21 Action<RepositoryCreationData> createRepositoryAction,
22 Action<CreateWorkspaceViewState> createWorkspaceAction,
23 EditorWindow parentWindow,
24 IPlasticWebRestApi plasticWebRestApi,
25 string defaultServer,
26 ref CreateWorkspaceViewState state)
27 {
28 DoTitle();
29
30 GUILayout.Space(15);
31
32 DoFieldsArea(
33 selectRepositoryAction,
34 createRepositoryAction,
35 parentWindow,
36 plasticWebRestApi,
37 defaultServer,
38 ref state);
39
40 GUILayout.Space(10);
41
42 DoRadioButtonsArea(ref state);
43
44 GUILayout.Space(3);
45
46 DoHelpLabel();
47
48 GUILayout.Space(10);
49
50 DoCreateWorkspaceButton(
51 createWorkspaceAction,
52 ref state);
53
54 GUILayout.Space(5);
55
56 DoNotificationArea(state.ProgressData);
57 }
58
59 static void DoTitle()
60 {
61 GUILayout.Label(
62 PlasticLocalization.GetString(
63 PlasticLocalization.Name.NewWorkspace),
64 UnityStyles.Dialog.MessageTitle);
65
66 GUILayout.Label(
67 PlasticLocalization.GetString(PlasticLocalization.Name.WorkspacesExplanationLabel),
68 EditorStyles.wordWrappedLabel);
69 }
70
71 static void DoFieldsArea(
72 Action<string> selectRepositoryAction,
73 Action<RepositoryCreationData> createRepositoryAction,
74 EditorWindow parentWindow,
75 IPlasticWebRestApi plasticWebRestApi,
76 string defaultServer,
77 ref CreateWorkspaceViewState state)
78 {
79 DoRepositoryField(
80 selectRepositoryAction,
81 createRepositoryAction,
82 parentWindow,
83 plasticWebRestApi,
84 defaultServer,
85 ref state);
86
87 DoWorkspaceField(ref state);
88 }
89
90 static void DoRepositoryField(
91 Action<string> selectRepositoryAction,
92 Action<RepositoryCreationData> createRepositoryAction,
93 EditorWindow parentWindow,
94 IPlasticWebRestApi plasticWebRestApi,
95 string defaultServer,
96 ref CreateWorkspaceViewState state)
97 {
98 EditorGUILayout.BeginHorizontal();
99
100 DoLabel(PlasticLocalization.GetString(PlasticLocalization.Name.Repository));
101
102 state.Repository = DoTextField(
103 state.Repository,
104 !state.ProgressData.IsOperationRunning,
105 LABEL_WIDTH,
106 TEXTBOX_WIDTH - BROWSE_BUTTON_WIDTH);
107
108 float browseButtonX =
109 LABEL_WIDTH + TEXTBOX_WIDTH + BUTTON_MARGIN -
110 BROWSE_BUTTON_WIDTH;
111 float browseButtonWidth =
112 BROWSE_BUTTON_WIDTH - BUTTON_MARGIN;
113
114 if (DoButton(
115 "...",
116 !state.ProgressData.IsOperationRunning,
117 browseButtonX,
118 browseButtonWidth))
119 {
120 DoBrowseRepositoryButton(
121 selectRepositoryAction,
122 parentWindow,
123 plasticWebRestApi,
124 defaultServer);
125
126 EditorGUIUtility.ExitGUI();
127 }
128
129 float newButtonX =
130 LABEL_WIDTH + TEXTBOX_WIDTH + BUTTON_MARGIN;
131 float newButtonWidth =
132 NEW_BUTTON_WIDTH - BUTTON_MARGIN;
133
134 if (DoButton(
135 PlasticLocalization.GetString(PlasticLocalization.Name.NewButton),
136 !state.ProgressData.IsOperationRunning,
137 newButtonX, newButtonWidth))
138 {
139 DoNewRepositoryButton(
140 createRepositoryAction,
141 parentWindow,
142 plasticWebRestApi,
143 state.Repository,
144 defaultServer);
145
146 EditorGUIUtility.ExitGUI();
147 }
148
149 ValidationResult validationResult = ValidateRepository(state.Repository);
150
151 if (!validationResult.IsValid)
152 DoWarningLabel(validationResult.ErrorMessage,
153 LABEL_WIDTH + TEXTBOX_WIDTH + NEW_BUTTON_WIDTH + LABEL_MARGIN);
154
155 EditorGUILayout.EndHorizontal();
156 }
157
158 static void DoWorkspaceField(
159 ref CreateWorkspaceViewState state)
160 {
161 EditorGUILayout.BeginHorizontal();
162
163 DoLabel(
164 PlasticLocalization.GetString(PlasticLocalization.Name.WorkspaceName));
165
166 state.WorkspaceName = DoTextField(
167 state.WorkspaceName,
168 !state.ProgressData.IsOperationRunning,
169 LABEL_WIDTH,
170 TEXTBOX_WIDTH - BROWSE_BUTTON_WIDTH);
171
172 ValidationResult validationResult = ValidateWorkspaceName(
173 state.WorkspaceName);
174
175 if (!validationResult.IsValid)
176 DoWarningLabel(validationResult.ErrorMessage,
177 LABEL_WIDTH + TEXTBOX_WIDTH - BROWSE_BUTTON_WIDTH + LABEL_MARGIN);
178
179 EditorGUILayout.EndHorizontal();
180 }
181
182 static void DoRadioButtonsArea(
183 ref CreateWorkspaceViewState state)
184 {
185 EditorGUILayout.BeginVertical();
186 DoLabel(
187 PlasticLocalization.GetString(PlasticLocalization.Name.WorkPreferenceQuestion));
188
189 if (DoRadioButton(
190 PlasticLocalization.GetString(PlasticLocalization.Name.WorkPreferenceAnswerUnityVCS),
191 state.WorkspaceMode == CreateWorkspaceViewState.WorkspaceModes.Developer,
192 !state.ProgressData.IsOperationRunning,
193 RADIO_BUTTON_MARGIN))
194 state.WorkspaceMode = CreateWorkspaceViewState.WorkspaceModes.Developer;
195
196 if (DoRadioButton(
197 PlasticLocalization.GetString(PlasticLocalization.Name.WorkPreferenceAnswerGluon),
198 state.WorkspaceMode == CreateWorkspaceViewState.WorkspaceModes.Gluon,
199 !state.ProgressData.IsOperationRunning,
200 RADIO_BUTTON_MARGIN))
201 state.WorkspaceMode = CreateWorkspaceViewState.WorkspaceModes.Gluon;
202
203 EditorGUILayout.EndVertical();
204 }
205
206 static void DoCreateWorkspaceButton(
207 Action<CreateWorkspaceViewState> createWorkspaceAction,
208 ref CreateWorkspaceViewState state)
209 {
210 EditorGUILayout.BeginHorizontal();
211
212 bool isButtonEnabled =
213 IsValidState(state) &&
214 !state.ProgressData.IsOperationRunning;
215
216 string buttonText = PlasticLocalization.GetString(
217 PlasticLocalization.Name.CreateWorkspace);
218
219 bool isButtonClicked = DoButton(buttonText, isButtonEnabled,
220 CREATE_WORKSPACE_BUTTON_MARGIN, CREATE_WORKSPACE_BUTTON_WIDTH);
221
222 GUI.enabled = true;
223
224 if (state.ProgressData.IsOperationRunning)
225 {
226 DoProgress(state.ProgressData,
227 CREATE_WORKSPACE_BUTTON_MARGIN +
228 PROGRESS_MARGIN +
229 CREATE_WORKSPACE_BUTTON_WIDTH);
230 }
231
232 EditorGUILayout.EndHorizontal();
233
234 if (isButtonClicked)
235 createWorkspaceAction(state);
236 }
237
238 static void DoBrowseRepositoryButton(
239 Action<string> selectRepositoryAction,
240 EditorWindow parentWindow,
241 IPlasticWebRestApi plasticWebRestApi,
242 string defaultServer)
243 {
244 string selectedRepository = RepositoryExplorerDialog.BrowseRepository(
245 parentWindow,
246 plasticWebRestApi,
247 defaultServer);
248
249 if (string.IsNullOrEmpty(selectedRepository))
250 return;
251
252 selectRepositoryAction(selectedRepository);
253 }
254
255 static void DoNewRepositoryButton(
256 Action<RepositoryCreationData> createRepositoryAction,
257 EditorWindow parentWindow,
258 IPlasticWebRestApi plasticWebRestApi,
259 string repositorySpecInput,
260 string defaultServer)
261 {
262 string proposedRepositoryName;
263 string proposedServer;
264
265 RepositorySpec repSpec = OrganizationsInformation.TryResolveRepositorySpecFromInput(repositorySpecInput);
266
267 if (repSpec != null)
268 {
269 proposedRepositoryName = repSpec.Name;
270 proposedServer = repSpec.Server;
271 }
272 else
273 {
274 proposedRepositoryName = ExtractRepositoryName(repositorySpecInput);
275 proposedServer = defaultServer;
276 }
277
278 RepositoryCreationData creationData = CreateRepositoryDialog.CreateRepository(
279 parentWindow,
280 plasticWebRestApi,
281 proposedRepositoryName,
282 proposedServer,
283 defaultServer,
284 ClientConfig.Get().GetWorkspaceServer());
285
286 createRepositoryAction(creationData);
287 }
288
289 static string ExtractRepositoryName(string repositorySpec)
290 {
291 string[] repositoryParts = repositorySpec.Split('@');
292
293 return repositoryParts.Length > 0 ? repositoryParts[0] : string.Empty;
294 }
295
296 static void DoHelpLabel()
297 {
298 string linkText =PlasticLocalization.Name.HereLink.GetString();
299 string labelText = PlasticLocalization.Name.LearnMoreDifferencesUnityVCS.GetString();
300
301 EditorGUILayout.BeginHorizontal();
302
303 GUIStyle labelStyle = new GUIStyle(EditorStyles.miniLabel);
304 labelStyle.richText = true;
305 labelStyle.stretchWidth = false;
306 labelStyle.margin = new RectOffset();
307 labelStyle.padding.left = LEARN_MORE_LABEL_LEFT_PADDING;
308
309 GUILayout.Label(labelText, labelStyle);
310
311 GUIStyle linkStyle = new GUIStyle(UnityStyles.LinkLabel);
312 linkStyle.fontSize = EditorStyles.miniLabel.fontSize;
313 linkStyle.stretchWidth = false;
314 linkStyle.margin = new RectOffset();
315
316 if (GUILayout.Button(linkText, linkStyle))
317 {
318 Application.OpenURL(PlasticLocalization.Name.PlasticSCMFullVsPartialWorkspaceLink.GetString());
319 }
320
321 EditorGUIUtility.AddCursorRect(
322 GUILayoutUtility.GetLastRect(), MouseCursor.Link);
323
324 EditorGUILayout.EndHorizontal();
325 }
326
327 static void DoNotificationArea(ProgressControlsForViews.Data progressData)
328 {
329 if (string.IsNullOrEmpty(progressData.NotificationMessage))
330 return;
331
332 DrawProgressForViews.ForNotificationArea(progressData);
333 }
334
335 static void DoLabel(string labelText)
336 {
337 GUIStyle labelStyle = new GUIStyle(EditorStyles.label);
338
339 Rect rect = GUILayoutUtility.GetRect(
340 new GUIContent(labelText),
341 labelStyle);
342
343 GUI.Label(rect, labelText, labelStyle);
344 }
345
346 static string DoTextField(
347 string entryValue,
348 bool enabled,
349 float textBoxLeft,
350 float textBoxWidth)
351 {
352 GUI.enabled = enabled;
353
354 var rect = GUILayoutUtility.GetRect(
355 new GUIContent(entryValue),
356 UnityStyles.Dialog.EntryLabel);
357 rect.width = textBoxWidth;
358 rect.x = textBoxLeft;
359
360 string result = GUI.TextField(rect, entryValue);
361
362 GUI.enabled = true;
363
364 return result;
365 }
366
367 static bool DoButton(
368 string text,
369 bool isEnabled,
370 float buttonLeft,
371 float buttonWidth)
372 {
373 GUI.enabled = isEnabled;
374
375 var rect = GUILayoutUtility.GetRect(
376 new GUIContent(text),
377 UnityStyles.Dialog.EntryLabel);
378
379 rect.width = buttonWidth;
380 rect.x = buttonLeft;
381
382 bool result = GUI.Button(rect, text);
383 GUI.enabled = true;
384 return result;
385 }
386
387 static bool DoRadioButton(
388 string text,
389 bool isChecked,
390 bool isEnabled,
391 float buttonLeft)
392 {
393 GUI.enabled = isEnabled;
394
395 GUIStyle radioButtonStyle = new GUIStyle(EditorStyles.radioButton);
396 radioButtonStyle.padding.left = RADIO_BUTTON_LEFT_PADDING;
397
398 var rect = GUILayoutUtility.GetRect(
399 new GUIContent(text),
400 radioButtonStyle);
401
402 rect.x = buttonLeft;
403
404 bool result = GUI.Toggle(
405 rect,
406 isChecked,
407 text,
408 radioButtonStyle);
409
410 GUI.enabled = true;
411
412 return result;
413 }
414
415 static void DoWarningLabel(
416 string labelText,
417 float labelLeft)
418 {
419 Rect rect = GUILayoutUtility.GetRect(
420 new GUIContent(labelText),
421 EditorStyles.label);
422
423 rect.x = labelLeft;
424
425 GUI.Label(rect,
426 new GUIContent(labelText, Images.GetWarnIcon()),
427 UnityStyles.HeaderWarningLabel);
428 }
429
430 static void DoProgress(
431 ProgressControlsForViews.Data data,
432 float progressLeft)
433 {
434 if (string.IsNullOrEmpty(data.ProgressMessage))
435 return;
436
437 var rect = GUILayoutUtility.GetRect(
438 new GUIContent(data.ProgressMessage),
439 UnityStyles.Dialog.EntryLabel);
440
441 rect.x = progressLeft;
442
443 GUI.Label(rect, data.ProgressMessage);
444 }
445
446 static bool IsValidState(
447 CreateWorkspaceViewState state)
448 {
449 if (!ValidateRepository(state.Repository).IsValid)
450 return false;
451
452 if (!ValidateWorkspaceName(state.WorkspaceName).IsValid)
453 return false;
454
455 return true;
456 }
457
458 static ValidationResult ValidateRepository(string repository)
459 {
460 ValidationResult result = new ValidationResult();
461
462 if (string.IsNullOrEmpty(repository))
463 {
464 result.ErrorMessage = PlasticLocalization.GetString(PlasticLocalization.Name.RepositoryNameEmpty);
465 result.IsValid = false;
466 return result;
467 }
468
469 result.IsValid = true;
470 return result;
471 }
472
473 static ValidationResult ValidateWorkspaceName(string workspaceName)
474 {
475 ValidationResult result = new ValidationResult();
476
477 if (string.IsNullOrEmpty(workspaceName))
478 {
479 result.ErrorMessage = PlasticLocalization.GetString(PlasticLocalization.Name.WorkspaceNameEmpty);
480 result.IsValid = false;
481 return result;
482 }
483
484 result.IsValid = true;
485 return result;
486 }
487
488 class ValidationResult
489 {
490 internal string ErrorMessage;
491 internal bool IsValid;
492 }
493
494 const float LABEL_WIDTH = 150;
495 const float TEXTBOX_WIDTH = 400;
496 const float BROWSE_BUTTON_WIDTH = 25;
497 const float NEW_BUTTON_WIDTH = 60;
498 const float BUTTON_MARGIN = 2;
499 const float LABEL_MARGIN = 2;
500 const float RADIO_BUTTON_MARGIN = 38;
501 const int RADIO_BUTTON_LEFT_PADDING = 20;
502 const float PROGRESS_MARGIN = 5;
503 const float CREATE_WORKSPACE_BUTTON_MARGIN = 32;
504 const float CREATE_WORKSPACE_BUTTON_WIDTH = 160;
505 const int LEARN_MORE_LABEL_LEFT_PADDING = 10;
506 }
507}