Skip to content

Commit

Permalink
[#187]: Center gizmo on multiselect
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Jan 30, 2024
1 parent 2199a7b commit ae16eda
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,33 @@ Editor::MouseButtonCallback(MouseButtonEvent& event)

auto& firstObject = currentLevel_->GetGameObjectRef(selectedObjects.front());
auto gizmoPos = firstObject.GetCenteredPosition();
glm::vec2 min = gizmoPos;
glm::vec2 max = gizmoPos;

for (const auto object : selectedObjects)
{
gui_.ObjectSelected(object);
auto& gameObject = currentLevel_->GetGameObjectRef(object);
gizmoPos = (gameObject.GetCenteredPosition() + gizmoPos) / 2.0f;
const auto& objectPos =
currentLevel_->GetGameObjectRef(object).GetCenteredPosition();

min = glm::vec2{
glm::min(min.x, objectPos.x),
glm::min(min.y, objectPos.y),
};

max = glm::vec2{
glm::max(max.x, objectPos.x),
glm::max(max.y, objectPos.y),
};
}

selectedObjects_ = selectedObjects;

gizmoActive_ = true;
gizmo_.Show();
gizmo_.Update(gizmoPos, selectedObjects_.size() == 1
? firstObject.GetSprite().GetRotation()
: 0.0f);
gizmo_.Update((min + max) / 2.0f, selectedObjects_.size() == 1
? firstObject.GetSprite().GetRotation()
: 0.0f);
}
selectStartPos_ = glm::vec2{};
selectRect_ = std::array< glm::vec2, 4 >{};
Expand Down Expand Up @@ -582,14 +595,26 @@ Editor::RecalculateGizmoPos()
}

auto& firstObject = currentLevel_->GetGameObjectRef(selectedObjects_.front());

auto gizmoPos = firstObject.GetCenteredPosition();
glm::vec2 min = gizmoPos;
glm::vec2 max = gizmoPos;

for (const auto object : selectedObjects_)
{
auto& gameObject = currentLevel_->GetGameObjectRef(object);
gizmoPos = (gameObject.GetCenteredPosition() + gizmoPos) / 2.0f;
const auto& objectPos = currentLevel_->GetGameObjectRef(object).GetCenteredPosition();
min = glm::vec2{
glm::min(min.x, objectPos.x),
glm::min(min.y, objectPos.y),
};

max = glm::vec2{
glm::max(max.x, objectPos.x),
glm::max(max.y, objectPos.y),
};
}

gizmo_.Update(gizmoPos,
gizmo_.Update((min + max) / 2.0f,
selectedObjects_.size() == 1 ? firstObject.GetSprite().GetRotation() : 0.0f);
}

Expand Down Expand Up @@ -1382,21 +1407,6 @@ Editor::Update()
gui_.UpdateUI();
}

// if (gizmoActive_)
//{
// if (currentSelectedEditorObject_ != Object::INVALID_ID)
// {
// const auto& objectRef = GetEditorObjectRef(currentSelectedEditorObject_);
// gizmo_.Update(objectRef.GetCenteredPosition(), objectRef.GetSprite().GetRotation());
// }
// else if (currentSelectedGameObject_ != Object::INVALID_ID)
// {
// const auto& objectRef = dynamic_cast< GameObject& >(
// currentLevel_->GetGameObjectRef(currentSelectedGameObject_));
// gizmo_.Update(objectRef.GetCenteredPosition(), objectRef.GetSprite().GetRotation());
// }
// }

auto& renderData = renderer::GetRenderData();
renderData.viewMat = camera_.GetViewMatrix();
renderData.projMat = camera_.GetProjectionMatrix();
Expand Down

0 comments on commit ae16eda

Please sign in to comment.