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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 296 - (show annotations) (download)
Fri Dec 15 10:47:15 2017 UTC (2 years, 2 months ago) by william
File size: 40303 byte(s)
+ Add support for runtime caching of roms
+ Add support to list the progress of rom loading

1 #define HAVE_X11_BORDERSTYLE_ERROR // indicates that there is an error with Control.set_InternalBorderStyle resulting in an X11 Error BadWindow (invalid Window parameter)
2
3 //#define DISABLE_CURSOR_HIDE // when this is present, the cursor will not be hidden
4 //#define DISABLE_PROGRESS_PERCENTAGE_MESSASGE // when this is present, no progress percent message will be shown on any progressbar
5
6 #define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
7 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
8 using System;
9 using System.Collections.Generic;
10 using System.ComponentModel;
11 using System.Data;
12 using System.Drawing;
13 using System.Linq;
14 using System.Text;
15 using System.Windows.Forms;
16 using EmuXPortal.Api;
17 using System.Diagnostics;
18 using System.Reflection;
19 using System.Threading;
20 using Utilities.TransparentControls;
21 using Enterprise.Logging;
22 using System.IO;
23 using Enterprise.CrossPlatform;
24
25 namespace EmuXPortal
26 {
27 public partial class Form1 : Form
28 {
29 static readonly Color SELECTED_CONTROL_BACKCOLOR = Color.SteelBlue;
30 const float PROGRESS_BAR_FONT_SIZE = 24;
31 private delegate Font Delegate_GetFormFont();
32 private delegate Font Delegate_ResizeFont(Font font, float size);
33 private IEmuConfig CurrentSelectedRom = null;
34 private PlatformControl CurrentPlatformControl = null;
35 private int SavedPlatformIndex= -1;
36 private GameControl CurrentGameControl = null;
37 private RomCache cache;
38
39 private CustomProgressBar progress_bar;
40
41
42 #region unhandled exception support
43 static void Application_Unhandled_ThreadException(object sender, ThreadExceptionEventArgs e)
44 {
45 UnhandledExceptionEventArgs uea = new UnhandledExceptionEventArgs(e.Exception, false);
46 UnhandledExceptionEventHandler(sender, uea);
47 }
48 static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs args)
49 {
50 Exception ex = (args.ExceptionObject as Exception);
51 if (sender == null)
52 {
53 gLog.Error.WriteLine("Caught an unhandled exception from an unkown source");
54 }
55 else
56 {
57 gLog.Error.WriteLine("Caught an unhandled exception from type: {0}", sender.GetType().Name);
58 }
59
60 if (ex == null)
61 {
62 gLog.Error.WriteLine("The exception object was null -- it probably is not derived from System.Exception");
63 }
64 else
65 {
66 ex = ex.GetBaseException();
67 gLog.Error.WriteLine("{0}:", ex.GetType().Name);
68 gLog.Verbose.Error.WriteLine(ex.ToString());
69 }
70
71 }
72 #endregion
73 public Form1()
74 {
75 InitializeComponent();
76 this.progress_bar = new CustomProgressBar();
77 this.KeyPreview = true;
78
79 #region logging support
80 string log_path = Application.StartupPath;
81 string log_filename = string.Format("{0}.log", typeof(Form1).Assembly.GetName().Name);
82 FileInfo log_file = new FileInfo(OSInfo.FormatPath(string.Format(@"{0}\{1}", log_path, log_filename)));
83
84 gLog.CreateLog(log_file.Name, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
85 #if DEBUG
86 LogLevel gLevel = gLog.LogLevel;
87 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
88 gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
89 #else
90 gLevel |= LogLevel.kLogLevel_VerboseDebug;
91 #endif
92 gLevel |= LogLevel.kLogLevel_Debug;
93 gLog.SetLogLevel(gLevel);
94 #else
95 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
96 // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
97 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
98 gLevel &= ~LogLevel.kLogLevel_Debug;
99 #else
100 gLevel |= LogLevel.kLogLevel_Debug;
101 #endif
102 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
103 gLog.SetLogLevel(gLevel);
104 #endif
105 #endregion
106
107 #region unhandled exception support
108 // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
109 // Application.ThreadException += Application_Unhandled_ThreadException;
110 #endregion
111
112 platform_flow.Dock = DockStyle.Fill;
113 rom_flow.Dock = DockStyle.Fill;
114
115 cache = new RomCache();
116 }
117
118
119
120
121 #region logging support
122 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
123 void OnLogFlush(string logmessage)
124 {
125 //if (this.IsDisposed) { return; }
126 ////UpdateStatus(logmessage);
127 ////UpdateLogOutput(logmessage);
128 //Application.DoEvents();
129 }
130 #endregion
131
132 private void Form1_Load(object sender, EventArgs e)
133 {
134 Config.LoadConfig();
135 Config.InitializePresentationForm(this);
136 }
137 private void Form1_Shown(object sender, EventArgs e)
138 {
139 platform_flow.Visible = true;
140 #if !DISABLE_CURSOR_HIDE
141 Cursor.Hide();
142 #else
143 Cursor.Show();
144 #endif
145 }
146 void platform_ctrl_LostFocus(object sender, EventArgs e)
147 {
148 PlatformControl c = sender as PlatformControl;
149 if (c != null) {
150 #if !HAVE_X11_BORDERSTYLE_ERROR
151 c.BorderStyle = BorderStyle.None;
152 #endif
153 c.BackColor = this.BackColor;
154 }
155 }
156
157 void platform_ctrl_GotFocus(object sender, EventArgs e)
158 {
159 PlatformControl c = sender as PlatformControl;
160 if (c != null) {
161 #if !HAVE_X11_BORDERSTYLE_ERROR
162 c.BorderStyle = BorderStyle.FixedSingle;
163 #endif
164 c.BackColor = SELECTED_CONTROL_BACKCOLOR;
165 CurrentPlatformControl = c;
166 }
167 }
168 void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
169 {
170 //gLog.Verbose.Debug.WriteLine ("game_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
171 GameControl c = sender as GameControl;
172
173 int changeAmount = 0;
174 int currentPosition = 0;
175 if (e.KeyCode == Keys.F && (e.Modifiers & Keys.Control) == Keys.Control)
176 {
177 IRomConfig config = c.Tag as IRomConfig;
178 if (config == null)
179 {
180 gLog.Error.WriteLine("Unable to add/remove from/to favorites (config is null): {0} [{1}]", config.RomTitle, config.RomFile);
181 }
182 else
183 {
184 var isFavorite = RomFavorite.IsFavorite(config);
185 if (isFavorite)
186 {
187 // add to favorites
188 //gLog.Debug.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
189 if (!RomFavorite.RemoveFavorite(config))
190 {
191 gLog.Error.WriteLine("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
192 }
193 else
194 {
195 gLog.Info.WriteLine("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
196 if (config.Config.PlatformNameShort == "Favorites")
197 {
198 var parent = c.Parent;
199 if (parent != null)
200 {
201 parent.Controls.Remove(c);
202 if (parent.Controls.Count > 0)
203 {
204 var next_ctrl = parent.Controls[0];
205 if (next_ctrl != null)
206 {
207 next_ctrl.Select();
208 }
209 }
210 }
211 }
212 }
213 }
214 else
215 {
216 // add to favorites
217 //gLog.Debug.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
218 if (!RomFavorite.AddFavorite(config))
219 {
220 gLog.Error.WriteLine("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
221 }
222 else
223 {
224 gLog.Info.WriteLine("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
225 }
226 }
227 //gLog.Debug.WriteLine("Updateing favorites");
228 if (!RomFavorite.UpdateFavorites())
229 {
230 gLog.Error.WriteLine("Failed to update favorites");
231 }
232 else
233 {
234 gLog.Info.WriteLine("Updated favorites");
235 }
236 }
237 return; // stop processing other keys
238
239 }
240 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
241 if (OSInfo.OSIsUnix) {
242 rom_flow.SuspendLayout ();
243 var ctl = game_ctrl_get_previous_control (true);
244 rom_flow.ScrollControlIntoView (ctl);
245 rom_flow.ResumeLayout (false);
246 }
247 }
248 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
249 if (OSInfo.OSIsUnix) {
250 rom_flow.SuspendLayout ();
251 var ctl = game_ctrl_get_next_control (true);
252 rom_flow.ScrollControlIntoView (ctl);
253 rom_flow.ResumeLayout (false);
254 }
255 }
256 if (e.KeyCode == Keys.Home)
257 {
258 rom_flow.SuspendLayout();
259 rom_flow.Controls[0].Select();
260 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
261 rom_flow.ResumeLayout(false);
262 }
263 if (e.KeyCode == Keys.End)
264 {
265 rom_flow.SuspendLayout();
266 rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
267 rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
268 rom_flow.ResumeLayout(false);
269 }
270 if (e.KeyCode == Keys.PageUp)
271 {
272 rom_flow.SuspendLayout();
273 changeAmount = rom_flow.VerticalScroll.LargeChange;
274 currentPosition = rom_flow.VerticalScroll.Value;
275 if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
276 {
277 try
278 {
279 rom_flow.VerticalScroll.Value -= changeAmount;
280 }
281 catch
282 {
283 rom_flow.Controls[0].Select();
284 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
285 rom_flow.PerformLayout();
286 return;
287 }
288 }
289 else
290 {
291 rom_flow.Controls[0].Select();
292 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
293 }
294 GameControl s = game_ctrl_get_last_visible();
295 s.Select();
296 rom_flow.ScrollControlIntoView(s);
297 rom_flow.ResumeLayout(false);
298 }
299 if (e.KeyCode == Keys.PageDown)
300 {
301 rom_flow.SuspendLayout();
302 changeAmount = rom_flow.VerticalScroll.LargeChange;
303 currentPosition = rom_flow.VerticalScroll.Value;
304 if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
305 {
306 try
307 {
308 rom_flow.VerticalScroll.Value += changeAmount;
309 }
310 catch
311 {
312 rom_flow.Controls[0].Select();
313 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
314 rom_flow.PerformLayout();
315 return;
316 }
317 }
318 else
319 {
320 rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
321 }
322 GameControl s = game_ctrl_get_last_visible();
323 s.Select();
324 rom_flow.ScrollControlIntoView(s);
325 rom_flow.ResumeLayout(false);
326 }
327
328 if (e.KeyCode == Keys.Enter)
329 {
330 IRomConfig config = c.Tag as IRomConfig;
331
332 Process p = new Process();
333
334 p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
335 p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(config) : config.Config.GameExeArgs;
336
337 //gLog.Verbose.Debug.WriteLine ("Emulator: {0}", p.StartInfo.FileName);
338 //gLog.Verbose.Debug.WriteLine ("Args: {0}", p.StartInfo.Arguments);
339 p.Start();
340
341 // minimize EmuXPortal
342 this.WindowState = FormWindowState.Minimized;
343 // wait for exit of game
344 p.WaitForExit();
345 // maximize EmuXPortal
346 this.WindowState = FormWindowState.Maximized;
347 }
348 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape)
349 {
350 SavedPlatformIndex = platform_flow.Controls.IndexOf (CurrentPlatformControl);
351 rom_flow.Visible = false;
352 platform_flow.Visible = true;
353 }
354
355 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
356 (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
357 {
358 rom_flow.SuspendLayout();
359 char t = (char)e.KeyCode;
360 GameControl ctrl = (rom_flow.GetNextControl(CurrentGameControl, true) as GameControl);
361 if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
362 bool found = false;
363 GameControl pc = CurrentGameControl;
364 bool wrapped = false;
365 bool not_found = true;
366 while (!found)
367 {
368 if (wrapped)
369 {
370 foreach (Control ctl in rom_flow.Controls)
371 {
372 GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
373 }
374 if (not_found) { found = true; }
375 }
376 ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
377 if (ctrl == null)
378 {
379 ctrl = rom_flow.Controls[0] as GameControl;
380 wrapped = true;
381 }
382 if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
383 {
384 rom_flow.ScrollControlIntoView(ctrl);
385 ctrl.Select();
386 found = true;
387 }
388 pc = ctrl;
389 }
390 rom_flow.ResumeLayout(false);
391 }
392 }
393 void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
394 {
395 //gLog.Verbose.Debug.WriteLine ("platform_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
396 PlatformControl c = sender as PlatformControl;
397 int changeAmount = 0;
398 int currentPosition = 0;
399 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
400 if (OSInfo.OSIsUnix) {
401 platform_flow.SuspendLayout ();
402 var ctl = platform_ctrl_get_previous_control (true);
403 platform_flow.ScrollControlIntoView (ctl);
404 platform_flow.ResumeLayout (false);
405 }
406 }
407 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
408 if (OSInfo.OSIsUnix) {
409 platform_flow.SuspendLayout ();
410 var ctl = platform_ctrl_get_next_control (true);
411 platform_flow.ScrollControlIntoView (ctl);
412 platform_flow.ResumeLayout (false);
413 }
414 }
415 if (e.KeyCode == Keys.Home)
416 {
417 platform_flow.SuspendLayout();
418 platform_flow.Controls[0].Select();
419 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
420 platform_flow.ResumeLayout(false);
421 }
422 if (e.KeyCode == Keys.End)
423 {
424 platform_flow.SuspendLayout();
425 platform_flow.Controls[platform_flow.Controls.Count -1].Select();
426 platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
427 platform_flow.ResumeLayout(false);
428 }
429 if (e.KeyCode == Keys.PageUp)
430 {
431 platform_flow.SuspendLayout();
432 changeAmount = platform_flow.VerticalScroll.LargeChange;
433 currentPosition = platform_flow.VerticalScroll.Value;
434 if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
435 {
436 platform_flow.VerticalScroll.Value -= changeAmount;
437 }
438 else
439 {
440 platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
441 }
442 PlatformControl s = platform_ctrl_get_last_visible();
443 s.Select();
444 platform_flow.ScrollControlIntoView(s);
445 platform_flow.ResumeLayout(false);
446 }
447 if (e.KeyCode == Keys.PageDown)
448 {
449 platform_flow.SuspendLayout();
450 changeAmount = platform_flow.VerticalScroll.LargeChange;
451 currentPosition = platform_flow.VerticalScroll.Value;
452 if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
453 {
454 try
455 {
456 platform_flow.VerticalScroll.Value += changeAmount;
457 }
458 catch
459 {
460 platform_flow.Controls[0].Select();
461 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
462 rom_flow.PerformLayout();
463 return;
464 }
465 }
466 else
467 {
468 platform_flow.Controls[0].Select();
469 platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
470 }
471 PlatformControl s = platform_ctrl_get_last_visible();
472 s.Select();
473 platform_flow.ScrollControlIntoView(s);
474 platform_flow.ResumeLayout(false);
475 }
476 if (e.KeyCode == Keys.Enter)
477 {
478 // load this platform
479 platform_flow.Visible = false;
480 CurrentSelectedRom = c.Tag as IEmuConfig;
481 rom_flow.Visible = true;
482 rom_flow.BringToFront();
483 }
484 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape)
485 {
486 this.Close();
487 }
488 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
489 (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
490 {
491 platform_flow.SuspendLayout();
492 char t = (char)e.KeyCode;
493 PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
494 if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
495 bool found = false;
496 PlatformControl pc = CurrentPlatformControl;
497 bool wrapped = false;
498 bool not_found = true;
499 while (!found)
500 {
501 if (wrapped)
502 {
503 foreach (Control ctl in platform_flow.Controls)
504 {
505 PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
506 }
507 if (not_found) { found = true; }
508 }
509 ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
510 if (ctrl == null)
511 {
512 ctrl = platform_flow.Controls[0] as PlatformControl;
513 wrapped = true;
514 }
515 if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
516 {
517 platform_flow.ScrollControlIntoView(ctrl);
518 ctrl.Select();
519 found = true;
520 }
521 pc = ctrl;
522 }
523 platform_flow.ResumeLayout(false);
524 }
525 }
526
527 private void platform_flow_VisibleChanged(object sender, EventArgs e)
528 {
529 if (!platform_flow.Visible) return;
530 platform_flow.Controls.Clear();
531 platform_flow.BringToFront();
532 Stopwatch t = new Stopwatch();
533 t.Start();
534 platformWorker.RunWorkerAsync(t);
535 }
536
537 private void rom_flow_VisibleChanged(object sender, EventArgs e)
538 {
539 if (!rom_flow.Visible) return;
540 rom_flow.Controls.Clear();
541 rom_flow.BringToFront();
542 Stopwatch t = new Stopwatch();
543 t.Start();
544 gameWorker.RunWorkerAsync(t);
545 }
546
547 void game_ctrl_LostFocus(object sender, EventArgs e)
548 {
549 GameControl c = sender as GameControl;
550 #if !HAVE_X11_BORDERSTYLE_ERROR
551 c.BorderStyle = BorderStyle.None;
552 #endif
553 c.BackColor = this.BackColor;
554 }
555
556 void game_ctrl_GotFocus(object sender, EventArgs e)
557 {
558 GameControl c = sender as GameControl;
559 #if !HAVE_X11_BORDERSTYLE_ERROR
560 c.BorderStyle = BorderStyle.FixedSingle;
561 #endif
562 c.BackColor = SELECTED_CONTROL_BACKCOLOR;
563 CurrentGameControl = c;
564 }
565
566 private GameControl game_ctrl_get_last_visible()
567 {
568 GameControl s = new GameControl();
569 foreach (GameControl c in rom_flow.Controls)
570 {
571 if (c.Bounds.IntersectsWith(rom_flow.Bounds))
572 s = c;
573 }
574 return s;
575 }
576 private PlatformControl platform_ctrl_get_last_visible()
577 {
578 PlatformControl s = new PlatformControl();
579 foreach (PlatformControl c in platform_flow.Controls)
580 {
581 if (c.Bounds.IntersectsWith(platform_flow.Bounds))
582 s = c;
583 }
584 return s;
585 }
586 private PlatformControl platform_ctrl_get_next_control(bool update_control)
587 {
588 PlatformControl s = new PlatformControl();
589 int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
590 index = index + 1;
591 if (index < 0) {
592 index = platform_flow.Controls.Count-1;
593 }
594 if (index > platform_flow.Controls.Count-1) {
595 index = 0;
596 }
597 s = platform_flow.Controls [index] as PlatformControl;
598 if (update_control) {
599 CurrentPlatformControl = s;
600 s.Select ();
601 }
602 return s;
603 }
604 private PlatformControl platform_ctrl_get_previous_control(bool update_control)
605 {
606 PlatformControl s = null;
607 int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
608 index = index - 1;
609 if (index < 0) {
610 index = platform_flow.Controls.Count-1;
611 }
612 if (index > platform_flow.Controls.Count-1) {
613 index = 0;
614 }
615 s = platform_flow.Controls [index] as PlatformControl;
616 if (update_control) {
617 CurrentPlatformControl = s;
618 s.Select ();
619 }
620 return s;
621 }
622 private GameControl game_ctrl_get_next_control(bool update_control)
623 {
624 GameControl s = new GameControl();
625 int index = rom_flow.Controls.IndexOf (CurrentGameControl);
626 index = index + 1;
627 if (index < 0) {
628 index = rom_flow.Controls.Count-1;
629 }
630 if (index > rom_flow.Controls.Count-1) {
631 index = 0;
632 }
633 s = rom_flow.Controls [index] as GameControl;
634 if (update_control) {
635 CurrentGameControl = s;
636 s.Select ();
637 }
638 return s;
639 }
640 private GameControl game_ctrl_get_previous_control(bool update_control)
641 {
642 GameControl s = new GameControl();
643 int index = rom_flow.Controls.IndexOf (CurrentGameControl);
644 index = index - 1;
645 if (index < 0) {
646 index = rom_flow.Controls.Count-1;
647 }
648 if (index > rom_flow.Controls.Count-1) {
649 index = 0;
650 }
651 s = rom_flow.Controls [index] as GameControl;
652 if (update_control) {
653 CurrentGameControl = s;
654 s.Select ();
655 }
656 return s;
657 }
658
659
660 #region Background Workers
661
662 private int GetFormWidth()
663 {
664 if (this.InvokeRequired)
665 {
666 return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
667 }
668 else
669 {
670 return this.Width;
671 }
672 }
673 private Font GetFormFont()
674 {
675 if (this.InvokeRequired)
676 {
677 return (this.Invoke(new Delegate_GetFormFont(GetFormFont)) as Font);
678 }
679 else
680 {
681 return this.Font;
682 }
683 }
684
685 private Font ResizeFont(Font font, float size)
686 {
687 if (this.InvokeRequired)
688 {
689 return (this.Invoke(new Delegate_ResizeFont(ResizeFont), new object[] { font, size })as Font);
690 }
691 else
692 {
693 return new Font(font.FontFamily, size);
694 }
695 }
696
697 //private void AddPlatformControl(Control c)
698 //{
699 // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
700 // else
701 // {
702 // platform_flow.Controls.Add(c);
703 // }
704 //}
705 //private void UpdatePlatformControls()
706 //{
707 // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
708 // //else { this.Update(); }
709 //}
710 //private void AddGameControl(Control c)
711 //{
712 // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
713 // else
714 // {
715 // rom_flow.Controls.Add(c);
716 // }
717 //}
718 //private void UpdateGameControls()
719 //{
720 // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
721 // //else { this.Update(); }
722 //}
723 #region gameWorker
724 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
725 private object gameimage_lock = new object();
726 private void worker_progress_event (object event_source, ProgressEventArgs event_args)
727 {
728 //gLog.Debug.WriteLine ("worker_progress_event called!");
729
730 object user_object = event_args.GetUserObject ();
731 int progress = event_args.GetProgress();
732 string message = event_args.GetUserMessage() == null ? "" : event_args.GetUserMessage();
733 if (user_object != null) {
734 IRomConfig irc = (user_object as IRomConfig);
735 if (irc != null) {
736 gLog.Verbose.Debug.WriteLine ("Read Rom[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, irc);
737 } else {
738 IEmuConfig iec = (user_object as IEmuConfig);
739 if (iec != null) {
740 gLog.Verbose.Debug.WriteLine ("Read Platform[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, iec);
741 }
742 }
743 }
744 if (progress_bar.InvokeRequired) {
745 progress_bar.Invoke (new MethodInvoker (delegate {
746 progress_bar.Message = message;
747 progress_bar.Value = progress;
748 progress_bar.Update();
749 }));
750 } else {
751 progress_bar.Message = message;
752 progress_bar.Value = progress;
753 progress_bar.Update();
754 }
755 }
756 private void gameWorker_DoWork (object sender, DoWorkEventArgs e)
757 {
758 Stopwatch t = e.Argument as Stopwatch;
759 progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
760 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
761 progress_bar.ShowPercentageLabel = false;
762 #endif
763 progress_bar.ProgressColor = Color.LimeGreen;
764 progress_bar.Dock = DockStyle.Top;
765
766 if (this.InvokeRequired) {
767 this.Invoke ((MethodInvoker)delegate() {
768 this.Controls.Add (progress_bar);
769 });
770 } else {
771 this.Controls.Add (progress_bar);
772 }
773 progress_bar.Invoke (new MethodInvoker (delegate {
774 progress_bar.Margin = new System.Windows.Forms.Padding (0);
775 progress_bar.Size = new Size (GetFormWidth () - 25, 100);
776 }));
777
778 if (!cache.AreRomsCachedForPlatform(CurrentSelectedRom)) {
779 using (RomParser parser = new RomParser (CurrentSelectedRom, new ProgressEvent (worker_progress_event))) {
780 cache.CacheRoms (parser);
781 }
782 }
783 HashSet<IRomConfig> roms = cache.GetCachedRomsForPlatform (CurrentSelectedRom);
784
785 double count = 0;
786 double total_count = roms.Count;
787 if (total_count > 0) {
788 progress_bar.Invoke (new MethodInvoker (delegate {
789 progress_bar.Message = string.Format ("Please Wait Adding {0} {1} Roms...", total_count, CurrentSelectedRom.PlatformNameLong);
790 }));
791 }
792
793 foreach (IRomConfig config in roms)
794 {
795 progress_bar.Invoke(new MethodInvoker(delegate { progress_bar.Value = (int)(100.0 * (count / total_count)); }));
796 GameControl game_ctrl = new GameControl();
797 game_ctrl.Font = GetFormFont();
798 game_ctrl.Dock = DockStyle.Top;
799 game_ctrl.Width = this.Width - 10;
800 game_ctrl.Tag = config;
801 try
802 {
803 lock (gameimage_lock)
804 {
805 //gLog.Debug.WriteLine ("handling rom: {0}", config.ToString ());
806 if(String.IsNullOrEmpty(config.RomImage)) {
807 game_ctrl.GameImage = (Image)DefaultGameImage.Clone ();
808 }
809 else {
810 string path = config.GetFullRomImage();
811 game_ctrl.GameImage = Image.FromFile(path);
812 }
813 }
814 }
815 catch (Exception ex)
816 {
817 gLog.Error.WriteLine (ex.Message);
818 gLog.Verbose.Error.WriteLine (ex.ToString());
819 throw ex;
820 }
821 if (CurrentSelectedRom.PlatformNameShort == "Favorites")
822 {
823 //game_ctrl.GameName = config.RomTitle;
824 game_ctrl.GameName = RomFavorite.GetRomTitleFromConfig(config);
825 }
826 else
827 {
828 game_ctrl.GameName = config.RomTitle;
829 }
830 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
831 game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
832 game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
833 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
834 else { rom_flow.Controls.Add(game_ctrl); }
835 count++;
836 }
837 progress_bar.Invoke(new MethodInvoker(delegate { progress_bar.Value = 0; progress_bar.Update(); }));
838
839
840 e.Result = t;
841 }
842 private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
843 private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
844 {
845 Stopwatch t = e.Result as Stopwatch;
846 if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() {
847 progress_bar.Message = "";
848 this.Controls.Remove(progress_bar); });
849 this.Refresh();
850 } else {
851 progress_bar.Message = "";
852 this.Controls.Remove(progress_bar);
853 this.Refresh();
854
855 }
856 if (rom_flow.Controls.Count == 0)
857 {
858 GameControl game_ctrl = new GameControl();
859 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
860 game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
861 game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
862 game_ctrl.Font = GetFormFont();
863 game_ctrl.Dock = DockStyle.Top;
864 game_ctrl.Width = this.Width - 10;
865 IEmuConfig config = CurrentPlatformControl.Tag as IEmuConfig;
866 if (config.PlatformNameLong == "Favorites") {
867 game_ctrl.GameName = "You haven't favorited any games, select a game and then press CTRL+F to favorite it";
868 } else {
869 game_ctrl.GameName = string.Format ("No {0} games were found", config.PlatformNameShort);
870 }
871 rom_flow.Controls.Add(game_ctrl);
872 }
873 rom_flow.Controls[0].Select();
874 t.Stop();
875 gLog.Profiler.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
876 }
877 #endregion
878 #region platformWorker
879 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
880 private object platformimage_lock = new object();
881 private void platformWorker_DoWork (object sender, DoWorkEventArgs e)
882 {
883 Stopwatch t = e.Argument as Stopwatch;
884 progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
885 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
886 progress_bar.ShowPercentageLabel = false;
887 #endif
888 progress_bar.ProgressColor = Color.LimeGreen;
889 progress_bar.Dock = DockStyle.Top;
890
891 if (this.InvokeRequired) {
892 this.Invoke ((MethodInvoker)delegate() {
893 this.Controls.Add (progress_bar);
894 });
895 } else {
896 this.Controls.Add (progress_bar);
897 }
898 progress_bar.Invoke (new MethodInvoker (delegate {
899 progress_bar.Margin = new System.Windows.Forms.Padding (0);
900 progress_bar.Size = new Size (GetFormWidth () - 25, 100);
901 }));
902
903 PlatformParser parser = new PlatformParser (Config.RomPath, new ProgressEvent (worker_progress_event));
904
905
906 double count = 0;
907 double total_count = parser.Platforms.Count;
908 if (total_count > 0) {
909 progress_bar.Invoke (new MethodInvoker (delegate {
910 progress_bar.Message = string.Format ("Please Wait Loading {0} Rom Platforms...", total_count);
911 }));
912 }
913 foreach (IEmuConfig config in parser.Platforms) {
914 progress_bar.Invoke (new MethodInvoker (delegate {
915 progress_bar.Value = (int)(100.0 * (count / total_count));
916 }));
917
918 PlatformControl platform_ctrl = new PlatformControl ();
919 platform_ctrl.Font = GetFormFont ();
920 platform_ctrl.Dock = DockStyle.Top;
921 platform_ctrl.Width = this.Width - 10;
922 platform_ctrl.Tag = config;
923 try {
924 lock (platformimage_lock) {
925 //gLog.Debug.WriteLine ("handling platform: {0}", config.ToString ());
926 if(String.IsNullOrEmpty(config.PlatformImage)) {
927 platform_ctrl.PlatformImage = (Image)DefaultPlatformImage.Clone ();
928 }
929 else {
930 string path = config.GetFullPlatformImage();
931 platform_ctrl.PlatformImage = Image.FromFile(path);
932 }
933 }
934 } catch (Exception ex) {
935 gLog.Error.WriteLine (ex.Message);
936 gLog.Verbose.Error.WriteLine (ex.ToString());
937 throw ex;
938 }
939 platform_ctrl.PlatformName = config.ToString();
940 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
941 platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
942 platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
943 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
944 else { platform_flow.Controls.Add(platform_ctrl); }
945 count++;
946 }
947 progress_bar.Invoke(new MethodInvoker(delegate {
948 progress_bar.Value = 0; progress_bar.Update();
949 }));
950 e.Result = t;
951 }
952 private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
953 private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
954 {
955 gLog.Debug.WriteLine("platformWorker_RunWorkerCompleted called");
956 if (this.InvokeRequired) {
957 this.Invoke((MethodInvoker)delegate() {
958 progress_bar.Message = "";
959 this.Controls.Remove(progress_bar);
960 });
961 }
962 else {
963 progress_bar.Message = "";
964 this.Controls.Remove(progress_bar);
965 }
966 Stopwatch t = e.Result as Stopwatch;
967 if (platform_flow.Controls.Count == 0)
968 {
969 PlatformControl platform_ctrl = new PlatformControl();
970 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
971 platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
972 platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
973 platform_ctrl.Font = GetFormFont();
974 platform_ctrl.Dock = DockStyle.Top;
975 platform_ctrl.Width = this.Width - 10;
976 platform_ctrl.PlatformName = string.Format("You don't have any roms in your rompath: '{0}'",Config.RomPath);
977 platform_flow.Controls.Add(platform_ctrl);
978 }
979 if (SavedPlatformIndex != -1) {
980 var platform_control = platform_flow.Controls [SavedPlatformIndex] as PlatformControl;
981 platform_flow.SuspendLayout ();
982 CurrentPlatformControl = platform_control;
983 platform_flow.ScrollControlIntoView (CurrentPlatformControl);
984 CurrentPlatformControl.Select ();
985 platform_flow.ResumeLayout ();
986 SavedPlatformIndex = -1;
987 } else {
988 platform_flow.Controls[0].Select();
989 }
990 t.Stop();
991 gLog.Profiler.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
992 }
993 #endregion
994
995 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
996 {
997 Cursor.Show();
998 }
999 #endregion
1000 }
1001 }

  ViewVC Help
Powered by ViewVC 1.1.22