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

Annotation of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22