EventDispatcher.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include <vector>
  22. #include <unordered_map>
  23. #include <functional>
  24. #include <string>
  25. namespace cocos2d
  26. {
  27. // Touch event related
  28. struct TouchInfo
  29. {
  30. float x = 0;
  31. float y = 0;
  32. int index = 0;
  33. };
  34. struct TouchEvent
  35. {
  36. enum class Type : uint8_t
  37. {
  38. BEGAN,
  39. MOVED,
  40. ENDED,
  41. CANCELLED,
  42. UNKNOWN
  43. };
  44. std::vector<TouchInfo> touches;
  45. Type type = Type::UNKNOWN;
  46. };
  47. struct MouseEvent
  48. {
  49. enum class Type : uint8_t
  50. {
  51. DOWN,
  52. UP,
  53. MOVE,
  54. WHEEL,
  55. UNKNOWN
  56. };
  57. float x = 0.0f;
  58. float y = 0.0f;
  59. // The button number that was pressed when the mouse event was fired: Left button=0, middle button=1 (if present), right button=2.
  60. // For mice configured for left handed use in which the button actions are reversed the values are instead read from right to left.
  61. unsigned short button = 0;
  62. Type type = Type::UNKNOWN;
  63. };
  64. struct KeyboardEvent
  65. {
  66. enum class Action : uint8_t {
  67. PRESS,
  68. RELEASE,
  69. REPEAT,
  70. UNKNOWN
  71. };
  72. int key = -1;
  73. Action action = Action::UNKNOWN;
  74. bool altKeyActive = false;
  75. bool ctrlKeyActive = false;
  76. bool metaKeyActive = false;
  77. bool shiftKeyActive = false;
  78. };
  79. class CustomEvent
  80. {
  81. public:
  82. std::string name;
  83. union {
  84. void* ptrVal;
  85. long longVal;
  86. int intVal;
  87. short shortVal;
  88. char charVal;
  89. bool boolVal;
  90. } args[10];
  91. CustomEvent(){};
  92. virtual ~CustomEvent(){};
  93. };
  94. class EventDispatcher
  95. {
  96. public:
  97. static void init();
  98. static void destroy();
  99. static void dispatchTouchEvent(const struct TouchEvent& touchEvent);
  100. static void dispatchMouseEvent(const struct MouseEvent& mouseEvent);
  101. static void dispatchKeyboardEvent(const struct KeyboardEvent& keyboardEvent);
  102. static void dispatchTickEvent(float dt);
  103. static void dispatchResizeEvent(int width, int height);
  104. static void dispatchOrientationChangeEvent(int rotation);
  105. static void dispatchOnPauseEvent();
  106. static void dispatchOnResumeEvent();
  107. using CustomEventListener = std::function<void(const CustomEvent&)>;
  108. static uint32_t addCustomEventListener(const std::string& eventName, const CustomEventListener& listener);
  109. static void removeCustomEventListener(const std::string& eventName, uint32_t listenerID);
  110. static void removeAllCustomEventListeners(const std::string& eventName);
  111. static void dispatchCustomEvent(const CustomEvent& event);
  112. private:
  113. struct Node
  114. {
  115. CustomEventListener listener;
  116. uint32_t listenerID;
  117. struct Node* next;
  118. };
  119. static std::unordered_map<std::string, Node*> _listeners;
  120. };
  121. } // end of namespace cocos2d