CedricGuillemet / ImGuizmo

Immediate mode 3D gizmo for scene editing and other controls based on Dear Imgui
MIT License
3.22k stars 907 forks source link

Is it possible to clip the gizmos outside of the rect of ImGuizmo::SetRect? #285

Closed jjiangweilan closed 1 year ago

jjiangweilan commented 1 year ago

image I want to clip the gizmos so that it won't go out of bounds of the red rectangle which is the rect set to ImGuizmo::SetRect. Is it possible?

NikolayKanchevski commented 1 year ago

Yes, I have been having the same problem.

CedricGuillemet commented 1 year ago

I think it's doable by calling clip method on the drawlist at this place : https://github.com/CedricGuillemet/ImGuizmo/blob/93dd6aee3a3a1b296fde738ba28a4198fc45072c/ImGuizmo.cpp#L2524

NikolayKanchevski commented 1 year ago

@jjiangweilan, thanks to the suggestion from @CedricGuillemet, I was able to fix this like so:

First, draw your "Game Scene" window, whilst saving its corresponding draw list:

// Create scene view tab
if (ImGui::Begin("Scene View", nullptr))
{
    sceneDrawList = ImGui::GetWindowDrawList();

    // Draw rendered scene texture 
}

Then, you need to apply the clipping to the draw list you have saved from the "Game View". You can do this easily:

ImGuizmo::SetDrawlist(sceneDrawList);
sceneDrawList->PushClipRect(
     // Bottom-left X and Y coordinates of your "Game Scene"
     { ImGuiCore::GetSceneViewPositionX(), ImGuiCore::GetSceneViewPositionY() },
     // The X and Y coordinates you just passed, added with the width and height of the window
     { ImGuiCore::GetSceneViewPositionX() + ImGuiCore::GetSceneViewWidth(), ImGuiCore::GetSceneViewPositionY() + ImGuiCore::GetSceneViewHeight() }
);
jjiangweilan commented 1 year ago

thanks, I will give it a try