From 18dc065715c12fd8793f8793c7bf834619527399 Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Sat, 10 Jul 2010 12:32:55 +0200 Subject: [PATCH] Reworked Input::Event, EventList filter --- input/driver.hpp | 7 ++-- input/event.hpp | 25 ++++++++------ input/filters/eventlist.hpp | 45 ++++++++++++++++++++++++++ input/servers/ois_driver.cpp | 4 +-- input/tests/Makefile | 5 ++- input/tests/common.cpp | 5 +-- input/tests/evtlist_test.cpp | 45 ++++++++++++++++++++++++++ input/tests/output/evtlist_test.out | 12 +++++++ input/tests/output/ois_driver_test.out | 2 +- input/tests/output/sdl_driver_test.out | 2 +- rend2d/driver.hpp | 6 ++-- 11 files changed, 136 insertions(+), 22 deletions(-) create mode 100644 input/filters/eventlist.hpp create mode 100644 input/tests/evtlist_test.cpp create mode 100644 input/tests/output/evtlist_test.out diff --git a/input/driver.hpp b/input/driver.hpp index 1c1e950478..f4ba159c52 100644 --- a/input/driver.hpp +++ b/input/driver.hpp @@ -1,7 +1,6 @@ #ifndef MANGLE_INPUT_DRIVER_H #define MANGLE_INPUT_DRIVER_H -#include "../tools/shared_ptr.hpp" #include "event.hpp" namespace Mangle @@ -23,7 +22,7 @@ namespace Mangle */ struct Driver { - Driver() : event(NULL) {} + Driver() {} virtual ~Driver() {} /** Captures input and produces the relevant events from it. An @@ -46,7 +45,7 @@ namespace Mangle and *p parameters will be specific to each driver and to each input system. */ - void setEvent(Event *evt) + void setEvent(EventPtr evt) { event = evt; } /** Instigate an event. Is used internally for all events, but @@ -61,7 +60,7 @@ namespace Mangle private: /// Holds the event callback set byt setEvent() - Event *event; + EventPtr event; }; typedef boost::shared_ptr DriverPtr; diff --git a/input/event.hpp b/input/event.hpp index 5948653bd9..f7ee7e58b7 100644 --- a/input/event.hpp +++ b/input/event.hpp @@ -1,6 +1,8 @@ #ifndef MANGLE_INPUT_EVENT_H #define MANGLE_INPUT_EVENT_H +#include "../tools/shared_ptr.hpp" + namespace Mangle { namespace Input @@ -13,24 +15,29 @@ namespace Mangle /// Event types enum Type { - EV_Unknown = -1, // Unknown event type - EV_KeyDown = 1, // Key, mouse or other button was pressed - EV_KeyUp = 2, // Key, mouse or other button was released - EV_MouseMove = 3, // Mouse movement (all axis movement?) - EV_Other = 4 // Other event + EV_Unknown = 1, // Unknown event type + EV_KeyDown = 2, // Keyboard button was pressed + EV_KeyUp = 4, // Keyboard button was released + EV_MouseMove = 8, // Mouse movement + EV_MouseDown = 16, // Mouse button pressed + EV_MouseUp = 32, // Mouse button released + + EV_ALL = 63 // All events }; /** Called upon all events. The first parameter give the event type, the second gives additional data (usually the local - keysym as defined by the driver), and the pointer points to - the full custom event structure provided by the driver (the - type may vary depending on the EventType, this is defined in - the Driver documentation.) + keysym or button index as defined by the driver), and the + pointer points to the full custom event structure provided by + the driver (the type may vary depending on the EventType, + this is defined in the Driver documentation.) */ virtual void event(Type type, int index, const void *p) = 0; virtual ~Event() {} }; + + typedef boost::shared_ptr EventPtr; } } #endif diff --git a/input/filters/eventlist.hpp b/input/filters/eventlist.hpp new file mode 100644 index 0000000000..d7703fbc4f --- /dev/null +++ b/input/filters/eventlist.hpp @@ -0,0 +1,45 @@ +#ifndef MANGLE_INPUT_EVENTLIST_H +#define MANGLE_INPUT_EVENTLIST_H + +#include "../event.hpp" +#include + +namespace Mangle +{ + namespace Input + { + /** And Event handler that distributes each event to a list of + other handlers. Supports filtering events by their Type + parameter. + */ + struct EventList : Event + { + struct Filter + { + EventPtr evt; + int flags; + }; + std::vector list; + + void add(EventPtr e, int flags = EV_ALL) + { + Filter f; + f.evt = e; + f.flags = flags; + list.push_back(f); + } + + virtual void event(Type type, int index, const void *p) + { + std::vector::iterator it; + + for(it=list.begin(); it!=list.end(); it++) + { + if(type & it->flags) + it->evt->event(type,index,p); + } + } + }; + } +} +#endif diff --git a/input/servers/ois_driver.cpp b/input/servers/ois_driver.cpp index eabe9739d0..2071b91ea6 100644 --- a/input/servers/ois_driver.cpp +++ b/input/servers/ois_driver.cpp @@ -35,14 +35,14 @@ struct Mangle::Input::OISListener : OIS::KeyListener, OIS::MouseListener { // Mouse button events are handled as key events // TODO: Translate mouse buttons into pseudo-keysyms - drv.makeEvent(Event::EV_KeyDown, -1, &arg); + drv.makeEvent(Event::EV_MouseDown, id, &arg); return true; } bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) { // TODO: ditto - drv.makeEvent(Event::EV_KeyUp, -1, &arg); + drv.makeEvent(Event::EV_MouseUp, id, &arg); return true; } diff --git a/input/tests/Makefile b/input/tests/Makefile index b7d7b5b4ed..8b7663dd57 100644 --- a/input/tests/Makefile +++ b/input/tests/Makefile @@ -1,6 +1,6 @@ GCC=g++ -all: sdl_driver_test ois_driver_test +all: sdl_driver_test ois_driver_test evtlist_test sdl_driver_test: sdl_driver_test.cpp $(GCC) $< ../servers/sdl_driver.cpp -o $@ -I/usr/include/SDL/ -lSDL @@ -8,5 +8,8 @@ sdl_driver_test: sdl_driver_test.cpp ois_driver_test: ois_driver_test.cpp $(GCC) $< ../servers/ois_driver.cpp -o $@ -I/usr/local/include/OGRE/ -lOgreMain -lOIS -lboost_filesystem +evtlist_test: evtlist_test.cpp ../filters/eventlist.hpp ../event.hpp + $(GCC) $< -o $@ + clean: rm *_test diff --git a/input/tests/common.cpp b/input/tests/common.cpp index 47a0c53de0..0c7c76466b 100644 --- a/input/tests/common.cpp +++ b/input/tests/common.cpp @@ -12,11 +12,12 @@ struct MyCB : Event { cout << "got event: type=" << type << " index=" << i << endl; } -} mycb; +}; + void mainLoop(int argc, int quitKey) { cout << "Hold the Q key to quit:\n"; - input->setEvent(&mycb); + input->setEvent(EventPtr(new MyCB)); while(!input->isDown(quitKey)) { input->capture(); diff --git a/input/tests/evtlist_test.cpp b/input/tests/evtlist_test.cpp new file mode 100644 index 0000000000..fbd980cbd9 --- /dev/null +++ b/input/tests/evtlist_test.cpp @@ -0,0 +1,45 @@ +#include +#include "../filters/eventlist.hpp" + +using namespace std; +using namespace Mangle::Input; + +struct MyEvent : Event +{ + int ii; + MyEvent(int i) : ii(i) {} + + void event(Event::Type type, int i, const void *p) + { + cout << " #" << ii << " got event: type=" << type << " index=" << i << endl; + } +}; + +EventList lst; + +int iii=1; +void make(int flags) +{ + lst.add(EventPtr(new MyEvent(iii++)), flags); +} + +void send(Event::Type type) +{ + cout << "Sending type " << type << endl; + lst.event(type,0,NULL); +} + +int main() +{ + make(Event::EV_ALL); + make(Event::EV_KeyDown); + make(Event::EV_KeyUp | Event::EV_MouseDown); + + send(Event::EV_Unknown); + send(Event::EV_KeyDown); + send(Event::EV_KeyUp); + send(Event::EV_MouseDown); + + cout << "Enough of that\n"; + return 0; +} diff --git a/input/tests/output/evtlist_test.out b/input/tests/output/evtlist_test.out new file mode 100644 index 0000000000..180dcc58a8 --- /dev/null +++ b/input/tests/output/evtlist_test.out @@ -0,0 +1,12 @@ +Sending type 1 + #1 got event: type=1 index=0 +Sending type 2 + #1 got event: type=2 index=0 + #2 got event: type=2 index=0 +Sending type 4 + #1 got event: type=4 index=0 + #3 got event: type=4 index=0 +Sending type 16 + #1 got event: type=16 index=0 + #3 got event: type=16 index=0 +Enough of that diff --git a/input/tests/output/ois_driver_test.out b/input/tests/output/ois_driver_test.out index eb9c29bf47..7d273fd46d 100644 --- a/input/tests/output/ois_driver_test.out +++ b/input/tests/output/ois_driver_test.out @@ -1,5 +1,5 @@ Hold the Q key to quit: -got event: type=3 index=-1 +got event: type=8 index=-1 You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly Bye bye! diff --git a/input/tests/output/sdl_driver_test.out b/input/tests/output/sdl_driver_test.out index 54e941fb66..2df2e4014e 100644 --- a/input/tests/output/sdl_driver_test.out +++ b/input/tests/output/sdl_driver_test.out @@ -1,5 +1,5 @@ Hold the Q key to quit: -got event: type=-1 index=-1 +got event: type=1 index=-1 You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly Bye bye! diff --git a/rend2d/driver.hpp b/rend2d/driver.hpp index 7366e04625..08a15b0aeb 100644 --- a/rend2d/driver.hpp +++ b/rend2d/driver.hpp @@ -34,10 +34,12 @@ namespace Mangle /// Set the window title void setWindowTitle(const std::string &title) { setWindowTitle(title,title); } - /// Load sprite from an image file + /// Load sprite from an image file. Thows an exception on + /// failure. virtual Sprite* loadImage(const std::string &file) = 0; - /// Load a sprite from an image file stored in memory. + /// Load a sprite from an image file stored in memory. Throws + /// exception on failure. virtual Sprite* loadImage(const void* data, size_t size) = 0; /** @brief Set gamma value for all colors.