Skip to content

Commit 98631d6

Browse files
committed
fix: duplicated item legend entry
1 parent 1143db7 commit 98631d6

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

implot3d.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,10 @@ void EndPlot() {
13641364
// Reset current plot
13651365
gp.CurrentPlot = nullptr;
13661366
gp.CurrentItems = nullptr;
1367+
1368+
// Reset the plot items for the next frame
1369+
for (int i = 0; i < plot.Items.GetItemCount(); i++)
1370+
plot.Items.GetItemByIndex(i)->SeenThisFrame = false;
13671371
}
13681372

13691373
//-----------------------------------------------------------------------------

implot3d_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,15 @@ struct ImPlot3DItem {
287287
int NameOffset;
288288
bool Show;
289289
bool LegendHovered;
290+
bool SeenThisFrame;
290291

291292
ImPlot3DItem() {
292293
ID = 0;
293294
Color = IM_COL32_WHITE;
294295
NameOffset = -1;
295296
Show = true;
296297
LegendHovered = false;
298+
SeenThisFrame = false;
297299
}
298300
~ImPlot3DItem() { ID = 0; }
299301
};

implot3d_items.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ ImPlot3DItem* RegisterOrGetItem(const char* label_id, ImPlot3DItemFlags flags, b
231231
if (just_created != nullptr)
232232
*just_created = Items.GetItem(id) == nullptr;
233233
ImPlot3DItem* item = Items.GetOrAddItem(id);
234+
235+
// Avoid re-adding the same item to the legend (the legend is reset every frame)
236+
if (item->SeenThisFrame)
237+
return item;
238+
item->SeenThisFrame = true;
239+
240+
// Add item to the legend
234241
int idx = Items.GetItemIndex(item);
235242
item->ID = id;
236243
if (!ImHasFlag(flags, ImPlot3DItemFlags_NoLegend) && ImGui::FindRenderedTextEnd(label_id, nullptr) != label_id) {

0 commit comments

Comments
 (0)