imgui

框架

ocornut/imgui

无臃肿的 C++ GUI 库,用于快速工具开发。

项目简介

Dear ImGui 是一个即时模式 GUI 库,专为 C++ 设计,强调简洁与高效。适用于游戏引擎、实时 3D 应用及内容创作工具,无需外部依赖,状态同步极小。

README 预览

Dear ImGui\n=====\n\n"Give someone state and they'll have a bug one day, but teach them how to represent state in two separate locations that have to be kept in sync and they'll have bugs for a lifetime." -ryg\n\n----\n\n[](https://github.com/ocornut/imgui/actions?workflow=build) [](https://github.com/ocornut/imgui/actions?workflow=static-analysis) [](https://github.com/ocornut/imgui_test_engine/actions?workflow=tests)\n\n(This library is available under a free and permissive license, but needs financial support to sustain its continued improvements. In addition to maintenance and stability there are many desirable features yet to be added. If your company is using Dear ImGui, please consider reaching out.)\n\nBusinesses: support continued development and maintenance via invoiced sponsoring/support contracts:\n  _E-mail: contact @ dearimgui dot com_\nIndividuals: support continued development and maintenance [here](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WGHNC6MBFLZ2S). Also see [Funding](https://github.com/ocornut/imgui/wiki/Funding) page.\n\n| [The Pitch](#the-pitch) - [Usage](#usage) - [How it works](#how-it-works) - [Releases & Changelogs](#releases--changelogs) - [Demo](#demo) - [Getting Started & Integration](#getting-started--integration) |\n:----------------------------------------------------------: |\n| [Gallery](#gallery) - [Support, FAQ](#support-frequently-asked-questions-faq) -  [How to help](#how-to-help) - **[Funding & Sponsors](https://github.com/ocornut/imgui/wiki/Funding)** - [Credits](#credits) - [License](#license) |\n| [Wiki](https://github.com/ocornut/imgui/wiki) - [Extensions](https://github.com/ocornut/imgui/wiki/Useful-Extensions) - [Language bindings & framework backends](https://github.com/ocornut/imgui/wiki/Bindings) - [Software using Dear ImGui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) - [User quotes](https://github.com/ocornut/imgui/wiki/Quotes) |\n\n### The Pitch\n\nDear I

常见问题 (5)

故障排除
为什么 Dear ImGui 的表格列在添加新列或使用保存的 .ini 设置重启后会变得错乱?

此问题影响 Dear ImGui 1.93.0 WIP 中不可重排的表格。在列被协调或从设置中加载时,过期的 DisplayOrder 值会被保留,导致新旧列排序错误——尤其是对于没有保存列数据的纯 .ini 条目。

请升级到包含提交 68bd43b 的版本,或在 TableFixDisplayOrder() 中应用以下修复:

void ImGui::TableFixDisplayOrder(ImGuiTable* table)
{
    if ((table->Flags & ImGuiTableFlags_Reorderable) == 0)
    {
        for (int n = 0; n < table->ColumnsCount; n++)
            table->DisplayOrderToIndex[n] = table->Columns[n].DisplayOrder = (ImGuiTableColumnIdx)n;
        return;
    }

    ImGuiContext& g = *GImGui;
    // 现有的可重排表格逻辑……
}

对于未设置 ImGuiTableFlags_Reorderable 的表格,列此后将始终按声明/索引顺序显示。列宽和其他逐列数据仍按 ID 匹配。请注意,此更改之后,对非 Reorderable 表格以编程方式进行的重排将不再被保留。

来源 Issue #9570
故障排除
为什么 IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION 无法在 Dear ImGui 中禁用 stb_truetype?

IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION 仅禁用 stb_truetype.h 中可链接的实现内容;它并不会禁用 ImGui 的 stb_truetype 字体后端。请将 Dear ImGui 更新到包含提交 4989b4c 的修订版本,然后将以下内容添加到 imconfig.h 或编译器定义中:

#define IMGUI_DISABLE_STB_TRUETYPE 1

不要定义 IMGUI_ENABLE_STB_TRUETYPE。定义 IMGUI_ENABLE_FREETYPE 也能阻止 stb_truetype 被默认启用,但如果你使用的是自定义字体加载器,则没有必要这样做。

在旧版本上,请修改 imgui_internal.h 中的默认选择代码块:

#if !defined(IMGUI_ENABLE_FREETYPE) && !defined(IMGUI_ENABLE_STB_TRUETYPE) && !defined(IMGUI_DISABLE_STB_TRUETYPE)
#define IMGUI_ENABLE_STB_TRUETYPE
#endif

然后定义 IMGUI_DISABLE_STB_TRUETYPE 并重新构建。

来源 Issue #9573
故障排除
如何修复 Dear ImGui 中 AddRect 轮廓比 AddRectFilled 少 1 个像素的问题?

当使用相同的 p_min/p_max 绘制填充矩形和轮廓描边时,描边的底边在栅格化时可能会比填充矩形的底边高出一个像素。对于默认的 1 像素边框,只需将描边的最大 Y 坐标扩展 1 像素:

draw_list->AddRectFilled(pmin, pmax, fill_color);
draw_list->AddRect(pmin, pmax + ImVec2(0.0f, 1.0f), border_color);

保持 AddRectFilled 的 pmax 不变。这样可以使可见的底边框与填充矩形对齐。AddCircle/AddCircleFilled 不受影响,因为它们使用相同的基于半径的几何体。

来源 Issue #6971
操作指南
如何在 Dear ImGui 中为 MenuItem 添加图标?

推荐的做法是将图标字体(如 FontAwesome)合并到你的字体图集中:在加载图标字体时设置 ImFontConfig::MergeMode = true,然后直接将图标字形嵌入菜单标签文本中(例如 ImGui::MenuItem(ICON_FA_SAVE " Save"))。详细说明请参阅 misc/fonts/README.txt。另一种方式是使用内部的 MenuItemEx() 函数,它可以单独接收一个图标参数。对于基于图像的图标,你可以先调用 SameLine(),再调用 Image((void*)texture_id, ImVec2(16,16)),在菜单项旁边手动渲染纹理,但这需要修改控件或自定义布局代码,而且不如图标字体方案灵活。

来源 Issue #2582
故障排除
为什么在 Dear ImGui 1.93.0 中用平行的 AddLine() 调用填充多边形会看起来发白或颜色被冲淡?

在 ImGui 1.93.0 中,AddLine() 默认使用抗锯齿描边。当通过绘制相邻的平行线来扫描填充某个区域(奇偶行扫描填充)时,半透明的 AA 边缘会相互重叠并混合,使填充看起来发淡。修复方法:传入 ImDrawFlags_StrokeLegacy 以禁用这些描边的 AA 边缘:

draw->AddLine(as, bs, color, 1.0f, ImDrawFlags_StrokeLegacy);

这样可以恢复 1.93 之前的实色描边行为和预期的填充颜色。请注意,这是一个使用上的限制,而不是 bug;对于一般多边形/凹多边形,如果不需要孔洞,可以考虑使用 ImGui 的凸多边形填充 API。

来源 Issue #9585