LumixEngine/src/renderer/editor/scene_view.cpp

276 lines
7.1 KiB
C++
Raw Normal View History

2015-09-20 17:30:37 +02:00
#include "scene_view.h"
2015-10-24 14:28:03 +02:00
#include "core/crc32.h"
#include "core/input_system.h"
2016-01-13 23:36:38 +01:00
#include "core/path.h"
#include "core/profiler.h"
2015-09-20 17:30:37 +02:00
#include "core/resource_manager.h"
2016-01-28 15:19:56 +01:00
#include "core/string.h"
2015-09-20 17:30:37 +02:00
#include "editor/gizmo.h"
#include "editor/imgui/imgui.h"
2016-02-12 23:49:56 +01:00
#include "editor/log_ui.h"
#include "editor/platform_interface.h"
#include "editor/settings.h"
2016-02-05 14:32:21 +01:00
#include "engine/engine.h"
#include "engine/plugin_manager.h"
2015-09-20 17:30:37 +02:00
#include "renderer/frame_buffer.h"
#include "renderer/pipeline.h"
2015-10-24 14:28:03 +02:00
#include "renderer/render_scene.h"
2016-01-12 13:52:14 +01:00
#include "renderer/renderer.h"
2015-09-20 17:30:37 +02:00
SceneView::SceneView()
{
2016-02-26 17:09:19 +01:00
m_pipeline = nullptr;
2015-09-20 17:30:37 +02:00
m_editor = nullptr;
m_camera_speed = 0.1f;
m_is_mouse_captured = false;
2016-01-28 15:19:56 +01:00
m_show_stats = false;
2016-02-12 23:49:56 +01:00
m_log_ui = nullptr;
2015-09-20 17:30:37 +02:00
}
SceneView::~SceneView()
{
}
2015-09-21 22:55:15 +02:00
void SceneView::setWireframe(bool wireframe)
{
2016-02-26 17:09:19 +01:00
m_pipeline->setWireframe(wireframe);
2015-09-21 22:55:15 +02:00
}
2015-09-20 17:30:37 +02:00
void SceneView::setScene(Lumix::RenderScene* scene)
{
2016-02-26 17:09:19 +01:00
m_pipeline->setScene(scene);
2015-09-20 17:30:37 +02:00
}
void SceneView::shutdown()
{
2016-01-13 17:21:59 +01:00
m_editor->universeCreated().unbind<SceneView, &SceneView::onUniverseCreated>(this);
m_editor->universeDestroyed().unbind<SceneView, &SceneView::onUniverseDestroyed>(this);
2016-02-26 17:09:19 +01:00
Lumix::Pipeline::destroy(m_pipeline);
m_pipeline = nullptr;
2015-09-20 17:30:37 +02:00
}
2015-10-24 14:28:03 +02:00
void SceneView::onUniverseCreated()
{
auto* scene = m_editor->getScene(Lumix::crc32("renderer"));
2016-02-26 17:09:19 +01:00
m_pipeline->setScene(static_cast<Lumix::RenderScene*>(scene));
2015-10-24 14:28:03 +02:00
}
void SceneView::onUniverseDestroyed()
{
2016-02-26 17:09:19 +01:00
m_pipeline->setScene(nullptr);
2015-10-24 14:28:03 +02:00
}
2016-02-12 23:49:56 +01:00
bool SceneView::init(LogUI& log_ui, Lumix::WorldEditor& editor, Lumix::Array<Action*>& actions)
2015-09-20 17:30:37 +02:00
{
2016-02-12 23:49:56 +01:00
m_log_ui = &log_ui;
2015-09-20 17:30:37 +02:00
m_editor = &editor;
auto& engine = editor.getEngine();
auto& allocator = engine.getAllocator();
2016-01-12 13:52:14 +01:00
auto* renderer = static_cast<Lumix::Renderer*>(engine.getPluginManager().getPlugin("renderer"));
2015-09-20 17:30:37 +02:00
2016-01-12 13:52:14 +01:00
Lumix::Path path("pipelines/main.lua");
2016-02-26 17:09:19 +01:00
m_pipeline = Lumix::Pipeline::create(*renderer, path, engine.getAllocator());
m_pipeline->load();
m_pipeline->addCustomCommandHandler("renderGizmos")
.callback.bind<SceneView, &SceneView::renderGizmos>(this);
2016-02-26 17:09:19 +01:00
m_pipeline->addCustomCommandHandler("renderIcons")
.callback.bind<SceneView, &SceneView::renderIcons>(this);
2015-10-24 14:28:03 +02:00
editor.universeCreated().bind<SceneView, &SceneView::onUniverseCreated>(this);
editor.universeDestroyed().bind<SceneView, &SceneView::onUniverseDestroyed>(this);
onUniverseCreated();
2015-10-16 21:19:27 +02:00
m_toggle_gizmo_step_action =
LUMIX_NEW(editor.getAllocator(), Action)("Enable/disable gizmo step", "toggleGizmoStep");
m_toggle_gizmo_step_action->is_global = false;
actions.push(m_toggle_gizmo_step_action);
2015-09-20 17:30:37 +02:00
return true;
}
void SceneView::update()
{
PROFILE_FUNCTION();
2015-10-16 21:19:27 +02:00
if (ImGui::IsAnyItemActive()) return;
2015-09-20 17:30:37 +02:00
if (!m_is_opened) return;
2015-10-24 14:34:57 +02:00
if (ImGui::GetIO().KeyCtrl) return;
2015-09-20 17:30:37 +02:00
2015-10-16 21:19:27 +02:00
m_camera_speed =
Lumix::Math::maxValue(0.01f, m_camera_speed + ImGui::GetIO().MouseWheel / 20.0f);
2015-09-20 17:30:37 +02:00
int screen_x = int(ImGui::GetIO().MousePos.x);
int screen_y = int(ImGui::GetIO().MousePos.y);
bool is_inside = screen_x >= m_screen_x && screen_y >= m_screen_y &&
2015-10-16 21:19:27 +02:00
screen_x <= m_screen_x + m_width && screen_y <= m_screen_y + m_height;
2015-09-20 17:30:37 +02:00
if (!is_inside) return;
float speed = m_camera_speed;
2015-10-24 14:34:57 +02:00
if (ImGui::GetIO().KeyShift)
2015-09-20 17:30:37 +02:00
{
speed *= 10;
}
if (ImGui::GetIO().KeysDown['W'])
{
m_editor->navigate(1.0f, 0, speed);
}
if (ImGui::GetIO().KeysDown['S'])
{
m_editor->navigate(-1.0f, 0, speed);
}
if (ImGui::GetIO().KeysDown['A'])
{
m_editor->navigate(0.0f, -1.0f, speed);
}
if (ImGui::GetIO().KeysDown['D'])
{
m_editor->navigate(0.0f, 1.0f, speed);
}
}
void SceneView::renderIcons()
2015-09-20 17:30:37 +02:00
{
2016-01-13 23:36:38 +01:00
m_editor->renderIcons();
}
void SceneView::renderGizmos()
{
2016-01-13 23:36:38 +01:00
m_editor->getGizmo().render();
2015-09-20 17:30:37 +02:00
}
void SceneView::captureMouse(bool capture)
{
if(m_is_mouse_captured == capture) return;
m_is_mouse_captured = capture;
PlatformInterface::showCursor(!m_is_mouse_captured);
if(!m_is_mouse_captured) PlatformInterface::unclipCursor();
}
2015-10-03 01:14:38 +02:00
void SceneView::onGUI()
2015-09-20 17:30:37 +02:00
{
2015-09-27 12:56:01 +02:00
PROFILE_FUNCTION();
2015-09-20 17:30:37 +02:00
m_is_opened = false;
2016-01-28 15:19:56 +01:00
ImVec2 view_pos;
2016-02-12 23:49:56 +01:00
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))
2015-09-20 17:30:37 +02:00
{
m_is_opened = true;
auto size = ImGui::GetContentRegionAvail();
size.y -= ImGui::GetTextLineHeightWithSpacing();
2016-02-26 17:09:19 +01:00
auto* fb = m_pipeline->getFramebuffer("default");
if (size.x > 0 && size.y > 0 && fb)
{
auto pos = ImGui::GetWindowPos();
2016-02-26 17:09:19 +01:00
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);
ImGui::Image(&m_texture_handle, size);
2016-01-28 15:19:56 +01:00
view_pos = content_min;
2016-01-13 17:21:59 +01:00
if (ImGui::IsItemHovered())
{
2016-02-26 20:51:45 +01:00
m_editor->getGizmo().enableStep(m_toggle_gizmo_step_action->isActive());
2016-01-13 17:21:59 +01:00
auto rel_mp = ImGui::GetMousePos();
rel_mp.x -= m_screen_x;
rel_mp.y -= m_screen_y;
for (int i = 0; i < 3; ++i)
{
if (ImGui::IsMouseClicked(i))
{
ImGui::ResetActiveID();
captureMouse(true);
2016-01-13 17:21:59 +01:00
m_editor->onMouseDown((int)rel_mp.x, (int)rel_mp.y, (Lumix::MouseButton::Value)i);
}
if (ImGui::IsMouseReleased(i))
{
captureMouse(false);
2016-01-13 17:21:59 +01:00
m_editor->onMouseUp((int)rel_mp.x, (int)rel_mp.y, (Lumix::MouseButton::Value)i);
}
auto& input = m_editor->getEngine().getInputSystem();
auto delta = Lumix::Vec2(input.getMouseXMove(), input.getMouseYMove());
2016-01-13 17:21:59 +01:00
if(delta.x != 0 || delta.y != 0)
{
m_editor->onMouseMove((int)rel_mp.x, (int)rel_mp.y, (int)delta.x, (int)delta.y);
}
}
}
if(m_is_mouse_captured)
{
PlatformInterface::clipCursor(
content_min.x, content_min.y, content_max.x, content_max.y);
}
2016-02-26 17:09:19 +01:00
m_pipeline->render();
}
2016-01-01 20:31:34 +01:00
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());
}
2015-10-16 21:19:27 +02:00
2016-01-01 20:31:34 +01:00
ImGui::SameLine();
int step = m_editor->getGizmo().getStep();
if (ImGui::DragInt("Gizmo step", &step, 1.0f, 0, 200))
2015-11-10 22:25:36 +01:00
{
2016-01-01 20:31:34 +01:00
m_editor->getGizmo().setStep(step);
2015-11-10 22:25:36 +01:00
}
2016-01-28 15:19:56 +01:00
ImGui::SameLine();
ImGui::Checkbox("Stats", &m_show_stats);
ImGui::SameLine();
2015-11-10 22:25:36 +01:00
}
2016-01-01 20:31:34 +01:00
ImGui::EndDock();
2016-01-28 15:19:56 +01:00
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))
{
2016-02-26 17:09:19 +01:00
const auto& stats = m_pipeline->getStats();
2016-01-28 15:19:56 +01:00
ImGui::LabelText("Draw calls", "%d", stats.m_draw_call_count);
ImGui::LabelText("Instances", "%d", stats.m_instance_count);
char buf[30];
Lumix::toCStringPretty(stats.m_triangle_count, buf, Lumix::lengthOf(buf));
ImGui::LabelText("Triangles", buf);
}
ImGui::End();
ImGui::PopStyleColor();
}
2015-09-20 17:30:37 +02:00
}