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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 181 - (hide annotations) (download)
Mon Aug 4 08:10:19 2014 UTC (5 years, 6 months ago) by william
Original Path: trunk/EmuXPortal/Form1.cs
File size: 25763 byte(s)

1 william 181 #define DISABLE_CURSOR_HIDE // when this is present, the cursor will not be hidden
2     using System;
3 william 4 using System.Collections.Generic;
4     using System.ComponentModel;
5     using System.Data;
6     using System.Drawing;
7     using System.Linq;
8     using System.Text;
9     using System.Windows.Forms;
10 william 12 using EmuXPortal.Api;
11 william 17 using EmuXPortal.Logging;
12 william 23 using System.Diagnostics;
13 william 60 using System.Reflection;
14     using System.Threading;
15 william 4
16     namespace EmuXPortal
17     {
18     public partial class Form1 : Form
19     {
20 william 17 IEmuConfig CurrentSelectedRom = null;
21 william 31 PlatformControl CurrentPlatformControl = null;
22     GameControl CurrentGameControl = null;
23 william 112 logger log = new logger();
24 william 4 public Form1()
25     {
26     InitializeComponent();
27 william 17 platform_flow.Dock = DockStyle.Fill;
28     rom_flow.Dock = DockStyle.Fill;
29 william 112 log.OpenLog();
30 william 4 }
31 william 17
32     private void Form1_Load(object sender, EventArgs e)
33     {
34 william 112 Config.LoadConfig(log);
35 william 172 Config.InitializePresentationForm(log,this);
36 william 17 }
37 william 181 private void Form1_Shown(object sender, EventArgs e)
38     {
39     platform_flow.Visible = true;
40     #if !DISABLE_CURSOR_HIDE
41     Cursor.Hide();
42     #else
43     Cursor.Show();
44     #endif
45     }
46 william 17 void platform_ctrl_LostFocus(object sender, EventArgs e)
47     {
48     PlatformControl c = sender as PlatformControl;
49     c.BorderStyle = BorderStyle.None;
50     }
51 william 13
52 william 17 void platform_ctrl_GotFocus(object sender, EventArgs e)
53 william 13 {
54 william 17 PlatformControl c = sender as PlatformControl;
55     c.BorderStyle = BorderStyle.FixedSingle;
56 william 31 CurrentPlatformControl = c;
57 william 13 }
58 william 168 void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
59     {
60     GameControl c = sender as GameControl;
61 william 17
62 william 168 int changeAmount = 0;
63     int currentPosition = 0;
64     if (e.KeyCode == Keys.F && e.Modifiers.HasFlag(Keys.Control))
65     {
66     IRomConfig config = c.Tag as IRomConfig;
67     if (config == null)
68     {
69     log.WriteLine("Unable to add/remove from/to favorites (config is null): {0} [{1}]", config.RomTitle, config.RomFile);
70     }
71     else
72     {
73 william 169 var isFavorite = RomFavorite.IsFavorite(log, config);
74 william 168 if (isFavorite)
75     {
76     // add to favorites
77     log.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
78 william 169 if (!RomFavorite.RemoveFavorite(log, config))
79 william 168 {
80     log.WriteLine("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
81     }
82     else
83     {
84     log.WriteLine("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
85 william 177 if (config.Config.PlatformNameShort == "Favorites")
86 william 176 {
87 william 177 var parent = c.Parent;
88     if (parent != null)
89 william 176 {
90 william 177 parent.Controls.Remove(c);
91     if (parent.Controls.Count > 0)
92 william 176 {
93 william 177 var next_ctrl = parent.Controls[0];
94     if (next_ctrl != null)
95     {
96     next_ctrl.Select();
97     }
98 william 176 }
99     }
100 william 177 }
101 william 168 }
102     }
103     else
104     {
105     // add to favorites
106     log.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
107 william 169 if (!RomFavorite.AddFavorite(log,config))
108 william 168 {
109     log.WriteLine("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
110     }
111     else
112     {
113     log.WriteLine("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
114     }
115     }
116     log.WriteLine("Updateing favorites");
117 william 169 if (!RomFavorite.UpdateFavorites(log))
118 william 168 {
119     log.WriteLine("Failed to update favorites");
120     }
121     else
122     {
123     log.WriteLine("Updated favorites");
124     }
125     }
126     return; // stop processing other keys
127    
128     }
129     if (e.KeyCode == Keys.Home)
130     {
131     rom_flow.Controls[0].Select();
132     rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
133     }
134     if (e.KeyCode == Keys.End)
135     {
136     rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
137     rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
138     }
139     if (e.KeyCode == Keys.PageUp)
140     {
141     changeAmount = rom_flow.VerticalScroll.LargeChange;
142     currentPosition = rom_flow.VerticalScroll.Value;
143     if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
144     {
145     try
146     {
147 william 179 rom_flow.VerticalScroll.Value -= changeAmount;
148 william 168 }
149     catch
150     {
151     rom_flow.Controls[0].Select();
152 william 179 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
153 william 168 rom_flow.PerformLayout();
154     return;
155     }
156     }
157     else
158     {
159     rom_flow.Controls[0].Select();
160 william 179 rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
161 william 168 }
162     GameControl s = game_ctrl_get_last_visible();
163     s.Select();
164     rom_flow.ScrollControlIntoView(s);
165     rom_flow.PerformLayout();
166     }
167     if (e.KeyCode == Keys.PageDown)
168     {
169     changeAmount = rom_flow.VerticalScroll.LargeChange;
170     currentPosition = rom_flow.VerticalScroll.Value;
171     if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
172     {
173     rom_flow.VerticalScroll.Value += changeAmount;
174     }
175     else
176     {
177     rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
178     }
179     GameControl s = game_ctrl_get_last_visible();
180     s.Select();
181     rom_flow.ScrollControlIntoView(s);
182     rom_flow.PerformLayout();
183     }
184    
185     if (e.KeyCode == Keys.Enter)
186     {
187     IRomConfig config = c.Tag as IRomConfig;
188    
189     Process p = new Process();
190    
191     p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
192     p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(log, config) : config.Config.GameExeArgs;
193     p.Start();
194    
195     // minimize EmuXPortal
196     this.WindowState = FormWindowState.Minimized;
197     // wait for exit of game
198     p.WaitForExit();
199     // maximize EmuXPortal
200     this.WindowState = FormWindowState.Maximized;
201     }
202     if (e.KeyCode == Keys.Back)
203     {
204     rom_flow.Visible = false;
205     platform_flow.Visible = true;
206     }
207    
208     if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
209     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
210     {
211     char t = (char)e.KeyCode;
212 william 181 GameControl ctrl = (rom_flow.GetNextControl(CurrentGameControl, true) as GameControl);
213 william 168 if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
214     bool found = false;
215     GameControl pc = CurrentGameControl;
216     bool wrapped = false;
217     bool not_found = true;
218     while (!found)
219     {
220     if (wrapped)
221     {
222     foreach (Control ctl in rom_flow.Controls)
223     {
224     GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
225     }
226     if (not_found) { found = true; }
227     }
228     ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
229     if (ctrl == null)
230     {
231     ctrl = rom_flow.Controls[0] as GameControl;
232     wrapped = true;
233     }
234     if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
235     {
236     rom_flow.ScrollControlIntoView(ctrl);
237     ctrl.Select();
238     found = true;
239     }
240     pc = ctrl;
241     }
242     }
243     }
244 william 17 void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
245     {
246     PlatformControl c = sender as PlatformControl;
247 william 60 int changeAmount = 0;
248     int currentPosition = 0;
249     if (e.KeyCode == Keys.Home)
250     {
251     platform_flow.Controls[0].Select();
252     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
253     }
254     if (e.KeyCode == Keys.End)
255     {
256     platform_flow.Controls[platform_flow.Controls.Count -1].Select();
257     platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
258     }
259     if (e.KeyCode == Keys.PageUp)
260     {
261     changeAmount = platform_flow.VerticalScroll.LargeChange;
262     currentPosition = platform_flow.VerticalScroll.Value;
263     if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
264     {
265     platform_flow.VerticalScroll.Value -= changeAmount;
266     }
267     else
268     {
269     platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
270     }
271     PlatformControl s = platform_ctrl_get_last_visible();
272     s.Select();
273     platform_flow.ScrollControlIntoView(s);
274     platform_flow.PerformLayout();
275     }
276     if (e.KeyCode == Keys.PageDown)
277 william 180 {
278 william 60 changeAmount = platform_flow.VerticalScroll.LargeChange;
279     currentPosition = platform_flow.VerticalScroll.Value;
280     if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
281     {
282     try
283     {
284     platform_flow.VerticalScroll.Value += changeAmount;
285     }
286     catch
287     {
288     platform_flow.Controls[0].Select();
289     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
290     rom_flow.PerformLayout();
291     return;
292     }
293     }
294     else
295     {
296     platform_flow.Controls[0].Select();
297     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
298     }
299     PlatformControl s = platform_ctrl_get_last_visible();
300     s.Select();
301     platform_flow.ScrollControlIntoView(s);
302     platform_flow.PerformLayout();
303     }
304 william 17 if (e.KeyCode == Keys.Enter)
305     {
306     // load this platform
307     platform_flow.Visible = false;
308     CurrentSelectedRom = c.Tag as IEmuConfig;
309     rom_flow.Visible = true;
310     rom_flow.BringToFront();
311 william 25 }
312     if (e.KeyCode == Keys.Back)
313     {
314     this.Close();
315     }
316 william 30 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
317     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
318     {
319     char t = (char)e.KeyCode;
320 william 31 PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
321     if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
322     bool found = false;
323     PlatformControl pc = CurrentPlatformControl;
324     bool wrapped = false;
325     bool not_found = true;
326     while (!found)
327 william 30 {
328 william 31 if (wrapped)
329 william 30 {
330 william 31 foreach (Control ctl in platform_flow.Controls)
331     {
332     PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
333     }
334     if (not_found) { found = true; }
335 william 30 }
336 william 31 ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
337     if (ctrl == null)
338     {
339     ctrl = platform_flow.Controls[0] as PlatformControl;
340     wrapped = true;
341     }
342     if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
343     {
344     platform_flow.ScrollControlIntoView(ctrl);
345     ctrl.Select();
346     found = true;
347     }
348     pc = ctrl;
349 william 30 }
350     }
351 william 17 }
352    
353     private void platform_flow_VisibleChanged(object sender, EventArgs e)
354     {
355     if (!platform_flow.Visible) return;
356     platform_flow.Controls.Clear();
357     platform_flow.BringToFront();
358 william 37 Stopwatch t = new Stopwatch();
359     t.Start();
360 william 60 platformWorker.RunWorkerAsync(t);
361 william 17 }
362    
363     private void rom_flow_VisibleChanged(object sender, EventArgs e)
364     {
365     if (!rom_flow.Visible) return;
366     rom_flow.Controls.Clear();
367     rom_flow.BringToFront();
368 william 37 Stopwatch t = new Stopwatch();
369     t.Start();
370 william 60 gameWorker.RunWorkerAsync(t);
371 william 17 }
372    
373     void game_ctrl_LostFocus(object sender, EventArgs e)
374     {
375     GameControl c = sender as GameControl;
376     c.BorderStyle = BorderStyle.None;
377     }
378    
379     void game_ctrl_GotFocus(object sender, EventArgs e)
380     {
381     GameControl c = sender as GameControl;
382     c.BorderStyle = BorderStyle.FixedSingle;
383 william 31 CurrentGameControl = c;
384 william 17 }
385    
386 william 60 private GameControl game_ctrl_get_last_visible()
387     {
388     GameControl s = new GameControl();
389     foreach (GameControl c in rom_flow.Controls)
390     {
391     if (c.Bounds.IntersectsWith(rom_flow.Bounds))
392     s = c;
393     }
394     return s;
395     }
396     private PlatformControl platform_ctrl_get_last_visible()
397     {
398     PlatformControl s = new PlatformControl();
399     foreach (PlatformControl c in platform_flow.Controls)
400     {
401     if (c.Bounds.IntersectsWith(platform_flow.Bounds))
402     s = c;
403     }
404     return s;
405     }
406 william 168
407 william 60
408     #region Background Workers
409    
410     private int GetFormWidth()
411     {
412     if (this.InvokeRequired)
413     {
414     return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
415     }
416     else
417     {
418     return this.Width;
419     }
420     }
421 william 113 //private void AddPlatformControl(Control c)
422     //{
423     // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
424     // else
425     // {
426     // platform_flow.Controls.Add(c);
427     // }
428     //}
429     //private void UpdatePlatformControls()
430     //{
431     // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
432     // //else { this.Update(); }
433     //}
434     //private void AddGameControl(Control c)
435     //{
436     // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
437     // else
438     // {
439     // rom_flow.Controls.Add(c);
440     // }
441     //}
442     //private void UpdateGameControls()
443     //{
444     // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
445     // //else { this.Update(); }
446     //}
447 william 60 #region gameWorker
448 william 110 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
449 william 137 private object gameimage_lock = new object();
450 william 60 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
451     {
452     Stopwatch t = e.Argument as Stopwatch;
453 william 112 RomParser parser = new RomParser(log, CurrentSelectedRom);
454 william 60
455     ProgressBar bar = new ProgressBar();
456 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(bar); }); }
457     else { rom_flow.Controls.Add(bar); }
458     //UpdateGameControls();
459     //Application.DoEvents();
460     //Thread.Sleep(10);
461 william 60 bar.Invoke(new MethodInvoker(delegate
462     {
463     //bar.Message = "Please Wait...";
464     //bar.ShowPercentageLabel = true;
465     bar.Margin = new System.Windows.Forms.Padding(0);
466     bar.Size = new Size(GetFormWidth() - 25, 100);
467     }));
468    
469     double count = 0;
470     double total_count = parser.Roms.Count;
471     foreach (IRomConfig config in parser.Roms)
472     {
473     GameControl game_ctrl = new GameControl();
474     game_ctrl.Dock = DockStyle.Top;
475     game_ctrl.Width = this.Width - 10;
476     game_ctrl.Tag = config;
477 william 137 try
478     {
479     lock (gameimage_lock)
480     {
481     game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
482     config.ReleaseRomImageResource();
483     }
484     }
485     catch (Exception ex)
486     {
487     throw ex;
488 william 178 }
489     if (CurrentSelectedRom.PlatformNameShort == "Favorites")
490     {
491     //game_ctrl.GameName = config.RomTitle;
492     game_ctrl.GameName = RomFavorite.GetRomTitleFromConfig(config);
493     }
494     else
495     {
496     game_ctrl.GameName = config.RomTitle;
497 william 137 }
498 william 60 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
499     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
500     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
501 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
502     else { rom_flow.Controls.Add(game_ctrl); }
503     //UpdateGameControls();
504     //Application.DoEvents();
505     //int percent = (int)(100.0 * (count / total_count));
506     //log.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
507 william 60 bar.Invoke(new MethodInvoker(delegate
508     {
509 william 113 bar.Value = (int)(100.0 * (count / total_count));
510 william 60 }));
511     count++;
512     }
513     e.Result = t;
514 william 105 parser.Dispose();
515 william 60 }
516     private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
517     private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
518     {
519     Stopwatch t = e.Result as Stopwatch;
520     rom_flow.Controls.RemoveAt(0);
521     rom_flow.Controls[0].Select();
522     (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
523     t.Stop();
524 william 112 log.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
525 william 60 }
526     #endregion
527     #region platformWorker
528 william 110 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
529 william 137 private object platformimage_lock = new object();
530 william 60 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
531     {
532     Stopwatch t = e.Argument as Stopwatch;
533 william 112 PlatformParser parser = new PlatformParser(log, Config.RomPath);
534 william 60 double count = 0;
535     double total_count = parser.Platforms.Count;
536     ProgressBar bar = new ProgressBar();
537 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(bar); }); }
538     else { platform_flow.Controls.Add(bar); }
539     //UpdatePlatformControls();
540     //Application.DoEvents();
541     //Thread.Sleep(10);
542 william 60 bar.Invoke(new MethodInvoker(delegate
543     {
544     bar.Margin = new System.Windows.Forms.Padding(0);
545     bar.Size = new Size(GetFormWidth() - 25, 100);
546     }));
547     foreach (IEmuConfig config in parser.Platforms)
548     {
549     PlatformControl platform_ctrl = new PlatformControl();
550     platform_ctrl.Dock = DockStyle.Top;
551     platform_ctrl.Width = this.Width - 10;
552     platform_ctrl.Tag = config;
553 william 137 try
554     {
555     lock (platformimage_lock)
556     {
557 william 138 platform_ctrl.PlatformImage = config.PlatformImage == null ? (Image)DefaultPlatformImage.Clone() : (Image)config.PlatformImage.Clone();
558 william 137 config.ReleasePlatformImageResource();
559     }
560     }
561     catch (Exception ex)
562     {
563     throw ex;
564     }
565 william 60 platform_ctrl.PlatformName = config.ToString();
566     platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
567     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
568     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
569 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
570     else { platform_flow.Controls.Add(platform_ctrl); }
571     //Application.DoEvents();
572     //int percent = (int)(100.0 * (count / total_count));
573     //log.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
574 william 60 bar.Invoke(new MethodInvoker(delegate
575     {
576 william 113 bar.Value = (int)(100.0 * (count / total_count));
577 william 60 }));
578     count++;
579     }
580     e.Result = t;
581 william 105 parser.Dispose();
582 william 60 }
583     private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
584     private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
585     {
586     Stopwatch t = e.Result as Stopwatch;
587     platform_flow.Controls.RemoveAt(0);
588     platform_flow.Controls[0].Select();
589     (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
590 william 112 log.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
591 william 60 }
592     #endregion
593    
594     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
595     {
596     Cursor.Show();
597 william 112 log.CloseLog();
598 william 60 }
599     #endregion
600 william 4 }
601     }

  ViewVC Help
Powered by ViewVC 1.1.22