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()
48 // All the usual translation from window system to FG / plib
49 int osgToFGModifiers(int modifiers)
52 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT |
53 osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT))
54 result |= KEYMOD_SHIFT;
55 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL |
56 osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
57 result |= KEYMOD_CTRL;
58 if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_ALT |
59 osgGA::GUIEventAdapter::MODKEY_RIGHT_ALT))
65 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
66 osgGA::GUIActionAdapter& us)
68 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
73 void eventToViewport(const osgGA::GUIEventAdapter& ea,
74 osgGA::GUIActionAdapter& us,
79 if (ea.getMouseYOrientation()
80 == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
81 y = (int)ea.getWindowHeight() - y;
85 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
86 osgGA::GUIActionAdapter& us)
89 switch (ea.getEventType()) {
90 case osgGA::GUIEventAdapter::FRAME:
96 case osgGA::GUIEventAdapter::KEYDOWN:
97 case osgGA::GUIEventAdapter::KEYUP:
100 handleKey(ea, key, modmask);
101 eventToViewport(ea, us, x, y);
103 (*keyHandler)(key, modmask, x, y);
106 case osgGA::GUIEventAdapter::PUSH:
107 case osgGA::GUIEventAdapter::RELEASE:
109 eventToViewport(ea, us, x, y);
111 switch (ea.getButton()) {
112 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
115 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
118 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
122 if (mouseClickHandler)
123 (*mouseClickHandler)(button,
125 == osgGA::GUIEventAdapter::RELEASE), x, y);
128 case osgGA::GUIEventAdapter::MOVE:
129 case osgGA::GUIEventAdapter::DRAG:
130 eventToViewport(ea, us, x, y);
131 if (mouseMotionHandler)
132 (*mouseMotionHandler)(x, y);
134 case osgGA::GUIEventAdapter::RESIZE:
135 if (windowResizeHandler)
136 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
138 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
139 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
147 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
151 // XXX Probably other translations are needed too.
153 case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
154 case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
155 case osgGA::GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
156 case osgGA::GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
157 case osgGA::GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
158 case osgGA::GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
159 case osgGA::GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
160 case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
161 case osgGA::GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
162 case osgGA::GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
163 case osgGA::GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
164 case osgGA::GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
165 case osgGA::GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
166 case osgGA::GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
167 case osgGA::GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
168 case osgGA::GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
169 case osgGA::GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
170 case osgGA::GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
171 case osgGA::GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
172 case osgGA::GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
173 case osgGA::GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
174 case osgGA::GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
175 case osgGA::GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
177 modifiers = osgToFGModifiers(ea.getModKeyMask());
178 currentModifiers = modifiers;
179 if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
180 modifiers |= KEYMOD_RELEASED;