当前位置: 首页 > news >正文

ImGui 学习笔记(四)—— 实现每窗口背景色

ImGui 的窗口背景仅通过全局的 style 控制,这一点不方便于我们设置特定窗口的背景透明度(一般不用于调整颜色),分析代码,我们可以找到 ImGui::RenderWindowDecorations 函数:

void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, bool handle_borders_and_resize_grips, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size)
{
    ImGuiContext& g = *GImGui;
    ImGuiStyle& style = g.Style;
    ImGuiWindowFlags flags = window->Flags;

    // Ensure that Scrollbar() doesn't read last frame's SkipItems
    // 确保 Scrollbar() 不读取上一帧的 SkipItems
    IM_ASSERT(window->BeginCount == 0);
    window->SkipItems = false;
    window->DC.NavLayerCurrent = ImGuiNavLayer_Menu;

    // Draw window + handle manual resize
    // 绘制窗口 + 手动调整大小
    // As we highlight the title bar when want_focus is set, multiple reappearing windows will have their title bar highlighted on their reappearing frame.
    // 当设置了 want_focus 后,在我们突出显示标题栏时,多个重新出现的窗口将在其重新出现的框架上突出显示其标题栏。
    const float window_rounding = window->WindowRounding;
    const float window_border_size = window->WindowBorderSize;
    if (window->Collapsed)
    {
        // Title bar only
        // 仅标题栏
        const float backup_border_size = style.FrameBorderSize;
        g.Style.FrameBorderSize = window->WindowBorderSize;
        ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && g.NavCursorVisible) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed);
        if (window->ViewportOwned)
            title_bar_col |= IM_COL32_A_MASK; // No alpha (we don't support is_docking_transparent_payload here because simpler and less meaningful, but could with a bit of code shuffle/reuse)
        RenderFrame(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, true, window_rounding);
        g.Style.FrameBorderSize = backup_border_size;
    }
    else
    {
        // Window background
        // 窗口背景
        if (!(flags & ImGuiWindowFlags_NoBackground))
        {
            bool is_docking_transparent_payload = false;
            if (g.DragDropActive && (g.FrameCount - g.DragDropAcceptFrameCount) <= 1 && g.IO.ConfigDockingTransparentPayload)
                if (g.DragDropPayload.IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) && *(ImGuiWindow**)g.DragDropPayload.Data == window)
                    is_docking_transparent_payload = true;

            ImU32 bg_col = GetColorU32(GetWindowBgColorIdx(window));
            if (window->ViewportOwned)
            {
                //bg_col |= IM_COL32_A_MASK; // No alpha
                if (!(g.ConfigFlagsCurrFrame & ImGuiConfigFlags_TransparentBackbuffers)) {
                    bg_col = (bg_col | IM_COL32_A_MASK);
                }
                if (is_docking_transparent_payload)
                    window->Viewport->Alpha *= DOCKING_TRANSPARENT_PAYLOAD_ALPHA;
            }
            else
            {
                // Adjust alpha. For docking
                // 用于 dock 模式下调整 Alpha 
                bool override_alpha = false;
                float alpha = 1.0f;
                if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasBgAlpha)
                {
                    alpha = g.NextWindowData.BgAlphaVal;
                    override_alpha = true;
                }
                if (is_docking_transparent_payload)
                {
                    alpha *= DOCKING_TRANSPARENT_PAYLOAD_ALPHA; // FIXME-DOCK: Should that be an override?
                    override_alpha = true;
                }
                if (override_alpha)
                    bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT);
            }
......
}

这里的 ImU32 bg_col = GetColorU32(GetWindowBgColorIdx(window)); 就是窗口要设置的背景颜色,想办法改成跟窗口 id,name 或者 hash 绑定的数值即可。

相关文章:

  • DQN 玩 2048 实战|第一期!搭建游戏环境(附 PyGame 可视化源码)
  • 洛谷 P2801 教主的魔法 题解
  • Vulkan视频解码decode显示display之同步
  • 贪吃蛇小游戏-简单开发版
  • 【大模型基础_毛玉仁】2.4 基于 Encoder-Decoder 架构的大语言模型
  • AI芯片设计
  • Web3到底解决了什么问题?
  • Android 数据持久化之 SharedPreferences 存储
  • 稳定运行的以Oracle数据库为数据源和目标的ETL性能变差时提高性能方法和步骤
  • 压敏电阻结构特点及选型指南
  • WebRTC中音视频服务质量QoS之RTT衡量网络往返时延的加权平均RTT计算机制‌详解
  • Spring Boot应用首次请求性能优化实战:从数据库连接池到JVM调优
  • 利用 OpenCV 库进行实时目标物体检测
  • 基于Python+SQLite实现(Web)验室设备管理系统
  • 7、基于osg引擎实现读取vtk数据通过着色器实现简单体渲染(1)
  • 服务性能防腐体系:基于自动化压测的熔断机制
  • NPM 常用操作指令大全
  • 网页制作12-html,css,javascript初认识のJavascipt脚本基础
  • 备赛蓝桥杯-Python-Day1-基础语法回顾
  • SWPU 2021 新生赛
  • 诠释微末处的丰盈:“上海制造佳品汇”首届海外专场即将亮相日本大阪
  • 将人工智能送上太空,我国太空计算卫星星座成功发射
  • 马上评|“为偶像正名”的正确做法是什么
  • 跨文化戏剧的拓荒者与传承者——洪深与复旦剧社的百年回响
  • 广东省中医院脾胃病科大科主任张北平病逝,年仅52岁
  • 6连败后再战萨巴伦卡,郑钦文期待打出更稳定发挥