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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22