]> git.mxchange.org Git - flightgear.git/blob - src/GUI/MouseCursor.cxx
Windows cursor implementation.
[flightgear.git] / src / GUI / MouseCursor.cxx
1 // MouseCursor.cxx - abstract inteface for  mouse cursor control
2
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
4 //
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.
9 //
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.
14 //
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.
18 //
19
20 #ifdef HAVE_CONFIG_H
21   #include "config.h"
22 #endif
23
24 #include "MouseCursor.hxx"
25
26 #include <cstring>
27 #include <boost/foreach.hpp>
28
29 #include <osgViewer/GraphicsWindow>
30 #include <osgViewer/Viewer>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/simgear_config.h>
34 #include <simgear/structure/commands.hxx>
35
36 #ifdef SG_MAC
37 #include "CocoaMouseCursor.hxx"
38 #endif
39
40 #ifdef SG_WINDOWS
41 #include "WindowsMouseCursor.hxx"
42 #endif
43
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
46 #include <Viewer/renderer.hxx>
47 #include <Main/fg_os.hxx> // for fgWarpMouse
48
49 namespace
50 {
51     
52 /**
53  * @brief when no native cursor implementation is available, use the osgViewer support. This
54  * has several limitations but is better than nothing
55  */
56 class StockOSGCursor : public FGMouseCursor
57 {
58 public:
59     StockOSGCursor() :
60         mCursorObscured(false),
61         mCursorVisible(true),
62         mCursor(osgViewer::GraphicsWindow::InheritCursor)
63     {
64         mActualCursor = mCursor;
65         
66         globals->get_renderer()->getViewer()->getWindows(mWindows);
67     }
68
69     virtual void setCursor(Cursor aCursor)
70     {
71         mCursor = translateCursor(aCursor);
72         updateCursor();
73     }
74     
75     virtual void setCursorVisible(bool aVis)
76     {
77         if (mCursorObscured == aVis) {
78             return;
79         }
80         
81         mCursorVisible = aVis;
82         updateCursor();
83     }
84     
85     virtual void hideCursorUntilMouseMove()
86     {
87         if (mCursorObscured) {
88             return;
89         }
90         
91         mCursorObscured = true;
92         updateCursor();
93     }
94     
95     virtual void mouseMoved()
96     {
97         if (mCursorObscured) {
98             mCursorObscured = false;
99             updateCursor();
100         }
101     }
102 private:
103     osgViewer::GraphicsWindow::MouseCursor translateCursor(Cursor aCursor)
104     {
105         switch (aCursor) {
106                 case CURSOR_ARROW: return osgViewer::GraphicsWindow::RightArrowCursor;
107         case CURSOR_HAND: return osgViewer::GraphicsWindow::HandCursor;
108         case CURSOR_CROSSHAIR: return osgViewer::GraphicsWindow::CrosshairCursor;
109         case CURSOR_IBEAM: return osgViewer::GraphicsWindow::TextCursor;
110         case CURSOR_LEFT_RIGHT: return osgViewer::GraphicsWindow::LeftRightCursor;
111                     
112         default:
113                         return osgViewer::GraphicsWindow::RightArrowCursor;  
114         }
115     }
116     
117     void updateCursor()
118     {
119         osgViewer::GraphicsWindow::MouseCursor cur = osgViewer::GraphicsWindow::InheritCursor;
120         if (mCursorObscured || !mCursorVisible) {
121             cur = osgViewer::GraphicsWindow::NoCursor;
122         } else {
123             cur = mCursor;
124         }
125         
126         if (cur == mActualCursor) {
127             return;
128         }
129         
130         BOOST_FOREACH(osgViewer::GraphicsWindow* gw, mWindows) {
131             gw->setCursor(cur);
132         }
133         
134         mActualCursor = cur;
135     }
136     
137     bool mCursorObscured;
138     bool mCursorVisible;
139     osgViewer::GraphicsWindow::MouseCursor mCursor, mActualCursor;
140     std::vector<osgViewer::GraphicsWindow*> mWindows;
141 };
142     
143 } // of anonymous namespace
144
145 static FGMouseCursor* static_instance = NULL;
146
147 FGMouseCursor::FGMouseCursor() :
148     mAutoHideTimeMsec(10000)
149 {
150 }
151
152 FGMouseCursor* FGMouseCursor::instance()
153 {
154     if (static_instance == NULL) {
155     #ifdef SG_MAC
156         if (true) {
157             static_instance = new CocoaMouseCursor;
158         }
159     #endif
160         #ifdef SG_WINDOWS
161         // set osgViewer cursor inherit, otherwise it will interefere
162                 std::vector<osgViewer::GraphicsWindow*> gws;
163                 globals->get_renderer()->getViewer()->getWindows(gws);
164                 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, gws) {
165             gw->setCursor(osgViewer::GraphicsWindow::InheritCursor);
166         }
167
168         // and create our real implementation
169                 static_instance = new WindowsMouseCursor;
170         #endif
171         
172         // X11
173                 
174         if (static_instance == NULL) {
175             static_instance = new StockOSGCursor;
176         }
177         
178         // initialise mouse-hide delay from global properties
179         
180         globals->get_commands()->addCommand("set-cursor", static_instance, &FGMouseCursor::setCursorCommand);
181     }
182     
183     return static_instance;
184 }
185
186 void FGMouseCursor::setAutoHideTimeMsec(unsigned int aMsec)
187 {
188     mAutoHideTimeMsec = aMsec;
189 }
190
191
192 bool FGMouseCursor::setCursorCommand(const SGPropertyNode* arg)
193 {
194     // JMT 2013 - I would prefer this was a seperate 'warp' command, but
195     // historically set-cursor has done both. 
196     if (arg->hasValue("x") || arg->hasValue("y")) {
197         SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
198         SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
199         int x = arg->getIntValue("x", mx->getIntValue());
200         int y = arg->getIntValue("y", my->getIntValue());
201         fgWarpMouse(x, y);
202         mx->setIntValue(x);
203         my->setIntValue(y);
204     }
205
206     
207     Cursor c = cursorFromString(arg->getStringValue("cursor"));    
208     setCursor(c);
209     return true;
210 }
211
212 typedef struct {
213     const char * name;
214     FGMouseCursor::Cursor cursor;
215 } MouseCursorMap;
216
217 const MouseCursorMap mouse_cursor_map[] = {
218     { "inherit", FGMouseCursor::CURSOR_ARROW },
219     { "crosshair", FGMouseCursor::CURSOR_CROSSHAIR },
220     { "left-right", FGMouseCursor::CURSOR_LEFT_RIGHT },
221     { "hand", FGMouseCursor::CURSOR_HAND },
222     { "text", FGMouseCursor::CURSOR_IBEAM },
223     { 0, FGMouseCursor::CURSOR_ARROW }
224 };
225
226 FGMouseCursor::Cursor FGMouseCursor::cursorFromString(const char* cursor_name)
227 {
228     for (unsigned int k = 0; mouse_cursor_map[k].name != 0; k++) {
229         if (!strcmp(mouse_cursor_map[k].name, cursor_name)) {
230             return mouse_cursor_map[k].cursor;
231         }
232     }
233
234     SG_LOG(SG_GENERAL, SG_WARN, "unknown cursor:" << cursor_name);
235     return CURSOR_ARROW;
236 }