/[EmuXPortal]/trunk/EmuXPortal/Form1.cs
ViewVC logotype

Diff of /trunk/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/EmuXPortal/Form1.cs	2012/04/04 07:21:54	37
+++ trunk/EmuXPortal/Form1.cs	2012/04/08 14:34:07	60
@@ -9,6 +9,8 @@
 using EmuXPortal.Api;
 using EmuXPortal.Logging;
 using System.Diagnostics;
+using System.Reflection;
+using System.Threading;
 
 namespace EmuXPortal
 {
@@ -30,7 +32,7 @@
             Config.LoadConfig();
             Config.InitializePresentationForm(this);           
         }
-        private void Form1_Shown(object sender, EventArgs e) { platform_flow.Visible = true; }
+        private void Form1_Shown(object sender, EventArgs e) { platform_flow.Visible = true; Cursor.Hide(); }
         void platform_ctrl_LostFocus(object sender, EventArgs e)
         {
             PlatformControl c = sender as PlatformControl;
@@ -47,6 +49,65 @@
         void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
         {
             PlatformControl c = sender as PlatformControl;
+            int changeAmount = 0;
+            int currentPosition = 0;
+            if (e.KeyCode == Keys.Home)
+            {
+                platform_flow.Controls[0].Select();
+                platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
+            }
+            if (e.KeyCode == Keys.End)
+            {
+                platform_flow.Controls[platform_flow.Controls.Count -1].Select();
+                platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
+            }
+            if (e.KeyCode == Keys.PageUp)
+            {
+
+
+                changeAmount = platform_flow.VerticalScroll.LargeChange;
+                currentPosition = platform_flow.VerticalScroll.Value;
+                if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
+                {
+                    platform_flow.VerticalScroll.Value -= changeAmount;
+                }
+                else
+                {
+                    platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
+                }
+                PlatformControl s = platform_ctrl_get_last_visible();
+                s.Select();
+                platform_flow.ScrollControlIntoView(s);
+                platform_flow.PerformLayout();
+            }
+            if (e.KeyCode == Keys.PageDown)
+            {
+                changeAmount = platform_flow.VerticalScroll.LargeChange;
+                currentPosition = platform_flow.VerticalScroll.Value;
+                if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
+                {
+                    try
+                    {
+                        platform_flow.VerticalScroll.Value += changeAmount;
+                    }
+                    catch
+                    {
+                        platform_flow.Controls[0].Select();
+                        platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
+                        rom_flow.PerformLayout();
+                        return;
+                    }
+                }
+                else
+                {
+                    platform_flow.Controls[0].Select();
+                    platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
+                }
+                PlatformControl s = platform_ctrl_get_last_visible();
+                s.Select();
+                platform_flow.ScrollControlIntoView(s);
+                platform_flow.PerformLayout();
+            }
             if (e.KeyCode == Keys.Enter)
             {
                 // load this platform
@@ -103,24 +164,7 @@
             platform_flow.BringToFront();
             Stopwatch t = new Stopwatch();
             t.Start();
-            PlatformParser parser = new PlatformParser(Config.RomPath);           
-            foreach (IEmuConfig config in parser.Platforms)
-            {
-                PlatformControl platform_ctrl = new PlatformControl();
-                platform_ctrl.Dock = DockStyle.Top;
-                platform_ctrl.Width = this.Width - 10;
-                platform_ctrl.Tag = config;
-                platform_ctrl.PlatformImage = config.PlatformImage;
-                platform_ctrl.PlatformName = config.ToString();
-                platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
-                platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
-                platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
-                platform_flow.Controls.Add(platform_ctrl);
-
-            }
-            platform_flow.Controls[0].Select();
-            (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
-            logger.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
+            platformWorker.RunWorkerAsync(t);
         }
 
         private void rom_flow_VisibleChanged(object sender, EventArgs e)
@@ -130,25 +174,7 @@
             rom_flow.BringToFront();
             Stopwatch t = new Stopwatch();
             t.Start();
-            RomParser parser = new RomParser(CurrentSelectedRom);
-            foreach (IRomConfig config in parser.Roms)
-            {
-                GameControl game_ctrl = new GameControl();
-                game_ctrl.Dock = DockStyle.Top;
-                game_ctrl.Width = this.Width - 10;
-                game_ctrl.Tag = config;
-                game_ctrl.GameImage = config.RomImage;
-                game_ctrl.GameName = config.RomTitle;
-                game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
-                game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
-                game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
-                rom_flow.Controls.Add(game_ctrl);
-                rom_flow.Update();    
-            }
-            rom_flow.Controls[0].Select();
-            (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
-            t.Stop();
-            logger.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
+            gameWorker.RunWorkerAsync(t);
         }
 
         void game_ctrl_LostFocus(object sender, EventArgs e)
@@ -164,9 +190,88 @@
             CurrentGameControl = c;
         }
 
+        private GameControl game_ctrl_get_last_visible()
+        {
+            GameControl s = new GameControl();
+            foreach (GameControl c in rom_flow.Controls)
+            {
+                if (c.Bounds.IntersectsWith(rom_flow.Bounds))
+                    s = c;
+            }
+            return s;
+        }
+        private PlatformControl platform_ctrl_get_last_visible()
+        {
+            PlatformControl s = new PlatformControl();
+            foreach (PlatformControl c in platform_flow.Controls)
+            {
+                if (c.Bounds.IntersectsWith(platform_flow.Bounds))
+                    s = c;
+            }
+            return s;
+        }
         void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
         {
             GameControl c = sender as GameControl;
+
+            int changeAmount = 0;
+            int currentPosition = 0;
+            if (e.KeyCode == Keys.Home)
+            {
+                rom_flow.Controls[0].Select();
+                rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
+            }
+            if (e.KeyCode == Keys.End)
+            {
+                rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
+                rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
+            }
+            if (e.KeyCode == Keys.PageUp)
+            {
+                changeAmount = rom_flow.VerticalScroll.LargeChange;
+                currentPosition = rom_flow.VerticalScroll.Value;
+                if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
+                {
+                    try
+                    {
+                        rom_flow.VerticalScroll.Value += changeAmount;
+                    }
+                    catch
+                    {
+                        rom_flow.Controls[0].Select();
+                        rom_flow.ScrollControlIntoView(platform_flow.Controls[0]);
+                        rom_flow.PerformLayout();
+                        return;
+                    }
+                }
+                else
+                {
+                    rom_flow.Controls[0].Select();
+                    rom_flow.ScrollControlIntoView(platform_flow.Controls[0]);
+                }
+                GameControl s = game_ctrl_get_last_visible();
+                s.Select();
+                rom_flow.ScrollControlIntoView(s);
+                rom_flow.PerformLayout();
+            }
+            if (e.KeyCode == Keys.PageDown)
+            {
+                changeAmount = rom_flow.VerticalScroll.LargeChange;
+                currentPosition = rom_flow.VerticalScroll.Value;
+                if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
+                {
+                    rom_flow.VerticalScroll.Value += changeAmount;
+                }
+                else
+                {
+                    rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
+                }
+                GameControl s = game_ctrl_get_last_visible();
+                s.Select();
+                rom_flow.ScrollControlIntoView(s);
+                rom_flow.PerformLayout();
+            }
+
             if (e.KeyCode == Keys.Enter)
             {
                 IRomConfig config = c.Tag as IRomConfig;
@@ -217,6 +322,158 @@
                     pc = ctrl;
                 }           
             }
-        }    
+        }
+
+        #region Background Workers
+
+        private int GetFormWidth()
+        {
+            if (this.InvokeRequired)
+            {
+                return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
+            }
+            else
+            {
+                return this.Width;
+            }
+        }
+        private void AddPlatformControl(Control c)
+        {          
+            if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
+            else 
+            {
+                platform_flow.Controls.Add(c);            
+            }            
+        }
+        private void UpdatePlatformControls() 
+        {
+            if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
+            else { this.Refresh(); this.Update(); }
+        }
+        private void AddGameControl(Control c)
+        {
+            if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
+            else
+            {              
+                rom_flow.Controls.Add(c);
+            }
+        }
+        private void UpdateGameControls()
+        {
+            if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
+            else { this.Refresh(); this.Update(); }
+        }
+        #region gameWorker
+        private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
+        {
+            Stopwatch t = e.Argument as Stopwatch;
+            RomParser parser = new RomParser(CurrentSelectedRom);
+
+            ProgressBar bar = new ProgressBar();
+            AddGameControl(bar);
+            UpdateGameControls();
+            Application.DoEvents();
+            Thread.Sleep(10);
+            bar.Invoke(new MethodInvoker(delegate
+            {
+                //bar.Message = "Please Wait...";
+                //bar.ShowPercentageLabel = true;
+                bar.Margin = new System.Windows.Forms.Padding(0);
+                bar.Size = new Size(GetFormWidth() - 25, 100);
+            }));
+            
+            double count = 0;
+            double total_count = parser.Roms.Count;
+            foreach (IRomConfig config in parser.Roms)
+            {
+                GameControl game_ctrl = new GameControl();
+                game_ctrl.Dock = DockStyle.Top;
+                game_ctrl.Width = this.Width - 10;
+                game_ctrl.Tag = config;
+                game_ctrl.GameImage = config.RomImage;
+                game_ctrl.GameName = config.RomTitle;
+                game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
+                game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
+                game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
+                AddGameControl(game_ctrl);
+                UpdateGameControls();
+                Application.DoEvents();
+                int percent = (int)(100.0 * (count / total_count));
+                logger.WriteLine("gameWorker_DoWork(): count={0} total={1} percent={2}",count,total_count,percent);
+                bar.Invoke(new MethodInvoker(delegate
+                {
+                    bar.Value = percent;
+                }));
+                count++;
+            }
+            e.Result = t;
+        }
+        private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
+        private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
+        {
+            Stopwatch t = e.Result as Stopwatch;
+            rom_flow.Controls.RemoveAt(0);
+            rom_flow.Controls[0].Select();
+            (rom_flow.Controls[0] as GameControl).BorderStyle = BorderStyle.FixedSingle;
+            t.Stop();
+            logger.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
+        }
+        #endregion
+        #region platformWorker
+        private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
+        {
+            Stopwatch t = e.Argument as Stopwatch;
+            PlatformParser parser = new PlatformParser(Config.RomPath);
+            double count = 0;
+            double total_count = parser.Platforms.Count;
+            ProgressBar bar = new ProgressBar();
+             AddPlatformControl(bar);
+            UpdatePlatformControls();
+            Application.DoEvents();
+            Thread.Sleep(10);
+            bar.Invoke(new MethodInvoker(delegate
+            {
+                bar.Margin = new System.Windows.Forms.Padding(0);
+                bar.Size = new Size(GetFormWidth() - 25, 100);
+            }));
+            foreach (IEmuConfig config in parser.Platforms)
+            {
+                PlatformControl platform_ctrl = new PlatformControl();
+                platform_ctrl.Dock = DockStyle.Top;
+                platform_ctrl.Width = this.Width - 10;
+                platform_ctrl.Tag = config;
+                platform_ctrl.PlatformImage = config.PlatformImage;
+                platform_ctrl.PlatformName = config.ToString();
+                platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
+                platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
+                platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
+                AddPlatformControl(platform_ctrl);
+                Application.DoEvents();
+                int percent = (int)(100.0 * (count / total_count));
+                logger.WriteLine("platformWorker_DoWork(): count={0} total={1} percent={2}", count, total_count, percent);
+                bar.Invoke(new MethodInvoker(delegate
+                {
+                    bar.Value = percent;
+                }));
+                count++;
+            }
+            e.Result = t;
+        }
+        private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
+        private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
+        {
+            Stopwatch t = e.Result as Stopwatch;
+            platform_flow.Controls.RemoveAt(0);
+            platform_flow.Controls[0].Select();
+            (platform_flow.Controls[0] as PlatformControl).BorderStyle = BorderStyle.FixedSingle;
+            logger.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
+        }
+        #endregion
+
+        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
+        {
+            Cursor.Show();
+        }
+        #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22