Bläddra i källkod

Finally get input scaling correctly for mobile platforms.

Dominique Louis 2 veckor sedan
förälder
incheckning
5de1054624

+ 22 - 12
CardsStarterKit/Core/Game/Blackjack/Misc/BetGameComponent.cs

@@ -344,21 +344,31 @@ namespace Blackjack
             bool isClicked = false;
             Vector2 position = Vector2.Zero;
 
-            // Check for tap gestures (touch release)
-            if (input.Gestures.Count > 0 && input.Gestures[0].GestureType == GestureType.Tap)
+            // Use transformed cursor location for all input types (handles scaling/letterboxing)
+            Vector2 transformedCursorPos = input.CurrentCursorLocation;
+
+            if (UIUtility.IsMobile)
             {
-                isClicked = true;
-                position = input.Gestures[0].Position;
+                // Handle touch input with gestures
+                if (input.Gestures.Count > 0 && input.Gestures[0].GestureType == GestureType.Tap)
+                {
+                    isClicked = true;
+                    // Use transformed cursor position (already set by InputState)
+                    position = transformedCursorPos;
+                }
             }
-
-            // Check for mouse click (button was pressed last frame, released this frame)
-            bool wasPressed = input.LastMouseState.LeftButton == ButtonState.Pressed;
-            bool isReleased = input.CurrentMouseState.LeftButton == ButtonState.Released;
-
-            if (wasPressed && isReleased)
+            else if (UIUtility.IsDesktop)
             {
-                isClicked = true;
-                position = new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y);
+                // Handle mouse click (button was pressed last frame, released this frame)
+                bool wasPressed = input.LastMouseState.LeftButton == ButtonState.Pressed;
+                bool isReleased = input.CurrentMouseState.LeftButton == ButtonState.Released;
+
+                if (wasPressed && isReleased)
+                {
+                    isClicked = true;
+                    // Use transformed cursor position
+                    position = transformedCursorPos;
+                }
             }
 
             // Handle chip interaction logic only on click/tap completion

+ 32 - 27
CardsStarterKit/Core/Game/Blackjack/UI/Button.cs

@@ -125,46 +125,51 @@ namespace Blackjack
             bool pressed = false;
             Vector2 position = Vector2.Zero;
 
-            // Check for tap gestures
-            if (input.Gestures.Count > 0 && input.Gestures[0].GestureType == GestureType.Tap)
-            {
-                pressed = true;
-                position = input.Gestures[0].Position;
-            }
-
-            // Check for mouse input
-            if (input.CurrentMouseState.LeftButton == ButtonState.Pressed)
-            {
-                pressed = true;
-                position = new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y);
-            }
+            // Use transformed cursor location for all input types (handles scaling/letterboxing)
+            Vector2 transformedCursorPos = input.CurrentCursorLocation;
 
-            // Handle button press logic
-            if (pressed)
+            if (UIUtility.IsDesktop)
             {
-                if (!isKeyDown && IntersectWith(position))
+                // Handle mouse input - detect click (press + release)
+                if (input.CurrentMouseState.LeftButton == ButtonState.Pressed)
                 {
-                    isPressed = true;
+                    pressed = true;
+                    position = transformedCursorPos;
+                }
 
-                    if (UIUtility.IsMobile)
+                // Handle button press logic for desktop
+                if (pressed)
+                {
+                    if (!isKeyDown && IntersectWith(position))
+                    {
+                        isPressed = true;
+                        isKeyDown = true;
+                    }
+                }
+                else
+                {
+                    if (isPressed && IntersectWith(transformedCursorPos))
                     {
                         FireClick();
-                        isPressed = false;
                     }
 
-                    isKeyDown = true;
+                    isPressed = false;
+                    isKeyDown = false;
                 }
             }
-            else
+            else if (UIUtility.IsMobile)
             {
-                if (isPressed && (IntersectWith(new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y)) ||
-                                  IntersectWith(inputHelper?.PointPosition ?? Vector2.Zero)))
+                // Handle touch input with gestures
+                if (input.Gestures.Count > 0 && input.Gestures[0].GestureType == GestureType.Tap)
                 {
-                    FireClick();
-                }
+                    // Use transformed cursor location (already set by InputState)
+                    position = transformedCursorPos;
 
-                isPressed = false;
-                isKeyDown = false;
+                    if (IntersectWith(position))
+                    {
+                        FireClick();
+                    }
+                }
             }
         }
 

+ 15 - 15
CardsStarterKit/Core/Game/ScreenManager/MenuScreen.cs

@@ -84,11 +84,11 @@ namespace GameStateManagement
         /// Responds to user input, changing the selected entry and accepting
         /// or cancelling the menu.
         /// </summary>
-        public override void HandleInput(InputState input)
+        public override void HandleInput(InputState inputState)
         {
             // Cancel the current menu screen if the user presses the back button
             PlayerIndex player;
-            if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
+            if (inputState.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
             {
                 OnCancel(player);
             }
@@ -96,32 +96,32 @@ namespace GameStateManagement
             if (UIUtility.IsDesktop)
             {
                 // Handle keyboard input
-                if (input.IsMenuUp(ControllingPlayer))
+                if (inputState.IsMenuUp(ControllingPlayer))
                 {
                     selectedEntry--;
                     if (selectedEntry < 0)
                         selectedEntry = menuEntries.Count - 1;
                 }
-                else if (input.IsMenuDown(ControllingPlayer))
+                else if (inputState.IsMenuDown(ControllingPlayer))
                 {
                     selectedEntry++;
                     if (selectedEntry >= menuEntries.Count)
                         selectedEntry = 0;
                 }
-                else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
-                         input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
+                else if (inputState.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
+                         inputState.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
                 {
                     OnSelectEntry(selectedEntry, player);
                 }
 
                 // Handle mouse input using transformed coordinates
-                if (input.CurrentMouseState.LeftButton == ButtonState.Released)
+                if (inputState.CurrentMouseState.LeftButton == ButtonState.Released)
                 {
                     if (isMouseDown)
                     {
                         isMouseDown = false;
                         // Use transformed cursor location for proper scaling/letterboxing
-                        Point clickLocation = new Point((int)input.CurrentCursorLocation.X, (int)input.CurrentCursorLocation.Y);
+                        Point clickLocation = new Point((int)inputState.CurrentCursorLocation.X, (int)inputState.CurrentCursorLocation.Y);
 
                         for (int i = 0; i < menuEntries.Count; i++)
                         {
@@ -133,11 +133,11 @@ namespace GameStateManagement
                         }
                     }
                 }
-                else if (input.CurrentMouseState.LeftButton == ButtonState.Pressed)
+                else if (inputState.CurrentMouseState.LeftButton == ButtonState.Pressed)
                 {
                     isMouseDown = true;
                     // Use transformed cursor location for proper scaling/letterboxing
-                    Point clickLocation = new Point((int)input.CurrentCursorLocation.X, (int)input.CurrentCursorLocation.Y);
+                    Point clickLocation = new Point((int)inputState.CurrentCursorLocation.X, (int)inputState.CurrentCursorLocation.Y);
 
                     for (int i = 0; i < menuEntries.Count; i++)
                     {
@@ -150,12 +150,12 @@ namespace GameStateManagement
             else if (UIUtility.IsMobile)
             {
                 // Handle touch input with transformed coordinates
-                foreach (GestureSample gesture in input.Gestures)
+                foreach (GestureSample gesture in inputState.Gestures)
                 {
                     if (gesture.GestureType == GestureType.Tap)
                     {
                         // Use transformed cursor location (InputState already transforms gesture position)
-                        Point tapLocation = new Point((int)input.CurrentCursorLocation.X, (int)input.CurrentCursorLocation.Y);
+                        Point tapLocation = new Point((int)inputState.CurrentCursorLocation.X, (int)inputState.CurrentCursorLocation.Y);
 
                         for (int i = 0; i < menuEntries.Count; i++)
                         {
@@ -171,19 +171,19 @@ namespace GameStateManagement
             else
             {
                 // Handle gamepad input
-                if (input.IsMenuUp(ControllingPlayer))
+                if (inputState.IsMenuUp(ControllingPlayer))
                 {
                     selectedEntry--;
                     if (selectedEntry < 0)
                         selectedEntry = menuEntries.Count - 1;
                 }
-                else if (input.IsMenuDown(ControllingPlayer))
+                else if (inputState.IsMenuDown(ControllingPlayer))
                 {
                     selectedEntry++;
                     if (selectedEntry >= menuEntries.Count)
                         selectedEntry = 0;
                 }
-                else if (input.IsNewButtonPress(Buttons.A, ControllingPlayer, out player))
+                else if (inputState.IsNewButtonPress(Buttons.A, ControllingPlayer, out player))
                 {
                     OnSelectEntry(selectedEntry, player);
                 }

+ 114 - 69
CardsStarterKit/Core/Game/Screens/SettingsScreen.cs

@@ -10,8 +10,10 @@ using GameStateManagement;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Input.Touch;
 using Microsoft.Xna.Framework.Content;
 using System.IO;
+using CardsFramework;
 
 namespace Blackjack
 {
@@ -46,6 +48,8 @@ namespace Blackjack
         private Rectangle backButtonBounds;
         private bool isBackButtonPressed = false;
 
+        private bool isMouseDown = false;
+
         private InputHelper inputHelper;
         private float itemSpacing;
         private float groupSpacing;
@@ -56,6 +60,9 @@ namespace Blackjack
 
         public SettingsScreen()
         {
+            // Settings screen needs Tap gestures for mobile
+            EnabledGestures = GestureType.Tap;
+            
             TransitionOnTime = TimeSpan.FromSeconds(0.5);
             TransitionOffTime = TimeSpan.FromSeconds(0.5);
             settings = GameSettings.Instance;
@@ -411,108 +418,146 @@ namespace Blackjack
         {
             base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
 
-            if (IsActive && !coveredByOtherScreen)
-            {
-                HandleInput();
-            }
+
         }
 
-        private void HandleInput()
+        /// <summary>
+        /// Responds to user input.
+        /// </summary>
+        public override void HandleInput(InputState inputState)
         {
-            // Use ScreenManager's centralized input state with proper coordinate transformation
-            KeyboardState keyboardState = ScreenManager.InputState.CurrentKeyboardStates[0];
-            KeyboardState previousKeyboardState = ScreenManager.InputState.LastKeyboardStates[0];
-
-            // Get transformed cursor position (handles scaling/letterboxing on mobile)
-            Vector2 cursorPos = ScreenManager.InputState.CurrentCursorLocation;
-            Point mousePos = new Point((int)cursorPos.X, (int)cursorPos.Y);
+            // Cancel the settings screen if the user presses the back button
+            PlayerIndex player;
+            if (inputState.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
+            {
+                ExitScreen();
+                return;
+            }
 
-            // Check for escape/back
-            if ((keyboardState.IsKeyDown(Keys.Escape) && !previousKeyboardState.IsKeyDown(Keys.Escape)) ||
-                inputHelper.IsEscape)
+            // Check for escape key
+            if (inputState.IsNewKeyPress(Keys.Escape, ControllingPlayer, out player))
             {
                 ExitScreen();
                 return;
             }
 
+            // Get transformed cursor position (handles scaling/letterboxing on mobile)
+            Vector2 cursorPos = inputState.CurrentCursorLocation;
+            Point mousePos = new Point((int)cursorPos.X, (int)cursorPos.Y);
+
             // Track button press state
             pressedButtonIndex = -1;
             isNextButtonPressed = false;
             isPrevButtonPressed = false;
             isBackButtonPressed = false;
 
-            // Check for mouse click (left button just clicked - pressed then released)
-            MouseState currentMouseState = ScreenManager.InputState.CurrentMouseState;
-            MouseState lastMouseState = ScreenManager.InputState.LastMouseState;
-
-            if (currentMouseState.LeftButton == ButtonState.Released &&
-                lastMouseState.LeftButton == ButtonState.Pressed)
+            if (UIUtility.IsDesktop)
             {
-                // Check back button
-                if (backButtonBounds.Contains(mousePos))
+                // Handle mouse input - detect click (press + release)
+                if (inputState.CurrentMouseState.LeftButton == ButtonState.Released)
                 {
-                    isBackButtonPressed = true;
-                    AudioManager.PlaySound("menu_select");
-                    ExitScreen();
-                    return;
+                    if (isMouseDown)
+                    {
+                        isMouseDown = false;
+                        HandleSettingItemClick(mousePos);
+                    }
                 }
-
-                // Check navigation buttons
-                if (currentPage < TotalPages - 1 && nextButtonBounds.Contains(mousePos))
+                else if (inputState.CurrentMouseState.LeftButton == ButtonState.Pressed)
                 {
-                    isNextButtonPressed = true;
-                    currentPage++;
-                    BuildSettingItems();
-                    AudioManager.PlaySound("menu_select");
-                    return;
+                    isMouseDown = true;
+                    // Track hover on press
+                    TrackHoverState(mousePos);
                 }
-                else if (currentPage > 0 && prevButtonBounds.Contains(mousePos))
+            }
+            else if (UIUtility.IsMobile)
+            {
+                // Handle touch input with gestures (like MenuScreen does)
+                foreach (GestureSample gesture in inputState.Gestures)
                 {
-                    isPrevButtonPressed = true;
-                    currentPage--;
-                    BuildSettingItems();
-                    AudioManager.PlaySound("menu_select");
-                    return;
+                    if (gesture.GestureType == GestureType.Tap)
+                    {
+                        HandleSettingItemClick(mousePos);
+                    }
                 }
+            }
+
+            // Track hover state for visual feedback on desktop
+            if (UIUtility.IsDesktop && inputState.CurrentMouseState.LeftButton == ButtonState.Released)
+            {
+                TrackHoverState(mousePos);
+            }
+        }
 
-                for (int i = 0; i < settingItems.Count; i++)
+        private void HandleSettingItemClick(Point clickLocation)
+        {
+            // Check back button
+            if (backButtonBounds.Contains(clickLocation))
+            {
+                isBackButtonPressed = true;
+                AudioManager.PlaySound("menu_select");
+                ExitScreen();
+                return;
+            }
+
+            // Check navigation buttons
+            if (currentPage < TotalPages - 1 && nextButtonBounds.Contains(clickLocation))
+            {
+                isNextButtonPressed = true;
+                currentPage++;
+                BuildSettingItems();
+                AudioManager.PlaySound("menu_select");
+                return;
+            }
+            else if (currentPage > 0 && prevButtonBounds.Contains(clickLocation))
+            {
+                isPrevButtonPressed = true;
+                currentPage--;
+                BuildSettingItems();
+                AudioManager.PlaySound("menu_select");
+                return;
+            }
+
+            // Check setting items
+            for (int i = 0; i < settingItems.Count; i++)
+            {
+                SettingItem item = settingItems[i];
+                if (item.Type == SettingType.Header) continue;
+
+                if (item.Bounds.Contains(clickLocation))
                 {
-                    SettingItem item = settingItems[i];
-                    if (item.Type == SettingType.Header) continue;
+                    // Button bounds
+                    Rectangle leftButton = GetLeftButtonBounds(item.Bounds);
+                    Rectangle rightButton = GetRightButtonBounds(item.Bounds);
 
-                    if (item.Bounds.Contains(mousePos))
+                    if (item.Type == SettingType.Checkbox)
                     {
-                        // Button bounds
-                        Rectangle leftButton = GetLeftButtonBounds(item.Bounds);
-                        Rectangle rightButton = GetRightButtonBounds(item.Bounds);
-
-                        if (item.Type == SettingType.Checkbox)
-                        {
-                            // Get checkbox bounds (centered in value column)
-                            Rectangle checkboxBounds = GetCheckboxButtonBounds(item.Bounds);
-                            if (checkboxBounds.Contains(mousePos))
-                            {
-                                item.OnClick?.Invoke();
-                                AudioManager.PlaySound("menu_select");
-                            }
-                        }
-                        else if (leftButton.Contains(mousePos))
-                        {
-                            item.OnDecrease?.Invoke();
-                            AudioManager.PlaySound("menu_select");
-                            pressedButtonIndex = i * 2; // Even indices for left buttons
-                        }
-                        else if (rightButton.Contains(mousePos))
+                        // Get checkbox bounds (centered in value column)
+                        Rectangle checkboxBounds = GetCheckboxButtonBounds(item.Bounds);
+                        if (checkboxBounds.Contains(clickLocation))
                         {
-                            item.OnIncrease?.Invoke();
+                            item.OnClick?.Invoke();
                             AudioManager.PlaySound("menu_select");
-                            pressedButtonIndex = i * 2 + 1; // Odd indices for right buttons
                         }
-                        break;
                     }
+                    else if (leftButton.Contains(clickLocation))
+                    {
+                        item.OnDecrease?.Invoke();
+                        AudioManager.PlaySound("menu_select");
+                        pressedButtonIndex = i * 2; // Even indices for left buttons
+                    }
+                    else if (rightButton.Contains(clickLocation))
+                    {
+                        item.OnIncrease?.Invoke();
+                        AudioManager.PlaySound("menu_select");
+                        pressedButtonIndex = i * 2 + 1; // Odd indices for right buttons
+                    }
+                    break;
                 }
             }
+        }
 
+        private void TrackHoverState(Point mousePos)
+        {
             // Track hover state for visual feedback
             hoveredIndex = -1;
             for (int i = 0; i < settingItems.Count; i++)