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

Contents of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 112 - (show annotations) (download)
Thu Aug 23 04:13:19 2012 UTC (8 years, 1 month ago) by william
File size: 20330 byte(s)
OPTIMIZE LOGGING

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

  ViewVC Help
Powered by ViewVC 1.1.22