5 #include <osgViewer/Viewer>
7 #include "FGManipulator.hxx"
9 // The manipulator is responsible for updating a Viewer's camera. It's
10 // event handling method is also a convenient place to run the the FG
11 // idle and draw handlers.
13 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
16 position = matrix.getTrans();
17 attitude = matrix.getRotate();
20 osg::Matrixd FGManipulator::getMatrix() const
22 return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
25 osg::Matrixd FGManipulator::getInverseMatrix() const
27 return (osg::Matrixd::translate(-position)
28 * osg::Matrixd::rotate(attitude.inverse())) ;
31 // Not used, but part of the interface.
32 void FGManipulator::setNode(osg::Node* node)
37 const osg::Node* FGManipulator::getNode() const
42 osg::Node* FGManipulator::getNode()
47 // All the usual translation from window system to FG / plib
48 static int osgToFGModifiers(int modifiers)
51 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT |
52 osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT))
53 result |= KEYMOD_SHIFT;
54 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL |
55 osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
56 result |= KEYMOD_CTRL;
57 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_ALT |
58 osgGA::GUIEventAdapter::MODKEY_RIGHT_ALT))
63 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
64 osgGA::GUIActionAdapter& us)
66 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
71 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
77 const osgViewer::Viewer* viewer;
78 viewer = dynamic_cast<const osgViewer::Viewer*>(&us);
83 const osg::Camera* camera;
84 camera = viewer->getCameraContainingPosition(ea.getX(), ea.getY(), lx, ly);
86 if (!fgOSIsMainCamera(camera))
90 y = int(camera->getViewport()->height() - ly);
95 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
96 osgGA::GUIActionAdapter& us)
99 switch (ea.getEventType()) {
100 case osgGA::GUIEventAdapter::FRAME:
106 case osgGA::GUIEventAdapter::KEYDOWN:
107 case osgGA::GUIEventAdapter::KEYUP:
110 handleKey(ea, key, modmask);
111 eventToViewport(ea, us, x, y);
113 (*keyHandler)(key, modmask, x, y);
116 case osgGA::GUIEventAdapter::PUSH:
117 case osgGA::GUIEventAdapter::RELEASE:
119 bool mainWindow = eventToViewport(ea, us, x, y);
121 switch (ea.getButton()) {
122 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
125 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
128 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
132 if (mouseClickHandler)
133 (*mouseClickHandler)(button,
135 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
138 case osgGA::GUIEventAdapter::MOVE:
139 case osgGA::GUIEventAdapter::DRAG:
140 eventToViewport(ea, us, x, y);
141 if (mouseMotionHandler)
142 (*mouseMotionHandler)(x, y);
144 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
145 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
153 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
157 // XXX Probably other translations are needed too.
159 case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
160 case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
161 case osgGA::GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
162 case osgGA::GUIEventAdapter::KEY_Delete: key = 0x7f; break;
163 case osgGA::GUIEventAdapter::KEY_Tab: key = '\t'; break;
164 case osgGA::GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
165 case osgGA::GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
166 case osgGA::GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
167 case osgGA::GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
168 case osgGA::GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
169 case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
170 case osgGA::GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
171 case osgGA::GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
172 case osgGA::GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
173 case osgGA::GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
174 case osgGA::GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
175 case osgGA::GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
176 case osgGA::GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
177 case osgGA::GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
178 case osgGA::GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
179 case osgGA::GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
180 case osgGA::GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
181 case osgGA::GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
182 case osgGA::GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
183 case osgGA::GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
184 case osgGA::GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
185 case osgGA::GUIEventAdapter::KEY_KP_0: key = 364; break;
186 case osgGA::GUIEventAdapter::KEY_KP_1: key = 363; break;
187 case osgGA::GUIEventAdapter::KEY_KP_2: key = 359; break;
188 case osgGA::GUIEventAdapter::KEY_KP_3: key = 361; break;
189 case osgGA::GUIEventAdapter::KEY_KP_4: key = 356; break;
190 case osgGA::GUIEventAdapter::KEY_KP_5: key = 309; break;
191 case osgGA::GUIEventAdapter::KEY_KP_6: key = 358; break;
192 case osgGA::GUIEventAdapter::KEY_KP_7: key = 362; break;
193 case osgGA::GUIEventAdapter::KEY_KP_8: key = 357; break;
194 case osgGA::GUIEventAdapter::KEY_KP_9: key = 360; break;
195 case osgGA::GUIEventAdapter::KEY_KP_Enter: key = 269; break;
197 modifiers = osgToFGModifiers(ea.getModKeyMask());
198 currentModifiers = modifiers;
199 if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
200 modifiers |= KEYMOD_RELEASED;