ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/branches/mono/EmuXPortal/Form1.cs
Revision: 264
Committed: Thu Mar 17 04:23:49 2016 UTC (7 years, 2 months ago) by william
File size: 33593 byte(s)
Log Message:
update emuxportal for building under mono

File Contents

# Content
1 //#define DISABLE_CURSOR_HIDE // when this is present, the cursor will not be hidden
2 //#define DISABLE_PROGRESS_PERCENTAGE_MESSASGE // when this is present, no progress percent message will be shown on any progressbar
3 #define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
4 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
5 using System;
6 using System.Collections.Generic;
7 using System.ComponentModel;
8 using System.Data;
9 using System.Drawing;
10 using System.Linq;
11 using System.Text;
12 using System.Windows.Forms;
13 using EmuXPortal.Api;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Threading;
17 using Utilities.TransparentControls;
18 using Enterprise.Logging;
19 using System.IO;
20
21 namespace EmuXPortal
22 {
23 public partial class Form1 : Form
24 {
25 static readonly Color SELECTED_CONTROL_BACKCOLOR = Color.SteelBlue;
26 const float PROGRESS_BAR_FONT_SIZE = 24;
27 private delegate Font Delegate_GetFormFont();
28 private delegate Font Delegate_ResizeFont(Font font, float size);
29 IEmuConfig CurrentSelectedRom = null;
30 PlatformControl CurrentPlatformControl = null;
31 GameControl CurrentGameControl = null;
32
33 #region unhandled exception support
34 static void Application_Unhandled_ThreadException(object sender, ThreadExceptionEventArgs e)
35 {
36 UnhandledExceptionEventArgs uea = new UnhandledExceptionEventArgs(e.Exception, false);
37 UnhandledExceptionEventHandler(sender, uea);
38 }
39 static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs args)
40 {
41 Exception ex = (args.ExceptionObject as Exception);
42 if (sender == null)
43 {
44 gLog.Error.WriteLine("Caught an unhandled exception from an unkown source");
45 }
46 else
47 {
48 gLog.Error.WriteLine("Caught an unhandled exception from type: {0}", sender.GetType().Name);
49 }
50
51 if (ex == null)
52 {
53 gLog.Error.WriteLine("The exception object was null -- it probably is not derived from System.Exception");
54 }
55 else
56 {
57 ex = ex.GetBaseException();
58 gLog.Error.WriteLine("{0}:", ex.GetType().Name);
59 gLog.Verbose.Error.WriteLine(ex.ToString());
60 }
61
62 }
63 #endregion
64 public Form1()
65 {
66 InitializeComponent();
67
68
69 #region logging support
70 string log_path = Application.StartupPath;
71 string log_filename = string.Format("{0}.log", typeof(Form1).Assembly.GetName().Name);
72 FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
73
74 gLog.CreateLog(log_file.FullName, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
75 #if DEBUG
76 LogLevel gLevel = gLog.LogLevel;
77 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
78 gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
79 #else
80 gLevel |= LogLevel.kLogLevel_VerboseDebug;
81 #endif
82 gLevel |= LogLevel.kLogLevel_Debug;
83 gLog.SetLogLevel(gLevel);
84 #else
85 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
86 // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
87 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
88 gLevel &= ~LogLevel.kLogLevel_Debug;
89 #else
90 gLevel |= LogLevel.kLogLevel_Debug;
91 #endif
92 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
93 gLog.SetLogLevel(gLevel);
94 #endif
95 #endregion
96
97 #region unhandled exception support
98 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
99 Application.ThreadException += Application_Unhandled_ThreadException;
100 #endregion
101
102 platform_flow.Dock = DockStyle.Fill;
103 rom_flow.Dock = DockStyle.Fill;
104 }
105
106
107
108
109 #region logging support
110 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
111 void OnLogFlush(string logmessage)
112 {
113 //if (this.IsDisposed) { return; }
114 ////UpdateStatus(logmessage);
115 ////UpdateLogOutput(logmessage);
116 //Application.DoEvents();
117 }
118 #endregion
119
120 private void Form1_Load(object sender, EventArgs e)
121 {
122 Config.LoadConfig();
123 Config.InitializePresentationForm(this);
124 }
125 private void Form1_Shown(object sender, EventArgs e)
126 {
127 platform_flow.Visible = true;
128 #if !DISABLE_CURSOR_HIDE
129 Cursor.Hide();
130 #else
131 Cursor.Show();
132 #endif
133 }
134 void platform_ctrl_LostFocus(object sender, EventArgs e)
135 {
136 PlatformControl c = sender as PlatformControl;
137 c.BorderStyle = BorderStyle.None;
138 c.BackColor = this.BackColor;
139 }
140
141 void platform_ctrl_GotFocus(object sender, EventArgs e)
142 {
143 PlatformControl c = sender as PlatformControl;
144 c.BorderStyle = BorderStyle.FixedSingle;
145 c.BackColor = SELECTED_CONTROL_BACKCOLOR;
146 CurrentPlatformControl = c;
147 }
148 void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
149 {
150 GameControl c = sender as GameControl;
151
152 int changeAmount = 0;
153 int currentPosition = 0;
154 if (e.KeyCode == Keys.F && (e.Modifiers & Keys.Control) == Keys.Control)
155 {
156 IRomConfig config = c.Tag as IRomConfig;
157 if (config == null)
158 {
159 gLog.Error.WriteLine("Unable to add/remove from/to favorites (config is null): {0} [{1}]", config.RomTitle, config.RomFile);
160 }
161 else
162 {
163 var isFavorite = RomFavorite.IsFavorite(config);
164 if (isFavorite)
165 {
166 // add to favorites
167 gLog.Debug.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
168 if (!RomFavorite.RemoveFavorite(config))
169 {
170 gLog.Error.WriteLine("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
171 }
172 else
173 {
174 gLog.Info.WriteLine("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
175 if (config.Config.PlatformNameShort == "Favorites")
176 {
177 var parent = c.Parent;
178 if (parent != null)
179 {
180 parent.Controls.Remove(c);
181 if (parent.Controls.Count > 0)
182 {
183 var next_ctrl = parent.Controls[0];
184 if (next_ctrl != null)
185 {
186 next_ctrl.Select();
187 }
188 }
189 }
190 }
191 }
192 }
193 else
194 {
195 // add to favorites
196 gLog.Debug.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
197 if (!RomFavorite.AddFavorite(config))
198 {
199 gLog.Error.WriteLine("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
200 }
201 else
202 {
203 gLog.Info.WriteLine("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
204 }
205 }
206 gLog.Debug.WriteLine("Updateing favorites");
207 if (!RomFavorite.UpdateFavorites())
208 {
209 gLog.Error.WriteLine("Failed to update favorites");
210 }
211 else
212 {
213 gLog.Info.WriteLine("Updated favorites");
214 }
215 }
216 return; // stop processing other keys
217
218 }
219 if (e.KeyCode == Keys.Home)
220 {
221 rom_flow.SuspendLayout();
222 rom_flow.Controls[0].Select();
223 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
224 rom_flow.ResumeLayout(false);
225 }
226 if (e.KeyCode == Keys.End)
227 {
228 rom_flow.SuspendLayout();
229 rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
230 rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
231 rom_flow.ResumeLayout(false);
232 }
233 if (e.KeyCode == Keys.PageUp)
234 {
235 rom_flow.SuspendLayout();
236 changeAmount = rom_flow.VerticalScroll.LargeChange;
237 currentPosition = rom_flow.VerticalScroll.Value;
238 if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
239 {
240 try
241 {
242 rom_flow.VerticalScroll.Value -= changeAmount;
243 }
244 catch
245 {
246 rom_flow.Controls[0].Select();
247 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
248 rom_flow.PerformLayout();
249 return;
250 }
251 }
252 else
253 {
254 rom_flow.Controls[0].Select();
255 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
256 }
257 GameControl s = game_ctrl_get_last_visible();
258 s.Select();
259 rom_flow.ScrollControlIntoView(s);
260 rom_flow.ResumeLayout(false);
261 }
262 if (e.KeyCode == Keys.PageDown)
263 {
264 rom_flow.SuspendLayout();
265 changeAmount = rom_flow.VerticalScroll.LargeChange;
266 currentPosition = rom_flow.VerticalScroll.Value;
267 if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
268 {
269 try
270 {
271 rom_flow.VerticalScroll.Value += changeAmount;
272 }
273 catch
274 {
275 rom_flow.Controls[0].Select();
276 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
277 rom_flow.PerformLayout();
278 return;
279 }
280 }
281 else
282 {
283 rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
284 }
285 GameControl s = game_ctrl_get_last_visible();
286 s.Select();
287 rom_flow.ScrollControlIntoView(s);
288 rom_flow.ResumeLayout(false);
289 }
290
291 if (e.KeyCode == Keys.Enter)
292 {
293 IRomConfig config = c.Tag as IRomConfig;
294
295 Process p = new Process();
296
297 p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
298 p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(config) : config.Config.GameExeArgs;
299 p.Start();
300
301 // minimize EmuXPortal
302 this.WindowState = FormWindowState.Minimized;
303 // wait for exit of game
304 p.WaitForExit();
305 // maximize EmuXPortal
306 this.WindowState = FormWindowState.Maximized;
307 }
308 if (e.KeyCode == Keys.Back)
309 {
310 rom_flow.Visible = false;
311 platform_flow.Visible = true;
312 }
313
314 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
315 (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
316 {
317 rom_flow.SuspendLayout();
318 char t = (char)e.KeyCode;
319 GameControl ctrl = (rom_flow.GetNextControl(CurrentGameControl, true) as GameControl);
320 if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
321 bool found = false;
322 GameControl pc = CurrentGameControl;
323 bool wrapped = false;
324 bool not_found = true;
325 while (!found)
326 {
327 if (wrapped)
328 {
329 foreach (Control ctl in rom_flow.Controls)
330 {
331 GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
332 }
333 if (not_found) { found = true; }
334 }
335 ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
336 if (ctrl == null)
337 {
338 ctrl = rom_flow.Controls[0] as GameControl;
339 wrapped = true;
340 }
341 if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
342 {
343 rom_flow.ScrollControlIntoView(ctrl);
344 ctrl.Select();
345 found = true;
346 }
347 pc = ctrl;
348 }
349 rom_flow.ResumeLayout(false);
350 }
351 }
352 void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
353 {
354 PlatformControl c = sender as PlatformControl;
355 int changeAmount = 0;
356 int currentPosition = 0;
357 if (e.KeyCode == Keys.Home)
358 {
359 platform_flow.SuspendLayout();
360 platform_flow.Controls[0].Select();
361 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
362 platform_flow.ResumeLayout(false);
363 }
364 if (e.KeyCode == Keys.End)
365 {
366 platform_flow.SuspendLayout();
367 platform_flow.Controls[platform_flow.Controls.Count -1].Select();
368 platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
369 platform_flow.ResumeLayout(false);
370 }
371 if (e.KeyCode == Keys.PageUp)
372 {
373 platform_flow.SuspendLayout();
374 changeAmount = platform_flow.VerticalScroll.LargeChange;
375 currentPosition = platform_flow.VerticalScroll.Value;
376 if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
377 {
378 platform_flow.VerticalScroll.Value -= changeAmount;
379 }
380 else
381 {
382 platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
383 }
384 PlatformControl s = platform_ctrl_get_last_visible();
385 s.Select();
386 platform_flow.ScrollControlIntoView(s);
387 platform_flow.ResumeLayout(false);
388 }
389 if (e.KeyCode == Keys.PageDown)
390 {
391 platform_flow.SuspendLayout();
392 changeAmount = platform_flow.VerticalScroll.LargeChange;
393 currentPosition = platform_flow.VerticalScroll.Value;
394 if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
395 {
396 try
397 {
398 platform_flow.VerticalScroll.Value += changeAmount;
399 }
400 catch
401 {
402 platform_flow.Controls[0].Select();
403 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
404 rom_flow.PerformLayout();
405 return;
406 }
407 }
408 else
409 {
410 platform_flow.Controls[0].Select();
411 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
412 }
413 PlatformControl s = platform_ctrl_get_last_visible();
414 s.Select();
415 platform_flow.ScrollControlIntoView(s);
416 platform_flow.ResumeLayout(false);
417 }
418 if (e.KeyCode == Keys.Enter)
419 {
420 // load this platform
421 platform_flow.Visible = false;
422 CurrentSelectedRom = c.Tag as IEmuConfig;
423 rom_flow.Visible = true;
424 rom_flow.BringToFront();
425 }
426 if (e.KeyCode == Keys.Back)
427 {
428 this.Close();
429 }
430 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
431 (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
432 {
433 platform_flow.SuspendLayout();
434 char t = (char)e.KeyCode;
435 PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
436 if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
437 bool found = false;
438 PlatformControl pc = CurrentPlatformControl;
439 bool wrapped = false;
440 bool not_found = true;
441 while (!found)
442 {
443 if (wrapped)
444 {
445 foreach (Control ctl in platform_flow.Controls)
446 {
447 PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
448 }
449 if (not_found) { found = true; }
450 }
451 ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
452 if (ctrl == null)
453 {
454 ctrl = platform_flow.Controls[0] as PlatformControl;
455 wrapped = true;
456 }
457 if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
458 {
459 platform_flow.ScrollControlIntoView(ctrl);
460 ctrl.Select();
461 found = true;
462 }
463 pc = ctrl;
464 }
465 platform_flow.ResumeLayout(false);
466 }
467 }
468
469 private void platform_flow_VisibleChanged(object sender, EventArgs e)
470 {
471 if (!platform_flow.Visible) return;
472 platform_flow.Controls.Clear();
473 platform_flow.BringToFront();
474 Stopwatch t = new Stopwatch();
475 t.Start();
476 platformWorker.RunWorkerAsync(t);
477 }
478
479 private void rom_flow_VisibleChanged(object sender, EventArgs e)
480 {
481 if (!rom_flow.Visible) return;
482 rom_flow.Controls.Clear();
483 rom_flow.BringToFront();
484 Stopwatch t = new Stopwatch();
485 t.Start();
486 gameWorker.RunWorkerAsync(t);
487 }
488
489 void game_ctrl_LostFocus(object sender, EventArgs e)
490 {
491 GameControl c = sender as GameControl;
492 c.BorderStyle = BorderStyle.None;
493 c.BackColor = this.BackColor;
494 }
495
496 void game_ctrl_GotFocus(object sender, EventArgs e)
497 {
498 GameControl c = sender as GameControl;
499 c.BorderStyle = BorderStyle.FixedSingle;
500 c.BackColor = SELECTED_CONTROL_BACKCOLOR;
501 CurrentGameControl = c;
502 }
503
504 private GameControl game_ctrl_get_last_visible()
505 {
506 GameControl s = new GameControl();
507 foreach (GameControl c in rom_flow.Controls)
508 {
509 if (c.Bounds.IntersectsWith(rom_flow.Bounds))
510 s = c;
511 }
512 return s;
513 }
514 private PlatformControl platform_ctrl_get_last_visible()
515 {
516 PlatformControl s = new PlatformControl();
517 foreach (PlatformControl c in platform_flow.Controls)
518 {
519 if (c.Bounds.IntersectsWith(platform_flow.Bounds))
520 s = c;
521 }
522 return s;
523 }
524
525
526 #region Background Workers
527
528 private int GetFormWidth()
529 {
530 if (this.InvokeRequired)
531 {
532 return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
533 }
534 else
535 {
536 return this.Width;
537 }
538 }
539 private Font GetFormFont()
540 {
541 if (this.InvokeRequired)
542 {
543 return (this.Invoke(new Delegate_GetFormFont(GetFormFont)) as Font);
544 }
545 else
546 {
547 return this.Font;
548 }
549 }
550
551 private Font ResizeFont(Font font, float size)
552 {
553 if (this.InvokeRequired)
554 {
555 return (this.Invoke(new Delegate_ResizeFont(ResizeFont), new object[] { font, size })as Font);
556 }
557 else
558 {
559 return new Font(font.FontFamily, size);
560 }
561 }
562
563 //private void AddPlatformControl(Control c)
564 //{
565 // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
566 // else
567 // {
568 // platform_flow.Controls.Add(c);
569 // }
570 //}
571 //private void UpdatePlatformControls()
572 //{
573 // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
574 // //else { this.Update(); }
575 //}
576 //private void AddGameControl(Control c)
577 //{
578 // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
579 // else
580 // {
581 // rom_flow.Controls.Add(c);
582 // }
583 //}
584 //private void UpdateGameControls()
585 //{
586 // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
587 // //else { this.Update(); }
588 //}
589 #region gameWorker
590 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
591 private object gameimage_lock = new object();
592 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
593 {
594 Stopwatch t = e.Argument as Stopwatch;
595 RomParser parser = new RomParser(CurrentSelectedRom);
596
597 CustomProgressBar bar = new CustomProgressBar();
598 bar.Font = ResizeFont(GetFormFont(), PROGRESS_BAR_FONT_SIZE);
599 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
600 bar.ShowPercentageLabel = false;
601 #endif
602 bar.ProgressColor = Color.LimeGreen;
603 bar.Dock = DockStyle.Top;
604
605 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.SuspendLayout(); }); } else { rom_flow.SuspendLayout(); }
606
607 if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Add(bar); }); }
608 else { this.Controls.Add(bar); }
609 bar.Invoke(new MethodInvoker(delegate { bar.Margin = new System.Windows.Forms.Padding(0); bar.Size = new Size(GetFormWidth() - 25, 100); }));
610
611 double count = 0;
612 double total_count = parser.Roms.Count;
613 foreach (IRomConfig config in parser.Roms)
614 {
615 bar.Invoke(new MethodInvoker(delegate { bar.Value = (int)(100.0 * (count / total_count)); }));
616 GameControl game_ctrl = new GameControl();
617 game_ctrl.Font = GetFormFont();
618 game_ctrl.Dock = DockStyle.Top;
619 game_ctrl.Width = this.Width - 10;
620 game_ctrl.Tag = config;
621 try
622 {
623 lock (gameimage_lock)
624 {
625 game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
626 config.ReleaseRomImageResource();
627 }
628 }
629 catch (Exception ex)
630 {
631 throw ex;
632 }
633 if (CurrentSelectedRom.PlatformNameShort == "Favorites")
634 {
635 //game_ctrl.GameName = config.RomTitle;
636 game_ctrl.GameName = RomFavorite.GetRomTitleFromConfig(config);
637 }
638 else
639 {
640 game_ctrl.GameName = config.RomTitle;
641 }
642 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
643 game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
644 game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
645 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
646 else { rom_flow.Controls.Add(game_ctrl); }
647 count++;
648 }
649 bar.Invoke(new MethodInvoker(delegate { bar.Value = 1; bar.Update(); }));
650 if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Remove(bar); }); } else { this.Controls.Remove(bar); }
651 e.Result = t;
652 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.ResumeLayout(false); }); } else { rom_flow.ResumeLayout(false); }
653 parser.Dispose();
654 }
655 private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
656 private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
657 {
658 Stopwatch t = e.Result as Stopwatch;
659 if (rom_flow.Controls.Count == 0)
660 {
661 GameControl game_ctrl = new GameControl();
662 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
663 game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
664 game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
665 game_ctrl.Font = GetFormFont();
666 game_ctrl.Dock = DockStyle.Top;
667 game_ctrl.Width = this.Width - 10;
668 game_ctrl.GameName = "You haven't favorited any games, select a game and then press CTRL+F to favorite it";
669 rom_flow.Controls.Add(game_ctrl);
670 }
671 rom_flow.Controls[0].Select();
672 t.Stop();
673 gLog.Profiler.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
674 }
675 #endregion
676 #region platformWorker
677 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
678 private object platformimage_lock = new object();
679 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
680 {
681 Stopwatch t = e.Argument as Stopwatch;
682 PlatformParser parser = new PlatformParser(Config.RomPath);
683 double count = 0;
684 double total_count = parser.Platforms.Count;
685 CustomProgressBar bar = new CustomProgressBar();
686 bar.Font = ResizeFont(GetFormFont(), PROGRESS_BAR_FONT_SIZE);
687 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
688 bar.ShowPercentageLabel = false;
689 #endif
690 bar.ProgressColor = Color.LimeGreen;
691 bar.Dock = DockStyle.Top;
692
693 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.SuspendLayout(); }); } else { platform_flow.SuspendLayout(); }
694
695 if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Add(bar); }); }
696 else { this.Controls.Add(bar); }
697 bar.Invoke(new MethodInvoker(delegate { bar.Margin = new System.Windows.Forms.Padding(0); bar.Size = new Size(GetFormWidth() - 25, 100); }));
698 foreach (IEmuConfig config in parser.Platforms)
699 {
700 bar.Invoke(new MethodInvoker(delegate { bar.Value = (int)(100.0 * (count / total_count)); }));
701
702 PlatformControl platform_ctrl = new PlatformControl();
703 platform_ctrl.Font = GetFormFont();
704 platform_ctrl.Dock = DockStyle.Top;
705 platform_ctrl.Width = this.Width - 10;
706 platform_ctrl.Tag = config;
707 try
708 {
709 lock (platformimage_lock)
710 {
711 platform_ctrl.PlatformImage = config.PlatformImage == null ? (Image)DefaultPlatformImage.Clone() : (Image)config.PlatformImage.Clone();
712 config.ReleasePlatformImageResource();
713 }
714 }
715 catch (Exception ex)
716 {
717 throw ex;
718 }
719 platform_ctrl.PlatformName = config.ToString();
720 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
721 platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
722 platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
723 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
724 else { platform_flow.Controls.Add(platform_ctrl); }
725 count++;
726 }
727 bar.Invoke(new MethodInvoker(delegate { bar.Value = 1; bar.Update(); }));
728 if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Remove(bar); }); } else { this.Controls.Remove(bar); }
729 e.Result = t;
730 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.ResumeLayout(false); }); } else { platform_flow.ResumeLayout(false); }
731 parser.Dispose();
732 }
733 private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
734 private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
735 {
736 Stopwatch t = e.Result as Stopwatch;
737 if (platform_flow.Controls.Count == 0)
738 {
739 PlatformControl platform_ctrl = new PlatformControl();
740 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
741 platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
742 platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
743 platform_ctrl.Font = GetFormFont();
744 platform_ctrl.Dock = DockStyle.Top;
745 platform_ctrl.Width = this.Width - 10;
746 platform_ctrl.PlatformName = string.Format("You don't have any roms in your rompath: '{0}'",Config.RomPath);
747 platform_flow.Controls.Add(platform_ctrl);
748 }
749 platform_flow.Controls[0].Select();
750 gLog.Profiler.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
751 }
752 #endregion
753
754 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
755 {
756 Cursor.Show();
757 }
758 #endregion
759 }
760 }