From 39328e1106794f8e71973ed0dd1d76a36141cef9 Mon Sep 17 00:00:00 2001 From: Max Thrun Date: Wed, 16 Sep 2020 21:47:32 -0700 Subject: [PATCH] add an example demonstrating how you can make custom dragable lines --- implot_demo.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/implot_demo.cpp b/implot_demo.cpp index 6f67bed..cf324e7 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -1218,6 +1218,38 @@ void ShowDemoWindow(bool* p_open) { } } //------------------------------------------------------------------------- + if (ImGui::CollapsingHeader("Custom Dragable Lines")) { + static float line_value = 0.5f; + const float line_width = 3.0f; + const float grabbable_width = 5.0f; + if (ImPlot::BeginPlot("Dragable Lines")) { + + auto x0 = ImPlot::GetPlotPos().x; + auto x1 = ImPlot::GetPlotPos().x + ImPlot::GetPlotSize().x; + auto y = ImPlot::PlotToPixels(0, line_value).y; + ImPlot::PushPlotClipRect(); + ImPlot::GetPlotDrawList()->AddLine({ x0, y }, { x1, y }, IM_COL32_WHITE, line_width); + ImPlot::PopPlotClipRect(); + + ImVec2 old_cursor_pos = ImGui::GetCursorScreenPos(); + ImVec2 new_cursor_pos = ImVec2(x0, y - grabbable_width / 2.0f); + ImGui::SetItemAllowOverlap(); + ImGui::SetCursorScreenPos(new_cursor_pos); + ImGui::InvisibleButton("horizontal_line", ImVec2(x1 - x0, grabbable_width)); + ImGui::SetCursorScreenPos(old_cursor_pos); + + if (ImGui::IsItemHovered() || ImGui::IsItemActive()) { + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS); + } + + if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) { + line_value = ImPlot::GetPlotMousePos().y; + } + + ImPlot::EndPlot(); + } + } + //------------------------------------------------------------------------- ImGui::End(); }