Ejemplo n.º 1
0
        private void Update_Keyboard(GameTime gameTime)
        {
            if (isShowingConnectDialog)
            {
                return;
            }

            var keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Up))
            {
                SelectUp(gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Down))
            {
                SelectDown(gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Enter))
            {
                switch (selectedMenuOption)
                {
                case MenuConstants.MenuOption.Server:
                    DoEnter(gameTime, () =>
                    {
                        if (OnServer != null)
                        {
                            OnServer();
                        }
                    });
                    break;

                case MenuConstants.MenuOption.Client:
                    // This wonky code is to allow the Windows Forms libraries to be side-loaded while our animation runs, so the user doesn't notice the delay. Crazy, but works.
                    var    dialogReadyEvent   = new System.Threading.AutoResetEvent(false);
                    object getConnectIPObject = null;
                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((o) =>
                    {
                        getConnectIPObject = new Win.Libs.GetConnectIPDialog();
                        dialogReadyEvent.Set();
                    }));
                    DoEnter(gameTime, () =>
                    {
                        isShowingConnectDialog = true;
                        using (dialogReadyEvent)
                        {
                            // In case the dialog isn't created yet, we'll wait for it.
                            dialogReadyEvent.WaitOne();
                            using (var getConnectIP = (Win.Libs.GetConnectIPDialog)getConnectIPObject)
                            {
                                if (getConnectIP.ShowDialog(System.Windows.Forms.Form.FromHandle(SharedResources.GameWindow.Handle)) == System.Windows.Forms.DialogResult.OK)
                                {
                                    OnClientConnect(new SimpleServerAddress()
                                    {
                                        Address = getConnectIP.Address,
                                        Port    = getConnectIP.Port
                                    });
                                    isShowingConnectDialog = false;
                                }
                                else
                                {
                                    TryRaiseOnExit();
                                    isShowingConnectDialog = false;
                                }
                            }
                        }
                    });
                    break;

                case MenuConstants.MenuOption.Exit:
                    DoEnter(gameTime, TryRaiseOnExit);
                    break;
                }
            }
            else if (keyboardState.IsKeyDown(Keys.Escape))
            {
                TryRaiseOnExit();
            }
        }
Ejemplo n.º 2
0
        private void Update_Keyboard(GameTime gameTime)
        {
            if (isShowingConnectDialog)
                return;

            var keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                SelectUp(gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Down))
            {
                SelectDown(gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Enter))
            {
                switch (selectedMenuOption)
                {
                    case MenuConstants.MenuOption.Server:
                        DoEnter(gameTime, () =>
                        {
                            if (OnServer != null)
                                OnServer();
                        });
                        break;
                    case MenuConstants.MenuOption.Client:
                        // This wonky code is to allow the Windows Forms libraries to be side-loaded while our animation runs, so the user doesn't notice the delay. Crazy, but works.
                        var dialogReadyEvent = new System.Threading.AutoResetEvent(false);
                        object getConnectIPObject = null;
                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((o) =>
                            {
                                getConnectIPObject = new Win.Libs.GetConnectIPDialog();
                                dialogReadyEvent.Set();
                            }));
                        DoEnter(gameTime, () =>
                                              {
                                                  isShowingConnectDialog = true;
                                                  using (dialogReadyEvent)
                                                  {
                                                      // In case the dialog isn't created yet, we'll wait for it.
                                                      dialogReadyEvent.WaitOne();
                                                      using (var getConnectIP = (Win.Libs.GetConnectIPDialog)getConnectIPObject)
                                                      {
                                                          if (getConnectIP.ShowDialog(System.Windows.Forms.Form.FromHandle(SharedResources.GameWindow.Handle)) == System.Windows.Forms.DialogResult.OK)
                                                          {
                                                              OnClientConnect(new SimpleServerAddress()
                                                              {
                                                                  Address = getConnectIP.Address,
                                                                  Port = getConnectIP.Port
                                                              });
                                                              isShowingConnectDialog = false;
                                                          }
                                                          else
                                                          {
                                                              TryRaiseOnExit();
                                                              isShowingConnectDialog = false;
                                                          }
                                                      }
                                                  }
                                              });
                        break;
                    case MenuConstants.MenuOption.Exit:
                        DoEnter(gameTime, TryRaiseOnExit);
                        break;
                }
            }
            else if (keyboardState.IsKeyDown(Keys.Escape))
            {
                TryRaiseOnExit();
            }
        }