Skip to content

Commit

Permalink
Add BeginPopupContextLegend and EndPopup
Browse files Browse the repository at this point in the history
EndPopup is just a wrapper around ImGui::EndPopup.

BeginPopupContextLegend provides easy-to-use context menus for legend
entries along the lines of ImGui::BeginPopupContextItem.
  • Loading branch information
PeterJohnson committed Sep 1, 2020
1 parent 92f7879 commit 9494307
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
19 changes: 19 additions & 0 deletions implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,25 @@ bool IsLegendEntryHovered(const char* label_id) {
return false;
}

bool BeginPopupContextLegend(const char* label_id, ImGuiMouseButton mouse_button) {
ImPlotContext& gp = *GImPlot;
IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginLegendPopup() needs to be called between BeginPlot() and EndPlot()!");
ImGuiWindow* window = GImGui->CurrentWindow;
if (window->SkipItems)
return false;
ImGuiID id = ImGui::GetID(label_id);
if (ImGui::IsMouseReleased(mouse_button)) {
ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id);
if (item && item->Highlight)
ImGui::OpenPopupEx(id);
}
return ImGui::BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings);
}

void EndPopup() {
ImGui::EndPopup();
}

//-----------------------------------------------------------------------------
// STYLING
//-----------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions implot.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ void PushPlotClipRect();
// Pop plot clip rect.
void PopPlotClipRect();

// Begin a popup for a legend entry.
bool BeginPopupContextLegend(const char* label_id, ImGuiMouseButton mouse_button = 1);
// End a popup for a legend entry.
void EndPopup();

//-----------------------------------------------------------------------------
// Demo (add implot_demo.cpp to your sources!)
//-----------------------------------------------------------------------------
Expand Down
11 changes: 10 additions & 1 deletion implot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ void ShowDemoWindow(bool* p_open) {
}
static float weight = ImPlot::GetStyle().LineWeight;
ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help).");
ImGui::BulletText("Right click on a legend item to bring up its context menu");
ImGui::DragFloat("Line Weight", &weight, 0.05f, 1.0f, 5.0f, "%.2f px");

if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) {
Expand All @@ -246,6 +247,14 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PushStyleVar(ImPlotStyleVar_Marker, ImPlotMarker_Circle);
ImPlot::PlotLine("x^2", xs2, ys2, 11);
ImPlot::PopStyleVar(2);
if (ImPlot::BeginPopupContextLegend("sin(x)")) {
ImGui::Text("Context menu for sin(x)");
ImPlot::EndPopup();
}
if (ImPlot::BeginPopupContextLegend("x^2")) {
ImGui::Text("Context menu for x^2");
ImPlot::EndPopup();
}
ImPlot::EndPlot();
}
}
Expand Down Expand Up @@ -1354,4 +1363,4 @@ void ShowBenchmarkTool() {
}
}

}
}

0 comments on commit 9494307

Please sign in to comment.