EventSpecification.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "EventSpecification.h"
  2. #include "../../Include/RmlUi/Core/ID.h"
  3. #include "ControlledLifetimeResource.h"
  4. namespace Rml {
  5. struct EventSpecificationData {
  6. // An EventId is an index into the specifications vector, must be listed in the same order as the EventId values.
  7. Vector<EventSpecification> specifications = {
  8. // clang-format off
  9. // id type interruptible bubbles default_action
  10. {EventId::Invalid , "invalid" , false , false , DefaultActionPhase::None},
  11. {EventId::Mousedown , "mousedown" , true , true , DefaultActionPhase::TargetAndBubble},
  12. {EventId::Mousescroll , "mousescroll" , true , true , DefaultActionPhase::None},
  13. {EventId::Mouseover , "mouseover" , true , true , DefaultActionPhase::Target},
  14. {EventId::Mouseout , "mouseout" , true , true , DefaultActionPhase::Target},
  15. {EventId::Focus , "focus" , false , false , DefaultActionPhase::Target},
  16. {EventId::Blur , "blur" , false , false , DefaultActionPhase::Target},
  17. {EventId::Keydown , "keydown" , true , true , DefaultActionPhase::TargetAndBubble},
  18. {EventId::Keyup , "keyup" , true , true , DefaultActionPhase::TargetAndBubble},
  19. {EventId::Textinput , "textinput" , true , true , DefaultActionPhase::TargetAndBubble},
  20. {EventId::Mouseup , "mouseup" , true , true , DefaultActionPhase::TargetAndBubble},
  21. {EventId::Click , "click" , true , true , DefaultActionPhase::TargetAndBubble},
  22. {EventId::Dblclick , "dblclick" , true , true , DefaultActionPhase::TargetAndBubble},
  23. {EventId::Load , "load" , false , false , DefaultActionPhase::None},
  24. {EventId::Unload , "unload" , false , false , DefaultActionPhase::None},
  25. {EventId::Show , "show" , false , false , DefaultActionPhase::None},
  26. {EventId::Hide , "hide" , false , false , DefaultActionPhase::None},
  27. {EventId::Mousemove , "mousemove" , true , true , DefaultActionPhase::None},
  28. {EventId::Dragmove , "dragmove" , true , true , DefaultActionPhase::None},
  29. {EventId::Drag , "drag" , false , true , DefaultActionPhase::Target},
  30. {EventId::Dragstart , "dragstart" , false , true , DefaultActionPhase::Target},
  31. {EventId::Dragover , "dragover" , true , true , DefaultActionPhase::None},
  32. {EventId::Dragdrop , "dragdrop" , true , true , DefaultActionPhase::None},
  33. {EventId::Dragout , "dragout" , true , true , DefaultActionPhase::None},
  34. {EventId::Dragend , "dragend" , true , true , DefaultActionPhase::None},
  35. {EventId::Handledrag , "handledrag" , false , true , DefaultActionPhase::None},
  36. {EventId::Resize , "resize" , false , false , DefaultActionPhase::None},
  37. {EventId::Scroll , "scroll" , false , true , DefaultActionPhase::None},
  38. {EventId::Animationend , "animationend" , false , true , DefaultActionPhase::None},
  39. {EventId::Transitionend , "transitionend" , false , true , DefaultActionPhase::None},
  40. {EventId::Change , "change" , false , true , DefaultActionPhase::None},
  41. {EventId::Submit , "submit" , true , true , DefaultActionPhase::None},
  42. {EventId::Tabchange , "tabchange" , false , true , DefaultActionPhase::None},
  43. // clang-format on
  44. };
  45. // Reverse lookup map from event type to id.
  46. UnorderedMap<String, EventId> type_lookup;
  47. };
  48. static ControlledLifetimeResource<EventSpecificationData> event_specification_data;
  49. namespace EventSpecificationInterface {
  50. void Initialize()
  51. {
  52. event_specification_data.Initialize();
  53. auto& specifications = event_specification_data->specifications;
  54. auto& type_lookup = event_specification_data->type_lookup;
  55. type_lookup.reserve(specifications.size());
  56. for (auto& specification : specifications)
  57. type_lookup.emplace(specification.type, specification.id);
  58. #ifdef RMLUI_DEBUG
  59. // Verify that all event ids are specified
  60. RMLUI_ASSERT((int)specifications.size() == (int)EventId::NumDefinedIds);
  61. for (int i = 0; i < (int)specifications.size(); i++)
  62. {
  63. // Verify correct order
  64. RMLUI_ASSERT(i == (int)specifications[i].id);
  65. }
  66. #endif
  67. }
  68. void Shutdown()
  69. {
  70. event_specification_data.Shutdown();
  71. }
  72. static EventSpecification& GetMutable(EventId id)
  73. {
  74. auto& specifications = event_specification_data->specifications;
  75. size_t i = static_cast<size_t>(id);
  76. if (i < specifications.size())
  77. return specifications[i];
  78. return specifications[0];
  79. }
  80. // Get event specification for the given type.
  81. // If not found: Inserts a new entry with given values.
  82. static EventSpecification& GetOrInsert(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  83. {
  84. auto& specifications = event_specification_data->specifications;
  85. auto& type_lookup = event_specification_data->type_lookup;
  86. auto it = type_lookup.find(event_type);
  87. if (it != type_lookup.end())
  88. return GetMutable(it->second);
  89. const size_t new_id_num = specifications.size();
  90. if (new_id_num >= size_t(EventId::MaxNumIds))
  91. {
  92. Log::Message(Log::LT_ERROR, "Error while registering event type '%s': Maximum number of allowed events exceeded.", event_type.c_str());
  93. RMLUI_ERROR;
  94. return specifications.front();
  95. }
  96. // No specification found for this name, insert a new entry with default values
  97. EventId new_id = static_cast<EventId>(new_id_num);
  98. specifications.push_back(EventSpecification{new_id, event_type, interruptible, bubbles, default_action_phase});
  99. type_lookup.emplace(event_type, new_id);
  100. return specifications.back();
  101. }
  102. const EventSpecification& Get(EventId id)
  103. {
  104. return GetMutable(id);
  105. }
  106. const EventSpecification& GetOrInsert(const String& event_type)
  107. {
  108. // Default values for new event types defined as follows:
  109. constexpr bool interruptible = true;
  110. constexpr bool bubbles = true;
  111. constexpr DefaultActionPhase default_action_phase = DefaultActionPhase::None;
  112. return GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  113. }
  114. EventId GetIdOrInsert(const String& event_type)
  115. {
  116. auto& type_lookup = event_specification_data->type_lookup;
  117. auto it = type_lookup.find(event_type);
  118. if (it != type_lookup.end())
  119. return it->second;
  120. return GetOrInsert(event_type).id;
  121. }
  122. EventId InsertOrReplaceCustom(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  123. {
  124. auto& specifications = event_specification_data->specifications;
  125. const size_t size_before = specifications.size();
  126. EventSpecification& specification = GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  127. bool got_existing_entry = (size_before == specifications.size());
  128. // If we found an existing entry of same type, replace it, but only if it is a custom event id.
  129. if (got_existing_entry && (int)specification.id >= (int)EventId::FirstCustomId)
  130. {
  131. specification.interruptible = interruptible;
  132. specification.bubbles = bubbles;
  133. specification.default_action_phase = default_action_phase;
  134. }
  135. return specification.id;
  136. }
  137. } // namespace EventSpecificationInterface
  138. } // namespace Rml