mtevent.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #pragma once
  17. #include "irrlichttypes.h"
  18. class MtEvent
  19. {
  20. public:
  21. enum Type : u8
  22. {
  23. VIEW_BOBBING_STEP = 0,
  24. CAMERA_PUNCH_LEFT,
  25. CAMERA_PUNCH_RIGHT,
  26. PLAYER_FALLING_DAMAGE,
  27. PLAYER_DAMAGE,
  28. NODE_DUG,
  29. PLAYER_JUMP,
  30. PLAYER_REGAIN_GROUND,
  31. TYPE_MAX,
  32. };
  33. virtual ~MtEvent() = default;
  34. virtual Type getType() const = 0;
  35. };
  36. // An event with no parameters and customizable name
  37. class SimpleTriggerEvent : public MtEvent
  38. {
  39. Type type;
  40. public:
  41. SimpleTriggerEvent(Type type) : type(type) {}
  42. Type getType() const override { return type; }
  43. };
  44. class MtEventReceiver
  45. {
  46. public:
  47. virtual ~MtEventReceiver() = default;
  48. virtual void onEvent(MtEvent *e) = 0;
  49. };
  50. typedef void (*event_receive_func)(MtEvent *e, void *data);
  51. class MtEventManager
  52. {
  53. public:
  54. virtual ~MtEventManager() = default;
  55. virtual void put(MtEvent *e) = 0;
  56. virtual void reg(MtEvent::Type type, event_receive_func f, void *data) = 0;
  57. // If data==NULL, every occurrence of f is deregistered.
  58. virtual void dereg(MtEvent::Type type, event_receive_func f, void *data) = 0;
  59. };