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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 176 - (show annotations) (download)
Mon Aug 4 06:40:59 2014 UTC (5 years, 6 months ago) by william
Original Path: trunk/EmuXPortal/Form1.cs
File size: 25033 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22