1 // FGMouseInput.cxx -- handle user input from mouse devices
3 // Written by Torsten Dreyer, started August 2009
4 // Based on work from David Megginson, started May 2001.
6 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // Copyright (C) 2001 David Megginson, david@megginson.com
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.
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.
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.
29 #include "FGMouseInput.hxx"
30 #include "Main/globals.hxx"
34 void ActivePickCallbacks::init( int b, const osgGA::GUIEventAdapter* ea )
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);
53 void ActivePickCallbacks::update( double dt )
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) {
66 #include <Model/panelnode.hxx>
67 #include <Cockpit/panel.hxx>
68 ////////////////////////////////////////////////////////////////////////
69 // The Mouse Input Implementation
70 ////////////////////////////////////////////////////////////////////////
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 },
81 FGMouseInput * FGMouseInput::mouseInput = NULL;
83 FGMouseInput::FGMouseInput() :
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 ) )
92 if( mouseInput == NULL )
96 FGMouseInput::~FGMouseInput()
98 if( mouseInput == this )
102 void FGMouseInput::init()
104 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
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);
114 for (int i = 0; i < MAX_MICE; i++) {
115 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
116 mouse &m = bindings[i];
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);
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);
133 // Read all the modes
134 m.nModes = mouse_node->getIntValue("mode-count", 1);
135 m.modes = new mouse_mode[m.nModes];
137 for (int j = 0; j < m.nModes; j++) {
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;
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);
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 );
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 );
172 fgRegisterMouseClickHandler(mouseClickHandler);
173 fgRegisterMouseMotionHandler(mouseMotionHandler);
176 void FGMouseInput::update ( double dt )
178 double cursorTimeout = cursorTimeoutNode ? cursorTimeoutNode->getDoubleValue() : 10.0;
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);
192 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
193 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
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);
204 if ( m.timeout <= 0.0 ) {
205 fgSetMouseCursor(MOUSE_CURSOR_NONE);
213 activePickCallbacks.update( dt );
216 FGMouseInput::mouse::mouse ()
228 FGMouseInput::mouse::~mouse ()
233 FGMouseInput::mouse_mode::mouse_mode ()
234 : cursor(MOUSE_CURSOR_POINTER),
241 FGMouseInput::mouse_mode::~mouse_mode ()
243 // FIXME: memory leak
244 // for (int i = 0; i < KEYMOD_MAX; i++) {
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];
254 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
256 int modifiers = fgGetKeyModifiers();
258 mouse &m = bindings[0];
259 mouse_mode &mode = m.modes[m.current_mode];
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);
265 // Pass on to PUI and the panel if
266 // requested, and return if one of
267 // them consumes the event.
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();
278 if (mode.pass_through) {
279 // remove once PUI uses standard picking mechanism
280 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
283 // pui didn't want the click event so compute a
284 // scenegraph intersection point corresponding to the mouse click
285 if (updown == MOUSE_BUTTON_DOWN) {
286 activePickCallbacks.init( b, ea );
291 // OK, PUI and the panel didn't want the click
292 if (b >= MAX_MOUSE_BUTTONS) {
293 SG_LOG(SG_INPUT, SG_ALERT, "Mouse button " << b
294 << " where only " << MAX_MOUSE_BUTTONS << " expected");
298 m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
301 void FGMouseInput::doMouseMotion (int x, int y)
303 // Don't call fgGetKeyModifiers() here, until we are using a
304 // toolkit that supports getting the mods from outside a key
305 // callback. Glut doesn't.
306 int modifiers = KEYMOD_NONE;
308 int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
309 int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
311 mouse &m = bindings[0];
313 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
318 mouse_mode &mode = m.modes[m.current_mode];
320 // Pass on to PUI if requested, and return
321 // if PUI consumed the event.
322 if (mode.pass_through && puMouse(x, y)) {
330 // don't fire mouse-movement events at the first update after warping the mouse,
331 // just remember the new mouse position
336 // OK, PUI didn't want the event,
337 // so we can play with it.
340 xAccelNode->setIntValue( delta );
341 for (unsigned int i = 0; i < mode.x_bindings[modifiers].size(); i++)
342 mode.x_bindings[modifiers][i]->fire(double(delta), double(xsize));
346 yAccelNode->setIntValue( -delta );
347 for (unsigned int i = 0; i < mode.y_bindings[modifiers].size(); i++)
348 mode.y_bindings[modifiers][i]->fire(double(delta), double(ysize));
351 // Constrain the mouse if requested
352 if (mode.constrained) {
355 bool need_warp = false;
356 if (x <= (xsize * .25) || x >= (xsize * .75)) {
357 new_x = int(xsize * .5);
361 if (y <= (ysize * .25) || y >= (ysize * .75)) {
362 new_y = int(ysize * .5);
368 fgWarpMouse(new_x, new_y);
370 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse warp: " << x << ", " << y << " => " << new_x << ", " << new_y);
375 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
378 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
381 void FGMouseInput::mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
384 mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
387 void FGMouseInput::mouseMotionHandler(int x, int y)
390 mouseInput->doMouseMotion(x, y);