Skip to content

how to render in background? #4588

Closed
Closed

Description

@gucio321

Version/Branch of Dear ImGui:

Version: master
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_sdl.cpp + imgui_impl_opengl3.cpp
Operating System: Fedora 34

My Issue/Question:

Hi,
I'm going to measure widget width (#3714) and align it.
I'm able to do so rendering one widget two times with the following steps:

  • save cursor position
  • push temporaty ID
  • render widget (with 0 alpha)
  • save its size
  • pop id
  • set cursor position to x = (availableRegionW - itemWidth), y = start pos.Y
  • render widget

the problem is, that this first (invisible) widget is rendered in foreground?... and covers this second (visible).
The solution is to put the visible widget inside of Child.
my question is: is it possible to do it another way so that the second widget could be rendered in a normal way (without any other steps before)

Screenshots/Video

with steps above (without Push..Alpha to show both buttons):
image
when mouse over the common region of both buttons
image
with child (mouse over common region of two buttons)
image

Standalone, minimal, complete and verifiable example: (see #2261)

// Here's some code anyone can copy and paste to reproduce your issue
        {
            ImGui::Begin("Hello, world!");

            struct foo {
                    bool btn() { return ImGui::Button("btn",ImVec2{200,20}); };
            } btn;

            ImVec2 start = ImGui::GetCursorPos();
            ImVec2 size;

            ImGui::PushID("someotherid");
            ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0);
            if (btn.btn() ) {
                    std::cout << "btn 1 (invisible) pressed\n";
            };
            ImGui::PopStyleVar();
            ImGui::PopID();

            size = ImGui::GetItemRectSize();
            ImVec2 available = ImGui::GetContentRegionMax();
            start.x = (available.x-size.x)/2;
            ImGui::SetCursorPos(start);

            if (ImGui::BeginChild("container")) {
                if (btn.btn()){
                    std::cout << "btn 1 pressed\n";
                }
            }
            ImGui::EndChild();

            ImGui::End();
        }


Activity

  1. ocornut commented on Sep 29, 2021

    @ocornut
    Owner

    This scream like XY Problem please start by describing what you are trying to do in the first place.

    It also doesn't make sense to do any of that if your button has a fixed size of (200,20) so your code is not adequately representing your problem. The solution is likely that you should be able to tell the compute of a widget yourself given they are pretty standardized.

  2. gucio321 commented on Sep 29, 2021

    @gucio321
    Author

    @ocornut thanx for your reply!
    I'm going to add a common method for aligning widgets (in fact for getting its widths).
    I'm reffering to this comment: #3714 (comment)

  3. gucio321 commented on Sep 29, 2021

    @gucio321
    Author

    well, so as I can read:

    • If there are other solutions you've already ruled out, share why you've ruled them out. This gives more information about your requirements.
    • just render widget with 0 alpha in default position, get its size, move cursor back, add dummy, render centered widget.
      the problem was, that the second widget (e.g. button) (both widgets has the same id) wasn't return true on user interaction. Instead, user was able to interact with this invisible widget
    • well, so lets try to PushID for this invisible widget, but now, it seems that the first widget convers the visible so that the visible one is unable to be interacted.

    the solution I really need is to render my first widget behind other imgui widgets to be able to safely measure it.

  4. ocornut commented on Sep 30, 2021

    @ocornut
    Owner

    That's not really possible (other than output the "first" widget in a dummy window then hide the window...).
    What we are missing a dummy layout mode where ItemSize() would record size and ItemAdd() always return false. This is desirable for many other features and we'll do it eventually (It's one of the last thing missing to allow #846 to be an independent extension).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址