event.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef EVENT_HEADER
  17. #define EVENT_HEADER
  18. class MtEvent
  19. {
  20. public:
  21. virtual ~MtEvent(){};
  22. //virtual MtEvent* clone(){ return new IEvent; }
  23. virtual const char* getType() const = 0;
  24. MtEvent* checkIs(const std::string &type)
  25. {
  26. if(type == getType())
  27. return this;
  28. return NULL;
  29. }
  30. };
  31. // An event with no parameters and customizable name
  32. class SimpleTriggerEvent: public MtEvent
  33. {
  34. const char *type;
  35. public:
  36. SimpleTriggerEvent(const char *type):
  37. type(type)
  38. {}
  39. const char* getType() const
  40. {return type;}
  41. };
  42. class MtEventReceiver
  43. {
  44. public:
  45. virtual ~MtEventReceiver(){};
  46. virtual void onEvent(MtEvent *e) = 0;
  47. };
  48. typedef void (*event_receive_func)(MtEvent *e, void *data);
  49. class MtEventManager
  50. {
  51. public:
  52. virtual ~MtEventManager(){};
  53. virtual void put(MtEvent *e) = 0;
  54. virtual void reg(const char *type, event_receive_func f, void *data) = 0;
  55. // If data==NULL, every occurence of f is deregistered.
  56. virtual void dereg(const char *type, event_receive_func f, void *data) = 0;
  57. virtual void reg(MtEventReceiver *r, const char *type) = 0;
  58. virtual void dereg(MtEventReceiver *r, const char *type) = 0;
  59. };
  60. #endif