/[EmuXPortal]/branches/mono/EmuXPortal/Form1.cs
ViewVC logotype

Contents of /branches/mono/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 248 - (show annotations) (download)
Tue Aug 5 09:20:39 2014 UTC (5 years, 6 months ago) by william
Original Path: trunk/EmuXPortal/Form1.cs
File size: 34159 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22