]> git.mxchange.org Git - flightgear.git/blob - src/Main/FGManipulator.cxx
Modified Files:
[flightgear.git] / src / Main / FGManipulator.cxx
1 #include <osg/Math>
2
3 #include <osgViewer/Viewer>
4
5 #include <plib/pu.h>
6 #include "FGManipulator.hxx"
7
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.
11
12 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
13 {
14     // Yuck
15     position = matrix.getTrans();
16     attitude = matrix.getRotate();
17 }
18
19 osg::Matrixd FGManipulator::getMatrix() const
20 {
21     return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
22 }
23
24 osg::Matrixd FGManipulator::getInverseMatrix() const
25 {
26     return (osg::Matrixd::translate(-position)
27             * osg::Matrixd::rotate(attitude.inverse())) ;
28 }
29
30 // Not used, but part of the interface.
31 void FGManipulator::setNode(osg::Node* node)
32 {
33     _node = node;
34 }
35     
36 const osg::Node* FGManipulator::getNode() const
37 {
38     return _node.get();
39 }
40
41 osg::Node* FGManipulator::getNode()
42 {
43     return _node.get();
44 }
45
46 namespace {
47 // All the usual translation from window system to FG / plib
48 int osgToFGModifiers(int modifiers)
49 {
50     int result = 0;
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))
59         result |= KEYMOD_ALT;
60     return result;
61 }
62 } // namespace
63
64 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
65                          osgGA::GUIActionAdapter& us)
66 {
67     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
68     (void)handle(ea, us);
69 }
70
71 namespace {
72 void eventToViewport(const osgGA::GUIEventAdapter& ea,
73                      osgGA::GUIActionAdapter& us,
74                      int& x, int& y)
75 {
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();
80     x = (int)ea.getX();
81     y = (int)ea.getY();
82     if (ea.getMouseYOrientation()
83         == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
84         y = (int)traits->height - y;
85 }
86 }
87
88 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
89                            osgGA::GUIActionAdapter& us)
90 {
91     int x, y;
92     switch (ea.getEventType()) {
93     case osgGA::GUIEventAdapter::FRAME:
94         if (idleHandler)
95             (*idleHandler)();
96         if (drawHandler)
97             (*drawHandler)();
98         return true;
99     case osgGA::GUIEventAdapter::KEYDOWN:
100     case osgGA::GUIEventAdapter::KEYUP:
101     {
102         int key, modmask;
103         handleKey(ea, key, modmask);
104         eventToViewport(ea, us, x, y);
105         if (keyHandler)
106             (*keyHandler)(key, modmask, x, y);
107     }
108     return true;
109     case osgGA::GUIEventAdapter::PUSH:
110     case osgGA::GUIEventAdapter::RELEASE:
111     {
112         eventToViewport(ea, us, x, y);
113         int button = 0;
114         switch (ea.getButton()) {
115         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
116             button = 0;
117             break;
118         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
119             button = 1;
120             break;
121         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
122             button = 2;
123             break;
124         }
125         if (mouseClickHandler)
126             (*mouseClickHandler)(button,
127                                  (ea.getEventType()
128                                   == osgGA::GUIEventAdapter::RELEASE), x, y);
129         return true;
130     }
131     case osgGA::GUIEventAdapter::MOVE:
132     case osgGA::GUIEventAdapter::DRAG:
133         eventToViewport(ea, us, x, y);
134         if (mouseMotionHandler)
135             (*mouseMotionHandler)(x, y);
136         return true;
137     case osgGA::GUIEventAdapter::RESIZE:
138         if (windowResizeHandler)
139             (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
140         return true;
141     default:
142         return false;
143     }
144 }
145             
146 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
147                               int& modifiers)
148 {
149     key = ea.getKey();
150     // XXX Probably other translations are needed too.
151     switch (key) {
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;
175     }
176     modifiers = osgToFGModifiers(ea.getModKeyMask());
177     currentModifiers = modifiers;
178     if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
179         modifiers |= KEYMOD_RELEASED;
180 }
181