1 #include <cpExtensions/Interaction/BaseStyle.h>
5 #include <vtkCallbackCommand.h>
7 #include <vtkRenderer.h>
8 #include <vtkRenderWindowInteractor.h>
10 // -------------------------------------------------------------------------
11 long cpExtensions::Interaction::BaseStyle::_TMouseButtonEvent::
12 MaxDoubleClick = 350; // ms
14 // -------------------------------------------------------------------------
15 void cpExtensions::Interaction::BaseStyle::
16 SetSetDoubleClickDelay( long delay )
18 Self::_TMouseButtonEvent::MaxDoubleClick = delay;
21 // -------------------------------------------------------------------------
22 void cpExtensions::Interaction::BaseStyle::
23 DelegateTDxEvent( unsigned long event, void* calldata )
26 std::cerr << "No TDx support at this time!" << std::endl;
30 // -------------------------------------------------------------------------
31 void cpExtensions::Interaction::BaseStyle::
34 if( this->Interactor == NULL )
38 bool alt = ( this->Interactor->GetAltKey( ) == 1 );
39 bool ctr = ( this->Interactor->GetControlKey( ) == 1 );
40 bool sft = ( this->Interactor->GetShiftKey( ) == 1 );
41 ButtonID button = this->GetButtonID( );
43 // Invoke possible generic events
44 auto x = this->Interactor->GetEventPosition( )[ 0 ];
45 auto y = this->Interactor->GetEventPosition( )[ 1 ];
46 if( button == Self::ButtonID_Right )
48 if( !alt && !ctr && !sft )
50 this->FindPokedRenderer( x, y );
55 else if( button == Self::ButtonID_Middle )
57 if( !alt && !ctr && !sft )
59 this->FindPokedRenderer( x, y );
67 // -------------------------------------------------------------------------
68 void cpExtensions::Interaction::BaseStyle::
71 this->m_ActiveButton = Self::ButtonID_Left;
74 // -------------------------------------------------------------------------
75 void cpExtensions::Interaction::BaseStyle::
78 this->m_ActiveButton = Self::ButtonID_None;
81 // -------------------------------------------------------------------------
82 void cpExtensions::Interaction::BaseStyle::
85 this->m_ActiveButton = Self::ButtonID_Middle;
87 // Get current position on the associated actors
88 if( this->Interactor == NULL )
92 bool alt = ( this->Interactor->GetAltKey( ) == 1 );
93 bool ctr = ( this->Interactor->GetControlKey( ) == 1 );
94 bool sft = ( this->Interactor->GetShiftKey( ) == 1 );
95 if( !alt && !ctr && !sft )
99 // -------------------------------------------------------------------------
100 void cpExtensions::Interaction::BaseStyle::
103 this->m_ActiveButton = Self::ButtonID_None;
105 // Get current position on the associated actors
106 if( this->Interactor == NULL )
109 switch( this->State )
119 // -------------------------------------------------------------------------
120 void cpExtensions::Interaction::BaseStyle::
123 this->m_ActiveButton = Self::ButtonID_Right;
125 // Get current position on the associated actors
126 if( this->Interactor == NULL )
130 bool alt = ( this->Interactor->GetAltKey( ) == 1 );
131 bool ctr = ( this->Interactor->GetControlKey( ) == 1 );
132 bool sft = ( this->Interactor->GetShiftKey( ) == 1 );
133 if( !alt && !ctr && !sft )
137 // -------------------------------------------------------------------------
138 void cpExtensions::Interaction::BaseStyle::
141 this->m_ActiveButton = Self::ButtonID_None;
143 // Get current position on the associated actors
144 if( this->Interactor == NULL )
147 switch( this->State )
157 // -------------------------------------------------------------------------
158 #define cpExtensions_BaseStyle_Click( S, T ) \
159 void cpExtensions::Interaction::BaseStyle:: \
162 if( this->Interactor == NULL ) \
164 static int idx[ 2 ]; \
165 static double pos[ 3 ]; \
166 if( !( this->_PickPosition( idx, pos ) ) ) \
168 auto i = this->m_Mouse##T##Commands.begin( ); \
169 for( ; i != this->m_Mouse##T##Commands.end( ); ++i ) \
172 Self::ButtonID_##S, \
174 this->Interactor->GetAltKey( ) == 1, \
175 this->Interactor->GetControlKey( ) == 1, \
176 this->Interactor->GetShiftKey( ) == 1 \
180 cpExtensions_BaseStyle_Click( Left, Click );
181 cpExtensions_BaseStyle_Click( Middle, Click );
182 cpExtensions_BaseStyle_Click( Right, Click );
183 cpExtensions_BaseStyle_Click( Left, DoubleClick );
184 cpExtensions_BaseStyle_Click( Middle, DoubleClick );
185 cpExtensions_BaseStyle_Click( Right, DoubleClick );
187 // -------------------------------------------------------------------------
188 void cpExtensions::Interaction::BaseStyle::
191 if( this->CurrentRenderer == NULL )
194 vtkRenderWindowInteractor* rwi = this->GetInteractor( );
195 double *center = this->CurrentRenderer->GetCenter( );
196 int dy = rwi->GetEventPosition( )[ 1 ] - rwi->GetLastEventPosition( )[ 1 ];
197 double dyf = this->m_MotionFactor * dy / center[ 1 ];
198 this->_Dolly( std::pow( 1.1, dyf ) );
201 // -------------------------------------------------------------------------
202 void cpExtensions::Interaction::BaseStyle::
205 if( this->CurrentRenderer == NULL )
208 vtkRenderWindowInteractor* rwi = this->Interactor;
209 double viewFocus[ 4 ], focalDepth, viewPoint[ 3 ];
210 double newPickPoint[ 4 ], oldPickPoint[ 4 ], motionVector[ 3 ];
212 // Calculate the focal depth since we'll be using it a lot
213 vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
214 camera->GetFocalPoint( viewFocus );
215 this->ComputeWorldToDisplay(
216 viewFocus[ 0 ], viewFocus[ 1 ], viewFocus[ 2 ], viewFocus
218 focalDepth = viewFocus[ 2 ];
219 this->ComputeDisplayToWorld(
220 rwi->GetEventPosition( )[ 0 ],
221 rwi->GetEventPosition( )[ 1 ],
226 // Has to recalc old mouse point since the viewport has moved,
227 // so can't move it outside the loop
228 this->ComputeDisplayToWorld(
229 rwi->GetLastEventPosition( )[ 0 ],
230 rwi->GetLastEventPosition( )[ 1 ],
235 // Camera motion is reversed
236 motionVector[ 0 ] = oldPickPoint[ 0 ] - newPickPoint[ 0 ];
237 motionVector[ 1 ] = oldPickPoint[ 1 ] - newPickPoint[ 1 ];
238 motionVector[ 2 ] = oldPickPoint[ 2 ] - newPickPoint[ 2 ];
240 camera->GetFocalPoint( viewFocus );
241 camera->GetPosition( viewPoint );
242 camera->SetFocalPoint(
243 motionVector[ 0 ] + viewFocus[ 0 ],
244 motionVector[ 1 ] + viewFocus[ 1 ],
245 motionVector[ 2 ] + viewFocus[ 2 ]
248 motionVector[ 0 ] + viewPoint[ 0 ],
249 motionVector[ 1 ] + viewPoint[ 1 ],
250 motionVector[ 2 ] + viewPoint[ 2 ]
252 if( rwi->GetLightFollowCamera( ) )
253 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
257 // -------------------------------------------------------------------------
258 cpExtensions::Interaction::BaseStyle::
261 m_MotionFactor( double( 10 ) )
263 this->m_LeftButtonEvent.Reset( );
264 this->m_MiddleButtonEvent.Reset( );
265 this->m_RightButtonEvent.Reset( );
266 this->m_ActiveButton = Self::ButtonID_None;
268 this->EventCallbackCommand->SetCallback( Self::_ProcessEvents );
271 // -------------------------------------------------------------------------
272 cpExtensions::Interaction::BaseStyle::
277 // -------------------------------------------------------------------------
278 void cpExtensions::Interaction::BaseStyle::
279 _Dolly( double factor )
281 if( this->CurrentRenderer == NULL )
284 vtkCamera* camera = this->CurrentRenderer->GetActiveCamera( );
285 if( camera->GetParallelProjection( ) == 0 )
287 camera->Dolly( factor );
288 if( this->AutoAdjustCameraClippingRange )
289 this->CurrentRenderer->ResetCameraClippingRange( );
292 camera->SetParallelScale( camera->GetParallelScale( ) / factor );
293 if( this->Interactor->GetLightFollowCamera( ) )
294 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera( );
295 this->Interactor->Render( );
298 // -------------------------------------------------------------------------
299 void cpExtensions::Interaction::BaseStyle::
307 // Get active style and interactor
308 Self* s = reinterpret_cast< Self* >( clientdata );
315 case vtkCommand::MouseMoveEvent:
320 case vtkCommand::LeftButtonPressEvent:
322 unsigned char nc = s->m_LeftButtonEvent.Clicks( );
324 s->OnLeftDoubleClick( );
327 s->OnLeftButtonDown( );
330 case vtkCommand::LeftButtonReleaseEvent:
332 s->m_LeftButtonEvent.Release( );
333 s->OnLeftButtonUp( );
336 case vtkCommand::MiddleButtonPressEvent:
338 unsigned char nc = s->m_MiddleButtonEvent.Clicks( );
340 s->OnMiddleDoubleClick( );
343 s->OnMiddleButtonDown( );
346 case vtkCommand::MiddleButtonReleaseEvent:
348 s->m_MiddleButtonEvent.Release( );
349 s->OnMiddleButtonUp( );
352 case vtkCommand::RightButtonPressEvent:
354 unsigned char nc = s->m_RightButtonEvent.Clicks( );
356 s->OnRightDoubleClick( );
359 s->OnRightButtonDown( );
362 case vtkCommand::RightButtonReleaseEvent:
364 s->m_RightButtonEvent.Release( );
365 s->OnRightButtonUp( );
368 case vtkCommand::MouseWheelForwardEvent:
370 s->OnMouseWheelForward( );
373 case vtkCommand::MouseWheelBackwardEvent:
375 s->OnMouseWheelBackward( );
378 case vtkCommand::KeyPressEvent:
384 case vtkCommand::KeyReleaseEvent:
390 case vtkCommand::CharEvent:
395 case vtkCommand::ExposeEvent:
400 case vtkCommand::ConfigureEvent:
405 case vtkCommand::EnterEvent:
410 case vtkCommand::LeaveEvent:
415 case vtkCommand::TimerEvent:
420 case vtkCommand::DeleteEvent:
422 s->SetInteractor( 0 );
425 case vtkCommand::TDxMotionEvent:
426 case vtkCommand::TDxButtonPressEvent:
427 case vtkCommand::TDxButtonReleaseEvent:
429 s->DelegateTDxEvent( event, calldata );