User Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

annuserspaceeventlauncher [2017/01/29 18:47]
ybalrid created
annuserspaceeventlauncher [2017/03/07 01:52] (current)
ybalrid
Line 5: Line 5:
 Every [[AnnUserSpaceSubSystem]] inherit from this class, and provide you as interface a protected final override of the dispatchEvent() method. Every [[AnnUserSpaceSubSystem]] inherit from this class, and provide you as interface a protected final override of the dispatchEvent() method.
  
 +===== Example =====
 +
 +Here's a practical example on how to setup a custom event, a listener for it, and a subsystem that sends this event each few seconds.
 +
 +Here's the class definition for the event, note the string passed to the parent AnnUserSpaceEvent constructor. This is what will permit you to identify the type of the event.
 +
 +<code cpp>
 +class TextMessageEvent : public AnnUserSpaceEvent
 +{
 +public:
 + TextMessageEvent(string message) : AnnUserSpaceEvent("​TextMessage"​),​
 + enclosedMessage(message)
 + {
 + }
 +
 + ~TextMessageEvent()
 + {
 + //This is just to check that everything is in order
 + //​AnnDebug() << "event destructed!";​
 + }
 +
 + string getMessage() const { return enclosedMessage;​ };
 +
 +private:
 + string enclosedMessage;​
 +};
 +</​code>​
 +
 +
 +Here's a listener that will listen to this even via overriding the "​EventFromUserSubsystem"​ method. Not that we're using an hashed version of the same "​identifier string"​ as above.
 +<code cpp>
 +class SomeEventListener : LISTENER
 +{
 +public:
 + SomeEventListener() : constructListener(),​
 + hashTypeCheckTextEvent(AnnGetStringUtility()->​hash("​TextMessage"​))
 + {
 + }
 +
 + void EventFromUserSubsystem(AnnUserSpaceEvent&​ e, AnnUserSpaceEventLauncher* origin) override
 + {
 + if (e.getType() == hashTypeCheckTextEvent)
 + AnnDebug() << "​SomeEventListener got the TextMessageEvent \""​
 + << dynamic_cast<​TextMessageEvent&>​(e).getMessage()
 + << "​\"​ from " << origin;
 + }
 +
 +private:
 + const AnnUserSpaceEvent::​AnnUserSpaceEventTypeHash hashTypeCheckTextEvent;​
 +};
 +</​code>​
 +
 +
 +Here's a user space subsystem that send this custom event every few seconds:
 +
 +<code cpp>
 +class SomeSubSystem : public AnnUserSubSystem
 +{
 +public:
 + SomeSubSystem(double updateRate = 3) : AnnUserSubSystem("​Useless Subsystem"​),​
 + now(0),
 + last(0),
 + delay(updateRate)
 + {
 + }
 +
 + void update() override
 + {
 + now = AnnGetEngine()->​getTimeFromStartupSeconds();​
 +
 + if (now - last > delay)
 + {
 + static unsigned int count = 0;
 + last = now;
 + dispatchEvent(make_shared<​TextMessageEvent>​("​Useless message! " + to_string(++count)));​
 + }
 + }
 +
 + bool needUpdate() override
 + {
 + return true;
 + }
 +
 +private:
 + double now, last, delay;
 +};
 +</​code>​