1 // MouseCursor.cxx - abstract inteface for mouse cursor control
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "MouseCursor.hxx"
27 #include <boost/foreach.hpp>
29 #include <osgViewer/GraphicsWindow>
30 #include <osgViewer/Viewer>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/simgear_config.h>
34 #include <simgear/structure/commands.hxx>
37 #include "CocoaMouseCursor.hxx"
40 #include <Main/fg_props.hxx>
41 #include <Main/globals.hxx>
42 #include <Viewer/renderer.hxx>
43 #include <Main/fg_os.hxx> // for fgWarpMouse
49 * @brief when no native cursor implementation is available, use the osgViewer support. This
50 * has several limitations but is better than nothing
52 class StockOSGCursor : public FGMouseCursor
56 mCursorObscured(false),
58 mCursor(osgViewer::GraphicsWindow::InheritCursor)
60 mActualCursor = mCursor;
62 globals->get_renderer()->getViewer()->getWindows(mWindows);
65 virtual void setCursor(Cursor aCursor)
67 mCursor = translateCursor(aCursor);
71 virtual void setCursorVisible(bool aVis)
73 if (mCursorObscured == aVis) {
77 mCursorVisible = aVis;
81 virtual void hideCursorUntilMouseMove()
83 if (mCursorObscured) {
87 mCursorObscured = true;
91 virtual void mouseMoved()
93 if (mCursorObscured) {
94 mCursorObscured = false;
99 osgViewer::GraphicsWindow::MouseCursor translateCursor(Cursor aCursor)
102 case CURSOR_HAND: return osgViewer::GraphicsWindow::HandCursor;
103 case CURSOR_CROSSHAIR: return osgViewer::GraphicsWindow::CrosshairCursor;
104 case CURSOR_IBEAM: return osgViewer::GraphicsWindow::TextCursor;
105 case CURSOR_LEFT_RIGHT: return osgViewer::GraphicsWindow::LeftRightCursor;
107 default: return osgViewer::GraphicsWindow::InheritCursor;
113 osgViewer::GraphicsWindow::MouseCursor cur = osgViewer::GraphicsWindow::InheritCursor;
114 if (mCursorObscured || !mCursorVisible) {
115 cur = osgViewer::GraphicsWindow::NoCursor;
120 if (cur == mActualCursor) {
124 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, mWindows) {
131 bool mCursorObscured;
133 osgViewer::GraphicsWindow::MouseCursor mCursor, mActualCursor;
134 std::vector<osgViewer::GraphicsWindow*> mWindows;
137 } // of anonymous namespace
139 static FGMouseCursor* static_instance = NULL;
141 FGMouseCursor::FGMouseCursor() :
142 mAutoHideTimeMsec(10000)
146 FGMouseCursor* FGMouseCursor::instance()
148 if (static_instance == NULL) {
151 static_instance = new CocoaMouseCursor;
159 if (static_instance == NULL) {
160 static_instance = new StockOSGCursor;
163 // initialise mouse-hide delay from global properties
165 globals->get_commands()->addCommand("set-cursor", static_instance, &FGMouseCursor::setCursorCommand);
168 return static_instance;
171 void FGMouseCursor::setAutoHideTimeMsec(unsigned int aMsec)
173 mAutoHideTimeMsec = aMsec;
177 bool FGMouseCursor::setCursorCommand(const SGPropertyNode* arg)
179 // JMT 2013 - I would prefer this was a seperate 'warp' command, but
180 // historically set-cursor has done both.
181 if (arg->hasValue("x") || arg->hasValue("y")) {
182 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
183 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
184 int x = arg->getIntValue("x", mx->getIntValue());
185 int y = arg->getIntValue("y", my->getIntValue());
192 Cursor c = cursorFromString(arg->getStringValue("cursor"));
199 FGMouseCursor::Cursor cursor;
202 const MouseCursorMap mouse_cursor_map[] = {
203 { "inherit", FGMouseCursor::CURSOR_ARROW },
204 { "crosshair", FGMouseCursor::CURSOR_CROSSHAIR },
205 { "left-right", FGMouseCursor::CURSOR_LEFT_RIGHT },
206 { "hand", FGMouseCursor::CURSOR_HAND },
207 { "text", FGMouseCursor::CURSOR_IBEAM },
208 { 0, FGMouseCursor::CURSOR_ARROW }
211 FGMouseCursor::Cursor FGMouseCursor::cursorFromString(const char* cursor_name)
213 for (unsigned int k = 0; mouse_cursor_map[k].name != 0; k++) {
214 if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
215 return mouse_cursor_map[k].cursor;
219 SG_LOG(SG_GENERAL, SG_WARN, "unknown cursor:" << cursor_name);