A game about forced loneliness, made by TACStudios
1using System.IO;
2
3using UnityEditor;
4using UnityEngine;
5
6using PlasticGui;
7using PlasticGui.WorkspaceWindow;
8using PlasticGui.WorkspaceWindow.Diff;
9using Unity.PlasticSCM.Editor.UI;
10using Unity.PlasticSCM.Editor.UI.Progress;
11
12namespace Unity.PlasticSCM.Editor.Views.Diff.Dialogs
13{
14 internal class GetRestorePathDialog : PlasticDialog
15 {
16 protected override Rect DefaultRect
17 {
18 get
19 {
20 var baseRect = base.DefaultRect;
21 return new Rect(baseRect.x, baseRect.y, 600, 205);
22 }
23 }
24
25 internal static GetRestorePathData GetRestorePath(
26 string wkPath,
27 string restorePath,
28 string explanation,
29 bool isDirectory,
30 bool showSkipButton,
31 EditorWindow parentWindow)
32 {
33 GetRestorePathDialog dialog = Create(
34 new ProgressControlsForDialogs(),
35 wkPath,
36 GetProposedRestorePath.For(restorePath),
37 explanation,
38 isDirectory,
39 showSkipButton);
40
41 ResponseType dialogResult = dialog.RunModal(parentWindow);
42
43 GetRestorePathData result = dialog.BuildGetRestorePathResult();
44
45 result.Result = GetRestorePathResultType(dialogResult);
46 return result;
47 }
48
49 protected override void OnModalGUI()
50 {
51 Title(PlasticLocalization.GetString(
52 PlasticLocalization.Name.EnterRestorePathFormTitle));
53
54 Paragraph(mExplanation);
55
56 DoEntryArea();
57
58 GUILayout.Space(10);
59
60 DrawProgressForDialogs.For(
61 mProgressControls.ProgressData);
62
63 DoButtonsArea();
64
65 mProgressControls.ForcedUpdateProgress(this);
66 }
67
68 void DoEntryArea()
69 {
70 GUILayout.Label(PlasticLocalization.GetString(
71 PlasticLocalization.Name.EnterRestorePathFormTextBoxExplanation),
72 EditorStyles.label);
73
74 GUILayout.BeginHorizontal();
75
76 mRestorePath = GUILayout.TextField(
77 mRestorePath, GUILayout.Width(TEXTBOX_WIDTH));
78
79 if (GUILayout.Button("...", EditorStyles.miniButton))
80 {
81 mRestorePath = (mIsDirectory) ?
82 DoOpenFolderPanel(mRestorePath) :
83 DoOpenFilePanel(mRestorePath);
84 }
85
86 GUILayout.EndHorizontal();
87 }
88
89 protected override string GetTitle()
90 {
91 return PlasticLocalization.GetString(
92 PlasticLocalization.Name.EnterRestorePathFormTitle);
93 }
94
95 static string DoOpenFolderPanel(string actualPath)
96 {
97 string parentDirectory = null;
98 string directoryName = null;
99
100 if (!string.IsNullOrEmpty(actualPath))
101 {
102 parentDirectory = Path.GetDirectoryName(actualPath);
103 directoryName = Path.GetFileName(actualPath);
104 }
105
106 string result = EditorUtility.SaveFolderPanel(
107 PlasticLocalization.GetString(PlasticLocalization.Name.SelectPathToRestore),
108 parentDirectory,
109 directoryName);
110
111 if (string.IsNullOrEmpty(result))
112 return actualPath;
113
114 return Path.GetFullPath(result);
115 }
116
117 static string DoOpenFilePanel(string actualPath)
118 {
119 string parentDirectory = null;
120 string fileName = null;
121 string extension = null;
122
123 if (!string.IsNullOrEmpty(actualPath))
124 {
125 parentDirectory = Path.GetDirectoryName(actualPath);
126 fileName = Path.GetFileName(actualPath);
127 extension = Path.GetExtension(actualPath);
128 }
129
130 string result = EditorUtility.SaveFilePanel(
131 PlasticLocalization.GetString(PlasticLocalization.Name.SelectPathToRestore),
132 parentDirectory,
133 fileName,
134 extension);
135
136 if (string.IsNullOrEmpty(result))
137 return actualPath;
138
139 return Path.GetFullPath(result);
140 }
141
142 void DoButtonsArea()
143 {
144 using (new EditorGUILayout.HorizontalScope())
145 {
146 GUILayout.FlexibleSpace();
147
148 if (Application.platform == RuntimePlatform.WindowsEditor)
149 {
150 DoOkButton();
151 DoSkipButton(mShowSkipButton);
152 DoCancelButton();
153 return;
154 }
155
156 DoCancelButton();
157 DoSkipButton(mShowSkipButton);
158 DoOkButton();
159 }
160 }
161
162 void DoOkButton()
163 {
164 if (!AcceptButton(PlasticLocalization.GetString(
165 PlasticLocalization.Name.OkButton)))
166 return;
167
168 OkButtonWithValidationAction();
169 }
170
171 void DoSkipButton(bool showSkipButton)
172 {
173 if (!showSkipButton)
174 return;
175
176 if (!NormalButton(PlasticLocalization.GetString(PlasticLocalization.Name.SkipRestoreButton)))
177 return;
178
179 CloseButtonAction();
180 }
181
182 void DoCancelButton()
183 {
184 if (!NormalButton(PlasticLocalization.GetString(
185 PlasticLocalization.Name.CancelButton)))
186 return;
187
188 CancelButtonAction();
189 }
190
191 void OkButtonWithValidationAction()
192 {
193 GetRestorePathValidation.Validation(
194 mWkPath, BuildGetRestorePathResult(),
195 this, mProgressControls);
196 }
197
198 GetRestorePathData BuildGetRestorePathResult()
199 {
200 return new GetRestorePathData(mRestorePath);
201 }
202
203 static GetRestorePathData.ResultType GetRestorePathResultType(
204 ResponseType dialogResult)
205 {
206 switch (dialogResult)
207 {
208 case ResponseType.None:
209 return GetRestorePathData.ResultType.Skip;
210 case ResponseType.Ok:
211 return GetRestorePathData.ResultType.OK;
212 case ResponseType.Cancel:
213 return GetRestorePathData.ResultType.Cancel;
214 }
215
216 return GetRestorePathData.ResultType.Cancel;
217 }
218
219 static GetRestorePathDialog Create(
220 ProgressControlsForDialogs progressControls,
221 string wkPath,
222 string restorePath,
223 string explanation,
224 bool isDirectory,
225 bool showSkipButton)
226 {
227 var instance = CreateInstance<GetRestorePathDialog>();
228 instance.mWkPath = wkPath;
229 instance.mRestorePath = restorePath;
230 instance.mExplanation = explanation;
231 instance.mIsDirectory = isDirectory;
232 instance.mShowSkipButton = showSkipButton;
233 instance.mEnterKeyAction = instance.OkButtonWithValidationAction;
234 instance.mEscapeKeyAction = instance.CancelButtonAction;
235 instance.mProgressControls = progressControls;
236 return instance;
237 }
238
239 bool mIsDirectory;
240 bool mShowSkipButton;
241 string mExplanation = string.Empty;
242 string mRestorePath = string.Empty;
243 string mWkPath = string.Empty;
244
245 ProgressControlsForDialogs mProgressControls;
246
247 const float TEXTBOX_WIDTH = 520;
248 }
249}