Beispiel #1
0
        /// <summary>Update the current button states for the given tick.</summary>
        public void TrueUpdate()
        {
            // update base state
            base.Update();

            // update SMAPI extended data
            // note: Stardew Valley is *not* in UI mode when this code runs
            try
            {
                float zoomMultiplier = (1f / Game1.options.zoomLevel);

                // get real values
                var               controller        = new GamePadStateBuilder(base.GetGamePadState());
                var               keyboard          = new KeyboardStateBuilder(base.GetKeyboardState());
                var               mouse             = new MouseStateBuilder(base.GetMouseState());
                Vector2           cursorAbsolutePos = new Vector2((mouse.X * zoomMultiplier) + Game1.viewport.X, (mouse.Y * zoomMultiplier) + Game1.viewport.Y);
                Vector2?          playerTilePos     = Context.IsPlayerFree ? Game1.player.getTileLocation() : (Vector2?)null;
                HashSet <SButton> reallyDown        = new HashSet <SButton>(this.GetPressedButtons(keyboard, mouse, controller));

                // apply overrides
                bool hasOverrides = false;
                if (this.CustomPressedKeys.Count > 0 || this.CustomReleasedKeys.Count > 0)
                {
                    // reset overrides that no longer apply
                    this.CustomPressedKeys.RemoveWhere(key => reallyDown.Contains(key));
                    this.CustomReleasedKeys.RemoveWhere(key => !reallyDown.Contains(key));

                    // apply overrides
                    if (this.ApplyOverrides(this.CustomPressedKeys, this.CustomReleasedKeys, controller, keyboard, mouse))
                    {
                        hasOverrides = true;
                    }

                    // remove pressed keys
                    this.CustomPressedKeys.Clear();
                }

                // get button states
                var pressedButtons = hasOverrides
                    ? new HashSet <SButton>(this.GetPressedButtons(keyboard, mouse, controller))
                    : reallyDown;
                var activeButtons = this.DeriveStates(this.ButtonStates, pressedButtons);

                // update
                this.HasNewOverrides = false;
                this.ControllerState = controller.GetState();
                this.KeyboardState   = keyboard.GetState();
                this.MouseState      = mouse.GetState();
                this.ButtonStates    = activeButtons;
                if (cursorAbsolutePos != this.CursorPositionImpl?.AbsolutePixels || playerTilePos != this.LastPlayerTile)
                {
                    this.LastPlayerTile     = playerTilePos;
                    this.CursorPositionImpl = this.GetCursorPosition(this.MouseState, cursorAbsolutePos, zoomMultiplier);
                }
            }
            catch (InvalidOperationException)
            {
                // GetState() may crash for some players if window doesn't have focus but game1.IsActive == true
            }
        }
Beispiel #2
0
        /// <summary>Apply input overrides to the current state.</summary>
        public void ApplyOverrides()
        {
            if (this.HasNewOverrides)
            {
                var controller = new GamePadStateBuilder(this.ControllerState);
                var keyboard   = new KeyboardStateBuilder(this.KeyboardState);
                var mouse      = new MouseStateBuilder(this.MouseState);

                if (this.ApplyOverrides(pressed: this.CustomPressedKeys, released: this.CustomReleasedKeys, controller, keyboard, mouse))
                {
                    this.ControllerState = controller.GetState();
                    this.KeyboardState   = keyboard.GetState();
                    this.MouseState      = mouse.GetState();
                }
            }
        }