55*/
66
77#include " OvEditor/Core/GizmoBehaviour.h"
8+ #include " OvEditor/Settings/EditorSettings.h"
9+
10+ float SnapValue (float p_value, float p_step)
11+ {
12+ return p_value - std::fmod (p_value, p_step);
13+ }
14+
15+ bool OvEditor::Core::GizmoBehaviour::IsSnappedBehaviourEnabled () const
16+ {
17+ using namespace OvWindowing ::Inputs;
18+
19+ const auto & inputManager = EDITOR_CONTEXT (inputManager);
20+ return inputManager->GetKeyState (EKey::KEY_LEFT_CONTROL) == EKeyState::KEY_DOWN || inputManager->GetKeyState (EKey::KEY_RIGHT_CONTROL) == EKeyState::KEY_DOWN;
21+ }
822
923void OvEditor::Core::GizmoBehaviour::StartPicking (OvCore::ECS::Actor& p_target, const OvMaths::FVector3& p_cameraPosition, EGizmoOperation p_operation, EDirection p_direction)
1024{
@@ -101,9 +115,14 @@ void OvEditor::Core::GizmoBehaviour::ApplyTranslation(const OvMaths::FMatrix4& p
101115 auto screenDirection = GetScreenDirection (p_viewMatrix, p_projectionMatrix, p_viewSize);
102116
103117 auto totalDisplacement = m_currentMouse - m_originMouse;
104- auto translationCoefficient = OvMaths::FVector2::Dot (totalDisplacement, screenDirection);
118+ auto translationCoefficient = OvMaths::FVector2::Dot (totalDisplacement, screenDirection) * unitsPerPixel;
119+
120+ if (IsSnappedBehaviourEnabled ())
121+ {
122+ translationCoefficient = SnapValue (translationCoefficient, OvEditor::Settings::EditorSettings::TranslationSnapUnit);
123+ }
105124
106- m_target->transform .SetLocalPosition (originPosition + GetRealDirection () * translationCoefficient * unitsPerPixel );
125+ m_target->transform .SetLocalPosition (originPosition + GetRealDirection () * translationCoefficient);
107126}
108127
109128void OvEditor::Core::GizmoBehaviour::ApplyRotation (const OvMaths::FMatrix4& p_viewMatrix, const OvMaths::FMatrix4& p_projectionMatrix, const OvMaths::FVector2& p_viewSize) const
@@ -115,9 +134,14 @@ void OvEditor::Core::GizmoBehaviour::ApplyRotation(const OvMaths::FMatrix4& p_vi
115134 screenDirection = OvMaths::FVector2 (-screenDirection.y , screenDirection.x );
116135
117136 auto totalDisplacement = m_currentMouse - m_originMouse;
118- auto rotationCoefficient = OvMaths::FVector2::Dot (totalDisplacement, screenDirection);
137+ auto rotationCoefficient = OvMaths::FVector2::Dot (totalDisplacement, screenDirection) * unitsPerPixel;
138+
139+ if (IsSnappedBehaviourEnabled ())
140+ {
141+ rotationCoefficient = SnapValue (rotationCoefficient, OvEditor::Settings::EditorSettings::RotationSnapUnit);
142+ }
119143
120- auto rotationToApply = OvMaths::FQuaternion (OvMaths::FVector3 (GetFakeDirection () * rotationCoefficient * unitsPerPixel ));
144+ auto rotationToApply = OvMaths::FQuaternion (OvMaths::FVector3 (GetFakeDirection () * rotationCoefficient));
121145 m_target->transform .SetLocalRotation (originRotation * rotationToApply);
122146}
123147
@@ -129,9 +153,14 @@ void OvEditor::Core::GizmoBehaviour::ApplyScale(const OvMaths::FMatrix4& p_viewM
129153 auto screenDirection = GetScreenDirection (p_viewMatrix, p_projectionMatrix, p_viewSize);
130154
131155 auto totalDisplacement = m_currentMouse - m_originMouse;
132- auto scaleCoefficient = OvMaths::FVector2::Dot (totalDisplacement, screenDirection);
156+ auto scaleCoefficient = OvMaths::FVector2::Dot (totalDisplacement, screenDirection) * unitsPerPixel;
157+
158+ if (IsSnappedBehaviourEnabled ())
159+ {
160+ scaleCoefficient = SnapValue (scaleCoefficient, OvEditor::Settings::EditorSettings::ScalingSnapUnit);
161+ }
133162
134- auto newScale = originScale + GetFakeDirection () * scaleCoefficient * unitsPerPixel ;
163+ auto newScale = originScale + GetFakeDirection () * scaleCoefficient;
135164
136165 /* Prevent scale from being negative*/
137166 newScale.x = std::max (newScale.x , 0 .0001f );
0 commit comments