Skip to content

Commit

Permalink
[Editor] Added a fully functional transformation gizmo (position, rot…
Browse files Browse the repository at this point in the history
…ation, scale)
  • Loading branch information
PanosK92 committed Nov 30, 2022
1 parent edd11f5 commit 5f7784f
Show file tree
Hide file tree
Showing 41 changed files with 6,690 additions and 1,546 deletions.
1 change: 1 addition & 0 deletions editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "Widgets/Profiler.h"
#include "Widgets/RenderOptions.h"
#include "Widgets/TextureViewer.h"
#include "Rendering/Renderer_Definitions.h"
//==============================================

//= NAMESPACES =====
Expand Down
12 changes: 5 additions & 7 deletions editor/ImGui/ImGuiExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ using namespace std;
using namespace Spartan;
//======================

Editor* EditorHelper::editor = nullptr;
Context* EditorHelper::context = nullptr;
World* EditorHelper::world = nullptr;
Renderer* EditorHelper::renderer = nullptr;
Input* EditorHelper::input = nullptr;
function<void()> EditorHelper::on_entity_selected = nullptr;
weak_ptr<Entity> EditorHelper::selected_entity;
Editor* EditorHelper::editor = nullptr;
Context* EditorHelper::context = nullptr;
World* EditorHelper::world = nullptr;
Renderer* EditorHelper::renderer = nullptr;
Input* EditorHelper::input = nullptr;
45 changes: 7 additions & 38 deletions editor/ImGui/ImGuiExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#pragma once

//= INCLUDES =====================================
//= INCLUDES ===============================
#include <string>
#include <variant>
#include <chrono>
Expand All @@ -33,12 +33,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "Input/Input.h"
#include "World/World.h"
#include "World/Components/Camera.h"
#include "World/TransformHandle/TransformHandle.h"
#include "Display/Display.h"
#include "../WidgetsDeferred/IconProvider.h"
#include "Source/imgui_internal.h"
#include "../Editor.h"
//================================================
//==========================================

class EditorHelper
{
Expand Down Expand Up @@ -82,41 +81,11 @@ class EditorHelper
});
}

static void PickEntity()
{
// If the transform handle hasn't finished editing don't do anything.
if (world->GetTransformHandle()->IsEditing())
return;

// Get camera
std::shared_ptr<Spartan::Camera> camera = renderer->GetCamera();
if (!camera)
return;

// Pick the world
std::shared_ptr<Spartan::Entity> entity;
camera->Pick(entity);

// Set the transform handle to the selected entity
SetSelectedEntity(entity);

// Fire callback
on_entity_selected();
}

static void SetSelectedEntity(const std::shared_ptr<Spartan::Entity>& entity)
{
// keep returned entity instead as the transform handle can decide to reject it
selected_entity = world->GetTransformHandle()->SetSelectedEntity(entity);
}

static Editor* editor;
static Spartan::Context* context;
static Spartan::World* world;
static Spartan::Renderer* renderer;
static Spartan::Input* input;
static std::function<void()> on_entity_selected;
static std::weak_ptr<Spartan::Entity> selected_entity;
static Editor* editor;
static Spartan::Context* context;
static Spartan::World* world;
static Spartan::Renderer* renderer;
static Spartan::Input* input;
};

namespace ImGui_SP
Expand Down
120 changes: 120 additions & 0 deletions editor/ImGui/Implementation/ImGui_TransformGizmo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright(c) 2016-2022 Panos Karabelas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

//= INCLUDES ===========================
#include "../Source/ImGuizmo/ImGuizmo.h"
#include "../Source/imgui.h"
#include "World/Components/Transform.h"
#include "World/Entity.h"
#include "Context.h"
#include "Rendering/Renderer.h"
#include "Input/Input.h"
//======================================

namespace ImGui::TransformGizmo
{
static void apply_style()
{
ImGuizmo::Style& style = ImGuizmo::GetStyle();
style.TranslationLineThickness = 6.0f;
style.TranslationLineArrowSize = 10.0f;
style.RotationLineThickness = 4.0f;
style.RotationOuterLineThickness = 5.0f;
style.ScaleLineThickness = 6.0f;
style.ScaleLineCircleSize = 6.0f;
style.HatchedAxisLineThickness = 6.0f;
style.CenterCircleSize = 6.0f;
}

static void tick(Spartan::Context* context)
{
std::shared_ptr<Spartan::Camera> camera = context->GetSystem<Spartan::Renderer>()->GetCamera();
if (!camera)
return;

// Get selected entity
std::shared_ptr<Spartan::Entity> entity = camera->GetSelectedEntity();

// Enable/disable gizmo
ImGuizmo::Enable(entity != nullptr);
if (!entity)
return;

// Switch between position, rotation and scale operations, with W, E and R respectively
static ImGuizmo::OPERATION transform_operation = ImGuizmo::TRANSLATE;
if (!camera->IsControledInFirstPerson())
{
if (context->GetSystem<Spartan::Input>()->GetKeyDown(Spartan::KeyCode::W))
{
transform_operation = ImGuizmo::TRANSLATE;
}
else if (context->GetSystem<Spartan::Input>()->GetKeyDown(Spartan::KeyCode::E))
{
transform_operation = ImGuizmo::ROTATE;
}
else if (context->GetSystem<Spartan::Input>()->GetKeyDown(Spartan::KeyCode::R))
{
transform_operation = ImGuizmo::SCALE;
}
}

ImGuizmo::MODE transform_space = ImGuizmo::WORLD;

// Get some data
const Spartan::Math::Matrix& matrix_projection = camera->GetProjectionMatrix().Transposed();
const Spartan::Math::Matrix& matrix_view = camera->GetViewMatrix().Transposed();
Spartan::Transform* transform = entity->GetComponent<Spartan::Transform>();

// Begin
const bool is_orthographic = false;
ImGuizmo::SetOrthographic(is_orthographic);
ImGuizmo::BeginFrame();

// Map transform to ImGuizmo
float matrix_delta[16];
float translation[3] = { transform->GetPosition().x, transform->GetPosition().y, transform->GetPosition().z };
float rotation[3] = { transform->GetRotation().ToEulerAngles().x, transform->GetRotation().ToEulerAngles().y, transform->GetRotation().ToEulerAngles().z };
float scale[3] = { transform->GetScale().x, transform->GetScale().y, transform->GetScale().z };
ImGuizmo::RecomposeMatrixFromComponents(&translation[0], rotation, scale, &matrix_delta[0]);

// Set viewport rectangle
ImGuizmo::SetDrawlist();
ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());
ImGuizmo::Manipulate(&matrix_view.m00, &matrix_projection.m00, transform_operation, transform_space, &matrix_delta[0], 0, 0);

// Map ImGuizmo to transform
if (ImGuizmo::IsUsing())
{
ImGuizmo::DecomposeMatrixToComponents(&matrix_delta[0], translation, rotation, scale);

transform->SetPosition(Spartan::Math::Vector3(translation[0], translation[1], translation[2]));
transform->SetRotation(Spartan::Math::Quaternion::FromEulerAngles(rotation[0], rotation[1], rotation[2]));
transform->SetScale(Spartan::Math::Vector3(scale[0], scale[1], scale[2]));
}
}

static bool allow_picking()
{
return !ImGuizmo::IsOver() && !ImGuizmo::IsUsing();
}
}

0 comments on commit 5f7784f

Please sign in to comment.