]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Interaction/BaseStyle.h
0a70dea38c90319ae1394fba9d3a62caa43cc982
[cpPlugins.git] / lib / cpExtensions / Interaction / BaseStyle.h
1 #ifndef __cpExtensions__Interaction__BaseStyle__h__
2 #define __cpExtensions__Interaction__BaseStyle__h__
3
4 #include <cpExtensions/Config.h>
5 #include <cpExtensions/Utility.h>
6 #include <vtkInteractorStyle.h>
7 #include <map>
8
9 /* =========================================================================
10  * Double click algorithm inspired from:
11  * http://www.autohotkey.com/board/topic/56493-easiest-way-to-detect-double-clicks/
12  * =========================================================================
13  */
14
15 // -------------------------------------------------------------------------
16 #define cpExtensions_BaseStyle_Commands( C )                            \
17   protected:                                                            \
18   std::map< T##C##Command, void* > m_##C##Commands;                     \
19   public:                                                               \
20   inline void Add##C##Command( T##C##Command c, void* d )               \
21   {                                                                     \
22     if( c != NULL )                                                     \
23     {                                                                   \
24       this->m_##C##Commands[ c ] = d;                                   \
25       this->Modified( );                                                \
26     }                                                                   \
27   }                                                                     \
28   inline void Remove##C##Command( T##C##Command c )                     \
29   {                                                                     \
30     std::map< T##C##Command, void* >::iterator i =                      \
31       this->m_##C##Commands.find( c );                                  \
32     if( i != this->m_##C##Commands.end( ) )                             \
33     {                                                                   \
34       this->m_##C##Commands.erase( i );                                 \
35       this->Modified( );                                                \
36     }                                                                   \
37   }
38
39 // -------------------------------------------------------------------------
40 namespace cpExtensions
41 {
42   namespace Interaction
43   {
44     /**
45      */
46     class cpExtensions_EXPORT BaseStyle
47       : public vtkInteractorStyle
48     {
49     public:
50       typedef BaseStyle Self;
51       vtkTypeMacro( BaseStyle, vtkInteractorStyle );
52
53       enum ButtonID
54       {
55         ButtonID_None = 0,
56         ButtonID_Left,
57         ButtonID_Middle,
58         ButtonID_Right
59       };
60
61       // Callbacks types
62       typedef void ( *TMouseCommand )(
63         void*, const ButtonID&, int*, double*, bool, bool, bool
64         );
65       typedef TMouseCommand TMouseClickCommand;
66       typedef TMouseCommand TMouseDoubleClickCommand;
67
68       // Associate callbacks for each event
69       cpExtensions_BaseStyle_Commands( MouseClick );
70       cpExtensions_BaseStyle_Commands( MouseDoubleClick );
71
72     public:
73       static void SetSetDoubleClickDelay( long delay );
74       void DelegateTDxEvent( unsigned long event, void* calldata );
75
76       // Possible mouse motion events
77       virtual void OnMouseMove( ) cpExtensions_OVERRIDE;
78       virtual void OnMouseWheelForward( )  cpExtensions_OVERRIDE { }
79       virtual void OnMouseWheelBackward( ) cpExtensions_OVERRIDE { }
80
81       // Possible mouse click-related events
82       inline ButtonID GetButtonID( ) const { return( this->m_ActiveButton ); }
83
84       virtual void OnLeftButtonDown( ) cpExtensions_OVERRIDE;
85       virtual void OnLeftButtonUp( ) cpExtensions_OVERRIDE;
86       virtual void OnMiddleButtonDown( ) cpExtensions_OVERRIDE;
87       virtual void OnMiddleButtonUp( ) cpExtensions_OVERRIDE;
88       virtual void OnRightButtonDown( ) cpExtensions_OVERRIDE;
89       virtual void OnRightButtonUp( ) cpExtensions_OVERRIDE;
90
91       virtual void OnLeftClick( );
92       virtual void OnLeftDoubleClick( );
93       virtual void OnMiddleClick( );
94       virtual void OnMiddleDoubleClick( );
95       virtual void OnRightClick( );
96       virtual void OnRightDoubleClick( );
97
98       // Keyboard-related events
99       virtual void OnChar( ) cpExtensions_OVERRIDE       { }
100       virtual void OnKeyDown( ) cpExtensions_OVERRIDE    { }
101       virtual void OnKeyUp( ) cpExtensions_OVERRIDE      { }
102       virtual void OnKeyPress( ) cpExtensions_OVERRIDE   { }
103       virtual void OnKeyRelease( ) cpExtensions_OVERRIDE { }
104
105       // Other events
106       virtual void OnExpose( ) cpExtensions_OVERRIDE    { }
107       virtual void OnConfigure( ) cpExtensions_OVERRIDE { }
108       virtual void OnEnter( ) cpExtensions_OVERRIDE     { }
109       virtual void OnLeave( ) cpExtensions_OVERRIDE     { }
110
111       virtual void Dolly( ) cpExtensions_OVERRIDE;
112       virtual void Pan( ) cpExtensions_OVERRIDE;
113
114     protected:
115       BaseStyle( );
116       virtual ~BaseStyle( );
117
118       virtual void _Dolly( double factor );
119
120       // Extension interface
121       virtual bool _PickPosition( int idx[ 2 ], double pos[ 3 ] ) = 0;
122
123       // Main event callback
124       static void _ProcessEvents(
125         vtkObject* object,
126         unsigned long event,
127         void* clientdata,
128         void* calldata
129         );
130
131     private:
132       // Purposely not implemented
133       BaseStyle( const Self& );
134       Self& operator=( const Self& );
135
136     protected:
137       double m_MotionFactor;
138
139       /**
140        * Button events
141        */
142       struct _TMouseButtonEvent
143       {
144         static long MaxDoubleClick;
145         long m_LastButtonUp;
146         long m_LastButtonHeld;
147         long m_LastButtonDown;
148
149         inline _TMouseButtonEvent( )
150           { this->Reset( ); }
151         inline void Reset( )
152           {
153             this->m_LastButtonUp = 0;
154             this->m_LastButtonHeld = 0;
155             this->m_LastButtonDown = -1;
156           }
157         inline void Release( )
158           {
159             long c = cpExtensions_CHRONO;
160             this->m_LastButtonUp = c;
161             this->m_LastButtonHeld = c - this->m_LastButtonDown;
162             this->m_LastButtonDown = -1;
163           }
164         inline unsigned char Clicks( )
165           {
166             unsigned char n = 0;
167             long c = cpExtensions_CHRONO;
168             if(
169               this->m_LastButtonHeld < MaxDoubleClick &&
170               ( c - this->m_LastButtonUp ) < MaxDoubleClick
171               )
172             {
173               this->Reset( );
174               n = 2;
175             }
176             else
177               n = 1;
178             if( this->m_LastButtonDown < 0 )
179               this->m_LastButtonDown = c;
180             return( n );
181           }
182       };
183       _TMouseButtonEvent m_LeftButtonEvent;
184       _TMouseButtonEvent m_MiddleButtonEvent;
185       _TMouseButtonEvent m_RightButtonEvent;
186       ButtonID           m_ActiveButton;
187     };
188
189   } // ecapseman
190
191 } // ecapseman
192
193 #endif // __cpExtensions__Interaction__BaseStyle__h__
194
195 // eof - $RCSfile$