Skip to content

Commit

Permalink
Preditor: EditTools: Switch pivot between origin and center
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Dec 31, 2023
1 parent 10c1f50 commit f61c9bf
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ChairManager/Data/Preditor/ActionSets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
name="transform_tools"
description="Transform Tools"
priority="60">
<Action name="toggle_pivot" description="Toggle tool pivot between origin and center">
<Bind key="z"/>
</Action>
<Action name="toggle_tool_space" description="Toggle tool space between local and world">
<Bind key="x"/>
</Action>
Expand Down
3 changes: 3 additions & 0 deletions Preditor/Common/Preditor/SceneEditor/IObjectManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ struct IObjectManipulator

//! Sets an object's world translation matrix.
virtual void SetObjectWorldTM(SceneObjectId id, const Matrix34& tm) = 0;

//! Gets the object's local-space bounding box.
virtual void GetObjectLocalBounds(SceneObjectId id, AABB& aabb) = 0;
};
10 changes: 10 additions & 0 deletions Preditor/EditTools/EditToolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ EditTools::EditToolManager::EditToolManager(ISceneEditor* pEditor)
RegisterToolSelectKey("rotate", m_pRotateTool);
RegisterToolSelectKey("scale", m_pScaleTool);

gPreditor->pInput->FindAction("transform_tools.toggle_pivot")->AddListener([this](const KeyActionEventArgs& e)
{
if (e.isPressed)
m_bPivotCenter ^= 1;
});

gPreditor->pInput->FindAction("transform_tools.toggle_tool_space")->AddListener([this](const KeyActionEventArgs& e)
{
if (e.isPressed)
Expand All @@ -39,6 +45,10 @@ EditTools::EditToolManager::~EditToolManager()

void EditTools::EditToolManager::ShowSelectionUI()
{
if (ImGui::Button(m_bPivotCenter ? ICON_MD_CENTER_FOCUS_WEAK : ICON_MD_TRIP_ORIGIN))
m_bPivotCenter ^= 1;
ImGui::SameLine();

if (ImGui::Button(m_bWorldTransform ? ICON_MD_LANGUAGE : ICON_MD_VIEW_IN_AR))
m_bWorldTransform ^= 1;
ImGui::SameLine();
Expand Down
6 changes: 5 additions & 1 deletion Preditor/EditTools/EditToolManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class EditToolManager final : public IEditToolManager
//! @returns the current tool (or nullptr, if none).
EditTool* GetCurrentTool() const { return m_pCurTool; }

//! @returns whether transformation tools should pivot around the center instead of local origin.
bool IsPivotCenter() const { return m_bPivotCenter; }

//! @returns whether transformation tools should tranform in the world space.
bool IsWorldTransform() const { return m_bWorldTransform; }

Expand All @@ -33,7 +36,8 @@ class EditToolManager final : public IEditToolManager
bool m_bIsActive = false;
ISceneEditor* m_pEditor = nullptr;
EditTool* m_pCurTool = nullptr;
bool m_bWorldTransform = true;
bool m_bPivotCenter = true;
bool m_bWorldTransform = false;

std::unique_ptr<EditTool> m_pSelectTool;
std::unique_ptr<EditTool> m_pMoveTool;
Expand Down
16 changes: 13 additions & 3 deletions Preditor/EditTools/ImGuizmoTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@ void EditTools::ImGuizmoTool::DrawViewport(const Vec4& bounds, const CCamera& ca
if (activeObj != INVALID_SCENE_OBJECT)
{
IObjectManipulator* pManip = pEditor->GetManipulator();
Matrix44 pivotTransform(IDENTITY);

if (GetManager()->IsPivotCenter())
{
// Get AABB center
AABB aabb;
pManip->GetObjectLocalBounds(activeObj, aabb);
pivotTransform.SetTranslation(aabb.GetCenter());
}

Matrix44 viewMat = CryToImGuizmo(camera.GetViewMatrix());
Matrix44 objectTM = CryToImGuizmo(pManip->GetObjectWorldTM(activeObj));
Matrix44 objectTM = CryToImGuizmo(pManip->GetObjectWorldTM(activeObj) * pivotTransform);

Matrix44 projMat;
FrustumFromCamera(camera, bounds.z - bounds.x, bounds.w - bounds.y, projMat.GetData());
Expand All @@ -119,10 +129,10 @@ void EditTools::ImGuizmoTool::DrawViewport(const Vec4& bounds, const CCamera& ca
GetManager()->IsWorldTransform() ? ImGuizmo::WORLD : ImGuizmo::LOCAL,
objectTM.GetData(),
nullptr, // deltaMatrix
m_pSnapKey->IsHeldDown() ? & m_Snap.x : nullptr);
m_pSnapKey->IsHeldDown() ? &m_Snap.x : nullptr);

if (manipulated)
pManip->SetObjectWorldTM(activeObj, Matrix34(ImGuizmoToCry(objectTM)));
pManip->SetObjectWorldTM(activeObj, Matrix34(ImGuizmoToCry(objectTM) * pivotTransform.GetInverted()));
}
}

Expand Down
13 changes: 13 additions & 0 deletions Preditor/GameEditor/EntityManipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ void GameEditor::EntityManipulator::SetObjectWorldTM(SceneObjectId id, const Mat

pEnt->SetWorldTM(tm, ENTITY_XFORM_EDITOR);
}

void GameEditor::EntityManipulator::GetObjectLocalBounds(SceneObjectId id, AABB& aabb)
{
IEntity* pEnt = gEnv->pEntitySystem->GetEntity((EntityId)id);

if (!pEnt)
{
CryError("GetObjectLocalBounds called with invalid entity id {}", id);
return;
}

pEnt->GetLocalBounds(aabb);
}
1 change: 1 addition & 0 deletions Preditor/GameEditor/EntityManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EntityManipulator : public IObjectManipulator
// IObjectManipulator
virtual Matrix34 GetObjectWorldTM(SceneObjectId id) override;
virtual void SetObjectWorldTM(SceneObjectId id, const Matrix34& tm) override;
virtual void GetObjectLocalBounds(SceneObjectId id, AABB& aabb) override;

private:
GameEditMode* m_pEditor = nullptr;
Expand Down

0 comments on commit f61c9bf

Please sign in to comment.