Mouse State

Click and Drag Detection

How do I detect if the user is clicking and dragging?

Abstract art representing computer programming

Detecting click-and-drag interactions involves tracking both the mouse button state and movement. Here's how to implement it:

#include <SDL.h>
#include <iostream>
#include "Window.h"

class DragDetector {
 public:
  void Tick() {
    int x, y;
    Uint32 Buttons = SDL_GetMouseState(&x, &y);

    if (Buttons & SDL_BUTTON_LMASK) {
      if (!IsDragging) {
        // Start of drag
        DragStartX = x;
        DragStartY = y;
        IsDragging = true;
        std::cout << "Started dragging at ("
          << x << ", " << y << ")\n";
      } else {
        // Continue dragging
        int DragDistanceX = x - DragStartX;
        int DragDistanceY = y - DragStartY;
        std::cout << "Dragging - Distance: ("
          << DragDistanceX << ", "
          << DragDistanceY << ")\n";
      }
    } else if (IsDragging) {
      // End of drag
      IsDragging = false;
      std::cout << "Stopped dragging\n";
    }
  }

 private:
  bool IsDragging{false};
  int DragStartX{0};
  int DragStartY{0};
};

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  Window GameWindow;
  DragDetector Detector;
  SDL_Event E;

  while (true) {
    while (SDL_PollEvent(&E)) {
      // Handle other events...
    }

    Detector.Tick();
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}
Started dragging at (100, 150)
Dragging - Distance: (5, 2)
Dragging - Distance: (10, 5)
Dragging - Distance: (15, 8)
Stopped dragging

How It Works

The drag detection system works by:

  1. Checking if the left mouse button is pressed using SDL_GetMouseState()
  2. When the button is first pressed, storing the starting position
  3. While dragging, calculating the distance from the start position
  4. Detecting when the button is released to end the drag

You can enhance this basic system by:

  • Adding a minimum drag distance before considering it a drag (to distinguish from clicks)
  • Tracking drag velocity
  • Storing the drag path for more complex gestures
  • Adding support for right-button dragging

This approach using SDL_GetMouseState() is often more reliable than using events for drag detection, as it ensures you don't miss any mouse movement between frames.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved