Skip to content

Commit

Permalink
[#179]: Move Gizmo logic to separate struct
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Jan 15, 2024
1 parent 38da5f8 commit dfa1079
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 86 deletions.
Binary file added assets/images/arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/images/arrow_right.png
Binary file not shown.
Binary file removed assets/images/arrow_up.png
Binary file not shown.
Binary file added assets/images/scale_slider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 28 additions & 55 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,27 @@ Editor::ShowCursor(bool choice)
}

void
Editor::HandleCamera()
Editor::KeyCallback(KeyEvent& event)
{
auto cameraMoveBy = glm::vec2();

if (!EditorGUI::IsBlockingEvents() && levelLoaded_)
if (event.action_ == GLFW_PRESS)
{
if (InputManager::CheckKeyPressed(GLFW_KEY_W))
{
cameraMoveBy += glm::vec2(0.0f, -1.0f);
}
if (InputManager::CheckKeyPressed(GLFW_KEY_S))
{
cameraMoveBy += glm::vec2(0.0f, 1.0f);
}
if (InputManager::CheckKeyPressed(GLFW_KEY_A))
{
cameraMoveBy += glm::vec2(-1.0f, 0.0f);
}
if (InputManager::CheckKeyPressed(GLFW_KEY_D))
{
cameraMoveBy += glm::vec2(1.0f, 0);
}
if (InputManager::CheckKeyPressed(GLFW_KEY_SPACE))
if (currentEditorObjectSelected_ != Object::INVALID_ID
or currentSelectedGameObject_ != Object::INVALID_ID)
{
camera_.SetCameraAtPosition({0.0f, 0.0f, 0.0f});
if (event.key_ == GLFW_KEY_T)
{
gizmo_.SwitchToTranslate();
}
if (event.key_ == GLFW_KEY_R)
{
gizmo_.SwitchToRotate();
}
if (event.key_ == GLFW_KEY_S)
{
gizmo_.SwitchToScale();
}
}

camera_.Move(glm::vec3(cameraMoveBy, 0.0f));
}
}

void
Editor::KeyCallback(KeyEvent& event)
{
if (event.action_ == GLFW_PRESS)
{
if (IsAnyObjectSelected())
{
ACTION action = ACTION::NONE;
Expand Down Expand Up @@ -211,6 +196,8 @@ Editor::CursorPositionCallback(CursorPositionEvent& event)
}
else
{
gizmo_.CheckHovered(camera_.GetPosition(), ScreenToGlobal(currentCursorPosition));

ShowCursor(true);
}

Expand Down Expand Up @@ -409,22 +396,8 @@ Editor::HandleGameObjectSelected(Object::ID newSelectedGameObject, bool groupSel

auto& gameObject =
dynamic_cast< GameObject& >(currentLevel_->GetObjectRef(currentSelectedGameObject_));
gizmoCenter_.SetInitialPosition(
glm::vec3{gameObject.GetCenteredPosition(), 0.0f});
gizmoCenter_.SetSize(gameObject.GetSize() / 7);
gizmoCenter_.SetColor({0.3f, 0.3f, 0.3f, 1.0f});

gizmoUp_.SetSize({gizmoCenter_.GetSize().x * 2.0f, gameObject.GetSize().y / 3});
gizmoUp_.SetInitialPosition(
gizmoCenter_.GetPosition()
+ glm::vec3{0.0f, gizmoCenter_.GetSize().y / 2.0f + gizmoUp_.GetSize().y / 2, 0.0f});
gizmoUp_.SetColor({0.0f, 0.3f, 0.0f, 1.0f});

gizmoSide_.SetSize({gameObject.GetSize().x / 3, gizmoCenter_.GetSize().y * 2.0f});
gizmoSide_.SetInitialPosition(gizmoCenter_.GetPosition() +
glm::vec3{glm::vec2(gizmoCenter_.GetSize().x / 2 + gizmoSide_.GetSize().x / 2, 0.0f),
0.0f});
gizmoSide_.SetColor({0.3f, 0.0f, 0.0f, 1.0f});
gizmo_.NewObjectSelected(gameObject.GetCenteredPosition());


// Make sure to render animation points if needed
Expand All @@ -446,6 +419,7 @@ Editor::HandleGameObjectSelected(Object::ID newSelectedGameObject, bool groupSel
}

gui_.ObjectSelected(currentSelectedGameObject_);
gizmo_.Show();
}

movementOnGameObject_ = !fromGUI;
Expand Down Expand Up @@ -563,6 +537,8 @@ Editor::UnselectGameObject(Object::ID object, bool groupSelect)
selectedObjects_.erase(it);
}
}

gizmo_.Hide();
}

void
Expand All @@ -578,6 +554,8 @@ Editor::HandleEditorObjectSelected(EditorObject& newSelectedEditorObject, bool f
movementOnEditorObject_ = !fromGUI;

newSelectedEditorObject.SetObjectSelected();

gizmo_.Show();
}

EditorObject&
Expand Down Expand Up @@ -605,6 +583,8 @@ Editor::UnselectEditorObject(Object::ID object)
auto& editorObject = GetEditorObjectRef(object);
editorObject.SetObjectUnselected();
currentEditorObjectSelected_ = Object::INVALID_ID;

gizmo_.Hide();
}

void
Expand Down Expand Up @@ -776,10 +756,7 @@ Editor::Render(VkCommandBuffer cmdBuffer)

currentLevel_->GetSprite().Render();

gizmoCenter_.Render();
gizmoUp_.Render();
gizmoSide_.Render();

gizmo_.Render();
DrawAnimationPoints();

currentLevel_->RenderGameObjects();
Expand Down Expand Up @@ -1079,9 +1056,7 @@ Editor::LoadLevel(const std::string& levelPath)

window_.MakeFocus();

gizmoCenter_.SetSpriteTextured(glm::vec3{}, {}, "rounded_square.png");
gizmoUp_.SetSpriteTextured(glm::vec3{}, {}, "arrow_up.png");
gizmoSide_.SetSpriteTextured(glm::vec3{}, {}, "arrow_right.png");
gizmo_.Initialize();
}

SetupRendererData();
Expand Down Expand Up @@ -1252,8 +1227,6 @@ Editor::SetLockAnimationPoints(bool lock)
void
Editor::Update()
{
HandleCamera();

if (animateGameObject_ && currentSelectedGameObject_ != Object::INVALID_ID)
{
auto& objectBase = currentLevel_->GetObjectRef(currentSelectedGameObject_);
Expand Down
8 changes: 2 additions & 6 deletions editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "application.hpp"
#include "editor_object.hpp"
#include "gizmo.hpp"
#include "gui/editor_gui.hpp"
#include "level.hpp"
#include "logger.hpp"
Expand Down Expand Up @@ -207,9 +208,6 @@ class Editor : public Application
[[nodiscard]] bool
IsRunning() const override;

void
HandleCamera();

void
HandleMouseDrag(const glm::vec2& currentCursorPos, const glm::vec2& axis);

Expand Down Expand Up @@ -297,9 +295,7 @@ class Editor : public Application
std::future< void > updateReady_;
std::future< void > renderReady_;

renderer::Sprite gizmoCenter_ = {};
renderer::Sprite gizmoUp_ = {};
renderer::Sprite gizmoSide_ = {};
Gizmo gizmo_ = {};
};

} // namespace looper
97 changes: 97 additions & 0 deletions editor/gizmo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "gizmo.hpp"

namespace looper {

void
Gizmo::Initialize()
{
gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "rounded_square.png");
gizmoCenter_.SetColor({0.3f, 0.3f, 0.3f, 1.0f});

gizmoUp_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png");
gizmoUp_.Rotate(90.0f, renderer::Sprite::RotationType::DEGREES);
gizmoUp_.SetColor({0.0f, 1.0f, 0.0f, 1.0f});

gizmoSide_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png");
gizmoSide_.SetColor({1.0f, 0.0f, 0.0f, 1.0f});
}

void
Gizmo::Show()
{
gizmoCenter_.Show();
gizmoSide_.Show();
gizmoUp_.Show();
}
void
Gizmo::Hide()
{
gizmoCenter_.Hide();
gizmoSide_.Hide();
gizmoUp_.Hide();
}
void
Gizmo::Render()
{
gizmoCenter_.Render();
gizmoSide_.Render();
gizmoUp_.Render();
}
void
Gizmo::NewObjectSelected(const glm::vec2& centeredPos)
{
gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, 0.0f});

gizmoUp_.SetInitialPosition(
gizmoCenter_.GetPosition()
+ glm::vec3{0.0f, gizmoCenter_.GetSize().y / 2.0f + gizmoUp_.GetSize().x / 1.85f, 0.0f});


gizmoSide_.SetInitialPosition(
gizmoCenter_.GetPosition()
+ glm::vec3{glm::vec2(gizmoCenter_.GetSize().x / 2.0f + gizmoSide_.GetSize().x / 1.85f, 0.0f),
0.0f});
}

void
Gizmo::CheckHovered(const glm::vec3& cameraPos, const glm::vec2& globalPosition)
{
bool gizmoTouched = false;
auto checkGizmo = [&gizmoTouched, cameraPos, globalPosition](auto& gizmo) {
if (not gizmoTouched and gizmo.CheckIfCollidedScreenPosion(cameraPos, globalPosition))
{
gizmo.SetSize(gizmo.initialSize_ * 1.1f);
gizmoTouched = true;
}
else
{
gizmo.SetSize(gizmo.initialSize_);
}
};

checkGizmo(gizmoCenter_);
checkGizmo(gizmoSide_);
checkGizmo(gizmoUp_);
}
void
Gizmo::SwitchToScale()
{
gizmoSide_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("scale_slider.png")->GetID());
gizmoUp_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("scale_slider.png")->GetID());
}
void
Gizmo::SwitchToRotate()
{
}
void
Gizmo::SwitchToTranslate()
{
gizmoSide_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("arrow.png")->GetID());
gizmoUp_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("arrow.png")->GetID());
}

} // namespace looper
31 changes: 31 additions & 0 deletions editor/gizmo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "renderer/sprite.hpp"

namespace looper {

struct Gizmo
{
void
Initialize();
void
Show();
void
Hide();
void
Render();
void
NewObjectSelected(const glm::vec2& centeredPos);

void
CheckHovered(const glm::vec3& cameraPos, const glm::vec2& globalPosition);
void
SwitchToScale();
void
SwitchToRotate();
void
SwitchToTranslate();

renderer::Sprite gizmoCenter_ = {};
renderer::Sprite gizmoUp_ = {};
renderer::Sprite gizmoSide_ = {};
};
} // namespace looper
2 changes: 1 addition & 1 deletion engine/game/game_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ GameObject::Setup(Application* application, const glm::vec3& position, const glm
bool
GameObject::CheckIfCollidedScreenPosion(const glm::vec2& screenPosition) const
{
const renderer::CollisionCamera camera(appHandle_->GetCamera().GetPosition(), this);
const renderer::CollisionCamera camera(appHandle_->GetCamera().GetPosition(), &sprite_);
return camera.CheckCollision(appHandle_->ScreenToGlobal(screenPosition));
}

Expand Down
10 changes: 5 additions & 5 deletions engine/renderer/camera/collision_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#include "collision_camera.hpp"
#include "camera.hpp"
#include "game_object.hpp"
#include "sprite.hpp"

#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/transform.hpp>

namespace looper::renderer {

CollisionCamera::CollisionCamera(const glm::vec3& position, const GameObject* obj)
CollisionCamera::CollisionCamera(const glm::vec3& position, const Sprite* sprite)
: position_(position),
upVector_(Camera::originalUpVec),
lookAtDirection_(Camera::originalLookAt),
object_(obj)
sprite_(sprite)
{
Rotate(object_->GetSprite().GetRotation());
Rotate(sprite_->GetRotation());
}

bool
CollisionCamera::CheckCollision(const glm::vec2& globalVec) const
{
bool collided = false;

const auto boundingRectangle = object_->GetSprite().GetTransformedRectangle();
const auto boundingRectangle = sprite_->GetTransformedRectangle();

const auto transformed0 = viewMatrix_ * glm::vec4(boundingRectangle[0], 0.0f, 1.0f);
const auto transformed1 = viewMatrix_ * glm::vec4(boundingRectangle[1], 0.0f, 1.0f);
Expand Down
9 changes: 3 additions & 6 deletions engine/renderer/camera/collision_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

#include <glm/glm.hpp>

namespace looper {
class GameObject;
}

namespace looper::renderer {
class Sprite;

class CollisionCamera
{
public:
CollisionCamera(const glm::vec3& position, const GameObject* obj);
CollisionCamera(const glm::vec3& position, const Sprite* obj);

[[nodiscard]] bool
CheckCollision(const glm::vec2& globalVec) const;
Expand All @@ -28,7 +25,7 @@ class CollisionCamera
glm::vec3 lookAtDirection_ = {};

glm::mat4 viewMatrix_ = {};
const GameObject* object_ = nullptr;
const Sprite* sprite_ = nullptr;
};

} // namespace looper::renderer
Loading

0 comments on commit dfa1079

Please sign in to comment.