ElementDataModels.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "ElementDataModels.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/DataModelHandle.h"
  32. #include "../../Include/RmlUi/Core/DataVariable.h"
  33. #include "../../Include/RmlUi/Core/StyleSheet.h"
  34. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  35. #include "../../Include/RmlUi/Core/SystemInterface.h"
  36. #include "CommonSource.h"
  37. #include "DataModelsSource.h"
  38. #include <algorithm>
  39. namespace Rml {
  40. namespace Debugger {
  41. static void ReadDataVariableRecursive(String& out_rml, const int indent_level, const DataVariable variable)
  42. {
  43. constexpr int max_num_child_elements = 50;
  44. String indent_child_element;
  45. for (int i = 0; i <= indent_level; i++)
  46. indent_child_element += "&nbsp;&nbsp;";
  47. switch (variable.Type())
  48. {
  49. case DataVariableType::Scalar:
  50. {
  51. Variant variant;
  52. if (!variable.Get(variant))
  53. out_rml += "<em>(unreadable)</em>";
  54. else if (variant.GetType() == Variant::NONE)
  55. out_rml += "<em>(none)</em>";
  56. else if (variant.GetType() == Variant::BOOL)
  57. out_rml += variant.Get<bool>() ? "true" : "false";
  58. else
  59. out_rml += StringUtilities::EncodeRml(variant.Get<String>());
  60. out_rml += "<br/>";
  61. }
  62. break;
  63. case DataVariableType::Array:
  64. {
  65. const int size = variable.Size();
  66. out_rml += "Array (" + ToString(variable.Size()) + ")<br/>";
  67. for (int i = 0; i < size; i++)
  68. {
  69. if (i >= max_num_child_elements)
  70. {
  71. out_rml += indent_child_element + "<span class='name'>[...]</span><br/>";
  72. break;
  73. }
  74. out_rml += indent_child_element + "<span class='name'>[" + ToString(i) + "]</span>: ";
  75. DataVariable child_variable = variable.Child(DataAddressEntry(i));
  76. ReadDataVariableRecursive(out_rml, indent_level + 1, child_variable);
  77. }
  78. }
  79. break;
  80. case DataVariableType::Struct:
  81. {
  82. const StringList members = Detail::DataVariableAccessor::GetDefinition(variable)->ReflectMemberNames();
  83. out_rml += "Struct (" + ToString(members.size()) + ")<br/>";
  84. for (const String& member_name : members)
  85. {
  86. if (std::distance(&members.front(), &member_name) >= max_num_child_elements)
  87. {
  88. out_rml += indent_child_element + "<span class='name'>[...]</span><br/>";
  89. break;
  90. }
  91. out_rml += indent_child_element + "<span class='name'>." + member_name + "</span>: ";
  92. DataVariable child_variable = variable.Child(DataAddressEntry(member_name));
  93. ReadDataVariableRecursive(out_rml, indent_level + 1, child_variable);
  94. }
  95. }
  96. break;
  97. }
  98. }
  99. ElementDataModels::ElementDataModels(const String& tag) : ElementDebugDocument(tag) {}
  100. ElementDataModels::~ElementDataModels()
  101. {
  102. RemoveEventListener(EventId::Click, this);
  103. }
  104. bool ElementDataModels::Initialise(Context* target_context)
  105. {
  106. SetInnerRML(data_models_rml);
  107. SetId("rmlui-debug-data-models");
  108. AddEventListener(EventId::Click, this);
  109. SharedPtr<StyleSheetContainer> style_sheet = Factory::InstanceStyleSheetString(String(common_rcss) + String(data_models_rcss));
  110. if (!style_sheet)
  111. return false;
  112. SetStyleSheetContainer(std::move(style_sheet));
  113. SetDebugContext(target_context);
  114. return true;
  115. }
  116. void ElementDataModels::Reset()
  117. {
  118. SetDebugContext(nullptr);
  119. }
  120. void ElementDataModels::SetDebugContext(Context* new_debug_context)
  121. {
  122. debug_context = new_debug_context;
  123. }
  124. void ElementDataModels::OnUpdate()
  125. {
  126. if (!IsVisible() || !debug_context)
  127. return;
  128. const double t = GetSystemInterface()->GetElapsedTime();
  129. const float dt = (float)(t - previous_update_time);
  130. constexpr float update_interval = 0.3f;
  131. if (dt > update_interval)
  132. {
  133. previous_update_time = t;
  134. UpdateContent();
  135. }
  136. }
  137. void ElementDataModels::ProcessEvent(Event& event)
  138. {
  139. if (!IsVisible())
  140. return;
  141. Element* target_element = event.GetTargetElement();
  142. if (target_element->GetOwnerDocument() != this)
  143. return;
  144. if (event == EventId::Click)
  145. {
  146. const String& id = event.GetTargetElement()->GetId();
  147. if (id == "close_button")
  148. Hide();
  149. event.StopPropagation();
  150. }
  151. }
  152. void ElementDataModels::UpdateContent()
  153. {
  154. RMLUI_ASSERT(debug_context);
  155. Element* models_content_element = GetElementById("content");
  156. SmallOrderedMap<String, String> new_model_rml_map;
  157. const UnorderedMap<String, DataModelConstructor> data_models = debug_context->GetDataModels();
  158. for (const auto& name_model_pair : data_models)
  159. {
  160. const String& model_name = name_model_pair.first;
  161. const DataModelConstructor& model_constructor = name_model_pair.second;
  162. String& model_rml = new_model_rml_map[model_name];
  163. model_rml += "<div class='model'>";
  164. model_rml += "<h2>" + model_name + "</h2>";
  165. model_rml += "<div class='model-content'>";
  166. const UnorderedMap<String, DataVariable>& variables = Detail::DataModelConstructorAccessor::GetAllVariables(model_constructor);
  167. for (const auto& name_variable_pair : variables)
  168. {
  169. const String& name = name_variable_pair.first;
  170. const DataVariable& variable = name_variable_pair.second;
  171. model_rml += "<span class='name'>" + name + "</span>: ";
  172. ReadDataVariableRecursive(model_rml, 0, variable);
  173. }
  174. if (variables.empty())
  175. model_rml += "<em>No data variables in data model.</em><br/>";
  176. model_rml += "</div>";
  177. model_rml += "</div>";
  178. }
  179. if (new_model_rml_map != model_rml_map)
  180. {
  181. model_rml_map = std::move(new_model_rml_map);
  182. String models_rml;
  183. for (const auto& name_rml_pair : model_rml_map)
  184. models_rml += name_rml_pair.second;
  185. if (data_models.empty())
  186. models_rml = "<em>No data models in context.</em>";
  187. models_content_element->SetInnerRML(models_rml);
  188. }
  189. }
  190. } // namespace Debugger
  191. } // namespace Rml