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