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