void GameView::onGui() { PROFILE_FUNCTION(); if (!m_pipeline->isReady()) return; auto& io = ImGui::GetIO(); bool is_foreground_win = PlatformInterface::isWindowActive(); if (m_is_mouse_captured && (io.KeysDown[ImGui::GetKeyIndex(ImGuiKey_Escape)] || !m_editor->isGameMode() || !is_foreground_win)) { captureMouse(false); } const char* window_name = "Game View###game_view"; if (m_is_mouse_captured) window_name = "Game View (mouse captured)###game_view"; if (ImGui::BeginDock(window_name, &m_is_opened)) { m_is_mouse_hovering_window = ImGui::IsMouseHoveringWindow(); auto content_min = ImGui::GetCursorScreenPos(); auto size = ImGui::GetContentRegionAvail(); size.y -= ImGui::GetTextLineHeightWithSpacing(); ImVec2 content_max(content_min.x + size.x, content_min.y + size.y); if (size.x > 0 && size.y > 0) { auto pos = ImGui::GetWindowPos(); auto cp = ImGui::GetCursorPos(); m_pipeline->setViewport(0, 0, int(size.x), int(size.y)); auto* fb = m_pipeline->getFramebuffer("default"); m_texture_handle = fb->getRenderbufferHandle(0); ImGui::Image(&m_texture_handle, size); if (ImGui::Checkbox("Pause", &m_paused)) { m_editor->getEngine().pause(m_paused); } if (m_paused) { ImGui::SameLine(); if (ImGui::Button("Next frame")) { m_editor->getEngine().nextFrame(); } } ImGui::SameLine(); if (ImGui::DragFloat("m_time_multiplier", &m_time_multiplier, 0.01f, 0.01f, 30.0f)) { m_editor->getEngine().setTimeMultiplier(m_time_multiplier); } m_pipeline->render(); } if (m_is_mouse_captured) { PlatformInterface::clipCursor( content_min.x, content_min.y, content_max.x, content_max.y); if (io.KeysDown[ImGui::GetKeyIndex(ImGuiKey_Escape)] || !m_editor->isGameMode()) { captureMouse(false); } } if (ImGui::IsMouseHoveringRect(content_min, content_max) && m_is_mouse_hovering_window && ImGui::IsMouseClicked(0) && m_editor->isGameMode()) { captureMouse(true); } } ImGui::EndDock(); }
void SceneView::onGUI() { PROFILE_FUNCTION(); m_is_opened = false; ImVec2 view_pos; const char* title = "Scene View###Scene View"; if (m_log_ui && m_log_ui->getUnreadErrorCount() > 0) { title = "Scene View | errors in log###Scene View"; } if (ImGui::BeginDock(title, nullptr, ImGuiWindowFlags_NoScrollWithMouse)) { m_is_opened = true; auto size = ImGui::GetContentRegionAvail(); size.y -= ImGui::GetTextLineHeightWithSpacing(); auto* fb = m_pipeline->getFramebuffer("default"); if (size.x > 0 && size.y > 0 && fb) { auto pos = ImGui::GetWindowPos(); m_pipeline->setViewport(0, 0, int(size.x), int(size.y)); m_texture_handle = fb->getRenderbufferHandle(0); auto cursor_pos = ImGui::GetCursorScreenPos(); m_screen_x = int(cursor_pos.x); m_screen_y = int(cursor_pos.y); m_width = int(size.x); m_height = int(size.y); auto content_min = ImGui::GetCursorScreenPos(); ImVec2 content_max(content_min.x + size.x, content_min.y + size.y); if (m_is_opengl) { ImGui::Image(&m_texture_handle, size, ImVec2(0, 1), ImVec2(1, 0)); } else { ImGui::Image(&m_texture_handle, size); } view_pos = content_min; auto rel_mp = ImGui::GetMousePos(); rel_mp.x -= m_screen_x; rel_mp.y -= m_screen_y; if (ImGui::IsItemHovered()) { m_editor->getGizmo().enableStep(m_toggle_gizmo_step_action->isActive()); for (int i = 0; i < 3; ++i) { if (ImGui::IsMouseClicked(i)) { ImGui::ResetActiveID(); if(i == 1) captureMouse(true); m_editor->onMouseDown((int)rel_mp.x, (int)rel_mp.y, (Lumix::MouseButton::Value)i); break; } } } if (m_is_mouse_captured || ImGui::IsItemHovered()) { auto& input = m_editor->getEngine().getInputSystem(); auto delta = Lumix::Vec2(input.getMouseXMove(), input.getMouseYMove()); if (delta.x != 0 || delta.y != 0) { m_editor->onMouseMove((int)rel_mp.x, (int)rel_mp.y, (int)delta.x, (int)delta.y); } } for (int i = 0; i < 3; ++i) { auto rel_mp = ImGui::GetMousePos(); rel_mp.x -= m_screen_x; rel_mp.y -= m_screen_y; if (ImGui::IsMouseReleased(i)) { if (i == 1) captureMouse(false); m_editor->onMouseUp((int)rel_mp.x, (int)rel_mp.y, (Lumix::MouseButton::Value)i); } } m_pipeline->render(); } ImGui::PushItemWidth(60); ImGui::DragFloat("Camera speed", &m_camera_speed, 0.1f, 0.01f, 999.0f, "%.2f"); ImGui::SameLine(); if (m_editor->isMeasureToolActive()) { ImGui::Text("| Measured distance: %f", m_editor->getMeasuredDistance()); } ImGui::SameLine(); int step = m_editor->getGizmo().getStep(); if (ImGui::DragInt("Gizmo step", &step, 1.0f, 0, 200)) { m_editor->getGizmo().setStep(step); } ImGui::SameLine(); ImGui::Checkbox("Stats", &m_show_stats); m_pipeline->callLuaFunction("onGUI"); } ImGui::EndDock(); if(m_show_stats) { view_pos.x += ImGui::GetStyle().FramePadding.x; view_pos.y += ImGui::GetStyle().FramePadding.y; ImGui::SetNextWindowPos(view_pos); auto col = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; col.w = 0.3f; ImGui::PushStyleColor(ImGuiCol_WindowBg, col); if (ImGui::Begin("###stats_overlay", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_ShowBorders)) { const auto& stats = m_pipeline->getStats(); ImGui::LabelText("Draw calls", "%d", stats.draw_call_count); ImGui::LabelText("Instances", "%d", stats.instance_count); char buf[30]; Lumix::toCStringPretty(stats.triangle_count, buf, Lumix::lengthOf(buf)); ImGui::LabelText("Triangles", "%s", buf); ImGui::LabelText("Resolution", "%dx%d", m_pipeline->getWidth(), m_pipeline->getHeight()); ImGui::LabelText("FPS", "%.2f", m_editor->getEngine().getFPS()); ImGui::LabelText("CPU time", "%.2f", m_pipeline->getCPUTime() * 1000.0f); ImGui::LabelText("GPU time", "%.2f", m_pipeline->getGPUTime() * 1000.0f); ImGui::LabelText("Waiting for submit", "%.2f", m_pipeline->getWaitSubmitTime() * 1000.0f); ImGui::LabelText("Waiting for render thread", "%.2f", m_pipeline->getGPUTime() * 1000.0f); } ImGui::End(); ImGui::PopStyleColor(); } }