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

Contents of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 181 - (show annotations) (download)
Mon Aug 4 08:10:19 2014 UTC (6 years ago) by william
File size: 25763 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22