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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22