diff --git a/implot.cpp b/implot.cpp index 1b699ea..48e0dbe 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1474,13 +1474,13 @@ void EndPlot() { label_bb.Min = legend_content_bb.Min + legend_spacing + ImVec2(0, i * txt_ht) + ImVec2(2, 2); label_bb.Max = legend_content_bb.Min + legend_spacing + ImVec2(0, i * txt_ht) + ImVec2(legend_content_bb.Max.x, legend_icon_size - 2); ImU32 col_hl_txt; - if (ImHasFlag(plot.Flags, ImPlotFlags_Highlight) && hov_legend && (icon_bb.Contains(IO.MousePos) || label_bb.Contains(IO.MousePos))) { - item->Highlight = true; + if (hov_legend && (icon_bb.Contains(IO.MousePos) || label_bb.Contains(IO.MousePos))) { + item->LegendHovered = true; col_hl_txt = ImGui::GetColorU32(ImLerp(col_txt, item->Color, 0.25f)); } else { - item->Highlight = false; + item->LegendHovered = false; col_hl_txt = ImGui::GetColorU32(col_txt); } ImU32 iconColor; @@ -1820,10 +1820,7 @@ bool IsLegendEntryHovered(const char* label_id) { IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotItemHighlight() needs to be called between BeginPlot() and EndPlot()!"); ImGuiID id = ImGui::GetID(label_id); ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); - if (item && item->Highlight) - return true; - else - return false; + return item && item->LegendHovered; } bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags) { @@ -1831,7 +1828,7 @@ bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags) { IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginLegendDragDropSource() needs to be called between BeginPlot() and EndPlot()!"); ImGuiID source_id = ImGui::GetID(label_id); ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(source_id); - bool is_hovered = item && item->Highlight; + bool is_hovered = item && item->LegendHovered; ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -1891,7 +1888,7 @@ bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags) { return false; } -void EndDragDropSource() { +void EndLegendDragDropSource() { ImGui::EndDragDropSource(); } diff --git a/implot.h b/implot.h index d351413..c41af89 100644 --- a/implot.h +++ b/implot.h @@ -422,8 +422,6 @@ ImPlotLimits GetPlotLimits(int y_axis = IMPLOT_AUTO); bool IsPlotQueried(); // Returns the current plot query bounds. ImPlotLimits GetPlotQuery(int y_axis = IMPLOT_AUTO); -// Returns true if a plot item legend entry is hovered. -bool IsLegendEntryHovered(const char* label_id); //----------------------------------------------------------------------------- // Plot and Item Styling @@ -513,6 +511,17 @@ void ShowColormapScale(double scale_min, double scale_max, float height); // Returns a null terminated string name for a built-in colormap. const char* GetColormapName(ImPlotColormap colormap); +//----------------------------------------------------------------------------- +// Legend Utils +//----------------------------------------------------------------------------- + +// Returns true if a plot item legend entry is hovered. +bool IsLegendEntryHovered(const char* label_id); +// Begin a drag and drop source from a legend entry. The only supported flag is SourceNoPreviewTooltip +bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags = 0); +// End legend drag and drop source. +void EndLegendDragDropSource(); + //----------------------------------------------------------------------------- // Miscellaneous //----------------------------------------------------------------------------- @@ -534,11 +543,6 @@ void PushPlotClipRect(); // Pop plot clip rect. void PopPlotClipRect(); -// Begin a drag and drop source from a legend entry. The only supported flag is SourceNoPreviewTooltip -bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags = 0); -// End drag and drop source. -void EndDragDropSource(); - //----------------------------------------------------------------------------- // Demo (add implot_demo.cpp to your sources!) //----------------------------------------------------------------------------- diff --git a/implot_demo.cpp b/implot_demo.cpp index 23b6ae2..1fd1dd7 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -715,7 +715,7 @@ void ShowDemoWindow(bool* p_open) { init = false; } ImGui::BulletText("Drag data items from the left column onto the plot or onto a specific y-axis."); - ImGui::BulletText("Drag data items from the legend onto a specific y-axis."); + ImGui::BulletText("Redrag data items from the legend onto other y-axes."); ImGui::BeginGroup(); if (ImGui::Button("Clear", ImVec2(100, 0))) { for (int i = 0; i < K_CHANNELS; ++i) { @@ -755,17 +755,21 @@ void ShowDemoWindow(bool* p_open) { sprintf(label, "data_%d", i); ImPlot::SetPlotYAxis(yAxis[i]); ImPlot::PlotLine(label, &data[i].Data[0].x, &data[i].Data[0].y, data[i].Data.size(), data[i].Offset, 2 * sizeof(t_float)); + // allow legend labels to be dragged and dropped if (ImPlot::BeginLegendDragDropSource(label)) { ImGui::SetDragDropPayload("DND_PLOT", &i, sizeof(int)); ImGui::TextUnformatted(label); - ImPlot::EndDragDropSource(); + ImPlot::EndLegendDragDropSource(); } } } + // make our plot a drag and drop target if (ImGui::BeginDragDropTarget()) { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_PLOT")) { int i = *(int*)payload->Data; show[i] = true; + yAxis[i] = 0; + // set specific y-axis if hovered for (int y = 0; y < 3; y++) { if (ImPlot::IsPlotYAxisHovered(y)) yAxis[i] = y; diff --git a/implot_internal.h b/implot_internal.h index 4475705..e83791a 100644 --- a/implot_internal.h +++ b/implot_internal.h @@ -301,7 +301,7 @@ struct ImPlotItem ImVec4 Color; int NameOffset; bool Show; - bool Highlight; + bool LegendHovered; bool SeenThisFrame; ImPlotItem() { @@ -310,7 +310,7 @@ struct ImPlotItem NameOffset = -1; Show = true; SeenThisFrame = false; - Highlight = false; + LegendHovered = false; } ~ImPlotItem() { ID = 0; } diff --git a/implot_items.cpp b/implot_items.cpp index ed15992..e3de59d 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -145,7 +145,7 @@ bool BeginItem(const char* label_id, ImPlotCol recolor_from) { s.Colors[ImPlotCol_Fill].w *= s.FillAlpha; // s.Colors[ImPlotCol_MarkerFill].w *= s.FillAlpha; // TODO: this should be separate, if it at all // apply highlight mods - if (item->Highlight) { + if (item->LegendHovered && ImHasFlag(gp.CurrentPlot->Flags, ImPlotFlags_Highlight)) { s.LineWeight *= 2; s.MarkerWeight *= 2; // TODO: highlight fills?