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