ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
61.07k stars 10.29k forks source link

Add text_baseline_y to ImGui::Dummy #4549

Open codecat opened 3 years ago

codecat commented 3 years ago

Version/Branch of Dear ImGui:

Version: 1.84.1 Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_win32.cpp + imgui_impl_dx11.cpp Operating System: Windows

My Issue/Question:

For custom controls, it would be useful to have access to the baseline offset from ImGui::Dummy which would be passed down to ItemSize.

https://github.com/ocornut/imgui/blob/2d0a6a4969b4da5eae0704e740e1ae0e11b946d9/imgui_widgets.cpp#L1319-L1328

Screenshots/Video

My custom control is a simple tag, which is a colored background around around some text. In the below screenshots, note the test text is an ImGui::Text after ImGui::SameLine.

Without baseline offset: image

With baseline offset: image

Standalone, minimal, complete and verifiable example: (see https://github.com/ocornut/imgui/issues/2261)

ImGui::Begin("Example Bug");
ImGui::Dummy(ImVec2(32, 32));
ImGui::SameLine();
ImGui::Text("Hello");
ImGui::End();
codecat commented 3 years ago

As a workaround, I've included imgui_internal.h and wrote my own function:

static void ScriptUI_DummyEx(const glm::vec2& size, float text_baseline_y)
{
    //NOTE: Same as ImGui::Dummy, but with the added baseline parameter

    ImGuiWindow* window = ImGui::GetCurrentWindow();
    if (window->SkipItems) {
        return;
    }

    const ImRect bb(window->DC.CursorPos, ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y + size.y));
    ImGui::ItemSize(ImVec2(size.x, size.y), text_baseline_y);
    ImGui::ItemAdd(bb, 0);
}
ocornut commented 3 years ago

Hello, For now I suggest you keep using your helper DummyEx(). I'll like to keep this open as I'm been unhappy with the amount of issues caused by text baseline features and I'd like to consider a better overall solution (maybe have baseline as a style var).