Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Events/EventDispatcher.cpp@0:0a590815d51c, 2016-03-25 (annotated)
- Committer:
- duncanFrance
- Date:
- Fri Mar 25 13:47:04 2016 +0000
- Revision:
- 0:0a590815d51c
- Child:
- 12:63db16fea709
Added EventSource interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 0:0a590815d51c | 1 | #include "EventDispatcher.h" |
duncanFrance | 0:0a590815d51c | 2 | |
duncanFrance | 0:0a590815d51c | 3 | EventDispatcher::EventDispatcher() { |
duncanFrance | 0:0a590815d51c | 4 | } |
duncanFrance | 0:0a590815d51c | 5 | |
duncanFrance | 0:0a590815d51c | 6 | |
duncanFrance | 0:0a590815d51c | 7 | |
duncanFrance | 0:0a590815d51c | 8 | void EventDispatcher::attachListener(EventListener* l) { |
duncanFrance | 0:0a590815d51c | 9 | if(_listeners == NULL) { |
duncanFrance | 0:0a590815d51c | 10 | _listeners = new EventListenerWrapper(l); |
duncanFrance | 0:0a590815d51c | 11 | } else { |
duncanFrance | 0:0a590815d51c | 12 | EventListenerWrapper* w = new EventListenerWrapper(l); |
duncanFrance | 0:0a590815d51c | 13 | EventListenerWrapper* p = _listeners; |
duncanFrance | 0:0a590815d51c | 14 | while(p->next != NULL) { |
duncanFrance | 0:0a590815d51c | 15 | p = p->next; |
duncanFrance | 0:0a590815d51c | 16 | } |
duncanFrance | 0:0a590815d51c | 17 | p->next = w; |
duncanFrance | 0:0a590815d51c | 18 | w->prev = p; |
duncanFrance | 0:0a590815d51c | 19 | } |
duncanFrance | 0:0a590815d51c | 20 | } |
duncanFrance | 0:0a590815d51c | 21 | |
duncanFrance | 0:0a590815d51c | 22 | void EventDispatcher::detachListener(EventListener* l) { |
duncanFrance | 0:0a590815d51c | 23 | |
duncanFrance | 0:0a590815d51c | 24 | EventListenerWrapper *p = _listeners; |
duncanFrance | 0:0a590815d51c | 25 | |
duncanFrance | 0:0a590815d51c | 26 | if(p == NULL) { |
duncanFrance | 0:0a590815d51c | 27 | return; |
duncanFrance | 0:0a590815d51c | 28 | } |
duncanFrance | 0:0a590815d51c | 29 | |
duncanFrance | 0:0a590815d51c | 30 | while(p->listener != l) { |
duncanFrance | 0:0a590815d51c | 31 | if(p->next == NULL) { |
duncanFrance | 0:0a590815d51c | 32 | return; |
duncanFrance | 0:0a590815d51c | 33 | } |
duncanFrance | 0:0a590815d51c | 34 | p = p->next; |
duncanFrance | 0:0a590815d51c | 35 | } |
duncanFrance | 0:0a590815d51c | 36 | |
duncanFrance | 0:0a590815d51c | 37 | // Found the listener |
duncanFrance | 0:0a590815d51c | 38 | // Is there only one in the list? |
duncanFrance | 0:0a590815d51c | 39 | if(p->prev == NULL && p->next == NULL) { |
duncanFrance | 0:0a590815d51c | 40 | _listeners = NULL; |
duncanFrance | 0:0a590815d51c | 41 | } else if(p->prev == NULL) { |
duncanFrance | 0:0a590815d51c | 42 | // First in the list. Move everything down |
duncanFrance | 0:0a590815d51c | 43 | _listeners = p->next; |
duncanFrance | 0:0a590815d51c | 44 | _listeners->prev = NULL; |
duncanFrance | 0:0a590815d51c | 45 | } else if(p->next == NULL) { |
duncanFrance | 0:0a590815d51c | 46 | p->prev->next = NULL; |
duncanFrance | 0:0a590815d51c | 47 | } else { |
duncanFrance | 0:0a590815d51c | 48 | // Middle of the list |
duncanFrance | 0:0a590815d51c | 49 | p->prev->next = p->next; |
duncanFrance | 0:0a590815d51c | 50 | p->next->prev = p->prev; |
duncanFrance | 0:0a590815d51c | 51 | } |
duncanFrance | 0:0a590815d51c | 52 | |
duncanFrance | 0:0a590815d51c | 53 | delete p; |
duncanFrance | 0:0a590815d51c | 54 | } |
duncanFrance | 0:0a590815d51c | 55 | |
duncanFrance | 0:0a590815d51c | 56 | void EventDispatcher::dispatchEvent(Event e) { |
duncanFrance | 0:0a590815d51c | 57 | EventListenerWrapper* p = _listeners; |
duncanFrance | 0:0a590815d51c | 58 | while(p != NULL) { |
duncanFrance | 0:0a590815d51c | 59 | if(p->listener->isEventTarget(e)) { |
duncanFrance | 0:0a590815d51c | 60 | p->listener->handleEvent(e); |
duncanFrance | 0:0a590815d51c | 61 | } |
duncanFrance | 0:0a590815d51c | 62 | |
duncanFrance | 0:0a590815d51c | 63 | p = p->next; |
duncanFrance | 0:0a590815d51c | 64 | } |
duncanFrance | 0:0a590815d51c | 65 | } |