]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGMouseInput.cxx
fix #416: reciprocal filter broken
[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   haveWarped(false),
79   xSizeNode(fgGetNode("/sim/startup/xsize", false ) ),
80   ySizeNode(fgGetNode("/sim/startup/ysize", false ) ),
81   xAccelNode(fgGetNode("/devices/status/mice/mouse/accel-x", true ) ),
82   yAccelNode(fgGetNode("/devices/status/mice/mouse/accel-y", true ) ),
83   hideCursorNode(fgGetNode("/sim/mouse/hide-cursor", true ) ),
84   cursorTimeoutNode(fgGetNode("/sim/mouse/cursor-timeout-sec", true ) )
85 {
86   if( mouseInput == NULL )
87     mouseInput = this;
88 }
89
90 FGMouseInput::~FGMouseInput()
91 {
92   if( mouseInput == this )
93     mouseInput = NULL;
94 }
95
96 void FGMouseInput::init()
97 {
98   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
99   string module = "";
100
101   SGPropertyNode * mouse_nodes = fgGetNode("/input/mice");
102   if (mouse_nodes == 0) {
103     SG_LOG(SG_INPUT, SG_WARN, "No mouse bindings (/input/mice)!!");
104     mouse_nodes = fgGetNode("/input/mice", true);
105   }
106
107   int j;
108   for (int i = 0; i < MAX_MICE; i++) {
109     SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
110     mouse &m = bindings[i];
111
112                                 // Grab node pointers
113     std::ostringstream buf;
114     buf <<  "/devices/status/mice/mouse[" << i << "]/mode";
115     m.mode_node = fgGetNode(buf.str().c_str());
116     if (m.mode_node == NULL) {
117       m.mode_node = fgGetNode(buf.str().c_str(), true);
118       m.mode_node->setIntValue(0);
119     }
120     for (j = 0; j < MAX_MOUSE_BUTTONS; j++) {
121       buf.seekp(ios_base::beg);
122       buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]";
123       m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true);
124       m.mouse_button_nodes[j]->setBoolValue(false);
125     }
126
127                                 // Read all the modes
128     m.nModes = mouse_node->getIntValue("mode-count", 1);
129     m.modes = new mouse_mode[m.nModes];
130
131     for (int j = 0; j < m.nModes; j++) {
132       int k;
133
134                                 // Read the mouse cursor for this mode
135       SGPropertyNode * mode_node = mouse_node->getChild("mode", j, true);
136       const char * cursor_name =
137         mode_node->getStringValue("cursor", "inherit");
138       m.modes[j].cursor = MOUSE_CURSOR_POINTER;
139       for (k = 0; mouse_cursor_map[k].name != 0; k++) {
140         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
141           m.modes[j].cursor = mouse_cursor_map[k].cursor;
142           break;
143         }
144       }
145
146                                 // Read other properties for this mode
147       m.modes[j].constrained = mode_node->getBoolValue("constrained", false);
148       m.modes[j].pass_through = mode_node->getBoolValue("pass-through", false);
149
150                                 // Read the button bindings for this mode
151       m.modes[j].buttons = new FGButton[MAX_MOUSE_BUTTONS];
152       std::ostringstream buf;
153       for (k = 0; k < MAX_MOUSE_BUTTONS; k++) {
154         buf.seekp(ios_base::beg);
155         buf << "mouse button " << k;
156         SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse button " << k);
157         m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module );
158       }
159
160                                 // Read the axis bindings for this mode
161       read_bindings(mode_node->getChild("x-axis", 0, true), m.modes[j].x_bindings, KEYMOD_NONE, module );
162       read_bindings(mode_node->getChild("y-axis", 0, true), m.modes[j].y_bindings, KEYMOD_NONE, module );
163     }
164   }
165
166   fgRegisterMouseClickHandler(mouseClickHandler);
167   fgRegisterMouseMotionHandler(mouseMotionHandler);
168 }
169
170 void FGMouseInput::update ( double dt )
171 {
172   double cursorTimeout = cursorTimeoutNode ? cursorTimeoutNode->getDoubleValue() : 10.0;
173
174   mouse &m = bindings[0];
175   int mode =  m.mode_node->getIntValue();
176   if (mode != m.current_mode) {
177     m.current_mode = mode;
178     m.timeout = cursorTimeout;
179     if (mode >= 0 && mode < m.nModes) {
180       fgSetMouseCursor(m.modes[mode].cursor);
181       m.x = (xSizeNode ? xSizeNode->getIntValue() : 800) / 2;
182       m.y = (ySizeNode ? ySizeNode->getIntValue() : 600) / 2;
183       fgWarpMouse(m.x, m.y);
184       haveWarped = true;
185     } else {
186       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
187       fgSetMouseCursor(MOUSE_CURSOR_POINTER);
188     }
189   }
190
191   if ( hideCursorNode ==NULL || hideCursorNode->getBoolValue() ) {
192       if ( m.x != m.save_x || m.y != m.save_y ) {
193           m.timeout = cursorTimeout;
194           if (fgGetMouseCursor() == MOUSE_CURSOR_NONE)
195               fgSetMouseCursor(m.modes[mode].cursor);
196       } else {
197           m.timeout -= dt;
198           if ( m.timeout <= 0.0 ) {
199               fgSetMouseCursor(MOUSE_CURSOR_NONE);
200               m.timeout = 0.0;
201           }
202       }
203       m.save_x = m.x;
204       m.save_y = m.y;
205   }
206
207   activePickCallbacks.update( dt );
208 }
209
210 FGMouseInput::mouse::mouse ()
211   : x(-1),
212     y(-1),
213     save_x(-1),
214     save_y(-1),
215     nModes(1),
216     current_mode(0),
217     timeout(0),
218     modes(NULL)
219 {
220 }
221
222 FGMouseInput::mouse::~mouse ()
223 {
224   delete [] modes;
225 }
226
227 FGMouseInput::mouse_mode::mouse_mode ()
228   : cursor(MOUSE_CURSOR_POINTER),
229     constrained(false),
230     pass_through(false),
231     buttons(NULL)
232 {
233 }
234
235 FGMouseInput::mouse_mode::~mouse_mode ()
236 {
237                                 // FIXME: memory leak
238 //   for (int i = 0; i < KEYMOD_MAX; i++) {
239 //     int j;
240 //     for (j = 0; i < x_bindings[i].size(); j++)
241 //       delete bindings[i][j];
242 //     for (j = 0; j < y_bindings[i].size(); j++)
243 //       delete bindings[i][j];
244 //   }
245   delete [] buttons;
246 }
247
248 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
249 {
250   int modifiers = fgGetKeyModifiers();
251
252   mouse &m = bindings[0];
253   mouse_mode &mode = m.modes[m.current_mode];
254
255                                 // Let the property manager know.
256   if (b >= 0 && b < MAX_MOUSE_BUTTONS)
257     m.mouse_button_nodes[b]->setBoolValue(updown == MOUSE_BUTTON_DOWN);
258
259                                 // Pass on to PUI and the panel if
260                                 // requested, and return if one of
261                                 // them consumes the event.
262
263   if (updown != MOUSE_BUTTON_DOWN) {
264     // Execute the mouse up event in any case, may be we should
265     // stop processing here?
266     while (!activePickCallbacks[b].empty()) {
267       activePickCallbacks[b].front()->buttonReleased();
268       activePickCallbacks[b].pop_front();
269     }
270   }
271
272   if (mode.pass_through) {
273     if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
274       return;
275     else if (0 <= x && 0 <= y && (globals->get_current_panel() != 0) &&
276              globals->get_current_panel()->getVisibility() &&
277              globals->get_current_panel()->doMouseAction(b, updown, x, y))
278       return;
279     else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
280       return;
281     else {
282       // pui didn't want the click event so compute a
283       // scenegraph intersection point corresponding to the mouse click
284       if (updown == MOUSE_BUTTON_DOWN) {
285         activePickCallbacks.init( b, ea );
286       }
287     }
288   }
289
290   // OK, PUI and the panel didn't want the click
291   if (b >= MAX_MOUSE_BUTTONS) {
292     SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
293            << " where only " << MAX_MOUSE_BUTTONS << " expected");
294     return;
295   }
296
297   m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
298 }
299
300 void FGMouseInput::doMouseMotion (int x, int y)
301 {
302   // Don't call fgGetKeyModifiers() here, until we are using a
303   // toolkit that supports getting the mods from outside a key
304   // callback.  Glut doesn't.
305   int modifiers = KEYMOD_NONE;
306
307   int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
308   int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
309
310   mouse &m = bindings[0];
311
312   if (m.current_mode < 0 || m.current_mode >= m.nModes) {
313       m.x = x;
314       m.y = y;
315       return;
316   }
317   mouse_mode &mode = m.modes[m.current_mode];
318
319                                 // Pass on to PUI if requested, and return
320                                 // if PUI consumed the event.
321   if (mode.pass_through && puMouse(x, y)) {
322       m.x = x;
323       m.y = y;
324       return;
325   }
326   
327   if (haveWarped)
328   {
329       // don't fire mouse-movement events at the first update after warping the mouse,
330       // just remember the new mouse position
331       haveWarped = false;
332   }
333   else
334   {
335                                 // OK, PUI didn't want the event,
336                                 // so we can play with it.
337       if (x != m.x) {
338         int delta = x - m.x;
339         xAccelNode->setIntValue( delta );
340         for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
341           mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
342       }
343       if (y != m.y) {
344         int delta = y - m.y;
345         yAccelNode->setIntValue( -delta );
346         for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
347           mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
348       }
349   }
350                                 // Constrain the mouse if requested
351   if (mode.constrained) {
352     int new_x=x,new_y=y;
353     
354     bool need_warp = false;
355     if (x <= (xsize * .25) || x >= (xsize * .75)) {
356       new_x = int(xsize * .5);
357       need_warp = true;
358     }
359
360     if (y <= (ysize * .25) || y >= (ysize * .75)) {
361       new_y = int(ysize * .5);
362       need_warp = true;
363     }
364
365     if (need_warp)
366     {
367       fgWarpMouse(new_x, new_y);
368       haveWarped = true;
369       SG_LOG(SG_INPUT, SG_DEBUG, "Mouse warp: " << x << ", " << y << " => " << new_x << ", " << new_y);
370     }
371   }
372
373   if (m.x != x)
374       fgSetInt("/devices/status/mice/mouse/x", m.x = x);
375
376   if (m.y != y)
377       fgSetInt("/devices/status/mice/mouse/y", m.y = y);
378 }
379
380 void FGMouseInput::mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
381 {
382     if(mouseInput)
383       mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
384 }
385
386 void FGMouseInput::mouseMotionHandler(int x, int y)
387 {
388     if (mouseInput != 0)
389         mouseInput->doMouseMotion(x, y);
390 }
391
392