3 #include <osgViewer/Viewer>
6 #include "FGManipulator.hxx"
8 // The manipulator is responsible for updating a Viewer's camera. It's
9 // event handling method is also a convenient place to run the the FG
10 // idle and draw handlers.
12 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
15 position = matrix.getTrans();
16 attitude = matrix.getRotate();
19 osg::Matrixd FGManipulator::getMatrix() const
21 return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
24 osg::Matrixd FGManipulator::getInverseMatrix() const
26 return (osg::Matrixd::translate(-position)
27 * osg::Matrixd::rotate(attitude.inverse())) ;
30 // Not used, but part of the interface.
31 void FGManipulator::setNode(osg::Node* node)
36 const osg::Node* FGManipulator::getNode() const
41 osg::Node* FGManipulator::getNode()
47 // All the usual translation from window system to FG / plib
48 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))
64 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
65 osgGA::GUIActionAdapter& us)
67 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
72 void eventToViewport(const osgGA::GUIEventAdapter& ea,
73 osgGA::GUIActionAdapter& us,
76 osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&us);
77 osg::Viewport* viewport = viewer->getCamera()->getViewport();
78 const osg::GraphicsContext::Traits* traits
79 = viewer->getCamera()->getGraphicsContext()->getTraits();
82 if (ea.getMouseYOrientation()
83 == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
84 y = (int)traits->height - y;
88 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
89 osgGA::GUIActionAdapter& us)
92 switch (ea.getEventType()) {
93 case osgGA::GUIEventAdapter::FRAME:
99 case osgGA::GUIEventAdapter::KEYDOWN:
100 case osgGA::GUIEventAdapter::KEYUP:
103 handleKey(ea, key, modmask);
104 eventToViewport(ea, us, x, y);
106 (*keyHandler)(key, modmask, x, y);
109 case osgGA::GUIEventAdapter::PUSH:
110 case osgGA::GUIEventAdapter::RELEASE:
112 eventToViewport(ea, us, x, y);
114 switch (ea.getButton()) {
115 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
118 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
121 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
125 if (mouseClickHandler)
126 (*mouseClickHandler)(button,
128 == osgGA::GUIEventAdapter::RELEASE), x, y);
131 case osgGA::GUIEventAdapter::MOVE:
132 case osgGA::GUIEventAdapter::DRAG:
133 eventToViewport(ea, us, x, y);
134 if (mouseMotionHandler)
135 (*mouseMotionHandler)(x, y);
137 case osgGA::GUIEventAdapter::RESIZE:
138 if (windowResizeHandler)
139 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
146 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
150 // XXX Probably other translations are needed too.
152 case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
153 case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
154 case osgGA::GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
155 case osgGA::GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
156 case osgGA::GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
157 case osgGA::GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
158 case osgGA::GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
159 case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
160 case osgGA::GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
161 case osgGA::GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
162 case osgGA::GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
163 case osgGA::GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
164 case osgGA::GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
165 case osgGA::GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
166 case osgGA::GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
167 case osgGA::GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
168 case osgGA::GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
169 case osgGA::GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
170 case osgGA::GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
171 case osgGA::GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
172 case osgGA::GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
173 case osgGA::GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
174 case osgGA::GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
176 modifiers = osgToFGModifiers(ea.getModKeyMask());
177 currentModifiers = modifiers;
178 if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
179 modifiers |= KEYMOD_RELEASED;