//--------------------------------------------------------------------------------------------------------------------------------------------------- // // Copyright (C)2007 DarkWynter Studios. All rights reserved. // //--------------------------------------------------------------------------------------------------------------------------------------------------- // {Contact : darkwynter.com for licensing information //--------------------------------------------------------------------------------------------------------------------------------------------------- namespace DarkWynter.Engine.Controllers { #region Using Statements using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using System.Diagnostics; using DarkWynter.Stream; using DarkWynter.Engine.ObjectLib; using DarkWynter.Engine.Menus; using Globals; #endregion /// /// Mouse event class for mapping a Mouse button/motion to an event /// public class MouseControl { // Time delay between key presses to avoid double input private int KEY_TIMER_MAX_MILLI = 10; private Stopwatch controlTimer = new Stopwatch(); private float MOUSE_ROTATION_SCALE = 0.005f; private float lastMouseX, lastMouseY; private int windowHeight, windowWidth; // Game window Height and Width for resetting the Mouse position private int lastMouseWheelPos = 0; // Position of mouse wheel on element list /// /// On click delegate type /// /// Controller boolean event arguments public delegate void OnClickHandler(ControllerBoolEventArgs args); private event OnClickHandler click; /// /// On motion delegate type /// /// Controller vector2 event arguments public delegate void OnMotionHandler(ControllerVec2EventArgs args); private event OnMotionHandler motion; /// /// On scroll delegate type /// /// Controller integer event arguments public delegate void OnScrollHandler(ControllerIntEventArgs args); private event OnScrollHandler scroll; /// /// Type of control for this event /// public Enums_Engine.ControlType controlType; /// /// Click constructor /// /// Click control type /// Click handler /// Time delay between multiple invocations public MouseControl(Enums_Engine.ControlType type, OnClickHandler onClickHandler, int timerValue) { controlType = type; click += new OnClickHandler(onClickHandler); controlTimer.Start(); KEY_TIMER_MAX_MILLI = timerValue; } /// /// Motion constructor /// /// Motion control type /// Motion handler /// Time delay between multiple invocations public MouseControl(Enums_Engine.ControlType type, OnMotionHandler onMotionHandler, int timerValue) { controlType = type; motion += new OnMotionHandler(onMotionHandler); controlTimer.Start(); KEY_TIMER_MAX_MILLI = timerValue; lastMouseX = Mouse.GetState().X; lastMouseY = Mouse.GetState().Y; } /// /// Scroll constructor /// /// Scroll control type /// Scroll handler /// Time delay between multiple invocations public MouseControl(Enums_Engine.ControlType type, OnScrollHandler onScrollHandler, int timerValue) { controlType = type; scroll += new OnScrollHandler(onScrollHandler); controlTimer.Start(); KEY_TIMER_MAX_MILLI = timerValue; lastMouseWheelPos = Mouse.GetState().ScrollWheelValue; } /// /// Update Method /// /// Updated mouse state /// Object Library /// Menu System public void Update(MouseState mouseState, ref ObjectLibrary objectLibrary, ref MenuSystem menuSystem) { // Check double-input timer if (controlTimer.ElapsedMilliseconds > KEY_TIMER_MAX_MILLI) { // Reset timer controlTimer.Reset(); controlTimer.Start(); // Get updated window dimensions windowHeight = Statics_Stream.RenderSettings.clientHeight; windowWidth = Statics_Stream.RenderSettings.clientWidth; #region Movement Section // Calculate Mouse movement since last frame if (controlType == Enums_Engine.ControlType.Motion) { // If mouse is in the window if (mouseState.X > 0 && mouseState.X < windowWidth - 1 && mouseState.Y > 0 && mouseState.Y < windowHeight - 1) { // Find change in mouse position since last frame float diffX = mouseState.X - windowWidth / 2; float diffY = mouseState.Y - windowHeight / 2; // Scale difference Vector2 mouseRotation = new Vector2( MOUSE_ROTATION_SCALE * diffX, -MOUSE_ROTATION_SCALE * diffY); // Set up Event ControllerVec2EventArgs args = new ControllerVec2EventArgs(mouseRotation, ref objectLibrary, ref menuSystem); // Set mouse back to center of screen Mouse.SetPosition(windowWidth / 2, windowHeight / 2); if (motion != null) { motion(args); } } } #endregion #region Click Section // Left Button Check else if (controlType == Enums_Engine.ControlType.LeftButton) { if (mouseState.LeftButton == ButtonState.Pressed) { // Reset timer controlTimer.Reset(); controlTimer.Start(); if (click != null) { click(new ControllerBoolEventArgs(true, ref objectLibrary, ref menuSystem)); } } } // Right Button Check else if (controlType == Enums_Engine.ControlType.RightButton) { if (mouseState.RightButton == ButtonState.Pressed) { // Reset timer controlTimer.Reset(); controlTimer.Start(); if (click != null) { click(new ControllerBoolEventArgs(true, ref objectLibrary, ref menuSystem)); } } } // Middle Button Click else if (controlType == Enums_Engine.ControlType.MiddleButton) { if (mouseState.MiddleButton == ButtonState.Pressed) { // Reset timer controlTimer.Reset(); controlTimer.Start(); if (click != null) { click(new ControllerBoolEventArgs(true, ref objectLibrary, ref menuSystem)); } } } #endregion #region Scroll Section // Scroll Check else if (controlType == Enums_Engine.ControlType.ScrollWheel) { if (mouseState.ScrollWheelValue != lastMouseWheelPos) { // Reset timer controlTimer.Reset(); controlTimer.Start(); int diff; if (mouseState.ScrollWheelValue > lastMouseWheelPos) { diff = 1; } else { diff = -1; } lastMouseWheelPos = mouseState.ScrollWheelValue; if (scroll != null) { scroll(new ControllerIntEventArgs(diff, ref objectLibrary, ref menuSystem)); } } } #endregion } } } }