ocornut / imgui

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

API: support `Get/SetCursorScreenPosX/Y` as well? #8086

Open achabense opened 1 month ago

achabense commented 1 month ago

Version/Branch of Dear ImGui:

Version 1.91.2

Back-ends:

imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

No response

Details:

Sometimes I will do:

const float cursor_pos = GetCursorPosX();
...
SetCursorPosX(cursor_pos);

I hope to replace them with the screen-pos version, as they will go through unnecessary +- window-pos and window-scroll, and GetCursorPos is said "not the best friend". However, there are no SetCursorScreenPosX/Y counterparts, and it's inconvenient to use SetCursorScreenPos directly as I don't want to care about y-pos.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

static void example() {
    if (ImGui::Begin("Example", 0, ImGuiWindowFlags_NoSavedSettings)) {
        for (int i = 0; i < 10; ++i) {
            ImGui::Text("%d", i);
            ImGui::SameLine();

            // (Initially tried this when I tried to simplify a complex conditional Begin/EndGroup.)
            ImGui::PushID(i);
            const float cursor_pos = ImGui::GetCursorPosX(); // -> ImGui::GetCursorScreenPosX
            ImGui::Button("AAAAA");
            ImGui::SetCursorPosX(cursor_pos); // -> ImGui::SetCursorScreenPosX
            ImGui::Button("BBBBB");
            ImGui::PopID();
        }
    }
    ImGui::End();
}
ocornut commented 1 month ago

I have been reluctant to add them because in my experience, almost everytime people use the X/Y it is to add an offset, and therefore the GetCursorPosX() versions are perfectly fine.

However, it does confuse the user and in the name of trying to get people to do the mental switch I should probably add them.