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.
25 #include "FGMouseInput.hxx"
26 #include "Main/globals.hxx"
28 void ActivePickCallbacks::init( int b, const osgGA::GUIEventAdapter* ea )
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);
47 void ActivePickCallbacks::update( double dt )
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) {
60 #include <Model/panelnode.hxx>
61 #include <Cockpit/panel.hxx>
62 ////////////////////////////////////////////////////////////////////////
63 // The Mouse Input Implementation
64 ////////////////////////////////////////////////////////////////////////
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 },
75 FGMouseInput * FGMouseInput::mouseInput = NULL;
77 FGMouseInput::FGMouseInput() :
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 ) )
86 if( mouseInput == NULL )
90 FGMouseInput::~FGMouseInput()
92 if( mouseInput == this )
96 void FGMouseInput::init()
98 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing mouse bindings");
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);
108 for (int i = 0; i < MAX_MICE; i++) {
109 SGPropertyNode * mouse_node = mouse_nodes->getChild("mouse", i, true);
110 mouse &m = bindings[i];
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);
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);
127 // Read all the modes
128 m.nModes = mouse_node->getIntValue("mode-count", 1);
129 m.modes = new mouse_mode[m.nModes];
131 for (int j = 0; j < m.nModes; j++) {
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;
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);
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 );
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 );
166 fgRegisterMouseClickHandler(mouseClickHandler);
167 fgRegisterMouseMotionHandler(mouseMotionHandler);
170 void FGMouseInput::update ( double dt )
172 double cursorTimeout = cursorTimeoutNode ? cursorTimeoutNode->getDoubleValue() : 10.0;
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);
186 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse mode " << mode << " out of range");
187 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
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);
198 if ( m.timeout <= 0.0 ) {
199 fgSetMouseCursor(MOUSE_CURSOR_NONE);
207 activePickCallbacks.update( dt );
210 FGMouseInput::mouse::mouse ()
222 FGMouseInput::mouse::~mouse ()
227 FGMouseInput::mouse_mode::mouse_mode ()
228 : cursor(MOUSE_CURSOR_POINTER),
235 FGMouseInput::mouse_mode::~mouse_mode ()
237 // FIXME: memory leak
238 // for (int i = 0; i < KEYMOD_MAX; i++) {
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];
248 void FGMouseInput::doMouseClick (int b, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
250 int modifiers = fgGetKeyModifiers();
252 mouse &m = bindings[0];
253 mouse_mode &mode = m.modes[m.current_mode];
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);
259 // Pass on to PUI and the panel if
260 // requested, and return if one of
261 // them consumes the event.
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();
272 if (mode.pass_through) {
273 if (0 <= x && 0 <= y && puMouse(b, updown, x, y))
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))
279 else if (0 <= x && 0 <= y && fgHandle3DPanelMouseEvent(b, updown, x, y))
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 );
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");
297 m.modes[m.current_mode].buttons[b].update( modifiers, 0 != updown, x, y);
300 void FGMouseInput::doMouseMotion (int x, int y)
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;
307 int xsize = xSizeNode ? xSizeNode->getIntValue() : 800;
308 int ysize = ySizeNode ? ySizeNode->getIntValue() : 600;
310 mouse &m = bindings[0];
312 if (m.current_mode < 0 || m.current_mode >= m.nModes) {
317 mouse_mode &mode = m.modes[m.current_mode];
319 // Pass on to PUI if requested, and return
320 // if PUI consumed the event.
321 if (mode.pass_through && puMouse(x, y)) {
329 // don't fire mouse-movement events at the first update after warping the mouse,
330 // just remember the new mouse position
335 // OK, PUI didn't want the event,
336 // so we can play with it.
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));
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));
350 // Constrain the mouse if requested
351 if (mode.constrained) {
354 bool need_warp = false;
355 if (x <= (xsize * .25) || x >= (xsize * .75)) {
356 new_x = int(xsize * .5);
360 if (y <= (ysize * .25) || y >= (ysize * .75)) {
361 new_y = int(ysize * .5);
367 fgWarpMouse(new_x, new_y);
369 SG_LOG(SG_INPUT, SG_DEBUG, "Mouse warp: " << x << ", " << y << " => " << new_x << ", " << new_y);
374 fgSetInt("/devices/status/mice/mouse/x", m.x = x);
377 fgSetInt("/devices/status/mice/mouse/y", m.y = y);
380 void FGMouseInput::mouseClickHandler(int button, int updown, int x, int y, bool mainWindow, const osgGA::GUIEventAdapter* ea)
383 mouseInput->doMouseClick(button, updown, x, y, mainWindow, ea);
386 void FGMouseInput::mouseMotionHandler(int x, int y)
389 mouseInput->doMouseMotion(x, y);