Skip to content

Mouse click dragging: surprising click positions? #167

Closed
@unpacklo

Description

Hey Omar,

I've been working on a feature that required me to detect when the mouse was clicked and subsequently dragged. I wrote a small code sequence to help me detect this case:

    static bool leftPressed = false, leftReleased = false, leftPrev = false, isDragging = false;
    static int dragCount = 0;
    ImGuiIO *io = &ImGui::GetIO();
    leftPressed = !leftPrev && io->MouseDown[0];
    leftReleased = leftPrev && !io->MouseDown[0];
    leftPrev = io->MouseDown[0];

    if (leftReleased)
    {
        printf("Left released!\n");
        isDragging = false;
    }

    const float DRAG_RADIUS = 4.0f;
    ImVec2 distFromClick(io->MouseClickedPos[0]);
    distFromClick -= ImGui::GetMousePos();

    if (!isDragging && io->MouseDown[0] && (((distFromClick.x * distFromClick.x) + (distFromClick.y * distFromClick.y)) > (DRAG_RADIUS * DRAG_RADIUS)))
    {
        printf("Started dragging!\n");
        isDragging = true;
    }

    if (isDragging)
    {
        printf("Dragging %d\n", ++dragCount);
    }

The issue I ran into was that this can detect double clicking as a drag if the mouse is moved quickly between each click (so that the drag threshold is exceeded).

I took a look at the code that sets the click positions

imgui/imgui.cpp

Lines 1829 to 1839 in ff5378b

if (g.Time - g.IO.MouseClickedTime[i] < g.IO.MouseDoubleClickTime)
{
if (ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i]) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist)
g.IO.MouseDoubleClicked[i] = true;
g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click
}
else
{
g.IO.MouseClickedTime[i] = g.Time;
g.IO.MouseClickedPos[i] = g.IO.MousePos;
}
and was wondering if there is a reason why the click position is not updated during the double click time? Updating the click positions on every click, regardless of whether or not its in the double click time, makes more sense to me.

For now, I work around this issue by keeping track of the click positions on my own.

-Dale Kim

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions