root/OpenSceneGraph/trunk/src/osgManipulator/Translate1DDragger.cpp @ 13041

Revision 13041, 7.0 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
14
15#include <osgManipulator/Translate1DDragger>
16#include <osgManipulator/Command>
17
18#include <osg/ShapeDrawable>
19#include <osg/Geometry>
20#include <osg/LineWidth>
21#include <osg/Material>
22
23using namespace osgManipulator;
24
25Translate1DDragger::Translate1DDragger() : Dragger(), _checkForNodeInNodePath(true)
26{
27    _projector = new LineProjector;
28    setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
29    setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
30}
31
32Translate1DDragger::Translate1DDragger(const osg::Vec3d& s, const osg::Vec3d& e) : Dragger(), _checkForNodeInNodePath(true)
33{
34    _projector = new LineProjector(s,e);
35    setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
36    setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
37}
38
39Translate1DDragger::~Translate1DDragger()
40{
41}
42
43bool Translate1DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
44{
45    // Check if the dragger node is in the nodepath.
46    if (_checkForNodeInNodePath)
47    {
48        if (!pointer.contains(this)) return false;
49    }
50
51    switch (ea.getEventType())
52    {
53        // Pick start.
54        case (osgGA::GUIEventAdapter::PUSH):
55            {
56                // Get the LocalToWorld matrix for this node and set it for the projector.
57                osg::NodePath nodePathToRoot;
58                computeNodePathToRoot(*this,nodePathToRoot);
59                osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot);
60                _projector->setLocalToWorld(localToWorld);
61
62                if (_projector->project(pointer, _startProjectedPoint))
63                {
64                    // Generate the motion command.
65                    osg::ref_ptr<TranslateInLineCommand> cmd = new TranslateInLineCommand(_projector->getLineStart(),
66                                                                                          _projector->getLineEnd());
67                    cmd->setStage(MotionCommand::START);
68                    cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
69
70                    // Dispatch command.
71                    dispatch(*cmd);
72
73                    // Set color to pick color.
74                    setMaterialColor(_pickColor,*this);
75
76                    aa.requestRedraw();
77                }
78                return true;
79            }
80
81        // Pick move.
82        case (osgGA::GUIEventAdapter::DRAG):
83            {
84                osg::Vec3d projectedPoint;
85                if (_projector->project(pointer, projectedPoint))
86                {
87                    // Generate the motion command.
88                    osg::ref_ptr<TranslateInLineCommand> cmd = new TranslateInLineCommand(_projector->getLineStart(),
89                                                                                          _projector->getLineEnd());
90                    cmd->setStage(MotionCommand::MOVE);
91                    cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
92                    cmd->setTranslation(projectedPoint - _startProjectedPoint);
93
94                    // Dispatch command.
95                    dispatch(*cmd);
96
97                    aa.requestRedraw();
98                }
99                return true;
100            }
101
102        // Pick finish.
103        case (osgGA::GUIEventAdapter::RELEASE):
104            {
105                osg::Vec3d projectedPoint;
106                if (_projector->project(pointer, projectedPoint))
107                {
108                    osg::ref_ptr<TranslateInLineCommand> cmd = new TranslateInLineCommand(_projector->getLineStart(),
109                            _projector->getLineEnd());
110
111                    cmd->setStage(MotionCommand::FINISH);
112                    cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
113
114                    // Dispatch command.
115                    dispatch(*cmd);
116
117                    // Reset color.
118                    setMaterialColor(_color,*this);
119
120                    aa.requestRedraw();
121                }
122
123                return true;
124            }
125        default:
126            return false;
127    }
128}
129
130void Translate1DDragger::setupDefaultGeometry()
131{
132    // Get the line length and direction.
133    osg::Vec3 lineDir = _projector->getLineEnd()-_projector->getLineStart();
134    float lineLength = lineDir.length();
135    lineDir.normalize();
136
137    osg::Geode* geode = new osg::Geode;
138    // Create a left cone.
139    {
140        osg::Cone* cone = new osg::Cone (_projector->getLineStart(), 0.025f * lineLength, 0.10f * lineLength);
141        osg::Quat rotation;
142        rotation.makeRotate(lineDir, osg::Vec3(0.0f, 0.0f, 1.0f));
143        cone->setRotation(rotation);
144
145        geode->addDrawable(new osg::ShapeDrawable(cone));
146    }
147
148    // Create a right cone.
149    {
150        osg::Cone* cone = new osg::Cone (_projector->getLineEnd(), 0.025f * lineLength, 0.10f * lineLength);
151        osg::Quat rotation;
152        rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), lineDir);
153        cone->setRotation(rotation);
154
155        geode->addDrawable(new osg::ShapeDrawable(cone));
156    }
157
158    // Create an invisible cylinder for picking the line.
159    {
160        osg::Cylinder* cylinder = new osg::Cylinder ((_projector->getLineStart()+_projector->getLineEnd())/2, 0.015f * lineLength, lineLength);
161        osg::Quat rotation;
162        rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), lineDir);
163        cylinder->setRotation(rotation);
164        osg::Drawable* cylinderGeom = new osg::ShapeDrawable(cylinder);
165
166        setDrawableToAlwaysCull(*cylinderGeom);
167
168        geode->addDrawable(cylinderGeom);
169    }
170
171    osg::Geode* lineGeode = new osg::Geode;
172    // Create a line.
173    {
174        osg::Geometry* geometry = new osg::Geometry();
175
176        osg::Vec3Array* vertices = new osg::Vec3Array(2);
177        (*vertices)[0] = _projector->getLineStart();
178        (*vertices)[1] = _projector->getLineEnd();
179
180        geometry->setVertexArray(vertices);
181        geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
182
183        lineGeode->addDrawable(geometry);
184    }
185
186    // Turn of lighting for line and set line width.
187    lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
188    osg::LineWidth* linewidth = new osg::LineWidth();
189    linewidth->setWidth(2.0f);
190    lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
191
192    // Add line and cones to the scene.
193    addChild(lineGeode);
194    addChild(geode);
195}
Note: See TracBrowser for help on using the browser.