feat: move rendering to an imgui window

This commit is contained in:
2026-10-22 22:02:57 +02:00
parent dde2c7daf9
commit f99147b1a2
13 changed files with 226 additions and 49 deletions
+15 -16
View File
@@ -56,8 +56,9 @@ bool RectGizmo::CanManipulate(GameObject& object)
Canvas::FindFor(object) != nullptr;
}
bool RectGizmo::Manipulate(GameObject& object, const glm::vec2& drawImageSize)
bool RectGizmo::Manipulate(GameObject& object, const glm::vec2& drawImageSize, const glm::vec2& viewMin, const glm::vec2& viewSize)
{
m_Hovered = false;
auto* rectTransform = object.GetComponent<RectTransform>();
const Canvas* canvas = Canvas::FindFor(object);
if (!CanManipulate(object) || drawImageSize.x <= 0.0f || drawImageSize.y <= 0.0f) {
@@ -65,20 +66,19 @@ bool RectGizmo::Manipulate(GameObject& object, const glm::vec2& drawImageSize)
return false;
}
const ImGuiViewport* viewport = ImGui::GetMainViewport();
const glm::vec2 viewOrigin = ToVec(viewport->Pos);
const glm::vec2 viewScale = ToVec(viewport->Size) / drawImageSize;
const auto toView = [&](const glm::vec2& point) { return viewOrigin + point * viewScale; };
const glm::vec2 viewScale = viewSize / drawImageSize;
const auto toView = [&](const glm::vec2& point) { return viewMin + point * viewScale; };
const UIRect& rect = rectTransform->GetScreenRect();
const glm::vec2 viewMin = toView(rect.min);
const glm::vec2 viewMax = toView(rect.Max());
const glm::vec2 viewCenter = (viewMin + viewMax) * 0.5f;
const glm::vec2 viewHalfSize = (viewMax - viewMin) * 0.5f;
const glm::vec2 rectMin = toView(rect.min);
const glm::vec2 rectMax = toView(rect.Max());
const glm::vec2 viewCenter = (rectMin + rectMax) * 0.5f;
const glm::vec2 viewHalfSize = (rectMax - rectMin) * 0.5f;
const glm::vec2 mouse = ToVec(ImGui::GetIO().MousePos);
int hovered = -1;
const bool overImGui = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) || ImGui::IsAnyItemActive();
// Called inside the window that shows the scene, anything on top of it blocks the handles
const bool overImGui = !ImGui::IsWindowHovered() || ImGui::IsAnyItemActive();
if (m_ActiveHandle < 0 && !overImGui) {
for (std::size_t i = 0; i < HandleDirections.size(); ++i) {
const glm::vec2 handle = viewCenter + HandleDirections[i] * viewHalfSize;
@@ -87,7 +87,7 @@ bool RectGizmo::Manipulate(GameObject& object, const glm::vec2& drawImageSize)
break;
}
}
if (hovered < 0 && mouse.x >= viewMin.x && mouse.y >= viewMin.y && mouse.x <= viewMax.x && mouse.y <= viewMax.y) {
if (hovered < 0 && mouse.x >= rectMin.x && mouse.y >= rectMin.y && mouse.x <= rectMax.x && mouse.y <= rectMax.y) {
hovered = MoveHandle;
}
if (hovered >= 0 && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
@@ -135,13 +135,12 @@ bool RectGizmo::Manipulate(GameObject& object, const glm::vec2& drawImageSize)
}
const int highlighted = m_ActiveHandle >= 0 ? m_ActiveHandle : hovered;
m_Hovered = highlighted >= 0;
if (highlighted >= 0) {
ImGui::SetMouseCursor(CursorFor(highlighted));
// Keeps the click from reaching the game, same as the 3D gizmo
ImGui::SetNextFrameWantCaptureMouse(true);
}
ImDrawList& drawList = *ImGui::GetBackgroundDrawList();
ImDrawList& drawList = *ImGui::GetWindowDrawList();
const ImU32 outlineColor = object.IsActiveInHierarchy() ? OutlineColor : HiddenColor;
const UIRect& parentRect = rectTransform->GetParentRect();
@@ -152,9 +151,9 @@ bool RectGizmo::Manipulate(GameObject& object, const glm::vec2& drawImageSize)
DrawAnchor(drawList, {anchorMax.x, anchorMax.y}, {1.0f, 1.0f});
DrawAnchor(drawList, {anchorMin.x, anchorMax.y}, {-1.0f, 1.0f});
drawList.AddRect(ToImVec(viewMin), ToImVec(viewMax), highlighted == MoveHandle ? ActiveColor : outlineColor, 0.0f, 0, 1.5f);
drawList.AddRect(ToImVec(rectMin), ToImVec(rectMax), highlighted == MoveHandle ? ActiveColor : outlineColor, 0.0f, 0, 1.5f);
const glm::vec2 pivot = viewMin + (viewMax - viewMin) * rectTransform->GetPivot();
const glm::vec2 pivot = rectMin + (rectMax - rectMin) * rectTransform->GetPivot();
drawList.AddCircle(ToImVec(pivot), 4.0f, outlineColor, 12, 1.5f);
for (std::size_t i = 0; i < HandleDirections.size(); ++i) {