2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
|
2008-07-26 04:04:41 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2008-07-26 04:04:41 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2008-07-26 04:04:41 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-07-26 04:04:41 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
// Code by: Jeremy Moles (cubicool) 2007-2008
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
#ifndef OSGWIDGET_EVENT_INTERFACE
|
|
|
|
#define OSGWIDGET_EVENT_INTERFACE
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <osg/ref_ptr>
|
2008-12-17 05:16:06 +08:00
|
|
|
#include <osg/observer_ptr>
|
2008-07-16 01:21:25 +08:00
|
|
|
#include <osg/Referenced>
|
|
|
|
|
|
|
|
#include <osgWidget/Export>
|
|
|
|
|
|
|
|
namespace osgWidget {
|
|
|
|
|
|
|
|
class WindowManager;
|
|
|
|
class Window;
|
|
|
|
class Widget;
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
enum EventType
|
2008-07-26 03:57:17 +08:00
|
|
|
{
|
|
|
|
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
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Helpful wrapper around using the raw types, since it often doesn't make sense to
|
|
|
|
// use some without the others.
|
2008-07-26 04:50:42 +08:00
|
|
|
enum EventMask
|
2008-07-26 03:57:17 +08:00
|
|
|
{
|
|
|
|
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
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
class OSGWIDGET_EXPORT Event
|
|
|
|
{
|
|
|
|
public:
|
2008-07-26 04:50:42 +08:00
|
|
|
EventType type;
|
2008-07-26 03:57:17 +08:00
|
|
|
double x;
|
|
|
|
double y;
|
|
|
|
int key;
|
|
|
|
int keyMask;
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
Event(WindowManager* wm, EventType _type = EVENT_NONE):
|
2008-07-26 03:57:17 +08:00
|
|
|
type (_type),
|
|
|
|
x (0.0f),
|
|
|
|
y (0.0f),
|
|
|
|
key (-1),
|
2008-11-28 22:35:33 +08:00
|
|
|
keyMask (-1),
|
|
|
|
_wm (wm),
|
|
|
|
_window (0),
|
|
|
|
_widget (0),
|
|
|
|
_data (0) {
|
2008-07-26 03:57:17 +08:00
|
|
|
}
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
Event& makeType(EventType _type) {
|
2008-07-26 03:57:17 +08:00
|
|
|
if(_type != EVENT_NONE) type = _type;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
Event& makeMouse(double _x, double _y, EventType _type = EVENT_NONE) {
|
2008-07-26 03:57:17 +08:00
|
|
|
x = _x;
|
|
|
|
y = _y;
|
|
|
|
|
|
|
|
if(_type != EVENT_NONE) type = _type;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
Event& makeKey(int _key, int _keyMask, EventType _type = EVENT_NONE) {
|
2008-07-26 03:57:17 +08:00
|
|
|
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;
|
|
|
|
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The Callback interface was inspired by the CEGUI project:
|
|
|
|
//
|
2008-07-26 03:57:17 +08:00
|
|
|
// http://www.cegui.org.uk/wiki/index.php/Main_Page
|
2008-07-16 01:21:25 +08:00
|
|
|
//
|
|
|
|
// 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.
|
2008-07-26 03:57:17 +08:00
|
|
|
struct CallbackInterface: public osg::Referenced
|
|
|
|
{
|
|
|
|
virtual ~CallbackInterface() {}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2010-02-19 05:17:36 +08:00
|
|
|
virtual const char* className() const { return "osgWidget::CallbackInterface"; }
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
virtual bool operator()(Event&) = 0;
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The object that facilitates a class method as a callback.
|
|
|
|
template<typename T>
|
2008-07-26 03:57:17 +08:00
|
|
|
class ObjectCallback: public CallbackInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef bool (T::*ObjectCallbackType)(Event&);
|
|
|
|
|
|
|
|
ObjectCallback(ObjectCallbackType callback, T* obj):
|
2009-01-07 18:32:59 +08:00
|
|
|
_object (obj),
|
|
|
|
_callback (callback) {}
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
virtual bool operator()(Event& ev) {
|
|
|
|
return (_object->*_callback)(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* _object;
|
2009-01-07 18:32:59 +08:00
|
|
|
ObjectCallbackType _callback;
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The object that facilitates general functions as callbacks.
|
|
|
|
template<typename T>
|
2008-07-26 03:57:17 +08:00
|
|
|
class FunctionCallback: public CallbackInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FunctionCallback(T* callback):
|
|
|
|
_callback(callback) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool operator()(Event& ev) {
|
|
|
|
return (*_callback)(ev);
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
T* _callback;
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The highlevel functor.
|
2008-11-28 22:35:33 +08:00
|
|
|
class OSGWIDGET_EXPORT Callback: public osg::Referenced
|
2008-07-26 03:57:17 +08:00
|
|
|
{
|
|
|
|
public:
|
2008-11-28 22:35:33 +08:00
|
|
|
Callback(): _type(EVENT_NONE), _data(0), _callback(0) {}
|
|
|
|
Callback(const Callback& rhs): osg::Referenced(rhs), _type(rhs._type), _data(rhs._data), _callback(rhs._callback) {}
|
|
|
|
|
2010-02-19 05:17:36 +08:00
|
|
|
virtual const char* className() const { return "osgWidget::Callback"; }
|
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
// The more traditional style of OSG Callbacks.
|
|
|
|
Callback(EventType type, void* data=0):
|
|
|
|
_type (type),
|
|
|
|
_data (data),
|
|
|
|
_callback (0) {
|
|
|
|
}
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
// Creates a Callback that is bound to a member function.
|
|
|
|
template<typename T>
|
2008-07-26 04:50:42 +08:00
|
|
|
Callback(bool (T::*function)(Event&), T* obj, EventType type, void* data=0):
|
2008-07-26 03:57:17 +08:00
|
|
|
_type (type),
|
|
|
|
_data (data),
|
|
|
|
_callback (new ObjectCallback<T>(function, obj)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a Callback that is bound to a functor pointer.
|
|
|
|
template<typename T>
|
2008-07-26 04:50:42 +08:00
|
|
|
Callback(T* functor, EventType type, void* data=0):
|
2008-07-26 03:57:17 +08:00
|
|
|
_type (type),
|
|
|
|
_data (data),
|
|
|
|
_callback (new FunctionCallback<T>(functor)) {
|
|
|
|
}
|
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
virtual ~Callback() {}
|
|
|
|
|
|
|
|
virtual bool operator()(Event& ev) {
|
|
|
|
if(!_callback) return false;
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return (*_callback)(ev);
|
|
|
|
}
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
EventType getType() const {
|
2008-07-26 03:57:17 +08:00
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* getData() {
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* getData() const {
|
|
|
|
return _data;
|
|
|
|
}
|
2008-11-28 22:35:33 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
protected:
|
2008-07-26 04:50:42 +08:00
|
|
|
EventType _type;
|
2008-11-28 22:35:33 +08:00
|
|
|
void* _data;
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
// We use a ref_ptr here so that we don't have to worry about memory.
|
|
|
|
osg::ref_ptr<CallbackInterface> _callback;
|
|
|
|
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
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 seperate events here.
|
2009-11-19 19:30:22 +08:00
|
|
|
virtual bool focus (const WindowManager*) { return false; }
|
|
|
|
virtual bool unfocus (const WindowManager*) { return false; }
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
// Mouse events, pretty self-explanatory.
|
2009-11-19 19:30:22 +08:00
|
|
|
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; }
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
// These functions pass the osgGA::GUIEventAdapter::KeySymbol and KeyModMask and,
|
|
|
|
// as above, the WindowManager.
|
2009-11-19 19:30:22 +08:00
|
|
|
virtual bool keyDown (int, int, const WindowManager*) { return false; }
|
|
|
|
virtual bool keyUp (int, int, const WindowManager*) { return false; }
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
void addCallback(Callback* cb) {
|
2008-07-26 03:57:17 +08:00
|
|
|
_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;
|
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
if(i->get()->getType() & ev.type) {
|
|
|
|
ev.setData(i->get()->getData());
|
2008-07-26 03:57:17 +08:00
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
if((*i->get())(ev)) return true;
|
2008-07-26 03:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2013-04-16 17:26:40 +08:00
|
|
|
typedef std::list<osg::ref_ptr<Callback> > CallbackList;
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
unsigned int _eventMask;
|
|
|
|
CallbackList _callbacks;
|
|
|
|
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|