三、ImGui如何在一行中显示多个按钮或图片

按钮

//void ImGui::SameLine(float offset_from_start_x, float spacing_w)
// 传第一个参赛,元素存在定位效果
// 第二个参数是左边距
// Gets back to previous line and continue with horizontal layout
//      offset_from_start_x == 0 : follow right after previous item
//      offset_from_start_x != 0 : align to specified x position (relative to window/group left)
//      spacing_w < 0            : use default spacing if pos_x == 0, no spacing if pos_x != 0
//      spacing_w >= 0           : enforce spacing amount



const ImGuiViewport *main_viewport = ImGui::GetMainViewport();
// 设置padding为0
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
 if (ImGui::Begin("demo", p_open, window_flags)) {
    ImGui::PopStyleVar(1);
    // 1.设置第二个参数,通过设置第二个参数,可以直接设置出第二个按钮与第一个按钮的位置 
    ImGui::Button("按钮", ImVec2(80, 20));
    ImGui::SameLine(0, 2.0f);
    ImGui::Button("按钮", ImVec2(80, 20));
     
    // 2. 设置第一个参数,第一个参数的82.0f与第二个参数的2.0f一样
    // 原因是,全局设置了0.0f的边距 + 80.0f的按钮宽度 + 2.0f的距离
    ImGui::Button("按钮", ImVec2(80, 20));
    ImGui::SameLine(82.0f, 0.0f);
    ImGui::Button("按钮", ImVec2(80, 20));
    
    // 3. 两端对齐 162 = 第一个button 80 + 边距 2 + 第二个button
    ImGui::Button("按钮", ImVec2(80, 20));
    ImGui::SameLine(82.0f, main_viewport->WorkSize.x - 162);
    ImGui::Button("按钮", ImVec2(80, 20));
    ImGui::End();

     //4. 两端对齐
    ImGui::Button("按钮", ImVec2(80, 20));
    ImGui::SameLine(0, 2.0f);
    ImGui::Button("按钮", ImVec2(80, 20));

    ImGui::SameLine(164.0f, main_viewport->WorkSize.x - 244);
    ImGui::Button("按钮", ImVec2(80, 20));
     //5 如果使用两端对齐, SameLine的第一个参数传左边的总宽度, 第二个参数传右边加左边的总宽度;
 }

图片

 ofTexture image;
	ofxImGui::Gui gui;
	int btnNum;
	bool flag;
	GLuint img2;
	ofTexture image2;
	ofxImGui::Gui gui2;
};
img1 = gui.loadTexture(image, "ground.jpg"); // 导入图片
        ImVec2 buttonSize(108, 24);                  // 设置图片宽高
        ImTextureID textureID = (ImTextureID)img1;
        ImGui::ImageButton(textureID, buttonSize, ImVec2(0, 0), ImVec2(1, 1), 0); // 设置一个图片按钮,不支持圆角
        bool pressed = ImGui::IsItemClicked();                                    // 定义是否点击的变量
        if (pressed)
        { // 点击事件
            cout << "成功" << endl;
            flag = !flag;
            // onImgButtonClick(this);
        }
        ImGui::SameLine(0, 2.0f);
        if (flag)
        {
            img2 = gui2.loadTexture(image2, "girl.jpg");
            ImTextureID textureID2 = (ImTextureID)img2;
            ImVec4 buttonColor2(1.0f, 1.0f, 1.0f, 0.5f);                                    // 按钮颜色,opacity透明度
            ImGui::ImageButton(textureID2, ImVec2(108, 24), ImVec2(0, 0), ImVec2(1, 1), 0); // 设置一个图片按钮,不支持圆角
            ImGui::Button("悬浮按钮", ImVec2(108, 24));
        }
        else
        {
            img2 = gui2.loadTexture(image2, "ground.jpg");
            ImTextureID textureID2 = (ImTextureID)img2;
            ImVec4 buttonColor2(1.0f, 1.0f, 1.0f, 0.5f);                                    // 按钮颜色,opacity透明度
            ImGui::ImageButton(textureID2, ImVec2(108, 24), ImVec2(0, 0), ImVec2(1, 1), 0); // 设置一个图片按钮,不支持圆角
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值