EventInterface
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
// Code by: Jeremy Moles (cubicool) 2007-2008
#ifndef OSGWIDGET_EVENT_INTERFACE
#define OSGWIDGET_EVENT_INTERFACE
#include <list>
#include <osg/ref_ptr>
#include <osg/observer_ptr>
#include <osg/Referenced>
#include <osgWidget/Export>
namespace osgWidget {
class WindowManager;
class Window;
class Widget;
enum EventType
{
EVENT_NONE = 0x0000,
EVENT_FOCUS = 0x0001,
EVENT_UNFOCUS = 0x0002,
EVENT_MOUSE_ENTER = 0x0004,
EVENT_MOUSE_OVER = 0x0008,
EVENT_MOUSE_LEAVE = 0x0010,
EVENT_MOUSE_DRAG = 0x0020,
EVENT_MOUSE_PUSH = 0x0040,
EVENT_MOUSE_RELEASE = 0x0080,
EVENT_MOUSE_SCROLL = 0x0100,
EVENT_KEY_DOWN = 0x0200,
EVENT_KEY_UP = 0x0400,
EVENT_ALL = 0xFFFF
};
// Helpful wrapper around using the raw types, since it often doesn't make sense to
// use some without the others.
enum EventMask
{
EVENT_MASK_FOCUS = EVENT_FOCUS | EVENT_UNFOCUS,
EVENT_MASK_MOUSE_MOVE = EVENT_MOUSE_ENTER | EVENT_MOUSE_OVER | EVENT_MOUSE_LEAVE,
EVENT_MASK_MOUSE_CLICK = EVENT_MOUSE_PUSH | EVENT_MOUSE_RELEASE,
EVENT_MASK_MOUSE_DRAG = EVENT_MASK_MOUSE_MOVE | EVENT_MASK_MOUSE_CLICK | EVENT_MOUSE_DRAG,
EVENT_MASK_KEY = EVENT_KEY_UP | EVENT_KEY_DOWN
};
class OSGWIDGET_EXPORT Event
{
public:
EventType type;
double x;
double y;
int key;
int keyMask;
Event(WindowManager* wm, EventType _type = EVENT_NONE):
type (_type),
x (0.0f),
y (0.0f),
key (-1),
keyMask (-1),
_wm (wm),
_window (0),
_widget (0),
_data (0) {
}
Event& makeType(EventType _type) {
if(_type != EVENT_NONE) type = _type;
return *this;
}
Event& makeMouse(double _x, double _y, EventType _type = EVENT_NONE) {
x = _x;
y = _y;
if(_type != EVENT_NONE) type = _type;
return *this;
}
Event& makeKey(int _key, int _keyMask, EventType _type = EVENT_NONE) {
key = _key;
keyMask = _keyMask;
if(_type != EVENT_NONE) type = _type;
return *this;
}
WindowManager* getWindowManager() { return _wm; }
const WindowManager* getWindowManager() const {
return _wm;
}
Window* getWindow() {
return _window;
}
const Window* getWindow() const {
return _window;
}
Widget* getWidget() {
return _widget;
}
const Widget* getWidget() const {
return _widget;
}
void* getData() {
return _data;
}
const void* getData() const {
return _data;
}
void setData(void* data) {
_data = data;
}
protected:
friend class WindowManager;
friend class Window;
WindowManager* _wm;
Window* _window;
Widget* _widget;
void* _data;
};
// The Callback interface was inspired by the CEGUI project:
//
// http://www.cegui.org.uk/wiki/index.php/Main_Page
//
// It's a great little way to cleanly implement callbacks for events, although
// I did change the names a bit to make them more appropriate for OSG. MANY THANKS
// to the CEGUI project!
// The CallbackInterface, which the highest-level functor keeps a pointer to.
struct CallbackInterface: public osg::Referenced
{
virtual ~CallbackInterface() {}
virtual const char* className() const { return "osgWidget::CallbackInterface"; }
virtual bool operator()(Event&) = 0;
};
// The object that facilitates a class method as a callback.
template<typename T>
class ObjectCallback: public CallbackInterface
{
public:
typedef bool (T::*ObjectCallbackType)(Event&);
ObjectCallback(ObjectCallbackType callback, T* obj):
_object (obj),
_callback (callback) {}
virtual bool operator()(Event& ev) {
return (_object->*_callback)(ev);
}
private:
T* _object;
ObjectCallbackType _callback;
};
// The object that facilitates general functions as callbacks.
template<typename T>
class FunctionCallback: public CallbackInterface
{
public:
FunctionCallback(T* callback):
_callback(callback) {
}
virtual bool operator()(Event& ev) {
return (*_callback)(ev);
}
protected:
T* _callback;
};
// The highlevel functor.
class OSGWIDGET_EXPORT Callback: public osg::Referenced
{
public:
Callback(): _type(EVENT_NONE), _data(0), _callback(0) {}
Callback(const Callback& rhs): osg::Referenced(rhs), _type(rhs._type), _data(rhs._data), _callback(rhs._callback) {}
virtual const char* className() const { return "osgWidget::Callback"; }
// The more traditional style of OSG Callbacks.
Callback(EventType type, void* data=0):
_type (type),
_data (data),
_callback (0) {
}
// Creates a Callback that is bound to a member function.
template<typename T>
Callback(bool (T::*function)(Event&), T* obj, EventType type, void* data=0):
_type (type),
_data (data),
_callback (new ObjectCallback<T>(function, obj)) {
}
// Creates a Callback that is bound to a functor pointer.
template<typename T>
Callback(T* functor, EventType type, void* data=0):
_type (type),
_data (data),
_callback (new FunctionCallback<T>(functor)) {
}
virtual ~Callback() {}
virtual bool operator()(Event& ev) {
if(!_callback) return false;
return (*_callback)(ev);
}
EventType getType() const {
return _type;
}
void* getData() {
return _data;
}
const void* getData() const {
return _data;
}
protected:
EventType _type;
void* _data;
// We use a ref_ptr here so that we don't have to worry about memory.
osg::ref_ptr<CallbackInterface> _callback;
};
class OSGWIDGET_EXPORT EventInterface
{
public:
EventInterface(): _eventMask(EVENT_NONE) {}
EventInterface(const EventInterface& ei):
_eventMask (ei._eventMask),
_callbacks (ei._callbacks) {}
virtual ~EventInterface() {}
// These functions take as their final argument the WindowManager which issued the
// request. This is sometimes useful to get information about key state, etc.
// Notify the EventInterface object that is has been focused or unfocused; since
// this isn't always bound to a mouse event (i.e., if you want to be able to use
// the TAB key to focus), we need separate events here.
virtual bool focus (const WindowManager*) { return false; }
virtual bool unfocus (const WindowManager*) { return false; }
// Mouse events, pretty self-explanatory.
virtual bool mouseEnter (double, double, const WindowManager*) { return false; }
virtual bool mouseOver (double, double, const WindowManager*) { return false; }
virtual bool mouseLeave (double, double, const WindowManager*) { return false; }
virtual bool mouseDrag (double, double, const WindowManager*) { return false; }
virtual bool mousePush (double, double, const WindowManager*) { return false; }
virtual bool mouseRelease (double, double, const WindowManager*) { return false; }
virtual bool mouseScroll (double, double, const WindowManager*) { return false; }
// These functions pass the osgGA::GUIEventAdapter::KeySymbol and KeyModMask and,
// as above, the WindowManager.
virtual bool keyDown (int, int, const WindowManager*) { return false; }
virtual bool keyUp (int, int, const WindowManager*) { return false; }
void setEventMask(unsigned int mask) {
_eventMask = mask;
}
void addEventMask(unsigned int mask) {
_eventMask |= mask;
}
void removeEventMask(unsigned int mask) {
_eventMask ^= mask;
}
unsigned int getEventMask() const {
return _eventMask;
}
typedef std::list<osg::ref_ptr<Callback> > CallbackList;
inline CallbackList& getCallbacks() { return _callbacks; }
inline const CallbackList& getCallbacks() const { return _callbacks; }
void addCallback(Callback* cb) {
_callbacks.push_back(cb);
}
bool callCallbacks(Event& ev) {
if(ev.type == EVENT_NONE || !(_eventMask & ev.type)) return false;
for(CallbackList::iterator i = _callbacks.begin(); i != _callbacks.end(); i++) {
// This is the OLD method; testing a new method below.
// if(i->getType() == ev.type && (*i)(ev)) return true;
if(i->get()->getType() & ev.type) {
ev.setData(i->get()->getData());
if((*i->get())(ev)) return true;
}
}
return false;
}
bool callMethodAndCallbacks(Event& ev) {
if(ev.type == EVENT_NONE || !(_eventMask & ev.type)) return false;
bool handled = false;
if(ev.type == EVENT_FOCUS) handled = focus(ev.getWindowManager());
else if(ev.type == EVENT_UNFOCUS) handled = unfocus(ev.getWindowManager());
else if(ev.type == EVENT_MOUSE_ENTER)
handled = mouseEnter(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_MOUSE_OVER)
handled = mouseOver(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_MOUSE_LEAVE)
handled = mouseLeave(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_MOUSE_DRAG)
handled = mouseDrag(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_MOUSE_PUSH)
handled = mousePush(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_MOUSE_RELEASE)
handled = mouseRelease(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_MOUSE_SCROLL)
handled = mouseScroll(ev.x, ev.y, ev.getWindowManager())
;
else if(ev.type == EVENT_KEY_DOWN)
handled = keyDown(ev.key, ev.keyMask, ev.getWindowManager())
;
else if(ev.type == EVENT_KEY_UP)
handled = keyUp(ev.key, ev.keyMask, ev.getWindowManager())
;
else return false;
return callCallbacks(ev) || handled;
}
bool canFocus () const { return (_eventMask & EVENT_FOCUS) != 0; }
bool canUnfocus () const { return (_eventMask & EVENT_UNFOCUS) != 0; }
bool canMouseEnter () const { return (_eventMask & EVENT_MOUSE_ENTER) != 0; }
bool canMouseOver () const { return (_eventMask & EVENT_MOUSE_OVER) != 0; }
bool canMouseLeave () const { return (_eventMask & EVENT_MOUSE_LEAVE) != 0; }
bool canMouseDrag () const { return (_eventMask & EVENT_MOUSE_DRAG) != 0; }
bool canMousePush () const { return (_eventMask & EVENT_MOUSE_PUSH) != 0; }
bool canMouseRelease () const { return (_eventMask & EVENT_MOUSE_RELEASE) != 0; }
bool canMouseScroll () const { return (_eventMask & EVENT_MOUSE_SCROLL) != 0; }
bool canKeyDown () const { return (_eventMask & EVENT_KEY_DOWN) != 0; }
bool canKeyUp () const { return (_eventMask & EVENT_KEY_UP) != 0; }
private:
unsigned int _eventMask;
CallbackList _callbacks;
};
}
#endif
Referenced
osgWidget::Callback
Definition EventInterface:207
osgWidget::Callback::getData
void * getData()
Definition EventInterface:249
osgWidget::Callback::className
virtual const char * className() const
Definition EventInterface:212
osgWidget::Callback::Callback
Callback(bool(T::*function)(Event &), T *obj, EventType type, void *data=0)
Definition EventInterface:223
osgWidget::Callback::_data
void * _data
Definition EventInterface:259
osgWidget::Callback::operator()
virtual bool operator()(Event &ev)
Definition EventInterface:239
osgWidget::Callback::Callback
Callback()
Definition EventInterface:209
osgWidget::Callback::_callback
osg::ref_ptr< CallbackInterface > _callback
Definition EventInterface:262
osgWidget::Callback::Callback
Callback(EventType type, void *data=0)
Definition EventInterface:215
osgWidget::Callback::Callback
Callback(T *functor, EventType type, void *data=0)
Definition EventInterface:231
osgWidget::Callback::getData
const void * getData() const
Definition EventInterface:253
osgWidget::Callback::Callback
Callback(const Callback &rhs)
Definition EventInterface:210
osgWidget::Callback::_type
EventType _type
Definition EventInterface:258
osgWidget::Callback::~Callback
virtual ~Callback()
Definition EventInterface:237
osgWidget::Callback::getType
EventType getType() const
Definition EventInterface:245
osgWidget::EventInterface::addCallback
void addCallback(Callback *cb)
Definition EventInterface:322
osgWidget::EventInterface::getCallbacks
const CallbackList & getCallbacks() const
Definition EventInterface:320
osgWidget::EventInterface::keyUp
virtual bool keyUp(int, int, const WindowManager *)
Definition EventInterface:299
osgWidget::EventInterface::canMousePush
bool canMousePush() const
Definition EventInterface:400
osgWidget::EventInterface::CallbackList
std::list< osg::ref_ptr< Callback > > CallbackList
Definition EventInterface:317
osgWidget::EventInterface::addEventMask
void addEventMask(unsigned int mask)
Definition EventInterface:305
osgWidget::EventInterface::EventInterface
EventInterface(const EventInterface &ei)
Definition EventInterface:272
osgWidget::EventInterface::canKeyDown
bool canKeyDown() const
Definition EventInterface:404
osgWidget::EventInterface::EventInterface
EventInterface()
Definition EventInterface:270
osgWidget::EventInterface::canUnfocus
bool canUnfocus() const
Definition EventInterface:394
osgWidget::EventInterface::canMouseEnter
bool canMouseEnter() const
Definition EventInterface:396
osgWidget::EventInterface::getEventMask
unsigned int getEventMask() const
Definition EventInterface:313
osgWidget::EventInterface::mouseScroll
virtual bool mouseScroll(double, double, const WindowManager *)
Definition EventInterface:294
osgWidget::EventInterface::mouseOver
virtual bool mouseOver(double, double, const WindowManager *)
Definition EventInterface:289
osgWidget::EventInterface::unfocus
virtual bool unfocus(const WindowManager *)
Definition EventInterface:285
osgWidget::EventInterface::mousePush
virtual bool mousePush(double, double, const WindowManager *)
Definition EventInterface:292
osgWidget::EventInterface::setEventMask
void setEventMask(unsigned int mask)
Definition EventInterface:301
osgWidget::EventInterface::mouseEnter
virtual bool mouseEnter(double, double, const WindowManager *)
Definition EventInterface:288
osgWidget::EventInterface::canFocus
bool canFocus() const
Definition EventInterface:393
osgWidget::EventInterface::~EventInterface
virtual ~EventInterface()
Definition EventInterface:276
osgWidget::EventInterface::getCallbacks
CallbackList & getCallbacks()
Definition EventInterface:319
osgWidget::EventInterface::focus
virtual bool focus(const WindowManager *)
Definition EventInterface:284
osgWidget::EventInterface::mouseDrag
virtual bool mouseDrag(double, double, const WindowManager *)
Definition EventInterface:291
osgWidget::EventInterface::mouseRelease
virtual bool mouseRelease(double, double, const WindowManager *)
Definition EventInterface:293
osgWidget::EventInterface::canMouseScroll
bool canMouseScroll() const
Definition EventInterface:402
osgWidget::EventInterface::removeEventMask
void removeEventMask(unsigned int mask)
Definition EventInterface:309
osgWidget::EventInterface::canMouseLeave
bool canMouseLeave() const
Definition EventInterface:398
osgWidget::EventInterface::canMouseDrag
bool canMouseDrag() const
Definition EventInterface:399
osgWidget::EventInterface::canMouseRelease
bool canMouseRelease() const
Definition EventInterface:401
osgWidget::EventInterface::callCallbacks
bool callCallbacks(Event &ev)
Definition EventInterface:326
osgWidget::EventInterface::mouseLeave
virtual bool mouseLeave(double, double, const WindowManager *)
Definition EventInterface:290
osgWidget::EventInterface::keyDown
virtual bool keyDown(int, int, const WindowManager *)
Definition EventInterface:298
osgWidget::EventInterface::canMouseOver
bool canMouseOver() const
Definition EventInterface:397
osgWidget::EventInterface::canKeyUp
bool canKeyUp() const
Definition EventInterface:405
osgWidget::EventInterface::callMethodAndCallbacks
bool callMethodAndCallbacks(Event &ev)
Definition EventInterface:343
osgWidget::Event
Definition EventInterface:61
osgWidget::Event::makeType
Event & makeType(EventType _type)
Definition EventInterface:81
osgWidget::Event::x
double x
Definition EventInterface:64
osgWidget::Event::_data
void * _data
Definition EventInterface:147
osgWidget::Event::getWindowManager
WindowManager * getWindowManager()
Definition EventInterface:105
osgWidget::Event::_window
Window * _window
Definition EventInterface:145
osgWidget::Event::Event
Event(WindowManager *wm, EventType _type=EVENT_NONE)
Definition EventInterface:69
osgWidget::Event::makeMouse
Event & makeMouse(double _x, double _y, EventType _type=EVENT_NONE)
Definition EventInterface:87
osgWidget::Event::setData
void setData(void *data)
Definition EventInterface:135
osgWidget::Event::type
EventType type
Definition EventInterface:63
osgWidget::Event::keyMask
int keyMask
Definition EventInterface:67
osgWidget::Event::getData
void * getData()
Definition EventInterface:127
osgWidget::Event::Window
friend class Window
Definition EventInterface:142
osgWidget::Event::key
int key
Definition EventInterface:66
osgWidget::Event::getWidget
Widget * getWidget()
Definition EventInterface:119
osgWidget::Event::y
double y
Definition EventInterface:65
osgWidget::Event::WindowManager
friend class WindowManager
Definition EventInterface:141
osgWidget::Event::_widget
Widget * _widget
Definition EventInterface:146
osgWidget::Event::_wm
WindowManager * _wm
Definition EventInterface:144
osgWidget::Event::getData
const void * getData() const
Definition EventInterface:131
osgWidget::Event::getWindow
Window * getWindow()
Definition EventInterface:111
osgWidget::Event::getWindow
const Window * getWindow() const
Definition EventInterface:115
osgWidget::Event::makeKey
Event & makeKey(int _key, int _keyMask, EventType _type=EVENT_NONE)
Definition EventInterface:96
osgWidget::Event::getWindowManager
const WindowManager * getWindowManager() const
Definition EventInterface:107
osgWidget::Event::getWidget
const Widget * getWidget() const
Definition EventInterface:123
osgWidget::FunctionCallback
Definition EventInterface:192
osgWidget::FunctionCallback::FunctionCallback
FunctionCallback(T *callback)
Definition EventInterface:194
osgWidget::FunctionCallback::operator()
virtual bool operator()(Event &ev)
Definition EventInterface:198
osgWidget::FunctionCallback::_callback
T * _callback
Definition EventInterface:202
osgWidget::ObjectCallback
Definition EventInterface:172
osgWidget::ObjectCallback::ObjectCallbackType
bool(T::* ObjectCallbackType)(Event &)
Definition EventInterface:174
osgWidget::ObjectCallback::operator()
virtual bool operator()(Event &ev)
Definition EventInterface:180
osgWidget::ObjectCallback::ObjectCallback
ObjectCallback(ObjectCallbackType callback, T *obj)
Definition EventInterface:176
osgWidget::Widget
Definition osgWidget/Widget:35
osgWidget::WindowManager
Definition WindowManager:39
osg::Referenced
Definition Referenced:44
osg::Referenced::Referenced
Referenced()
osg::ref_ptr
Definition ref_ptr:32
osgWidget
Definition Box:21
osgWidget::EventType
EventType
Definition EventInterface:33
osgWidget::EVENT_UNFOCUS
@ EVENT_UNFOCUS
Definition EventInterface:36
osgWidget::EVENT_MOUSE_SCROLL
@ EVENT_MOUSE_SCROLL
Definition EventInterface:43
osgWidget::EVENT_MOUSE_ENTER
@ EVENT_MOUSE_ENTER
Definition EventInterface:37
osgWidget::EVENT_MOUSE_RELEASE
@ EVENT_MOUSE_RELEASE
Definition EventInterface:42
osgWidget::EVENT_MOUSE_OVER
@ EVENT_MOUSE_OVER
Definition EventInterface:38
osgWidget::EVENT_FOCUS
@ EVENT_FOCUS
Definition EventInterface:35
osgWidget::EVENT_ALL
@ EVENT_ALL
Definition EventInterface:46
osgWidget::EVENT_KEY_UP
@ EVENT_KEY_UP
Definition EventInterface:45
osgWidget::EVENT_NONE
@ EVENT_NONE
Definition EventInterface:34
osgWidget::EVENT_MOUSE_DRAG
@ EVENT_MOUSE_DRAG
Definition EventInterface:40
osgWidget::EVENT_MOUSE_LEAVE
@ EVENT_MOUSE_LEAVE
Definition EventInterface:39
osgWidget::EVENT_MOUSE_PUSH
@ EVENT_MOUSE_PUSH
Definition EventInterface:41
osgWidget::EVENT_KEY_DOWN
@ EVENT_KEY_DOWN
Definition EventInterface:44
osgWidget::EventMask
EventMask
Definition EventInterface:52
osgWidget::EVENT_MASK_MOUSE_CLICK
@ EVENT_MASK_MOUSE_CLICK
Definition EventInterface:55
osgWidget::EVENT_MASK_MOUSE_DRAG
@ EVENT_MASK_MOUSE_DRAG
Definition EventInterface:56
osgWidget::EVENT_MASK_FOCUS
@ EVENT_MASK_FOCUS
Definition EventInterface:53
osgWidget::EVENT_MASK_MOUSE_MOVE
@ EVENT_MASK_MOUSE_MOVE
Definition EventInterface:54
osgWidget::EVENT_MASK_KEY
@ EVENT_MASK_KEY
Definition EventInterface:57
osg
Definition AlphaFunc:19
observer_ptr
Export
OSGWIDGET_EXPORT
#define OSGWIDGET_EXPORT
Definition osgWidget/Export:42
ref_ptr
osgWidget::CallbackInterface
Definition EventInterface:161
osgWidget::CallbackInterface::className
virtual const char * className() const
Definition EventInterface:164
osgWidget::CallbackInterface::operator()
virtual bool operator()(Event &)=0
osgWidget::CallbackInterface::~CallbackInterface
virtual ~CallbackInterface()
Definition EventInterface:162