/[EmuXPortal]/trunk/EmuXPortal/Form1.cs
ViewVC logotype

Annotation of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 177 - (hide annotations) (download)
Mon Aug 4 06:43:34 2014 UTC (6 years ago) by william
File size: 25249 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22