]> git.mxchange.org Git - flightgear.git/blob - src/Main/FGManipulator.cxx
loadxml: if argument "targetnode" isn't defined, return the file data in
[flightgear.git] / src / Main / FGManipulator.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4 #include <osg/Math>
5 #include <osgViewer/Viewer>
6 #include <plib/pu.h>
7 #include "FGManipulator.hxx"
8
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.
12
13 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
14 {
15     // Yuck
16     position = matrix.getTrans();
17     attitude = matrix.getRotate();
18 }
19
20 osg::Matrixd FGManipulator::getMatrix() const
21 {
22     return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
23 }
24
25 osg::Matrixd FGManipulator::getInverseMatrix() const
26 {
27     return (osg::Matrixd::translate(-position)
28             * osg::Matrixd::rotate(attitude.inverse())) ;
29 }
30
31 // Not used, but part of the interface.
32 void FGManipulator::setNode(osg::Node* node)
33 {
34     _node = node;
35 }
36     
37 const osg::Node* FGManipulator::getNode() const
38 {
39     return _node.get();
40 }
41
42 osg::Node* FGManipulator::getNode()
43 {
44     return _node.get();
45 }
46
47 // All the usual translation from window system to FG / plib
48 static 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
63 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
64                          osgGA::GUIActionAdapter& us)
65 {
66     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
67     (void)handle(ea, us);
68 }
69
70 static bool
71 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
72                 int& x, int& y)
73 {
74   x = -1;
75   y = -1;
76
77   const osgViewer::Viewer* viewer;
78   viewer = dynamic_cast<const osgViewer::Viewer*>(&us);
79   if (!viewer)
80       return false;
81
82   float lx, ly;
83   const osg::Camera* camera;
84   camera = viewer->getCameraContainingPosition(ea.getX(), ea.getY(), lx, ly);
85
86   if (!fgOSIsMainCamera(camera))
87       return false;
88
89   x = int(lx);
90   y = int(camera->getViewport()->height() - ly);
91
92   return true;
93 }
94
95 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
96                            osgGA::GUIActionAdapter& us)
97 {
98     int x, y;
99     switch (ea.getEventType()) {
100     case osgGA::GUIEventAdapter::FRAME:
101         if (idleHandler)
102             (*idleHandler)();
103         if (drawHandler)
104             (*drawHandler)();
105         return true;
106     case osgGA::GUIEventAdapter::KEYDOWN:
107     case osgGA::GUIEventAdapter::KEYUP:
108     {
109         int key, modmask;
110         handleKey(ea, key, modmask);
111         eventToViewport(ea, us, x, y);
112         if (keyHandler)
113             (*keyHandler)(key, modmask, x, y);
114         return true;
115     }
116     case osgGA::GUIEventAdapter::PUSH:
117     case osgGA::GUIEventAdapter::RELEASE:
118     {
119         bool mainWindow = eventToViewport(ea, us, x, y);
120         int button = 0;
121         switch (ea.getButton()) {
122         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
123             button = 0;
124             break;
125         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
126             button = 1;
127             break;
128         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
129             button = 2;
130             break;
131         }
132         if (mouseClickHandler)
133             (*mouseClickHandler)(button,
134                                  (ea.getEventType()
135                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
136         return true;
137     }
138     case osgGA::GUIEventAdapter::MOVE:
139     case osgGA::GUIEventAdapter::DRAG:
140         eventToViewport(ea, us, x, y);
141         if (mouseMotionHandler)
142             (*mouseMotionHandler)(x, y);
143         return true;
144     case osgGA::GUIEventAdapter::CLOSE_WINDOW:
145     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
146         fgOSExit(0);
147         return true;
148     default:
149         return false;
150     }
151 }
152
153 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
154                               int& modifiers)
155 {
156     key = ea.getKey();
157     // XXX Probably other translations are needed too.
158     switch (key) {
159     case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
160     case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
161     case osgGA::GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
162     case osgGA::GUIEventAdapter::KEY_Delete: key = 0x7f; break;
163     case osgGA::GUIEventAdapter::KEY_Left:     key = PU_KEY_LEFT;      break;
164     case osgGA::GUIEventAdapter::KEY_Up:       key = PU_KEY_UP;        break;
165     case osgGA::GUIEventAdapter::KEY_Right:    key = PU_KEY_RIGHT;     break;
166     case osgGA::GUIEventAdapter::KEY_Down:     key = PU_KEY_DOWN;      break;
167     case osgGA::GUIEventAdapter::KEY_Page_Up:   key = PU_KEY_PAGE_UP;   break;
168     case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
169     case osgGA::GUIEventAdapter::KEY_Home:     key = PU_KEY_HOME;      break;
170     case osgGA::GUIEventAdapter::KEY_End:      key = PU_KEY_END;       break;
171     case osgGA::GUIEventAdapter::KEY_Insert:   key = PU_KEY_INSERT;    break;
172     case osgGA::GUIEventAdapter::KEY_F1:       key = PU_KEY_F1;        break;
173     case osgGA::GUIEventAdapter::KEY_F2:       key = PU_KEY_F2;        break;
174     case osgGA::GUIEventAdapter::KEY_F3:       key = PU_KEY_F3;        break;
175     case osgGA::GUIEventAdapter::KEY_F4:       key = PU_KEY_F4;        break;
176     case osgGA::GUIEventAdapter::KEY_F5:       key = PU_KEY_F5;        break;
177     case osgGA::GUIEventAdapter::KEY_F6:       key = PU_KEY_F6;        break;
178     case osgGA::GUIEventAdapter::KEY_F7:       key = PU_KEY_F7;        break;
179     case osgGA::GUIEventAdapter::KEY_F8:       key = PU_KEY_F8;        break;
180     case osgGA::GUIEventAdapter::KEY_F9:       key = PU_KEY_F9;        break;
181     case osgGA::GUIEventAdapter::KEY_F10:      key = PU_KEY_F10;       break;
182     case osgGA::GUIEventAdapter::KEY_F11:      key = PU_KEY_F11;       break;
183     case osgGA::GUIEventAdapter::KEY_F12:      key = PU_KEY_F12;       break;
184     case osgGA::GUIEventAdapter::KEY_KP_0: key = 364; break;
185     case osgGA::GUIEventAdapter::KEY_KP_1: key = 363; break;
186     case osgGA::GUIEventAdapter::KEY_KP_2: key = 359; break;
187     case osgGA::GUIEventAdapter::KEY_KP_3: key = 361; break;
188     case osgGA::GUIEventAdapter::KEY_KP_4: key = 356; break;
189     case osgGA::GUIEventAdapter::KEY_KP_5: key = 309; break;
190     case osgGA::GUIEventAdapter::KEY_KP_6: key = 358; break;
191     case osgGA::GUIEventAdapter::KEY_KP_7: key = 362; break;
192     case osgGA::GUIEventAdapter::KEY_KP_8: key = 357; break;
193     case osgGA::GUIEventAdapter::KEY_KP_9: key = 360; break;
194     case osgGA::GUIEventAdapter::KEY_KP_Enter: key = 269; break;
195     }
196     modifiers = osgToFGModifiers(ea.getModKeyMask());
197     currentModifiers = modifiers;
198     if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
199         modifiers |= KEYMOD_RELEASED;
200 }
201