1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkInteractorStyle.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkInteractorStyle
17  * @brief   provide event-driven interface to the rendering window (defines trackball mode)
18  *
19  * vtkInteractorStyle is a base class implementing the majority of motion
20  * control routines and defines an event driven interface to support
21  * vtkRenderWindowInteractor. vtkRenderWindowInteractor implements
22  * platform dependent key/mouse routing and timer control, which forwards
23  * events in a neutral form to vtkInteractorStyle.
24  *
25  * vtkInteractorStyle implements the "joystick" style of interaction. That
26  * is, holding down the mouse keys generates a stream of events that cause
27  * continuous actions (e.g., rotate, translate, pan, zoom). (The class
28  * vtkInteractorStyleTrackball implements a grab and move style.) The event
29  * bindings for this class include the following:
30  * - Keypress j / Keypress t: toggle between joystick (position sensitive) and
31  * trackball (motion sensitive) styles. In joystick style, motion occurs
32  * continuously as long as a mouse button is pressed. In trackball style,
33  * motion occurs when the mouse button is pressed and the mouse pointer
34  * moves.
35  * - Keypress c / Keypress a: toggle between camera and actor modes. In
36  * camera mode, mouse events affect the camera position and focal point. In
37  * actor mode, mouse events affect the actor that is under the mouse pointer.
38  * - Button 1: rotate the camera around its focal point (if camera mode) or
39  * rotate the actor around its origin (if actor mode). The rotation is in the
40  * direction defined from the center of the renderer's viewport towards
41  * the mouse position. In joystick mode, the magnitude of the rotation is
42  * determined by the distance the mouse is from the center of the render
43  * window.
44  * - Button 2: pan the camera (if camera mode) or translate the actor (if
45  * actor mode). In joystick mode, the direction of pan or translation is
46  * from the center of the viewport towards the mouse position. In trackball
47  * mode, the direction of motion is the direction the mouse moves. (Note:
48  * with 2-button mice, pan is defined as \<Shift\>-Button 1.)
49  * - Button 3: zoom the camera (if camera mode) or scale the actor (if
50  * actor mode). Zoom in/increase scale if the mouse position is in the top
51  * half of the viewport; zoom out/decrease scale if the mouse position is in
52  * the bottom half. In joystick mode, the amount of zoom is controlled by the
53  * distance of the mouse pointer from the horizontal centerline of the
54  * window.
55  * - Keypress 3: toggle the render window into and out of stereo mode. By
56  * default, red-blue stereo pairs are created. Some systems support Crystal
57  * Eyes LCD stereo glasses; you have to invoke SetStereoTypeToCrystalEyes()
58  * on the rendering window.
59  * - Keypress e: exit the application.
60  * - Keypress f: fly to the picked point
61  * - Keypress p: perform a pick operation. The render window interactor has
62  * an internal instance of vtkCellPicker that it uses to pick.
63  * - Keypress r: reset the camera view along the current view
64  * direction. Centers the actors and moves the camera so that all actors are
65  * visible.
66  * - Keypress s: modify the representation of all actors so that they are
67  * surfaces.
68  * - Keypress u: invoke the user-defined function. Typically,
69  * this keypress will bring up an interactor that you can type commands in.
70  * Typing u calls UserCallBack() on the vtkRenderWindowInteractor, which
71  * invokes a vtkCommand::UserEvent. In other words, to define a user-defined
72  * callback, just add an observer to the vtkCommand::UserEvent on the
73  * vtkRenderWindowInteractor object.
74  * - Keypress w: modify the representation of all actors so that they are
75  * wireframe.
76  *
77  * vtkInteractorStyle can be subclassed to provide new interaction styles and
78  * a facility to override any of the default mouse/key operations which
79  * currently handle trackball or joystick styles is provided. Note that this
80  * class will fire a variety of events that can be watched using an observer,
81  * such as LeftButtonPressEvent, LeftButtonReleaseEvent,
82  * MiddleButtonPressEvent, MiddleButtonReleaseEvent, RightButtonPressEvent,
83  * RightButtonReleaseEvent, EnterEvent, LeaveEvent, KeyPressEvent,
84  * KeyReleaseEvent, CharEvent, ExposeEvent, ConfigureEvent, TimerEvent,
85  * MouseMoveEvent,
86  *
87  *
88  * @sa
89  * vtkInteractorStyleTrackball
90  */
91 
92 #ifndef vtkInteractorStyle_h
93 #define vtkInteractorStyle_h
94 
95 #include "vtkInteractorObserver.h"
96 #include "vtkRenderingCoreModule.h" // For export macro
97 
98 // Motion flags
99 
100 #define VTKIS_START 0
101 #define VTKIS_NONE 0
102 
103 #define VTKIS_ROTATE 1
104 #define VTKIS_PAN 2
105 #define VTKIS_SPIN 3
106 #define VTKIS_DOLLY 4
107 #define VTKIS_ZOOM 5
108 #define VTKIS_USCALE 6
109 #define VTKIS_TIMER 7
110 #define VTKIS_FORWARDFLY 8
111 #define VTKIS_REVERSEFLY 9
112 #define VTKIS_TWO_POINTER 10
113 #define VTKIS_CLIP 11
114 #define VTKIS_PICK 12                 // perform a pick at the last location
115 #define VTKIS_LOAD_CAMERA_POSE 13     // iterate through saved camera poses
116 #define VTKIS_POSITION_PROP 14        // adjust the position, orientation of a prop
117 #define VTKIS_EXIT 15                 // call exit callback
118 #define VTKIS_TOGGLE_DRAW_CONTROLS 16 // draw device controls helpers
119 #define VTKIS_MENU 17                 // invoke an application menu
120 #define VTKIS_GESTURE 18              // touch interaction in progress
121 #define VTKIS_ENV_ROTATE 19           // rotate the renderer environment texture
122 
123 #define VTKIS_ANIM_OFF 0
124 #define VTKIS_ANIM_ON 1
125 
126 class vtkActor2D;
127 class vtkActor;
128 class vtkCallbackCommand;
129 class vtkEventData;
130 class vtkEventForwarderCommand;
131 class vtkOutlineSource;
132 class vtkPolyDataMapper;
133 class vtkProp3D;
134 class vtkProp;
135 class vtkStringArray;
136 class vtkTDxInteractorStyle;
137 
138 class VTKRENDERINGCORE_EXPORT vtkInteractorStyle : public vtkInteractorObserver
139 {
140 public:
141   /**
142    * This class must be supplied with a vtkRenderWindowInteractor wrapper or
143    * parent. This class should not normally be instantiated by application
144    * programmers.
145    */
146   static vtkInteractorStyle* New();
147 
148   vtkTypeMacro(vtkInteractorStyle, vtkInteractorObserver);
149   void PrintSelf(ostream& os, vtkIndent indent) override;
150 
151   /**
152    * Set/Get the Interactor wrapper being controlled by this object.
153    * (Satisfy superclass API.)
154    */
155   void SetInteractor(vtkRenderWindowInteractor* interactor) override;
156 
157   /**
158    * Turn on/off this interactor. Interactor styles operate a little
159    * bit differently than other types of interactor observers. When
160    * the SetInteractor() method is invoked, the automatically enable
161    * themselves. This is a legacy requirement, and convenient for the
162    * user.
163    */
164   void SetEnabled(int) override;
165 
166   ///@{
167   /**
168    * If AutoAdjustCameraClippingRange is on, then before each render the
169    * camera clipping range will be adjusted to "fit" the whole scene. Clipping
170    * will still occur if objects in the scene are behind the camera or
171    * come very close. If AutoAdjustCameraClippingRange is off, no adjustment
172    * will be made per render, but the camera clipping range will still
173    * be reset when the camera is reset.
174    */
175   vtkSetClampMacro(AutoAdjustCameraClippingRange, vtkTypeBool, 0, 1);
176   vtkGetMacro(AutoAdjustCameraClippingRange, vtkTypeBool);
177   vtkBooleanMacro(AutoAdjustCameraClippingRange, vtkTypeBool);
178   ///@}
179 
180   /**
181    * When an event occurs, we must determine which Renderer the event
182    * occurred within, since one RenderWindow may contain multiple
183    * renderers.
184    */
185   void FindPokedRenderer(int, int);
186 
187   ///@{
188   /**
189    * Some useful information for interaction
190    */
191   vtkGetMacro(State, int);
192   ///@}
193 
194   ///@{
195   /**
196    * Set/Get timer hint
197    */
198   vtkGetMacro(UseTimers, vtkTypeBool);
199   vtkSetMacro(UseTimers, vtkTypeBool);
200   vtkBooleanMacro(UseTimers, vtkTypeBool);
201   ///@}
202 
203   ///@{
204   /**
205    * If using timers, specify the default timer interval (in
206    * milliseconds). Care must be taken when adjusting the timer interval from
207    * the default value of 10 milliseconds--it may adversely affect the
208    * interactors.
209    */
210   vtkSetClampMacro(TimerDuration, unsigned long, 1, 100000);
211   vtkGetMacro(TimerDuration, unsigned long);
212   ///@}
213 
214   ///@{
215   /**
216    * Does ProcessEvents handle observers on this class or not
217    */
218   vtkSetMacro(HandleObservers, vtkTypeBool);
219   vtkGetMacro(HandleObservers, vtkTypeBool);
220   vtkBooleanMacro(HandleObservers, vtkTypeBool);
221   ///@}
222 
223   /**
224    * Generic event bindings can be overridden in subclasses
225    */
OnMouseMove()226   virtual void OnMouseMove() {}
OnLeftButtonDown()227   virtual void OnLeftButtonDown() {}
OnLeftButtonUp()228   virtual void OnLeftButtonUp() {}
OnMiddleButtonDown()229   virtual void OnMiddleButtonDown() {}
OnMiddleButtonUp()230   virtual void OnMiddleButtonUp() {}
OnRightButtonDown()231   virtual void OnRightButtonDown() {}
OnRightButtonUp()232   virtual void OnRightButtonUp() {}
OnLeftButtonDoubleClick()233   virtual void OnLeftButtonDoubleClick() {}
OnMiddleButtonDoubleClick()234   virtual void OnMiddleButtonDoubleClick() {}
OnRightButtonDoubleClick()235   virtual void OnRightButtonDoubleClick() {}
OnMouseWheelForward()236   virtual void OnMouseWheelForward() {}
OnMouseWheelBackward()237   virtual void OnMouseWheelBackward() {}
OnMouseWheelLeft()238   virtual void OnMouseWheelLeft() {}
OnMouseWheelRight()239   virtual void OnMouseWheelRight() {}
OnFourthButtonDown()240   virtual void OnFourthButtonDown() {}
OnFourthButtonUp()241   virtual void OnFourthButtonUp() {}
OnFifthButtonDown()242   virtual void OnFifthButtonDown() {}
OnFifthButtonUp()243   virtual void OnFifthButtonUp() {}
244 
245   /**
246    * Generic 3D event bindings can be overridden in subclasses
247    */
OnMove3D(vtkEventData *)248   virtual void OnMove3D(vtkEventData*) {}
OnButton3D(vtkEventData *)249   virtual void OnButton3D(vtkEventData*) {}
OnPick3D(vtkEventData *)250   virtual void OnPick3D(vtkEventData*) {}
OnClip3D(vtkEventData *)251   virtual void OnClip3D(vtkEventData*) {}
OnSelect3D(vtkEventData *)252   virtual void OnSelect3D(vtkEventData*) {}
OnMenu3D(vtkEventData *)253   virtual void OnMenu3D(vtkEventData*) {}
OnNextPose3D(vtkEventData *)254   virtual void OnNextPose3D(vtkEventData*) {}
OnPositionProp3D(vtkEventData *)255   virtual void OnPositionProp3D(vtkEventData*) {}
OnViewerMovement3D(vtkEventData *)256   virtual void OnViewerMovement3D(vtkEventData*) {}
257 
258   /**
259    * OnChar is triggered when an ASCII key is pressed. Some basic key presses
260    * are handled here ('q' for Quit, 'p' for Pick, etc)
261    */
262   void OnChar() override;
263 
264   // OnKeyDown is triggered by pressing any key (identical to OnKeyPress()).
265   // An empty implementation is provided. The behavior of this function should
266   // be specified in the subclass.
OnKeyDown()267   virtual void OnKeyDown() {}
268 
269   // OnKeyUp is triggered by releaseing any key (identical to OnKeyRelease()).
270   // An empty implementation is provided. The behavior of this function should
271   // be specified in the subclass.
OnKeyUp()272   virtual void OnKeyUp() {}
273 
274   // OnKeyPress is triggered by pressing any key (identical to OnKeyDown()).
275   // An empty implementation is provided. The behavior of this function should
276   // be specified in the subclass.
OnKeyPress()277   virtual void OnKeyPress() {}
278 
279   // OnKeyRelease is triggered by pressing any key (identical to OnKeyUp()).
280   // An empty implementation is provided. The behavior of this function should
281   // be specified in the subclass.
OnKeyRelease()282   virtual void OnKeyRelease() {}
283 
284   /**
285    * These are more esoteric events, but are useful in some cases.
286    */
OnExpose()287   virtual void OnExpose() {}
OnConfigure()288   virtual void OnConfigure() {}
OnEnter()289   virtual void OnEnter() {}
OnLeave()290   virtual void OnLeave() {}
291 
292   /**
293    * OnTimer calls Rotate, Rotate etc which should be overridden by
294    * style subclasses.
295    */
296   virtual void OnTimer();
297 
298   /**
299    * These methods for the different interactions in different modes
300    * are overridden in subclasses to perform the correct motion. Since
301    * they might be called from OnTimer, they do not have mouse coord parameters
302    * (use interactor's GetEventPosition and GetLastEventPosition)
303    */
Rotate()304   virtual void Rotate() {}
Spin()305   virtual void Spin() {}
Pan()306   virtual void Pan() {}
Dolly()307   virtual void Dolly() {}
Zoom()308   virtual void Zoom() {}
UniformScale()309   virtual void UniformScale() {}
EnvironmentRotate()310   virtual void EnvironmentRotate() {}
311 
312   /**
313    * gesture based events
314    */
OnStartSwipe()315   virtual void OnStartSwipe() {}
OnSwipe()316   virtual void OnSwipe() {}
OnEndSwipe()317   virtual void OnEndSwipe() {}
OnStartPinch()318   virtual void OnStartPinch() {}
OnPinch()319   virtual void OnPinch() {}
OnEndPinch()320   virtual void OnEndPinch() {}
OnStartRotate()321   virtual void OnStartRotate() {}
OnRotate()322   virtual void OnRotate() {}
OnEndRotate()323   virtual void OnEndRotate() {}
OnStartPan()324   virtual void OnStartPan() {}
OnPan()325   virtual void OnPan() {}
OnEndPan()326   virtual void OnEndPan() {}
OnTap()327   virtual void OnTap() {}
OnLongTap()328   virtual void OnLongTap() {}
329 
330   ///@{
331   /**
332    * utility routines used by state changes
333    */
334   virtual void StartState(int newstate);
335   virtual void StopState();
336   ///@}
337 
338   ///@{
339   /**
340    * Interaction mode entry points used internally.
341    */
342   virtual void StartAnimate();
343   virtual void StopAnimate();
344   virtual void StartRotate();
345   virtual void EndRotate();
346   virtual void StartZoom();
347   virtual void EndZoom();
348   virtual void StartPan();
349   virtual void EndPan();
350   virtual void StartSpin();
351   virtual void EndSpin();
352   virtual void StartDolly();
353   virtual void EndDolly();
354   virtual void StartUniformScale();
355   virtual void EndUniformScale();
356   virtual void StartTimer();
357   virtual void EndTimer();
358   virtual void StartTwoPointer();
359   virtual void EndTwoPointer();
360   virtual void StartGesture();
361   virtual void EndGesture();
362   virtual void StartEnvRotate();
363   virtual void EndEnvRotate();
364   ///@}
365 
366   /**
367    * When the mouse location is updated while dragging files.
368    * The argument contains the position relative to the window of the mouse
369    * where the files are dropped.
370    * It is called before OnDropFiles.
371    */
OnDropLocation(double * vtkNotUsed (position))372   virtual void OnDropLocation(double* vtkNotUsed(position)) {}
373 
374   /**
375    * When files are dropped on the render window.
376    * The argument contains the list of file paths dropped.
377    * It is called after OnDropLocation.
378    */
OnDropFiles(vtkStringArray * vtkNotUsed (filePaths))379   virtual void OnDropFiles(vtkStringArray* vtkNotUsed(filePaths)) {}
380 
381   ///@{
382   /**
383    * When picking successfully selects an actor, this method highlights the
384    * picked prop appropriately. Currently this is done by placing a bounding
385    * box around a picked vtkProp3D, and using the PickColor to highlight a
386    * vtkProp2D.
387    */
388   virtual void HighlightProp(vtkProp* prop);
389   virtual void HighlightActor2D(vtkActor2D* actor2D);
390   virtual void HighlightProp3D(vtkProp3D* prop3D);
391   ///@}
392 
393   ///@{
394   /**
395    * Set/Get the pick color (used by default to color vtkActor2D's).
396    * The color is expressed as red/green/blue values between (0.0,1.0).
397    */
398   vtkSetVector3Macro(PickColor, double);
399   vtkGetVectorMacro(PickColor, double, 3);
400   ///@}
401 
402   ///@{
403   /**
404    * Set/Get the mouse wheel motion factor. Default to 1.0. Set it to a
405    * different value to emphasize or de-emphasize the action triggered by
406    * mouse wheel motion.
407    */
408   vtkSetMacro(MouseWheelMotionFactor, double);
409   vtkGetMacro(MouseWheelMotionFactor, double);
410   ///@}
411 
412   ///@{
413   /**
414    * 3Dconnexion device interactor style. Initial value is a pointer to an
415    * object of class vtkTdxInteractorStyleCamera.
416    */
417   vtkGetObjectMacro(TDxStyle, vtkTDxInteractorStyle);
418   virtual void SetTDxStyle(vtkTDxInteractorStyle* tdxStyle);
419   ///@}
420 
421   /**
422    * Called by the callback to process 3DConnexion device events.
423    */
424   void DelegateTDxEvent(unsigned long event, void* calldata);
425 
426 protected:
427   vtkInteractorStyle();
428   ~vtkInteractorStyle() override;
429 
430   /**
431    * Main process event method
432    */
433   static void ProcessEvents(
434     vtkObject* object, unsigned long event, void* clientdata, void* calldata);
435 
436   // Keep track of current state
437   int State;
438   int AnimState;
439 
440   // Should observers be handled here, should we fire timers
441   vtkTypeBool HandleObservers;
442   vtkTypeBool UseTimers;
443   int TimerId; // keep track of the timers that are created/destroyed
444 
445   vtkTypeBool AutoAdjustCameraClippingRange;
446 
447   // For picking and highlighting props
448   vtkOutlineSource* Outline;
449   vtkPolyDataMapper* OutlineMapper;
450   vtkActor* OutlineActor;
451   vtkRenderer* PickedRenderer;
452   vtkProp* CurrentProp;
453   vtkActor2D* PickedActor2D;
454   int PropPicked;      // bool: prop picked?
455   double PickColor[3]; // support 2D picking
456   double MouseWheelMotionFactor;
457 
458   // Control the timer duration
459   unsigned long TimerDuration; // in milliseconds
460 
461   // Forward events to the RenderWindowInteractor
462   vtkEventForwarderCommand* EventForwarder;
463 
464   vtkTDxInteractorStyle* TDxStyle;
465 
466 private:
467   vtkInteractorStyle(const vtkInteractorStyle&) = delete;
468   void operator=(const vtkInteractorStyle&) = delete;
469 };
470 
471 #endif
472