| 1 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 2 | #include <osg/Notify> |
|---|
| 3 | |
|---|
| 4 | using namespace osgGA; |
|---|
| 5 | |
|---|
| 6 | void KeySwitchMatrixManipulator::addMatrixManipulator(int key, std::string name, MatrixManipulator *cm) |
|---|
| 7 | { |
|---|
| 8 | if(!cm) return; |
|---|
| 9 | |
|---|
| 10 | _manips[key]=std::make_pair(name,osg::ref_ptr<MatrixManipulator>(cm)); |
|---|
| 11 | |
|---|
| 12 | if(!_current) |
|---|
| 13 | { |
|---|
| 14 | _current=cm; |
|---|
| 15 | _current->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition); |
|---|
| 16 | _current->setNode(0); |
|---|
| 17 | _current->setCoordinateFrameCallback(getCoordinateFrameCallback()); |
|---|
| 18 | _current->setByMatrix(getMatrix()); |
|---|
| 19 | } |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | void KeySwitchMatrixManipulator::addNumberedMatrixManipulator(MatrixManipulator *cm) |
|---|
| 23 | { |
|---|
| 24 | if(!cm) return; |
|---|
| 25 | addMatrixManipulator('1'+_manips.size(),cm->className(),cm); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | void KeySwitchMatrixManipulator::selectMatrixManipulator(unsigned int num) |
|---|
| 29 | { |
|---|
| 30 | unsigned int manipNo = 0; |
|---|
| 31 | KeyManipMap::iterator itr; |
|---|
| 32 | for(itr=_manips.begin(); |
|---|
| 33 | manipNo!=num && itr!=_manips.end(); |
|---|
| 34 | ++itr,++manipNo) |
|---|
| 35 | { |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | if (itr!=_manips.end()) |
|---|
| 39 | { |
|---|
| 40 | itr->second.second->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition); |
|---|
| 41 | |
|---|
| 42 | if (_current.valid()) |
|---|
| 43 | { |
|---|
| 44 | if ( !itr->second.second->getCoordinateFrameCallback() ) |
|---|
| 45 | { |
|---|
| 46 | itr->second.second->setCoordinateFrameCallback(_current->getCoordinateFrameCallback()); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | if ( !itr->second.second->getNode() ) |
|---|
| 50 | { |
|---|
| 51 | itr->second.second->setNode(_current->getNode()); |
|---|
| 52 | } |
|---|
| 53 | itr->second.second->setByMatrix(_current->getMatrix()); |
|---|
| 54 | } |
|---|
| 55 | _current = itr->second.second; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | void KeySwitchMatrixManipulator::setDistance(double distance) |
|---|
| 61 | { |
|---|
| 62 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 63 | itr!=_manips.end(); |
|---|
| 64 | ++itr) |
|---|
| 65 | { |
|---|
| 66 | itr->second.second->setDistance(distance); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | double KeySwitchMatrixManipulator::getDistance() const |
|---|
| 71 | { |
|---|
| 72 | if(!_current) |
|---|
| 73 | { |
|---|
| 74 | return _current->getDistance(); |
|---|
| 75 | } |
|---|
| 76 | else return 1.0; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void KeySwitchMatrixManipulator::setNode(osg::Node* node) |
|---|
| 80 | { |
|---|
| 81 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 82 | itr!=_manips.end(); |
|---|
| 83 | ++itr) |
|---|
| 84 | { |
|---|
| 85 | itr->second.second->setNode(node); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | void KeySwitchMatrixManipulator::setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition) |
|---|
| 90 | { |
|---|
| 91 | MatrixManipulator::setHomePosition(eye, center, up, autoComputeHomePosition); |
|---|
| 92 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 93 | itr!=_manips.end(); |
|---|
| 94 | ++itr) |
|---|
| 95 | { |
|---|
| 96 | itr->second.second->setHomePosition(eye, center, up, autoComputeHomePosition); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | void KeySwitchMatrixManipulator::setAutoComputeHomePosition(bool flag) |
|---|
| 101 | { |
|---|
| 102 | _autoComputeHomePosition = flag; |
|---|
| 103 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 104 | itr!=_manips.end(); |
|---|
| 105 | ++itr) |
|---|
| 106 | { |
|---|
| 107 | itr->second.second->setAutoComputeHomePosition(flag); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void KeySwitchMatrixManipulator::setMinimumDistance(float minimumDistance) |
|---|
| 112 | { |
|---|
| 113 | _minimumDistance = minimumDistance; |
|---|
| 114 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 115 | itr!=_manips.end(); |
|---|
| 116 | ++itr) |
|---|
| 117 | { |
|---|
| 118 | itr->second.second->setMinimumDistance(minimumDistance); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void KeySwitchMatrixManipulator::computeHomePosition() |
|---|
| 123 | { |
|---|
| 124 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 125 | itr!=_manips.end(); |
|---|
| 126 | ++itr) |
|---|
| 127 | { |
|---|
| 128 | itr->second.second->computeHomePosition(); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void KeySwitchMatrixManipulator::setCoordinateFrameCallback(CoordinateFrameCallback* cb) |
|---|
| 133 | { |
|---|
| 134 | _coordinateFrameCallback = cb; |
|---|
| 135 | for(KeyManipMap::iterator itr=_manips.begin(); |
|---|
| 136 | itr!=_manips.end(); |
|---|
| 137 | ++itr) |
|---|
| 138 | { |
|---|
| 139 | itr->second.second->setCoordinateFrameCallback(cb); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index) |
|---|
| 144 | { |
|---|
| 145 | unsigned i=0; |
|---|
| 146 | for(KeyManipMap::iterator itr = _manips.begin(); |
|---|
| 147 | itr != _manips.end(); |
|---|
| 148 | ++itr, ++i) |
|---|
| 149 | { |
|---|
| 150 | if (i==index) return itr->second.second.get(); |
|---|
| 151 | } |
|---|
| 152 | return 0; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | const MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index) const |
|---|
| 156 | { |
|---|
| 157 | unsigned i=0; |
|---|
| 158 | for(KeyManipMap::const_iterator itr = _manips.begin(); |
|---|
| 159 | itr != _manips.end(); |
|---|
| 160 | ++itr, ++i) |
|---|
| 161 | { |
|---|
| 162 | if (i==index) return itr->second.second.get(); |
|---|
| 163 | } |
|---|
| 164 | return 0; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key) |
|---|
| 168 | { |
|---|
| 169 | KeyManipMap::iterator itr = _manips.find(key); |
|---|
| 170 | if (itr!=_manips.end()) return itr->second.second.get(); |
|---|
| 171 | else return 0; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | const MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key) const |
|---|
| 175 | { |
|---|
| 176 | KeyManipMap::const_iterator itr = _manips.find(key); |
|---|
| 177 | if (itr!=_manips.end()) return itr->second.second.get(); |
|---|
| 178 | else return 0; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | bool KeySwitchMatrixManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa) |
|---|
| 182 | { |
|---|
| 183 | if (!_current) return false; |
|---|
| 184 | |
|---|
| 185 | bool handled = false; |
|---|
| 186 | |
|---|
| 187 | if (!ea.getHandled() && ea.getEventType()==GUIEventAdapter::KEYDOWN) |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | KeyManipMap::iterator it=_manips.find(ea.getKey()); |
|---|
| 191 | if(it != _manips.end()) |
|---|
| 192 | { |
|---|
| 193 | osg::notify(osg::INFO)<<"Switching to manipulator: "<<(*it).second.first<<std::endl; |
|---|
| 194 | if ( !it->second.second->getNode() ) |
|---|
| 195 | { |
|---|
| 196 | it->second.second->setNode(_current->getNode()); |
|---|
| 197 | } |
|---|
| 198 | it->second.second->setByMatrix(_current->getMatrix()); |
|---|
| 199 | it->second.second->init(ea,aa); |
|---|
| 200 | _current = it->second.second; |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | handled = true; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | return _current->handle(ea,aa) || handled; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | void KeySwitchMatrixManipulator::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 212 | { |
|---|
| 213 | for(KeyManipMap::const_iterator itr=_manips.begin(); |
|---|
| 214 | itr!=_manips.end(); |
|---|
| 215 | ++itr) |
|---|
| 216 | { |
|---|
| 217 | std::string key; key += (char)(itr->first); |
|---|
| 218 | std::string explanation(std::string("Select '")+itr->second.first+std::string("' camera manipulator")); |
|---|
| 219 | if (_current==itr->second.second) explanation += " (default)"; |
|---|
| 220 | |
|---|
| 221 | usage.addKeyboardMouseBinding(key,explanation); |
|---|
| 222 | itr->second.second->getUsage(usage); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|