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

Revision 13041, 7.4 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/Translate2DDragger>
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
25Translate2DDragger::Translate2DDragger()
26{
27    _projector = new PlaneProjector(osg::Plane(0.0,1.0,0.0,0.0));
28    _polygonOffset = new osg::PolygonOffset(-1.0f,-1.0f);
29    setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
30    setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
31}
32
33Translate2DDragger::Translate2DDragger(const osg::Plane& plane)
34{
35    _projector = new PlaneProjector(plane);
36    _polygonOffset = new osg::PolygonOffset(-1.0f,-1.0f);
37    setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
38    setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
39}
40
41Translate2DDragger::~Translate2DDragger()
42{
43}
44
45bool Translate2DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
46{
47    // Check if the dragger node is in the nodepath.
48    if (!pointer.contains(this)) return false;
49
50    switch (ea.getEventType())
51    {
52        // Pick start.
53        case (osgGA::GUIEventAdapter::PUSH):
54            {
55                // Get the LocalToWorld matrix for this node and set it for the projector.
56                osg::NodePath nodePathToRoot;
57                computeNodePathToRoot(*this,nodePathToRoot);
58                osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot);
59                _projector->setLocalToWorld(localToWorld);
60
61                if (_projector->project(pointer, _startProjectedPoint))
62                {
63                    // Generate the motion command.
64                    osg::ref_ptr<TranslateInPlaneCommand> cmd = new TranslateInPlaneCommand(_projector->getPlane());
65
66                    cmd->setStage(MotionCommand::START);
67                    cmd->setReferencePoint(_startProjectedPoint);
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                    getOrCreateStateSet()->setAttributeAndModes(_polygonOffset.get(), osg::StateAttribute::ON);
76
77                    aa.requestRedraw();
78                }
79                return true;
80            }
81
82        // Pick move.
83        case (osgGA::GUIEventAdapter::DRAG):
84            {
85                osg::Vec3d projectedPoint;
86                if (_projector->project(pointer, projectedPoint))
87                {
88                    // Generate the motion command.
89                    osg::ref_ptr<TranslateInPlaneCommand> cmd = new TranslateInPlaneCommand(_projector->getPlane());
90
91                    cmd->setStage(MotionCommand::MOVE);
92                    cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
93                    cmd->setTranslation(projectedPoint - _startProjectedPoint);
94                    cmd->setReferencePoint(_startProjectedPoint);
95
96                    // Dispatch command.
97                    dispatch(*cmd);
98
99                    aa.requestRedraw();
100                }
101                return true;
102            }
103
104        // Pick finish.
105        case (osgGA::GUIEventAdapter::RELEASE):
106            {
107                osg::ref_ptr<TranslateInPlaneCommand> cmd = new TranslateInPlaneCommand(_projector->getPlane());
108
109                    cmd->setStage(MotionCommand::FINISH);
110                cmd->setReferencePoint(_startProjectedPoint);
111                cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
112
113                // Dispatch command.
114                dispatch(*cmd);
115
116                // Reset color.
117                setMaterialColor(_color,*this);
118                getOrCreateStateSet()->removeAttribute(_polygonOffset.get());
119
120                aa.requestRedraw();
121
122                return true;
123            }
124        default:
125            return false;
126    }
127}
128
129void Translate2DDragger::setupDefaultGeometry()
130{
131    // Create a line.
132    osg::Geode* lineGeode = new osg::Geode;
133    {
134        osg::Geometry* geometry = new osg::Geometry();
135
136        osg::Vec3Array* vertices = new osg::Vec3Array(2);
137        (*vertices)[0] = osg::Vec3(0.0f,0.0f,-0.5f);
138        (*vertices)[1] = osg::Vec3(0.0f,0.0f,0.5f);
139
140        geometry->setVertexArray(vertices);
141        geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
142
143        lineGeode->addDrawable(geometry);
144    }
145
146    // Turn of lighting for line and set line width.
147    osg::LineWidth* linewidth = new osg::LineWidth();
148    linewidth->setWidth(2.0f);
149    lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
150    lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
151
152    osg::Geode* geode = new osg::Geode;
153
154    // Create left cone.
155    {
156        osg::Cone* cone = new osg::Cone (osg::Vec3(0.0f, 0.0f, -0.5f), 0.025f, 0.10f);
157        osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f,0.0f,-1.0f), osg::Vec3(0.0f, 0.0f, 1.0f));
158        cone->setRotation(rotation);
159        geode->addDrawable(new osg::ShapeDrawable(cone));
160    }
161
162    // Create right cone.
163    {
164        osg::Cone* cone = new osg::Cone (osg::Vec3(0.0f, 0.0f, 0.5f), 0.025f, 0.10f);
165        geode->addDrawable(new osg::ShapeDrawable(cone));
166    }
167
168    // Create an invisible cylinder for picking the line.
169    {
170        osg::Cylinder* cylinder = new osg::Cylinder (osg::Vec3(0.0f,0.0f,0.0f), 0.015f, 1.0f);
171        osg::Drawable* drawable = new osg::ShapeDrawable(cylinder);
172        setDrawableToAlwaysCull(*drawable);
173        geode->addDrawable(drawable);
174    }
175
176    // MatrixTransform to rotate the geometry according to the normal of the plane.
177    osg::MatrixTransform* xform = new osg::MatrixTransform;
178
179    // Create an arrow in the X axis.
180    {
181        osg::MatrixTransform* arrow = new osg::MatrixTransform;
182        arrow->addChild(lineGeode);
183        arrow->addChild(geode);
184
185        // Rotate X-axis arrow appropriately.
186        osg::Quat rotation; rotation.makeRotate(osg::Vec3(1.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 1.0f));
187        arrow->setMatrix(osg::Matrix(rotation));
188
189        xform->addChild(arrow);
190    }
191
192    // Create an arrow in the Z axis.
193    {
194        osg::Group* arrow = new osg::Group;
195        arrow->addChild(lineGeode);
196        arrow->addChild(geode);
197
198        xform->addChild(arrow);
199    }
200
201    // Rotate the xform so that the geometry lies on the plane.
202    {
203        osg::Vec3 normal = _projector->getPlane().getNormal(); normal.normalize();
204        osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 1.0f, 0.0f), normal);
205        xform->setMatrix(osg::Matrix(rotation));
206    }
207
208    addChild(xform);
209}
Note: See TracBrowser for help on using the browser.