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

Revision 13041, 11.2 kB (checked in by robert, 2 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/TabPlaneDragger>
16#include <osgManipulator/AntiSquish>
17
18#include <osg/ShapeDrawable>
19#include <osg/Geometry>
20#include <osg/LineWidth>
21#include <osg/Quat>
22#include <osg/PolygonMode>
23#include <osg/CullFace>
24#include <osg/AutoTransform>
25
26using namespace osgManipulator;
27
28namespace
29{
30
31osg::Node* createHandleNode(Scale2DDragger* cornerScaleDragger, float handleScaleFactor, bool twosided)
32{
33    osg::Vec3Array* vertices = new osg::Vec3Array(4);
34    (*vertices)[0] = osg::Vec3(cornerScaleDragger->getTopLeftHandlePosition()[0],0.0,cornerScaleDragger->getTopLeftHandlePosition()[1]) * handleScaleFactor;
35    (*vertices)[1] = osg::Vec3(cornerScaleDragger->getBottomLeftHandlePosition()[0],0.0,cornerScaleDragger->getBottomLeftHandlePosition()[1]) * handleScaleFactor;
36    (*vertices)[2] = osg::Vec3(cornerScaleDragger->getBottomRightHandlePosition()[0],0.0,cornerScaleDragger->getBottomRightHandlePosition()[1]) * handleScaleFactor;
37    (*vertices)[3] = osg::Vec3(cornerScaleDragger->getTopRightHandlePosition()[0],0.0,cornerScaleDragger->getTopRightHandlePosition()[1]) * handleScaleFactor;
38
39    osg::Geometry* geometry = new osg::Geometry();
40    geometry->setVertexArray(vertices);
41    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size()));
42
43    osg::Vec3Array* normals = new osg::Vec3Array;
44    normals->push_back(osg::Vec3(0.0,1.0,0.0));
45    geometry->setNormalArray(normals);
46    geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
47
48    osg::Geode* geode = new osg::Geode;
49    geode->setName("Dragger Handle");
50    geode->addDrawable(geometry);
51
52    if (!twosided)
53    {
54        osg::CullFace* cullface = new osg::CullFace;
55        cullface->setMode(osg::CullFace::FRONT);
56        geode->getOrCreateStateSet()->setAttribute(cullface, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
57        geode->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
58    }
59
60    geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
61
62    return geode;
63}
64
65osg::Node* createHandleScene(const osg::Vec3& pos, osg::Node* handleNode, float handleScaleFactor)
66{
67    osg::AutoTransform *at = new osg::AutoTransform;
68    at->setPosition(pos);
69    at->setPivotPoint(pos * handleScaleFactor);
70    at->setAutoScaleToScreen(true);
71    at->addChild(handleNode);
72
73    AntiSquish* as = new AntiSquish;
74    as->setPivot(pos);
75    as->addChild(at);
76
77    return as;
78}
79
80void createCornerScaleDraggerGeometry(Scale2DDragger* cornerScaleDragger, osg::Node* handleNode, float handleScaleFactor)
81{
82    // Create a top left box.
83    {
84        osg::Node* handleScene = createHandleScene(osg::Vec3(cornerScaleDragger->getTopLeftHandlePosition()[0],
85                                                             0.0,cornerScaleDragger->getTopLeftHandlePosition()[1]),
86                                                   handleNode, handleScaleFactor);
87        cornerScaleDragger->addChild(handleScene);
88        cornerScaleDragger->setTopLeftHandleNode(*handleScene);
89    }
90
91    // Create a bottom left box.
92    {
93        osg::Node* handleScene = createHandleScene(osg::Vec3(cornerScaleDragger->getBottomLeftHandlePosition()[0],
94                                                             0.0,cornerScaleDragger->getBottomLeftHandlePosition()[1]),
95                                                   handleNode, handleScaleFactor);
96        cornerScaleDragger->addChild(handleScene);
97        cornerScaleDragger->setBottomLeftHandleNode(*handleScene);
98    }
99
100    // Create a bottom right box.
101    {
102        osg::Node* handleScene = createHandleScene(osg::Vec3(cornerScaleDragger->getBottomRightHandlePosition()[0],
103                                                             0.0,cornerScaleDragger->getBottomRightHandlePosition()[1]),
104                                                   handleNode, handleScaleFactor);
105        cornerScaleDragger->addChild(handleScene);
106        cornerScaleDragger->setBottomRightHandleNode(*handleScene);
107    }
108
109    // Create a top right box.
110    {
111        osg::Node* handleScene = createHandleScene(osg::Vec3(cornerScaleDragger->getTopRightHandlePosition()[0],
112                                                             0.0,cornerScaleDragger->getTopRightHandlePosition()[1]),
113                                                    handleNode, handleScaleFactor);
114        cornerScaleDragger->addChild(handleScene);
115        cornerScaleDragger->setTopRightHandleNode(*handleScene);
116    }
117}
118
119void createEdgeScaleDraggerGeometry(Scale1DDragger* horzEdgeScaleDragger, Scale1DDragger* vertEdgeScaleDragger,
120                                    osg::Node* handleNode, float handleScaleFactor)
121{
122    // Create a left box.
123    {
124        osg::Node* handleScene = createHandleScene(osg::Vec3(horzEdgeScaleDragger->getLeftHandlePosition(),0.0,0.0),
125                                                   handleNode, handleScaleFactor);
126        horzEdgeScaleDragger->addChild(handleScene);
127        horzEdgeScaleDragger->setLeftHandleNode(*handleScene);
128    }
129
130    // Create a right box.
131    {
132        osg::Node* handleScene = createHandleScene(osg::Vec3(horzEdgeScaleDragger->getRightHandlePosition(),0.0,0.0),
133                                                   handleNode, handleScaleFactor);
134        horzEdgeScaleDragger->addChild(handleScene);
135        horzEdgeScaleDragger->setRightHandleNode(*handleScene);
136    }
137
138    // Create a top box.
139    {
140        osg::Node* handleScene = createHandleScene(osg::Vec3(vertEdgeScaleDragger->getLeftHandlePosition(),0.0,0.0),
141                                                   handleNode, handleScaleFactor);
142        vertEdgeScaleDragger->addChild(handleScene);
143        vertEdgeScaleDragger->setLeftHandleNode(*handleScene);
144    }
145
146    // Create a bottom box.
147    {
148        osg::Node* handleScene = createHandleScene(osg::Vec3(vertEdgeScaleDragger->getRightHandlePosition(),0.0,0.0),
149                                                   handleNode, handleScaleFactor);
150        vertEdgeScaleDragger->addChild(handleScene);
151        vertEdgeScaleDragger->setRightHandleNode(*handleScene);
152    }
153
154    osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f));
155    vertEdgeScaleDragger->setMatrix(osg::Matrix(rotation));
156}
157
158void createTranslateDraggerGeometry(Scale2DDragger* cornerScaleDragger, TranslatePlaneDragger* translateDragger)
159{
160    // Create a polygon.
161    {
162        osg::Geode* geode = new osg::Geode;
163        osg::Geometry* geometry = new osg::Geometry();
164
165        osg::Vec3Array* vertices = new osg::Vec3Array(4);
166        (*vertices)[0] = osg::Vec3(cornerScaleDragger->getTopLeftHandlePosition()[0],0.0,cornerScaleDragger->getTopLeftHandlePosition()[1]);
167        (*vertices)[1] = osg::Vec3(cornerScaleDragger->getBottomLeftHandlePosition()[0],0.0,cornerScaleDragger->getBottomLeftHandlePosition()[1]);
168        (*vertices)[2] = osg::Vec3(cornerScaleDragger->getBottomRightHandlePosition()[0],0.0,cornerScaleDragger->getBottomRightHandlePosition()[1]);
169        (*vertices)[3] = osg::Vec3(cornerScaleDragger->getTopRightHandlePosition()[0],0.0,cornerScaleDragger->getTopRightHandlePosition()[1]);
170
171        geometry->setVertexArray(vertices);
172        geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size()));
173
174        osg::Vec3Array* normals = new osg::Vec3Array;
175        normals->push_back(osg::Vec3(0.0,1.0,0.0));
176        geometry->setNormalArray(normals);
177        geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
178
179        geode->addDrawable(geometry);
180
181        osg::PolygonMode* polymode = new osg::PolygonMode;
182        polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
183        geode->getOrCreateStateSet()->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
184
185        geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
186
187        translateDragger->getTranslate2DDragger()->addChild(geode);
188    }
189
190}
191
192}
193
194TabPlaneDragger::TabPlaneDragger() : _handleScaleFactor(20.0)
195{
196    _cornerScaleDragger = new Scale2DDragger(Scale2DDragger::SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT);
197    addChild(_cornerScaleDragger.get());
198    addDragger(_cornerScaleDragger.get());
199
200    _horzEdgeScaleDragger = new Scale1DDragger(Scale1DDragger::SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT);
201    addChild(_horzEdgeScaleDragger.get());
202    addDragger(_horzEdgeScaleDragger.get());
203
204    _vertEdgeScaleDragger = new Scale1DDragger(Scale1DDragger::SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT);
205    addChild(_vertEdgeScaleDragger.get());
206    addDragger(_vertEdgeScaleDragger.get());
207
208    _translateDragger = new TranslatePlaneDragger();
209    _translateDragger->setColor(osg::Vec4(0.7f, 0.7f, 0.7f, 1.0f));
210    addChild(_translateDragger.get());
211    addDragger(_translateDragger.get());
212
213    setParentDragger(getParentDragger());
214}
215
216TabPlaneDragger::~TabPlaneDragger()
217{
218}
219
220bool TabPlaneDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
221{
222    if (ea.getButtonMask() & osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return false;
223
224    // Check if the dragger node is in the nodepath.
225    if (!pointer.contains(this)) return false;
226
227    // Since the translate plane and the handleNode lie on the same plane the hit could've been on either one. But we
228    // need to handle the scaling draggers before the translation. Check if the node path has the scaling nodes else
229    // check for the scaling nodes in next hit.
230    if (_cornerScaleDragger->handle(pointer, ea, aa))
231        return true;
232    if (_horzEdgeScaleDragger->handle(pointer, ea, aa))
233        return true;
234    if (_vertEdgeScaleDragger->handle(pointer, ea, aa))
235        return true;
236
237    PointerInfo nextPointer(pointer);
238    nextPointer.next();
239
240    while (!nextPointer.completed())
241    {
242        if (_cornerScaleDragger->handle(nextPointer, ea, aa))
243            return true;
244        if (_horzEdgeScaleDragger->handle(nextPointer, ea, aa))
245            return true;
246        if (_vertEdgeScaleDragger->handle(nextPointer, ea, aa))
247            return true;
248
249        nextPointer.next();
250    }
251
252    if (_translateDragger->handle(pointer, ea, aa))
253        return true;
254
255    return false;
256}
257
258void TabPlaneDragger::setupDefaultGeometry(bool twoSidedHandle)
259{
260    osg::ref_ptr<osg::Node> handleNode = createHandleNode(_cornerScaleDragger.get(), _handleScaleFactor, twoSidedHandle);
261
262    createCornerScaleDraggerGeometry(_cornerScaleDragger.get(), handleNode.get(), _handleScaleFactor);
263    createEdgeScaleDraggerGeometry(_horzEdgeScaleDragger.get(),_vertEdgeScaleDragger.get(),handleNode.get(),_handleScaleFactor);
264    createTranslateDraggerGeometry(_cornerScaleDragger.get(), _translateDragger.get());
265}
266
Note: See TracBrowser for help on using the browser.