Browse Source

Get Input scaling working for Mobile too.

Dominique Louis 2 weeks ago
parent
commit
f701b3d85a

+ 11 - 8
CardsStarterKit/Core/Game/ScreenManager/MenuScreen.cs

@@ -76,7 +76,7 @@ namespace GameStateManagement
             return new Rectangle(
                 0,
                 (int)entry.Destination.Y - menuEntryPadding,
-                ScreenManager.GraphicsDevice.Viewport.Width,
+                (int)ScreenManager.BaseScreenSize.X,
                 entry.GetHeight(this) + (menuEntryPadding * 2));
         }
 
@@ -114,13 +114,14 @@ namespace GameStateManagement
                     OnSelectEntry(selectedEntry, player);
                 }
 
-                // Handle mouse input using InputState
+                // Handle mouse input using transformed coordinates
                 if (input.CurrentMouseState.LeftButton == ButtonState.Released)
                 {
                     if (isMouseDown)
                     {
                         isMouseDown = false;
-                        Point clickLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y);
+                        // Use transformed cursor location for proper scaling/letterboxing
+                        Point clickLocation = new Point((int)input.CurrentCursorLocation.X, (int)input.CurrentCursorLocation.Y);
 
                         for (int i = 0; i < menuEntries.Count; i++)
                         {
@@ -135,7 +136,8 @@ namespace GameStateManagement
                 else if (input.CurrentMouseState.LeftButton == ButtonState.Pressed)
                 {
                     isMouseDown = true;
-                    Point clickLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y);
+                    // Use transformed cursor location for proper scaling/letterboxing
+                    Point clickLocation = new Point((int)input.CurrentCursorLocation.X, (int)input.CurrentCursorLocation.Y);
 
                     for (int i = 0; i < menuEntries.Count; i++)
                     {
@@ -147,12 +149,13 @@ namespace GameStateManagement
             }
             else if (UIUtility.IsMobile)
             {
-                // Handle touch input
+                // Handle touch input with transformed coordinates
                 foreach (GestureSample gesture in input.Gestures)
                 {
                     if (gesture.GestureType == GestureType.Tap)
                     {
-                        Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);
+                        // Use transformed cursor location (InputState already transforms gesture position)
+                        Point tapLocation = new Point((int)input.CurrentCursorLocation.X, (int)input.CurrentCursorLocation.Y);
 
                         for (int i = 0; i < menuEntries.Count; i++)
                         {
@@ -287,7 +290,7 @@ namespace GameStateManagement
             // make sure our entries are in the right place before we draw them
             //UpdateMenuEntryLocations();
 
-            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
+            GraphicsDevice graphicsDevice = ScreenManager.GraphicsDevice;
             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
             SpriteFont font = ScreenManager.Font;
 
@@ -310,7 +313,7 @@ namespace GameStateManagement
 
             // Draw the menu title in the top third of the screen
             Vector2 titlePosition = new Vector2(
-                graphics.Viewport.Width / 2,
+                ScreenManager.BaseScreenSize.X / 2,
                 ScreenManager.SafeArea.Top + 80); // Position in top third instead of center
             Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
             Color titleColor = new Color(192, 192, 192) * TransitionAlpha;

+ 2 - 2
CardsStarterKit/Core/Game/Screens/MessageBoxScreen.cs

@@ -45,8 +45,8 @@ namespace Blackjack
             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
             SpriteFont font = ScreenManager.Font;
             Vector2 viewportSize = new Vector2(
-                ScreenManager.GraphicsDevice.Viewport.Width,
-                ScreenManager.GraphicsDevice.Viewport.Height);
+                ScreenManager.BaseScreenSize.X,
+                ScreenManager.BaseScreenSize.Y);
             Vector2 textSize = font.MeasureString(message);
             Vector2 position = (viewportSize - textSize) / 2;
             spriteBatch.Begin();

+ 14 - 23
CardsStarterKit/Core/Game/Screens/SettingsScreen.cs

@@ -79,8 +79,7 @@ namespace Blackjack
             // Initialize input helper
             inputHelper = new InputHelper(ScreenManager);
 
-            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
-            safeArea = viewport.TitleSafeArea;
+            safeArea = ScreenManager.SafeArea;
 
             // Calculate proportional spacing and sizes based on screen height
             float heightScale = safeArea.Height / 720f; // Scale based on 720p baseline
@@ -418,35 +417,36 @@ namespace Blackjack
             }
         }
 
-        private MouseState previousMouseState;
-        private KeyboardState previousKeyboardState;
-
         private void HandleInput()
         {
-            KeyboardState keyboardState = Keyboard.GetState();
-            MouseState mouseState = Mouse.GetState();
+            // 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);
 
             // Check for escape/back
             if ((keyboardState.IsKeyDown(Keys.Escape) && !previousKeyboardState.IsKeyDown(Keys.Escape)) ||
                 inputHelper.IsEscape)
             {
                 ExitScreen();
-                previousKeyboardState = keyboardState;
                 return;
             }
 
-            // Get mouse position
-            Point mousePos = new Point(mouseState.X, mouseState.Y);
-
             // Track button press state
             pressedButtonIndex = -1;
             isNextButtonPressed = false;
             isPrevButtonPressed = false;
             isBackButtonPressed = false;
 
-            // Check for mouse click (left button just pressed)
-            if (mouseState.LeftButton == ButtonState.Pressed &&
-                previousMouseState.LeftButton == ButtonState.Released)
+            // 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)
             {
                 // Check back button
                 if (backButtonBounds.Contains(mousePos))
@@ -454,8 +454,6 @@ namespace Blackjack
                     isBackButtonPressed = true;
                     AudioManager.PlaySound("menu_select");
                     ExitScreen();
-                    previousMouseState = mouseState;
-                    previousKeyboardState = keyboardState;
                     return;
                 }
 
@@ -466,8 +464,6 @@ namespace Blackjack
                     currentPage++;
                     BuildSettingItems();
                     AudioManager.PlaySound("menu_select");
-                    previousMouseState = mouseState;
-                    previousKeyboardState = keyboardState;
                     return;
                 }
                 else if (currentPage > 0 && prevButtonBounds.Contains(mousePos))
@@ -476,8 +472,6 @@ namespace Blackjack
                     currentPage--;
                     BuildSettingItems();
                     AudioManager.PlaySound("menu_select");
-                    previousMouseState = mouseState;
-                    previousKeyboardState = keyboardState;
                     return;
                 }
 
@@ -530,9 +524,6 @@ namespace Blackjack
                     break;
                 }
             }
-
-            previousMouseState = mouseState;
-            previousKeyboardState = keyboardState;
         }
 
         private Rectangle GetLeftButtonBounds(Rectangle itemBounds)