Skip to content

Commit

Permalink
[#190]: Make gizmo scale with camera zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Feb 6, 2024
1 parent 6ca2d86 commit 3977f95
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
2 changes: 2 additions & 0 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Editor::MouseScrollCallback(MouseScrollEvent& event)
if (!playGame_ && !EditorGUI::IsBlockingEvents() && levelLoaded_)
{
camera_.Zoom(static_cast< float >(event.xOffset_ + event.yOffset_));
gizmo_.Zoom(static_cast< int32_t >(-event.yOffset_));

event.handled_ = true;
}
}
Expand Down
75 changes: 59 additions & 16 deletions editor/gizmo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace looper {
void
Gizmo::Initialize()
{
gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "centered_move.png",
renderer::SpriteType::alwaysOnTop);
gizmoCenter_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {16, 16},
"centered_move.png", renderer::SpriteType::alwaysOnTop);
gizmoCenter_.SetColor({1.0f, 1.0f, 1.0f, 1.0f});

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

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

Expand Down Expand Up @@ -61,7 +61,47 @@ Gizmo::Update(const glm::vec2& centeredPos, float rotation)
gizmoCenter_.Rotate(currentRotation_);
}

gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, renderer::LAYER_0});
gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, renderer::LAYER_1});

AdjustSize();
}

void
Gizmo::Move(const glm::vec2& moveBy)
{
gizmoCenter_.Translate(moveBy);
gizmoUp_.Translate(moveBy);
gizmoSide_.Translate(moveBy);
}

void
Gizmo::Zoom(int32_t zoomVal)
{
zoomLevel_ = glm::min(zoomLevel_ + zoomVal, 18);

AdjustSize();
}

void
Gizmo::AdjustSize()
{
float scaleFactor = 1.0f;
if (zoomLevel_ >= 0)
{
scaleFactor = 20.0f / (20.0f - zoomLevel_);
}

gizmoCenter_.Scale(
(currentState_ == GizmoState::rotate ? centerInitialSize_.second : centerInitialSize_.first)
* scaleFactor);
centerCurrentSize_ = {centerInitialSize_.first * scaleFactor,
centerInitialSize_.second * scaleFactor};

upCurrentSize_ = glm::vec2{upInitialSize_.x * scaleFactor, upInitialSize_.y * scaleFactor};
gizmoUp_.Scale(upCurrentSize_);

sideCurrentSize_ = glm::vec2{sideInitialSize_.x * scaleFactor, sideInitialSize_.y * scaleFactor};
gizmoSide_.Scale(sideCurrentSize_);

gizmoUp_.SetInitialPosition(
gizmoCenter_.GetPosition()
Expand All @@ -74,12 +114,11 @@ Gizmo::Update(const glm::vec2& centeredPos, float rotation)
0.0f});
}

void
Gizmo::Move(const glm::vec2& moveBy)

glm::vec2
Gizmo::Position() const
{
gizmoCenter_.Translate(moveBy);
gizmoUp_.Translate(moveBy);
gizmoSide_.Translate(moveBy);
return gizmoCenter_.GetPosition();
}

void
Expand All @@ -103,11 +142,11 @@ Gizmo::CheckHovered(const glm::vec3& cameraPos, const glm::vec2& globalPosition)
};

checkGizmo(gizmoCenter_,
currentState_ == GizmoState::rotate ? centerInitialSize_.second
: centerInitialSize_.first,
currentState_ == GizmoState::rotate ? centerCurrentSize_.second
: centerCurrentSize_.first,
GizmoPart::center);
checkGizmo(gizmoSide_, sideInitialSize_, GizmoPart::hotizontal);
checkGizmo(gizmoUp_, upInitialSize_, GizmoPart::vertical);
checkGizmo(gizmoSide_, sideCurrentSize_, GizmoPart::hotizontal);
checkGizmo(gizmoUp_, upCurrentSize_, GizmoPart::vertical);

mouseOnGizmo_ = gizmoTouched;
}
Expand All @@ -128,6 +167,8 @@ Gizmo::SwitchToScale()
renderer::TextureLibrary::GetTexture("scale_slider.png")->GetID());
gizmoUp_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("scale_slider.png")->GetID());

AdjustSize();
}
void
Gizmo::SwitchToRotate()
Expand All @@ -139,15 +180,17 @@ Gizmo::SwitchToRotate()

gizmoCenter_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("rotate.png")->GetID());
gizmoCenter_.SetSize(centerInitialSize_.second);

gizmoCenter_.Rotate(currentRotation_);

AdjustSize();
}
void
Gizmo::SwitchToTranslate()
{
currentState_ = GizmoState::translate;

gizmoCenter_.SetSize(centerInitialSize_.first);
AdjustSize();
gizmoCenter_.Rotate(0.0f);
gizmoSide_.Show();
gizmoUp_.Show();
Expand Down
14 changes: 14 additions & 0 deletions editor/gizmo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Gizmo
Update(const glm::vec2& centeredPos, float rotation);
void
Move(const glm::vec2& moveBy);
void
Zoom(int32_t zoomVal);

void
CheckHovered(const glm::vec3& cameraPos, const glm::vec2& globalPosition);
Expand All @@ -43,6 +45,12 @@ class Gizmo
SwitchToRotate();
void
SwitchToTranslate();
glm::vec2
Position() const;

private:
void
AdjustSize();

public:
GizmoState currentState_ = GizmoState::translate;
Expand All @@ -54,10 +62,16 @@ class Gizmo
renderer::Sprite gizmoUp_ = {};
renderer::Sprite gizmoSide_ = {};

int32_t zoomLevel_ = 0;
float currentRotation_ = 0.0f;
// <default, rotate>
std::pair< glm::vec2, glm::vec2 > centerInitialSize_ = {};
glm::vec2 upInitialSize_ = {};
glm::vec2 sideInitialSize_ = {};

// <default, rotate>
std::pair< glm::vec2, glm::vec2 > centerCurrentSize_ = {};
glm::vec2 upCurrentSize_ = {};
glm::vec2 sideCurrentSize_ = {};
};
} // namespace looper

0 comments on commit 3977f95

Please sign in to comment.