ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/branches/mono-xml-linq/EmuXPortal/Form1.cs
Revision: 335
Committed: Tue Dec 19 03:44:45 2017 UTC (5 years, 3 months ago) by william
File size: 41186 byte(s)
Log Message:
merge changes from branch /branches/mono (image resource loading)

File Contents

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