ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/branches/uienhancements/EmuXPortal/Form1.cs
Revision: 54
Committed: Sun Apr 8 13:25:33 2012 UTC (11 years, 5 months ago) by william
File size: 13993 byte(s)
Log Message:
update with progress bar support

File Contents

# User Rev Content
1 william 4 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 william 12 using EmuXPortal.Api;
10 william 17 using EmuXPortal.Logging;
11 william 23 using System.Diagnostics;
12 william 54 using System.Reflection;
13     using System.Threading;
14     using Utilities.TransparentControls;
15 william 4
16     namespace EmuXPortal
17     {
18     public partial class Form1 : Form
19     {
20 william 17 IEmuConfig CurrentSelectedRom = null;
21 william 31 PlatformControl CurrentPlatformControl = null;
22     GameControl CurrentGameControl = null;
23 william 4 public Form1()
24     {
25     InitializeComponent();
26 william 17 platform_flow.Dock = DockStyle.Fill;
27     rom_flow.Dock = DockStyle.Fill;
28 william 33
29 william 4 }
30 william 17
31     private void Form1_Load(object sender, EventArgs e)
32     {
33 william 33 Config.LoadConfig();
34 william 17 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 william 13
43 william 17 void platform_ctrl_GotFocus(object sender, EventArgs e)
44 william 13 {
45 william 17 PlatformControl c = sender as PlatformControl;
46     c.BorderStyle = BorderStyle.FixedSingle;
47 william 31 CurrentPlatformControl = c;
48 william 13 }
49 william 17
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 william 25 }
61     if (e.KeyCode == Keys.Back)
62     {
63     this.Close();
64     }
65 william 30 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 william 31 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 william 30 {
77 william 31 if (wrapped)
78 william 30 {
79 william 31 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 william 30 }
85 william 31 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 william 30 }
99     }
100 william 17 }
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 william 37 Stopwatch t = new Stopwatch();
108     t.Start();
109 william 54 platformWorker.RunWorkerAsync(t);
110 william 17 }
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 william 37 Stopwatch t = new Stopwatch();
118     t.Start();
119 william 54 gameWorker.RunWorkerAsync(t);
120 william 17 }
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 william 31 CurrentGameControl = c;
133 william 17 }
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 william 23 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 william 17 }
147     if (e.KeyCode == Keys.Back)
148     {
149     rom_flow.Visible = false;
150     platform_flow.Visible = true;
151     }
152 william 30
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 william 31 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 william 30 {
165 william 31 if (wrapped)
166 william 30 {
167 william 31 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 william 30 }
173 william 31 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 william 30 }
188 william 54 }
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 william 4 }
337     }