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