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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations) (download)
Thu Aug 23 04:47:35 2012 UTC (7 years, 5 months ago) by william
Original Path: trunk/EmuXPortal/Form1.cs
File size: 21148 byte(s)
+ BUGFIX (1.0.0.4): properly send roms to emulator

1 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 using EmuXPortal.Api;
10 using EmuXPortal.Logging;
11 using System.Diagnostics;
12 using System.Reflection;
13 using System.Threading;
14
15 namespace EmuXPortal
16 {
17 public partial class Form1 : Form
18 {
19 IEmuConfig CurrentSelectedRom = null;
20 PlatformControl CurrentPlatformControl = null;
21 GameControl CurrentGameControl = null;
22 logger log = new logger();
23 public Form1()
24 {
25 InitializeComponent();
26 platform_flow.Dock = DockStyle.Fill;
27 rom_flow.Dock = DockStyle.Fill;
28 log.OpenLog();
29 }
30
31 private void Form1_Load(object sender, EventArgs e)
32 {
33 Config.LoadConfig(log);
34 Config.InitializePresentationForm(log,this);
35 }
36 private void Form1_Shown(object sender, EventArgs e) { platform_flow.Visible = true; Cursor.Hide(); }
37 void platform_ctrl_LostFocus(object sender, EventArgs e)
38 {
39 PlatformControl c = sender as PlatformControl;
40 c.BorderStyle = BorderStyle.None;
41 }
42
43 void platform_ctrl_GotFocus(object sender, EventArgs e)
44 {
45 PlatformControl c = sender as PlatformControl;
46 c.BorderStyle = BorderStyle.FixedSingle;
47 CurrentPlatformControl = c;
48 }
49
50 void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
51 {
52 PlatformControl c = sender as PlatformControl;
53 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 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 }
120 if (e.KeyCode == Keys.Back)
121 {
122 this.Close();
123 }
124 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 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 {
136 if (wrapped)
137 {
138 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 }
144 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 }
158 }
159 }
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 Stopwatch t = new Stopwatch();
167 t.Start();
168 platformWorker.RunWorkerAsync(t);
169 }
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 Stopwatch t = new Stopwatch();
177 t.Start();
178 gameWorker.RunWorkerAsync(t);
179 }
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 CurrentGameControl = c;
192 }
193
194 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 void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
215 {
216 GameControl c = sender as GameControl;
217
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 if (e.KeyCode == Keys.Enter)
277 {
278 IRomConfig config = c.Tag as IRomConfig;
279
280 Process p = new Process();
281
282 p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
283 p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(log, config) : config.Config.GameExeArgs;
284 p.Start();
285 }
286 if (e.KeyCode == Keys.Back)
287 {
288 rom_flow.Visible = false;
289 platform_flow.Visible = true;
290 }
291
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 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 {
304 if (wrapped)
305 {
306 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 }
312 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 }
327 }
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 //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 #region gameWorker
369 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
370 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
371 {
372 Stopwatch t = e.Argument as Stopwatch;
373 RomParser parser = new RomParser(log, CurrentSelectedRom);
374
375 ProgressBar bar = new ProgressBar();
376 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(bar); }); }
377 else { rom_flow.Controls.Add(bar); }
378 //UpdateGameControls();
379 //Application.DoEvents();
380 //Thread.Sleep(10);
381 bar.Invoke(new MethodInvoker(delegate
382 {
383 //bar.Message = "Please Wait...";
384 //bar.ShowPercentageLabel = true;
385 bar.Margin = new System.Windows.Forms.Padding(0);
386 bar.Size = new Size(GetFormWidth() - 25, 100);
387 }));
388
389 double count = 0;
390 double total_count = parser.Roms.Count;
391 foreach (IRomConfig config in parser.Roms)
392 {
393 GameControl game_ctrl = new GameControl();
394 game_ctrl.Dock = DockStyle.Top;
395 game_ctrl.Width = this.Width - 10;
396 game_ctrl.Tag = config;
397 game_ctrl.GameImage = config.RomImage == null ? DefaultGameImage : (Image)config.RomImage.Clone();
398 config.ReleaseRomImageResource();
399 game_ctrl.GameName = config.RomTitle;
400 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
401 game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
402 game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
403 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
404 else { rom_flow.Controls.Add(game_ctrl); }
405 //UpdateGameControls();
406 //Application.DoEvents();
407 //int percent = (int)(100.0 * (count / total_count));
408 //log.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
409 bar.Invoke(new MethodInvoker(delegate
410 {
411 bar.Value = (int)(100.0 * (count / total_count));
412 }));
413 count++;
414 }
415 e.Result = t;
416 parser.Dispose();
417 }
418 private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
419 private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
420 {
421 Stopwatch t = e.Result as Stopwatch;
422 rom_flow.Controls.RemoveAt(0);
423 rom_flow.Controls[0].Select();
424 (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
425 t.Stop();
426 log.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
427 }
428 #endregion
429 #region platformWorker
430 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
431 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
432 {
433 Stopwatch t = e.Argument as Stopwatch;
434 PlatformParser parser = new PlatformParser(log, Config.RomPath);
435 double count = 0;
436 double total_count = parser.Platforms.Count;
437 ProgressBar bar = new ProgressBar();
438 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(bar); }); }
439 else { platform_flow.Controls.Add(bar); }
440 //UpdatePlatformControls();
441 //Application.DoEvents();
442 //Thread.Sleep(10);
443 bar.Invoke(new MethodInvoker(delegate
444 {
445 bar.Margin = new System.Windows.Forms.Padding(0);
446 bar.Size = new Size(GetFormWidth() - 25, 100);
447 }));
448 foreach (IEmuConfig config in parser.Platforms)
449 {
450 PlatformControl platform_ctrl = new PlatformControl();
451 platform_ctrl.Dock = DockStyle.Top;
452 platform_ctrl.Width = this.Width - 10;
453 platform_ctrl.Tag = config;
454 platform_ctrl.PlatformImage = config.PlatformImage == null ? DefaultPlatformImage : (Image)config.PlatformImage.Clone();
455 config.ReleasePlatformImageResource();
456 platform_ctrl.PlatformName = config.ToString();
457 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
458 platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
459 platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
460 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
461 else { platform_flow.Controls.Add(platform_ctrl); }
462 //Application.DoEvents();
463 //int percent = (int)(100.0 * (count / total_count));
464 //log.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, (int)(100.0 * (count / total_count)));
465 bar.Invoke(new MethodInvoker(delegate
466 {
467 bar.Value = (int)(100.0 * (count / total_count));
468 }));
469 count++;
470 }
471 e.Result = t;
472 parser.Dispose();
473 }
474 private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
475 private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
476 {
477 Stopwatch t = e.Result as Stopwatch;
478 platform_flow.Controls.RemoveAt(0);
479 platform_flow.Controls[0].Select();
480 (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
481 log.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
482 }
483 #endregion
484
485 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
486 {
487 Cursor.Show();
488 log.CloseLog();
489 }
490 #endregion
491 }
492 }

  ViewVC Help
Powered by ViewVC 1.1.22