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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22