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

Contents of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 184 - (show annotations) (download)
Mon Aug 4 09:58:06 2014 UTC (5 years, 11 months ago) by william
File size: 26610 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22