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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations) (download)
Sun Apr 8 13:25:33 2012 UTC (9 years, 6 months ago) by william
File size: 13993 byte(s)
update with progress bar support

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

  ViewVC Help
Powered by ViewVC 1.1.22