1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#ifdef HAVE_VRML97
/*!
\class SoVRMLTouchSensor SoVRMLTouchSensor.h Inventor/VRMLnodes/SoVRMLTouchSensor.h
\brief The SoVRMLTouchSensor class tracks to pointer position and sends events based on user interaction.
\ingroup VRMLnodes
\WEB3DCOPYRIGHT
\verbatim
TouchSensor {
exposedField SFBool enabled TRUE
eventOut SFVec3f hitNormal_changed
eventOut SFVec3f hitPoint_changed
eventOut SFVec2f hitTexCoord_changed
eventOut SFBool isActive
eventOut SFBool isOver
eventOut SFTime touchTime
}
\endverbatim
A TouchSensor node tracks the location and state of the pointing
device and detects when the user points at geometry contained by the
TouchSensor node's parent group. A TouchSensor node can be enabled
or disabled by sending it an enabled event with a value of TRUE or
FALSE. If the TouchSensor node is disabled, it does not track user
input or send events.
The TouchSensor generates events when the pointing device points toward
any geometry nodes that are descendants of the TouchSensor's parent group.
See 4.6.7.5, Activating and manipulating sensors
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.7.5>),
for more details on using the pointing device to activate the TouchSensor.
The \e isOver eventOut reflects the state of the pointing device
with regard to whether it is pointing towards the TouchSensor node's
geometry or not. When the pointing device changes state from a
position such that its bearing does not intersect any of the
TouchSensor node's geometry to one in which it does intersect
geometry, an isOver TRUE event is generated. When the pointing
device moves from a position such that its bearing intersects
geometry to one in which it no longer intersects the geometry, or
some other geometry is obstructing the TouchSensor node's geometry,
an isOver FALSE event is generated. These events are generated only
when the pointing device has moved and changed `over' state. Events
are not generated if the geometry itself is animating and moving
underneath the pointing device.
As the user moves the bearing over the TouchSensor node's geometry,
the point of intersection (if any) between the bearing and the
geometry is determined. Each movement of the pointing device, while
isOver is TRUE, generates hitPoint_changed, hitNormal_changed and
hitTexCoord_changed events. hitPoint_changed events contain the 3D
point on the surface of the underlying geometry, given in the
TouchSensor node's coordinate system. hitNormal_changed events
contain the surface normal vector at the
hitPoint. hitTexCoord_changed events contain the texture coordinates
of that surface at the hitPoint. The values of hitTexCoord_changed
and hitNormal_changed events are computed as appropriate for the
associated shape.
If isOver is TRUE, the user may activate the pointing device to
cause the TouchSensor node to generate isActive events (e.g., by
pressing the primary mouse button). When the TouchSensor node
generates an isActive TRUE event, it grabs all further motion events
from the pointing device until it is released and generates an
isActive FALSE event (other pointing-device sensors will not
generate events during this time). Motion of the pointing device
while isActive is TRUE is termed a "drag." If a 2D pointing device
is in use, isActive events reflect the state of the primary button
associated with the device (i.e., isActive is TRUE when the primary
button is pressed and FALSE when it is released). If a 3D pointing
device is in use, isActive events will typically reflect whether the
pointing device is within (or in contact with) the TouchSensor
node's geometry.
The eventOut field touchTime is generated when all three of the
following conditions are true:
- The pointing device was pointing towards the geometry when it
was initially activated (isActive is TRUE).
- The pointing device is currently pointing towards the geometry (isOver is TRUE).
- The pointing device is deactivated (isActive FALSE event
is also generated).
More information about this behaviour is described in 4.6.7.3,
Pointing-device sensors
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.7.3>),
4.6.7.4, Drag sensors
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.7.4>),
and 4.6.7.5, Activating and manipulating sensors
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.7.5>).
\ENDWEB3D
Here is a small example which demonstrates use of the
SoVRMLTouchSensor. Click on the SoVRMLSphere to turn the
SoVRMLPointLight on:
\code
#VRML V2.0 utf8
Group {
children [
Transform {
children [
DEF light PointLight {
intensity 1
on FALSE
}
Transform {
translation -2 0 -2
children [
Shape {
appearance Appearance {
material Material {
diffuseColor 1 0 1
specularColor 1 1 1
shininess 0.9
}
}
geometry Sphere { }
}
DEF touchsensor TouchSensor { }
]
}
]
}
]
ROUTE touchsensor.isActive TO light.set_on
}
\endcode
*/
/*!
\var SoSFBool SoVRMLTouchSensor::enabled
TRUE is enabled. Default value is TRUE.
*/
/*!
\var SoSFVec3f SoVRMLTouchSensor::hitNormal_changed
An eventOut that is sent when the pointer is over some child
geometry. Contains the object space normal.
*/
/*!
\var SoSFVec3f SoVRMLTouchSensor::hitPoint_changed
An eventOut that is sent when the pointer is over some child
geometry. Contains the object space point.
*/
/*!
\var SoSFVec2f SoVRMLTouchSensor::hitTexCoord_changed
An eventOut that is sent when the pointer is over some child
geometry. Contains the object space texture coordinates.
*/
/*!
\var SoSFBool SoVRMLTouchSensor::isActive
An event out that is sent when the active state changes. The sensor
goes active when the user clicks on some child, and is inactive
again when the mouse button is release.
*/
/*!
\var SoSFBool SoVRMLTouchSensor::isOver
An event out that is sent when the pointer is moved onto or away from
a child object.
*/
/*!
\var SoSFTime SoVRMLTouchSensor::touchTime
An event out that is generated when the sensor is active and is
currently pointing on some child geometry.
*/
#include <Inventor/VRMLnodes/SoVRMLTouchSensor.h>
#include <Inventor/VRMLnodes/SoVRMLMacros.h>
#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/events/SoLocation2Event.h>
#include <Inventor/events/SoMouseButtonEvent.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/SoDB.h>
#include <Inventor/fields/SoSFTime.h>
#include <Inventor/SbMatrix.h>
#include "nodes/SoSubNodeP.h"
//
// returns the current time. First tries the realTime field, then
// SbTime::getTimeOfDay() if field is not found.
//
static SbTime
touchsensor_get_current_time(void)
{
SoField * realtime = SoDB::getGlobalField("realTime");
if (realtime && realtime->isOfType(SoSFTime::getClassTypeId())) {
return ((SoSFTime*)realtime)->getValue();
}
return SbTime::getTimeOfDay();
}
SO_NODE_SOURCE(SoVRMLTouchSensor);
// Doc in parent
void
SoVRMLTouchSensor::initClass(void)
{
SO_NODE_INTERNAL_INIT_CLASS(SoVRMLTouchSensor, SO_VRML97_NODE_TYPE);
}
/*!
Constructor.
*/
SoVRMLTouchSensor::SoVRMLTouchSensor(void)
{
SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLTouchSensor);
SO_VRMLNODE_ADD_EXPOSED_FIELD(enabled, (TRUE));
SO_VRMLNODE_ADD_EVENT_OUT(hitNormal_changed);
SO_VRMLNODE_ADD_EVENT_OUT(hitPoint_changed);
SO_VRMLNODE_ADD_EVENT_OUT(hitTexCoord_changed);
SO_VRMLNODE_ADD_EVENT_OUT(isActive);
SO_VRMLNODE_ADD_EVENT_OUT(isOver);
SO_VRMLNODE_ADD_EVENT_OUT(touchTime);
this->isactive = FALSE;
}
/*!
Destructor.
*/
SoVRMLTouchSensor::~SoVRMLTouchSensor()
{
}
// Doc in parent
SbBool
SoVRMLTouchSensor::affectsState(void) const // virtual
{
return TRUE;
}
// Doc in parent
void
SoVRMLTouchSensor::handleEvent(SoHandleEventAction * action)
{
const SoEvent * event = action->getEvent();
SbBool buttondown = SO_MOUSE_PRESS_EVENT(event, BUTTON1);
SbBool buttonup = SO_MOUSE_RELEASE_EVENT(event, BUTTON1);
SbBool mousemove = event->isOfType(SoLocation2Event::getClassTypeId());
SbBool wasover = this->isOver.getValue();
if (mousemove || buttondown || buttonup) {
SbBool isover = FALSE;
const SoPickedPoint * pp = action->getPickedPoint();
SoNode * parentnode = NULL;
if (pp) {
const SoFullPath * currpath = (const SoFullPath*) action->getCurPath();
SoFullPath * parentpath = (SoFullPath*) currpath->copy(0, currpath->getLength()-1);
parentnode = parentpath->getTail();
parentpath->ref();
isover = pp->getPath()->containsPath(parentpath);
parentpath->unref();
}
SbBool active = this->isactive;
if (active && buttonup) {
this->isActive.setValue(FALSE);
this->isactive = FALSE;
}
if (!active && buttondown && isover) {
this->isActive.setValue(TRUE);
this->isactive = TRUE;
active = TRUE;
}
if (wasover != isover) {
this->isOver.setValue(isover);
}
if (isover) {
if (active) {
this->touchTime.setValue(touchsensor_get_current_time());
}
SbMatrix transform = pp->getWorldToObject(parentnode);
SbVec3f normal = pp->getNormal();
transform.multDirMatrix(normal, normal);
this->hitNormal_changed = normal;
SbVec3f pt = pp->getPoint();
transform.multVecMatrix(pt, pt);
this->hitPoint_changed = pt;
transform = pp->getImageToObject(parentnode);
SbVec4f tc = pp->getTextureCoords();
transform.multVecMatrix(tc, tc);
float div = tc[3] ? 1.0f / tc[3] : 1.0f;
SbVec2f tc2(tc[0] * div, tc[1] * div);
this->hitTexCoord_changed = tc2;
}
}
inherited::handleEvent(action);
}
// Doc in parent
void
SoVRMLTouchSensor::notify(SoNotList * list)
{
inherited::notify(list);
}
#endif // HAVE_VRML97
|