| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 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 | |
|---|
| 23 | using namespace osgManipulator; |
|---|
| 24 | |
|---|
| 25 | Translate1DDragger::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 | |
|---|
| 32 | Translate1DDragger::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 | |
|---|
| 39 | Translate1DDragger::~Translate1DDragger() |
|---|
| 40 | { |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | bool Translate1DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | if (_checkForNodeInNodePath) |
|---|
| 47 | { |
|---|
| 48 | if (!pointer.contains(this)) return false; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | switch (ea.getEventType()) |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | case (osgGA::GUIEventAdapter::PUSH): |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 71 | dispatch(*cmd); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | setMaterialColor(_pickColor,*this); |
|---|
| 75 | |
|---|
| 76 | aa.requestRedraw(); |
|---|
| 77 | } |
|---|
| 78 | return true; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | case (osgGA::GUIEventAdapter::DRAG): |
|---|
| 83 | { |
|---|
| 84 | osg::Vec3d projectedPoint; |
|---|
| 85 | if (_projector->project(pointer, projectedPoint)) |
|---|
| 86 | { |
|---|
| 87 | |
|---|
| 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 | |
|---|
| 95 | dispatch(*cmd); |
|---|
| 96 | |
|---|
| 97 | aa.requestRedraw(); |
|---|
| 98 | } |
|---|
| 99 | return true; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 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 | |
|---|
| 115 | dispatch(*cmd); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | setMaterialColor(_color,*this); |
|---|
| 119 | |
|---|
| 120 | aa.requestRedraw(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | return true; |
|---|
| 124 | } |
|---|
| 125 | default: |
|---|
| 126 | return false; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | void Translate1DDragger::setupDefaultGeometry() |
|---|
| 131 | { |
|---|
| 132 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 193 | addChild(lineGeode); |
|---|
| 194 | addChild(geode); |
|---|
| 195 | } |
|---|