H3D API  2.4.1
X3DPointingDeviceSensorNode.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 __X3DPOINTINGDEVICESENSORNODE_H__
30 #define __X3DPOINTINGDEVICESENSORNODE_H__
31 
32 #include <H3D/X3DSensorNode.h>
33 #include <H3D/X3DGeometryNode.h>
34 #include <H3D/SFString.h>
35 
36 namespace H3D {
37 
108  class H3DAPI_API X3DPointingDeviceSensorNode :
109  public X3DSensorNode {
110  public:
111 
113  X3DPointingDeviceSensorNode( Inst< SFString > _description = 0,
114  Inst< SFBool > _enabled = 0,
115  Inst< SFNode > _metadata = 0,
116  Inst< SFBool > _isActive = 0,
117  Inst< SFBool > _isOver = 0 );
118 
119  virtual ~X3DPointingDeviceSensorNode();
120 
121  // Fields
130  H3DUniquePtr< SFString > description;
131 
149  H3DUniquePtr< SFBool > isOver;
150 
156  static void updateX3DPointingDeviceSensors( Node * n,
157  const Vec3f &from,
158  const Vec3f &to );
159 
162  static void updateButtonDependentFields( bool primary_button );
163 
166  static bool anyIsOver();
167 
170  static bool anyIsActive();
171 
173  static inline bool instancesExists() {
174  return !instances.empty();
175  }
176 
179 
180  protected:
181 
182  // The SetIsEnabled class is specialized to call setIsEnabled function
183  // when the enabled field chances value. If enabled is set to true while
184  // the primary pointing device button is pressed then no events are sent
185  // until the primary pointing device button is released.
186  // If the enabled field is set to false and isActive and isOver are true
187  // then those are set to false.
188  //
189  // - routes_in[0] is the enabled field of X3DPointingDeviceSensorNode
190  class H3DAPI_API SetIsEnabled:
191  public AutoUpdate< SFBool > {
192  public:
193  virtual void setValue( const bool &b, int _id = 0 ) {
196  static_cast< X3DPointingDeviceSensorNode * >(owner);
197  pdsn->setIsEnabled( pdsn->last_primary_button_value );
198  }
199  protected:
200  virtual void update() {
203  static_cast< X3DPointingDeviceSensorNode * >(owner);
204  pdsn->setIsEnabled( pdsn->last_primary_button_value );
205  }
206  };
207 #ifdef __BORLANDC__
208  friend class SetIsEnabled;
209 #endif
210 
211  // This value is used in onIsOver. Set here instead of sending as argument
212  // cause it is more effective to go through all instances of
213  // X3DPointingDeviceSensorNodes twice than it is to compare pointers for
214  // all devices to know if it should be true or not.
215  bool new_value;
216 
217  // This value must be true for a sensor to become active.
218  bool lowest_enabled;
219 
220  // Called to generate isOver events if they should be
221  // generated.
222  virtual void onIsOver( NodeIntersectResult * /*result*/ = 0, unsigned int /*closest_index*/ = 0,
223  Matrix4f * /*global_to_local*/ = 0 ) {
224  if( is_enabled && ( isActive->getValue() || number_of_active == 0 ) ) {
225  if( new_value != isOver->getValue() )
226  isOver->setValue( new_value, id );
227  }
228  }
229 
230  // To indicate how many active devices there are.
231  static int number_of_active;
232 
233  // used instead of enabled to enable and disable X3DPointingDeviceSensors.
234  // correctly.
235  bool is_enabled;
236 
238  // C++ only field.
239  H3DUniquePtr< SetIsEnabled > setIsEnabledField;
240 
241  // Contains information about the last value of the primary button of the
242  // pointing device sensor and the intersection points.
243  static bool last_primary_button_value;
244  static Vec3f last_from;
245  static Vec3f last_to;
246 
248  void setIsEnabled( bool primary_button );
250  void setIsActive( bool primary_button );
251 
252  // Virtual function that should check the isActive field and if it
253  // is true then update specific fields. If isActive field is false then
254  // reset state of internal variables. Do not call enabled->getValue() in
255  // this function.
256  // _enabled - Should be treated as enabled->getValue().
257  // from - The start of the line segment for which to intersect with.
258  // to - The end of the line segment for which to intersect with.
259  virtual void setDragOutputEvents( bool /*_enabled*/,
260  const Vec3f &/*from*/,
261  const Vec3f &/*to*/ ) {};
262 
263  private:
264  // The instances of X3DPointingDeviceSensorNode that has been created.
265  static vector< X3DPointingDeviceSensorNode * > instances;
266  };
267 }
268 
269 #endif
Contains the SFString field class.
Header file for X3DGeometryNode, X3D scene-graph node.
Header file for X3DSensorNode, X3D scene-graph node.
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
This abstract node type is the base type for all pointing device sensors.
Definition: X3DPointingDeviceSensorNode.h:109
static bool instancesExists()
Returns true if there are any X3DPointingDeviceSensors in the scene.
Definition: X3DPointingDeviceSensorNode.h:173
H3DUniquePtr< SetIsEnabled > setIsEnabledField
Used to call setIsEnabled function when the enabled field is changed.
Definition: X3DPointingDeviceSensorNode.h:239
H3DUniquePtr< SFString > description
The description field in a X3DPointingDeviceSensorNode node specifies a textual description of the X3...
Definition: X3DPointingDeviceSensorNode.h:130
void setIsEnabled(bool primary_button)
Handles the enabled logic.
Definition: X3DPointingDeviceSensorNode.cpp:273
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: X3DPointingDeviceSensorNode.h:178
H3DUniquePtr< SFBool > isOver
The isOver field reflects the state of the pointing device with regard to whether it is pointing towa...
Definition: X3DPointingDeviceSensorNode.h:149
This abstract node type is the base type for all sensors.
Definition: X3DSensorNode.h:40
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
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194