Adding code for mouse handling

--HG--
branch : axios-newgsm
master
nathan@daedalus 2012-04-17 22:07:51 -05:00
parent 2277056e6b
commit 494a128094
6 changed files with 125 additions and 20 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using FarseerPhysics.SamplesFramework; using FarseerPhysics.SamplesFramework;
using GameStateManagement;
namespace Axios.Engine 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); public delegate void AxiosGameObjectHandler(AxiosGameObject sender);
#region GameObjectEventMethods #region GameObjectEventMethods
public virtual void OnFocusEnter(AxiosGameScreen gameScreen, InputHelper input) public virtual void OnFocusEnter(AxiosGameScreen gameScreen, InputState input)
{ {
this.HasFocus = true; this.HasFocus = true;
this.OnEvent(FocusEnter, gameScreen, input); 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.HasFocus = false;
this.OnEvent(FocusLeave, gameScreen, input); 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); 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); 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); 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); 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); this.OnEvent(MouseUp, gameScreen, input);
} }

View File

@ -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); base.HandleInput(input, gameTime);

View File

@ -2,6 +2,7 @@
using Axios.Engine.Interfaces; using Axios.Engine.Interfaces;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using GameStateManagement;
namespace Axios.Engine 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)
{ {
} }

View File

@ -1,5 +1,6 @@
using FarseerPhysics.SamplesFramework; using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using GameStateManagement;
namespace Axios.Engine.Interfaces namespace Axios.Engine.Interfaces
@ -8,8 +9,8 @@ namespace Axios.Engine.Interfaces
{ {
void Update(AxiosGameScreen gameScreen, GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen); void Update(AxiosGameScreen gameScreen, GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen);
void LoadContent(AxiosGameScreen gameScreen); void LoadContent(AxiosGameScreen gameScreen);
void HandleInput(AxiosGameScreen gameScreen, InputHelper input, GameTime gameTime); void HandleInput(AxiosGameScreen gameScreen, InputState input, GameTime gameTime);
void HandleCursor(AxiosGameScreen gameScreen, InputHelper input); void HandleCursor(AxiosGameScreen gameScreen, InputState input);
void UnloadContent(AxiosGameScreen gameScreen); void UnloadContent(AxiosGameScreen gameScreen);
} }
} }

View File

@ -1,6 +1,7 @@
 
using FarseerPhysics.SamplesFramework; using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using GameStateManagement;
namespace Axios.Engine.UI 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); 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); base.OnMouseLeave(gameScreen, input);
this.Texture = _normaltexture; this.Texture = _normaltexture;
} }
public override void OnMouseDown(AxiosGameScreen gameScreen, InputHelper input) public override void OnMouseDown(AxiosGameScreen gameScreen, InputState input)
{ {
base.OnMouseDown(gameScreen, input); base.OnMouseDown(gameScreen, input);
this.Texture = _clicktexture; this.Texture = _clicktexture;
} }
public override void OnMouseUp(AxiosGameScreen gameScreen, InputHelper input) public override void OnMouseUp(AxiosGameScreen gameScreen, InputState input)
{ {
base.OnMouseUp(gameScreen, 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); base.HandleCursor(gameScreen, input);

View File

@ -247,7 +247,7 @@ namespace GameStateManagement
/// <summary> /// <summary>
/// Reads the latest state user input. /// Reads the latest state user input.
/// </summary> /// </summary>
public void Update() public void Update(GameTime gameTime)
{ {
_lastMouseState = _currentMouseState; _lastMouseState = _currentMouseState;
if (_handleVirtualStick) if (_handleVirtualStick)
@ -299,6 +299,52 @@ namespace GameStateManagement
{ {
Gestures.Add(TouchPanel.ReadGesture()); 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) && return (_lastVirtualState.IsButtonDown(button) &&
_currentVirtualState.IsButtonUp(button)); _currentVirtualState.IsButtonUp(button));
} }
/// <summary>
/// Helper for checking if a mouse button was newly pressed during this update.
/// </summary>
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;
}
}
/// <summary>
/// Checks if the requested mouse button is released.
/// </summary>
/// <param name="button">The button.</param>
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;
}
}
} }
} }