// ID conflict. Expected behaviour.
static float value1;
static float value2;
ImGui::DragFloat("##drag-float", &value1); // .../##drag-float
ImGui::DragFloat("##drag-float", &value2); // .../##drag-float
如果其中一个 DragFloat 发生更改,另一个 DragFloat 将得到更新,这是预期的,因为它们在运行时共享相同的 id(在评论中)
但是,我希望以下代码具有相同的行为。但事实并非如此。
// No ID conflict. Unexpected behaviour.
static float table_value1;
static float table_value2;
if (ImGui::BeginTable("table", 1)) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::DragFloat("##drag-float", &table_value1); // .../table/##drag-float
ImGui::EndTable();
}
if (ImGui::BeginTable("table", 1)) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::DragFloat("##drag-float", &table_value2); // .../table/0x8CD5DC7A [override]/##drag-float
ImGui::EndTable();
}
在第二种情况下,ImGui 在表 id 和 DragFloat id 之间添加 0x8CD5DC7A [覆盖]。
我不想要这种行为的原因是因为我想创建自己的包装器小部件,它将利用 ImGui Tables API,并且我还想支持“标签”行为作为 ImGui 小部件(即,如果有“ ##”,则显示标签是其之前的子字符串,id 是整个内容。如果标签中有“###”,则显示标签是其之前的子字符串,id 是其之后的子字符串)。
所以,当我像这样创建自定义小部件时:
void WrapperWidget(const std::string& label, float& value) {
std::pair<std::string, std::string> label_id_pair = extractLabelAndId(label);
ImGui::PushID(label_id_pair.second.c_str());
ImGui::Text(label_id_pair.first.c_str());
ImGui::SameLine();
if (ImGui::BeginTable("table", 1)) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::DragFloat("##drag-float", &value); // .../table/0x8CD5DC7A [override]/##drag-float
ImGui::EndTable();
}
ImGui::PopID();
}
static float value1;
static float value2;
WrapperWidget("WrapDrag1###ID", value1);
WrapperWidget("WrapDrag2###ID", value2); // Supposed to have the same id as the previous one.
我希望它们在共享相同 ID 时相互冲突,就像本机 ImGui 小部件一样。我如何实现这种行为?
我也遇到过这个问题,我希望两个输入相同并且存在 ID 冲突。两者也在桌子内。
深入观察,它与 ImGui 的
instance_no
函数中的 bool ImGui::BeginTableEx
有关。我还没有找到解决你的问题的方法,我只是不使用表格来解决我的问题。