From 4cd438f66302fa8dd93ad8afe4780e6b30fec262 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 20 Mar 2015 11:13:18 +0000 Subject: [PATCH] Added IsMouseDragging(), GetMouseDragDelta() given button and lock threshold. added io.MouseDragThreshold setting. (#167) Removed GetItemActiveDragDelta() --- imgui.cpp | 44 +++++++++++++++++++++++++++----------------- imgui.h | 6 ++++-- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index b6f0cd7a..33e03e12 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -577,6 +577,7 @@ ImGuiIO::ImGuiIO() MousePosPrev = ImVec2(-1,-1); MouseDoubleClickTime = 0.30f; MouseDoubleClickMaxDist = 6.0f; + MouseDragThreshold = 6.0f; UserData = NULL; // User functions @@ -1840,6 +1841,11 @@ void ImGui::NewFrame() g.IO.MouseClickedTime[i] = g.Time; } g.IO.MouseClickedPos[i] = g.IO.MousePos; + g.IO.MouseDragMaxDistanceSqr[i] = 0.0f; + } + else if (g.IO.MouseDown[i]) + { + g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(g.IO.MousePos - g.IO.MouseClickedPos[i])); } } for (size_t i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) @@ -2539,16 +2545,29 @@ bool ImGui::IsMouseDoubleClicked(int button) return g.IO.MouseDoubleClicked[button]; } +bool ImGui::IsMouseDragging(int button, float lock_threshold) +{ + ImGuiState& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (lock_threshold < 0.0f) + lock_threshold = g.IO.MouseDragThreshold; + return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold; +} + ImVec2 ImGui::GetMousePos() { return GImGui->IO.MousePos; } -ImVec2 ImGui::GetMouseDragDelta() +ImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold) { ImGuiState& g = *GImGui; - if (g.IO.MouseDown[0]) - return g.IO.MousePos - g.IO.MouseClickedPos[0]; // Assume we can only get active with left-mouse button (at the moment). + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (lock_threshold < 0.0f) + lock_threshold = g.IO.MouseDragThreshold; + if (g.IO.MouseDown[button]) + if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold) + return g.IO.MousePos - g.IO.MouseClickedPos[button]; // Assume we can only get active with left-mouse button (at the moment). return ImVec2(0.0f, 0.0f); } @@ -2581,17 +2600,6 @@ bool ImGui::IsAnyItemActive() return g.ActiveId != 0; } -ImVec2 ImGui::GetItemActiveDragDelta() -{ - if (ImGui::IsItemActive()) - { - ImGuiState& g = *GImGui; - if (g.IO.MouseDown[0]) - return g.IO.MousePos - g.IO.MouseClickedPos[0]; // Assume we can only get active with left-mouse button (at the moment). - } - return ImVec2(0.0f, 0.0f); -} - ImVec2 ImGui::GetItemRectMin() { ImGuiWindow* window = GetCurrentWindow(); @@ -9417,11 +9425,13 @@ void ImGui::ShowTestWindow(bool* opened) if (ImGui::TreeNode("Dragging")) { // You can use ImGui::GetItemActiveDragDelta() to query for the dragged amount on any widget. - static ImVec2 value(0.0f, 0.0f); + static ImVec2 value_raw(0.0f, 0.0f); + static ImVec2 value_with_lock_threshold(0.0f, 0.0f); ImGui::Button("Drag Me"); if (ImGui::IsItemActive()) { - value = ImGui::GetItemActiveDragDelta(); + value_raw = ImGui::GetMouseDragDelta(0, 0.0f); + value_with_lock_threshold = ImGui::GetMouseDragDelta(0); //ImGui::SetTooltip("Delta: %.1f, %.1f", value.x, value.y); // Draw a line between the button and the mouse cursor @@ -9430,7 +9440,7 @@ void ImGui::ShowTestWindow(bool* opened) draw_list->AddLine(ImGui::CalcItemRectClosestPoint(ImGui::GetIO().MousePos, true, -2.0f), ImGui::GetIO().MousePos, ImColor(ImGui::GetStyle().Colors[ImGuiCol_Button]), 2.0f); draw_list->PopClipRect(); } - ImGui::SameLine(); ImGui::Text("Value: %.1f, %.1f", value.x, value.y); + ImGui::SameLine(); ImGui::Text("Raw (%.1f, %.1f), WithLockThresold (%.1f, %.1f)", value_raw.x, value_raw.y, value_with_lock_threshold.x, value_with_lock_threshold.y); ImGui::TreePop(); } } diff --git a/imgui.h b/imgui.h index 5ba5e8bb..3319624b 100644 --- a/imgui.h +++ b/imgui.h @@ -355,7 +355,6 @@ namespace ImGui IMGUI_API bool IsItemHoveredRectOnly(); // was the last item hovered by mouse? even if another item is active while we are hovering this. IMGUI_API bool IsItemActive(); // was the last item active? (e.g. button being held, text field being edited- items that don't interact will always return false) IMGUI_API bool IsAnyItemActive(); // - IMGUI_API ImVec2 GetItemActiveDragDelta(); // mouse delta from the time the item first got active IMGUI_API ImVec2 GetItemRectMin(); // get bounding rect of last item IMGUI_API ImVec2 GetItemRectMax(); // " IMGUI_API ImVec2 GetItemRectSize(); // " @@ -369,9 +368,10 @@ namespace ImGui IMGUI_API bool IsMouseHoveringWindow(); // is mouse hovering current window ("window" in API names always refer to current window) IMGUI_API bool IsMouseHoveringAnyWindow(); // is mouse hovering any active imgui window IMGUI_API bool IsMouseHoveringRect(const ImVec2& rect_min, const ImVec2& rect_max);// is mouse hovering given bounding rect + IMGUI_API bool IsMouseDragging(int button = 0, float lock_threshold = -1.0f); // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold. IMGUI_API bool IsPosHoveringAnyWindow(const ImVec2& pos); // is given position hovering any active imgui window IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls - IMGUI_API ImVec2 GetMouseDragDelta(); // dragging amount - also see: GetItemActiveDragDelta(). + IMGUI_API ImVec2 GetMouseDragDelta(int button = 0, float lock_threshold = -1.0f); // dragging amount, also see: GetItemActiveDragDelta(). if lock_threshold < -1.0f uses io.MouseDraggingThreshold. IMGUI_API float GetTime(); IMGUI_API int GetFrameCount(); IMGUI_API const char* GetStyleColName(ImGuiCol idx); @@ -581,6 +581,7 @@ struct ImGuiIO const char* LogFilename; // = "imgui_log.txt" // Path to .log file (default parameter to ImGui::LogToFile when no file is specified). float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. + float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging int KeyMap[ImGuiKey_COUNT]; // // Map of indices into the KeysDown[512] entries array void* UserData; // = NULL // Store your own data for retrieval by callbacks. @@ -650,6 +651,7 @@ struct ImGuiIO bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? bool MouseDownOwned[5]; // Track if button was clicked inside a window. We don't request mouse capture from the application if click started outside ImGui bounds. float MouseDownTime[5]; // Time the mouse button has been down + float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the click point float KeysDownTime[512]; // Time the keyboard key has been down IMGUI_API ImGuiIO();