private void OnMouseInputEvent(Vector2 pixelPosition, MouseButton button, InputEventType type, float value = 0) { // The mouse wheel event are still received even when the mouse cursor is out of the control boundaries. Discard the event in this case. if (type == InputEventType.Wheel && !uiControl.ClientRectangle.Contains(uiControl.PointToClient(Control.MousePosition))) { return; } // the mouse events series has been interrupted because out of the window. if (type == InputEventType.Up && !MouseButtonCurrentlyDown[(int)button]) { return; } CurrentMousePosition = NormalizeScreenPosition(pixelPosition); var mouseInputEvent = new MouseInputEvent { Type = type, MouseButton = button, Value = value }; lock (MouseInputEvents) MouseInputEvents.Add(mouseInputEvent); if (type != InputEventType.Wheel) { var buttonId = (int)button; MouseButtonCurrentlyDown[buttonId] = type == InputEventType.Down; HandlePointerEvents(buttonId, CurrentMousePosition, InputEventTypeToPointerState(type), PointerType.Mouse); } }
void Mouse_ButtonDown(object sender, MouseButtonEventArgs e) { var button = ConvertMouseButtonFromOpenTK(e.Button); var buttonId = (int)button; CurrentMousePosition = new Vector2(e.X / ControlWidth, e.Y / ControlHeight); var mouseInputEvent = new MouseInputEvent { Type = InputEventType.Down, MouseButton = button }; lock (MouseInputEvents) MouseInputEvents.Add(mouseInputEvent); MouseButtonCurrentlyDown[buttonId] = true; HandlePointerEvents(buttonId, CurrentMousePosition, PointerState.Down, PointerType.Mouse); }
void Mouse_ButtonUp(object sender, MouseButtonEventArgs e) { var button = ConvertMouseButtonFromOpenTK(e.Button); var buttonId = (int)button; // the mouse events series has been interrupted because out of the window. if (!MouseButtonCurrentlyDown[buttonId]) { return; } CurrentMousePosition = new Vector2(e.X / ControlWidth, e.Y / ControlHeight); var mouseInputEvent = new MouseInputEvent { Type = InputEventType.Up, MouseButton = button }; lock (MouseInputEvents) MouseInputEvents.Add(mouseInputEvent); MouseButtonCurrentlyDown[buttonId] = false; HandlePointerEvents(buttonId, CurrentMousePosition, PointerState.Up, PointerType.Mouse); }
void HandlePointerEvent(WinRTPointerPoint p, PointerState ptrState) { var pointerType = ConvertPointerDeviceType(p.PointerDevice.PointerDeviceType); var isMouse = pointerType == PointerType.Mouse; var position = NormalizeScreenPosition(PointToVector2(p.Position)); if (isMouse) { if (ptrState == PointerState.Cancel || ptrState == PointerState.Out) { // invalidate mouse and current pointers LostFocus = true; for (int i = 0; i < MouseButtonCurrentlyDown.Length; i++) { if (MouseButtonCurrentlyDown[i]) { HandlePointerEvents(i, position, PointerState.Out, pointerType); MouseButtonCurrentlyDown[i] = false; } } } else // down/up/move { // Note: The problem here is that the PointerPressed event is not triggered twice when two button are pressed together. // That is why we are forced to continuously keep the state of all buttons of the mouse. MouseInputEvent mouseEvent; // Trigger mouse button and pointer Down events for newly pressed buttons. foreach (MouseButton button in Enum.GetValues(typeof(MouseButton))) { var buttonId = (int)button; if (!MouseButtonCurrentlyDown[buttonId] && MouseButtonIsPressed(p.Properties, button)) { lock (MouseInputEvents) { mouseEvent = new MouseInputEvent { Type = InputEventType.Down, MouseButton = button }; MouseInputEvents.Add(mouseEvent); } HandlePointerEvents(buttonId, position, PointerState.Down, pointerType); MouseButtonCurrentlyDown[buttonId] = true; } } // Trigger Move events to pointer that have changed position if (CurrentMousePosition != position) { foreach (MouseButton button in Enum.GetValues(typeof(MouseButton))) { var buttonId = (int)button; if (MouseButtonCurrentlyDown[buttonId]) { HandlePointerEvents(buttonId, position, PointerState.Move, pointerType); } } } // Trigger mouse button and pointer Up events for newly released buttons. foreach (MouseButton button in Enum.GetValues(typeof(MouseButton))) { var buttonId = (int)button; if (MouseButtonCurrentlyDown[buttonId] && !MouseButtonIsPressed(p.Properties, button)) { lock (MouseInputEvents) { mouseEvent = new MouseInputEvent { Type = InputEventType.Up, MouseButton = button }; MouseInputEvents.Add(mouseEvent); } HandlePointerEvents(buttonId, position, PointerState.Up, pointerType); MouseButtonCurrentlyDown[buttonId] = false; } } // Trigger mouse wheel events if (Math.Abs(p.Properties.MouseWheelDelta) > MathUtil.ZeroTolerance) { lock (MouseInputEvents) { mouseEvent = new MouseInputEvent { Type = InputEventType.Wheel, MouseButton = MouseButton.Middle, Value = p.Properties.MouseWheelDelta }; MouseInputEvents.Add(mouseEvent); } } } // Update mouse cursor position CurrentMousePosition = position; } else { HandlePointerEvents((int)p.PointerId, position, ptrState, pointerType); } }