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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22