| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgManipulator/Scale2DDragger> |
|---|
| 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 | osg::Vec2d computeScale(const osg::Vec3d& startProjectedPoint, |
|---|
| 29 | const osg::Vec3d& projectedPoint, |
|---|
| 30 | const osg::Vec2d& scaleCenter) |
|---|
| 31 | { |
|---|
| 32 | osg::Vec2d scale(1.0,1.0); |
|---|
| 33 | if ((startProjectedPoint[0] - scaleCenter[0]) != 0.0) |
|---|
| 34 | scale[0] = (projectedPoint[0] - scaleCenter[0])/(startProjectedPoint[0] - scaleCenter[0]); |
|---|
| 35 | if ((startProjectedPoint[2] - scaleCenter[1]) != 0.0) |
|---|
| 36 | scale[1] = (projectedPoint[2] - scaleCenter[1])/(startProjectedPoint[2] - scaleCenter[1]); |
|---|
| 37 | return scale; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | Scale2DDragger::Scale2DDragger(ScaleMode scaleMode) : Dragger(), _minScale(0.001,0.001), _scaleMode(scaleMode) |
|---|
| 43 | { |
|---|
| 44 | _projector = new PlaneProjector(osg::Plane(0.0,1.0,0.0,0.0)); |
|---|
| 45 | setColor(osg::Vec4(0.0, 1.0, 0.0, 1.0)); |
|---|
| 46 | setPickColor(osg::Vec4(1.0, 1.0, 0.0, 1.0)); |
|---|
| 47 | |
|---|
| 48 | _topLeftHandlePosition.set (-0.5,0.5); |
|---|
| 49 | _bottomLeftHandlePosition.set (-0.5,-0.5); |
|---|
| 50 | _bottomRightHandlePosition.set(0.5,-0.5); |
|---|
| 51 | _topRightHandlePosition.set (0.5,0.5); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | Scale2DDragger::~Scale2DDragger() |
|---|
| 55 | { |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | bool Scale2DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 59 | { |
|---|
| 60 | |
|---|
| 61 | if (!pointer.contains(this)) return false; |
|---|
| 62 | |
|---|
| 63 | switch (ea.getEventType()) |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | case (osgGA::GUIEventAdapter::PUSH): |
|---|
| 67 | { |
|---|
| 68 | |
|---|
| 69 | osg::NodePath nodePathToRoot; |
|---|
| 70 | computeNodePathToRoot(*this,nodePathToRoot); |
|---|
| 71 | osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot); |
|---|
| 72 | _projector->setLocalToWorld(localToWorld); |
|---|
| 73 | |
|---|
| 74 | if (_projector->project(pointer, _startProjectedPoint)) |
|---|
| 75 | { |
|---|
| 76 | _scaleCenter.set(0.0,0.0); |
|---|
| 77 | |
|---|
| 78 | if (pointer.contains(_topLeftHandleNode.get())) |
|---|
| 79 | { |
|---|
| 80 | _referencePoint = _topLeftHandlePosition; |
|---|
| 81 | if (_scaleMode == SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT) |
|---|
| 82 | _scaleCenter = _bottomRightHandlePosition; |
|---|
| 83 | } |
|---|
| 84 | else if (pointer.contains(_bottomLeftHandleNode.get())) |
|---|
| 85 | { |
|---|
| 86 | _referencePoint = _bottomLeftHandlePosition; |
|---|
| 87 | if (_scaleMode == SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT) |
|---|
| 88 | _scaleCenter = _topRightHandlePosition; |
|---|
| 89 | } |
|---|
| 90 | else if (pointer.contains(_bottomRightHandleNode.get())) |
|---|
| 91 | { |
|---|
| 92 | _referencePoint = _bottomRightHandlePosition; |
|---|
| 93 | if (_scaleMode == SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT) |
|---|
| 94 | _scaleCenter = _topLeftHandlePosition; |
|---|
| 95 | } |
|---|
| 96 | else if (pointer.contains(_topRightHandleNode.get())) |
|---|
| 97 | { |
|---|
| 98 | _referencePoint = _topRightHandlePosition; |
|---|
| 99 | if (_scaleMode == SCALE_WITH_OPPOSITE_HANDLE_AS_PIVOT) |
|---|
| 100 | _scaleCenter = _bottomLeftHandlePosition; |
|---|
| 101 | |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | osg::ref_ptr<Scale2DCommand> cmd = new Scale2DCommand(); |
|---|
| 106 | cmd->setStage(MotionCommand::START); |
|---|
| 107 | cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal()); |
|---|
| 108 | cmd->setReferencePoint(_referencePoint); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | dispatch(*cmd); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | setMaterialColor(_pickColor,*this); |
|---|
| 115 | |
|---|
| 116 | aa.requestRedraw(); |
|---|
| 117 | } |
|---|
| 118 | return true; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | case (osgGA::GUIEventAdapter::DRAG): |
|---|
| 123 | { |
|---|
| 124 | osg::Vec3d projectedPoint; |
|---|
| 125 | if (_projector->project(pointer, projectedPoint)) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | osg::Vec2d scale = computeScale(_startProjectedPoint,projectedPoint,_scaleCenter); |
|---|
| 129 | |
|---|
| 130 | if (scale[0] < getMinScale()[0]) scale[0] = getMinScale()[0]; |
|---|
| 131 | if (scale[1] < getMinScale()[1]) scale[1] = getMinScale()[1]; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | osg::ref_ptr<Scale2DCommand> cmd = new Scale2DCommand(); |
|---|
| 135 | cmd->setStage(MotionCommand::MOVE); |
|---|
| 136 | cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal()); |
|---|
| 137 | cmd->setScale(scale); |
|---|
| 138 | cmd->setScaleCenter(_scaleCenter); |
|---|
| 139 | cmd->setReferencePoint(_referencePoint); |
|---|
| 140 | cmd->setMinScale(getMinScale()); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | dispatch(*cmd); |
|---|
| 144 | |
|---|
| 145 | aa.requestRedraw(); |
|---|
| 146 | } |
|---|
| 147 | return true; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | case (osgGA::GUIEventAdapter::RELEASE): |
|---|
| 152 | { |
|---|
| 153 | osg::ref_ptr<Scale2DCommand> cmd = new Scale2DCommand(); |
|---|
| 154 | |
|---|
| 155 | cmd->setStage(MotionCommand::FINISH); |
|---|
| 156 | cmd->setReferencePoint(_referencePoint); |
|---|
| 157 | cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal()); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | dispatch(*cmd); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | setMaterialColor(_color,*this); |
|---|
| 164 | |
|---|
| 165 | aa.requestRedraw(); |
|---|
| 166 | |
|---|
| 167 | return true; |
|---|
| 168 | } |
|---|
| 169 | default: |
|---|
| 170 | return false; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void Scale2DDragger::setupDefaultGeometry() |
|---|
| 175 | { |
|---|
| 176 | osg::Geode* lineGeode = new osg::Geode; |
|---|
| 177 | |
|---|
| 178 | { |
|---|
| 179 | osg::Geometry* geometry = new osg::Geometry(); |
|---|
| 180 | |
|---|
| 181 | osg::Vec3Array* vertices = new osg::Vec3Array(4); |
|---|
| 182 | (*vertices)[0].set(_topLeftHandlePosition[0],0.0,_topLeftHandlePosition[1]); |
|---|
| 183 | (*vertices)[1].set(_bottomLeftHandlePosition[0],0.0,_bottomLeftHandlePosition[1]); |
|---|
| 184 | (*vertices)[2].set(_bottomRightHandlePosition[0],0.0,_bottomRightHandlePosition[1]); |
|---|
| 185 | (*vertices)[3].set(_topRightHandlePosition[0],0.0,_topRightHandlePosition[1]); |
|---|
| 186 | |
|---|
| 187 | geometry->setVertexArray(vertices); |
|---|
| 188 | geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,vertices->size())); |
|---|
| 189 | |
|---|
| 190 | lineGeode->addDrawable(geometry); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 195 | osg::LineWidth* linewidth = new osg::LineWidth(); |
|---|
| 196 | linewidth->setWidth(2.0); |
|---|
| 197 | lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | addChild(lineGeode); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | { |
|---|
| 204 | osg::Geode* geode = new osg::Geode; |
|---|
| 205 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(_topLeftHandlePosition[0], |
|---|
| 206 | 0.0,_topLeftHandlePosition[1]), 0.05))); |
|---|
| 207 | addChild(geode); |
|---|
| 208 | setTopLeftHandleNode(*geode); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | { |
|---|
| 213 | osg::Geode* geode = new osg::Geode; |
|---|
| 214 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(_bottomLeftHandlePosition[0], |
|---|
| 215 | 0.0,_bottomLeftHandlePosition[1]), 0.05))); |
|---|
| 216 | addChild(geode); |
|---|
| 217 | setBottomLeftHandleNode(*geode); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | { |
|---|
| 222 | osg::Geode* geode = new osg::Geode; |
|---|
| 223 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(_bottomRightHandlePosition[0], |
|---|
| 224 | 0.0,_bottomRightHandlePosition[1]), 0.05))); |
|---|
| 225 | addChild(geode); |
|---|
| 226 | setBottomRightHandleNode(*geode); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | { |
|---|
| 231 | osg::Geode* geode = new osg::Geode; |
|---|
| 232 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(_topRightHandlePosition[0], |
|---|
| 233 | 0.0,_topRightHandlePosition[1]), 0.05))); |
|---|
| 234 | addChild(geode); |
|---|
| 235 | setTopRightHandleNode(*geode); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|