Skip to content

Commit 23af3e2

Browse files
committed
fix: 防止 ImGui 中 ID 冲突
1 parent 07e143b commit 23af3e2

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

‎src/Magpie.Core/OverlayDrawer.cpp‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ static std::string_view GetEffectDisplayName(const Renderer::EffectInfo* effectI
546546
}
547547

548548
bool OverlayDrawer::_DrawTimingItem(
549+
int& itemId,
549550
const char* text,
550551
const ImColor* color,
551552
float time,
@@ -567,6 +568,8 @@ bool OverlayDrawer::_DrawTimingItem(
567568

568569
const float fontHeight = ImGui::GetFont()->FontSize;
569570

571+
ImGui::PushID(itemId++);
572+
570573
bool isHovered = false;
571574
if (color) {
572575
ImGui::Selectable("", false, 0, ImVec2(0, descHeight));
@@ -617,11 +620,14 @@ bool OverlayDrawer::_DrawTimingItem(
617620
ImGui::PopStyleColor();
618621
}
619622

623+
ImGui::PopID();
624+
620625
return isHovered;
621626
}
622627

623628
// 返回鼠标悬停的项的序号,未悬停于任何项返回 -1
624629
int OverlayDrawer::_DrawEffectTimings(
630+
int& itemId,
625631
const _EffectDrawInfo& drawInfo,
626632
bool showPasses,
627633
std::span<const ImColor> colors,
@@ -631,6 +637,7 @@ int OverlayDrawer::_DrawEffectTimings(
631637

632638
showPasses &= drawInfo.passTimings.size() > 1;
633639
if (_DrawTimingItem(
640+
itemId,
634641
std::string(GetEffectDisplayName(drawInfo.info)).c_str(),
635642
(!singleEffect && !showPasses) ? &colors[0] : nullptr,
636643
drawInfo.totalTime,
@@ -644,6 +651,7 @@ int OverlayDrawer::_DrawEffectTimings(
644651
ImGui::Indent(16);
645652

646653
if (_DrawTimingItem(
654+
itemId,
647655
drawInfo.info->passNames[j].c_str(),
648656
&colors[j],
649657
drawInfo.passTimings[j]
@@ -659,13 +667,16 @@ int OverlayDrawer::_DrawEffectTimings(
659667
}
660668

661669
void OverlayDrawer::_DrawTimelineItem(
670+
int& itemId,
662671
ImU32 color,
663672
float dpiScale,
664673
std::string_view name,
665674
float time,
666675
float effectsTotalTime,
667676
bool selected
668677
) {
678+
ImGui::PushID(itemId++);
679+
669680
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, color);
670681
ImGui::PushStyleColor(ImGuiCol_HeaderActive, color);
671682
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, color);
@@ -698,6 +709,8 @@ void OverlayDrawer::_DrawTimelineItem(
698709
ImGui::SetCursorPosY(ImGui::GetCursorPosY() - 0.5f * _dpiScale);
699710
ImGui::TextUnformatted(text.c_str());
700711
}
712+
713+
ImGui::PopID();
701714
}
702715

703716
void OverlayDrawer::_DrawFPS(uint32_t fps) noexcept {
@@ -957,6 +970,8 @@ bool OverlayDrawer::_DrawUI(const SmallVector<float>& effectTimings, uint32_t fp
957970
}
958971

959972
static int selectedIdx = -1;
973+
// 防止 ID 冲突
974+
int itemId = 0;
960975

961976
if (nEffect > 1 || showPasses) {
962977
ImGui::Spacing();
@@ -1003,7 +1018,7 @@ bool OverlayDrawer::_DrawUI(const SmallVector<float>& effectTimings, uint32_t fp
10031018
);
10041019
}
10051020

1006-
_DrawTimelineItem(colors[i], _dpiScale, name, drawInfo.passTimings[j],
1021+
_DrawTimelineItem(itemId, colors[i], _dpiScale, name, drawInfo.passTimings[j],
10071022
effectsTotalTime, selectedIdx == (int)i);
10081023

10091024
++i;
@@ -1036,6 +1051,7 @@ bool OverlayDrawer::_DrawUI(const SmallVector<float>& effectTimings, uint32_t fp
10361051

10371052
ImGui::TableNextColumn();
10381053
_DrawTimelineItem(
1054+
itemId,
10391055
colors[i],
10401056
_dpiScale,
10411057
GetEffectDisplayName(drawInfo.info),
@@ -1076,25 +1092,25 @@ bool OverlayDrawer::_DrawUI(const SmallVector<float>& effectTimings, uint32_t fp
10761092
ImGui::TableSetupColumn(nullptr, ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder);
10771093

10781094
if (nEffect == 1) {
1079-
int hovered = _DrawEffectTimings(effectDrawInfos[0], showPasses, colors, true);
1095+
int hovered = _DrawEffectTimings(itemId, effectDrawInfos[0], showPasses, colors, true);
10801096
if (hovered >= 0) {
10811097
selectedIdx = hovered;
10821098
}
10831099
} else {
1084-
size_t idx = 0;
1100+
int idx = 0;
10851101
for (const _EffectDrawInfo& effectInfo : effectDrawInfos) {
1086-
int idxBegin = (int)idx;
1102+
int idxBegin = idx;
10871103

10881104
std::span<const ImColor> colorSpan;
10891105
if (!showPasses || effectInfo.passTimings.size() == 1) {
10901106
colorSpan = std::span(colors.begin() + idx, colors.begin() + idx + 1);
10911107
++idx;
10921108
} else {
10931109
colorSpan = std::span(colors.begin() + idx, colors.begin() + idx + effectInfo.passTimings.size());
1094-
idx += effectInfo.passTimings.size();
1110+
idx += (int)effectInfo.passTimings.size();
10951111
}
10961112

1097-
int hovered = _DrawEffectTimings(effectInfo, showPasses, colorSpan, false);
1113+
int hovered = _DrawEffectTimings(itemId, effectInfo, showPasses, colorSpan, false);
10981114
if (hovered >= 0) {
10991115
selectedIdx = idxBegin + hovered;
11001116
}
@@ -1110,7 +1126,7 @@ bool OverlayDrawer::_DrawUI(const SmallVector<float>& effectTimings, uint32_t fp
11101126
if (ImGui::BeginTable("total", 1, ImGuiTableFlags_PadOuterX)) {
11111127
ImGui::TableSetupColumn(nullptr, ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder);
11121128

1113-
_DrawTimingItem(_GetResourceString(L"Overlay_Profiler_Timings_Total").c_str(), nullptr, effectsTotalTime);
1129+
_DrawTimingItem(itemId, _GetResourceString(L"Overlay_Profiler_Timings_Total").c_str(), nullptr, effectsTotalTime);
11141130

11151131
ImGui::EndTable();
11161132
}

‎src/Magpie.Core/OverlayDrawer.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,30 @@ class OverlayDrawer {
4343
};
4444

4545
bool _DrawTimingItem(
46+
int& itemId,
4647
const char* text,
4748
const ImColor* color,
4849
float time,
4950
bool isExpanded = false
5051
) const noexcept;
5152

5253
int _DrawEffectTimings(
54+
int& itemId,
5355
const _EffectDrawInfo& drawInfo,
5456
bool showPasses,
5557
std::span<const ImColor> colors,
5658
bool singleEffect
5759
) const noexcept;
5860

59-
void _DrawTimelineItem(ImU32 color, float dpiScale, std::string_view name, float time, float effectsTotalTime, bool selected = false);
61+
void _DrawTimelineItem(
62+
int& itemId,
63+
ImU32 color,
64+
float dpiScale,
65+
std::string_view name,
66+
float time,
67+
float effectsTotalTime,
68+
bool selected = false
69+
);
6070

6171
void _DrawFPS(uint32_t fps) noexcept;
6272

0 commit comments

Comments
 (0)