-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathNewProject.xaml.cs
More file actions
362 lines (314 loc) · 14.7 KB
/
NewProject.xaml.cs
File metadata and controls
362 lines (314 loc) · 14.7 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace UnityLauncherPro
{
public partial class NewProject : Window
{
public static string newProjectName = null;
public static string newVersion = null;
public static string newName = null;
public static string templateZipPath = null;
public static string selectedPlatform = null;
public static bool forceDX11 = false;
public static string[] platformsForThisUnity = null;
bool isInitializing = true; // to keep OnChangeEvent from firing too early
int previousSelectedTemplateIndex = -1;
int previousSelectedModuleIndex = -1;
public static string targetFolder { get; private set; } = null;
public NewProject(string unityVersion, string suggestedName, string targetFolder, bool nameIsLocked = false)
{
isInitializing = true;
InitializeComponent();
NewProject.targetFolder = targetFolder;
// get version
newVersion = unityVersion;
newName = suggestedName;
txtNewProjectName.IsEnabled = !nameIsLocked;
txtNewProjectName.Text = newName;
txtNewProjectFolder.Text = targetFolder;
// fill available versions
if (gridAvailableVersions.ItemsSource == null)
{
gridAvailableVersions.ItemsSource = MainWindow.unityInstallationsSource;
}
// we have that version installed
if (MainWindow.unityInstalledVersions.ContainsKey(unityVersion) == true)
{
// find this unity version, TODO theres probably easier way than looping all
for (int i = 0; i < MainWindow.unityInstallationsSource.Count; i++)
{
if (MainWindow.unityInstallationsSource[i].Version == newVersion)
{
gridAvailableVersions.SelectedIndex = i;
gridAvailableVersions.ScrollIntoView(gridAvailableVersions.SelectedItem);
break;
}
}
UpdateTemplatesDropDown((gridAvailableVersions.SelectedItem as UnityInstallation).Path);
UpdateModulesDropdown(newVersion);
}
else // we dont have requested unity version, select first item then
{
var path = MainWindow.unityInstallationsSource[0].Path;
gridAvailableVersions.SelectedIndex = 0;
gridAvailableVersions.ScrollIntoView(gridAvailableVersions.Items[0]);
UpdateTemplatesDropDown(path);
}
// select projectname text so can overwrite if needed
txtNewProjectName.Focus();
txtNewProjectName.SelectAll();
newProjectName = txtNewProjectName.Text;
isInitializing = false;
} // NewProject
void UpdateTemplatesDropDown(string unityPath)
{
// scan available templates, TODO could cache this at least per session?
cmbNewProjectTemplate.ItemsSource = Tools.ScanTemplates(unityPath);
cmbNewProjectTemplate.SelectedIndex = 0;
lblTemplateTitleAndCount.Content = "Templates: (" + (cmbNewProjectTemplate.Items.Count - 1) + ")";
}
void UpdateModulesDropdown(string version)
{
// get modules and stick into combobox, NOTE we already have this info from GetProjects.Scan, so could access it
platformsForThisUnity = Tools.GetPlatformsForUnityVersion(version);
cmbNewProjectPlatform.ItemsSource = platformsForThisUnity;
var lastUsedPlatform = Properties.Settings.Default.newProjectPlatform;
for (int i = 0; i < platformsForThisUnity.Length; i++)
{
// set default platform (win64) if never used this setting before
if ((string.IsNullOrEmpty(lastUsedPlatform) && platformsForThisUnity[i].ToLower() == "win64") || platformsForThisUnity[i] == lastUsedPlatform)
{
cmbNewProjectPlatform.SelectedIndex = i;
break;
}
}
// if nothing found, use win64
if (cmbNewProjectPlatform.SelectedIndex == -1)
{
//cmbNewProjectPlatform.SelectedIndex = cmbNewProjectPlatform.Items.Count > 1 ? 1 : 0;
for (int i = 0; i < platformsForThisUnity.Length; i++)
{
if (platformsForThisUnity[i].ToLower() == "win64")
{
cmbNewProjectPlatform.SelectedIndex = i;
break;
}
}
// if still nothing, use first
if (cmbNewProjectPlatform.SelectedIndex == -1) cmbNewProjectPlatform.SelectedIndex = 0;
//lblTemplateTitleAndCount.Content = "Templates: (" + (cmbNewProjectTemplate.Items.Count - 1) + ")";
}
}
private void BtnCreateNewProject_Click(object sender, RoutedEventArgs e)
{
// check if projectname already exists (only if should be automatically created name)
var targetPath = Path.Combine(targetFolder, txtNewProjectName.Text);
if (txtNewProjectName.IsEnabled == true && Directory.Exists(targetPath) == true)
{
Tools.SetStatus("Project already exists: " + txtNewProjectName.Text);
return;
}
templateZipPath = ((KeyValuePair<string, string>)cmbNewProjectTemplate.SelectedValue).Value;
selectedPlatform = cmbNewProjectPlatform.SelectedValue.ToString();
UpdateSelectedVersion();
// save last used value for platform
Properties.Settings.Default.newProjectPlatform = cmbNewProjectPlatform.SelectedValue.ToString();
Properties.Settings.Default.Save();
DialogResult = true;
}
private void BtnCancelNewProject_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Tab:
// manually tab into next component (automatic tabstops not really working here)
TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
UIElement keyboardFocus = Keyboard.FocusedElement as UIElement;
if (keyboardFocus != null)
{
keyboardFocus.MoveFocus(tRequest);
}
break;
case Key.F2: // select project name field
txtNewProjectName.Focus();
txtNewProjectName.SelectAll();
break;
case Key.F3: // next platform
cmbNewProjectPlatform.SelectedIndex = ++cmbNewProjectPlatform.SelectedIndex % cmbNewProjectPlatform.Items.Count;
break;
case Key.F4: // next template
case Key.Oem5: // select next template §-key
cmbNewProjectTemplate.SelectedIndex = ++cmbNewProjectTemplate.SelectedIndex % cmbNewProjectTemplate.Items.Count;
e.Handled = true; // override writing to textbox
break;
case Key.Enter: // enter, create proj
BtnCreateNewProject_Click(null, null);
e.Handled = true;
break;
case Key.Escape: // esc cancel
// if pressed esc while combobox is open, close that one instead of closing window
if (cmbNewProjectTemplate.IsDropDownOpen)
{
cmbNewProjectTemplate.IsDropDownOpen = false;
if (previousSelectedTemplateIndex > -1) cmbNewProjectTemplate.SelectedIndex = previousSelectedTemplateIndex;
return;
}
if (cmbNewProjectPlatform.IsDropDownOpen)
{
cmbNewProjectPlatform.IsDropDownOpen = false;
if (previousSelectedModuleIndex > -1) cmbNewProjectPlatform.SelectedIndex = previousSelectedModuleIndex;
return;
}
DialogResult = false;
e.Handled = true;
break;
default:
break;
}
}
void UpdateSelectedVersion()
{
var k = gridAvailableVersions.SelectedItem as UnityInstallation;
if (k != null && k.Version != newVersion)
{
newVersion = k.Version;
}
}
private void TxtNewProjectName_TextChanged(object sender, TextChangedEventArgs e)
{
if (isInitializing == true) return;
// warning yellow if contains space at start or end
if (txtNewProjectName.Text.StartsWith(" ") || txtNewProjectName.Text.EndsWith(" "))
{
// NOTE txtbox outline didnt work
txtNewProjectName.Background = Brushes.Yellow;
txtNewProjectStatus.Text = "Warning: Project name starts or ends with SPACE character";
txtNewProjectStatus.Foreground = Brushes.Orange;
}
else
{
// NOTE this element is not using themes yet, so can set white
txtNewProjectName.Background = Brushes.White;
txtNewProjectStatus.Foreground = Brushes.White;
txtNewProjectStatus.Text = "";
}
// validate new projectname that it doesnt exists already
var targetPath = Path.Combine(targetFolder, txtNewProjectName.Text);
if (Directory.Exists(targetPath) == true)
{
System.Console.WriteLine("Project already exists");
txtNewProjectName.BorderBrush = Brushes.Red; // not visible if focused
txtNewProjectName.ToolTip = "Project folder already exists";
btnCreateNewProject.IsEnabled = false;
}
else
{
txtNewProjectName.BorderBrush = null;
btnCreateNewProject.IsEnabled = true;
txtNewProjectName.ToolTip = "";
}
//System.Console.WriteLine("newProjectName: " + txtNewProjectName.Text);
newProjectName = txtNewProjectName.Text;
}
private void TxtNewProjectName_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.PageUp:
case Key.PageDown:
case Key.Up:
case Key.Down:
Tools.SetFocusToGrid(gridAvailableVersions);
break;
default:
break;
}
}
void GenerateNewName()
{
var newProj = Tools.GetSuggestedProjectName(newVersion, txtNewProjectFolder.Text.ToString());
txtNewProjectName.Text = newProj;
}
// FIXME this gets called when list is updated?
private void GridAvailableVersions_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (gridAvailableVersions.SelectedItem == null || isInitializing == true) return;
// new row selected, generate new project name for this version
var k = gridAvailableVersions.SelectedItem as UnityInstallation;
newVersion = k.Version;
// no new name, if field is locked (because its folder name then)
if (txtNewProjectName.IsEnabled == true) GenerateNewName();
// update templates list for selected unity version
UpdateTemplatesDropDown(k.Path);
UpdateModulesDropdown(k.Version);
// hide forceDX11 checkbox if version is below 6000
bool is6000 = k.Version.Contains("6000");
chkForceDX11.Visibility = is6000 ? Visibility.Visible : Visibility.Collapsed;
}
private void GridAvailableVersions_Loaded(object sender, RoutedEventArgs e)
{
// set initial default row color
DataGridRow row = (DataGridRow)gridAvailableVersions.ItemContainerGenerator.ContainerFromIndex(gridAvailableVersions.SelectedIndex);
// if no unitys available
if (row == null) return;
//row.Background = Brushes.Green;
row.Foreground = Brushes.White;
row.FontWeight = FontWeights.Bold;
}
private void CmbNewProjectTemplate_DropDownOpened(object sender, System.EventArgs e)
{
// on open, take current selection, so can undo later
previousSelectedTemplateIndex = cmbNewProjectTemplate.SelectedIndex;
}
private void CmbNewProjectPlatform_DropDownOpened(object sender, System.EventArgs e)
{
previousSelectedModuleIndex = cmbNewProjectPlatform.SelectedIndex;
}
private void gridAvailableVersions_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// check that we clicked actually on a row
var src = VisualTreeHelper.GetParent((DependencyObject)e.OriginalSource);
var srcType = src.GetType();
if (srcType == typeof(ContentPresenter))
{
BtnCreateNewProject_Click(null, null);
}
}
private void chkForceDX11_Checked(object sender, RoutedEventArgs e)
{
forceDX11 = chkForceDX11.IsChecked == true;
}
private void btnBrowseForProjectFolder_Click(object sender, RoutedEventArgs e)
{
var folder = Tools.BrowseForOutputFolder("Select New Project folder");
if (string.IsNullOrEmpty(folder) == false && Directory.Exists(folder) == true)
{
txtNewProjectFolder.Text = folder;
}
}
private void txtNewProjectFolder_TextChanged(object sender, TextChangedEventArgs e)
{
// validate that folder exists
if (Directory.Exists(txtNewProjectFolder.Text) == false)
{
txtNewProjectFolder.BorderBrush = Brushes.Red; // not visible if focused
btnCreateNewProject.IsEnabled = false;
}
else
{
txtNewProjectFolder.BorderBrush = null;
btnCreateNewProject.IsEnabled = true;
targetFolder = txtNewProjectFolder.Text;
}
}
}
}