| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgManipulator/Scale1DDragger> |
|---|
| 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 | namespace |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | double computeScale(const osg::Vec3d& startProjectedPoint, |
|---|
| 29 | const osg::Vec3d& projectedPoint, double scaleCenter) |
|---|
| 30 | { |
|---|
| 31 | double denom = startProjectedPoint[0] - scaleCenter; |
|---|
| 32 | double scale = denom ? (projectedPoint[0] - scaleCenter)/denom : 1.0; |
|---|
| 33 | return scale; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | Scale1DDragger::Scale1DDragger(ScaleMode scaleMode) : Dragger(), _minScale(0.001), _scaleMode(scaleMode) |
|---|
| 39 | { |
|---|
| 40 | _projector = new LineProjector(osg::Vec3d(-0.5,0.0,0.0),osg::Vec3d(0.5,0.0,0.0)); |
|---|
| 41 | setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 42 | setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | Scale1DDragger::~Scale1DDragger() |
|---|
| 46 | { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | bool Scale1DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | if (!pointer.contains(this)) return false; |
|---|
| 53 | |
|---|
| 54 | switch (ea.getEventType()) |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | case (osgGA::GUIEventAdapter::PUSH): |
|---|
| 58 | { |
|---|
| 59 | |
|---|
| 60 | osg::NodePath nodePathToRoot; |
|---|
| 61 | computeNodePathToRoot(*this,nodePathToRoot); |
|---|
| 62 | osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot); |
|---|
| 63 | _projector->setLocalToWorld(localToWorld); |
|---|
| 64 | |
|---|
| 65 | if (_projector->project(pointer, _startProjectedPoint)) |
|---|
| 66 | { |
|---|
| 67 | _scaleCenter = 0.0; |
|---|
| 68 | if (_scaleMode == SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT) |
|---|
| 69 | { |
|---|
| 70 | if ( pointer.contains(_leftHandleNode.get()) ) |
|---|
| 71 | _scaleCenter = _projector->getLineEnd()[0]; |
|---|
| 72 | else if ( pointer.contains( _rightHandleNode.get()) ) |
|---|
| 73 | _scaleCenter = _projector->getLineStart()[0]; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | osg::ref_ptr<Scale1DCommand> cmd = new Scale1DCommand(); |
|---|
| 78 | cmd->setStage(MotionCommand::START); |
|---|
| 79 | cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal()); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | dispatch(*cmd); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | setMaterialColor(_pickColor,*this); |
|---|
| 86 | |
|---|
| 87 | aa.requestRedraw(); |
|---|
| 88 | } |
|---|
| 89 | return true; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | case (osgGA::GUIEventAdapter::DRAG): |
|---|
| 94 | { |
|---|
| 95 | osg::Vec3d projectedPoint; |
|---|
| 96 | if (_projector->project(pointer, projectedPoint)) |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | osg::ref_ptr<Scale1DCommand> cmd = new Scale1DCommand(); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | double scale = computeScale(_startProjectedPoint,projectedPoint,_scaleCenter); |
|---|
| 103 | if (scale < getMinScale()) scale = getMinScale(); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | double referencePoint = _startProjectedPoint[0]; |
|---|
| 107 | if (fabs(_projector->getLineStart()[0] - referencePoint) < |
|---|
| 108 | fabs(_projector->getLineEnd()[0] - referencePoint)) |
|---|
| 109 | referencePoint = _projector->getLineStart()[0]; |
|---|
| 110 | else |
|---|
| 111 | referencePoint = _projector->getLineEnd()[0]; |
|---|
| 112 | |
|---|
| 113 | cmd->setStage(MotionCommand::MOVE); |
|---|
| 114 | cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal()); |
|---|
| 115 | cmd->setScale(scale); |
|---|
| 116 | cmd->setScaleCenter(_scaleCenter); |
|---|
| 117 | cmd->setReferencePoint(referencePoint); |
|---|
| 118 | cmd->setMinScale(getMinScale()); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | dispatch(*cmd); |
|---|
| 122 | |
|---|
| 123 | aa.requestRedraw(); |
|---|
| 124 | } |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | case (osgGA::GUIEventAdapter::RELEASE): |
|---|
| 130 | { |
|---|
| 131 | osg::ref_ptr<Scale1DCommand> cmd = new Scale1DCommand(); |
|---|
| 132 | |
|---|
| 133 | cmd->setStage(MotionCommand::FINISH); |
|---|
| 134 | cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal()); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | dispatch(*cmd); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | setMaterialColor(_color,*this); |
|---|
| 141 | |
|---|
| 142 | aa.requestRedraw(); |
|---|
| 143 | |
|---|
| 144 | return true; |
|---|
| 145 | } |
|---|
| 146 | default: |
|---|
| 147 | return false; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void Scale1DDragger::setupDefaultGeometry() |
|---|
| 152 | { |
|---|
| 153 | |
|---|
| 154 | osg::Vec3 lineDir = _projector->getLineEnd()-_projector->getLineStart(); |
|---|
| 155 | float lineLength = lineDir.length(); |
|---|
| 156 | lineDir.normalize(); |
|---|
| 157 | |
|---|
| 158 | osg::Geode* lineGeode = new osg::Geode; |
|---|
| 159 | |
|---|
| 160 | { |
|---|
| 161 | osg::Geometry* geometry = new osg::Geometry(); |
|---|
| 162 | |
|---|
| 163 | osg::Vec3Array* vertices = new osg::Vec3Array(2); |
|---|
| 164 | (*vertices)[0] = _projector->getLineStart(); |
|---|
| 165 | (*vertices)[1] = _projector->getLineEnd(); |
|---|
| 166 | |
|---|
| 167 | geometry->setVertexArray(vertices); |
|---|
| 168 | geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2)); |
|---|
| 169 | |
|---|
| 170 | lineGeode->addDrawable(geometry); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 175 | osg::LineWidth* linewidth = new osg::LineWidth(); |
|---|
| 176 | linewidth->setWidth(2.0f); |
|---|
| 177 | lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON); |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | addChild(lineGeode); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | { |
|---|
| 184 | osg::Geode* geode = new osg::Geode; |
|---|
| 185 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(_projector->getLineStart(), 0.05 * lineLength))); |
|---|
| 186 | addChild(geode); |
|---|
| 187 | setLeftHandleNode(*geode); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | { |
|---|
| 192 | osg::Geode* geode = new osg::Geode; |
|---|
| 193 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(_projector->getLineEnd(), 0.05 * lineLength))); |
|---|
| 194 | addChild(geode); |
|---|
| 195 | setRightHandleNode(*geode); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|