H3D API  2.4.1
H3DNavigationDevices.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __H3DNAVIGATIONDEVICES_H__
30 #define __H3DNAVIGATIONDEVICES_H__
31 
32 #include <H3D/MouseSensor.h>
33 #include <H3D/KeySensor.h>
34 #include <H3D/SFVec3f.h>
35 #include <H3D/SFRotation.h>
36 #include <H3D/SpaceWareSensor.h>
37 
38 namespace H3D {
39 
40  // Forward Declaration
41  class H3DNavigation;
42 
44  class H3DAPI_API H3DNavigationDevices {
45  public:
46 
47  struct MoveInfo{
48  public:
49  MoveInfo() {
50  zoom = false;
51  use_center_sum = false;
52  }
53  Vec3f translation_sum;
54  Rotation rotation_sum;
55  Vec3f center_of_rot_sum;
56  bool use_center_sum;
57  bool zoom;
58  };
59 
61  H3DNavigationDevices( H3DNavigation * h3d_navigation );
62 
65  for( DeviceMap::iterator i = all_devices.begin();
66  i != all_devices.end(); ++i ) {
67  (*i).second.remove( this );
68  }
69  }
70 
73  static bool getMoveInfo( MoveInfo &move_info,
74  H3DNavigation * h3d_navigation ) {
75  bool somethingmoved = false;
76  int centerCounter = 0;
77  for( list< H3DNavigationDevices * >::iterator i =
78  all_devices[h3d_navigation].begin();
79  i != all_devices[h3d_navigation].end(); ++i ) {
80  if( (*i)->shouldGetInfo->getValue() ) {
81  move_info.translation_sum += (*i)->move_dir;
82  move_info.rotation_sum = move_info.rotation_sum * (*i)->rel_rot;
83  move_info.zoom = (*i)->zoom;
84  if( (*i)->use_center ) {
85  move_info.use_center_sum = true;
86  ++centerCounter;
87  move_info.center_of_rot_sum += (*i)->getCenterOfRot();
88  }
89  (*i)->resetAll();
90  somethingmoved = true;
91  }
92  }
93  if( move_info.use_center_sum )
94  move_info.center_of_rot_sum /= centerCounter;
95  return somethingmoved;
96  }
97 
99  virtual void resetAll();
100 
102  virtual void disableDevice() {};
103 
105  virtual void enableDevice() {};
106 
108  static void setNavTypeForAll( string &_nav_type,
109  H3DNavigation * h3d_navigation ) {
110  for( list< H3DNavigationDevices * >::iterator i =
111  all_devices[h3d_navigation].begin();
112  i != all_devices[h3d_navigation].end(); ++i ) {
113  (*i)->setNavType( _nav_type );
114  }
115  }
116 
118  void setNavType( string &_nav_type ) {
119  nav_type = _nav_type;
120  }
121 
123  string getNavType( ) {
124  return nav_type;
125  }
126 
128  virtual Vec3f getCenterOfRot(){
129  return center_of_rot;
130  }
131 
132  protected:
133  // If this field is true then move_dir, rel_rot, center_of_rot, use_center
134  // and zoom contains something useful.
135  H3DUniquePtr< SFBool > shouldGetInfo;
136  // Need to map device to H3DNavigation.
137  typedef map< H3DNavigation*, list< H3DNavigationDevices * > > DeviceMap;
138  static DeviceMap all_devices;
139  // Navigation type.
140  string nav_type;
141 
142  // move_dir is the distance to move for WALK, FLY and EXAMINE mode while it
143  // is the direction to move when zooming in EXAMINE mode.
144  Vec3f move_dir;
145  // The relative rotation for navigation.
146  Rotation rel_rot;
147  // The center of rotation, only used if use_center is true.
148  Vec3f center_of_rot;
149  // If true center_of_rot is used.
150  bool use_center;
151  // If true we are in zoom mode.
152  bool zoom;
153  };
154 
155 
157  class H3DAPI_API MouseNavigation : public H3DNavigationDevices {
158  public:
160  MouseNavigation( H3DNavigation * h3d_navigation );
161 
163  virtual void resetAll();
164 
166  virtual void disableDevice();
167 
169  virtual void enableDevice();
170 
172  inline bool isEnabled() {
173  return enabled;
174  }
175 
177  void leftButtonUpdate( bool _left_button );
178 
180  void motionUpdate( int x, int y );
181 
183  void scrollWheelUpdate( bool up );
184  protected:
185  // Contains left button value
186  bool left_button;
187  // Contains mouse pos value
188  Vec2f mouse_pos;
189  // True if MouseNavigation is enabled.
190  bool enabled;
191  };
192 
194  class H3DAPI_API KeyboardNavigation : public H3DNavigationDevices {
195  public:
196 
198  KeyboardNavigation( H3DNavigation * h3d_navigation );
199 
201  virtual void resetAll();
202 
204  void handleKeyAction( int key, bool pressed );
205 
206  protected:
207  // Variables used to indicate which keys are pressed and which are not.
208  bool upPressed;
209  bool downPressed;
210  bool leftPressed;
211  bool rightPressed;
212  };
213 
215  class H3DAPI_API HapticDeviceNavigation : public H3DNavigationDevices {
216  public:
217 
218  class CalculateHapticDeviceMoveInfo :
219  public TypedField< SFBool, Types< SFBool, SFVec3f > > {
220 
221  public:
222  CalculateHapticDeviceMoveInfo() : the_owner( NULL ) {
223  button_pressed = false;
224  }
225 
226  virtual void update();
227 
228  HapticDeviceNavigation *the_owner;
229  protected:
230  bool button_pressed;
231  Rotation last_orn;
232  Vec3f last_pos;
233  Vec3f last_weight_pos;
234  };
235 #ifdef __BORLANDC__
236  friend class CalculateHapticDeviceMoveInfo;
237 #endif
238 
240  HapticDeviceNavigation( H3DNavigation * h3d_navigation );
241 
242  virtual void resetAll();
243 
244  virtual Vec3f getCenterOfRot();
245 
246  protected:
247  H3DUniquePtr< CalculateHapticDeviceMoveInfo > calculateHapticDeviceMoveInfo;
248  // Needed because the center of rotation should be calculated as late
249  // as possible and only once. Used in getCenterOfRot();
250  bool calculate_center;
251 
252  };
253 
256  class H3DAPI_API SWSNavigation : public H3DNavigationDevices {
257  public:
258 
259  class CalculateSWSMoveInfo :
260  public AutoUpdate< TypedField< SFBool, Types< SFVec3f, SFRotation, SFRotation > > > {
261 
262  public:
263  CalculateSWSMoveInfo() : the_owner( NULL ) {
264  }
265 
266  virtual void update();
267 
268  SWSNavigation *the_owner;
269  protected:
270  };
271 #ifdef __BORLANDC__
272  friend class CalculateSWSMoveInfo;
273 #endif
274 
276  SWSNavigation( H3DNavigation * h3d_navigation );
277 
278  virtual void resetAll();
279 
280  protected:
281  H3DUniquePtr< CalculateSWSMoveInfo > calculateSWSMoveInfo;
282  H3DUniquePtr< SpaceWareSensor > sws;
283  };
284 }
285 
286 #endif
Header file for KeySensor, X3D scene-graph node.
Header file for MouseSensor.
Contains the SFRotation field class.
Contains the SFVec3f field class.
Header file for SpaceWareSensor.
Base class for devices that should be used to navigate the scene.
Definition: H3DNavigationDevices.h:44
static bool getMoveInfo(MoveInfo &move_info, H3DNavigation *h3d_navigation)
Call this function to sum up movement changes from all devices returns true if there are changes.
Definition: H3DNavigationDevices.h:73
string getNavType()
Get the current type of navigation.
Definition: H3DNavigationDevices.h:123
virtual ~H3DNavigationDevices()
Destructor.
Definition: H3DNavigationDevices.h:64
static void setNavTypeForAll(string &_nav_type, H3DNavigation *h3d_navigation)
sets type of navigation for all devices
Definition: H3DNavigationDevices.h:108
virtual Vec3f getCenterOfRot()
Get the center or rotation.
Definition: H3DNavigationDevices.h:128
virtual void disableDevice()
virtual function to remove routes for this device.
Definition: H3DNavigationDevices.h:102
virtual void enableDevice()
virtual function to enable routes for this device.
Definition: H3DNavigationDevices.h:105
void setNavType(string &_nav_type)
Set type of navigation.
Definition: H3DNavigationDevices.h:118
H3DNavigation is a class used for navigation in H3DAPI.
Definition: H3DNavigation.h:41
Takes care of navigation using a haptics device.
Definition: H3DNavigationDevices.h:215
Takes care of navigation using keyboard.
Definition: H3DNavigationDevices.h:194
Take care of mouseNavigation.
Definition: H3DNavigationDevices.h:157
bool isEnabled()
Check if MouseNavigation is enabled.
Definition: H3DNavigationDevices.h:172
Takes care of navigation with SpaceWareSensor.
Definition: H3DNavigationDevices.h:256
A template modifier class for adding type checking on the routes to any Field class.
Definition: TypedField.h:84
Rotation()
H3DDouble x
Vec2f()
H3D API namespace.
Definition: Anchor.h:38
The AutoUpdate field is a template to force the BaseField to update itself as soon as an event is rec...
Definition: FieldTemplates.h:130