/[EmuXPortal]/trunk/EmuXPortal/Form1.cs
ViewVC logotype

Contents of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 172 - (show annotations) (download)
Mon Aug 4 05:36:08 2014 UTC (6 years, 1 month ago) by william
File size: 24402 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using EmuXPortal.Api;
10 using EmuXPortal.Logging;
11 using System.Diagnostics;
12 using System.Reflection;
13 using System.Threading;
14
15 namespace EmuXPortal
16 {
17 public partial class Form1 : Form
18 {
19 IEmuConfig CurrentSelectedRom = null;
20 PlatformControl CurrentPlatformControl = null;
21 GameControl CurrentGameControl = null;
22 logger log = new logger();
23 public Form1()
24 {
25 InitializeComponent();
26 platform_flow.Dock = DockStyle.Fill;
27 rom_flow.Dock = DockStyle.Fill;
28 log.OpenLog();
29 }
30
31 private void Form1_Load(object sender, EventArgs e)
32 {
33 Config.LoadConfig(log);
34 Config.InitializePresentationForm(log,this);
35 }
36 private void Form1_Shown(object sender, EventArgs e) { platform_flow.Visible = true; Cursor.Hide(); }
37 void platform_ctrl_LostFocus(object sender, EventArgs e)
38 {
39 PlatformControl c = sender as PlatformControl;
40 c.BorderStyle = BorderStyle.None;
41 }
42
43 void platform_ctrl_GotFocus(object sender, EventArgs e)
44 {
45 PlatformControl c = sender as PlatformControl;
46 c.BorderStyle = BorderStyle.FixedSingle;
47 CurrentPlatformControl = c;
48 }
49 void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
50 {
51 GameControl c = sender as GameControl;
52
53 int changeAmount = 0;
54 int currentPosition = 0;
55
56 if (e.KeyCode == Keys.F && e.Modifiers.HasFlag(Keys.Control))
57 {
58 IRomConfig config = c.Tag as IRomConfig;
59 if (config == null)
60 {
61 log.WriteLine("Unable to add/remove from/to favorites (config is null): {0} [{1}]", config.RomTitle, config.RomFile);
62 }
63 else
64 {
65 var isFavorite = RomFavorite.IsFavorite(log, config);
66 if (isFavorite)
67 {
68 // add to favorites
69 log.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
70 if (!RomFavorite.RemoveFavorite(log, config))
71 {
72 log.WriteLine("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
73 }
74 else
75 {
76 log.WriteLine("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
77 }
78 }
79 else
80 {
81 // add to favorites
82 log.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
83 if (!RomFavorite.AddFavorite(log,config))
84 {
85 log.WriteLine("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
86 }
87 else
88 {
89 log.WriteLine("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
90 }
91 }
92 log.WriteLine("Updateing favorites");
93 if (!RomFavorite.UpdateFavorites(log))
94 {
95 log.WriteLine("Failed to update favorites");
96 }
97 else
98 {
99 log.WriteLine("Updated favorites");
100 }
101 }
102 return; // stop processing other keys
103
104 }
105 if (e.KeyCode == Keys.Home)
106 {
107 rom_flow.Controls[0].Select();
108 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
109 }
110 if (e.KeyCode == Keys.End)
111 {
112 rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
113 rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
114 }
115 if (e.KeyCode == Keys.PageUp)
116 {
117 changeAmount = rom_flow.VerticalScroll.LargeChange;
118 currentPosition = rom_flow.VerticalScroll.Value;
119 if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
120 {
121 try
122 {
123 rom_flow.VerticalScroll.Value += changeAmount;
124 }
125 catch
126 {
127 rom_flow.Controls[0].Select();
128 rom_flow.ScrollControlIntoView(platform_flow.Controls[0]);
129 rom_flow.PerformLayout();
130 return;
131 }
132 }
133 else
134 {
135 rom_flow.Controls[0].Select();
136 rom_flow.ScrollControlIntoView(platform_flow.Controls[0]);
137 }
138 GameControl s = game_ctrl_get_last_visible();
139 s.Select();
140 rom_flow.ScrollControlIntoView(s);
141 rom_flow.PerformLayout();
142 }
143 if (e.KeyCode == Keys.PageDown)
144 {
145 changeAmount = rom_flow.VerticalScroll.LargeChange;
146 currentPosition = rom_flow.VerticalScroll.Value;
147 if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
148 {
149 rom_flow.VerticalScroll.Value += changeAmount;
150 }
151 else
152 {
153 rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
154 }
155 GameControl s = game_ctrl_get_last_visible();
156 s.Select();
157 rom_flow.ScrollControlIntoView(s);
158 rom_flow.PerformLayout();
159 }
160
161 if (e.KeyCode == Keys.Enter)
162 {
163 IRomConfig config = c.Tag as IRomConfig;
164
165 Process p = new Process();
166
167 p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
168 p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(log, config) : config.Config.GameExeArgs;
169 p.Start();
170
171 // minimize EmuXPortal
172 this.WindowState = FormWindowState.Minimized;
173 // wait for exit of game
174 p.WaitForExit();
175 // maximize EmuXPortal
176 this.WindowState = FormWindowState.Maximized;
177 }
178 if (e.KeyCode == Keys.Back)
179 {
180 rom_flow.Visible = false;
181 platform_flow.Visible = true;
182 }
183
184 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
185 (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
186 {
187 char t = (char)e.KeyCode;
188 GameControl ctrl = (rom_flow.GetNextControl(CurrentPlatformControl, true) as GameControl);
189 if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
190 bool found = false;
191 GameControl pc = CurrentGameControl;
192 bool wrapped = false;
193 bool not_found = true;
194 while (!found)
195 {
196 if (wrapped)
197 {
198 foreach (Control ctl in rom_flow.Controls)
199 {
200 GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
201 }
202 if (not_found) { found = true; }
203 }
204 ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
205 if (ctrl == null)
206 {
207 ctrl = rom_flow.Controls[0] as GameControl;
208 wrapped = true;
209 }
210 if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
211 {
212 rom_flow.ScrollControlIntoView(ctrl);
213 ctrl.Select();
214 found = true;
215 }
216 pc = ctrl;
217 }
218 }
219 }
220 void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
221 {
222 PlatformControl c = sender as PlatformControl;
223 int changeAmount = 0;
224 int currentPosition = 0;
225 if (e.KeyCode == Keys.Home)
226 {
227 platform_flow.Controls[0].Select();
228 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
229 }
230 if (e.KeyCode == Keys.End)
231 {
232 platform_flow.Controls[platform_flow.Controls.Count -1].Select();
233 platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
234 }
235 if (e.KeyCode == Keys.PageUp)
236 {
237
238
239 changeAmount = platform_flow.VerticalScroll.LargeChange;
240 currentPosition = platform_flow.VerticalScroll.Value;
241 if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
242 {
243 platform_flow.VerticalScroll.Value -= changeAmount;
244 }
245 else
246 {
247 platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
248 }
249 PlatformControl s = platform_ctrl_get_last_visible();
250 s.Select();
251 platform_flow.ScrollControlIntoView(s);
252 platform_flow.PerformLayout();
253 }
254 if (e.KeyCode == Keys.PageDown)
255 {
256 changeAmount = platform_flow.VerticalScroll.LargeChange;
257 currentPosition = platform_flow.VerticalScroll.Value;
258 if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
259 {
260 try
261 {
262 platform_flow.VerticalScroll.Value += changeAmount;
263 }
264 catch
265 {
266 platform_flow.Controls[0].Select();
267 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
268 rom_flow.PerformLayout();
269 return;
270 }
271 }
272 else
273 {
274 platform_flow.Controls[0].Select();
275 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
276 }
277 PlatformControl s = platform_ctrl_get_last_visible();
278 s.Select();
279 platform_flow.ScrollControlIntoView(s);
280 platform_flow.PerformLayout();
281 }
282 if (e.KeyCode == Keys.Enter)
283 {
284 // load this platform
285 platform_flow.Visible = false;
286 CurrentSelectedRom = c.Tag as IEmuConfig;
287 rom_flow.Visible = true;
288 rom_flow.BringToFront();
289 }
290 if (e.KeyCode == Keys.Back)
291 {
292 this.Close();
293 }
294 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
295 (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
296 {
297 char t = (char)e.KeyCode;
298 PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
299 if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
300 bool found = false;
301 PlatformControl pc = CurrentPlatformControl;
302 bool wrapped = false;
303 bool not_found = true;
304 while (!found)
305 {
306 if (wrapped)
307 {
308 foreach (Control ctl in platform_flow.Controls)
309 {
310 PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
311 }
312 if (not_found) { found = true; }
313 }
314 ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
315 if (ctrl == null)
316 {
317 ctrl = platform_flow.Controls[0] as PlatformControl;
318 wrapped = true;
319 }
320 if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
321 {
322 platform_flow.ScrollControlIntoView(ctrl);
323 ctrl.Select();
324 found = true;
325 }
326 pc = ctrl;
327 }
328 }
329 }
330
331 private void platform_flow_VisibleChanged(object sender, EventArgs e)
332 {
333 if (!platform_flow.Visible) return;
334 platform_flow.Controls.Clear();
335 platform_flow.BringToFront();
336 Stopwatch t = new Stopwatch();
337 t.Start();
338 platformWorker.RunWorkerAsync(t);
339 }
340
341 private void rom_flow_VisibleChanged(object sender, EventArgs e)
342 {
343 if (!rom_flow.Visible) return;
344 rom_flow.Controls.Clear();
345 rom_flow.BringToFront();
346 Stopwatch t = new Stopwatch();
347 t.Start();
348 gameWorker.RunWorkerAsync(t);
349 }
350
351 void game_ctrl_LostFocus(object sender, EventArgs e)
352 {
353 GameControl c = sender as GameControl;
354 c.BorderStyle = BorderStyle.None;
355 }
356
357 void game_ctrl_GotFocus(object sender, EventArgs e)
358 {
359 GameControl c = sender as GameControl;
360 c.BorderStyle = BorderStyle.FixedSingle;
361 CurrentGameControl = c;
362 }
363
364 private GameControl game_ctrl_get_last_visible()
365 {
366 GameControl s = new GameControl();
367 foreach (GameControl c in rom_flow.Controls)
368 {
369 if (c.Bounds.IntersectsWith(rom_flow.Bounds))
370 s = c;
371 }
372 return s;
373 }
374 private PlatformControl platform_ctrl_get_last_visible()
375 {
376 PlatformControl s = new PlatformControl();
377 foreach (PlatformControl c in platform_flow.Controls)
378 {
379 if (c.Bounds.IntersectsWith(platform_flow.Bounds))
380 s = c;
381 }
382 return s;
383 }
384
385
386 #region Background Workers
387
388 private int GetFormWidth()
389 {
390 if (this.InvokeRequired)
391 {
392 return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
393 }
394 else
395 {
396 return this.Width;
397 }
398 }
399 //private void AddPlatformControl(Control c)
400 //{
401 // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
402 // else
403 // {
404 // platform_flow.Controls.Add(c);
405 // }
406 //}
407 //private void UpdatePlatformControls()
408 //{
409 // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
410 // //else { this.Update(); }
411 //}
412 //private void AddGameControl(Control c)
413 //{
414 // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
415 // else
416 // {
417 // rom_flow.Controls.Add(c);
418 // }
419 //}
420 //private void UpdateGameControls()
421 //{
422 // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
423 // //else { this.Update(); }
424 //}
425 #region gameWorker
426 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
427 private object gameimage_lock = new object();
428 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
429 {
430 Stopwatch t = e.Argument as Stopwatch;
431 RomParser parser = new RomParser(log, CurrentSelectedRom);
432
433 ProgressBar bar = new ProgressBar();
434 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(bar); }); }
435 else { rom_flow.Controls.Add(bar); }
436 //UpdateGameControls();
437 //Application.DoEvents();
438 //Thread.Sleep(10);
439 bar.Invoke(new MethodInvoker(delegate
440 {
441 //bar.Message = "Please Wait...";
442 //bar.ShowPercentageLabel = true;
443 bar.Margin = new System.Windows.Forms.Padding(0);
444 bar.Size = new Size(GetFormWidth() - 25, 100);
445 }));
446
447 double count = 0;
448 double total_count = parser.Roms.Count;
449 foreach (IRomConfig config in parser.Roms)
450 {
451 GameControl game_ctrl = new GameControl();
452 game_ctrl.Dock = DockStyle.Top;
453 game_ctrl.Width = this.Width - 10;
454 game_ctrl.Tag = config;
455 try
456 {
457 lock (gameimage_lock)
458 {
459 game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
460 config.ReleaseRomImageResource();
461 }
462 }
463 catch (Exception ex)
464 {
465 throw ex;
466 }
467 game_ctrl.GameName = config.RomTitle;
468 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
469 game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
470 game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
471 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
472 else { rom_flow.Controls.Add(game_ctrl); }
473 //UpdateGameControls();
474 //Application.DoEvents();
475 //int percent = (int)(100.0 * (count / total_count));
476 //log.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
477 bar.Invoke(new MethodInvoker(delegate
478 {
479 bar.Value = (int)(100.0 * (count / total_count));
480 }));
481 count++;
482 }
483 e.Result = t;
484 parser.Dispose();
485 }
486 private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
487 private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
488 {
489 Stopwatch t = e.Result as Stopwatch;
490 rom_flow.Controls.RemoveAt(0);
491 rom_flow.Controls[0].Select();
492 (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
493 t.Stop();
494 log.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
495 }
496 #endregion
497 #region platformWorker
498 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
499 private object platformimage_lock = new object();
500 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
501 {
502 Stopwatch t = e.Argument as Stopwatch;
503 PlatformParser parser = new PlatformParser(log, Config.RomPath);
504 double count = 0;
505 double total_count = parser.Platforms.Count;
506 ProgressBar bar = new ProgressBar();
507 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(bar); }); }
508 else { platform_flow.Controls.Add(bar); }
509 //UpdatePlatformControls();
510 //Application.DoEvents();
511 //Thread.Sleep(10);
512 bar.Invoke(new MethodInvoker(delegate
513 {
514 bar.Margin = new System.Windows.Forms.Padding(0);
515 bar.Size = new Size(GetFormWidth() - 25, 100);
516 }));
517 foreach (IEmuConfig config in parser.Platforms)
518 {
519 PlatformControl platform_ctrl = new PlatformControl();
520 platform_ctrl.Dock = DockStyle.Top;
521 platform_ctrl.Width = this.Width - 10;
522 platform_ctrl.Tag = config;
523 try
524 {
525 lock (platformimage_lock)
526 {
527 platform_ctrl.PlatformImage = config.PlatformImage == null ? (Image)DefaultPlatformImage.Clone() : (Image)config.PlatformImage.Clone();
528 config.ReleasePlatformImageResource();
529 }
530 }
531 catch (Exception ex)
532 {
533 throw ex;
534 }
535 platform_ctrl.PlatformName = config.ToString();
536 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
537 platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
538 platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
539 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
540 else { platform_flow.Controls.Add(platform_ctrl); }
541 //Application.DoEvents();
542 //int percent = (int)(100.0 * (count / total_count));
543 //log.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
544 bar.Invoke(new MethodInvoker(delegate
545 {
546 bar.Value = (int)(100.0 * (count / total_count));
547 }));
548 count++;
549 }
550 e.Result = t;
551 parser.Dispose();
552 }
553 private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
554 private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
555 {
556 Stopwatch t = e.Result as Stopwatch;
557 platform_flow.Controls.RemoveAt(0);
558 platform_flow.Controls[0].Select();
559 (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
560 log.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
561 }
562 #endregion
563
564 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
565 {
566 Cursor.Show();
567 log.CloseLog();
568 }
569 #endregion
570 }
571 }

  ViewVC Help
Powered by ViewVC 1.1.22