IODevices

IODevices Module gives your game an access to all kinds of devices via unified observer pattern known from e.g. Java.

You simply create your own listeners that inherit from IODevices base classes. When any device-related event occur, IODevices will inform your listeners so you can take further steps.

So far, IODevices gives you support for those devices:

General assumptions

  • If you return true during processing an event, it will mean "stop propagation" - the event won't be delivered to rest of listeners (with lower priorities).

Using the listeners

First of all, you have to include the IODevices Module and it's Implementation (just like with any other Module).

Now you have to create an IOGether with IODevicesModule::createIOGether() method.

Then you can use that IOGether to assign the listeners for different devices (like keyboard or mouse) with void add_X_Listener(_X_Listener * listener, bool asHighestPriority) method, where _X_ is the name of the listener. The listeners with higher priorities will get the event faster (and can interrupt processing of it).

Your listener have to inherit from IODevices base listeners (like KeyListener for keyboard listeners).

When device-related event occur, the assigned listener with highest priority will receive it. For example, when you press a key, KeyListener::characterTyped(char characterUTF8) will be called. If you will return true (which means "stop propagation"), the event won't reach rest of listeners. Otherwise, the event will be passed to next listener and so on.

The sample code of the listener:

class MyKeyListener : public IODevices::KeyListener{
public:
bool characterTyped(char characterUTF8) override {
MessageBox(NULL, L"Key pressed", L"Key pressed", NULL);
return false; //event will propagate
}
bool functionKeyPressed(unsigned char number) override {
return false;
}
bool capsLockStateChanged(bool on) override {
return false;
}
};

Which you can assing that way:

IODevices::IOGether * ioGether = IODevicesModule::createIOGether();
//...
ioGether->addKeyListener(new MyKeyListener(), true);
ioGether->addKeyListener(...); //another key listener
ioGether->addMouseListener(...);

From now on, when you press a key, the accurate listeners will be called to react to that event.

TODO

  • Should the processing of events use true or false for "stop propagation"? If it will change in the future, make changes also to IOGether page (there's an info that true stands for "stop propagation").