| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <osgWidget/ViewerEventHandlers> |
|---|
| 4 | |
|---|
| 5 | namespace osgWidget { |
|---|
| 6 | |
|---|
| 7 | MouseHandler::MouseHandler(WindowManager* wm): |
|---|
| 8 | _wm(wm) { |
|---|
| 9 | } |
|---|
| 10 | |
|---|
| 11 | bool MouseHandler::handle( |
|---|
| 12 | const osgGA::GUIEventAdapter& gea, |
|---|
| 13 | osgGA::GUIActionAdapter& gaa, |
|---|
| 14 | osg::Object* obj, |
|---|
| 15 | osg::NodeVisitor* nv |
|---|
| 16 | ) { |
|---|
| 17 | osgGA::GUIEventAdapter::EventType ev = gea.getEventType(); |
|---|
| 18 | MouseAction ma = _isMouseEvent(ev); |
|---|
| 19 | |
|---|
| 20 | if(ma) { |
|---|
| 21 | |
|---|
| 22 | _wm->setScrollingMotion(gea.getScrollingMotion()); |
|---|
| 23 | |
|---|
| 24 | return (this->*ma)(gea.getX(), gea.getY(), gea.getButton()); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | return false; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | bool MouseHandler::_handleMousePush(float x, float y, int button) { |
|---|
| 31 | if(button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) return _doMouseEvent( |
|---|
| 32 | x, |
|---|
| 33 | y, |
|---|
| 34 | &WindowManager::mousePushedLeft |
|---|
| 35 | ); |
|---|
| 36 | |
|---|
| 37 | else if(button == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return _doMouseEvent( |
|---|
| 38 | x, |
|---|
| 39 | y, |
|---|
| 40 | &WindowManager::mousePushedRight |
|---|
| 41 | ); |
|---|
| 42 | |
|---|
| 43 | else if(button == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) return _doMouseEvent( |
|---|
| 44 | x, |
|---|
| 45 | y, |
|---|
| 46 | &WindowManager::mousePushedMiddle |
|---|
| 47 | ); |
|---|
| 48 | |
|---|
| 49 | else return false; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | bool MouseHandler::_handleMouseRelease(float x, float y, int button) { |
|---|
| 53 | if(button == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) return _doMouseEvent( |
|---|
| 54 | x, |
|---|
| 55 | y, |
|---|
| 56 | &WindowManager::mouseReleasedLeft |
|---|
| 57 | ); |
|---|
| 58 | |
|---|
| 59 | else if(button == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) return _doMouseEvent( |
|---|
| 60 | x, |
|---|
| 61 | y, |
|---|
| 62 | &WindowManager::mouseReleasedRight |
|---|
| 63 | ); |
|---|
| 64 | |
|---|
| 65 | else if(button == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) return _doMouseEvent( |
|---|
| 66 | x, |
|---|
| 67 | y, |
|---|
| 68 | &WindowManager::mouseReleasedMiddle |
|---|
| 69 | ); |
|---|
| 70 | |
|---|
| 71 | else return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | bool MouseHandler::_handleMouseDoubleClick(float x, float y, int button) { |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | bool MouseHandler::_handleMouseDrag(float x, float y, int button) { |
|---|
| 79 | return _doMouseEvent(x, y, &WindowManager::pointerDrag); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | bool MouseHandler::_handleMouseMove(float x, float y, int button) { |
|---|
| 83 | return _doMouseEvent(x, y, &WindowManager::pointerMove); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | bool MouseHandler::_handleMouseScroll(float x, float y, int) { |
|---|
| 87 | return _doMouseEvent(x, y, &WindowManager::mouseScroll); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | MouseHandler::MouseAction MouseHandler::_isMouseEvent( |
|---|
| 91 | osgGA::GUIEventAdapter::EventType ev |
|---|
| 92 | ) const { |
|---|
| 93 | if(ev == osgGA::GUIEventAdapter::PUSH) return |
|---|
| 94 | &MouseHandler::_handleMousePush |
|---|
| 95 | ; |
|---|
| 96 | |
|---|
| 97 | else if(ev == osgGA::GUIEventAdapter::RELEASE) return |
|---|
| 98 | &MouseHandler::_handleMouseRelease |
|---|
| 99 | ; |
|---|
| 100 | |
|---|
| 101 | else if(ev == osgGA::GUIEventAdapter::DOUBLECLICK) return |
|---|
| 102 | &MouseHandler::_handleMouseDoubleClick |
|---|
| 103 | ; |
|---|
| 104 | |
|---|
| 105 | else if(ev == osgGA::GUIEventAdapter::DRAG) return |
|---|
| 106 | &MouseHandler::_handleMouseDrag |
|---|
| 107 | ; |
|---|
| 108 | |
|---|
| 109 | else if(ev == osgGA::GUIEventAdapter::MOVE) return |
|---|
| 110 | &MouseHandler::_handleMouseMove |
|---|
| 111 | ; |
|---|
| 112 | |
|---|
| 113 | else if(ev == osgGA::GUIEventAdapter::SCROLL) return |
|---|
| 114 | &MouseHandler::_handleMouseScroll |
|---|
| 115 | ; |
|---|
| 116 | |
|---|
| 117 | else return 0; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | bool MouseHandler::_doMouseEvent(float x, float y, MouseEvent me) { |
|---|
| 121 | bool handled = (_wm.get()->*me)(x, y); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | _wm->setPointerXY(x, y); |
|---|
| 125 | |
|---|
| 126 | return handled; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | KeyboardHandler::KeyboardHandler(WindowManager* wm): |
|---|
| 130 | _wm(wm) { |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | bool KeyboardHandler::handle( |
|---|
| 134 | const osgGA::GUIEventAdapter& gea, |
|---|
| 135 | osgGA::GUIActionAdapter& gaa, |
|---|
| 136 | osg::Object* obj, |
|---|
| 137 | osg::NodeVisitor* nv |
|---|
| 138 | ) { |
|---|
| 139 | osgGA::GUIEventAdapter::EventType ev = gea.getEventType(); |
|---|
| 140 | |
|---|
| 141 | if( |
|---|
| 142 | ev != osgGA::GUIEventAdapter::KEYDOWN && |
|---|
| 143 | ev != osgGA::GUIEventAdapter::KEYUP |
|---|
| 144 | ) return false; |
|---|
| 145 | |
|---|
| 146 | int key = gea.getKey(); |
|---|
| 147 | int keyMask = gea.getModKeyMask(); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | if(key == -1) return false; |
|---|
| 151 | |
|---|
| 152 | if(ev == osgGA::GUIEventAdapter::KEYDOWN) return _wm->keyDown(key, keyMask); |
|---|
| 153 | |
|---|
| 154 | else if(ev == osgGA::GUIEventAdapter::KEYUP) return _wm->keyUp(key, keyMask); |
|---|
| 155 | |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | ResizeHandler::ResizeHandler(WindowManager* wm, osg::Camera* camera): |
|---|
| 160 | _wm (wm), |
|---|
| 161 | _camera (camera) { |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | bool ResizeHandler::handle( |
|---|
| 165 | const osgGA::GUIEventAdapter& gea, |
|---|
| 166 | osgGA::GUIActionAdapter& gaa, |
|---|
| 167 | osg::Object* obj, |
|---|
| 168 | osg::NodeVisitor* nv |
|---|
| 169 | ) { |
|---|
| 170 | osgGA::GUIEventAdapter::EventType ev = gea.getEventType(); |
|---|
| 171 | |
|---|
| 172 | if(ev != osgGA::GUIEventAdapter::RESIZE) return false; |
|---|
| 173 | |
|---|
| 174 | osg::Matrix::value_type w = gea.getWindowWidth(); |
|---|
| 175 | osg::Matrix::value_type h = gea.getWindowHeight(); |
|---|
| 176 | |
|---|
| 177 | if(_camera.valid()) { |
|---|
| 178 | _camera->setProjectionMatrix(osg::Matrix::ortho2D(0.0f, w, 0.0f, h)); |
|---|
| 179 | |
|---|
| 180 | _wm->setSize(w, h); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | _wm->setWindowSize(w, h); |
|---|
| 184 | _wm->resizeAllWindows(); |
|---|
| 185 | |
|---|
| 186 | return true; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | CameraSwitchHandler::CameraSwitchHandler(WindowManager* wm, osg::Camera* camera): |
|---|
| 190 | _wm (wm), |
|---|
| 191 | _camera (camera) { |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | bool CameraSwitchHandler::handle( |
|---|
| 195 | const osgGA::GUIEventAdapter& gea, |
|---|
| 196 | osgGA::GUIActionAdapter& gaa, |
|---|
| 197 | osg::Object* obj, |
|---|
| 198 | osg::NodeVisitor* nv |
|---|
| 199 | ) { |
|---|
| 200 | if( |
|---|
| 201 | gea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN || |
|---|
| 202 | gea.getKey() != osgGA::GUIEventAdapter::KEY_F12 |
|---|
| 203 | ) return false; |
|---|
| 204 | |
|---|
| 205 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&gaa); |
|---|
| 206 | |
|---|
| 207 | if(!view) return false; |
|---|
| 208 | |
|---|
| 209 | osg::Node* oldNode = view->getSceneData(); |
|---|
| 210 | |
|---|
| 211 | osg::MatrixTransform* oldTrans = dynamic_cast<osg::MatrixTransform*>(oldNode); |
|---|
| 212 | |
|---|
| 213 | if(!oldTrans) { |
|---|
| 214 | |
|---|
| 215 | double scale = 2000.0f; |
|---|
| 216 | double width = _wm->getWidth(); |
|---|
| 217 | double height = _wm->getHeight(); |
|---|
| 218 | |
|---|
| 219 | _oldNode = oldNode; |
|---|
| 220 | |
|---|
| 221 | osg::MatrixTransform* mt = new osg::MatrixTransform(); |
|---|
| 222 | |
|---|
| 223 | mt->setMatrix( |
|---|
| 224 | osg::Matrix::translate(width / 2.0f, 0.0f, 0.0f) * |
|---|
| 225 | osg::Matrix::scale(1.0f, 1.0f, scale) * |
|---|
| 226 | osg::Matrix::rotate(osg::DegreesToRadians(45.0f), 0.0f, 1.0f, 0.0f) |
|---|
| 227 | ); |
|---|
| 228 | |
|---|
| 229 | mt->addChild(_wm.get()); |
|---|
| 230 | mt->getOrCreateStateSet()->setMode( |
|---|
| 231 | GL_LIGHTING, |
|---|
| 232 | osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF |
|---|
| 233 | ); |
|---|
| 234 | mt->getOrCreateStateSet()->setMode( |
|---|
| 235 | GL_SCISSOR_TEST, |
|---|
| 236 | osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF |
|---|
| 237 | ); |
|---|
| 238 | |
|---|
| 239 | osgGA::CameraManipulator* mm = view->getCameraManipulator(); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | mm->setHomePosition( |
|---|
| 244 | |
|---|
| 245 | osg::Vec3(width / 2.0f, height, 100.0f), |
|---|
| 246 | |
|---|
| 247 | osg::Vec3(0.0f, 0.0f, -(scale / 2.0f)), |
|---|
| 248 | |
|---|
| 249 | osg::Vec3(0.0f, 1.0f, 0.0f) |
|---|
| 250 | ); |
|---|
| 251 | |
|---|
| 252 | view->setSceneData(mt); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | else view->setSceneData(_oldNode.get()); |
|---|
| 256 | |
|---|
| 257 | return true; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | } |
|---|