[go: up one dir, main page]

File: DragSensor.cpp

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (233 lines) | stat: -rw-r--r-- 6,182 bytes parent folder | download | duplicates (2)
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
/**************************************************************************\
 *
 *  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 SoVRMLDragSensor SoVRMLDragSensor.h Inventor/VRMLnodes/SoVRMLDragSensor.h
  \brief The SoVRMLDragSensor class is a superclass for VRML drag sensors.

  This class collects the two fields that are common for some of the
  sensor nodes, plus some common functions for these. Since this is an
  abstract "helper" class, it does not represent an actual node from
  the VRML97 specification, so don't use it as such.

  For more information, a detailed discussion of drag sensors is
  available in section 4.6.7.4 of the VRML97 specification:

  <http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.7.4>.
*/

#include <Inventor/VRMLnodes/SoVRMLDragSensor.h>

#include <Inventor/VRMLnodes/SoVRMLMacros.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/elements/SoViewportRegionElement.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/events/SoLocation2Event.h>
#include <Inventor/events/SoMouseButtonEvent.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/SbMatrix.h>

#include "nodes/SoSubNodeP.h"

/*!
  \var SoSFVec3f SoVRMLDragSensor::trackPoint_changed

  An event out that is generated when the trackpoint is changed.
*/

/*!
  \var SoSFBool SoVRMLDragSensor::autoOffset

  When \c TRUE, the offset field is updated when a dragging sequence
  is finished.  Default value is \c TRUE.
*/

/*!
  \fn SbBool SoVRMLDragSensor::dragStart(void)

  \COININTERNAL

  Called when dragger is selected (picked) by the user.
*/
/*!
  \fn void SoVRMLDragSensor::drag(void)

  \COININTERNAL

  Called when user drags the mouse after picking the dragger.
*/
/*!
  \fn void SoVRMLDragSensor::dragFinish(void)

  \COININTERNAL

  Called when mouse button is released after picking and interacting
  with the dragger.
*/


SO_NODE_ABSTRACT_SOURCE(SoVRMLDragSensor);

// Doc in parent
void
SoVRMLDragSensor::initClass(void)
{
  SO_NODE_INTERNAL_INIT_ABSTRACT_CLASS(SoVRMLDragSensor, SO_VRML97_NODE_TYPE);
}

/*!
  Constructor.
*/
SoVRMLDragSensor::SoVRMLDragSensor(void)
{
  SO_NODE_CONSTRUCTOR(SoVRMLDragSensor);

  SO_VRMLNODE_ADD_EVENT_OUT(trackPoint_changed);
  SO_VRMLNODE_ADD_EXPOSED_FIELD(autoOffset, (TRUE));
}

/*!
  Destructor.
*/
SoVRMLDragSensor::~SoVRMLDragSensor()
{
}

// Doc in parent
void
SoVRMLDragSensor::handleEvent(SoHandleEventAction * action)
{
  if (!this->enabled.getValue()) {
    if (this->isActive.getValue()) {
      this->isActive = FALSE;
      this->dragFinish();
    }
    inherited::handleEvent(action);
    return;
  }

  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 active = this->isActive.getValue();

  if ((!active && mousemove) ||
      (active && buttondown) ||
      (!active && buttonup)) {
    inherited::handleEvent(action);
    return;
  }

  SoState * state = action->getState();

  this->mousepos = event->getPosition();
  this->normpos = event->getNormalizedPosition(SoViewportRegionElement::get(state));
  this->viewvolume = SoViewVolumeElement::get(state);

  if (!active && buttondown) {
    SbBool hit = FALSE;
    const SoPickedPoint * pp = action->getPickedPoint();
    if (pp) {
      const SoFullPath * currpath = (const SoFullPath*) action->getCurPath();
      SoFullPath * parentpath = (SoFullPath*) currpath->copy(0, currpath->getLength()-1);
      SoNode * parentnode = parentpath->getTail();
      parentpath->ref();
      hit = pp->getPath()->containsPath(parentpath);
      parentpath->unref();

      if (hit) {
        // set object space hit point
        this->obj2world = pp->getObjectToWorld(parentnode);
        this->world2obj = this->obj2world.inverse();
        this->world2obj.multVecMatrix(pp->getPoint(), this->hitpt);
        if (this->dragStart()) {
          this->isActive = TRUE;
        }
      }
    }
  }
  else if (active && buttonup) {
    this->dragFinish();
    this->isActive = FALSE;
  }
  else if (active && mousemove) {
    this->drag();
  }
  inherited::handleEvent(action);
}

/*!
  Returns the interaction starting point.
*/
const SbVec3f &
SoVRMLDragSensor::getLocalStartingPoint(void) const
{
  return this->hitpt;
}

/*!
  Returns the matrix that transforms to the world coordinate system.
*/
const SbMatrix &
SoVRMLDragSensor::getLocalToWorldMatrix(void) const
{
  return this->obj2world;
}

/*!
  Returns the matrix that transforms from the world coordinate system.
*/
const SbMatrix &
SoVRMLDragSensor::getWorldToLocalMatrix(void) const
{
  return this->world2obj;
}

/*!
  Returns the current view volume.
*/
const SbViewVolume &
SoVRMLDragSensor::getViewVolume(void) const
{
  return this->viewvolume;
}

/*!
  Returns the current normalized pointer position.
*/
const SbVec2f &
SoVRMLDragSensor::getNormalizedLocaterPosition(void) const
{
  return this->normpos;
}

#endif // HAVE_VRML97