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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (hide annotations) (download)
Tue Oct 8 18:39:46 2013 UTC (6 years, 4 months ago) by william
Original Path: trunk/EmuXPortal/Form1.cs
File size: 22139 byte(s)
re-integrate branch: fullscreen-fix
** the application will be top-most (this reverts r153)
   -revert changes from r146, r147, and r148:
   -* don't make form TopMost (because no other window can be above it; which will cause issues with launching games)
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     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 17
50     void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
51     {
52     PlatformControl c = sender as PlatformControl;
53 william 60 int changeAmount = 0;
54     int currentPosition = 0;
55     if (e.KeyCode == Keys.Home)
56     {
57     platform_flow.Controls[0].Select();
58     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
59     }
60     if (e.KeyCode == Keys.End)
61     {
62     platform_flow.Controls[platform_flow.Controls.Count -1].Select();
63     platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
64     }
65     if (e.KeyCode == Keys.PageUp)
66     {
67    
68    
69     changeAmount = platform_flow.VerticalScroll.LargeChange;
70     currentPosition = platform_flow.VerticalScroll.Value;
71     if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
72     {
73     platform_flow.VerticalScroll.Value -= changeAmount;
74     }
75     else
76     {
77     platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
78     }
79     PlatformControl s = platform_ctrl_get_last_visible();
80     s.Select();
81     platform_flow.ScrollControlIntoView(s);
82     platform_flow.PerformLayout();
83     }
84     if (e.KeyCode == Keys.PageDown)
85     {
86     changeAmount = platform_flow.VerticalScroll.LargeChange;
87     currentPosition = platform_flow.VerticalScroll.Value;
88     if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
89     {
90     try
91     {
92     platform_flow.VerticalScroll.Value += changeAmount;
93     }
94     catch
95     {
96     platform_flow.Controls[0].Select();
97     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
98     rom_flow.PerformLayout();
99     return;
100     }
101     }
102     else
103     {
104     platform_flow.Controls[0].Select();
105     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
106     }
107     PlatformControl s = platform_ctrl_get_last_visible();
108     s.Select();
109     platform_flow.ScrollControlIntoView(s);
110     platform_flow.PerformLayout();
111     }
112 william 17 if (e.KeyCode == Keys.Enter)
113     {
114     // load this platform
115     platform_flow.Visible = false;
116     CurrentSelectedRom = c.Tag as IEmuConfig;
117     rom_flow.Visible = true;
118     rom_flow.BringToFront();
119 william 25 }
120     if (e.KeyCode == Keys.Back)
121     {
122     this.Close();
123     }
124 william 30 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
125     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
126     {
127     char t = (char)e.KeyCode;
128 william 31 PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
129     if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
130     bool found = false;
131     PlatformControl pc = CurrentPlatformControl;
132     bool wrapped = false;
133     bool not_found = true;
134     while (!found)
135 william 30 {
136 william 31 if (wrapped)
137 william 30 {
138 william 31 foreach (Control ctl in platform_flow.Controls)
139     {
140     PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
141     }
142     if (not_found) { found = true; }
143 william 30 }
144 william 31 ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
145     if (ctrl == null)
146     {
147     ctrl = platform_flow.Controls[0] as PlatformControl;
148     wrapped = true;
149     }
150     if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
151     {
152     platform_flow.ScrollControlIntoView(ctrl);
153     ctrl.Select();
154     found = true;
155     }
156     pc = ctrl;
157 william 30 }
158     }
159 william 17 }
160    
161     private void platform_flow_VisibleChanged(object sender, EventArgs e)
162     {
163     if (!platform_flow.Visible) return;
164     platform_flow.Controls.Clear();
165     platform_flow.BringToFront();
166 william 37 Stopwatch t = new Stopwatch();
167     t.Start();
168 william 60 platformWorker.RunWorkerAsync(t);
169 william 17 }
170    
171     private void rom_flow_VisibleChanged(object sender, EventArgs e)
172     {
173     if (!rom_flow.Visible) return;
174     rom_flow.Controls.Clear();
175     rom_flow.BringToFront();
176 william 37 Stopwatch t = new Stopwatch();
177     t.Start();
178 william 60 gameWorker.RunWorkerAsync(t);
179 william 17 }
180    
181     void game_ctrl_LostFocus(object sender, EventArgs e)
182     {
183     GameControl c = sender as GameControl;
184     c.BorderStyle = BorderStyle.None;
185     }
186    
187     void game_ctrl_GotFocus(object sender, EventArgs e)
188     {
189     GameControl c = sender as GameControl;
190     c.BorderStyle = BorderStyle.FixedSingle;
191 william 31 CurrentGameControl = c;
192 william 17 }
193    
194 william 60 private GameControl game_ctrl_get_last_visible()
195     {
196     GameControl s = new GameControl();
197     foreach (GameControl c in rom_flow.Controls)
198     {
199     if (c.Bounds.IntersectsWith(rom_flow.Bounds))
200     s = c;
201     }
202     return s;
203     }
204     private PlatformControl platform_ctrl_get_last_visible()
205     {
206     PlatformControl s = new PlatformControl();
207     foreach (PlatformControl c in platform_flow.Controls)
208     {
209     if (c.Bounds.IntersectsWith(platform_flow.Bounds))
210     s = c;
211     }
212     return s;
213     }
214 william 17 void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
215     {
216     GameControl c = sender as GameControl;
217 william 60
218     int changeAmount = 0;
219     int currentPosition = 0;
220     if (e.KeyCode == Keys.Home)
221     {
222     rom_flow.Controls[0].Select();
223     rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
224     }
225     if (e.KeyCode == Keys.End)
226     {
227     rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
228     rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
229     }
230     if (e.KeyCode == Keys.PageUp)
231     {
232     changeAmount = rom_flow.VerticalScroll.LargeChange;
233     currentPosition = rom_flow.VerticalScroll.Value;
234     if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
235     {
236     try
237     {
238     rom_flow.VerticalScroll.Value += changeAmount;
239     }
240     catch
241     {
242     rom_flow.Controls[0].Select();
243     rom_flow.ScrollControlIntoView(platform_flow.Controls[0]);
244     rom_flow.PerformLayout();
245     return;
246     }
247     }
248     else
249     {
250     rom_flow.Controls[0].Select();
251     rom_flow.ScrollControlIntoView(platform_flow.Controls[0]);
252     }
253     GameControl s = game_ctrl_get_last_visible();
254     s.Select();
255     rom_flow.ScrollControlIntoView(s);
256     rom_flow.PerformLayout();
257     }
258     if (e.KeyCode == Keys.PageDown)
259     {
260     changeAmount = rom_flow.VerticalScroll.LargeChange;
261     currentPosition = rom_flow.VerticalScroll.Value;
262     if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
263     {
264     rom_flow.VerticalScroll.Value += changeAmount;
265     }
266     else
267     {
268     rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
269     }
270     GameControl s = game_ctrl_get_last_visible();
271     s.Select();
272     rom_flow.ScrollControlIntoView(s);
273     rom_flow.PerformLayout();
274     }
275    
276 william 17 if (e.KeyCode == Keys.Enter)
277     {
278 william 23 IRomConfig config = c.Tag as IRomConfig;
279    
280     Process p = new Process();
281 william 85
282     p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
283 william 126 p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(log, config) : config.Config.GameExeArgs;
284 william 23 p.Start();
285 william 160
286     // minimize EmuXPortal
287     this.WindowState = FormWindowState.Minimized;
288     // wait for exit of game
289     p.WaitForExit();
290     // maximize EmuXPortal
291     this.WindowState = FormWindowState.Maximized;
292 william 17 }
293     if (e.KeyCode == Keys.Back)
294     {
295     rom_flow.Visible = false;
296     platform_flow.Visible = true;
297     }
298 william 30
299     if ( (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
300     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
301     {
302     char t = (char)e.KeyCode;
303 william 31 GameControl ctrl = (rom_flow.GetNextControl(CurrentPlatformControl, true) as GameControl);
304     if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
305     bool found = false;
306     GameControl pc = CurrentGameControl;
307     bool wrapped = false;
308     bool not_found = true;
309     while (!found)
310 william 30 {
311 william 31 if (wrapped)
312 william 30 {
313 william 31 foreach (Control ctl in rom_flow.Controls)
314     {
315     GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
316     }
317     if (not_found) { found = true; }
318 william 30 }
319 william 31 ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
320     if (ctrl == null)
321     {
322     ctrl = rom_flow.Controls[0] as GameControl;
323     wrapped = true;
324     }
325     if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
326     {
327     rom_flow.ScrollControlIntoView(ctrl);
328     ctrl.Select();
329     found = true;
330     }
331     pc = ctrl;
332     }
333 william 30 }
334 william 60 }
335    
336     #region Background Workers
337    
338     private int GetFormWidth()
339     {
340     if (this.InvokeRequired)
341     {
342     return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
343     }
344     else
345     {
346     return this.Width;
347     }
348     }
349 william 113 //private void AddPlatformControl(Control c)
350     //{
351     // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
352     // else
353     // {
354     // platform_flow.Controls.Add(c);
355     // }
356     //}
357     //private void UpdatePlatformControls()
358     //{
359     // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
360     // //else { this.Update(); }
361     //}
362     //private void AddGameControl(Control c)
363     //{
364     // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
365     // else
366     // {
367     // rom_flow.Controls.Add(c);
368     // }
369     //}
370     //private void UpdateGameControls()
371     //{
372     // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
373     // //else { this.Update(); }
374     //}
375 william 60 #region gameWorker
376 william 110 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
377 william 137 private object gameimage_lock = new object();
378 william 60 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
379     {
380     Stopwatch t = e.Argument as Stopwatch;
381 william 112 RomParser parser = new RomParser(log, CurrentSelectedRom);
382 william 60
383     ProgressBar bar = new ProgressBar();
384 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(bar); }); }
385     else { rom_flow.Controls.Add(bar); }
386     //UpdateGameControls();
387     //Application.DoEvents();
388     //Thread.Sleep(10);
389 william 60 bar.Invoke(new MethodInvoker(delegate
390     {
391     //bar.Message = "Please Wait...";
392     //bar.ShowPercentageLabel = true;
393     bar.Margin = new System.Windows.Forms.Padding(0);
394     bar.Size = new Size(GetFormWidth() - 25, 100);
395     }));
396    
397     double count = 0;
398     double total_count = parser.Roms.Count;
399     foreach (IRomConfig config in parser.Roms)
400     {
401     GameControl game_ctrl = new GameControl();
402     game_ctrl.Dock = DockStyle.Top;
403     game_ctrl.Width = this.Width - 10;
404     game_ctrl.Tag = config;
405 william 137 try
406     {
407     lock (gameimage_lock)
408     {
409     game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
410     config.ReleaseRomImageResource();
411     }
412     }
413     catch (Exception ex)
414     {
415     throw ex;
416     }
417 william 60 game_ctrl.GameName = config.RomTitle;
418     game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
419     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
420     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
421 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
422     else { rom_flow.Controls.Add(game_ctrl); }
423     //UpdateGameControls();
424     //Application.DoEvents();
425     //int percent = (int)(100.0 * (count / total_count));
426     //log.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
427 william 60 bar.Invoke(new MethodInvoker(delegate
428     {
429 william 113 bar.Value = (int)(100.0 * (count / total_count));
430 william 60 }));
431     count++;
432     }
433     e.Result = t;
434 william 105 parser.Dispose();
435 william 60 }
436     private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
437     private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
438     {
439     Stopwatch t = e.Result as Stopwatch;
440     rom_flow.Controls.RemoveAt(0);
441     rom_flow.Controls[0].Select();
442     (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
443     t.Stop();
444 william 112 log.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
445 william 60 }
446     #endregion
447     #region platformWorker
448 william 110 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
449 william 137 private object platformimage_lock = new object();
450 william 60 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
451     {
452     Stopwatch t = e.Argument as Stopwatch;
453 william 112 PlatformParser parser = new PlatformParser(log, Config.RomPath);
454 william 60 double count = 0;
455     double total_count = parser.Platforms.Count;
456     ProgressBar bar = new ProgressBar();
457 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(bar); }); }
458     else { platform_flow.Controls.Add(bar); }
459     //UpdatePlatformControls();
460     //Application.DoEvents();
461     //Thread.Sleep(10);
462 william 60 bar.Invoke(new MethodInvoker(delegate
463     {
464     bar.Margin = new System.Windows.Forms.Padding(0);
465     bar.Size = new Size(GetFormWidth() - 25, 100);
466     }));
467     foreach (IEmuConfig config in parser.Platforms)
468     {
469     PlatformControl platform_ctrl = new PlatformControl();
470     platform_ctrl.Dock = DockStyle.Top;
471     platform_ctrl.Width = this.Width - 10;
472     platform_ctrl.Tag = config;
473 william 137 try
474     {
475     lock (platformimage_lock)
476     {
477 william 138 platform_ctrl.PlatformImage = config.PlatformImage == null ? (Image)DefaultPlatformImage.Clone() : (Image)config.PlatformImage.Clone();
478 william 137 config.ReleasePlatformImageResource();
479     }
480     }
481     catch (Exception ex)
482     {
483     throw ex;
484     }
485 william 60 platform_ctrl.PlatformName = config.ToString();
486     platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
487     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
488     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
489 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
490     else { platform_flow.Controls.Add(platform_ctrl); }
491     //Application.DoEvents();
492     //int percent = (int)(100.0 * (count / total_count));
493     //log.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
494 william 60 bar.Invoke(new MethodInvoker(delegate
495     {
496 william 113 bar.Value = (int)(100.0 * (count / total_count));
497 william 60 }));
498     count++;
499     }
500     e.Result = t;
501 william 105 parser.Dispose();
502 william 60 }
503     private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
504     private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
505     {
506     Stopwatch t = e.Result as Stopwatch;
507     platform_flow.Controls.RemoveAt(0);
508     platform_flow.Controls[0].Select();
509     (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
510 william 112 log.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
511 william 60 }
512     #endregion
513    
514     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
515     {
516     Cursor.Show();
517 william 112 log.CloseLog();
518 william 60 }
519     #endregion
520 william 4 }
521     }

  ViewVC Help
Powered by ViewVC 1.1.22