diff --git a/FNA.csproj b/FNA.csproj index ac66572..6e2ec69 100644 --- a/FNA.csproj +++ b/FNA.csproj @@ -308,6 +308,7 @@ + @@ -335,7 +336,6 @@ - diff --git a/Makefile b/Makefile index eefc26f..9e1583e 100644 --- a/Makefile +++ b/Makefile @@ -274,6 +274,7 @@ SRC = \ src/Input/KeyboardState.cs \ src/Input/Keys.cs \ src/Input/KeyState.cs \ + src/Input/Mouse.cs \ src/Input/MouseState.cs \ src/Input/TextInputEXT.cs \ src/IUpdateable.cs \ @@ -301,7 +302,6 @@ SRC = \ src/Rectangle.cs \ src/SDL2/Input/SDL2_GamePad.cs \ src/SDL2/Input/SDL2_KeyboardUtil.cs \ - src/SDL2/Input/SDL2_Mouse.cs \ src/SDL2/SDL2_FNAPlatform.cs \ src/SDL2/SDL2_GameWindow.cs \ src/Storage/StorageContainer.cs \ diff --git a/src/FNAPlatform.cs b/src/FNAPlatform.cs index 15a9c72..1f25c49 100644 --- a/src/FNAPlatform.cs +++ b/src/FNAPlatform.cs @@ -42,6 +42,8 @@ namespace Microsoft.Xna.Framework SetPresentationInterval = SDL2_FNAPlatform.SetPresentationInterval; GetGraphicsAdapters = SDL2_FNAPlatform.GetGraphicsAdapters; GetKeyFromScancode = SDL2_FNAPlatform.GetKeyFromScancode; + GetMouseState = SDL2_FNAPlatform.GetMouseState; + SetMousePosition = SDL2_FNAPlatform.SetMousePosition; OnIsMouseVisibleChanged = SDL2_FNAPlatform.OnIsMouseVisibleChanged; GetStorageRoot = SDL2_FNAPlatform.GetStorageRoot; IsStoragePathConnected = SDL2_FNAPlatform.IsStoragePathConnected; @@ -76,6 +78,24 @@ namespace Microsoft.Xna.Framework public delegate Keys GetKeyFromScancodeFunc(Keys scancode); public static GetKeyFromScancodeFunc GetKeyFromScancode; + public delegate void GetMouseStateFunc( + out int x, + out int y, + out ButtonState left, + out ButtonState middle, + out ButtonState right, + out ButtonState x1, + out ButtonState x2 + ); + public static GetMouseStateFunc GetMouseState; + + public delegate void SetMousePositionFunc( + IntPtr window, + int x, + int y + ); + public static SetMousePositionFunc SetMousePosition; + public delegate void OnIsMouseVisibleChangedFunc(bool visible); public static OnIsMouseVisibleChangedFunc OnIsMouseVisibleChanged; diff --git a/src/Input/Mouse.cs b/src/Input/Mouse.cs new file mode 100644 index 0000000..cfde819 --- /dev/null +++ b/src/Input/Mouse.cs @@ -0,0 +1,113 @@ +#region License +/* FNA - XNA4 Reimplementation for Desktop Platforms + * Copyright 2009-2016 Ethan Lee and the MonoGame Team + * + * Released under the Microsoft Public License. + * See LICENSE for details. + */ +#endregion + +#region Using Statements +using System; +#endregion + +namespace Microsoft.Xna.Framework.Input +{ + /// + /// Allows reading position and button click information from mouse. + /// + public static class Mouse + { + #region Public Properties + + public static IntPtr WindowHandle + { + get; + set; + } + + #endregion + + #region Internal Variables + + internal static int INTERNAL_WindowWidth = 800; + internal static int INTERNAL_WindowHeight = 600; + + internal static int INTERNAL_MouseWheel = 0; + + // FIXME: Remove when global mouse state is accessible! -flibit + internal static bool INTERNAL_IsWarped = false; + internal static int INTERNAL_warpX = 0; + internal static int INTERNAL_warpY = 0; + + #endregion + + #region Public Interface + + /// + /// Gets mouse state information that includes position and button + /// presses for the provided window + /// + /// Current state of the mouse. + public static MouseState GetState() + { + int x, y; + ButtonState left, middle, right, x1, x2; + + FNAPlatform.GetMouseState( + out x, + out y, + out left, + out middle, + out right, + out x1, + out x2 + ); + + // If we warped the mouse, we've already done this in SetPosition. + if (INTERNAL_IsWarped) + { + x = INTERNAL_warpX; + y = INTERNAL_warpY; + } + else + { + // Scale the mouse coordinates for the faux-backbuffer + x = (int) ((double) x * Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Width / INTERNAL_WindowWidth); + y = (int) ((double) y * Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Height / INTERNAL_WindowHeight); + } + + return new MouseState( + x, + y, + INTERNAL_MouseWheel, + left, + middle, + right, + x1, + x2 + ); + } + + /// + /// Sets mouse cursor's relative position to game-window. + /// + /// Relative horizontal position of the cursor. + /// Relative vertical position of the cursor. + public static void SetPosition(int x, int y) + { + // The state should appear to be what they _think_ they're setting first. + INTERNAL_warpX = x; + INTERNAL_warpY = y; + + // Scale the mouse coordinates for the faux-backbuffer + x = (int) ((double) x * INTERNAL_WindowWidth / Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Width); + y = (int) ((double) y * INTERNAL_WindowHeight / Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Height); + + FNAPlatform.SetMousePosition(WindowHandle, x, y); + INTERNAL_IsWarped = true; + } + + #endregion + } +} diff --git a/src/SDL2/Input/SDL2_Mouse.cs b/src/SDL2/Input/SDL2_Mouse.cs deleted file mode 100644 index 263ecd0..0000000 --- a/src/SDL2/Input/SDL2_Mouse.cs +++ /dev/null @@ -1,102 +0,0 @@ -#region License -/* FNA - XNA4 Reimplementation for Desktop Platforms - * Copyright 2009-2016 Ethan Lee and the MonoGame Team - * - * Released under the Microsoft Public License. - * See LICENSE for details. - */ -#endregion - -#region Using Statements -using System; - -using SDL2; -#endregion - -namespace Microsoft.Xna.Framework.Input -{ - /// - /// Allows reading position and button click information from mouse. - /// - public static class Mouse - { - #region Public Properties - - public static IntPtr WindowHandle - { - get; - set; - } - - #endregion - - #region Internal Variables - - internal static int INTERNAL_WindowWidth = 800; - internal static int INTERNAL_WindowHeight = 600; - - internal static int INTERNAL_MouseWheel = 0; - - internal static bool INTERNAL_IsWarped = false; - - #endregion - - #region Private Variables - - private static MouseState state; - - #endregion - - #region Public Interface - - /// - /// Gets mouse state information that includes position and button - /// presses for the provided window - /// - /// Current state of the mouse. - public static MouseState GetState() - { - int x, y; - uint flags = SDL.SDL_GetMouseState(out x, out y); - - // If we warped the mouse, we've already done this in SetPosition. - if (!INTERNAL_IsWarped) - { - // Scale the mouse coordinates for the faux-backbuffer - state.X = (int) ((double) x * Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Width / INTERNAL_WindowWidth); - state.Y = (int) ((double) y * Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Height / INTERNAL_WindowHeight); - } - - state.LeftButton = (ButtonState) (flags & SDL.SDL_BUTTON_LMASK); - state.MiddleButton = (ButtonState) ((flags & SDL.SDL_BUTTON_MMASK) >> 1); - state.RightButton = (ButtonState) ((flags & SDL.SDL_BUTTON_RMASK) >> 2); - state.XButton1 = (ButtonState) ((flags & SDL.SDL_BUTTON_X1MASK) >> 3); - state.XButton2 = (ButtonState) ((flags & SDL.SDL_BUTTON_X2MASK) >> 4); - - state.ScrollWheelValue = INTERNAL_MouseWheel; - - return state; - } - - /// - /// Sets mouse cursor's relative position to game-window. - /// - /// Relative horizontal position of the cursor. - /// Relative vertical position of the cursor. - public static void SetPosition(int x, int y) - { - // The state should appear to be what they _think_ they're setting first. - state.X = x; - state.Y = y; - - // Scale the mouse coordinates for the faux-backbuffer - x = (int) ((double) x * INTERNAL_WindowWidth / Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Width); - y = (int) ((double) y * INTERNAL_WindowHeight / Game.Instance.GraphicsDevice.GLDevice.Backbuffer.Height); - - SDL.SDL_WarpMouseInWindow(WindowHandle, x, y); - INTERNAL_IsWarped = true; - } - - #endregion - } -} diff --git a/src/SDL2/SDL2_FNAPlatform.cs b/src/SDL2/SDL2_FNAPlatform.cs index 0a46450..9e8be59 100644 --- a/src/SDL2/SDL2_FNAPlatform.cs +++ b/src/SDL2/SDL2_FNAPlatform.cs @@ -522,6 +522,28 @@ namespace Microsoft.Xna.Framework return SDL2_KeyboardUtil.KeyFromScancode(scancode); } + public static void GetMouseState( + out int x, + out int y, + out ButtonState left, + out ButtonState middle, + out ButtonState right, + out ButtonState x1, + out ButtonState x2 + ) { + uint flags = SDL.SDL_GetMouseState(out x, out y); + left = (ButtonState) (flags & SDL.SDL_BUTTON_LMASK); + middle = (ButtonState) ((flags & SDL.SDL_BUTTON_MMASK) >> 1); + right = (ButtonState) ((flags & SDL.SDL_BUTTON_RMASK) >> 2); + x1 = (ButtonState) ((flags & SDL.SDL_BUTTON_X1MASK) >> 3); + x2 = (ButtonState) ((flags & SDL.SDL_BUTTON_X2MASK) >> 4); + } + + public static void SetMousePosition(IntPtr window, int x, int y) + { + SDL.SDL_WarpMouseInWindow(window, x, y); + } + public static void OnIsMouseVisibleChanged(bool visible) { SDL.SDL_ShowCursor(visible ? 1 : 0);