1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 19:53:54 +00:00

Reworked Input::Event, EventList filter

This commit is contained in:
Nicolay Korslund 2010-07-10 12:32:55 +02:00
parent 7583fc3f1b
commit 18dc065715
11 changed files with 136 additions and 22 deletions

View file

@ -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<Driver> DriverPtr;

View file

@ -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<Event> EventPtr;
}
}
#endif

View file

@ -0,0 +1,45 @@
#ifndef MANGLE_INPUT_EVENTLIST_H
#define MANGLE_INPUT_EVENTLIST_H
#include "../event.hpp"
#include <vector>
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<Filter> 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<Filter>::iterator it;
for(it=list.begin(); it!=list.end(); it++)
{
if(type & it->flags)
it->evt->event(type,index,p);
}
}
};
}
}
#endif

View file

@ -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;
}

View file

@ -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

View file

@ -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();

View file

@ -0,0 +1,45 @@
#include <iostream>
#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;
}

View file

@ -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

View file

@ -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!

View file

@ -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!

View file

@ -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.