| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 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 | |
|---|
| 23 | using namespace osgManipulator; |
|---|
| 24 | |
|---|
| 25 | Translate2DDragger::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 | |
|---|
| 33 | Translate2DDragger::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 | |
|---|
| 41 | Translate2DDragger::~Translate2DDragger() |
|---|
| 42 | { |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | bool Translate2DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 46 | { |
|---|
| 47 | |
|---|
| 48 | if (!pointer.contains(this)) return false; |
|---|
| 49 | |
|---|
| 50 | switch (ea.getEventType()) |
|---|
| 51 | { |
|---|
| 52 | |
|---|
| 53 | case (osgGA::GUIEventAdapter::PUSH): |
|---|
| 54 | { |
|---|
| 55 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 71 | dispatch(*cmd); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | setMaterialColor(_pickColor,*this); |
|---|
| 75 | getOrCreateStateSet()->setAttributeAndModes(_polygonOffset.get(), osg::StateAttribute::ON); |
|---|
| 76 | |
|---|
| 77 | aa.requestRedraw(); |
|---|
| 78 | } |
|---|
| 79 | return true; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | case (osgGA::GUIEventAdapter::DRAG): |
|---|
| 84 | { |
|---|
| 85 | osg::Vec3d projectedPoint; |
|---|
| 86 | if (_projector->project(pointer, projectedPoint)) |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 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 | |
|---|
| 97 | dispatch(*cmd); |
|---|
| 98 | |
|---|
| 99 | aa.requestRedraw(); |
|---|
| 100 | } |
|---|
| 101 | return true; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 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 | |
|---|
| 114 | dispatch(*cmd); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 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 | |
|---|
| 129 | void Translate2DDragger::setupDefaultGeometry() |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 177 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | { |
|---|
| 181 | osg::MatrixTransform* arrow = new osg::MatrixTransform; |
|---|
| 182 | arrow->addChild(lineGeode); |
|---|
| 183 | arrow->addChild(geode); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 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 | |
|---|
| 193 | { |
|---|
| 194 | osg::Group* arrow = new osg::Group; |
|---|
| 195 | arrow->addChild(lineGeode); |
|---|
| 196 | arrow->addChild(geode); |
|---|
| 197 | |
|---|
| 198 | xform->addChild(arrow); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 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 | } |
|---|