| 4124 | } |
| 4125 | |
| 4126 | bool DragRect(int n_id, double* x_min, double* y_min, double* x_max, double* y_max, const ImVec4& col, ImPlotDragToolFlags flags, bool* out_clicked, bool* out_hovered, bool* out_held) { |
| 4127 | ImGui::PushID("#IMPLOT_DRAG_RECT"); |
| 4128 | IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != nullptr, "DragRect() needs to be called between BeginPlot() and EndPlot()!"); |
| 4129 | SetupLock(); |
| 4130 | |
| 4131 | if (!ImHasFlag(flags,ImPlotDragToolFlags_NoFit) && FitThisFrame()) { |
| 4132 | FitPoint(ImPlotPoint(*x_min,*y_min)); |
| 4133 | FitPoint(ImPlotPoint(*x_max,*y_max)); |
| 4134 | } |
| 4135 | |
| 4136 | const bool input = !ImHasFlag(flags, ImPlotDragToolFlags_NoInputs); |
| 4137 | const bool show_curs = !ImHasFlag(flags, ImPlotDragToolFlags_NoCursors); |
| 4138 | const bool no_delay = !ImHasFlag(flags, ImPlotDragToolFlags_Delayed); |
| 4139 | bool h[] = {true,false,true,false}; |
| 4140 | double* x[] = {x_min,x_max,x_max,x_min}; |
| 4141 | double* y[] = {y_min,y_min,y_max,y_max}; |
| 4142 | ImVec2 p[4]; |
| 4143 | for (int i = 0; i < 4; ++i) |
| 4144 | p[i] = PlotToPixels(*x[i],*y[i],IMPLOT_AUTO,IMPLOT_AUTO); |
| 4145 | ImVec2 pc = PlotToPixels((*x_min+*x_max)/2,(*y_min+*y_max)/2,IMPLOT_AUTO,IMPLOT_AUTO); |
| 4146 | ImRect rect(ImMin(p[0],p[2]),ImMax(p[0],p[2])); |
| 4147 | ImRect rect_grab = rect; rect_grab.Expand(DRAG_GRAB_HALF_SIZE); |
| 4148 | |
| 4149 | ImGuiMouseCursor cur[4]; |
| 4150 | if (show_curs) { |
| 4151 | cur[0] = (rect.Min.x == p[0].x && rect.Min.y == p[0].y) || (rect.Max.x == p[0].x && rect.Max.y == p[0].y) ? ImGuiMouseCursor_ResizeNWSE : ImGuiMouseCursor_ResizeNESW; |
| 4152 | cur[1] = cur[0] == ImGuiMouseCursor_ResizeNWSE ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; |
| 4153 | cur[2] = cur[1] == ImGuiMouseCursor_ResizeNWSE ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; |
| 4154 | cur[3] = cur[2] == ImGuiMouseCursor_ResizeNWSE ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; |
| 4155 | } |
| 4156 | |
| 4157 | ImVec4 color = IsColorAuto(col) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : col; |
| 4158 | ImU32 col32 = ImGui::ColorConvertFloat4ToU32(color); |
| 4159 | color.w *= 0.25f; |
| 4160 | ImU32 col32_a = ImGui::ColorConvertFloat4ToU32(color); |
| 4161 | const ImGuiID id = ImGui::GetCurrentWindow()->GetID(n_id); |
| 4162 | |
| 4163 | bool modified = false; |
| 4164 | bool clicked = false, hovered = false, held = false; |
| 4165 | |
| 4166 | const bool is_movable = *x_min != *x_max || *y_min != *y_max; |
| 4167 | if (is_movable) { |
| 4168 | ImGui::KeepAliveID(id); |
| 4169 | if (input) { |
| 4170 | // middle point |
| 4171 | 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); |
| 4172 | clicked = ImGui::ButtonBehavior(b_rect,id,&hovered,&held); |
| 4173 | if (out_clicked) *out_clicked = clicked; |
| 4174 | if (out_hovered) *out_hovered = hovered; |
| 4175 | if (out_held) *out_held = held; |
| 4176 | } |
| 4177 | |
| 4178 | if ((hovered || held) && show_curs) |
| 4179 | ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll); |
| 4180 | if (held && ImGui::IsMouseDragging(0)) { |
| 4181 | for (int i = 0; i < 4; ++i) { |
| 4182 | ImPlotPoint pp = PixelsToPlot(p[i] + ImGui::GetIO().MouseDelta,IMPLOT_AUTO,IMPLOT_AUTO); |
| 4183 | *y[i] = pp.y; |
no test coverage detected