ImGui 简单使用
  uhAcWl7Ne2Li 2023年11月02日 95 0
C++

1 主窗口

🗡️ 创建主窗口

// 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之间)

  • ImGui::BeginMainMenuBar()是添加菜单栏, 与ImGui::EndMainMenuBar();`成对出现

  • if (ImGui::BeginMenu(u8" 文 件 "))是添加一个选项, 与ImGui::EndMenu();`成对出现

  • 再下一层就是

    • if (ImGui::MenuItem("Open File")) {(在里面写代码)} 相当于添加一个按钮

    • 或者是添加一个可以展开的按钮:

      if (ImGui::BeginMenu(u8"测试3"))
      {
      	if (ImGui::BeginMenu(u8"测试4"))
      	{
      		if (ImGui::Button(u8"测试成功"))
      		{
      			std::cout << "测试成功" << std::endl;
      		}
      		ImGui::EndMenu();
      	}
      	ImGui::EndMenu();
      }
      
//主窗口添加一个菜单
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();
}

2 小窗口

🗡️ 创建第一个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);
    

3 创建一些控件

🗡️ 一行字

ImGui::Text("This is some useful text.");
ImGui::TextWrapped("NB: Cursor & selection are preserved when refocusing last used item in code.");

🗡️ checkbox

第一个参数时名字, 第二个参数是要改变的bool值

image

ImGui::Checkbox("Console Window", &show_console_window);

🗡️ 按钮

第一个参数给它起一个名字, 第二个参数设置按钮的长宽

bool ImGui::Button(const char* label, const ImVec2& size_arg)
{
	return ButtonEx(label, size_arg, ImGuiButtonFlags_None);
}

例:
image

if (ImGui::Button("Button",ImVec2(100, 50)))
{
  //按一下按钮,就运行里面这些东西
}ImGui::SameLine();	//表示下个东西跟这个东西在同一行

注意: 如果 label 同名, 后面的那个按钮会被屏蔽

🗡️ 一些输入的组件

  • 输入double等

image

ImGui::SetNextItemWidth(60);	//设置下一个控件的宽度
static double d_edge_length = 9.01;
ImGui::InputDouble("input double", &d_edge_length, 0.01f, 1.0f, "%.8f");
  • 输入字符串

image

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;

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

🗡️ 折叠组件

image

if (ImGui::TreeNode("file path"))
{
	//这里写折叠组件中的东西
	...
	//结束
	ImGui::TreePop();
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  8Tw5Riv1mGFK   2024年05月01日   80   0   0 C++
  BYaHC1OPAeY4   2024年05月08日   58   0   0 C++
  yZdUbUDB8h5t   2024年05月05日   44   0   0 C++
  oXKBKZoQY2lx   2024年05月17日   58   0   0 C++
uhAcWl7Ne2Li
作者其他文章 更多