19 const std::string& plotName,
23 bool otherHoverTooltipsShown,
24 const std::function<
void(
size_t)>& showTooltipCallback)
26 bool tooltipShown =
false;
28 if (ImPlot::IsPlotHovered() && ImGui::IsKeyDown(ImGuiKey_ModCtrl))
30 if (
const auto* item = ImPlot::GetCurrentPlot()->Items.GetItem(plotName.c_str());
33 constexpr double HOVER_PIXEL_SIZE = 5.0;
34 auto limits = ImPlot::GetPlotLimits(IMPLOT_AUTO, axis);
36 ImVec2 scaling = ImVec2(
static_cast<float>(HOVER_PIXEL_SIZE * (limits.X.Max - limits.X.Min) / ImPlot::GetCurrentPlot()->PlotRect.GetWidth()),
37 static_cast<float>(HOVER_PIXEL_SIZE * (limits.Y.Max - limits.Y.Min) / ImPlot::GetCurrentPlot()->PlotRect.GetHeight()));
38 ImPlotPoint mouse = ImPlot::GetPlotMousePos();
40 for (
size_t i = 0; i < yData.
size(); i++)
42 if (std::abs(mouse.x - xData.
at(i)) < scaling.x
43 && std::abs(mouse.y - yData.
at(i)) < scaling.y)
45 ImGui::PushFont(Application::MonoFont());
46 ImGui::BeginTooltip();
47 if (otherHoverTooltipsShown)
49 ImGui::PushStyleColor(ImGuiCol_Separator, ImGui::GetStyleColorVec4(ImGuiCol_SeparatorActive));
52 ImGui::PopStyleColor();
55 if (showTooltipCallback) { showTooltipCallback(i); }
61 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left))
63 if (std::ranges::find_if(tooltips, [&](
const auto&
tooltip) {
68 auto mousePos = ImGui::GetMousePos();
70 tooltips.emplace_back(plotItemIdx, i, mousePos);
83 const std::string& plotName,
84 const std::string& uid,
85 const std::vector<int*>& parentWindows,
86 const std::function<
InsTime(
size_t)>& getInsTime,
87 const std::function<
void(
size_t,
const char*)>& showTooltipCallback)
89 std::vector<size_t> tooltipsToClose;
90 for (
size_t tipIdx = 0; tipIdx < tooltips.size(); tipIdx++)
92 auto&
tooltip = tooltips.at(tipIdx);
93 if (
tooltip.plotItemIdx == plotItemIdx)
95 auto tooltipTime = getInsTime(
tooltip.dataIdx).toYMDHMS(
GPST);
96 auto windowName = fmt::format(
"{} - {}", plotName, tooltipTime);
97 auto tooltipUID = fmt::format(
"{} {}", uid,
tooltip.dataIdx);
99 auto windowUniqueTitle = fmt::format(
"{}##{}", windowName, tooltipUID);
101 for (
size_t i = 0; i < tooltips.size(); i++)
103 if (i == tipIdx) {
continue; }
104 const auto&
tooltip = tooltips.at(i);
105 auto uniqueTitle = fmt::format(
"{} - {}##{} {}",
107 if (uniqueTitle == windowUniqueTitle)
109 tooltipsToClose.push_back(tipIdx);
113 if (!tooltipsToClose.empty() && tooltipsToClose.back() == tipIdx) {
break; }
118 ImGui::SetNextWindowPos(*
tooltip.startPos);
121 ImGui::PushFont(Application::MonoFont());
122 ImGui::Begin(windowUniqueTitle.c_str(), &open,
123 ImGuiWindowFlags_AlwaysAutoResize);
125 for (
int* parentWindow : parentWindows)
127 if (ImGuiWindow* tooltipWindow = ImGui::GetCurrentWindow();
128 ImGui::IsWindowAbove(
reinterpret_cast<ImGuiWindow*
>(parentWindow),
131 ImGui::BringWindowToDisplayFront(tooltipWindow);
135 if (ImGui::GetWindowSize().x < ImGui::CalcTextSize(windowName.c_str()).x + 50.0F)
137 ImGui::TextUnformatted(fmt::format(
"{}", tooltipTime).c_str());
141 if (showTooltipCallback) { showTooltipCallback(
tooltip.dataIdx, tooltipUID.c_str()); }
145 if (!open) { tooltipsToClose.push_back(tipIdx); }
148 for (
const size_t tipIdx : tooltipsToClose)
150 tooltips.erase(std::next(tooltips.begin(),
static_cast<int64_t
>(tipIdx)));
void ShowPlotTooltipWindows(std::vector< PlotTooltip > &tooltips, size_t plotItemIdx, const std::string &plotName, const std::string &uid, const std::vector< int * > &parentWindows, const std::function< InsTime(size_t)> &getInsTime, const std::function< void(size_t, const char *)> &showTooltipCallback)
Shows all tooltip windows in the vector.
bool ShowPlotTooltip(std::vector< PlotTooltip > &tooltips, size_t plotItemIdx, const std::string &plotName, ImAxis axis, const ScrollingBuffer< double > &xData, const ScrollingBuffer< double > &yData, bool otherHoverTooltipsShown, const std::function< void(size_t)> &showTooltipCallback)
Shows a tooltip if the plot is hovered.