EventInterface 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/* -*-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 seperate 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;
        }

        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:
        typedef std::list<osg::observer_ptr<Callback> > CallbackList;

        unsigned int _eventMask;
        CallbackList _callbacks;

};

}

#endif