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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22