OpenSceneGraph 3.6.5
EventInterface
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14// Code by: Jeremy Moles (cubicool) 2007-2008
15
16#ifndef OSGWIDGET_EVENT_INTERFACE
17#define OSGWIDGET_EVENT_INTERFACE
18
19#include <list>
20#include <osg/ref_ptr>
21#include <osg/observer_ptr>
22#include <osg/Referenced>
23
24#include <osgWidget/Export>
25
26namespace osgWidget {
27
28class WindowManager;
29class Window;
30class Widget;
31
48
49// Helpful wrapper around using the raw types, since it often doesn't make sense to
50// use some without the others.
59
61{
62 public:
64 double x;
65 double y;
66 int key;
68
70 type (_type),
71 x (0.0f),
72 y (0.0f),
73 key (-1),
74 keyMask (-1),
75 _wm (wm),
76 _window (0),
77 _widget (0),
78 _data (0) {
79 }
80
82 if(_type != EVENT_NONE) type = _type;
83
84 return *this;
85 }
86
87 Event& makeMouse(double _x, double _y, EventType _type = EVENT_NONE) {
88 x = _x;
89 y = _y;
90
91 if(_type != EVENT_NONE) type = _type;
92
93 return *this;
94 }
95
96 Event& makeKey(int _key, int _keyMask, EventType _type = EVENT_NONE) {
97 key = _key;
98 keyMask = _keyMask;
99
100 if(_type != EVENT_NONE) type = _type;
101
102 return *this;
103 }
104
106
108 return _wm;
109 }
110
112 return _window;
113 }
114
115 const Window* getWindow() const {
116 return _window;
117 }
118
120 return _widget;
121 }
122
123 const Widget* getWidget() const {
124 return _widget;
125 }
126
127 void* getData() {
128 return _data;
129 }
130
131 const void* getData() const {
132 return _data;
133 }
134
135 void setData(void* data) {
136 _data = data;
137 }
138
139 protected:
140
141 friend class WindowManager;
142 friend class Window;
143
147 void* _data;
148
149};
150
151// The Callback interface was inspired by the CEGUI project:
152//
153// http://www.cegui.org.uk/wiki/index.php/Main_Page
154//
155// It's a great little way to cleanly implement callbacks for events, although
156// I did change the names a bit to make them more appropriate for OSG. MANY THANKS
157// to the CEGUI project!
158
159// The CallbackInterface, which the highest-level functor keeps a pointer to.
161{
163
164 virtual const char* className() const { return "osgWidget::CallbackInterface"; }
165
166 virtual bool operator()(Event&) = 0;
167};
168
169// The object that facilitates a class method as a callback.
170template<typename T>
172{
173 public:
174 typedef bool (T::*ObjectCallbackType)(Event&);
175
177 _object (obj),
178 _callback (callback) {}
179
180 virtual bool operator()(Event& ev) {
181 return (_object->*_callback)(ev);
182 }
183
184 private:
185 T* _object;
186 ObjectCallbackType _callback;
187};
188
189// The object that facilitates general functions as callbacks.
190template<typename T>
192{
193 public:
194 FunctionCallback(T* callback):
195 _callback(callback) {
196 }
197
198 virtual bool operator()(Event& ev) {
199 return (*_callback)(ev);
200 }
201 protected:
203};
204
205// The highlevel functor.
207{
208 public:
210 Callback(const Callback& rhs): osg::Referenced(rhs), _type(rhs._type), _data(rhs._data), _callback(rhs._callback) {}
211
212 virtual const char* className() const { return "osgWidget::Callback"; }
213
214 // The more traditional style of OSG Callbacks.
215 Callback(EventType type, void* data=0):
216 _type (type),
217 _data (data),
218 _callback (0) {
219 }
220
221 // Creates a Callback that is bound to a member function.
222 template<typename T>
223 Callback(bool (T::*function)(Event&), T* obj, EventType type, void* data=0):
224 _type (type),
225 _data (data),
226 _callback (new ObjectCallback<T>(function, obj)) {
227 }
228
229 // Creates a Callback that is bound to a functor pointer.
230 template<typename T>
231 Callback(T* functor, EventType type, void* data=0):
232 _type (type),
233 _data (data),
234 _callback (new FunctionCallback<T>(functor)) {
235 }
236
237 virtual ~Callback() {}
238
239 virtual bool operator()(Event& ev) {
240 if(!_callback) return false;
241
242 return (*_callback)(ev);
243 }
244
246 return _type;
247 }
248
249 void* getData() {
250 return _data;
251 }
252
253 const void* getData() const {
254 return _data;
255 }
256
257 protected:
259 void* _data;
260
261 // We use a ref_ptr here so that we don't have to worry about memory.
263
264};
265
266
268{
269 public:
270 EventInterface(): _eventMask(EVENT_NONE) {}
271
273 _eventMask (ei._eventMask),
274 _callbacks (ei._callbacks) {}
275
276 virtual ~EventInterface() {}
277
278 // These functions take as their final argument the WindowManager which issued the
279 // request. This is sometimes useful to get information about key state, etc.
280
281 // Notify the EventInterface object that is has been focused or unfocused; since
282 // this isn't always bound to a mouse event (i.e., if you want to be able to use
283 // the TAB key to focus), we need separate events here.
284 virtual bool focus (const WindowManager*) { return false; }
285 virtual bool unfocus (const WindowManager*) { return false; }
286
287 // Mouse events, pretty self-explanatory.
288 virtual bool mouseEnter (double, double, const WindowManager*) { return false; }
289 virtual bool mouseOver (double, double, const WindowManager*) { return false; }
290 virtual bool mouseLeave (double, double, const WindowManager*) { return false; }
291 virtual bool mouseDrag (double, double, const WindowManager*) { return false; }
292 virtual bool mousePush (double, double, const WindowManager*) { return false; }
293 virtual bool mouseRelease (double, double, const WindowManager*) { return false; }
294 virtual bool mouseScroll (double, double, const WindowManager*) { return false; }
295
296 // These functions pass the osgGA::GUIEventAdapter::KeySymbol and KeyModMask and,
297 // as above, the WindowManager.
298 virtual bool keyDown (int, int, const WindowManager*) { return false; }
299 virtual bool keyUp (int, int, const WindowManager*) { return false; }
300
301 void setEventMask(unsigned int mask) {
302 _eventMask = mask;
303 }
304
305 void addEventMask(unsigned int mask) {
306 _eventMask |= mask;
307 }
308
309 void removeEventMask(unsigned int mask) {
310 _eventMask ^= mask;
311 }
312
313 unsigned int getEventMask() const {
314 return _eventMask;
315 }
316
317 typedef std::list<osg::ref_ptr<Callback> > CallbackList;
318
319 inline CallbackList& getCallbacks() { return _callbacks; }
320 inline const CallbackList& getCallbacks() const { return _callbacks; }
321
323 _callbacks.push_back(cb);
324 }
325
327 if(ev.type == EVENT_NONE || !(_eventMask & ev.type)) return false;
328
329 for(CallbackList::iterator i = _callbacks.begin(); i != _callbacks.end(); i++) {
330 // This is the OLD method; testing a new method below.
331 // if(i->getType() == ev.type && (*i)(ev)) return true;
332
333 if(i->get()->getType() & ev.type) {
334 ev.setData(i->get()->getData());
335
336 if((*i->get())(ev)) return true;
337 }
338 }
339
340 return false;
341 }
342
344 if(ev.type == EVENT_NONE || !(_eventMask & ev.type)) return false;
345
346 bool handled = false;
347
348 if(ev.type == EVENT_FOCUS) handled = focus(ev.getWindowManager());
349
350 else if(ev.type == EVENT_UNFOCUS) handled = unfocus(ev.getWindowManager());
351
352 else if(ev.type == EVENT_MOUSE_ENTER)
353 handled = mouseEnter(ev.x, ev.y, ev.getWindowManager())
354 ;
355
356 else if(ev.type == EVENT_MOUSE_OVER)
357 handled = mouseOver(ev.x, ev.y, ev.getWindowManager())
358 ;
359
360 else if(ev.type == EVENT_MOUSE_LEAVE)
361 handled = mouseLeave(ev.x, ev.y, ev.getWindowManager())
362 ;
363
364 else if(ev.type == EVENT_MOUSE_DRAG)
365 handled = mouseDrag(ev.x, ev.y, ev.getWindowManager())
366 ;
367
368 else if(ev.type == EVENT_MOUSE_PUSH)
369 handled = mousePush(ev.x, ev.y, ev.getWindowManager())
370 ;
371
372 else if(ev.type == EVENT_MOUSE_RELEASE)
373 handled = mouseRelease(ev.x, ev.y, ev.getWindowManager())
374 ;
375
376 else if(ev.type == EVENT_MOUSE_SCROLL)
377 handled = mouseScroll(ev.x, ev.y, ev.getWindowManager())
378 ;
379
380 else if(ev.type == EVENT_KEY_DOWN)
381 handled = keyDown(ev.key, ev.keyMask, ev.getWindowManager())
382 ;
383
384 else if(ev.type == EVENT_KEY_UP)
385 handled = keyUp(ev.key, ev.keyMask, ev.getWindowManager())
386 ;
387
388 else return false;
389
390 return callCallbacks(ev) || handled;
391 }
392
393 bool canFocus () const { return (_eventMask & EVENT_FOCUS) != 0; }
394 bool canUnfocus () const { return (_eventMask & EVENT_UNFOCUS) != 0; }
395
396 bool canMouseEnter () const { return (_eventMask & EVENT_MOUSE_ENTER) != 0; }
397 bool canMouseOver () const { return (_eventMask & EVENT_MOUSE_OVER) != 0; }
398 bool canMouseLeave () const { return (_eventMask & EVENT_MOUSE_LEAVE) != 0; }
399 bool canMouseDrag () const { return (_eventMask & EVENT_MOUSE_DRAG) != 0; }
400 bool canMousePush () const { return (_eventMask & EVENT_MOUSE_PUSH) != 0; }
401 bool canMouseRelease () const { return (_eventMask & EVENT_MOUSE_RELEASE) != 0; }
402 bool canMouseScroll () const { return (_eventMask & EVENT_MOUSE_SCROLL) != 0; }
403
404 bool canKeyDown () const { return (_eventMask & EVENT_KEY_DOWN) != 0; }
405 bool canKeyUp () const { return (_eventMask & EVENT_KEY_UP) != 0; }
406
407 private:
408
409 unsigned int _eventMask;
410 CallbackList _callbacks;
411
412};
413
414}
415
416#endif
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
The osgWidget library is a NodeKit that extends the core scene graph to support a 2D (and eventually ...
Definition Box:21
EventType
Definition EventInterface:33
@ EVENT_UNFOCUS
Definition EventInterface:36
@ EVENT_MOUSE_SCROLL
Definition EventInterface:43
@ EVENT_MOUSE_ENTER
Definition EventInterface:37
@ EVENT_MOUSE_RELEASE
Definition EventInterface:42
@ EVENT_MOUSE_OVER
Definition EventInterface:38
@ EVENT_FOCUS
Definition EventInterface:35
@ EVENT_ALL
Definition EventInterface:46
@ EVENT_KEY_UP
Definition EventInterface:45
@ EVENT_NONE
Definition EventInterface:34
@ EVENT_MOUSE_DRAG
Definition EventInterface:40
@ EVENT_MOUSE_LEAVE
Definition EventInterface:39
@ EVENT_MOUSE_PUSH
Definition EventInterface:41
@ EVENT_KEY_DOWN
Definition EventInterface:44
EventMask
Definition EventInterface:52
@ EVENT_MASK_MOUSE_CLICK
Definition EventInterface:55
@ EVENT_MASK_MOUSE_DRAG
Definition EventInterface:56
@ EVENT_MASK_FOCUS
Definition EventInterface:53
@ EVENT_MASK_MOUSE_MOVE
Definition EventInterface:54
@ EVENT_MASK_KEY
Definition EventInterface:57
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Base class for providing reference counted objects.
Definition Referenced:44
Definition EventInterface:61
Event & makeType(EventType _type)
Definition EventInterface:81
double x
Definition EventInterface:64
void * _data
Definition EventInterface:147
WindowManager * getWindowManager()
Definition EventInterface:105
Window * _window
Definition EventInterface:145
Event(WindowManager *wm, EventType _type=EVENT_NONE)
Definition EventInterface:69
Event & makeMouse(double _x, double _y, EventType _type=EVENT_NONE)
Definition EventInterface:87
void setData(void *data)
Definition EventInterface:135
EventType type
Definition EventInterface:63
int keyMask
Definition EventInterface:67
void * getData()
Definition EventInterface:127
friend class Window
Definition EventInterface:142
int key
Definition EventInterface:66
Widget * getWidget()
Definition EventInterface:119
double y
Definition EventInterface:65
friend class WindowManager
Definition EventInterface:141
Widget * _widget
Definition EventInterface:146
WindowManager * _wm
Definition EventInterface:144
const void * getData() const
Definition EventInterface:131
Window * getWindow()
Definition EventInterface:111
const Window * getWindow() const
Definition EventInterface:115
Event & makeKey(int _key, int _keyMask, EventType _type=EVENT_NONE)
Definition EventInterface:96
const WindowManager * getWindowManager() const
Definition EventInterface:107
const Widget * getWidget() const
Definition EventInterface:123
Definition EventInterface:161
virtual const char * className() const
Definition EventInterface:164
virtual bool operator()(Event &)=0
virtual ~CallbackInterface()
Definition EventInterface:162
Definition EventInterface:172
bool(T::* ObjectCallbackType)(Event &)
Definition EventInterface:174
virtual bool operator()(Event &ev)
Definition EventInterface:180
ObjectCallback(ObjectCallbackType callback, T *obj)
Definition EventInterface:176
Definition EventInterface:192
FunctionCallback(T *callback)
Definition EventInterface:194
virtual bool operator()(Event &ev)
Definition EventInterface:198
T * _callback
Definition EventInterface:202
Definition EventInterface:207
void * getData()
Definition EventInterface:249
virtual const char * className() const
Definition EventInterface:212
Callback(bool(T::*function)(Event &), T *obj, EventType type, void *data=0)
Definition EventInterface:223
void * _data
Definition EventInterface:259
virtual bool operator()(Event &ev)
Definition EventInterface:239
Callback()
Definition EventInterface:209
osg::ref_ptr< CallbackInterface > _callback
Definition EventInterface:262
Callback(EventType type, void *data=0)
Definition EventInterface:215
Callback(T *functor, EventType type, void *data=0)
Definition EventInterface:231
const void * getData() const
Definition EventInterface:253
Callback(const Callback &rhs)
Definition EventInterface:210
EventType _type
Definition EventInterface:258
virtual ~Callback()
Definition EventInterface:237
EventType getType() const
Definition EventInterface:245
void addCallback(Callback *cb)
Definition EventInterface:322
const CallbackList & getCallbacks() const
Definition EventInterface:320
virtual bool keyUp(int, int, const WindowManager *)
Definition EventInterface:299
bool canMousePush() const
Definition EventInterface:400
std::list< osg::ref_ptr< Callback > > CallbackList
Definition EventInterface:317
void addEventMask(unsigned int mask)
Definition EventInterface:305
EventInterface(const EventInterface &ei)
Definition EventInterface:272
bool canKeyDown() const
Definition EventInterface:404
EventInterface()
Definition EventInterface:270
bool canUnfocus() const
Definition EventInterface:394
bool canMouseEnter() const
Definition EventInterface:396
unsigned int getEventMask() const
Definition EventInterface:313
virtual bool mouseScroll(double, double, const WindowManager *)
Definition EventInterface:294
virtual bool mouseOver(double, double, const WindowManager *)
Definition EventInterface:289
virtual bool unfocus(const WindowManager *)
Definition EventInterface:285
virtual bool mousePush(double, double, const WindowManager *)
Definition EventInterface:292
void setEventMask(unsigned int mask)
Definition EventInterface:301
virtual bool mouseEnter(double, double, const WindowManager *)
Definition EventInterface:288
bool canFocus() const
Definition EventInterface:393
virtual ~EventInterface()
Definition EventInterface:276
CallbackList & getCallbacks()
Definition EventInterface:319
virtual bool focus(const WindowManager *)
Definition EventInterface:284
virtual bool mouseDrag(double, double, const WindowManager *)
Definition EventInterface:291
virtual bool mouseRelease(double, double, const WindowManager *)
Definition EventInterface:293
bool canMouseScroll() const
Definition EventInterface:402
void removeEventMask(unsigned int mask)
Definition EventInterface:309
bool canMouseLeave() const
Definition EventInterface:398
bool canMouseDrag() const
Definition EventInterface:399
bool canMouseRelease() const
Definition EventInterface:401
bool callCallbacks(Event &ev)
Definition EventInterface:326
virtual bool mouseLeave(double, double, const WindowManager *)
Definition EventInterface:290
virtual bool keyDown(int, int, const WindowManager *)
Definition EventInterface:298
bool canMouseOver() const
Definition EventInterface:397
bool canKeyUp() const
Definition EventInterface:405
bool callMethodAndCallbacks(Event &ev)
Definition EventInterface:343
Definition Widget:35
Definition WindowManager:39
#define OSGWIDGET_EXPORT
Definition Export:42

osg logo
Generated at Sun Jul 27 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.