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 FGManipulator::FGManipulator() :
14 idleHandler(0), drawHandler(0), windowResizeHandler(0), keyHandler(0),
15 mouseClickHandler(0), mouseMotionHandler(0), currentModifiers(0),
16 osgModifiers(0), resizable(true)
18 using namespace osgGA;
20 keyMaskMap[GUIEventAdapter::KEY_Shift_L]
21 = GUIEventAdapter::MODKEY_LEFT_SHIFT;
22 keyMaskMap[GUIEventAdapter::KEY_Shift_R]
23 = GUIEventAdapter::MODKEY_RIGHT_SHIFT;
24 keyMaskMap[GUIEventAdapter::KEY_Control_L]
25 = GUIEventAdapter::MODKEY_LEFT_CTRL;
26 keyMaskMap[GUIEventAdapter::KEY_Control_R]
27 = GUIEventAdapter::MODKEY_RIGHT_CTRL;
28 keyMaskMap[GUIEventAdapter::KEY_Alt_L] = GUIEventAdapter::MODKEY_LEFT_ALT;
29 keyMaskMap[GUIEventAdapter::KEY_Alt_R] = GUIEventAdapter::MODKEY_RIGHT_ALT;
30 // We have to implement numlock too.
31 numlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = '0';
32 numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1';
33 numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2';
34 numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3';
35 numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4';
36 numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
37 numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6';
38 numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7';
39 numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8';
40 numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9';
43 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
46 position = matrix.getTrans();
47 attitude = matrix.getRotate();
50 osg::Matrixd FGManipulator::getMatrix() const
52 return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
55 osg::Matrixd FGManipulator::getInverseMatrix() const
57 return (osg::Matrixd::translate(-position)
58 * osg::Matrixd::rotate(attitude.inverse())) ;
61 // Not used, but part of the interface.
62 void FGManipulator::setNode(osg::Node* node)
67 const osg::Node* FGManipulator::getNode() const
72 osg::Node* FGManipulator::getNode()
77 // All the usual translation from window system to FG / plib
78 static int osgToFGModifiers(int modifiers)
81 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT |
82 osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT))
83 result |= KEYMOD_SHIFT;
84 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL |
85 osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
86 result |= KEYMOD_CTRL;
87 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_ALT |
88 osgGA::GUIEventAdapter::MODKEY_RIGHT_ALT))
93 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
94 osgGA::GUIActionAdapter& us)
96 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
101 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
107 const osgViewer::Viewer* viewer;
108 viewer = dynamic_cast<const osgViewer::Viewer*>(&us);
113 const osg::Camera* camera;
114 camera = viewer->getCameraContainingPosition(ea.getX(), ea.getY(), lx, ly);
116 if (!fgOSIsMainCamera(camera))
120 y = int(camera->getViewport()->height() - ly);
125 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
126 osgGA::GUIActionAdapter& us)
129 switch (ea.getEventType()) {
130 case osgGA::GUIEventAdapter::FRAME:
136 case osgGA::GUIEventAdapter::KEYDOWN:
137 case osgGA::GUIEventAdapter::KEYUP:
140 handleKey(ea, key, modmask);
141 eventToViewport(ea, us, x, y);
143 (*keyHandler)(key, modmask, x, y);
146 case osgGA::GUIEventAdapter::PUSH:
147 case osgGA::GUIEventAdapter::RELEASE:
149 bool mainWindow = eventToViewport(ea, us, x, y);
151 switch (ea.getButton()) {
152 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
155 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
158 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
162 if (mouseClickHandler)
163 (*mouseClickHandler)(button,
165 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
168 case osgGA::GUIEventAdapter::MOVE:
169 case osgGA::GUIEventAdapter::DRAG:
170 eventToViewport(ea, us, x, y);
171 if (mouseMotionHandler)
172 (*mouseMotionHandler)(x, y);
174 case osgGA::GUIEventAdapter::RESIZE:
175 if (resizable && windowResizeHandler)
176 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
178 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
179 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
187 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
191 // XXX Probably other translations are needed too.
193 case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
194 case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
195 case osgGA::GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
196 case osgGA::GUIEventAdapter::KEY_Delete: key = 0x7f; break;
197 case osgGA::GUIEventAdapter::KEY_Tab: key = '\t'; break;
198 case osgGA::GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
199 case osgGA::GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
200 case osgGA::GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
201 case osgGA::GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
202 case osgGA::GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
203 case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
204 case osgGA::GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
205 case osgGA::GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
206 case osgGA::GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
207 case osgGA::GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
208 case osgGA::GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
209 case osgGA::GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
210 case osgGA::GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
211 case osgGA::GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
212 case osgGA::GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
213 case osgGA::GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
214 case osgGA::GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
215 case osgGA::GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
216 case osgGA::GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
217 case osgGA::GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
218 case osgGA::GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
219 case osgGA::GUIEventAdapter::KEY_KP_Delete: key = '.'; break;
220 case osgGA::GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
221 case osgGA::GUIEventAdapter::KEY_KP_Add: key = '+'; break;
222 case osgGA::GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
223 case osgGA::GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
224 case osgGA::GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
226 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
227 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
229 if (numPadIter != numlockKeyMap.end()) {
230 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
231 key = numPadIter->second;
234 // Track the modifiers because OSG is currently (2.0) broken
235 KeyMaskMap::iterator iter = keyMaskMap.find(key);
236 if (iter != keyMaskMap.end()) {
237 int mask = iter->second;
238 if (eventType == osgGA::GUIEventAdapter::KEYUP)
239 osgModifiers &= ~mask;
241 osgModifiers |= mask;
244 modifiers = osgToFGModifiers(osgModifiers);
245 currentModifiers = modifiers;
246 if (eventType == osgGA::GUIEventAdapter::KEYUP)
247 modifiers |= KEYMOD_RELEASED;