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,
77 osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&us);
78 osg::Viewport* viewport = viewer->getCamera()->getViewport();
79 const osg::GraphicsContext::Traits* traits
80 = viewer->getCamera()->getGraphicsContext()->getTraits();
83 if (ea.getMouseYOrientation()
84 == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
85 y = (int)traits->height - y;
89 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
90 osgGA::GUIActionAdapter& us)
93 switch (ea.getEventType()) {
94 case osgGA::GUIEventAdapter::FRAME:
100 case osgGA::GUIEventAdapter::KEYDOWN:
101 case osgGA::GUIEventAdapter::KEYUP:
104 handleKey(ea, key, modmask);
105 eventToViewport(ea, us, x, y);
107 (*keyHandler)(key, modmask, x, y);
110 case osgGA::GUIEventAdapter::PUSH:
111 case osgGA::GUIEventAdapter::RELEASE:
113 eventToViewport(ea, us, x, y);
115 switch (ea.getButton()) {
116 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
119 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
122 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
126 if (mouseClickHandler)
127 (*mouseClickHandler)(button,
129 == osgGA::GUIEventAdapter::RELEASE), x, y);
132 case osgGA::GUIEventAdapter::MOVE:
133 case osgGA::GUIEventAdapter::DRAG:
134 eventToViewport(ea, us, x, y);
135 if (mouseMotionHandler)
136 (*mouseMotionHandler)(x, y);
138 case osgGA::GUIEventAdapter::RESIZE:
139 if (windowResizeHandler)
140 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
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;