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; } |
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 |
if (e.KeyCode == Keys.Enter) |
53 |
{ |
54 |
// load this platform |
55 |
platform_flow.Visible = false; |
56 |
CurrentSelectedRom = c.Tag as IEmuConfig; |
57 |
rom_flow.Visible = true; |
58 |
rom_flow.BringToFront(); |
59 |
} |
60 |
if (e.KeyCode == Keys.Back) |
61 |
{ |
62 |
this.Close(); |
63 |
} |
64 |
if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) || |
65 |
(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)) |
66 |
{ |
67 |
char t = (char)e.KeyCode; |
68 |
PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl); |
69 |
if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); } |
70 |
bool found = false; |
71 |
PlatformControl pc = CurrentPlatformControl; |
72 |
bool wrapped = false; |
73 |
bool not_found = true; |
74 |
while (!found) |
75 |
{ |
76 |
if (wrapped) |
77 |
{ |
78 |
foreach (Control ctl in platform_flow.Controls) |
79 |
{ |
80 |
PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; } |
81 |
} |
82 |
if (not_found) { found = true; } |
83 |
} |
84 |
ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl); |
85 |
if (ctrl == null) |
86 |
{ |
87 |
ctrl = platform_flow.Controls[0] as PlatformControl; |
88 |
wrapped = true; |
89 |
} |
90 |
if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) |
91 |
{ |
92 |
platform_flow.ScrollControlIntoView(ctrl); |
93 |
ctrl.Select(); |
94 |
found = true; |
95 |
} |
96 |
pc = ctrl; |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
private void platform_flow_VisibleChanged(object sender, EventArgs e) |
102 |
{ |
103 |
if (!platform_flow.Visible) return; |
104 |
platform_flow.Controls.Clear(); |
105 |
platform_flow.BringToFront(); |
106 |
Stopwatch t = new Stopwatch(); |
107 |
t.Start(); |
108 |
platformWorker.RunWorkerAsync(t); |
109 |
} |
110 |
|
111 |
private void rom_flow_VisibleChanged(object sender, EventArgs e) |
112 |
{ |
113 |
if (!rom_flow.Visible) return; |
114 |
rom_flow.Controls.Clear(); |
115 |
rom_flow.BringToFront(); |
116 |
Stopwatch t = new Stopwatch(); |
117 |
t.Start(); |
118 |
gameWorker.RunWorkerAsync(t); |
119 |
} |
120 |
|
121 |
void game_ctrl_LostFocus(object sender, EventArgs e) |
122 |
{ |
123 |
GameControl c = sender as GameControl; |
124 |
c.BorderStyle = BorderStyle.None; |
125 |
} |
126 |
|
127 |
void game_ctrl_GotFocus(object sender, EventArgs e) |
128 |
{ |
129 |
GameControl c = sender as GameControl; |
130 |
c.BorderStyle = BorderStyle.FixedSingle; |
131 |
CurrentGameControl = c; |
132 |
} |
133 |
|
134 |
void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) |
135 |
{ |
136 |
GameControl c = sender as GameControl; |
137 |
if (e.KeyCode == Keys.Enter) |
138 |
{ |
139 |
IRomConfig config = c.Tag as IRomConfig; |
140 |
|
141 |
Process p = new Process(); |
142 |
p.StartInfo.FileName = config.Config.EmuPath; |
143 |
p.StartInfo.Arguments = EmuConfigLoader.GetEMUOptions(config); |
144 |
p.Start(); |
145 |
} |
146 |
if (e.KeyCode == Keys.Back) |
147 |
{ |
148 |
rom_flow.Visible = false; |
149 |
platform_flow.Visible = true; |
150 |
} |
151 |
|
152 |
if ( (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) || |
153 |
(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)) |
154 |
{ |
155 |
char t = (char)e.KeyCode; |
156 |
GameControl ctrl = (rom_flow.GetNextControl(CurrentPlatformControl, true) as GameControl); |
157 |
if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); } |
158 |
bool found = false; |
159 |
GameControl pc = CurrentGameControl; |
160 |
bool wrapped = false; |
161 |
bool not_found = true; |
162 |
while (!found) |
163 |
{ |
164 |
if (wrapped) |
165 |
{ |
166 |
foreach (Control ctl in rom_flow.Controls) |
167 |
{ |
168 |
GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; } |
169 |
} |
170 |
if (not_found) { found = true; } |
171 |
} |
172 |
ctrl = (rom_flow.GetNextControl(pc, true) as GameControl); |
173 |
if (ctrl == null) |
174 |
{ |
175 |
ctrl = rom_flow.Controls[0] as GameControl; |
176 |
wrapped = true; |
177 |
} |
178 |
if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower())) |
179 |
{ |
180 |
rom_flow.ScrollControlIntoView(ctrl); |
181 |
ctrl.Select(); |
182 |
found = true; |
183 |
} |
184 |
pc = ctrl; |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
#region Background Workers |
190 |
|
191 |
private int GetFormWidth() |
192 |
{ |
193 |
if (this.InvokeRequired) |
194 |
{ |
195 |
return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); })); |
196 |
} |
197 |
else |
198 |
{ |
199 |
return this.Width; |
200 |
} |
201 |
} |
202 |
private void AddPlatformControl(Control c) |
203 |
{ |
204 |
if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); } |
205 |
else |
206 |
{ |
207 |
platform_flow.Controls.Add(c); |
208 |
} |
209 |
} |
210 |
private void UpdatePlatformControls() |
211 |
{ |
212 |
if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); } |
213 |
else { this.Refresh(); this.Update(); } |
214 |
} |
215 |
private void AddGameControl(Control c) |
216 |
{ |
217 |
if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); } |
218 |
else |
219 |
{ |
220 |
rom_flow.Controls.Add(c); |
221 |
} |
222 |
} |
223 |
private void UpdateGameControls() |
224 |
{ |
225 |
if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); } |
226 |
else { this.Refresh(); this.Update(); } |
227 |
} |
228 |
#region gameWorker |
229 |
private void gameWorker_DoWork(object sender, DoWorkEventArgs e) |
230 |
{ |
231 |
Stopwatch t = e.Argument as Stopwatch; |
232 |
RomParser parser = new RomParser(CurrentSelectedRom); |
233 |
|
234 |
ProgressBar bar = new ProgressBar(); |
235 |
AddGameControl(bar); |
236 |
UpdateGameControls(); |
237 |
Application.DoEvents(); |
238 |
Thread.Sleep(10); |
239 |
bar.Invoke(new MethodInvoker(delegate |
240 |
{ |
241 |
//bar.Message = "Please Wait..."; |
242 |
//bar.ShowPercentageLabel = true; |
243 |
bar.Margin = new System.Windows.Forms.Padding(0); |
244 |
bar.Size = new Size(GetFormWidth() - 25, 100); |
245 |
})); |
246 |
|
247 |
double count = 0; |
248 |
double total_count = parser.Roms.Count; |
249 |
foreach (IRomConfig config in parser.Roms) |
250 |
{ |
251 |
GameControl game_ctrl = new GameControl(); |
252 |
game_ctrl.Dock = DockStyle.Top; |
253 |
game_ctrl.Width = this.Width - 10; |
254 |
game_ctrl.Tag = config; |
255 |
game_ctrl.GameImage = config.RomImage; |
256 |
game_ctrl.GameName = config.RomTitle; |
257 |
game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown); |
258 |
game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus); |
259 |
game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus); |
260 |
AddGameControl(game_ctrl); |
261 |
UpdateGameControls(); |
262 |
Application.DoEvents(); |
263 |
int percent = (int)(100.0 * (count / total_count)); |
264 |
logger.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}",count,total_count,percent); |
265 |
bar.Invoke(new MethodInvoker(delegate |
266 |
{ |
267 |
bar.Value = percent; |
268 |
})); |
269 |
count++; |
270 |
} |
271 |
e.Result = t; |
272 |
} |
273 |
private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { } |
274 |
private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
275 |
{ |
276 |
Stopwatch t = e.Result as Stopwatch; |
277 |
rom_flow.Controls.RemoveAt(0); |
278 |
rom_flow.Controls[0].Select(); |
279 |
(rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle; |
280 |
t.Stop(); |
281 |
logger.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds); |
282 |
} |
283 |
#endregion |
284 |
#region platformWorker |
285 |
private void platformWorker_DoWork(object sender, DoWorkEventArgs e) |
286 |
{ |
287 |
Stopwatch t = e.Argument as Stopwatch; |
288 |
PlatformParser parser = new PlatformParser(Config.RomPath); |
289 |
double count = 0; |
290 |
double total_count = parser.Platforms.Count; |
291 |
ProgressBar bar = new ProgressBar(); |
292 |
AddPlatformControl(bar); |
293 |
UpdatePlatformControls(); |
294 |
Application.DoEvents(); |
295 |
Thread.Sleep(10); |
296 |
bar.Invoke(new MethodInvoker(delegate |
297 |
{ |
298 |
bar.Margin = new System.Windows.Forms.Padding(0); |
299 |
bar.Size = new Size(GetFormWidth() - 25, 100); |
300 |
})); |
301 |
foreach (IEmuConfig config in parser.Platforms) |
302 |
{ |
303 |
PlatformControl platform_ctrl = new PlatformControl(); |
304 |
platform_ctrl.Dock = DockStyle.Top; |
305 |
platform_ctrl.Width = this.Width - 10; |
306 |
platform_ctrl.Tag = config; |
307 |
platform_ctrl.PlatformImage = config.PlatformImage; |
308 |
platform_ctrl.PlatformName = config.ToString(); |
309 |
platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown); |
310 |
platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus); |
311 |
platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus); |
312 |
AddPlatformControl(platform_ctrl); |
313 |
Application.DoEvents(); |
314 |
int percent = (int)(100.0 * (count / total_count)); |
315 |
logger.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, percent); |
316 |
bar.Invoke(new MethodInvoker(delegate |
317 |
{ |
318 |
bar.Value = percent; |
319 |
})); |
320 |
count++; |
321 |
} |
322 |
e.Result = t; |
323 |
} |
324 |
private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { } |
325 |
private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
326 |
{ |
327 |
Stopwatch t = e.Result as Stopwatch; |
328 |
platform_flow.Controls.RemoveAt(0); |
329 |
platform_flow.Controls[0].Select(); |
330 |
(platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle; |
331 |
logger.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds); |
332 |
} |
333 |
#endregion |
334 |
#endregion |
335 |
} |
336 |
} |