Radium Engine  1.5.0
GizmoManager.cpp
1 #include <Core/Asset/Camera.hpp>
2 #include <Engine/OpenGL.hpp>
3 #include <Engine/Scene/CameraComponent.hpp>
4 #include <Engine/Scene/SystemDisplay.hpp>
5 #include <Gui/Utils/Keyboard.hpp>
6 #include <Gui/Viewer/CameraManipulator.hpp>
7 #include <Gui/Viewer/Gizmo/GizmoManager.hpp>
8 #include <Gui/Viewer/Gizmo/RotateGizmo.hpp>
9 #include <Gui/Viewer/Gizmo/ScaleGizmo.hpp>
10 #include <Gui/Viewer/Gizmo/TranslateGizmo.hpp>
11 
12 namespace Ra {
13 namespace Gui {
14 
15 using GizmoMapping = KeyMappingManageable<GizmoManager>;
16 
19 #define KMA_VALUE( XX ) Gui::KeyMappingManager::KeyMappingAction GizmoManager::XX;
20 KeyMappingGizmo
21 #undef KMA_VALUE
22 
23 void GizmoManager::configureKeyMapping_impl() {
24  GizmoMapping::setContext( Gui::KeyMappingManager::getInstance()->getContext( "GizmoContext" ) );
25  if ( GizmoMapping::getContext().isInvalid() ) {
26  LOG( Ra::Core::Utils::logINFO )
27  << "GizmoContext not defined (maybe the configuration file do not contains it)";
28  LOG( Ra::Core::Utils::logERROR ) << "GizmoContext all keymapping invalide !";
29  return;
30  }
31 #define KMA_VALUE( XX ) \
32  XX = Gui::KeyMappingManager::getInstance()->getAction( GizmoMapping::getContext(), #XX );
33  KeyMappingGizmo
34 #undef KMA_VALUE
35 }
36 
37 bool GizmoManager::isValidAction( const Gui::KeyMappingManager::KeyMappingAction& action ) const {
38 
39  if ( action.isInvalid() ) return false;
40  bool res = false;
41 
42 #define KMA_VALUE( XX ) res = ( XX == action ) || res;
43  KeyMappingGizmo
44 #undef KMA_VALUE
45  return res;
46 }
47 
48 /*
49  * FIXME : Mathias -- Creating gizmos by default is not a good idea.
50  * implies that all applications developped on top of the engine will have them.
51  * This is not a good idea. Applications must be able to define and
52  * create their own gizmos
53  *
54  * \see issue #194
55  */
56 GizmoManager::GizmoManager( QObject* parent ) :
57  QObject( parent ), m_currentGizmoType( NONE ), m_mode( Gizmo::GLOBAL ) {
58  m_gizmos[0].reset( new TranslateGizmo( Engine::Scene::SystemEntity::uiCmp(),
59  Ra::Core::Transform::Identity(),
60  m_transform,
61  m_mode ) );
62  m_gizmos[1].reset( new RotateGizmo( Engine::Scene::SystemEntity::uiCmp(),
63  Ra::Core::Transform::Identity(),
64  m_transform,
65  m_mode ) );
66  m_gizmos[2].reset( new ScaleGizmo( Engine::Scene::SystemEntity::uiCmp(),
67  Ra::Core::Transform::Identity(),
68  m_transform,
69  m_mode ) );
70  for ( auto& g : m_gizmos ) {
71  if ( g ) { g->show( false ); }
72  }
73 }
74 
75 void GizmoManager::setEditable( const Engine::Scene::ItemEntry& ent ) {
76  TransformEditor::setEditable( ent );
77  updateGizmo();
78 }
79 
80 void GizmoManager::updateGizmo() {
81  for ( auto& g : m_gizmos ) {
82  if ( g ) { g->show( false ); }
83  }
84 
85  if ( canEdit() ) {
86  Core::Transform worldTransform = getWorldTransform();
87  auto g = currentGizmo();
88  if ( g ) {
89  g->updateTransform( m_mode, worldTransform, m_transform );
90  g->show( true );
91  }
92  }
93 }
94 
95 void GizmoManager::setLocal( bool useLocal ) {
96  m_mode = useLocal ? Gizmo::LOCAL : Gizmo::GLOBAL;
97  updateGizmo();
98 }
99 
100 void GizmoManager::changeGizmoType( GizmoManager::GizmoType type ) {
101  m_currentGizmoType = type;
102  updateGizmo();
103 }
104 
105 void GizmoManager::updateValues() {
106  if ( canEdit() ) {
107  getTransform();
108  if ( currentGizmo() ) {
109  currentGizmo()->updateTransform( m_mode, getWorldTransform(), m_transform );
110  }
111  }
112 }
113 
114 bool GizmoManager::handleMousePressEvent( QMouseEvent* event,
115  const Qt::MouseButtons& buttons,
116  const Qt::KeyboardModifiers& modifiers,
117  int key,
118  const Core::Asset::Camera& cam ) {
119 
120  if ( !canEdit() || m_currentGizmoType == NONE || !currentGizmo()->isSelected() ) {
121  return false;
122  }
123  auto action = KeyMappingManager::getInstance()->getAction(
124  GizmoMapping::getContext(), buttons, modifiers, key, false );
125 
126  if ( !( isValidAction( action ) ) ) { return false; }
127 
128  currentGizmo()->setInitialState( cam,
129  Core::Vector2( Scalar( event->x() ), Scalar( event->y() ) ) );
130  return true;
131 }
132 
133 bool GizmoManager::handleMouseReleaseEvent( QMouseEvent* /*event*/ ) {
134  if ( currentGizmo() ) { currentGizmo()->selectConstraint( -1 ); }
135  return ( currentGizmo() != nullptr );
136 }
137 
138 bool GizmoManager::handleMouseMoveEvent( QMouseEvent* event,
139  const Qt::MouseButtons& buttons,
140  const Qt::KeyboardModifiers& modifiers,
141  int key,
142  const Core::Asset::Camera& cam ) {
145  auto action = KeyMappingManager::getInstance()->getAction(
146  GizmoMapping::getContext(), buttons, modifiers, key, false );
147 
148  if ( m_currentGizmoType != NONE && canEdit() && isValidAction( action ) && currentGizmo() &&
149  currentGizmo()->isSelected() ) {
150  Core::Vector2 currentXY( event->x(), event->y() );
151  bool step = action == GIZMOMANAGER_STEP || action == GIZMOMANAGER_STEP_WHOLE;
152  bool whole = action == GIZMOMANAGER_WHOLE || action == GIZMOMANAGER_STEP_WHOLE;
153 
154  Core::Transform newTransform = currentGizmo()->mouseMove( cam, currentXY, step, whole );
155  setTransform( newTransform );
156  updateValues();
157  return true;
158  }
159  return false;
160 }
161 
162 void GizmoManager::handlePickingResult( int drawableId ) {
163  if ( currentGizmo() ) { currentGizmo()->selectConstraint( drawableId ); }
164 }
165 
166 Gizmo* GizmoManager::currentGizmo() {
167  return ( m_currentGizmoType == NONE ) ? nullptr : m_gizmos[m_currentGizmoType - 1].get();
168 }
169 
170 void GizmoManager::cleanup() {
171  for ( auto& g : m_gizmos ) {
172  g.reset( nullptr );
173  }
174 }
175 } // namespace Gui
176 } // namespace Ra
Camera class storing the Camera frame and the projection properties The view direction is -z in camer...
Definition: Camera.hpp:16
Ra::Core::Utils::Index KeyMappingAction
handle to an action
Definition: Cage.cpp:3