Ejemplo n.º 1
0
    private void UpdateMouse(TimeSpan deltaTime)
    {
      // ----- Update mouse states.
      _previousMouseState = _newMouseState;
      _newMouseState = Mouse.GetState();
      MousePosition = MousePositionRaw;
      MousePositionDelta = MousePositionDeltaRaw;

      // ----- Find pressed mouse button.
      MouseButtons? pressedMouseButton = null;
      foreach (MouseButtons button in _mouseButtons)
      {
        if (IsDown(ref _newMouseState, button) && IsUp(ref _previousMouseState, button))
        {
          pressedMouseButton = button;
          break;
        }
      }

      // ----- Handle mouse button double clicks and button repetition.
      _lastMouseButton.IsDoubleClick = false;
      _lastMouseButton.IsVirtualPress = false;
      if (!pressedMouseButton.HasValue)
      {
        // No mouse button pressed.
        // Increase or reset down duration.
        if (IsDown(_lastMouseButton.Button))
        {
          // Previously pressed mouse button is still down.
          // Increase down duration.
          _lastMouseButton.DownDuration += deltaTime;

          // If the start interval is exceeded, we generate a virtual button press.
          if (_lastMouseButton.DownDuration >= Settings.RepetitionDelay)
          {
            // Generate virtual button press.
            _lastMouseButton.IsVirtualPress = true;

            // Subtract repetition interval from down duration. This way the repetition interval
            // must pass until the if condition is true again.
            _lastMouseButton.DownDuration -= Settings.RepetitionInterval;
          }
        }
        else
        {
          // Reset down duration.
          _lastMouseButton.DownDuration = TimeSpan.Zero;
        }

        // Measure time between clicks.
        if (_lastMouseButton.TimeSinceLastClick != TimeSpan.MaxValue)
          _lastMouseButton.TimeSinceLastClick += deltaTime;
      }
      else
      {
        // Mouse button was pressed.
        // Check for double-click.
        if (pressedMouseButton == _lastMouseButton.Button
            && _lastMouseButton.TimeSinceLastClick < Settings.DoubleClickTime - deltaTime
            && Vector2F.Absolute(_lastMouseButton.MouseClickPosition - MousePosition) < Settings.DoubleClickSize)
        {
          // Double-click detected.
          _lastMouseButton.IsDoubleClick = true;

          // The current click cannot be used for another double-click.
          _lastMouseButton.TimeSinceLastClick = TimeSpan.MaxValue;
        }
        else
        {
          // Wrong button pressed or button pressed too late.
          // Restart double-click logic.
          _lastMouseButton.TimeSinceLastClick = TimeSpan.Zero;
        }

        _lastMouseButton.Button = pressedMouseButton.Value;
        _lastMouseButton.DownDuration = TimeSpan.Zero;
        _lastMouseButton.MouseClickPosition = MousePosition;
      }

      // ----- Reset mouse position if mouse-centering is enabled. 
      if (EnableMouseCentering)
        if (GlobalSettings.PlatformID != PlatformID.WindowsPhone7 && GlobalSettings.PlatformID != PlatformID.WindowsPhone8 && GlobalSettings.PlatformID != PlatformID.Android && GlobalSettings.PlatformID != PlatformID.iOS)
          Mouse.SetPosition((int)Settings.MouseCenter.X, (int)Settings.MouseCenter.Y);
    }