forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
690 lines (553 loc) · 21.9 KB
/
MainWindow.cs
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using Eto.Forms;
using Eto.Drawing;
using System.Reflection;
namespace MonoGame.Tools.Pipeline
{
partial class MainWindow : Form, IView
{
#pragma warning disable 649
public EventHandler<EventArgs> RecentChanged;
public EventHandler<EventArgs> TitleChanged;
#pragma warning restore 649
public const string TitleBase = "MonoGame Pipeline Tool";
public static MainWindow Instance;
private List<Pad> _pads;
private Clipboard _clipboard;
private ContextMenu _contextMenu;
private FileFilter _mgcbFileFilter, _allFileFilter, _xnaFileFilter;
private string[] monoLocations = {
"/usr/bin/mono",
"/usr/local/bin/mono",
"/Library/Frameworks/Mono.framework/Versions/Current/bin/mono",
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "mono"),
};
int setw = 0;
public MainWindow()
{
_pads = new List<Pad>();
_clipboard = new Clipboard();
InitializeComponent();
Instance = this;
// Fill in Pad menu
foreach (var pad in _pads)
{
if (pad.Commands.Count > 0)
{
var menu = new ButtonMenuItem();
menu.Text = pad.Title;
foreach (var com in pad.Commands)
menu.Items.Add(com.CreateMenuItem());
menuView.Items.Add(menu);
}
}
#if MONOMAC
splitterVertical.PositionChanged += delegate {
setw++;
if (setw > 2)
{
propertyGridControl.SetWidth();
setw = 0;
}
};
#endif
_contextMenu = new ContextMenu();
projectControl.SetContextMenu(_contextMenu);
_mgcbFileFilter = new FileFilter("MonoGame Content Build Project (*.mgcb)", new[] { ".mgcb" });
_allFileFilter = new FileFilter("All Files (*.*)", new[] { ".*" });
_xnaFileFilter = new FileFilter("XNA Content Projects (*.contentproj)", new[] { ".contentproj" });
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !PipelineController.Instance.Exit();
base.OnClosing(e);
}
#region IView implements
public void Attach(IController controller)
{
PipelineController.Instance.OnProjectLoaded += () => projectControl.ExpandBase();
cmdDebugMode.Checked = PipelineSettings.Default.DebugMode;
foreach (var control in _pads)
control.LoadSettings();
Style = "MainWindow";
}
public void Invoke(Action action)
{
Application.Instance.Invoke(action);
}
public AskResult AskSaveOrCancel()
{
var result = MessageBox.Show(this, "Do you want to save the project first?", "Save Project", MessageBoxButtons.YesNoCancel, MessageBoxType.Question);
if (result == DialogResult.Yes)
return AskResult.Yes;
if (result == DialogResult.No)
return AskResult.No;
return AskResult.Cancel;
}
public bool AskSaveName(ref string filePath, string title)
{
var dialog = new SaveFileDialog();
dialog.Title = title;
dialog.Filters.Add(_mgcbFileFilter);
dialog.Filters.Add(_allFileFilter);
dialog.CurrentFilter = _mgcbFileFilter;
if (dialog.ShowDialog(this) == DialogResult.Ok)
{
filePath = dialog.FileName;
if (dialog.CurrentFilter == _mgcbFileFilter && !filePath.EndsWith(".mgcb"))
filePath += ".mgcb";
return true;
}
return false;
}
public bool AskOpenProject(out string projectFilePath)
{
var dialog = new OpenFileDialog();
dialog.Filters.Add(_mgcbFileFilter);
dialog.Filters.Add(_allFileFilter);
dialog.CurrentFilter = _mgcbFileFilter;
if (dialog.ShowDialog(this) == DialogResult.Ok)
{
projectFilePath = dialog.FileName;
return true;
}
projectFilePath = "";
return false;
}
public bool AskImportProject(out string projectFilePath)
{
var dialog = new OpenFileDialog();
dialog.Filters.Add(_xnaFileFilter);
dialog.Filters.Add(_allFileFilter);
dialog.CurrentFilter = _xnaFileFilter;
if (dialog.ShowDialog(this) == DialogResult.Ok)
{
projectFilePath = dialog.FileName;
return true;
}
projectFilePath = "";
return false;
}
public void ShowError(string title, string message)
{
MessageBox.Show(this, message, title, MessageBoxButtons.OK, MessageBoxType.Error);
}
public void ShowMessage(string message)
{
MessageBox.Show(this, message, "Info", MessageBoxButtons.OK, MessageBoxType.Information);
}
public void BeginTreeUpdate()
{
}
public void SetTreeRoot(IProjectItem item)
{
projectControl.SetRoot(item);
}
public void AddTreeItem(IProjectItem item)
{
projectControl.AddItem(item);
}
public void RemoveTreeItem(IProjectItem item)
{
projectControl.RemoveItem(item);
}
public void UpdateTreeItem(IProjectItem item)
{
projectControl.UpdateItem(item);
}
public void EndTreeUpdate()
{
}
public void UpdateProperties()
{
propertyGridControl.SetObjects(PipelineController.Instance.SelectedItems);
}
public void OutputAppend(string text)
{
Application.Instance.AsyncInvoke(() => buildOutput.WriteLine(text));
}
public void OutputClear()
{
Application.Instance.Invoke(() => buildOutput.ClearOutput());
}
public bool ShowDeleteDialog(List<IProjectItem> items)
{
var dialog = new DeleteDialog(PipelineController.Instance, items);
return dialog.ShowModal(this);
}
public bool ShowEditDialog(string title, string text, string oldname, bool file, out string newname)
{
var dialog = new EditDialog(title, text, oldname, file);
var result = dialog.ShowModal(this);
newname = dialog.Text;
return result;
}
public bool ChooseContentFile(string initialDirectory, out List<string> files)
{
var dialog = new OpenFileDialog();
dialog.Directory = new Uri(initialDirectory);
dialog.MultiSelect = true;
dialog.Filters.Add(_allFileFilter);
dialog.CurrentFilter = _allFileFilter;
var result = dialog.ShowDialog(this) == DialogResult.Ok;
files = new List<string>();
files.AddRange(dialog.Filenames);
return result;
}
public bool ChooseContentFolder(string initialDirectory, out string folder)
{
var dialog = new SelectFolderDialog();
dialog.Directory = initialDirectory;
var result = dialog.ShowDialog(this) == DialogResult.Ok;
folder = dialog.Directory;
return result;
}
public bool ChooseItemTemplate(string folder, out ContentItemTemplate template, out string name)
{
var dialog = new NewItemDialog(PipelineController.Instance.Templates.GetEnumerator(), folder);
var result = dialog.ShowModal(this);
template = dialog.Selected;
name = dialog.Name;
return result;
}
public bool CopyOrLinkFile(string file, bool exists, out CopyAction action, out bool applyforall)
{
var dialog = new AddItemDialog(file, exists, FileType.File);
var result = dialog.ShowModal(this);
action = dialog.Responce;
applyforall = dialog.ApplyForAll;
return result;
}
public bool CopyOrLinkFolder(string folder, bool exists, out CopyAction action, out bool applyforall)
{
var afd = new AddItemDialog(folder, exists, FileType.Folder);
applyforall = false;
if (afd.ShowModal(this))
{
action = afd.Responce;
return true;
}
action = CopyAction.Link;
return false;
}
public Process CreateProcess(string exe, string commands)
{
var proc = new Process();
if (!Global.Unix)
{
proc.StartInfo.FileName = exe;
proc.StartInfo.Arguments = commands;
}
else
{
string monoLoc = null;
foreach (var path in monoLocations)
{
if (File.Exists(path))
{
monoLoc = path;
break;
}
}
if (string.IsNullOrEmpty(monoLoc))
{
monoLoc = "mono";
OutputAppend("Could not find mono. Please install the latest version from http://www.mono-project.com");
}
proc.StartInfo.FileName = monoLoc;
if (PipelineSettings.Default.DebugMode)
{
var port = Environment.GetEnvironmentVariable("MONO_DEBUGGER_PORT");
port = !string.IsNullOrEmpty(port) ? port : "55555";
var monodebugger = string.Format("--debug --debugger-agent=transport=dt_socket,server=y,address=127.0.0.1:{0}",
port);
proc.StartInfo.Arguments = string.Format("{0} \"{1}\" {2}", monodebugger, exe, commands);
OutputAppend("************************************************");
OutputAppend("RUNNING MGCB IN DEBUG MODE!!!");
OutputAppend(string.Format("Attach your Debugger to localhost:{0}", port));
OutputAppend("************************************************");
}
else
{
proc.StartInfo.Arguments = string.Format("\"{0}\" {1}", exe, commands);
}
}
return proc;
}
public void UpdateCommands(MenuInfo info)
{
// Title
if (TitleChanged != null)
TitleChanged(this, EventArgs.Empty);
else
{
var title = TitleBase;
if (PipelineController.Instance.ProjectOpen)
{
title += " - " + Path.GetFileName(PipelineController.Instance.ProjectItem.OriginalPath);
if (PipelineController.Instance.ProjectDirty)
title += "*";
}
Title = title;
}
// Menu
cmdNew.Enabled = info.New;
cmdOpen.Enabled = info.Open;
cmdImport.Enabled = info.Import;
cmdSave.Enabled = info.Save;
cmdSaveAs.Enabled = info.SaveAs;
cmdClose.Enabled = info.Close;
cmdExit.Enabled = info.Exit;
cmdUndo.Enabled = info.Undo;
cmdRedo.Enabled = info.Redo;
cmdAdd.Enabled = info.Add;
cmdNewItem.Enabled = info.Add;
cmdNewFolder.Enabled = info.Add;
cmdExistingItem.Enabled = info.Add;
cmdExistingFolder.Enabled = info.Add;
cmdExclude.Enabled = info.Exclude;
cmdRename.Enabled = info.Rename;
cmdDelete.Enabled = info.Delete;
cmdBuild.Enabled = info.Build;
cmdRebuild.Enabled = info.Rebuild;
cmdClean.Enabled = info.Clean;
cmdCancelBuild.Enabled = info.Cancel;
cmdOpenItem.Enabled = info.OpenItem;
cmdOpenItemWith.Enabled = info.OpenItemWith;
cmdOpenItemLocation.Enabled = info.OpenItemLocation;
cmdOpenOutputItemLocation.Enabled = info.OpenOutputItemLocation;
cmdCopyAssetPath.Enabled = info.CopyAssetPath;
cmdRebuildItem.Enabled = info.RebuildItem;
// Visibility of menu items can't be changed so
// we need to recreate the context menu each time.
// Context Menu
var sep = false;
_contextMenu.Items.Clear();
AddContextMenu(cmOpenItem, ref sep);
AddContextMenu(cmOpenItemWith, ref sep);
AddContextMenu(cmAdd, ref sep);
AddSeparator(ref sep);
AddContextMenu(cmOpenItemLocation, ref sep);
AddContextMenu(cmOpenOutputItemLocation, ref sep);
AddContextMenu(cmCopyAssetPath, ref sep);
AddContextMenu(cmRebuildItem, ref sep);
AddSeparator(ref sep);
AddContextMenu(cmExclude, ref sep);
AddSeparator(ref sep);
AddContextMenu(cmRename, ref sep);
AddContextMenu(cmDelete, ref sep);
if (_contextMenu.Items.Count > 0)
{
var lastItem = _contextMenu.Items[_contextMenu.Items.Count - 1];
if (lastItem is SeparatorMenuItem)
_contextMenu.Items.Remove(lastItem);
}
}
private void AddContextMenu(MenuItem item, ref bool separator)
{
item.Shortcut = Keys.None;
if (item.Enabled)
_contextMenu.Items.Add(item);
separator |= item.Enabled;
}
private void AddSeparator(ref bool separator)
{
if (separator)
{
if (!(_contextMenu.Items[_contextMenu.Items.Count - 1] is SeparatorMenuItem))
_contextMenu.Items.Add(new SeparatorMenuItem());
separator = false;
}
}
public void UpdateRecentList(List<string> recentList)
{
if (RecentChanged != null)
{
RecentChanged(recentList, EventArgs.Empty);
return;
}
menuRecent.Items.Clear();
foreach (var recent in recentList)
{
var item = new ButtonMenuItem();
item.Text = recent;
item.Click += (sender, e) => PipelineController.Instance.OpenProject(recent);
menuRecent.Items.Insert(0, item);
}
if (menuRecent.Items.Count > 0)
{
menuRecent.Items.Add(new SeparatorMenuItem());
var clearItem = new ButtonMenuItem();
clearItem.Text = "Clear";
clearItem.Click += (sender, e) => PipelineController.Instance.ClearRecentList();
menuRecent.Items.Add(clearItem);
}
}
public void SetClipboard(string text)
{
_clipboard.Clear();
_clipboard.Text = text;
}
#endregion
#region Commands
private void CmdNew_Executed(object sender, EventArgs e)
{
PipelineController.Instance.NewProject();
}
private void CmdOpen_Executed(object sender, EventArgs e)
{
PipelineController.Instance.OpenProject();
}
private void CmdClose_Executed(object sender, EventArgs e)
{
PipelineController.Instance.CloseProject();
}
private void CmdImport_Executed(object sender, EventArgs e)
{
PipelineController.Instance.ImportProject();
}
private void CmdSave_Executed(object sender, EventArgs e)
{
PipelineController.Instance.SaveProject(false);
}
private void CmdSaveAs_Executed(object sender, EventArgs e)
{
PipelineController.Instance.SaveProject(true);
}
private void CmdExit_Executed(object sender, EventArgs e)
{
Application.Instance.Quit();
}
private void CmdUndo_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Undo();
}
private void CmdRedo_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Redo();
}
private void CmdExclude_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Exclude(false);
}
private void CmdRename_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Rename();
}
private void CmdDelete_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Exclude(true);
}
private void CmdNewItem_Executed(object sender, EventArgs e)
{
PipelineController.Instance.NewItem();
}
private void CmdNewFolder_Executed(object sender, EventArgs e)
{
PipelineController.Instance.NewFolder();
}
private void CmdExistingItem_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Include();
}
private void CmdExistingFolder_Executed(object sender, EventArgs e)
{
PipelineController.Instance.IncludeFolder();
}
private void CmdBuild_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Build(false);
}
private void CmdRebuild_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Build(true);
}
private void CmdClean_Executed(object sender, EventArgs e)
{
PipelineController.Instance.Clean();
}
private void CmdCancelBuild_Executed(object sender, EventArgs e)
{
PipelineController.Instance.CancelBuild();
}
private void CmdDebugMode_Executed(object sender, EventArgs e)
{
PipelineSettings.Default.DebugMode = cmdDebugMode.Checked;
}
private void CmdHelp_Executed(object sender, EventArgs e)
{
Process.Start("http://www.monogame.net/documentation/?page=Pipeline");
}
private void CmdAbout_Executed(object sender, EventArgs e)
{
var adialog = new AboutDialog();
adialog.Logo = Bitmap.FromResource("Icons.monogame.png");
adialog.WebsiteLabel = "MonoGame Website";
adialog.Website = new Uri("http://www.monogame.net/");
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LICENSE.txt"))
using (var reader = new StreamReader(stream))
adialog.License = reader.ReadToEnd();
adialog.ShowDialog(this);
}
private void CmdOpenItem_Executed(object sender, EventArgs e)
{
if (PipelineController.Instance.SelectedItem is ContentItem)
Process.Start(PipelineController.Instance.GetFullPath(PipelineController.Instance.SelectedItem.OriginalPath));
}
private void CmdOpenItemWith_Executed(object sender, EventArgs e)
{
if (PipelineController.Instance.SelectedItem != null)
{
try
{
var filepath = PipelineController.Instance.GetFullPath(PipelineController.Instance.SelectedItem.OriginalPath);
var dialog = new OpenWithDialog(filepath);
dialog.ShowDialog(this);
}
catch
{
ShowError("Error", "An error occured while trying to launch an open with dialog.");
}
}
}
private void CmdOpenItemLocation_Executed(object sender, EventArgs e)
{
if (PipelineController.Instance.SelectedItem != null)
Process.Start(PipelineController.Instance.GetFullPath(PipelineController.Instance.SelectedItem.Location));
}
private void CmdOpenOutputItemLocation_Executed(object sender, EventArgs e)
{
if (PipelineController.Instance.SelectedItem != null)
{
var dir = Path.Combine(
PipelineController.Instance.ProjectItem.Location,
PipelineController.Instance.ProjectOutputDir,
PipelineController.Instance.SelectedItem.Location
);
dir = dir.Replace("$(Platform)", PipelineController.Instance.ProjectItem.Platform.ToString());
dir = dir.Replace("$(Configuration)", PipelineController.Instance.ProjectItem.Config);
dir = dir.Replace("$(Config)", PipelineController.Instance.ProjectItem.Config);
dir = dir.Replace("$(Profile)", PipelineController.Instance.ProjectItem.Profile.ToString());
if (Directory.Exists(dir))
Process.Start(dir);
else
ShowError("Directory Not Found", "The project output directory was not found, did you forget to build the project?");
}
}
private void CmdCopyAssetPath_Executed(object sender, EventArgs e)
{
PipelineController.Instance.CopyAssetPath();
}
private void CmdRebuildItem_Executed(object sender, EventArgs e)
{
PipelineController.Instance.RebuildItems();
}
#endregion
}
}