PageRenderTime 103ms CodeModel.GetById 73ms RepoModel.GetById 1ms app.codeStats 0ms

/DWControllers/HardwareControllers/MouseControl.cs

#
C# | 243 lines | 143 code | 29 blank | 71 comment | 37 complexity | 058eba92776829878c53eec634403670 MD5 | raw file
  1. //---------------------------------------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="MouseControl.cs" company="DarkWynter Studios">
  3. // Copyright (C)2007 DarkWynter Studios. All rights reserved.
  4. // </copyright>
  5. //---------------------------------------------------------------------------------------------------------------------------------------------------
  6. // {Contact : darkwynter.com for licensing information
  7. //---------------------------------------------------------------------------------------------------------------------------------------------------
  8. namespace DarkWynter.Engine.Controllers
  9. {
  10. #region Using Statements
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Input;
  16. using System.Diagnostics;
  17. //using DarkWynter.Stream;
  18. //using DarkWynter.Engine.ObjectLib;
  19. //using DarkWynter.Engine.Menus;
  20. //using Globals;
  21. #endregion
  22. /// <summary>
  23. /// Mouse event class for mapping a Mouse button/motion to an event
  24. /// </summary>
  25. public class MouseControl
  26. {
  27. // Time delay between key presses to avoid double input
  28. private int KEY_TIMER_MAX_MILLI = 10;
  29. private Stopwatch controlTimer = new Stopwatch();
  30. private float MOUSE_ROTATION_SCALE = 0.005f;
  31. private float lastMouseX, lastMouseY;
  32. private int windowHeight, windowWidth; // Game window Height and Width for resetting the Mouse position
  33. private int lastMouseWheelPos = 0; // Position of mouse wheel on element list
  34. /// <summary>
  35. /// On click delegate type
  36. /// </summary>
  37. /// <param name="args">Controller boolean event arguments</param>
  38. public delegate void OnClickHandler(ControllerBoolEventArgs args);
  39. private event OnClickHandler click;
  40. /// <summary>
  41. /// On motion delegate type
  42. /// </summary>
  43. /// <param name="args">Controller vector2 event arguments</param>
  44. public delegate void OnMotionHandler(ControllerVec2EventArgs args);
  45. private event OnMotionHandler motion;
  46. /// <summary>
  47. /// On scroll delegate type
  48. /// </summary>
  49. /// <param name="args">Controller integer event arguments</param>
  50. public delegate void OnScrollHandler(ControllerIntEventArgs args);
  51. private event OnScrollHandler scroll;
  52. /// <summary>
  53. /// Type of control for this event
  54. /// </summary>
  55. public ControlType controlType;
  56. /// <summary>
  57. /// Click constructor
  58. /// </summary>
  59. /// <param name="type">Click control type</param>
  60. /// <param name="onClickHandler">Click handler</param>
  61. /// <param name="timerValue">Time delay between multiple invocations</param>
  62. public MouseControl(ControlType type, OnClickHandler onClickHandler, int timerValue)
  63. {
  64. controlType = type;
  65. click += new OnClickHandler(onClickHandler);
  66. controlTimer.Start();
  67. KEY_TIMER_MAX_MILLI = timerValue;
  68. }
  69. /// <summary>
  70. /// Motion constructor
  71. /// </summary>
  72. /// <param name="type">Motion control type</param>
  73. /// <param name="onMotionHandler">Motion handler</param>
  74. /// <param name="timerValue">Time delay between multiple invocations</param>
  75. public MouseControl(ControlType type, OnMotionHandler onMotionHandler, int timerValue)
  76. {
  77. controlType = type;
  78. motion += new OnMotionHandler(onMotionHandler);
  79. controlTimer.Start();
  80. KEY_TIMER_MAX_MILLI = timerValue;
  81. lastMouseX = Mouse.GetState().X;
  82. lastMouseY = Mouse.GetState().Y;
  83. }
  84. /// <summary>
  85. /// Scroll constructor
  86. /// </summary>
  87. /// <param name="type">Scroll control type</param>
  88. /// <param name="onScrollHandler">Scroll handler</param>
  89. /// <param name="timerValue">Time delay between multiple invocations</param>
  90. public MouseControl(ControlType type, OnScrollHandler onScrollHandler, int timerValue)
  91. {
  92. controlType = type;
  93. scroll += new OnScrollHandler(onScrollHandler);
  94. controlTimer.Start();
  95. KEY_TIMER_MAX_MILLI = timerValue;
  96. lastMouseWheelPos = Mouse.GetState().ScrollWheelValue;
  97. }
  98. /// <summary>
  99. /// Update Method
  100. /// </summary>
  101. /// <param name="mouseState">Updated mouse state</param>
  102. /// <param name="objectLibrary">Object Library</param>
  103. /// <param name="menuSystem">Menu System</param>
  104. public void Update(MouseState mouseState, ref List<object> argObjects)
  105. {
  106. // Check double-input timer
  107. if (controlTimer.ElapsedMilliseconds > KEY_TIMER_MAX_MILLI)
  108. {
  109. // Reset timer
  110. controlTimer.Reset();
  111. controlTimer.Start();
  112. // Get updated window dimensions
  113. windowWidth = ControllerManager.clientSize.X;
  114. windowHeight = ControllerManager.clientSize.Y;
  115. #region Movement Section
  116. // Calculate Mouse movement since last frame
  117. if (controlType == ControlType.Motion)
  118. {
  119. // If mouse is in the window
  120. if (mouseState.X > 0 && mouseState.X < windowWidth - 1 &&
  121. mouseState.Y > 0 && mouseState.Y < windowHeight - 1)
  122. {
  123. // Find change in mouse position since last frame
  124. float diffX = mouseState.X - windowWidth / 2;
  125. float diffY = mouseState.Y - windowHeight / 2;
  126. // Scale difference
  127. Vector2 mouseRotation = new Vector2(
  128. MOUSE_ROTATION_SCALE * diffX,
  129. -MOUSE_ROTATION_SCALE * diffY);
  130. // Set up Event
  131. ControllerVec2EventArgs args = new ControllerVec2EventArgs(mouseRotation, ref argObjects);
  132. // Set mouse back to center of screen
  133. Mouse.SetPosition(windowWidth / 2, windowHeight / 2);
  134. if (motion != null)
  135. {
  136. motion(args);
  137. }
  138. }
  139. }
  140. #endregion
  141. #region Click Section
  142. // Left Button Check
  143. else if (controlType == ControlType.LeftButton)
  144. {
  145. if (mouseState.LeftButton == ButtonState.Pressed)
  146. {
  147. // Reset timer
  148. controlTimer.Reset();
  149. controlTimer.Start();
  150. if (click != null)
  151. {
  152. click(new ControllerBoolEventArgs(true, ref argObjects));
  153. }
  154. }
  155. }
  156. // Right Button Check
  157. else if (controlType == ControlType.RightButton)
  158. {
  159. if (mouseState.RightButton == ButtonState.Pressed)
  160. {
  161. // Reset timer
  162. controlTimer.Reset();
  163. controlTimer.Start();
  164. if (click != null)
  165. {
  166. click(new ControllerBoolEventArgs(true, ref argObjects));
  167. }
  168. }
  169. }
  170. // Middle Button Click
  171. else if (controlType == ControlType.MiddleButton)
  172. {
  173. if (mouseState.MiddleButton == ButtonState.Pressed)
  174. {
  175. // Reset timer
  176. controlTimer.Reset();
  177. controlTimer.Start();
  178. if (click != null)
  179. {
  180. click(new ControllerBoolEventArgs(true, ref argObjects));
  181. }
  182. }
  183. }
  184. #endregion
  185. #region Scroll Section
  186. // Scroll Check
  187. else if (controlType == ControlType.ScrollWheel)
  188. {
  189. if (mouseState.ScrollWheelValue != lastMouseWheelPos)
  190. {
  191. // Reset timer
  192. controlTimer.Reset();
  193. controlTimer.Start();
  194. int diff;
  195. if (mouseState.ScrollWheelValue > lastMouseWheelPos)
  196. {
  197. diff = 1;
  198. }
  199. else
  200. {
  201. diff = -1;
  202. }
  203. lastMouseWheelPos = mouseState.ScrollWheelValue;
  204. if (scroll != null)
  205. {
  206. scroll(new ControllerIntEventArgs(diff, ref argObjects));
  207. }
  208. }
  209. }
  210. #endregion
  211. }
  212. }
  213. }
  214. }