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 |
|
281 |
p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe; |
282 |
p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(config) : config.Config.GameExeArgs; |
283 |
p.Start(); |
284 |
} |
285 |
if (e.KeyCode == Keys.Back) |
286 |
{ |
287 |
rom_flow.Visible = false; |
288 |
platform_flow.Visible = true; |
289 |
} |
290 |
|
291 |
if ( (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) || |
292 |
(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)) |
293 |
{ |
294 |
char t = (char)e.KeyCode; |
295 |
GameControl ctrl = (rom_flow.GetNextControl(CurrentPlatformControl, true) as GameControl); |
296 |
if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); } |
297 |
bool found = false; |
298 |
GameControl pc = CurrentGameControl; |
299 |
bool wrapped = false; |
300 |
bool not_found = true; |
301 |
while (!found) |
302 |
{ |
303 |
if (wrapped) |
304 |
{ |
305 |
foreach (Control ctl in rom_flow.Controls) |
306 |
{ |
307 |
GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; } |
308 |
} |
309 |
if (not_found) { found = true; } |
310 |
} |
311 |
ctrl = (rom_flow.GetNextControl(pc, true) as GameControl); |
312 |
if (ctrl == null) |
313 |
{ |
314 |
ctrl = rom_flow.Controls[0] as GameControl; |
315 |
wrapped = true; |
316 |
} |
317 |
if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower())) |
318 |
{ |
319 |
rom_flow.ScrollControlIntoView(ctrl); |
320 |
ctrl.Select(); |
321 |
found = true; |
322 |
} |
323 |
pc = ctrl; |
324 |
} |
325 |
} |
326 |
} |
327 |
|
328 |
#region Background Workers |
329 |
|
330 |
private int GetFormWidth() |
331 |
{ |
332 |
if (this.InvokeRequired) |
333 |
{ |
334 |
return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); })); |
335 |
} |
336 |
else |
337 |
{ |
338 |
return this.Width; |
339 |
} |
340 |
} |
341 |
private void AddPlatformControl(Control c) |
342 |
{ |
343 |
if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); } |
344 |
else |
345 |
{ |
346 |
platform_flow.Controls.Add(c); |
347 |
} |
348 |
} |
349 |
private void UpdatePlatformControls() |
350 |
{ |
351 |
if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); } |
352 |
else { this.Refresh(); this.Update(); } |
353 |
} |
354 |
private void AddGameControl(Control c) |
355 |
{ |
356 |
if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); } |
357 |
else |
358 |
{ |
359 |
rom_flow.Controls.Add(c); |
360 |
} |
361 |
} |
362 |
private void UpdateGameControls() |
363 |
{ |
364 |
if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); } |
365 |
else { this.Refresh(); this.Update(); } |
366 |
} |
367 |
#region gameWorker |
368 |
private void gameWorker_DoWork(object sender, DoWorkEventArgs e) |
369 |
{ |
370 |
Stopwatch t = e.Argument as Stopwatch; |
371 |
RomParser parser = new RomParser(CurrentSelectedRom); |
372 |
|
373 |
ProgressBar bar = new ProgressBar(); |
374 |
AddGameControl(bar); |
375 |
UpdateGameControls(); |
376 |
Application.DoEvents(); |
377 |
Thread.Sleep(10); |
378 |
bar.Invoke(new MethodInvoker(delegate |
379 |
{ |
380 |
//bar.Message = "Please Wait..."; |
381 |
//bar.ShowPercentageLabel = true; |
382 |
bar.Margin = new System.Windows.Forms.Padding(0); |
383 |
bar.Size = new Size(GetFormWidth() - 25, 100); |
384 |
})); |
385 |
|
386 |
double count = 0; |
387 |
double total_count = parser.Roms.Count; |
388 |
foreach (IRomConfig config in parser.Roms) |
389 |
{ |
390 |
GameControl game_ctrl = new GameControl(); |
391 |
game_ctrl.Dock = DockStyle.Top; |
392 |
game_ctrl.Width = this.Width - 10; |
393 |
game_ctrl.Tag = config; |
394 |
game_ctrl.GameImage = (Image)config.RomImage.Clone(); |
395 |
game_ctrl.GameName = config.RomTitle; |
396 |
game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown); |
397 |
game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus); |
398 |
game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus); |
399 |
AddGameControl(game_ctrl); |
400 |
UpdateGameControls(); |
401 |
Application.DoEvents(); |
402 |
int percent = (int)(100.0 * (count / total_count)); |
403 |
logger.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}",count,total_count,percent); |
404 |
bar.Invoke(new MethodInvoker(delegate |
405 |
{ |
406 |
bar.Value = percent; |
407 |
})); |
408 |
count++; |
409 |
} |
410 |
e.Result = t; |
411 |
parser.Dispose(); |
412 |
} |
413 |
private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { } |
414 |
private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
415 |
{ |
416 |
Stopwatch t = e.Result as Stopwatch; |
417 |
rom_flow.Controls.RemoveAt(0); |
418 |
rom_flow.Controls[0].Select(); |
419 |
(rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle; |
420 |
t.Stop(); |
421 |
logger.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds); |
422 |
} |
423 |
#endregion |
424 |
#region platformWorker |
425 |
private void platformWorker_DoWork(object sender, DoWorkEventArgs e) |
426 |
{ |
427 |
Stopwatch t = e.Argument as Stopwatch; |
428 |
PlatformParser parser = new PlatformParser(Config.RomPath); |
429 |
double count = 0; |
430 |
double total_count = parser.Platforms.Count; |
431 |
ProgressBar bar = new ProgressBar(); |
432 |
AddPlatformControl(bar); |
433 |
UpdatePlatformControls(); |
434 |
Application.DoEvents(); |
435 |
Thread.Sleep(10); |
436 |
bar.Invoke(new MethodInvoker(delegate |
437 |
{ |
438 |
bar.Margin = new System.Windows.Forms.Padding(0); |
439 |
bar.Size = new Size(GetFormWidth() - 25, 100); |
440 |
})); |
441 |
foreach (IEmuConfig config in parser.Platforms) |
442 |
{ |
443 |
PlatformControl platform_ctrl = new PlatformControl(); |
444 |
platform_ctrl.Dock = DockStyle.Top; |
445 |
platform_ctrl.Width = this.Width - 10; |
446 |
platform_ctrl.Tag = config; |
447 |
platform_ctrl.PlatformImage = (Image)config.PlatformImage.Clone(); |
448 |
platform_ctrl.PlatformName = config.ToString(); |
449 |
platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown); |
450 |
platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus); |
451 |
platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus); |
452 |
AddPlatformControl(platform_ctrl); |
453 |
Application.DoEvents(); |
454 |
int percent = (int)(100.0 * (count / total_count)); |
455 |
logger.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, percent); |
456 |
bar.Invoke(new MethodInvoker(delegate |
457 |
{ |
458 |
bar.Value = percent; |
459 |
})); |
460 |
count++; |
461 |
} |
462 |
e.Result = t; |
463 |
parser.Dispose(); |
464 |
} |
465 |
private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { } |
466 |
private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
467 |
{ |
468 |
Stopwatch t = e.Result as Stopwatch; |
469 |
platform_flow.Controls.RemoveAt(0); |
470 |
platform_flow.Controls[0].Select(); |
471 |
(platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle; |
472 |
logger.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds); |
473 |
} |
474 |
#endregion |
475 |
|
476 |
private void Form1_FormClosed(object sender, FormClosedEventArgs e) |
477 |
{ |
478 |
Cursor.Show(); |
479 |
} |
480 |
#endregion |
481 |
} |
482 |
} |