1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-10-09 15:47:26 -04:00

tidy up legend DND

This commit is contained in:
epezent 2020-08-31 23:58:15 -05:00
parent fa2c704bb2
commit fb19e76443
5 changed files with 26 additions and 21 deletions

View File

@ -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.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); 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; ImU32 col_hl_txt;
if (ImHasFlag(plot.Flags, ImPlotFlags_Highlight) && hov_legend && (icon_bb.Contains(IO.MousePos) || label_bb.Contains(IO.MousePos))) { if (hov_legend && (icon_bb.Contains(IO.MousePos) || label_bb.Contains(IO.MousePos))) {
item->Highlight = true; item->LegendHovered = true;
col_hl_txt = ImGui::GetColorU32(ImLerp(col_txt, item->Color, 0.25f)); col_hl_txt = ImGui::GetColorU32(ImLerp(col_txt, item->Color, 0.25f));
} }
else else
{ {
item->Highlight = false; item->LegendHovered = false;
col_hl_txt = ImGui::GetColorU32(col_txt); col_hl_txt = ImGui::GetColorU32(col_txt);
} }
ImU32 iconColor; 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()!"); IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotItemHighlight() needs to be called between BeginPlot() and EndPlot()!");
ImGuiID id = ImGui::GetID(label_id); ImGuiID id = ImGui::GetID(label_id);
ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id);
if (item && item->Highlight) return item && item->LegendHovered;
return true;
else
return false;
} }
bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags) { 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()!"); IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginLegendDragDropSource() needs to be called between BeginPlot() and EndPlot()!");
ImGuiID source_id = ImGui::GetID(label_id); ImGuiID source_id = ImGui::GetID(label_id);
ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(source_id); ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(source_id);
bool is_hovered = item && item->Highlight; bool is_hovered = item && item->LegendHovered;
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow; ImGuiWindow* window = g.CurrentWindow;
@ -1891,7 +1888,7 @@ bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags) {
return false; return false;
} }
void EndDragDropSource() { void EndLegendDragDropSource() {
ImGui::EndDragDropSource(); ImGui::EndDragDropSource();
} }

View File

@ -422,8 +422,6 @@ ImPlotLimits GetPlotLimits(int y_axis = IMPLOT_AUTO);
bool IsPlotQueried(); bool IsPlotQueried();
// Returns the current plot query bounds. // Returns the current plot query bounds.
ImPlotLimits GetPlotQuery(int y_axis = IMPLOT_AUTO); 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 // 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. // Returns a null terminated string name for a built-in colormap.
const char* GetColormapName(ImPlotColormap 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 // Miscellaneous
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -534,11 +543,6 @@ void PushPlotClipRect();
// Pop plot clip rect. // Pop plot clip rect.
void PopPlotClipRect(); 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!) // Demo (add implot_demo.cpp to your sources!)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -715,7 +715,7 @@ void ShowDemoWindow(bool* p_open) {
init = false; 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 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(); ImGui::BeginGroup();
if (ImGui::Button("Clear", ImVec2(100, 0))) { if (ImGui::Button("Clear", ImVec2(100, 0))) {
for (int i = 0; i < K_CHANNELS; ++i) { for (int i = 0; i < K_CHANNELS; ++i) {
@ -755,17 +755,21 @@ void ShowDemoWindow(bool* p_open) {
sprintf(label, "data_%d", i); sprintf(label, "data_%d", i);
ImPlot::SetPlotYAxis(yAxis[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)); 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)) { if (ImPlot::BeginLegendDragDropSource(label)) {
ImGui::SetDragDropPayload("DND_PLOT", &i, sizeof(int)); ImGui::SetDragDropPayload("DND_PLOT", &i, sizeof(int));
ImGui::TextUnformatted(label); ImGui::TextUnformatted(label);
ImPlot::EndDragDropSource(); ImPlot::EndLegendDragDropSource();
} }
} }
} }
// make our plot a drag and drop target
if (ImGui::BeginDragDropTarget()) { if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_PLOT")) { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_PLOT")) {
int i = *(int*)payload->Data; int i = *(int*)payload->Data;
show[i] = true; show[i] = true;
yAxis[i] = 0;
// set specific y-axis if hovered
for (int y = 0; y < 3; y++) { for (int y = 0; y < 3; y++) {
if (ImPlot::IsPlotYAxisHovered(y)) if (ImPlot::IsPlotYAxisHovered(y))
yAxis[i] = y; yAxis[i] = y;

View File

@ -301,7 +301,7 @@ struct ImPlotItem
ImVec4 Color; ImVec4 Color;
int NameOffset; int NameOffset;
bool Show; bool Show;
bool Highlight; bool LegendHovered;
bool SeenThisFrame; bool SeenThisFrame;
ImPlotItem() { ImPlotItem() {
@ -310,7 +310,7 @@ struct ImPlotItem
NameOffset = -1; NameOffset = -1;
Show = true; Show = true;
SeenThisFrame = false; SeenThisFrame = false;
Highlight = false; LegendHovered = false;
} }
~ImPlotItem() { ID = 0; } ~ImPlotItem() { ID = 0; }

View File

@ -145,7 +145,7 @@ bool BeginItem(const char* label_id, ImPlotCol recolor_from) {
s.Colors[ImPlotCol_Fill].w *= s.FillAlpha; s.Colors[ImPlotCol_Fill].w *= s.FillAlpha;
// s.Colors[ImPlotCol_MarkerFill].w *= s.FillAlpha; // TODO: this should be separate, if it at all // s.Colors[ImPlotCol_MarkerFill].w *= s.FillAlpha; // TODO: this should be separate, if it at all
// apply highlight mods // apply highlight mods
if (item->Highlight) { if (item->LegendHovered && ImHasFlag(gp.CurrentPlot->Flags, ImPlotFlags_Highlight)) {
s.LineWeight *= 2; s.LineWeight *= 2;
s.MarkerWeight *= 2; s.MarkerWeight *= 2;
// TODO: highlight fills? // TODO: highlight fills?