From 494a128094f068952509e951b176f457b470fc6a Mon Sep 17 00:00:00 2001 From: "nathan@daedalus" Date: Tue, 17 Apr 2012 22:07:51 -0500 Subject: [PATCH] Adding code for mouse handling --HG-- branch : axios-newgsm --- axios/Engine/AxiosEvents.cs | 17 ++-- axios/Engine/AxiosGameScreen.cs | 2 +- axios/Engine/AxiosTimer.cs | 5 +- axios/Engine/Interfaces/IAxiosGameObject.cs | 5 +- axios/Engine/UI/AxiosButton.cs | 13 +-- axios/ScreenSystem/InputState.cs | 103 +++++++++++++++++++- 6 files changed, 125 insertions(+), 20 deletions(-) diff --git a/axios/Engine/AxiosEvents.cs b/axios/Engine/AxiosEvents.cs index c56de03..3d3fd30 100644 --- a/axios/Engine/AxiosEvents.cs +++ b/axios/Engine/AxiosEvents.cs @@ -1,5 +1,6 @@ using System; using FarseerPhysics.SamplesFramework; +using GameStateManagement; namespace Axios.Engine { @@ -19,45 +20,45 @@ namespace Axios.Engine } } - public delegate void AxiosHandler(object sender, AxiosGameScreen gameScreen, InputHelper input); + public delegate void AxiosHandler(object sender, AxiosGameScreen gameScreen, InputState input); public delegate void AxiosGameObjectHandler(AxiosGameObject sender); #region GameObjectEventMethods - public virtual void OnFocusEnter(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnFocusEnter(AxiosGameScreen gameScreen, InputState input) { this.HasFocus = true; this.OnEvent(FocusEnter, gameScreen, input); } - public virtual void OnFocusLeave(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnFocusLeave(AxiosGameScreen gameScreen, InputState input) { this.HasFocus = false; this.OnEvent(FocusLeave, gameScreen, input); } - public virtual void OnMouseHover(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnMouseHover(AxiosGameScreen gameScreen, InputState input) { this.OnEvent(MouseHover, gameScreen, input); } - public virtual void OnMouseLeave(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnMouseLeave(AxiosGameScreen gameScreen, InputState input) { this.OnEvent(MouseLeave, gameScreen, input); } - public virtual void OnValueChange(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnValueChange(AxiosGameScreen gameScreen, InputState input) { this.OnEvent(ValueChange, gameScreen, input); } - public virtual void OnMouseDown(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnMouseDown(AxiosGameScreen gameScreen, InputState input) { this.OnEvent(MouseDown, gameScreen, input); } - public virtual void OnMouseUp(AxiosGameScreen gameScreen, InputHelper input) + public virtual void OnMouseUp(AxiosGameScreen gameScreen, InputState input) { this.OnEvent(MouseUp, gameScreen, input); } diff --git a/axios/Engine/AxiosGameScreen.cs b/axios/Engine/AxiosGameScreen.cs index 938e756..835976f 100644 --- a/axios/Engine/AxiosGameScreen.cs +++ b/axios/Engine/AxiosGameScreen.cs @@ -334,7 +334,7 @@ namespace Axios.Engine } } - public override void HandleInput(InputHelper input, GameTime gameTime) + public override void HandleInput(InputState input, GameTime gameTime) { base.HandleInput(input, gameTime); diff --git a/axios/Engine/AxiosTimer.cs b/axios/Engine/AxiosTimer.cs index 582f1c7..a4fe5d0 100644 --- a/axios/Engine/AxiosTimer.cs +++ b/axios/Engine/AxiosTimer.cs @@ -2,6 +2,7 @@ using Axios.Engine.Interfaces; using Microsoft.Xna.Framework; +using GameStateManagement; namespace Axios.Engine { @@ -65,12 +66,12 @@ namespace Axios.Engine } - public override void HandleInput(AxiosGameScreen gameScreen, FarseerPhysics.SamplesFramework.InputHelper input, Microsoft.Xna.Framework.GameTime gameTime) + public override void HandleInput(AxiosGameScreen gameScreen, InputState input, Microsoft.Xna.Framework.GameTime gameTime) { } - public override void HandleCursor(AxiosGameScreen gameScreen, FarseerPhysics.SamplesFramework.InputHelper input) + public override void HandleCursor(AxiosGameScreen gameScreen, InputState input) { } diff --git a/axios/Engine/Interfaces/IAxiosGameObject.cs b/axios/Engine/Interfaces/IAxiosGameObject.cs index 69ae2cb..4a03cb4 100644 --- a/axios/Engine/Interfaces/IAxiosGameObject.cs +++ b/axios/Engine/Interfaces/IAxiosGameObject.cs @@ -1,5 +1,6 @@ using FarseerPhysics.SamplesFramework; using Microsoft.Xna.Framework; +using GameStateManagement; namespace Axios.Engine.Interfaces @@ -8,8 +9,8 @@ namespace Axios.Engine.Interfaces { void Update(AxiosGameScreen gameScreen, GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen); void LoadContent(AxiosGameScreen gameScreen); - void HandleInput(AxiosGameScreen gameScreen, InputHelper input, GameTime gameTime); - void HandleCursor(AxiosGameScreen gameScreen, InputHelper input); + void HandleInput(AxiosGameScreen gameScreen, InputState input, GameTime gameTime); + void HandleCursor(AxiosGameScreen gameScreen, InputState input); void UnloadContent(AxiosGameScreen gameScreen); } } diff --git a/axios/Engine/UI/AxiosButton.cs b/axios/Engine/UI/AxiosButton.cs index ac04d16..e2becf0 100644 --- a/axios/Engine/UI/AxiosButton.cs +++ b/axios/Engine/UI/AxiosButton.cs @@ -1,6 +1,7 @@  using FarseerPhysics.SamplesFramework; using Microsoft.Xna.Framework.Graphics; +using GameStateManagement; namespace Axios.Engine.UI { @@ -48,7 +49,7 @@ namespace Axios.Engine.UI } - public override void OnMouseHover(AxiosGameScreen gameScreen, InputHelper input) + public override void OnMouseHover(AxiosGameScreen gameScreen, InputState input) { base.OnMouseHover(gameScreen, input); @@ -56,21 +57,21 @@ namespace Axios.Engine.UI } - public override void OnMouseLeave(AxiosGameScreen gameScreen, InputHelper input) + public override void OnMouseLeave(AxiosGameScreen gameScreen, InputState input) { base.OnMouseLeave(gameScreen, input); this.Texture = _normaltexture; } - public override void OnMouseDown(AxiosGameScreen gameScreen, InputHelper input) + public override void OnMouseDown(AxiosGameScreen gameScreen, InputState input) { base.OnMouseDown(gameScreen, input); this.Texture = _clicktexture; } - public override void OnMouseUp(AxiosGameScreen gameScreen, InputHelper input) + public override void OnMouseUp(AxiosGameScreen gameScreen, InputState input) { base.OnMouseUp(gameScreen, input); @@ -79,9 +80,9 @@ namespace Axios.Engine.UI } - - public override void HandleCursor(AxiosGameScreen gameScreen, InputHelper input) + + public override void HandleCursor(AxiosGameScreen gameScreen, InputState input) { base.HandleCursor(gameScreen, input); diff --git a/axios/ScreenSystem/InputState.cs b/axios/ScreenSystem/InputState.cs index 63ff2db..f571ee3 100644 --- a/axios/ScreenSystem/InputState.cs +++ b/axios/ScreenSystem/InputState.cs @@ -247,7 +247,7 @@ namespace GameStateManagement /// /// Reads the latest state user input. /// - public void Update() + public void Update(GameTime gameTime) { _lastMouseState = _currentMouseState; if (_handleVirtualStick) @@ -299,6 +299,52 @@ namespace GameStateManagement { Gestures.Add(TouchPanel.ReadGesture()); } + + // Update cursor + Vector2 oldCursor = _cursor; + + if (CurrentGamePadStates[0].IsConnected && CurrentGamePadStates[0].ThumbSticks.Left != Vector2.Zero) + { + Vector2 temp = CurrentGamePadStates[0].ThumbSticks.Left; + _cursor += temp * new Vector2(300f, -300f) * (float)gameTime.ElapsedGameTime.TotalSeconds; + Mouse.SetPosition((int)_cursor.X, (int)_cursor.Y); + } + else + { + _cursor.X = _currentMouseState.X; + _cursor.Y = _currentMouseState.Y; + } + _cursor.X = MathHelper.Clamp(_cursor.X, 0f, _viewport.Width); + _cursor.Y = MathHelper.Clamp(_cursor.Y, 0f, _viewport.Height); + + if (_cursorIsValid && oldCursor != _cursor) + { + _cursorMoved = true; + } + else + { + _cursorMoved = false; + } + +#if WINDOWS + if (_viewport.Bounds.Contains(_currentMouseState.X, _currentMouseState.Y)) + { + _cursorIsValid = true; + } + else + { + _cursorIsValid = false; + } +#elif WINDOWS_PHONE + if (_currentMouseState.LeftButton == ButtonState.Pressed) + { + _cursorIsValid = true; + } + else + { + _cursorIsValid = false; + } +#endif } @@ -426,5 +472,60 @@ namespace GameStateManagement return (_lastVirtualState.IsButtonDown(button) && _currentVirtualState.IsButtonUp(button)); } + + /// + /// Helper for checking if a mouse button was newly pressed during this update. + /// + public bool IsNewMouseButtonPress(MouseButtons button) + { + switch (button) + { + case MouseButtons.LeftButton: + return (_currentMouseState.LeftButton == ButtonState.Pressed && + _lastMouseState.LeftButton == ButtonState.Released); + case MouseButtons.RightButton: + return (_currentMouseState.RightButton == ButtonState.Pressed && + _lastMouseState.RightButton == ButtonState.Released); + case MouseButtons.MiddleButton: + return (_currentMouseState.MiddleButton == ButtonState.Pressed && + _lastMouseState.MiddleButton == ButtonState.Released); + case MouseButtons.ExtraButton1: + return (_currentMouseState.XButton1 == ButtonState.Pressed && + _lastMouseState.XButton1 == ButtonState.Released); + case MouseButtons.ExtraButton2: + return (_currentMouseState.XButton2 == ButtonState.Pressed && + _lastMouseState.XButton2 == ButtonState.Released); + default: + return false; + } + } + + /// + /// Checks if the requested mouse button is released. + /// + /// The button. + public bool IsNewMouseButtonRelease(MouseButtons button) + { + switch (button) + { + case MouseButtons.LeftButton: + return (_lastMouseState.LeftButton == ButtonState.Pressed && + _currentMouseState.LeftButton == ButtonState.Released); + case MouseButtons.RightButton: + return (_lastMouseState.RightButton == ButtonState.Pressed && + _currentMouseState.RightButton == ButtonState.Released); + case MouseButtons.MiddleButton: + return (_lastMouseState.MiddleButton == ButtonState.Pressed && + _currentMouseState.MiddleButton == ButtonState.Released); + case MouseButtons.ExtraButton1: + return (_lastMouseState.XButton1 == ButtonState.Pressed && + _currentMouseState.XButton1 == ButtonState.Released); + case MouseButtons.ExtraButton2: + return (_lastMouseState.XButton2 == ButtonState.Pressed && + _currentMouseState.XButton2 == ButtonState.Released); + default: + return false; + } + } } }