adding and integrating ImGuizmo
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
#include "game_panel.hpp"
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
void GamePanel::onUpdate(const sophia::FrameInfo&) {
|
||||
syncSceneView(context, sceneViews, view, isShown());
|
||||
|
||||
if (!context.hasScene()) {
|
||||
ImGui::TextDisabled("No scene open");
|
||||
return;
|
||||
}
|
||||
|
||||
const ImVec2 origin = ImGui::GetCursorScreenPos();
|
||||
ui.image(target);
|
||||
|
||||
if (!context.getScene().getActiveCamera()) {
|
||||
ImGui::SetCursorScreenPos(ImVec2(origin.x + 8.0f, origin.y + 8.0f));
|
||||
ImGui::TextDisabled("The scene has no active camera");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <sophia/frame_info.hpp>
|
||||
#include <sophia/handles.hpp>
|
||||
#include <sophia/scene_view_registry.hpp>
|
||||
#include <sophia/ui/panel.hpp>
|
||||
#include <sophia/ui/ui.hpp>
|
||||
|
||||
#include "../editor_context.hpp"
|
||||
|
||||
/**
|
||||
* Shows the active scene through its own active camera: what the game will
|
||||
* see.
|
||||
*/
|
||||
class GamePanel : public sophia::Panel {
|
||||
public:
|
||||
// Renders into `target` through `view`, a SceneView with no camera of its
|
||||
// own that this panel keeps pointed at the active scene.
|
||||
GamePanel(sophia::UI& ui, EditorContext& context,
|
||||
sophia::SceneViewRegistry& sceneViews, sophia::SceneViewHandle view,
|
||||
sophia::RenderTargetHandle target)
|
||||
: Panel{{.name = "Game",
|
||||
.windowFlags = ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse}},
|
||||
ui{ui},
|
||||
context{context},
|
||||
sceneViews{sceneViews},
|
||||
view{view},
|
||||
target{target} {}
|
||||
|
||||
void onUpdate(const sophia::FrameInfo& frameInfo) override;
|
||||
|
||||
private:
|
||||
sophia::UI& ui;
|
||||
EditorContext& context;
|
||||
sophia::SceneViewRegistry& sceneViews;
|
||||
sophia::SceneViewHandle view;
|
||||
sophia::RenderTargetHandle target;
|
||||
};
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <sophia/components.hpp>
|
||||
|
||||
void InspectorPanel::onUpdate(const sophia::FrameInfo&) {
|
||||
selected = context.selected;
|
||||
if (!selected) {
|
||||
ImGui::TextDisabled("No entity selected");
|
||||
return;
|
||||
@@ -16,6 +17,7 @@ void InspectorPanel::onUpdate(const sophia::FrameInfo&) {
|
||||
drawTransform();
|
||||
drawCamera();
|
||||
drawSprite();
|
||||
drawMesh();
|
||||
|
||||
ImGui::Separator();
|
||||
drawAddComponent();
|
||||
@@ -40,11 +42,13 @@ void InspectorPanel::drawTransform() {
|
||||
auto& transform = selected.getComponent<sophia::TransformComponent>();
|
||||
ImGui::DragFloat3("Position", &transform.position.x, 0.1f);
|
||||
|
||||
// 2D only: edits rotation around Z and drops any X/Y rotation.
|
||||
float degrees = glm::degrees(glm::eulerAngles(transform.rotation).z);
|
||||
if (ImGui::DragFloat("Rotation", °rees, 1.0f)) {
|
||||
transform.rotation =
|
||||
glm::angleAxis(glm::radians(degrees), glm::vec3{0.f, 0.f, 1.f});
|
||||
// Euler angles in degrees, recomputed from the quaternion every frame.
|
||||
// Near 90 degrees of pitch the angles can jump between equivalent values
|
||||
// while dragging.
|
||||
// TODO: keep each entity's edited angles, or replace with gizmo rotation.
|
||||
glm::vec3 degrees = glm::degrees(glm::eulerAngles(transform.rotation));
|
||||
if (ImGui::DragFloat3("Rotation", °rees.x, 1.0f)) {
|
||||
transform.rotation = glm::quat{glm::radians(degrees)};
|
||||
}
|
||||
|
||||
ImGui::DragFloat3("Scale", &transform.scale.x, 0.1f);
|
||||
@@ -118,6 +122,29 @@ void InspectorPanel::drawSprite() {
|
||||
}
|
||||
}
|
||||
|
||||
void InspectorPanel::drawMesh() {
|
||||
if (!selected.hasComponent<sophia::MeshComponent>()) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool open = true;
|
||||
if (!ImGui::CollapsingHeader("Mesh", &open, ImGuiTreeNodeFlags_DefaultOpen)) {
|
||||
if (!open) {
|
||||
selected.removeComponent<sophia::MeshComponent>();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
auto& mesh = selected.getComponent<sophia::MeshComponent>();
|
||||
// TODO: pick the mesh once assets have names.
|
||||
ImGui::Text("Mesh: %u", static_cast<sophia::u32>(mesh.handle.index));
|
||||
ImGui::ColorEdit3("Color", &mesh.color.x);
|
||||
|
||||
if (!open) {
|
||||
selected.removeComponent<sophia::MeshComponent>();
|
||||
}
|
||||
}
|
||||
|
||||
void InspectorPanel::drawAddComponent() {
|
||||
if (ImGui::Button("Add Component")) {
|
||||
ImGui::OpenPopup("AddComponent");
|
||||
|
||||
@@ -8,14 +8,16 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../editor_context.hpp"
|
||||
|
||||
class InspectorPanel : public sophia::Panel {
|
||||
public:
|
||||
// Stand-in for an asset browser: the textures a sprite can pick from.
|
||||
using TextureList =
|
||||
std::vector<std::pair<std::string, sophia::TextureHandle>>;
|
||||
|
||||
InspectorPanel(sophia::Entity& selected, const TextureList& textures)
|
||||
: Panel{{.name = "Inspector"}}, selected{selected}, textures{textures} {}
|
||||
InspectorPanel(EditorContext& context, const TextureList& textures)
|
||||
: Panel{{.name = "Inspector"}}, context{context}, textures{textures} {}
|
||||
|
||||
void onUpdate(const sophia::FrameInfo& frameInfo) override;
|
||||
|
||||
@@ -24,8 +26,11 @@ class InspectorPanel : public sophia::Panel {
|
||||
void drawTransform();
|
||||
void drawCamera();
|
||||
void drawSprite();
|
||||
void drawMesh();
|
||||
void drawAddComponent();
|
||||
|
||||
sophia::Entity& selected;
|
||||
EditorContext& context;
|
||||
// The selected entity, for the duration of onUpdate().
|
||||
sophia::Entity selected;
|
||||
const TextureList& textures;
|
||||
};
|
||||
|
||||
@@ -2,18 +2,37 @@
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <sophia/components.hpp>
|
||||
#include <string>
|
||||
|
||||
void SceneHierarchyPanel::onUpdate(const sophia::FrameInfo&) {
|
||||
drawSceneSelector();
|
||||
ImGui::Separator();
|
||||
|
||||
if (!context.hasScene()) {
|
||||
ImGui::TextDisabled("No scene open");
|
||||
return;
|
||||
}
|
||||
|
||||
sophia::Scene& scene = context.getScene();
|
||||
sophia::Entity& selected = context.selected;
|
||||
|
||||
if (ImGui::Button("+ Sprite")) {
|
||||
selected = scene.createEntity("Sprite");
|
||||
selected.addComponent<sophia::SpriteComponent>();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("+ Cube")) {
|
||||
selected = scene.createEntity("Cube");
|
||||
selected.addComponent<sophia::MeshComponent>(
|
||||
sophia::MeshComponent{.handle = cubeMesh});
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
// Flat for now: there is no parent/child component yet.
|
||||
scene.forEachEntity([this](sophia::Entity entity) {
|
||||
scene.forEachEntity([&selected](sophia::Entity entity) {
|
||||
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf |
|
||||
ImGuiTreeNodeFlags_NoTreePushOnOpen |
|
||||
ImGuiTreeNodeFlags_SpanAvailWidth;
|
||||
@@ -30,3 +49,86 @@ void SceneHierarchyPanel::onUpdate(const sophia::FrameInfo&) {
|
||||
ImGui::PopID();
|
||||
});
|
||||
}
|
||||
|
||||
namespace {
|
||||
// See SceneHierarchyPanel::renameFocusFrames.
|
||||
constexpr int RENAME_FOCUS_FRAMES = 2;
|
||||
} // namespace
|
||||
|
||||
void SceneHierarchyPanel::drawSceneSelector() {
|
||||
// Leave room for the button: its label, its padding, and the gap to it.
|
||||
constexpr const char* ADD_SCENE_LABEL = "Add Scene";
|
||||
const ImGuiStyle& style = ImGui::GetStyle();
|
||||
ImGui::SetNextItemWidth(-(ImGui::CalcTextSize(ADD_SCENE_LABEL).x +
|
||||
style.FramePadding.x * 2.f + style.ItemSpacing.x));
|
||||
|
||||
const std::string* current = context.getSceneName(context.getSceneHandle());
|
||||
bool startRename = false;
|
||||
|
||||
if (renamingScene && current) {
|
||||
drawSceneRename();
|
||||
} else {
|
||||
renamingScene = false;
|
||||
if (ImGui::BeginCombo("##scene", current ? current->c_str() : "No scene")) {
|
||||
for (const EditorContext::OpenScene& open : context.getOpenScenes()) {
|
||||
ImGui::PushID(static_cast<int>(open.handle.index));
|
||||
bool isCurrent = open.handle == context.getSceneHandle();
|
||||
if (ImGui::Selectable(open.name.c_str(), isCurrent) && !isCurrent) {
|
||||
context.setScene(open.handle);
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
|
||||
if (current && ImGui::BeginPopupContextItem("##sceneMenu")) {
|
||||
startRename = ImGui::MenuItem("Rename", "F2");
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
startRename = startRename || (current && ImGui::IsWindowFocused() &&
|
||||
!ImGui::GetIO().WantTextInput &&
|
||||
ImGui::IsKeyPressed(ImGuiKey_F2));
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button(ADD_SCENE_LABEL)) {
|
||||
std::string name = "Untitled " + std::to_string(++this->untitledSceneCount);
|
||||
context.setScene(context.createScene(std::move(name)));
|
||||
startRename = false;
|
||||
}
|
||||
|
||||
if (startRename) {
|
||||
renamingScene = true;
|
||||
renameFocusFrames = RENAME_FOCUS_FRAMES;
|
||||
std::snprintf(renameBuffer.data(), renameBuffer.size(), "%s",
|
||||
current->c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void SceneHierarchyPanel::drawSceneRename() {
|
||||
if (renameFocusFrames == RENAME_FOCUS_FRAMES) {
|
||||
ImGui::SetKeyboardFocusHere();
|
||||
}
|
||||
|
||||
ImGui::InputText("##sceneName", renameBuffer.data(), renameBuffer.size(),
|
||||
ImGuiInputTextFlags_AutoSelectAll);
|
||||
|
||||
bool active = ImGui::IsItemActive();
|
||||
if (active) {
|
||||
renameFocusFrames = 0;
|
||||
} else if (renameFocusFrames > 0) {
|
||||
renameFocusFrames--;
|
||||
return;
|
||||
}
|
||||
|
||||
// Escape reverts the text before deactivating, so committing whatever is
|
||||
// left when the field closes covers Enter, clicking away and Escape alike.
|
||||
if (!active) {
|
||||
std::string name = renameBuffer.data();
|
||||
// Keep the old name rather than leave the scene nameless.
|
||||
if (name.find_first_not_of(' ') != std::string::npos) {
|
||||
context.renameScene(context.getSceneHandle(), std::move(name));
|
||||
}
|
||||
renamingScene = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <sophia/entity.hpp>
|
||||
#include <array>
|
||||
#include <sophia/frame_info.hpp>
|
||||
#include <sophia/scene.hpp>
|
||||
#include <sophia/handles.hpp>
|
||||
#include <sophia/ui/panel.hpp>
|
||||
|
||||
#include "../editor_context.hpp"
|
||||
|
||||
class SceneHierarchyPanel : public sophia::Panel {
|
||||
public:
|
||||
SceneHierarchyPanel(sophia::Scene& scene, sophia::Entity& selected)
|
||||
// `cubeMesh` is what "+ Cube" gives new entities.
|
||||
SceneHierarchyPanel(EditorContext& context, sophia::MeshHandle cubeMesh)
|
||||
: Panel{{.name = "Scene Hierarchy", .locked = true}},
|
||||
scene{scene},
|
||||
selected{selected} {}
|
||||
context{context},
|
||||
cubeMesh{cubeMesh} {}
|
||||
|
||||
void onUpdate(const sophia::FrameInfo& frameInfo) override;
|
||||
|
||||
private:
|
||||
sophia::Scene& scene;
|
||||
sophia::Entity& selected;
|
||||
// Switches between open scenes, renames the active one, and creates new
|
||||
// ones.
|
||||
void drawSceneSelector();
|
||||
// Replaces the selector with a text field editing the active scene's name.
|
||||
void drawSceneRename();
|
||||
|
||||
EditorContext& context;
|
||||
sophia::MeshHandle cubeMesh;
|
||||
// For naming new scenes "Untitled 1", "Untitled 2", ...
|
||||
int untitledSceneCount = 0;
|
||||
|
||||
// Set while the scene selector is a text field for renaming.
|
||||
bool renamingScene = false;
|
||||
// Frames the text field has left to become active after renaming starts;
|
||||
// focus can take a frame to land. If it never does, renaming ends.
|
||||
int renameFocusFrames = 0;
|
||||
std::array<char, 128> renameBuffer{};
|
||||
};
|
||||
|
||||
+472
-56
@@ -1,11 +1,20 @@
|
||||
#include "scene_panel.hpp"
|
||||
|
||||
#include <imgui.h>
|
||||
// After imgui.h, which it relies on.
|
||||
#include <ImGuizmo.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <sophia/aabb.hpp>
|
||||
#include <sophia/components.hpp>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -16,17 +25,125 @@ constexpr std::array<glm::vec2, 4> UNIT_QUAD{
|
||||
|
||||
const ImU32 SELECTION_COLOR = IM_COL32(255, 170, 40, 255);
|
||||
|
||||
// Holding Ctrl snaps the gizmo to these steps.
|
||||
constexpr float TRANSLATE_SNAP = 0.5f;
|
||||
constexpr float ROTATE_SNAP_DEGREES = 15.f;
|
||||
constexpr float SCALE_SNAP = 0.1f;
|
||||
|
||||
// Writes a matrix built from a translation, rotation and scale (no shear)
|
||||
// back into `transform`. A mirroring matrix comes back with a negative x
|
||||
// scale.
|
||||
void decompose(const glm::mat4& matrix, sophia::TransformComponent& transform) {
|
||||
transform.position = glm::vec3{matrix[3]};
|
||||
|
||||
glm::vec3 scale{glm::length(glm::vec3{matrix[0]}),
|
||||
glm::length(glm::vec3{matrix[1]}),
|
||||
glm::length(glm::vec3{matrix[2]})};
|
||||
if (glm::determinant(glm::mat3{matrix}) < 0.f) {
|
||||
scale.x = -scale.x;
|
||||
}
|
||||
transform.scale = scale;
|
||||
|
||||
// A zero scale loses the axis, and with it the rotation; keep the old one.
|
||||
if (scale.x == 0.f || scale.y == 0.f || scale.z == 0.f) {
|
||||
return;
|
||||
}
|
||||
glm::mat3 rotation{glm::vec3{matrix[0]} / scale.x,
|
||||
glm::vec3{matrix[1]} / scale.y,
|
||||
glm::vec3{matrix[2]} / scale.z};
|
||||
transform.rotation = glm::normalize(glm::quat_cast(rotation));
|
||||
}
|
||||
|
||||
// How far along a ray (from `origin`, along the normalized `direction`) it
|
||||
// enters `bounds` placed by `model`, or null if it misses. A ray starting
|
||||
// inside hits at 0.
|
||||
std::optional<float> intersectBox(const glm::vec3& origin,
|
||||
const glm::vec3& direction,
|
||||
const glm::mat4& model,
|
||||
const sophia::AABB& bounds) {
|
||||
if (glm::determinant(model) == 0.f) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Test in the box's space, where it's axis-aligned. The direction isn't
|
||||
// renormalized, so distances along it are still world distances.
|
||||
glm::mat4 inverse = glm::inverse(model);
|
||||
glm::vec3 localOrigin = glm::vec3{inverse * glm::vec4{origin, 1.f}};
|
||||
glm::vec3 localDirection = glm::vec3{inverse * glm::vec4{direction, 0.f}};
|
||||
|
||||
float enter = 0.f;
|
||||
float exit = std::numeric_limits<float>::infinity();
|
||||
for (int axis = 0; axis < 3; axis++) {
|
||||
if (std::abs(localDirection[axis]) < 1e-8f) {
|
||||
// Parallel to this pair of faces: hits only if already between them.
|
||||
if (localOrigin[axis] < bounds.min[axis] ||
|
||||
localOrigin[axis] > bounds.max[axis]) {
|
||||
return std::nullopt;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
float toMin = (bounds.min[axis] - localOrigin[axis]) / localDirection[axis];
|
||||
float toMax = (bounds.max[axis] - localOrigin[axis]) / localDirection[axis];
|
||||
if (toMin > toMax) {
|
||||
std::swap(toMin, toMax);
|
||||
}
|
||||
enter = std::max(enter, toMin);
|
||||
exit = std::min(exit, toMax);
|
||||
if (enter > exit) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
return enter;
|
||||
}
|
||||
|
||||
// How far along a ray it hits the unit quad (0, 0)-(1, 1) in the XY plane,
|
||||
// placed by `model`, or null if it misses.
|
||||
std::optional<float> intersectQuad(const glm::vec3& origin,
|
||||
const glm::vec3& direction,
|
||||
const glm::mat4& model) {
|
||||
if (glm::determinant(model) == 0.f) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
glm::mat4 inverse = glm::inverse(model);
|
||||
glm::vec3 localOrigin = glm::vec3{inverse * glm::vec4{origin, 1.f}};
|
||||
glm::vec3 localDirection = glm::vec3{inverse * glm::vec4{direction, 0.f}};
|
||||
// Edge on to the quad.
|
||||
if (std::abs(localDirection.z) < 1e-8f) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
float distance = -localOrigin.z / localDirection.z;
|
||||
if (distance < 0.f) {
|
||||
return std::nullopt;
|
||||
}
|
||||
glm::vec3 hit = localOrigin + distance * localDirection;
|
||||
if (hit.x < 0.f || hit.x > 1.f || hit.y < 0.f || hit.y > 1.f) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return distance;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ImVec2 ScenePanel::ViewportTransform::worldToScreen(
|
||||
const glm::vec3& world) const {
|
||||
glm::vec4 clip = viewProjection * glm::vec4{world, 1.f};
|
||||
ImVec2 ScenePanel::ViewportTransform::clipToScreen(
|
||||
const glm::vec4& clip) const {
|
||||
glm::vec2 ndc = glm::vec2{clip} / clip.w;
|
||||
// Vulkan NDC has +y pointing down, same as the screen.
|
||||
return {origin.x + (ndc.x * 0.5f + 0.5f) * size.x,
|
||||
origin.y + (ndc.y * 0.5f + 0.5f) * size.y};
|
||||
}
|
||||
|
||||
std::optional<ImVec2> ScenePanel::ViewportTransform::worldToScreen(
|
||||
const glm::vec3& world) const {
|
||||
glm::vec4 clip = viewProjection * glm::vec4{world, 1.f};
|
||||
if (clip.w <= 0.f) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return clipToScreen(clip);
|
||||
}
|
||||
|
||||
glm::vec2 ScenePanel::ViewportTransform::screenToWorld(
|
||||
const ImVec2& screen) const {
|
||||
glm::vec2 ndc{(screen.x - origin.x) / size.x * 2.f - 1.f,
|
||||
@@ -35,7 +152,42 @@ glm::vec2 ScenePanel::ViewportTransform::screenToWorld(
|
||||
return glm::vec2{world} / world.w;
|
||||
}
|
||||
|
||||
void ScenePanel::onUpdate(const sophia::FrameInfo&) {
|
||||
ScenePanel::Ray ScenePanel::ViewportTransform::screenToRay(
|
||||
const ImVec2& screen) const {
|
||||
glm::vec2 ndc{(screen.x - origin.x) / size.x * 2.f - 1.f,
|
||||
(screen.y - origin.y) / size.y * 2.f - 1.f};
|
||||
// Depth runs 0 at the near plane to 1 at the far one.
|
||||
glm::vec4 nearPoint = inverseViewProjection * glm::vec4{ndc, 0.f, 1.f};
|
||||
glm::vec4 farPoint = inverseViewProjection * glm::vec4{ndc, 1.f, 1.f};
|
||||
glm::vec3 from = glm::vec3{nearPoint} / nearPoint.w;
|
||||
glm::vec3 to = glm::vec3{farPoint} / farPoint.w;
|
||||
return {from, glm::normalize(to - from)};
|
||||
}
|
||||
|
||||
void ScenePanel::ViewportTransform::drawLine(ImDrawList* drawList,
|
||||
const glm::vec3& a,
|
||||
const glm::vec3& b, ImU32 color,
|
||||
float thickness) const {
|
||||
// Just in front of the camera. Points behind it project mirrored, so the
|
||||
// part of the line there is cut off rather than drawn.
|
||||
constexpr float MIN_W = 1e-4f;
|
||||
|
||||
glm::vec4 clipA = viewProjection * glm::vec4{a, 1.f};
|
||||
glm::vec4 clipB = viewProjection * glm::vec4{b, 1.f};
|
||||
if (clipA.w < MIN_W && clipB.w < MIN_W) {
|
||||
return;
|
||||
}
|
||||
if (clipA.w < MIN_W) {
|
||||
clipA = glm::mix(clipA, clipB, (MIN_W - clipA.w) / (clipB.w - clipA.w));
|
||||
} else if (clipB.w < MIN_W) {
|
||||
clipB = glm::mix(clipB, clipA, (MIN_W - clipB.w) / (clipA.w - clipB.w));
|
||||
}
|
||||
drawList->AddLine(clipToScreen(clipA), clipToScreen(clipB), color, thickness);
|
||||
}
|
||||
|
||||
void ScenePanel::onUpdate(const sophia::FrameInfo& frameInfo) {
|
||||
syncSceneView(context, sceneViews, view, isShown());
|
||||
|
||||
drawToolbar();
|
||||
ImGui::Separator();
|
||||
|
||||
@@ -45,35 +197,75 @@ void ScenePanel::onUpdate(const sophia::FrameInfo&) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!context.hasScene()) {
|
||||
ImGui::TextDisabled("No scene open");
|
||||
return;
|
||||
}
|
||||
|
||||
ui.image(target);
|
||||
|
||||
// Rolls over ImGuizmo's "mouse is over a handle" state, so IsOver() below
|
||||
// reports last frame's.
|
||||
ImGuizmo::BeginFrame();
|
||||
// A handle can't be grabbed while any ImGui item is hovered, so while the
|
||||
// mouse is over one, leave out the viewport button, and keep the window
|
||||
// still so dragging the handle doesn't drag it. Unless a camera drag is
|
||||
// already under way: dropping the button would end it.
|
||||
bool overGizmo =
|
||||
(ImGuizmo::IsOver() || ImGuizmo::IsUsing()) && !viewportActive;
|
||||
getConfig().windowFlags =
|
||||
overGizmo ? WINDOW_FLAGS | ImGuiWindowFlags_NoMove : WINDOW_FLAGS;
|
||||
|
||||
// The image doesn't take mouse input, so without this, dragging in it
|
||||
// would drag the window instead.
|
||||
ImGui::SetCursorScreenPos(origin);
|
||||
ImGui::InvisibleButton("##viewport", size);
|
||||
if (overGizmo) {
|
||||
ImGui::Dummy(size);
|
||||
} else {
|
||||
ImGui::InvisibleButton("##viewport", size,
|
||||
ImGuiButtonFlags_MouseButtonLeft |
|
||||
ImGuiButtonFlags_MouseButtonRight |
|
||||
ImGuiButtonFlags_MouseButtonMiddle);
|
||||
}
|
||||
viewportActive = !overGizmo && ImGui::IsItemActive();
|
||||
|
||||
handleCameraInput(frameInfo, size);
|
||||
handleGizmoHotkeys();
|
||||
|
||||
// Drive the view's camera from the editor camera. The scene view renders
|
||||
// after the panels update, so this frame already uses it.
|
||||
camera.getComponent<sophia::TransformComponent>() =
|
||||
editorCamera.getTransform();
|
||||
camera.getComponent<sophia::CameraComponent>() = editorCamera.getCamera();
|
||||
|
||||
ImDrawList* drawList = ImGui::GetWindowDrawList();
|
||||
drawList->PushClipRect(origin, ImVec2(origin.x + size.x, origin.y + size.y),
|
||||
true);
|
||||
|
||||
if (showGrid) {
|
||||
// Screen-space, so it only lines up with anything in 2D.
|
||||
// TODO: a world-space grid on the XZ plane for 3D, once there's debug
|
||||
// drawing in scene views.
|
||||
if (showGrid && editorCamera.mode == EditorCamera::Mode::Scene2D) {
|
||||
drawGrid(origin, size);
|
||||
}
|
||||
|
||||
sophia::Entity camera = scene.getActiveCamera();
|
||||
if (camera && camera.hasComponent<sophia::CameraComponent>()) {
|
||||
// Use the panel's size rather than the target's: the target is resized
|
||||
// a frame late, and the mouse is over the panel.
|
||||
float aspect = size.x / size.y;
|
||||
glm::mat4 viewProjection =
|
||||
camera.getComponent<sophia::CameraComponent>().getViewProjection(
|
||||
camera.getComponent<sophia::TransformComponent>(), aspect);
|
||||
// Use the panel's size rather than the target's: the target is resized a
|
||||
// frame late, and the mouse is over the panel.
|
||||
float aspect = size.x / size.y;
|
||||
glm::mat4 view = sophia::CameraComponent::getView(
|
||||
camera.getComponent<sophia::TransformComponent>());
|
||||
glm::mat4 viewProjection =
|
||||
camera.getComponent<sophia::CameraComponent>().getProjection(aspect) *
|
||||
view;
|
||||
ViewportTransform viewport{origin, size, viewProjection,
|
||||
glm::inverse(viewProjection)};
|
||||
|
||||
ViewportTransform viewport{origin, size, viewProjection,
|
||||
glm::inverse(viewProjection)};
|
||||
handleInput(viewport);
|
||||
drawSelection(viewport);
|
||||
}
|
||||
handleSelectionInput(viewport);
|
||||
drawSelection(viewport);
|
||||
|
||||
ImGuizmo::SetDrawlist();
|
||||
ImGuizmo::SetRect(origin.x, origin.y, size.x, size.y);
|
||||
drawGizmo(view, aspect);
|
||||
|
||||
drawList->PopClipRect();
|
||||
|
||||
@@ -81,7 +273,106 @@ void ScenePanel::onUpdate(const sophia::FrameInfo&) {
|
||||
ImGui::TextDisabled("%.0f x %.0f", size.x, size.y);
|
||||
}
|
||||
|
||||
void ScenePanel::drawToolbar() { ImGui::Checkbox("Grid", &showGrid); }
|
||||
void ScenePanel::drawToolbar() {
|
||||
bool is2D = editorCamera.mode == EditorCamera::Mode::Scene2D;
|
||||
if (ImGui::Checkbox("2D", &is2D)) {
|
||||
editorCamera.mode =
|
||||
is2D ? EditorCamera::Mode::Scene2D : EditorCamera::Mode::Scene3D;
|
||||
dragging = false;
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::BeginDisabled(!is2D);
|
||||
ImGui::Checkbox("Grid", &showGrid);
|
||||
ImGui::EndDisabled();
|
||||
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("|");
|
||||
ImGui::SameLine();
|
||||
const struct {
|
||||
const char* label;
|
||||
GizmoOperation operation;
|
||||
} operations[] = {{"Move (W)", GizmoOperation::Translate},
|
||||
{"Rotate (E)", GizmoOperation::Rotate},
|
||||
{"Scale (R)", GizmoOperation::Scale}};
|
||||
for (const auto& entry : operations) {
|
||||
if (ImGui::RadioButton(entry.label, gizmoOperation == entry.operation)) {
|
||||
gizmoOperation = entry.operation;
|
||||
}
|
||||
ImGui::SameLine();
|
||||
}
|
||||
if (ImGui::Button(gizmoLocal ? "Local" : "World")) {
|
||||
gizmoLocal = !gizmoLocal;
|
||||
}
|
||||
}
|
||||
|
||||
void ScenePanel::handleGizmoHotkeys() {
|
||||
const ImGuiIO& io = ImGui::GetIO();
|
||||
// W/E/Q fly the camera while the right button is held.
|
||||
if (!ImGui::IsWindowFocused() || io.WantTextInput ||
|
||||
ImGui::IsMouseDown(ImGuiMouseButton_Right)) {
|
||||
return;
|
||||
}
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_W, false)) {
|
||||
gizmoOperation = GizmoOperation::Translate;
|
||||
} else if (ImGui::IsKeyPressed(ImGuiKey_E, false)) {
|
||||
gizmoOperation = GizmoOperation::Rotate;
|
||||
} else if (ImGui::IsKeyPressed(ImGuiKey_R, false)) {
|
||||
gizmoOperation = GizmoOperation::Scale;
|
||||
}
|
||||
}
|
||||
|
||||
void ScenePanel::drawGizmo(const glm::mat4& view, float aspect) {
|
||||
sophia::Entity selected = context.selected;
|
||||
if (!selected) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool is2D = editorCamera.mode == EditorCamera::Mode::Scene2D;
|
||||
|
||||
// In 2D, only the axes that keep things in the XY plane.
|
||||
ImGuizmo::OPERATION operation{};
|
||||
float snapStep = 0.f;
|
||||
switch (gizmoOperation) {
|
||||
case GizmoOperation::Translate:
|
||||
operation = is2D ? ImGuizmo::TRANSLATE_X | ImGuizmo::TRANSLATE_Y
|
||||
: ImGuizmo::TRANSLATE;
|
||||
snapStep = TRANSLATE_SNAP;
|
||||
break;
|
||||
case GizmoOperation::Rotate:
|
||||
operation = is2D ? ImGuizmo::ROTATE_Z : ImGuizmo::ROTATE;
|
||||
snapStep = ROTATE_SNAP_DEGREES;
|
||||
break;
|
||||
case GizmoOperation::Scale:
|
||||
operation =
|
||||
is2D ? ImGuizmo::SCALE_X | ImGuizmo::SCALE_Y : ImGuizmo::SCALE;
|
||||
snapStep = SCALE_SNAP;
|
||||
break;
|
||||
}
|
||||
std::array<float, 3> snap{snapStep, snapStep, snapStep};
|
||||
bool snapping = ImGui::GetIO().KeyCtrl;
|
||||
|
||||
// ImGuizmo maps clip space to the screen the OpenGL way, with +y up, so it
|
||||
// needs the projection without the Y flip CameraComponent adds for Vulkan.
|
||||
glm::mat4 projection =
|
||||
camera.getComponent<sophia::CameraComponent>().getProjection(aspect);
|
||||
projection[1][1] *= -1.f;
|
||||
|
||||
auto& transform = selected.getComponent<sophia::TransformComponent>();
|
||||
glm::mat4 model = transform.mat4();
|
||||
|
||||
ImGuizmo::SetOrthographic(is2D);
|
||||
// Per entity, so a drag in progress doesn't carry over to a new selection.
|
||||
ImGuizmo::PushID(static_cast<int>(static_cast<sophia::u32>(selected)));
|
||||
bool changed = ImGuizmo::Manipulate(
|
||||
glm::value_ptr(view), glm::value_ptr(projection), operation,
|
||||
gizmoLocal ? ImGuizmo::LOCAL : ImGuizmo::WORLD, glm::value_ptr(model),
|
||||
nullptr, snapping ? snap.data() : nullptr);
|
||||
ImGuizmo::PopID();
|
||||
|
||||
if (changed) {
|
||||
decompose(model, transform);
|
||||
}
|
||||
}
|
||||
|
||||
void ScenePanel::drawGrid(ImVec2 origin, ImVec2 size) {
|
||||
constexpr float GRID_STEP = 40.0f;
|
||||
@@ -99,15 +390,55 @@ void ScenePanel::drawGrid(ImVec2 origin, ImVec2 size) {
|
||||
}
|
||||
}
|
||||
|
||||
void ScenePanel::handleInput(const ViewportTransform& viewport) {
|
||||
void ScenePanel::handleCameraInput(const sophia::FrameInfo& frameInfo,
|
||||
ImVec2 size) {
|
||||
const ImGuiIO& io = ImGui::GetIO();
|
||||
bool is3D = editorCamera.mode == EditorCamera::Mode::Scene3D;
|
||||
glm::vec2 mouseDelta{io.MouseDelta.x, io.MouseDelta.y};
|
||||
|
||||
if (ImGui::IsItemActivated()) {
|
||||
selected = pick(viewport.screenToWorld(io.MousePos));
|
||||
dragging = selected;
|
||||
// The button stays active while any of its mouse buttons is held, even
|
||||
// once the mouse leaves the panel.
|
||||
if (ImGui::IsItemActive()) {
|
||||
bool rightDown = ImGui::IsMouseDown(ImGuiMouseButton_Right);
|
||||
bool middleDown = ImGui::IsMouseDown(ImGuiMouseButton_Middle);
|
||||
|
||||
if (is3D && rightDown) {
|
||||
editorCamera.orbit(mouseDelta);
|
||||
|
||||
glm::vec3 direction{0.f};
|
||||
if (ImGui::IsKeyDown(ImGuiKey_D)) direction.x += 1.f;
|
||||
if (ImGui::IsKeyDown(ImGuiKey_A)) direction.x -= 1.f;
|
||||
if (ImGui::IsKeyDown(ImGuiKey_E)) direction.y += 1.f;
|
||||
if (ImGui::IsKeyDown(ImGuiKey_Q)) direction.y -= 1.f;
|
||||
if (ImGui::IsKeyDown(ImGuiKey_S)) direction.z += 1.f;
|
||||
if (ImGui::IsKeyDown(ImGuiKey_W)) direction.z -= 1.f;
|
||||
editorCamera.fly(direction, static_cast<float>(frameInfo.deltaTime));
|
||||
} else if (middleDown || (!is3D && rightDown)) {
|
||||
editorCamera.pan(mouseDelta, size.y);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ImGui::IsItemActive()) {
|
||||
if (ImGui::IsItemHovered() && io.MouseWheel != 0.f) {
|
||||
editorCamera.zoom(io.MouseWheel);
|
||||
}
|
||||
|
||||
if (ImGui::IsWindowFocused() && !io.WantTextInput &&
|
||||
ImGui::IsKeyPressed(ImGuiKey_F, false)) {
|
||||
frameSelection();
|
||||
}
|
||||
}
|
||||
|
||||
void ScenePanel::handleSelectionInput(const ViewportTransform& viewport) {
|
||||
const ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
if (ImGui::IsItemClicked(ImGuiMouseButton_Left)) {
|
||||
context.selected = pick(viewport.screenToRay(io.MousePos));
|
||||
// Dragging the selection around is 2D only; in 3D, use the gizmo.
|
||||
dragging =
|
||||
context.selected && editorCamera.mode == EditorCamera::Mode::Scene2D;
|
||||
}
|
||||
|
||||
if (!ImGui::IsItemActive() || !ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
|
||||
dragging = false;
|
||||
return;
|
||||
}
|
||||
@@ -118,13 +449,15 @@ void ScenePanel::handleInput(const ViewportTransform& viewport) {
|
||||
glm::vec2 delta =
|
||||
viewport.screenToWorld(io.MousePos) - viewport.screenToWorld(previous);
|
||||
|
||||
auto& transform = selected.getComponent<sophia::TransformComponent>();
|
||||
auto& transform =
|
||||
context.selected.getComponent<sophia::TransformComponent>();
|
||||
transform.position.x += delta.x;
|
||||
transform.position.y += delta.y;
|
||||
}
|
||||
}
|
||||
|
||||
void ScenePanel::drawSelection(const ViewportTransform& viewport) {
|
||||
sophia::Entity selected = context.selected;
|
||||
if (!selected) {
|
||||
return;
|
||||
}
|
||||
@@ -135,53 +468,136 @@ void ScenePanel::drawSelection(const ViewportTransform& viewport) {
|
||||
if (selected.hasComponent<sophia::SpriteComponent>()) {
|
||||
glm::mat4 model =
|
||||
selected.getComponent<sophia::SpriteComponent>().getModel(transform);
|
||||
|
||||
std::array<ImVec2, 4> corners;
|
||||
for (size_t i = 0; i < UNIT_QUAD.size(); i++) {
|
||||
corners[i] = viewport.worldToScreen(
|
||||
glm::vec3{model * glm::vec4{UNIT_QUAD[i], 0.f, 1.f}});
|
||||
const glm::vec2& from = UNIT_QUAD[i];
|
||||
const glm::vec2& to = UNIT_QUAD[(i + 1) % UNIT_QUAD.size()];
|
||||
viewport.drawLine(drawList, glm::vec3{model * glm::vec4{from, 0.f, 1.f}},
|
||||
glm::vec3{model * glm::vec4{to, 0.f, 1.f}},
|
||||
SELECTION_COLOR, 2.0f);
|
||||
}
|
||||
}
|
||||
|
||||
if (std::optional<sophia::AABB> bounds = getMeshBounds(selected)) {
|
||||
glm::mat4 model = transform.mat4();
|
||||
std::array<glm::vec3, 8> corners = bounds->getCorners();
|
||||
for (glm::vec3& corner : corners) {
|
||||
corner = glm::vec3{model * glm::vec4{corner, 1.f}};
|
||||
}
|
||||
// Corners whose indices differ in one bit share an edge.
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for (int bit : {1, 2, 4}) {
|
||||
if ((i & bit) == 0) {
|
||||
viewport.drawLine(drawList, corners[i], corners[i | bit],
|
||||
SELECTION_COLOR, 2.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
drawList->AddQuad(corners[0], corners[1], corners[2], corners[3],
|
||||
SELECTION_COLOR, 2.0f);
|
||||
}
|
||||
|
||||
// Mark the transform's position (the sprite's pivot), which is also all
|
||||
// there is to show for entities without a sprite, such as the camera.
|
||||
ImVec2 center = viewport.worldToScreen(transform.position);
|
||||
drawList->AddCircle(center, 4.0f, SELECTION_COLOR, 0, 2.0f);
|
||||
// there is to show for entities without a sprite or mesh, such as the
|
||||
// camera.
|
||||
if (std::optional<ImVec2> center =
|
||||
viewport.worldToScreen(transform.position)) {
|
||||
drawList->AddCircle(*center, 4.0f, SELECTION_COLOR, 0, 2.0f);
|
||||
}
|
||||
}
|
||||
|
||||
sophia::Entity ScenePanel::pick(glm::vec2 world) {
|
||||
sophia::Entity best;
|
||||
std::optional<std::tuple<sophia::s32, float>> bestOrder;
|
||||
void ScenePanel::frameSelection() {
|
||||
sophia::Entity selected = context.selected;
|
||||
if (!selected) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& transform = selected.getComponent<sophia::TransformComponent>();
|
||||
|
||||
// The selection's corners in world space, from its mesh or sprite.
|
||||
std::vector<glm::vec3> points;
|
||||
if (std::optional<sophia::AABB> bounds = getMeshBounds(selected)) {
|
||||
glm::mat4 model = transform.mat4();
|
||||
for (const glm::vec3& corner : bounds->getCorners()) {
|
||||
points.push_back(glm::vec3{model * glm::vec4{corner, 1.f}});
|
||||
}
|
||||
}
|
||||
if (selected.hasComponent<sophia::SpriteComponent>()) {
|
||||
glm::mat4 model =
|
||||
selected.getComponent<sophia::SpriteComponent>().getModel(transform);
|
||||
for (const glm::vec2& corner : UNIT_QUAD) {
|
||||
points.push_back(glm::vec3{model * glm::vec4{corner, 0.f, 1.f}});
|
||||
}
|
||||
}
|
||||
|
||||
if (points.empty()) {
|
||||
// Nothing with a size: frame a unit around its position.
|
||||
editorCamera.frame(transform.position, 0.5f);
|
||||
return;
|
||||
}
|
||||
|
||||
glm::vec3 min = points.front();
|
||||
glm::vec3 max = points.front();
|
||||
for (const glm::vec3& point : points) {
|
||||
min = glm::min(min, point);
|
||||
max = glm::max(max, point);
|
||||
}
|
||||
editorCamera.frame((min + max) * 0.5f, glm::length(max - min) * 0.5f);
|
||||
}
|
||||
|
||||
std::optional<sophia::AABB> ScenePanel::getMeshBounds(sophia::Entity entity) {
|
||||
if (!entity.hasComponent<sophia::MeshComponent>()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
sophia::MeshHandle mesh = entity.getComponent<sophia::MeshComponent>().handle;
|
||||
if (!resources.isAlive(mesh)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return resources.getBounds(mesh);
|
||||
}
|
||||
|
||||
sophia::Entity ScenePanel::pick(const Ray& ray) {
|
||||
sophia::Scene& scene = context.getScene();
|
||||
|
||||
// Meshes are opaque and depth tested, so the nearest one hides anything
|
||||
// behind it.
|
||||
sophia::Entity nearestMesh;
|
||||
float meshDistance = std::numeric_limits<float>::infinity();
|
||||
scene.forEachEntity([&](sophia::Entity entity) {
|
||||
std::optional<sophia::AABB> bounds = getMeshBounds(entity);
|
||||
if (!bounds) {
|
||||
return;
|
||||
}
|
||||
glm::mat4 model = entity.getComponent<sophia::TransformComponent>().mat4();
|
||||
std::optional<float> distance =
|
||||
intersectBox(ray.origin, ray.direction, model, *bounds);
|
||||
if (distance && *distance < meshDistance) {
|
||||
nearestMesh = entity;
|
||||
meshDistance = *distance;
|
||||
}
|
||||
});
|
||||
|
||||
// Sprites in front of that mesh draw over it, in the renderer's order:
|
||||
// higher sortOrder on top, then nearer.
|
||||
sophia::Entity topSprite;
|
||||
std::optional<std::tuple<sophia::s32, float>> topOrder;
|
||||
scene.forEachEntity([&](sophia::Entity entity) {
|
||||
if (!entity.hasComponent<sophia::SpriteComponent>()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& transform = entity.getComponent<sophia::TransformComponent>();
|
||||
const auto& sprite = entity.getComponent<sophia::SpriteComponent>();
|
||||
|
||||
glm::mat4 model = sprite.getModel(transform);
|
||||
// Zero size or scale; nothing to hit.
|
||||
if (glm::determinant(model) == 0.f) {
|
||||
glm::mat4 model =
|
||||
sprite.getModel(entity.getComponent<sophia::TransformComponent>());
|
||||
std::optional<float> distance =
|
||||
intersectQuad(ray.origin, ray.direction, model);
|
||||
if (!distance || *distance > meshDistance) {
|
||||
return;
|
||||
}
|
||||
|
||||
glm::vec4 local =
|
||||
glm::inverse(model) * glm::vec4{world, transform.position.z, 1.f};
|
||||
if (local.x < 0.f || local.x > 1.f || local.y < 0.f || local.y > 1.f) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Same ordering the renderer draws in, so this is the one on top.
|
||||
std::tuple order{sprite.sortOrder, transform.position.z};
|
||||
if (!bestOrder || order >= *bestOrder) {
|
||||
best = entity;
|
||||
bestOrder = order;
|
||||
// Negated distance, so nearer compares greater.
|
||||
std::tuple order{sprite.sortOrder, -*distance};
|
||||
if (!topOrder || order > *topOrder) {
|
||||
topSprite = entity;
|
||||
topOrder = order;
|
||||
}
|
||||
});
|
||||
|
||||
return best;
|
||||
return topSprite ? topSprite : nearestMesh;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <sophia/aabb.hpp>
|
||||
#include <sophia/entity.hpp>
|
||||
#include <sophia/frame_info.hpp>
|
||||
#include <sophia/handles.hpp>
|
||||
#include <sophia/math.hpp>
|
||||
#include <sophia/scene.hpp>
|
||||
#include <sophia/resource_registry_ref.hpp>
|
||||
#include <sophia/scene_view_registry.hpp>
|
||||
#include <sophia/ui/panel.hpp>
|
||||
#include <sophia/ui/ui.hpp>
|
||||
|
||||
#include "../editor_camera.hpp"
|
||||
#include "../editor_context.hpp"
|
||||
|
||||
/**
|
||||
* Shows the scene through its active camera. Click a sprite to select it,
|
||||
* drag it to move it (in Move mode).
|
||||
* Shows the active scene through the editor's own camera.
|
||||
*
|
||||
* Picking and moving are 2D: they assume an orthographic camera rotated only
|
||||
* around Z, and work in the z = 0 plane.
|
||||
* 3D: right-drag orbits, middle-drag pans, the wheel zooms, right-drag with
|
||||
* WASD/QE flies. 2D: right- or middle-drag pans, the wheel zooms. F frames
|
||||
* the selection in both.
|
||||
*
|
||||
* The selection gets a transform gizmo: W moves, E rotates, R scales, and
|
||||
* holding Ctrl snaps. Clicking selects the mesh or sprite under the mouse;
|
||||
* in 2D, dragging it also moves it.
|
||||
*/
|
||||
class ScenePanel : public sophia::Panel {
|
||||
public:
|
||||
ScenePanel(sophia::UI& ui, sophia::RenderTargetHandle target,
|
||||
sophia::Scene& scene, sophia::Entity& selected)
|
||||
: Panel{{.name = "Scene",
|
||||
.windowFlags = ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse}},
|
||||
// Renders into `target` through `view`, a SceneView this panel keeps
|
||||
// pointed at the active scene. `camera` is the entity the view looks
|
||||
// through; it belongs to a scene of the editor's own, and this panel
|
||||
// drives it from its EditorCamera.
|
||||
ScenePanel(sophia::UI& ui, sophia::ResourceRegistryRef resources,
|
||||
EditorContext& context, sophia::SceneViewRegistry& sceneViews,
|
||||
sophia::SceneViewHandle view, sophia::RenderTargetHandle target,
|
||||
sophia::Entity camera)
|
||||
: Panel{{.name = "Scene", .windowFlags = WINDOW_FLAGS}},
|
||||
ui{ui},
|
||||
resources{resources},
|
||||
context{context},
|
||||
sceneViews{sceneViews},
|
||||
view{view},
|
||||
target{target},
|
||||
scene{scene},
|
||||
selected{selected} {}
|
||||
camera{camera} {}
|
||||
|
||||
void onUpdate(const sophia::FrameInfo& frameInfo) override;
|
||||
|
||||
private:
|
||||
static constexpr ImGuiWindowFlags WINDOW_FLAGS =
|
||||
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse;
|
||||
|
||||
enum class GizmoOperation { Translate, Rotate, Scale };
|
||||
|
||||
// A half-line in world space. `direction` is normalized, so distances
|
||||
// along it are in world units.
|
||||
struct Ray {
|
||||
glm::vec3 origin;
|
||||
glm::vec3 direction;
|
||||
};
|
||||
|
||||
// Where the scene image is on screen this frame, and the camera looking
|
||||
// through it.
|
||||
struct ViewportTransform {
|
||||
@@ -38,22 +67,57 @@ class ScenePanel : public sophia::Panel {
|
||||
glm::mat4 viewProjection;
|
||||
glm::mat4 inverseViewProjection;
|
||||
|
||||
ImVec2 worldToScreen(const glm::vec3& world) const;
|
||||
// Null for points behind the camera.
|
||||
std::optional<ImVec2> worldToScreen(const glm::vec3& world) const;
|
||||
// Where `screen` is on the camera's near plane. Only the x and y, which
|
||||
// is all that's needed in 2D.
|
||||
glm::vec2 screenToWorld(const ImVec2& screen) const;
|
||||
// From the near plane through `screen`, into the scene.
|
||||
Ray screenToRay(const ImVec2& screen) const;
|
||||
|
||||
// Draws a world-space line, cut off where it passes behind the camera.
|
||||
void drawLine(ImDrawList* drawList, const glm::vec3& a, const glm::vec3& b,
|
||||
ImU32 color, float thickness) const;
|
||||
|
||||
private:
|
||||
ImVec2 clipToScreen(const glm::vec4& clip) const;
|
||||
};
|
||||
|
||||
void drawToolbar();
|
||||
void drawGrid(ImVec2 origin, ImVec2 size);
|
||||
void handleInput(const ViewportTransform& viewport);
|
||||
void handleCameraInput(const sophia::FrameInfo& frameInfo, ImVec2 size);
|
||||
void handleSelectionInput(const ViewportTransform& viewport);
|
||||
void handleGizmoHotkeys();
|
||||
// Draws the transform gizmo on the selection, and applies any change made
|
||||
// with it. `view` is the editor camera's, and `aspect` the viewport's
|
||||
// width over its height.
|
||||
void drawGizmo(const glm::mat4& view, float aspect);
|
||||
void drawSelection(const ViewportTransform& viewport);
|
||||
void frameSelection();
|
||||
|
||||
// The top-most sprite under `world`, or a null Entity.
|
||||
sophia::Entity pick(glm::vec2 world);
|
||||
// The bounds of the entity's mesh in the mesh's own space, if it has one.
|
||||
std::optional<sophia::AABB> getMeshBounds(sophia::Entity entity);
|
||||
|
||||
// The entity drawn in front along `ray`, or a null Entity. Only meshes and
|
||||
// sprites can be hit.
|
||||
sophia::Entity pick(const Ray& ray);
|
||||
|
||||
sophia::UI& ui;
|
||||
sophia::ResourceRegistryRef resources;
|
||||
EditorContext& context;
|
||||
sophia::SceneViewRegistry& sceneViews;
|
||||
sophia::SceneViewHandle view;
|
||||
sophia::RenderTargetHandle target;
|
||||
sophia::Scene& scene;
|
||||
sophia::Entity& selected;
|
||||
sophia::Entity camera;
|
||||
|
||||
EditorCamera editorCamera;
|
||||
|
||||
GizmoOperation gizmoOperation = GizmoOperation::Translate;
|
||||
// Whether the gizmo works along the entity's own axes or the world's.
|
||||
bool gizmoLocal = true;
|
||||
// Whether the viewport button was active last frame, i.e. a camera drag or
|
||||
// click is under way, which the gizmo mustn't interrupt.
|
||||
bool viewportActive = false;
|
||||
|
||||
bool showGrid = true;
|
||||
// Set while a drag that started on the selected entity is moving it.
|
||||
|
||||
Reference in New Issue
Block a user