EventSpecification.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "EventSpecification.h"
  29. #include "../../Include/RmlUi/Core/ID.h"
  30. #include "ControlledLifetimeResource.h"
  31. namespace Rml {
  32. struct EventSpecificationData {
  33. // An EventId is an index into the specifications vector, must be listed in the same order as the EventId values.
  34. Vector<EventSpecification> specifications = {
  35. // clang-format off
  36. // id type interruptible bubbles default_action
  37. {EventId::Invalid , "invalid" , false , false , DefaultActionPhase::None},
  38. {EventId::Mousedown , "mousedown" , true , true , DefaultActionPhase::TargetAndBubble},
  39. {EventId::Mousescroll , "mousescroll" , true , true , DefaultActionPhase::None},
  40. {EventId::Mouseover , "mouseover" , true , true , DefaultActionPhase::Target},
  41. {EventId::Mouseout , "mouseout" , true , true , DefaultActionPhase::Target},
  42. {EventId::Focus , "focus" , false , false , DefaultActionPhase::Target},
  43. {EventId::Blur , "blur" , false , false , DefaultActionPhase::Target},
  44. {EventId::Keydown , "keydown" , true , true , DefaultActionPhase::TargetAndBubble},
  45. {EventId::Keyup , "keyup" , true , true , DefaultActionPhase::TargetAndBubble},
  46. {EventId::Textinput , "textinput" , true , true , DefaultActionPhase::TargetAndBubble},
  47. {EventId::Mouseup , "mouseup" , true , true , DefaultActionPhase::TargetAndBubble},
  48. {EventId::Click , "click" , true , true , DefaultActionPhase::TargetAndBubble},
  49. {EventId::Dblclick , "dblclick" , true , true , DefaultActionPhase::TargetAndBubble},
  50. {EventId::Load , "load" , false , false , DefaultActionPhase::None},
  51. {EventId::Unload , "unload" , false , false , DefaultActionPhase::None},
  52. {EventId::Show , "show" , false , false , DefaultActionPhase::None},
  53. {EventId::Hide , "hide" , false , false , DefaultActionPhase::None},
  54. {EventId::Mousemove , "mousemove" , true , true , DefaultActionPhase::None},
  55. {EventId::Dragmove , "dragmove" , true , true , DefaultActionPhase::None},
  56. {EventId::Drag , "drag" , false , true , DefaultActionPhase::Target},
  57. {EventId::Dragstart , "dragstart" , false , true , DefaultActionPhase::Target},
  58. {EventId::Dragover , "dragover" , true , true , DefaultActionPhase::None},
  59. {EventId::Dragdrop , "dragdrop" , true , true , DefaultActionPhase::None},
  60. {EventId::Dragout , "dragout" , true , true , DefaultActionPhase::None},
  61. {EventId::Dragend , "dragend" , true , true , DefaultActionPhase::None},
  62. {EventId::Handledrag , "handledrag" , false , true , DefaultActionPhase::None},
  63. {EventId::Resize , "resize" , false , false , DefaultActionPhase::None},
  64. {EventId::Scroll , "scroll" , false , true , DefaultActionPhase::None},
  65. {EventId::Animationend , "animationend" , false , true , DefaultActionPhase::None},
  66. {EventId::Transitionend , "transitionend" , false , true , DefaultActionPhase::None},
  67. {EventId::Change , "change" , false , true , DefaultActionPhase::None},
  68. {EventId::Submit , "submit" , true , true , DefaultActionPhase::None},
  69. {EventId::Tabchange , "tabchange" , false , true , DefaultActionPhase::None},
  70. // clang-format on
  71. };
  72. // Reverse lookup map from event type to id.
  73. UnorderedMap<String, EventId> type_lookup;
  74. };
  75. static ControlledLifetimeResource<EventSpecificationData> event_specification_data;
  76. namespace EventSpecificationInterface {
  77. void Initialize()
  78. {
  79. event_specification_data.Initialize();
  80. auto& specifications = event_specification_data->specifications;
  81. auto& type_lookup = event_specification_data->type_lookup;
  82. type_lookup.reserve(specifications.size());
  83. for (auto& specification : specifications)
  84. type_lookup.emplace(specification.type, specification.id);
  85. #ifdef RMLUI_DEBUG
  86. // Verify that all event ids are specified
  87. RMLUI_ASSERT((int)specifications.size() == (int)EventId::NumDefinedIds);
  88. for (int i = 0; i < (int)specifications.size(); i++)
  89. {
  90. // Verify correct order
  91. RMLUI_ASSERT(i == (int)specifications[i].id);
  92. }
  93. #endif
  94. }
  95. void Shutdown()
  96. {
  97. event_specification_data.Shutdown();
  98. }
  99. static EventSpecification& GetMutable(EventId id)
  100. {
  101. auto& specifications = event_specification_data->specifications;
  102. size_t i = static_cast<size_t>(id);
  103. if (i < specifications.size())
  104. return specifications[i];
  105. return specifications[0];
  106. }
  107. // Get event specification for the given type.
  108. // If not found: Inserts a new entry with given values.
  109. static EventSpecification& GetOrInsert(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  110. {
  111. auto& specifications = event_specification_data->specifications;
  112. auto& type_lookup = event_specification_data->type_lookup;
  113. auto it = type_lookup.find(event_type);
  114. if (it != type_lookup.end())
  115. return GetMutable(it->second);
  116. const size_t new_id_num = specifications.size();
  117. if (new_id_num >= size_t(EventId::MaxNumIds))
  118. {
  119. Log::Message(Log::LT_ERROR, "Error while registering event type '%s': Maximum number of allowed events exceeded.", event_type.c_str());
  120. RMLUI_ERROR;
  121. return specifications.front();
  122. }
  123. // No specification found for this name, insert a new entry with default values
  124. EventId new_id = static_cast<EventId>(new_id_num);
  125. specifications.push_back(EventSpecification{new_id, event_type, interruptible, bubbles, default_action_phase});
  126. type_lookup.emplace(event_type, new_id);
  127. return specifications.back();
  128. }
  129. const EventSpecification& Get(EventId id)
  130. {
  131. return GetMutable(id);
  132. }
  133. const EventSpecification& GetOrInsert(const String& event_type)
  134. {
  135. // Default values for new event types defined as follows:
  136. constexpr bool interruptible = true;
  137. constexpr bool bubbles = true;
  138. constexpr DefaultActionPhase default_action_phase = DefaultActionPhase::None;
  139. return GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  140. }
  141. EventId GetIdOrInsert(const String& event_type)
  142. {
  143. auto& type_lookup = event_specification_data->type_lookup;
  144. auto it = type_lookup.find(event_type);
  145. if (it != type_lookup.end())
  146. return it->second;
  147. return GetOrInsert(event_type).id;
  148. }
  149. EventId InsertOrReplaceCustom(const String& event_type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  150. {
  151. auto& specifications = event_specification_data->specifications;
  152. const size_t size_before = specifications.size();
  153. EventSpecification& specification = GetOrInsert(event_type, interruptible, bubbles, default_action_phase);
  154. bool got_existing_entry = (size_before == specifications.size());
  155. // If we found an existing entry of same type, replace it, but only if it is a custom event id.
  156. if (got_existing_entry && (int)specification.id >= (int)EventId::FirstCustomId)
  157. {
  158. specification.interruptible = interruptible;
  159. specification.bubbles = bubbles;
  160. specification.default_action_phase = default_action_phase;
  161. }
  162. return specification.id;
  163. }
  164. } // namespace EventSpecificationInterface
  165. } // namespace Rml