-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStartupWindow.xaml.cs
More file actions
360 lines (324 loc) · 9.66 KB
/
StartupWindow.xaml.cs
File metadata and controls
360 lines (324 loc) · 9.66 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
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Win32;
using Newtonsoft.Json;
//using System.Windows.Media;
//using MaterialDesignColors;
//using MaterialDesignThemes.Wpf;
namespace Imperial_Commander_Editor
{
/// <summary>
/// Interaction logic for StartupWindow.xaml
/// </summary>
public partial class StartupWindow : Window, INotifyPropertyChanged
{
bool _isBusy;
private GitHubResponse gitHubResponse = null;
public bool scrollVisible { get; set; }
public ObservableCollection<ProjectItem> projectCollection;
public bool isBusy { get { return _isBusy; } set { _isBusy = value; PC(); } }
public void PC( [CallerMemberName] string n = "" )
{
if ( !string.IsNullOrEmpty( n ) )
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( n ) );
}
public event PropertyChangedEventHandler PropertyChanged;
public StartupWindow()
{
InitializeComponent();
Utils.LoadAllCardData();
Utils.InitColors();
DataContext = this;
scrollVisible = false;
PopulateRecents();
formatVersion.Text = "Mission Format: " + Utils.formatVersion;
appVersion.Text = "Editor Version: " + Utils.appVersion;
//var paletteHelper = new PaletteHelper();
//var theme = paletteHelper.GetTheme();
////theme.SetBaseTheme( Theme.Dark );
//theme.SetBaseTheme( (BaseTheme)Theme.GetSystemTheme() );//.Dark );
//paletteHelper.SetTheme( theme );
busyStatus.Text = "Checking...";
if ( NetworkInterface.GetIsNetworkAvailable() )
Task.Run( StartVersionCheck );
else
{
busyStatus.Text = "Error Checking For Update";
DoStatus( 0 );
}
}
private void PopulateRecents()
{
projectCollection = new ObservableCollection<ProjectItem>();
projectLV.ItemsSource = projectCollection;
//poll Project folder for files and populate Recent list, sorted by newest first
var projects = FileManager.GetMRUProjects();
if ( projects != null )
{
foreach ( ProjectItem pi in projects )
{
projectCollection.Add( pi );
}
}
else
{
//throw new Exception( "Could not properly load mission projects." );
MessageBox.Show( "Could not load mission projects.", "Error::PopulateRecents()" );
}
}
private void Window_MouseDown( object sender, MouseButtonEventArgs e )
{
if ( e.LeftButton == MouseButtonState.Pressed )
DragMove();
}
private void newMissionButton_Click( object sender, RoutedEventArgs e )
{
MainWindow mainWindow = new();
mainWindow.Show();
Close();
}
private void cancelButton_Click( object sender, RoutedEventArgs e )
{
Application.Current.Shutdown();
}
private void projectLV_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
ProjectItem item = ((ListView)e.Source).SelectedItem as ProjectItem;
var project = FileManager.LoadMission( item.fullPathWithFilename );
if ( project != null )
{
MainWindow mainWindow = new( project );
mainWindow.Show();
Close();
}
}
private void loadMissionButton_Click( object sender, RoutedEventArgs e )
{
OpenFileDialog od = new();
od.InitialDirectory = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments ), "ImperialCommander" );
od.Filter = "Mission File|*.json";
od.Title = "Open Mission";
if ( od.ShowDialog() == true )
{
var filePath = od.FileName;
var project = FileManager.LoadMission( filePath );
if ( project != null )
{
MainWindow mainWindow = new( project );
mainWindow.Show();
Close();
}
}
}
private void removeButton_Click( object sender, RoutedEventArgs e )
{
ProjectItem pitem = (sender as Button).DataContext as ProjectItem;
string basePath = pitem.fullPathWithFilename;
var r = MessageBox.Show( $"Are you sure you want to PERMANENTLY DELETE this mission OR remove it from the Recent Mission List?\n\nPath: {basePath}\n\nYES = PERMANENTLY DELETE THE MISSION\nNO = ONLY Remove the Mission from the Recent Mission List\nCANCEL = Don't do ANYTHING, Cancel!", "Permanently Delete Mission OR Remove from Recent Mission List", MessageBoxButton.YesNoCancel );
if ( r == MessageBoxResult.Yes )
{
try
{
FileManager.RemoveFromMRU( basePath );
File.Delete( basePath );
PopulateRecents();
}
catch ( Exception ee )
{
MessageBox.Show( $"Could not delete file '{basePath}'.\r\r{ee.InnerException}", "Error Deleting File", MessageBoxButton.OK, MessageBoxImage.Error );
}
}
else if ( r == MessageBoxResult.No )
{
FileManager.RemoveFromMRU( basePath );
PopulateRecents();
}
}
private async Task CheckVersion()
{
// /repos/{owner}/{repo}/releases
var web = new HttpClient();
web.DefaultRequestHeaders.Add( "User-Agent", "request" );
var result = await web.GetAsync( "https://api.github.com/repos/GlowPuff/ICEditor/releases/latest" );
if ( !result.IsSuccessStatusCode )
{
//Debug.Log( "network error" );
DoStatus( 0 );
gitHubResponse = null;
}
else
{
var response = await result.Content.ReadAsStringAsync();
//parse JSON response
gitHubResponse = JsonConvert.DeserializeObject<GitHubResponse>( response );
//remove beginning "v." and "." from tag_name
int ghVersion = int.Parse( gitHubResponse.tag_name.GetDigits() );
int v = int.Parse( Utils.appVersion.GetDigits() );
if ( ghVersion == v )
DoStatus( 1 );
else if ( v > ghVersion )
DoStatus( 3 );
else
DoStatus( 2, gitHubResponse.tag_name );
}
}
private async void StartVersionCheck()
{
//first check if internet is available
var ping = new Ping();
var reply = ping.Send( new IPAddress( new byte[] { 8, 8, 8, 8 } ), 5000 );
if ( reply.Status == IPStatus.Success )
{
//internet available, check for latest version
await CheckVersion();
}
else
{
DoStatus( 0 );
gitHubResponse = null;
}
}
/// <summary>
/// 0=red, 1=green, 2=yellow, 3=green(using a beta)
/// </summary>
private void DoStatus( int s, string version = "" )
{
Dispatcher.Invoke( () =>
{
//hide animated icon
busyIcon.Visibility = Visibility.Collapsed;
#if DEBUG
s = 3;
#endif
if ( s == 0 )
{
busyStatus.Text = "Error Checking For Update";
busyIconRed.Visibility = Visibility.Visible;
busyIconGreen.Visibility = Visibility.Collapsed;
busyIconYellow.Visibility = Visibility.Collapsed;
}
else if ( s == 1 )
{
busyStatus.Text = "Latest Version";
busyIconRed.Visibility = Visibility.Collapsed;
busyIconGreen.Visibility = Visibility.Visible;
busyIconYellow.Visibility = Visibility.Collapsed;
}
else if ( s == 2 )
{
busyStatus.Text = $"Update Available: {version}";
busyIconRed.Visibility = Visibility.Collapsed;
busyIconGreen.Visibility = Visibility.Collapsed;
busyIconYellow.Visibility = Visibility.Visible;
}
else if ( s == 3 )
{
busyStatus.Text = "BETA";
busyIconRed.Visibility = Visibility.Collapsed;
busyIconGreen.Visibility = Visibility.Visible;
busyIconYellow.Visibility = Visibility.Collapsed;
}
} );
}
private void toonEditorButton_Click( object sender, RoutedEventArgs e )
{
CharacterEditorWindow ced = new();
ced.Show();
Close();
}
private void packageButton_Click( object sender, RoutedEventArgs e )
{
CampaignPackager packager = new();
packager.Show();
Close();
}
private void Window_DragEnter( object sender, DragEventArgs e )
{
dropStatusText.Text = "DRAG AND DROP MISSION FILE";
if ( e.Data.GetDataPresent( DataFormats.FileDrop ) )
{
string[] filename = e.Data.GetData( DataFormats.FileDrop ) as string[];
if ( filename.Length == 1 && Path.GetExtension( filename[0] ) == ".json" )
{
e.Effects = DragDropEffects.Copy;
dropNotice.Visibility = Visibility.Visible;
}
}
else
{
e.Effects = DragDropEffects.None;
dropNotice.Visibility = Visibility.Hidden;
}
e.Handled = true;
}
private void Window_Drop( object sender, DragEventArgs e )
{
if ( e.Data.GetDataPresent( DataFormats.FileDrop ) )
{
string[] filename = e.Data.GetData( DataFormats.FileDrop ) as string[];
if ( filename.Length == 1 && Path.GetExtension( filename[0] ) == ".json" )
{
dropStatusText.Text = "OPENING MISSION...";
var task = new Task( () =>
{
var filePath = filename[0];
var project = FileManager.LoadMission( filePath );
if ( project != null )
{
Dispatcher.Invoke( () =>
{
MainWindow mainWindow = new( project );
mainWindow.Show();
Close();
} );
}
else
{
Dispatcher.Invoke( () =>
{
dropNotice.Visibility = Visibility.Hidden;
} );
}
} );
task.Start();
}
}
e.Handled = true;
}
private void Window_DragLeave( object sender, DragEventArgs e )
{
dropNotice.Visibility = Visibility.Hidden;
e.Handled = true;
}
private void Window_DragOver( object sender, DragEventArgs e )
{
string[] filename = e.Data.GetData( DataFormats.FileDrop ) as string[];
if ( filename.Length == 1 && Path.GetExtension( filename[0] ) == ".json" )
{
e.Effects = DragDropEffects.Copy;
dropNotice.Visibility = Visibility.Visible;
}
else
{
e.Effects = DragDropEffects.None;
dropNotice.Visibility = Visibility.Hidden;
}
e.Handled = true;
}
private void clearMRUBtn_Click( object sender, RoutedEventArgs e )
{
FileManager.ClearMRU();
PopulateRecents();
}
}
}