| 664 | } |
| 665 | |
| 666 | bool ShowLegendEntries(ImPlotItemGroup& items, const ImRect& legend_bb, bool hovered, const ImVec2& pad, const ImVec2& spacing, bool vertical, ImDrawList& DrawList) { |
| 667 | // vars |
| 668 | const float txt_ht = ImGui::GetTextLineHeight(); |
| 669 | const float icon_size = txt_ht; |
| 670 | const float icon_shrink = 2; |
| 671 | ImU32 col_txt = GetStyleColorU32(ImPlotCol_LegendText); |
| 672 | ImU32 col_txt_dis = ImAlphaU32(col_txt, 0.25f); |
| 673 | // render each legend item |
| 674 | float sum_label_width = 0; |
| 675 | bool any_item_hovered = false; |
| 676 | |
| 677 | const int num_items = items.GetLegendCount(); |
| 678 | if (num_items < 1) |
| 679 | return hovered; |
| 680 | // build render order |
| 681 | ImPlotContext& gp = *GImPlot; |
| 682 | ImVector<int>& indices = gp.TempInt1; |
| 683 | indices.resize(num_items); |
| 684 | for (int i = 0; i < num_items; ++i) |
| 685 | indices[i] = i; |
| 686 | if (ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_Sort) && num_items > 1) { |
| 687 | gp.SortItems = &items; |
| 688 | qsort(indices.Data, num_items, sizeof(int), LegendSortingComp); |
| 689 | } |
| 690 | // render |
| 691 | for (int i = 0; i < num_items; ++i) { |
| 692 | const int idx = ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_Reverse) ? indices[num_items - 1 - i] : indices[i]; |
| 693 | ImPlotItem* item = items.GetLegendItem(idx); |
| 694 | const char* label = items.GetLegendLabel(idx); |
| 695 | const float label_width = ImGui::CalcTextSize(label, nullptr, true).x; |
| 696 | const ImVec2 top_left = vertical ? |
| 697 | legend_bb.Min + pad + ImVec2(0, i * (txt_ht + spacing.y)) : |
| 698 | legend_bb.Min + pad + ImVec2(i * (icon_size + spacing.x) + sum_label_width, 0); |
| 699 | sum_label_width += label_width; |
| 700 | ImRect icon_bb; |
| 701 | icon_bb.Min = top_left + ImVec2(icon_shrink,icon_shrink); |
| 702 | icon_bb.Max = top_left + ImVec2(icon_size - icon_shrink, icon_size - icon_shrink); |
| 703 | ImRect label_bb; |
| 704 | label_bb.Min = top_left; |
| 705 | label_bb.Max = top_left + ImVec2(label_width + icon_size, icon_size); |
| 706 | ImU32 col_txt_hl; |
| 707 | ImU32 col_item = ImAlphaU32(item->Color,1); |
| 708 | |
| 709 | ImRect button_bb(icon_bb.Min, label_bb.Max); |
| 710 | |
| 711 | ImGui::KeepAliveID(item->ID); |
| 712 | |
| 713 | bool item_hov = false; |
| 714 | bool item_hld = false; |
| 715 | bool item_clk = ImHasFlag(items.Legend.Flags, ImPlotLegendFlags_NoButtons) |
| 716 | ? false |
| 717 | : ImGui::ButtonBehavior(button_bb, item->ID, &item_hov, &item_hld); |
| 718 | |
| 719 | if (item_clk) |
| 720 | item->Show = !item->Show; |
| 721 | |
| 722 | |
| 723 | const bool can_hover = (item_hov) |
no test coverage detected