forked from mirror/openmw-tes3mp
Reworked Input::Event, EventList filter
This commit is contained in:
parent
7583fc3f1b
commit
18dc065715
11 changed files with 136 additions and 22 deletions
|
@ -1,7 +1,6 @@
|
||||||
#ifndef MANGLE_INPUT_DRIVER_H
|
#ifndef MANGLE_INPUT_DRIVER_H
|
||||||
#define MANGLE_INPUT_DRIVER_H
|
#define MANGLE_INPUT_DRIVER_H
|
||||||
|
|
||||||
#include "../tools/shared_ptr.hpp"
|
|
||||||
#include "event.hpp"
|
#include "event.hpp"
|
||||||
|
|
||||||
namespace Mangle
|
namespace Mangle
|
||||||
|
@ -23,7 +22,7 @@ namespace Mangle
|
||||||
*/
|
*/
|
||||||
struct Driver
|
struct Driver
|
||||||
{
|
{
|
||||||
Driver() : event(NULL) {}
|
Driver() {}
|
||||||
virtual ~Driver() {}
|
virtual ~Driver() {}
|
||||||
|
|
||||||
/** Captures input and produces the relevant events from it. An
|
/** 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
|
and *p parameters will be specific to each driver and to
|
||||||
each input system.
|
each input system.
|
||||||
*/
|
*/
|
||||||
void setEvent(Event *evt)
|
void setEvent(EventPtr evt)
|
||||||
{ event = evt; }
|
{ event = evt; }
|
||||||
|
|
||||||
/** Instigate an event. Is used internally for all events, but
|
/** Instigate an event. Is used internally for all events, but
|
||||||
|
@ -61,7 +60,7 @@ namespace Mangle
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Holds the event callback set byt setEvent()
|
/// Holds the event callback set byt setEvent()
|
||||||
Event *event;
|
EventPtr event;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<Driver> DriverPtr;
|
typedef boost::shared_ptr<Driver> DriverPtr;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef MANGLE_INPUT_EVENT_H
|
#ifndef MANGLE_INPUT_EVENT_H
|
||||||
#define MANGLE_INPUT_EVENT_H
|
#define MANGLE_INPUT_EVENT_H
|
||||||
|
|
||||||
|
#include "../tools/shared_ptr.hpp"
|
||||||
|
|
||||||
namespace Mangle
|
namespace Mangle
|
||||||
{
|
{
|
||||||
namespace Input
|
namespace Input
|
||||||
|
@ -13,24 +15,29 @@ namespace Mangle
|
||||||
/// Event types
|
/// Event types
|
||||||
enum Type
|
enum Type
|
||||||
{
|
{
|
||||||
EV_Unknown = -1, // Unknown event type
|
EV_Unknown = 1, // Unknown event type
|
||||||
EV_KeyDown = 1, // Key, mouse or other button was pressed
|
EV_KeyDown = 2, // Keyboard button was pressed
|
||||||
EV_KeyUp = 2, // Key, mouse or other button was released
|
EV_KeyUp = 4, // Keyboard button was released
|
||||||
EV_MouseMove = 3, // Mouse movement (all axis movement?)
|
EV_MouseMove = 8, // Mouse movement
|
||||||
EV_Other = 4 // Other event
|
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
|
Called upon all events. The first parameter give the event
|
||||||
type, the second gives additional data (usually the local
|
type, the second gives additional data (usually the local
|
||||||
keysym as defined by the driver), and the pointer points to
|
keysym or button index as defined by the driver), and the
|
||||||
the full custom event structure provided by the driver (the
|
pointer points to the full custom event structure provided by
|
||||||
type may vary depending on the EventType, this is defined in
|
the driver (the type may vary depending on the EventType,
|
||||||
the Driver documentation.)
|
this is defined in the Driver documentation.)
|
||||||
*/
|
*/
|
||||||
virtual void event(Type type, int index, const void *p) = 0;
|
virtual void event(Type type, int index, const void *p) = 0;
|
||||||
virtual ~Event() {}
|
virtual ~Event() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef boost::shared_ptr<Event> EventPtr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
45
input/filters/eventlist.hpp
Normal file
45
input/filters/eventlist.hpp
Normal 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
|
|
@ -35,14 +35,14 @@ struct Mangle::Input::OISListener : OIS::KeyListener, OIS::MouseListener
|
||||||
{
|
{
|
||||||
// Mouse button events are handled as key events
|
// Mouse button events are handled as key events
|
||||||
// TODO: Translate mouse buttons into pseudo-keysyms
|
// TODO: Translate mouse buttons into pseudo-keysyms
|
||||||
drv.makeEvent(Event::EV_KeyDown, -1, &arg);
|
drv.makeEvent(Event::EV_MouseDown, id, &arg);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
|
bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
|
||||||
{
|
{
|
||||||
// TODO: ditto
|
// TODO: ditto
|
||||||
drv.makeEvent(Event::EV_KeyUp, -1, &arg);
|
drv.makeEvent(Event::EV_MouseUp, id, &arg);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
GCC=g++
|
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
|
sdl_driver_test: sdl_driver_test.cpp
|
||||||
$(GCC) $< ../servers/sdl_driver.cpp -o $@ -I/usr/include/SDL/ -lSDL
|
$(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
|
ois_driver_test: ois_driver_test.cpp
|
||||||
$(GCC) $< ../servers/ois_driver.cpp -o $@ -I/usr/local/include/OGRE/ -lOgreMain -lOIS -lboost_filesystem
|
$(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:
|
clean:
|
||||||
rm *_test
|
rm *_test
|
||||||
|
|
|
@ -12,11 +12,12 @@ struct MyCB : Event
|
||||||
{
|
{
|
||||||
cout << "got event: type=" << type << " index=" << i << endl;
|
cout << "got event: type=" << type << " index=" << i << endl;
|
||||||
}
|
}
|
||||||
} mycb;
|
};
|
||||||
|
|
||||||
void mainLoop(int argc, int quitKey)
|
void mainLoop(int argc, int quitKey)
|
||||||
{
|
{
|
||||||
cout << "Hold the Q key to quit:\n";
|
cout << "Hold the Q key to quit:\n";
|
||||||
input->setEvent(&mycb);
|
input->setEvent(EventPtr(new MyCB));
|
||||||
while(!input->isDown(quitKey))
|
while(!input->isDown(quitKey))
|
||||||
{
|
{
|
||||||
input->capture();
|
input->capture();
|
||||||
|
|
45
input/tests/evtlist_test.cpp
Normal file
45
input/tests/evtlist_test.cpp
Normal 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;
|
||||||
|
}
|
12
input/tests/output/evtlist_test.out
Normal file
12
input/tests/output/evtlist_test.out
Normal 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
|
|
@ -1,5 +1,5 @@
|
||||||
Hold the Q key to quit:
|
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
|
You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly
|
||||||
|
|
||||||
Bye bye!
|
Bye bye!
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
Hold the Q key to quit:
|
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
|
You are running in script mode, aborting. Run this test with a parameter (any at all) to test the input loop properly
|
||||||
|
|
||||||
Bye bye!
|
Bye bye!
|
||||||
|
|
|
@ -34,10 +34,12 @@ namespace Mangle
|
||||||
/// Set the window title
|
/// Set the window title
|
||||||
void setWindowTitle(const std::string &title) { setWindowTitle(title,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;
|
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;
|
virtual Sprite* loadImage(const void* data, size_t size) = 0;
|
||||||
|
|
||||||
/** @brief Set gamma value for all colors.
|
/** @brief Set gamma value for all colors.
|
||||||
|
|
Loading…
Reference in a new issue