Skip to content

Make a movable windows that follows the viewer transforms #1604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jdumas opened this issue Feb 9, 2018 · 4 comments
Closed

Make a movable windows that follows the viewer transforms #1604

jdumas opened this issue Feb 9, 2018 · 4 comments

Comments

@jdumas
Copy link
Contributor

jdumas commented Feb 9, 2018

Hi,

I am trying to draw a window that follows an object in a 2D viewer. I want the window to follow my object when I pan/zoom the viewer (it needs to scale/move accordingly). Moreover, I want that the user be able to move/resize the window when he is not changing the viewer transform. I am currently struggling to achieve that under the current API of ImGui.

My current approach is as follows:

  1. Store the coordinate of the window (min_corner and max_corner) in world-space.
  2. Project/Apply the viewer transformations (zoom/translation).
  3. Set ImGui::SetNextWindowPos() and ImGui::SetNextWindowSize() according to the transformed window corner in screen space.
  4. After the ImGui::Begin()/ImGui::End(), retrieve the window coordinate and size from ImGui (maybe the user has moved/resized the window).
  5. Unproject the coordinates (i.e. retrieve the world-space corner of the window), to reuse in the next frame (because we want the window to follow the transformations of the viewer).

I am not sure this is the best approach, since I am not very familiar with the internals of ImGui. I currently have two problems with this procedure:

  • Roundoff errors. imgui.h only offers to set the window position/size with integer coordinates. So going back and forth with the world-space floatting point position makes the window "drift away" under repeated zoom/pan of the viewer.
  • Unable to move the window. With the current setup resizing the window works well, but I cannot move it (because the position is set by ImGui::SetNextWindowPos() at every frame). If I set the position with ImGui::SetWindowPos() inside the Begin/End, on the condition that the window is not focused, then the user can also move the window. But then the window also "lags behind" when I update the viewer transform, so it's doesn't look so smooth.

It seems that I am making my life complicated for no reason, and maybe I shouldn't use ImGui for doing this. But if you have any advice on how these kind of things should be done, I would be glad to hear it.

@ocornut
Copy link
Owner

ocornut commented Feb 10, 2018

Hello Jérémie,

Some thoughts out of my heads (I haven't digged much):

  • Perhaps don't call SetNextWindowPos() every frame, but rather only when your view has been transformed (translated/zoomed), that would simplify things? Supposedly when the user is manipulating your view/camera, they are not moving window.

  • Perhaps hold on a cache of the unrounded window position in your app When you read the window coordinates back only apply to your cache when it differs by 1. So when you transform window you write unrounded value and keep them around, and clicking to move a window would be the only "destructive" act of rounding down the position to the pixel.

@jdumas
Copy link
Contributor Author

jdumas commented Feb 10, 2018

Hi Omar,

Thanks for the suggestions! In the meantime I found out about ImGuizmo and ended up using it (adapting the code for 2D), as it fits my need perfectly in this case. So I think there is no point in me trying to implement the cache mechanism you suggest.

Feel free to close this issue!

@ocornut
Copy link
Owner

ocornut commented Feb 10, 2018

Cool, closing.

Depending on what you were doing, if it is something like ImGuiGizmo where you just draw stuff over the scene, it may be simpler to create a fullscreen window (pos = 0,0, size = io.DisplaySize) with the NoInputs flag, 0.0f alpha background, and use this window to draw your content.

@ocornut ocornut closed this as completed Feb 10, 2018
@jdumas
Copy link
Contributor Author

jdumas commented Feb 10, 2018

I wanted a gizmo to position a box in my scene, so I needed interactions too! (I'm already using a transparent window to draw text labels on my scene :p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants