ImGui 简单使用

发布时间 2023-04-12 17:20:24作者: 无形深空

主窗口

? 创建主窗口

// Create window with graphics context
GLFWwindow* window = glfwCreateWindow(1280, 720, "Tet_Studio", NULL, NULL);
if (window == NULL)
    return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync 开启垂直同步

? 设置窗口风格

ImGui::StyleColorsClassic();

? 设置窗口背景颜色

  • 这是设置颜色的 ImVec4

    // ImVec4: 4D vector used to store clipping rectangles, colors etc. [Compile-time configurable type]
    struct ImVec4
    {
        float                                                     x, y, z, w;
        constexpr ImVec4()                                        : x(0.0f), y(0.0f), z(0.0f), w(0.0f) { }
        constexpr ImVec4(float _x, float _y, float _z, float _w)  : x(_x), y(_y), z(_z), w(_w) { }
    };
    
  • RGB转换为ImVec4

    //RGB转化为ImVec4
    ImVec4 RGBAtoIV4(int r, int g, int b, int a) {
    	float newr = r / 255;
    	float newg = g / 255;
    	float newb = b / 255;
    	float newa = a;
    	return ImVec4(newr, newg, newb, newa);
    }
    
    ImVec4 clear_color = RGBAtoIV4(115, 115, 151, 1);			//RGB
    

? 主窗口添加一个菜单
(写在 ImGui::NewFrameImGui::Render之间)

//主窗口添加一个菜单
if (ImGui::BeginMainMenuBar())
{
    if (ImGui::BeginMenu("File"))
    {
        if (ImGui::MenuItem("Open File")) {}
        ImGui::EndMenu();
    }
    if (ImGui::BeginMenu("Edit"))
    {
        if (ImGui::MenuItem("Undo", "CTRL+Z")) {}
        if (ImGui::MenuItem("Redo", "CTRL+Y", false, false)) {}  // Disabled item
        ImGui::Separator();	//----------------(分隔)
        if (ImGui::MenuItem("Cut", "CTRL+X")) {}
        if (ImGui::MenuItem("Copy", "CTRL+C")) {}
        if (ImGui::MenuItem("Paste", "CTRL+V")) {}
        ImGui::EndMenu();
    }
    ImGui::EndMainMenuBar();
}

小窗口

? 创建第一个ImGui Window

  • 把显示窗口的函数放在 ImGui::NewFrameImGui::Render之间

  • 使用一对begin - end创建窗口

    if(ImGui::Begin("My First Window")){
        //其余东西写在begin和end之间
    	ImGui::Text("Hello Dear ImGui!");
    }
    ImGui::End();
    
  • 第一个变量是名字, 第二个变量填一个窗口的状态bool(对应右上角的关闭)

    ImGui::Begin("Control Panel", &show_control_panel_window);
    

? 创建一些小东西

  • 一行字

    ImGui::Text("This is some useful text.");
    ImGui::TextWrapped("NB: Cursor & selection are preserved when refocusing last used item in code.");
    
  • 一个checkbox 按钮 第一个参数时名字, 第二个参数是要改变的bool值

    ImGui::Checkbox("Console Window", &show_console_window);
    
  • 一个按钮 按一下就返回的布尔值

    if (ImGui::Button("Button"))
    {
    	...
    }
    
  • 表示下个东西跟这个东西在同一行

    ImGui::SameLine();
    

? 一些输入的组件

  • 输入double等

    static double d0 = 999999.00000001;
    ImGui::InputDouble("input double", &d0, 0.01f, 1.0f, "%.8f");
    
  • 输入字符串

    static char buf[128] = "../../data/exp_11/bunny";
    ImGui::InputText("file path(node to generate mtr)", buf, IM_ARRAYSIZE(buf));
    //转型
    string test_pre_file = buf;
    

    这个为什么没用引用传值? 测试是能够改变参数的, 这是如何做到的?

? 折叠组件

if (ImGui::TreeNode("file path"))
{
	//这里写折叠组件中的东西
	...
	//结束
	ImGui::TreePop();
}