Skip to content

Commit 81b8b19

Browse files
authored
fix: DragRect resizing when its size is zero (#661)
1 parent 01f53eb commit 81b8b19

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

implot.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4102,31 +4102,34 @@ bool DragRect(int n_id, double* x_min, double* y_min, double* x_max, double* y_m
41024102

41034103
bool modified = false;
41044104
bool clicked = false, hovered = false, held = false;
4105-
ImRect b_rect(pc.x-DRAG_GRAB_HALF_SIZE,pc.y-DRAG_GRAB_HALF_SIZE,pc.x+DRAG_GRAB_HALF_SIZE,pc.y+DRAG_GRAB_HALF_SIZE);
41064105

4107-
ImGui::KeepAliveID(id);
4108-
if (input) {
4109-
// middle point
4110-
clicked = ImGui::ButtonBehavior(b_rect,id,&hovered,&held);
4111-
if (out_clicked) *out_clicked = clicked;
4112-
if (out_hovered) *out_hovered = hovered;
4113-
if (out_held) *out_held = held;
4114-
}
4106+
const bool is_movable = *x_min != *x_max || *y_min != *y_max;
4107+
if (is_movable) {
4108+
ImGui::KeepAliveID(id);
4109+
if (input) {
4110+
// middle point
4111+
ImRect b_rect(pc.x-DRAG_GRAB_HALF_SIZE,pc.y-DRAG_GRAB_HALF_SIZE,pc.x+DRAG_GRAB_HALF_SIZE,pc.y+DRAG_GRAB_HALF_SIZE);
4112+
clicked = ImGui::ButtonBehavior(b_rect,id,&hovered,&held);
4113+
if (out_clicked) *out_clicked = clicked;
4114+
if (out_hovered) *out_hovered = hovered;
4115+
if (out_held) *out_held = held;
4116+
}
41154117

4116-
if ((hovered || held) && show_curs)
4117-
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
4118-
if (held && ImGui::IsMouseDragging(0)) {
4119-
for (int i = 0; i < 4; ++i) {
4120-
ImPlotPoint pp = PixelsToPlot(p[i] + ImGui::GetIO().MouseDelta,IMPLOT_AUTO,IMPLOT_AUTO);
4121-
*y[i] = pp.y;
4122-
*x[i] = pp.x;
4118+
if ((hovered || held) && show_curs)
4119+
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
4120+
if (held && ImGui::IsMouseDragging(0)) {
4121+
for (int i = 0; i < 4; ++i) {
4122+
ImPlotPoint pp = PixelsToPlot(p[i] + ImGui::GetIO().MouseDelta,IMPLOT_AUTO,IMPLOT_AUTO);
4123+
*y[i] = pp.y;
4124+
*x[i] = pp.x;
4125+
}
4126+
modified = true;
41234127
}
4124-
modified = true;
41254128
}
41264129

41274130
for (int i = 0; i < 4; ++i) {
41284131
// points
4129-
b_rect = ImRect(p[i].x-DRAG_GRAB_HALF_SIZE,p[i].y-DRAG_GRAB_HALF_SIZE,p[i].x+DRAG_GRAB_HALF_SIZE,p[i].y+DRAG_GRAB_HALF_SIZE);
4132+
ImRect b_rect(p[i].x - DRAG_GRAB_HALF_SIZE, p[i].y - DRAG_GRAB_HALF_SIZE, p[i].x + DRAG_GRAB_HALF_SIZE, p[i].y + DRAG_GRAB_HALF_SIZE);
41304133
ImGuiID p_id = id + i + 1;
41314134
ImGui::KeepAliveID(p_id);
41324135
if (input) {

0 commit comments

Comments
 (0)