void InputEvents_MouseMoved(MouseData md) { x = md.X; y = md.Y; //clipping //... }
public void CheckForEvents(GameTime gameTime) { //detect keyboard events Keys[] keys = Keyboard.GetState().GetPressedKeys(); int modifiers = (keys.Contains(Keys.LeftShift) ? (int)KeyModifiers.LeftShift : 0) | (keys.Contains(Keys.RightShift) ? (int)KeyModifiers.RightShift : 0) | (keys.Contains(Keys.LeftControl) ? (int)KeyModifiers.LeftControl : 0) | (keys.Contains(Keys.RightControl) ? (int)KeyModifiers.RightControl : 0) | (keys.Contains(Keys.LeftAlt) ? (int)KeyModifiers.LeftAlt : 0) | (keys.Contains(Keys.RightAlt) ? (int)KeyModifiers.RightAlt : 0); for (int i = 0; i < keypress_times.Length; i++) { if (keypress_times[i] != 0) { if (keys.Contains((Keys)Enum.Parse(typeof(Keys), key_names[i]))) { //add current time keypress_times[i] += gameTime.ElapsedGameTime.Milliseconds; //check for key repeat event if (keypress_times[i] - key_repeat_edge > key_repeat) { keypress_times[i] -= key_repeat; if (KeyRepeated != null) KeyRepeated((Keys)Enum.Parse(typeof(Keys), key_names[i]), true, modifiers); } } else { //clear time keypress_times[i] = 0; if (KeyReleased != null) KeyReleased((Keys)Enum.Parse(typeof(Keys), key_names[i]), false, modifiers); } } else if (keys.Contains((Keys)Enum.Parse(typeof(Keys), key_names[i]))) { keypress_times[i] += gameTime.ElapsedGameTime.Milliseconds; if (KeyPressed != null) KeyPressed((Keys)Enum.Parse(typeof(Keys), key_names[i]), true, modifiers); } } //detect mouse events MouseState ms = Mouse.GetState(); MouseData md = new MouseData((int)(800 * (ms.X / (double)game.graphics.PreferredBackBufferWidth)), (int)(600 * (ms.Y / (double)game.graphics.PreferredBackBufferHeight)), ms.LeftButton == ButtonState.Pressed, ms.RightButton == ButtonState.Pressed, ms.MiddleButton == ButtonState.Pressed, mouse_x, mouse_y, mbtnLeft != 0, mbtnRight != 0, mbtnCenter != 0); if ((mouse_x != md.X) || (mouse_y != md.Y)) { if (MouseMoved != null) MouseMoved(md); } if ((mbtnLeft != 0) != md.Left) { if (MouseLeftChanged != null) MouseLeftChanged(md); if (!md.Left) mbtnLeft = 0; else mbtnLeft += gameTime.ElapsedGameTime.Milliseconds; } else //left mouse button remained in its state { if (md.Left) { mbtnLeft += gameTime.ElapsedGameTime.Milliseconds; if (mbtnLeft - mousekey_repeat_edge > mousekey_repeat) { if (MouseLeftRepeat != null) MouseLeftRepeat(md); mbtnLeft -= mousekey_repeat; } } } if ((mbtnRight != 0) != md.Right) { if (MouseRightChanged != null) MouseRightChanged(md); if (!md.Right) mbtnRight = 0; else mbtnRight += gameTime.ElapsedGameTime.Milliseconds; } else //right mouse button remained in its state { if (md.Right) { mbtnRight += gameTime.ElapsedGameTime.Milliseconds; if (mbtnRight - mousekey_repeat_edge > mousekey_repeat) { if (MouseRightRepeat != null) MouseRightRepeat(md); mbtnRight -= mousekey_repeat; } } } if ((mbtnCenter != 0) != md.Center) { if (MouseCenterChanged != null) MouseCenterChanged(md); if (!md.Center) mbtnCenter = 0; else mbtnCenter += gameTime.ElapsedGameTime.Milliseconds; } else //center mouse button remained in its state { if (md.Center) { mbtnCenter += gameTime.ElapsedGameTime.Milliseconds; if (mbtnCenter - mousekey_repeat_edge > mousekey_repeat) { if (MouseCenterRepeat != null) MouseCenterRepeat(md); mbtnCenter -= mousekey_repeat; } } } mouse_x = md.X; mouse_y = md.Y; }
void InputEvents_MouseLeftChanged(MouseData md) { pressed = md.Left; InputEvents_MouseMoved(md); }