PolyEventDispatcher.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyEventHandler.h"
  23. #include <vector>
  24. namespace Polycode {
  25. class Event;
  26. typedef struct {
  27. EventHandler *handler;
  28. int eventCode;
  29. } EventEntry;
  30. /**
  31. * Can dispatch events. The event dispatcher is base class which allows its subclass to dispatch custom events which EventHandler subclasses can then listen to. EventDispatcher and EventHandler are the two main classes in the Polycode event system. If you are familiar with ActionScript3's event system, you will find this to be very similar, except that it uses integers for event codes for speed, rather than strings.
  32. */
  33. class _PolyExport EventDispatcher : public EventHandler {
  34. public:
  35. /**
  36. * Default constructor
  37. */
  38. EventDispatcher();
  39. virtual ~EventDispatcher();
  40. /**
  41. * Removes all current event handlers from this dispatcher.
  42. */
  43. void removeAllHandlers();
  44. /**
  45. * Removes all current event handlers from this dispatcher for a specific event listener.
  46. * @param Event listener to remove handlers for.
  47. */
  48. void removeAllHandlersForListener(EventHandler *handler);
  49. /**
  50. * Adds an event listener for a specific event code. Once a listener is registered for a specific event code, that listener will start getting event callbacks into the handleEvent() method.
  51. * @param handler The event handler to add as a listener
  52. * @param eventCode The requested event code to listen to.
  53. * @see EventHandler
  54. */
  55. void addEventListener(EventHandler *handler, int eventCode);
  56. /**
  57. * Adds an event listener for specified event code if it hasn't already been added, otherwise does nothing.
  58. * @param handler The event handler to add as a listener
  59. * @param eventCode The requested event code to listen to.
  60. */
  61. void addEventListenerUnique(EventHandler *handler, int eventCode);
  62. /**
  63. * Returns true if this event dispatcher is registered with the specified EventHandler with the specified event code.
  64. * @param handler EventHandler to check.
  65. * @param eventCode The event code to check.
  66. */
  67. bool hasEventListener(EventHandler *handler, int eventCode);
  68. /**
  69. * Removes a listener for a specific handler and event code.
  70. * @param handler The event handler to remove as a listener
  71. * @param eventCode The requested event code to remove listener for.
  72. * @see EventHandler
  73. */
  74. void removeEventListener(EventHandler *handler, int eventCode);
  75. void __dispatchEvent(Event *event, int eventCode);
  76. /**
  77. * Dispatches an event to all handlers listening for the event code specified.
  78. * @param event Event class to dispatch to listeners. You can subclass the Event class to send data in your events.
  79. * @param eventCode The event code to dispatch the event for.
  80. * @see Event
  81. * @see EventHandler
  82. */
  83. virtual void dispatchEvent(Event *event, int eventCode);
  84. virtual void dispatchEventNoDelete(Event *event, int eventCode);
  85. protected:
  86. std::vector<EventEntry> handlerEntries;
  87. };
  88. }