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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 137 - (hide annotations) (download)
Thu Oct 4 21:52:13 2012 UTC (7 years, 4 months ago) by william
Original Path: trunk/EmuXPortal/Form1.cs
File size: 21840 byte(s)
+ thread locking

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 17 }
286     if (e.KeyCode == Keys.Back)
287     {
288     rom_flow.Visible = false;
289     platform_flow.Visible = true;
290     }
291 william 30
292     if ( (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
293     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
294     {
295     char t = (char)e.KeyCode;
296 william 31 GameControl ctrl = (rom_flow.GetNextControl(CurrentPlatformControl, true) as GameControl);
297     if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
298     bool found = false;
299     GameControl pc = CurrentGameControl;
300     bool wrapped = false;
301     bool not_found = true;
302     while (!found)
303 william 30 {
304 william 31 if (wrapped)
305 william 30 {
306 william 31 foreach (Control ctl in rom_flow.Controls)
307     {
308     GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
309     }
310     if (not_found) { found = true; }
311 william 30 }
312 william 31 ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
313     if (ctrl == null)
314     {
315     ctrl = rom_flow.Controls[0] as GameControl;
316     wrapped = true;
317     }
318     if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
319     {
320     rom_flow.ScrollControlIntoView(ctrl);
321     ctrl.Select();
322     found = true;
323     }
324     pc = ctrl;
325     }
326 william 30 }
327 william 60 }
328    
329     #region Background Workers
330    
331     private int GetFormWidth()
332     {
333     if (this.InvokeRequired)
334     {
335     return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
336     }
337     else
338     {
339     return this.Width;
340     }
341     }
342 william 113 //private void AddPlatformControl(Control c)
343     //{
344     // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
345     // else
346     // {
347     // platform_flow.Controls.Add(c);
348     // }
349     //}
350     //private void UpdatePlatformControls()
351     //{
352     // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
353     // //else { this.Update(); }
354     //}
355     //private void AddGameControl(Control c)
356     //{
357     // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
358     // else
359     // {
360     // rom_flow.Controls.Add(c);
361     // }
362     //}
363     //private void UpdateGameControls()
364     //{
365     // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
366     // //else { this.Update(); }
367     //}
368 william 60 #region gameWorker
369 william 110 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
370 william 137 private object gameimage_lock = new object();
371 william 60 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
372     {
373     Stopwatch t = e.Argument as Stopwatch;
374 william 112 RomParser parser = new RomParser(log, CurrentSelectedRom);
375 william 60
376     ProgressBar bar = new ProgressBar();
377 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(bar); }); }
378     else { rom_flow.Controls.Add(bar); }
379     //UpdateGameControls();
380     //Application.DoEvents();
381     //Thread.Sleep(10);
382 william 60 bar.Invoke(new MethodInvoker(delegate
383     {
384     //bar.Message = "Please Wait...";
385     //bar.ShowPercentageLabel = true;
386     bar.Margin = new System.Windows.Forms.Padding(0);
387     bar.Size = new Size(GetFormWidth() - 25, 100);
388     }));
389    
390     double count = 0;
391     double total_count = parser.Roms.Count;
392     foreach (IRomConfig config in parser.Roms)
393     {
394     GameControl game_ctrl = new GameControl();
395     game_ctrl.Dock = DockStyle.Top;
396     game_ctrl.Width = this.Width - 10;
397     game_ctrl.Tag = config;
398 william 137 try
399     {
400     lock (gameimage_lock)
401     {
402     game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
403     config.ReleaseRomImageResource();
404     }
405     }
406     catch (Exception ex)
407     {
408     throw ex;
409     }
410 william 60 game_ctrl.GameName = config.RomTitle;
411     game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
412     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
413     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
414 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
415     else { rom_flow.Controls.Add(game_ctrl); }
416     //UpdateGameControls();
417     //Application.DoEvents();
418     //int percent = (int)(100.0 * (count / total_count));
419     //log.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
420 william 60 bar.Invoke(new MethodInvoker(delegate
421     {
422 william 113 bar.Value = (int)(100.0 * (count / total_count));
423 william 60 }));
424     count++;
425     }
426     e.Result = t;
427 william 105 parser.Dispose();
428 william 60 }
429     private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
430     private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
431     {
432     Stopwatch t = e.Result as Stopwatch;
433     rom_flow.Controls.RemoveAt(0);
434     rom_flow.Controls[0].Select();
435     (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
436     t.Stop();
437 william 112 log.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
438 william 60 }
439     #endregion
440     #region platformWorker
441 william 110 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
442 william 137 private object platformimage_lock = new object();
443 william 60 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
444     {
445     Stopwatch t = e.Argument as Stopwatch;
446 william 112 PlatformParser parser = new PlatformParser(log, Config.RomPath);
447 william 60 double count = 0;
448     double total_count = parser.Platforms.Count;
449     ProgressBar bar = new ProgressBar();
450 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(bar); }); }
451     else { platform_flow.Controls.Add(bar); }
452     //UpdatePlatformControls();
453     //Application.DoEvents();
454     //Thread.Sleep(10);
455 william 60 bar.Invoke(new MethodInvoker(delegate
456     {
457     bar.Margin = new System.Windows.Forms.Padding(0);
458     bar.Size = new Size(GetFormWidth() - 25, 100);
459     }));
460     foreach (IEmuConfig config in parser.Platforms)
461     {
462     PlatformControl platform_ctrl = new PlatformControl();
463     platform_ctrl.Dock = DockStyle.Top;
464     platform_ctrl.Width = this.Width - 10;
465     platform_ctrl.Tag = config;
466 william 137 try
467     {
468     lock (platformimage_lock)
469     {
470     platform_ctrl.PlatformImage = config.PlatformImage == null ? DefaultPlatformImage : (Image)config.PlatformImage.Clone();
471     config.ReleasePlatformImageResource();
472     }
473     }
474     catch (Exception ex)
475     {
476     throw ex;
477     }
478 william 60 platform_ctrl.PlatformName = config.ToString();
479     platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
480     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
481     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
482 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
483     else { platform_flow.Controls.Add(platform_ctrl); }
484     //Application.DoEvents();
485     //int percent = (int)(100.0 * (count / total_count));
486     //log.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
487 william 60 bar.Invoke(new MethodInvoker(delegate
488     {
489 william 113 bar.Value = (int)(100.0 * (count / total_count));
490 william 60 }));
491     count++;
492     }
493     e.Result = t;
494 william 105 parser.Dispose();
495 william 60 }
496     private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
497     private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
498     {
499     Stopwatch t = e.Result as Stopwatch;
500     platform_flow.Controls.RemoveAt(0);
501     platform_flow.Controls[0].Select();
502     (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
503 william 112 log.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
504 william 60 }
505     #endregion
506    
507     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
508     {
509     Cursor.Show();
510 william 112 log.CloseLog();
511 william 60 }
512     #endregion
513 william 4 }
514     }

  ViewVC Help
Powered by ViewVC 1.1.22