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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22