예제 #1
0
        /// <summary>
        /// Update all screens and handle handlers
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            _screensToUpdate = new List <GameScreen>(Screens);

            while (_screensToUpdate.Count > 0)
            {
                GameScreen currentScreen = _screensToUpdate[_screensToUpdate.Count - 1];

                _screensToUpdate.RemoveAt(_screensToUpdate.Count - 1);

                // Verifies if we have to delete the screen
                if (currentScreen.IsExiting == true)
                {
                    Screens.Remove(currentScreen);
                    continue;
                }

                // handles mouse input for the screen
                if (currentScreen is IMouseHandler)
                {
                    IMouseHandler handlerScreen     = (IMouseHandler)currentScreen;
                    MouseState    currentMouseState = Mouse.GetState();

                    if (currentMouseState.LeftButton == ButtonState.Pressed && _oldMouseState.LeftButton == ButtonState.Released)
                    {
                        handlerScreen.MouseLeftPressed(currentMouseState);
                    }

                    if (currentMouseState.LeftButton == ButtonState.Released && _oldMouseState.LeftButton == ButtonState.Pressed)
                    {
                        handlerScreen.MouseLeftReleased(currentMouseState);
                    }

                    if (currentMouseState.RightButton == ButtonState.Pressed && _oldMouseState.RightButton == ButtonState.Released)
                    {
                        handlerScreen.MouseRightPressed(currentMouseState);
                    }

                    if (currentMouseState.RightButton == ButtonState.Released && _oldMouseState.RightButton == ButtonState.Pressed)
                    {
                        handlerScreen.MouseRightReleased(currentMouseState);
                    }

                    _oldMouseState = currentMouseState;
                }

                // handles keyboard input for the screen
                if (currentScreen is IKeyboardHandler)
                {
                    IKeyboardHandler handlerScreen        = (IKeyboardHandler)currentScreen;
                    KeyboardState    currentKeyboardState = Keyboard.GetState();

                    Keys[] pressedKeys = currentKeyboardState.GetPressedKeys();

                    handlerScreen.KeysPressed(pressedKeys);
                }

                // handles socket connection for the screen
                if (currentScreen is ISocketHandler)
                {
                    ISocketHandler handlerScreen = (ISocketHandler)currentScreen;
                    var            status        = currentScreen.Socket.Connection.Status;

                    // check for no status
                    if (status != null)
                    {
                        if (status == ConnStatus.Disconnected)
                        {
                            handlerScreen.OnDisconnect(_statusBefore, gameTime);
                        }

                        if (status == ConnStatus.Connecting)
                        {
                            handlerScreen.OnConnecting(_statusBefore, gameTime);
                        }

                        if (status == ConnStatus.Connected)
                        {
                            handlerScreen.OnConnected(_statusBefore, gameTime);
                        }

                        _statusBefore = status;
                    }
                }

                // handles socket datas
                if (currentScreen is ISocketDataHandler)
                {
                    ISocketDataHandler handlerScreen = (ISocketDataHandler)currentScreen;
                    handlerScreen.Receive(ReceiveData);
                    // Select just data that can't be deleted
                    ReceiveData = new List <SocketData>(from d in ReceiveData
                                                        where d.IsDeleting == false
                                                        select d);
                }

                currentScreen.Update(gameTime);
            }
        }