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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22