]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGMouseInput.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / Input / FGMouseInput.cxx
1 // FGMouseInput.cxx -- handle user input from mouse devices
2 //
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
5 //
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25 #include "FGMouseInput.hxx"
26 #include "Main/globals.hxx"
27
28 void ActivePickCallbacks::init( int b, const osgGA::GUIEventAdapter* ea )
29 {
30   // Get the list of hit callbacks. Take the first callback that
31   // accepts the mouse button press and ignore the rest of them
32   // That is they get sorted by distance and by scenegraph depth.
33   // The nearest one is the first one and the deepest
34   // (the most specialized one in the scenegraph) is the first.
35   std::vector<SGSceneryPick> pickList;
36   if (globals->get_renderer()->pick(pickList, ea)) {
37     std::vector<SGSceneryPick>::const_iterator i;
38     for (i = pickList.begin(); i != pickList.end(); ++i) {
39       if (i->callback->buttonPressed(b, i->info)) {
40           (*this)[b].push_back(i->callback);
41           return;
42       }
43     }
44   }
45 }
46
47 void ActivePickCallbacks::update( double dt )
48 {
49   // handle repeatable mouse press events
50   for( iterator mi = begin(); mi != end(); ++mi ) {
51     std::list<SGSharedPtr<SGPickCallback> >::iterator li;
52     for (li = mi->second.begin(); li != mi->second.end(); ++li) {
53       (*li)->update(dt);
54     }
55   }
56 }
57
58
59 #include <plib/pu.h>
60 #include <Model/panelnode.hxx>
61 #include <Cockpit/panel.hxx>
62 ////////////////////////////////////////////////////////////////////////
63 // The Mouse Input Implementation
64 ////////////////////////////////////////////////////////////////////////
65
66 const FGMouseInput::MouseCursorMap FGMouseInput::mouse_cursor_map[] = {
67     { "none", MOUSE_CURSOR_NONE },
68     { "inherit", MOUSE_CURSOR_POINTER },
69     { "wait", MOUSE_CURSOR_WAIT },
70     { "crosshair", MOUSE_CURSOR_CROSSHAIR },
71     { "left-right", MOUSE_CURSOR_LEFTRIGHT },
72     { 0, 0 }
73 };
74
75 FGMouseInput * FGMouseInput::mouseInput = NULL;
76
77 FGMouseInput::FGMouseInput() :
78   xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
79   ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
80   xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
81   yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
82   hideCursorNode(fgGetNode("/sim/mouse/hide-cursor", true ) ),
83   cursorTimeoutNode(fgGetNode("/sim/mouse/cursor-timeout-sec", true ) )
84 {
85   if( mouseInput == NULL )
86     mouseInput = this;
87 }
88
89 FGMouseInput::~FGMouseInput()
90 {
91   if( mouseInput == this )
92     mouseInput = NULL;
93 }
94
95 void FGMouseInput::init()
96 {
97   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
98   string module = "";
99
100   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
101   if (mouse_nodes == 0) {
102     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
103     mouse_nodes = fgGetNode("/input/mice", true);
104   }
105
106   int j;
107   for (int i = 0; i < MAX_MICE; i++) {
108     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
109     mouse &m = bindings[i];
110
111                                 // Grab node pointers
112     std::ostringstream buf;
113     buf <<  "/devices/status/mice/mouse[" << i << "]/mode";
114     m.mode_node = fgGetNode(buf.str().c_str());
115     if (m.mode_node == NULL) {
116       m.mode_node = fgGetNode(buf.str().c_str(), true);
117       m.mode_node->setIntValue(0);
118     }
119     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
120       buf.seekp(ios_base::beg);
121       buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]";
122       m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true);
123       m.mouse_button_nodes[j]->setBoolValue(false);
124     }
125
126                                 // Read all the modes
127     m.nModes = mouse_node->getIntValue("mode-count", 1);
128     m.modes = new mouse_mode[m.nModes];
129
130     for (int j = 0; j < m.nModes; j++) {
131       int k;
132
133                                 // Read the mouse cursor for this mode
134       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
135       const char * cursor_name =
136         mode_node->getStringValue("cursor", "inherit");
137       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
138       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
139         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
140           m.modes[j].cursor = mouse_cursor_map[k].cursor;
141           break;
142         }
143       }
144
145                                 // Read other properties for this mode
146       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
147       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
148
149                                 // Read the button bindings for this mode
150       m.modes[j].buttons = new FGButton[MAX_MOUSE_BUTTONS];
151       std::ostringstream buf;
152       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
153         buf.seekp(ios_base::beg);
154         buf << "mouse button " << k;
155         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
156         m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module );
157       }
158
159                                 // Read the axis bindings for this mode
160       read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module );
161       read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module );
162     }
163   }
164
165   fgRegisterMouseClickHandler(mouseClickHandler);
166   fgRegisterMouseMotionHandler(mouseMotionHandler);
167 }
168
169 void FGMouseInput::update ( double dt )
170 {
171   double cursorTimeout = cursorTimeoutNode ? cursorTimeoutNode->getDoubleValue() : 10.0;
172
173   mouse &m = bindings[0];
174   int mode =  m.mode_node->getIntValue();
175   if (mode != m.current_mode) {
176     m.current_mode = mode;
177     m.timeout = cursorTimeout;
178     if (mode >= 0 && mode < m.nModes) {
179       fgSetMouseCursor(m.modes[mode].cursor);
180       m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
181       m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
182       fgWarpMouse(m.x, m.y);
183     } else {
184       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
185       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
186     }
187   }
188
189   if ( hideCursorNode ==NULL || hideCursorNode->getBoolValue() ) {
190       if ( m.x != m.save_x || m.y != m.save_y ) {
191           m.timeout = cursorTimeout;
192           if (fgGetMouseCursor() == MOUSE_CURSOR_NONE)
193               fgSetMouseCursor(m.modes[mode].cursor);
194       } else {
195           m.timeout -= dt;
196           if ( m.timeout <= 0.0 ) {
197               fgSetMouseCursor(MOUSE_CURSOR_NONE);
198               m.timeout = 0.0;
199           }
200       }
201       m.save_x = m.x;
202       m.save_y = m.y;
203   }
204
205   activePickCallbacks.update( dt );
206 }
207
208 FGMouseInput::mouse::mouse ()
209   : x(-1),
210     y(-1),
211     save_x(-1),
212     save_y(-1),
213     nModes(1),
214     current_mode(0),
215     timeout(0),
216     modes(NULL)
217 {
218 }
219
220 FGMouseInput::mouse::~mouse ()
221 {
222   delete [] modes;
223 }
224
225 FGMouseInput::mouse_mode::mouse_mode ()
226   : cursor(MOUSE_CURSOR_POINTER),
227     constrained(false),
228     pass_through(false),
229     buttons(NULL)
230 {
231 }
232
233 FGMouseInput::mouse_mode::~mouse_mode ()
234 {
235                                 // FIXME: memory leak
236 //   for (int i = 0; i < KEYMOD_MAX; i++) {
237 //     int j;
238 //     for (j = 0; i < x_bindings[i].size(); j++)
239 //       delete bindings[i][j];
240 //     for (j = 0; j < y_bindings[i].size(); j++)
241 //       delete bindings[i][j];
242 //   }
243   delete [] buttons;
244 }
245
246 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
247 {
248   int modifiers = fgGetKeyModifiers();
249
250   mouse &m = bindings[0];
251   mouse_mode &mode = m.modes[m.current_mode];
252
253                                 // Let the property manager know.
254   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
255     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
256
257                                 // Pass on to PUI and the panel if
258                                 // requested, and return if one of
259                                 // them consumes the event.
260
261   if (updown != MOUSE_BUTTON_DOWN) {
262     // Execute the mouse up event in any case, may be we should
263     // stop processing here?
264     while (!activePickCallbacks[b].empty()) {
265       activePickCallbacks[b].front()->buttonReleased();
266       activePickCallbacks[b].pop_front();
267     }
268   }
269
270   if (mode.pass_through) {
271     if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
272       return;
273     else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
274              globals->get_current_panel()->getVisibility() &&
275              globals->get_current_panel()->doMouseAction(b, updown, x, y))
276       return;
277     else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
278       return;
279     else {
280       // pui didn't want the click event so compute a
281       // scenegraph intersection point corresponding to the mouse click
282       if (updown == MOUSE_BUTTON_DOWN) {
283         activePickCallbacks.init( b, ea );
284       }
285     }
286   }
287
288   // OK, PUI and the panel didn't want the click
289   if (b >= MAX_MOUSE_BUTTONS) {
290     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
291            << " where only " << MAX_MOUSE_BUTTONS << " expected");
292     return;
293   }
294
295   m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
296 }
297
298 void FGMouseInput::doMouseMotion (int x, int y)
299 {
300   // Don't call fgGetKeyModifiers() here, until we are using a
301   // toolkit that supports getting the mods from outside a key
302   // callback.  Glut doesn't.
303   int modifiers = KEYMOD_NONE;
304
305   int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
306   int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
307
308   mouse &m = bindings[0];
309
310   if (m.current_mode < 0 || m.current_mode >= m.nModes) {
311       m.x = x;
312       m.y = y;
313       return;
314   }
315   mouse_mode &mode = m.modes[m.current_mode];
316
317                                 // Pass on to PUI if requested, and return
318                                 // if PUI consumed the event.
319   if (mode.pass_through && puMouse(x, y)) {
320       m.x = x;
321       m.y = y;
322       return;
323   }
324
325                                 // OK, PUI didn't want the event,
326                                 // so we can play with it.
327   if (x != m.x) {
328     int delta = x - m.x;
329     xAccelNode->setIntValue( delta );
330     for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
331       mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
332   }
333   if (y != m.y) {
334     int delta = y - m.y;
335     yAccelNode->setIntValue( -delta );
336     for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
337       mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
338   }
339
340                                 // Constrain the mouse if requested
341   if (mode.constrained) {
342     bool need_warp = false;
343     if (x <= (xsize * .25) || x >= (xsize * .75)) {
344       x = int(xsize * .5);
345       need_warp = true;
346     }
347
348     if (y <= (ysize * .25) || y >= (ysize * .75)) {
349       y = int(ysize * .5);
350       need_warp = true;
351     }
352
353     if (need_warp)
354       fgWarpMouse(x, y);
355   }
356
357   if (m.x != x)
358       fgSetInt("/devices/status/mice/mouse/x", m.x = x);
359
360   if (m.y != y)
361       fgSetInt("/devices/status/mice/mouse/y", m.y = y);
362 }
363
364 void FGMouseInput::mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
365 {
366     if(mouseInput)
367       mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
368 }
369
370 void FGMouseInput::mouseMotionHandler(int x, int y)
371 {
372     if (mouseInput != 0)
373         mouseInput->doMouseMotion(x, y);
374 }
375
376