DebuggerPlugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "DebuggerPlugin.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/ElementInstancer.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/Factory.h"
  34. #include "../../Include/RmlUi/Core/Types.h"
  35. #include "DebuggerSystemInterface.h"
  36. #include "ElementContextHook.h"
  37. #include "ElementDataModels.h"
  38. #include "ElementDebugDocument.h"
  39. #include "ElementInfo.h"
  40. #include "ElementLog.h"
  41. #include "FontSource.h"
  42. #include "Geometry.h"
  43. #include "MenuSource.h"
  44. #include <stack>
  45. namespace Rml {
  46. namespace Debugger {
  47. DebuggerPlugin* DebuggerPlugin::instance = nullptr;
  48. DebuggerPlugin::DebuggerPlugin()
  49. {
  50. RMLUI_ASSERT(instance == nullptr);
  51. instance = this;
  52. host_context = nullptr;
  53. debug_context = nullptr;
  54. log_interface = nullptr;
  55. menu_element = nullptr;
  56. info_element = nullptr;
  57. log_element = nullptr;
  58. data_explorer_element = nullptr;
  59. hook_element = nullptr;
  60. render_outlines = false;
  61. application_interface = nullptr;
  62. }
  63. DebuggerPlugin::~DebuggerPlugin()
  64. {
  65. instance = nullptr;
  66. }
  67. bool DebuggerPlugin::Initialise(Context* context)
  68. {
  69. host_context = context;
  70. Geometry::SetContext(context);
  71. if (!LoadFont())
  72. {
  73. Log::Message(Log::LT_ERROR, "Failed to initialise debugger, unable to load font.");
  74. return false;
  75. }
  76. if (!LoadMenuElement() || !LoadInfoElement() || !LoadLogElement() || !LoadDataExplorerElement())
  77. {
  78. Log::Message(Log::LT_ERROR, "Failed to initialise debugger, error while load debugger elements.");
  79. return false;
  80. }
  81. hook_element_instancer = MakeUnique<ElementInstancerGeneric<ElementContextHook>>();
  82. Factory::RegisterElementInstancer("debug-hook", hook_element_instancer.get());
  83. return true;
  84. }
  85. bool DebuggerPlugin::SetContext(Context* context)
  86. {
  87. if (debug_context && hook_element)
  88. {
  89. debug_context->UnloadDocument(hook_element);
  90. hook_element = nullptr;
  91. }
  92. if (context)
  93. {
  94. ElementDocument* element = context->CreateDocument("debug-hook");
  95. if (!element)
  96. return false;
  97. RMLUI_ASSERT(!hook_element);
  98. hook_element = rmlui_dynamic_cast<ElementContextHook*>(element);
  99. if (!hook_element)
  100. {
  101. context->UnloadDocument(element);
  102. return false;
  103. }
  104. hook_element->Initialise(this);
  105. }
  106. if (info_element)
  107. {
  108. SetupInfoListeners(context);
  109. info_element->Reset();
  110. }
  111. if (data_explorer_element)
  112. {
  113. data_explorer_element->SetDebugContext(context);
  114. }
  115. debug_context = context;
  116. return true;
  117. }
  118. void DebuggerPlugin::SetVisible(bool visibility)
  119. {
  120. if (visibility)
  121. menu_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  122. else
  123. menu_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  124. }
  125. bool DebuggerPlugin::IsVisible()
  126. {
  127. return menu_element->IsVisible();
  128. }
  129. void DebuggerPlugin::Render()
  130. {
  131. // Render the outlines of the debug context's elements.
  132. if (render_outlines && debug_context)
  133. {
  134. for (int i = 0; i < debug_context->GetNumDocuments(); ++i)
  135. {
  136. ElementDocument* document = debug_context->GetDocument(i);
  137. if (document->GetId().find("rmlui-debug-") == 0)
  138. continue;
  139. Stack<Element*> element_stack;
  140. element_stack.push(document);
  141. while (!element_stack.empty())
  142. {
  143. Element* element = element_stack.top();
  144. element_stack.pop();
  145. if (element->IsVisible())
  146. {
  147. ElementUtilities::ApplyTransform(*element);
  148. for (int j = 0; j < element->GetNumBoxes(); ++j)
  149. {
  150. const RenderBox box = element->GetRenderBox(BoxArea::Border, j);
  151. Geometry::RenderOutline(element->GetAbsoluteOffset(BoxArea::Border) + box.GetBorderOffset(), box.GetFillSize(),
  152. Colourb(255, 0, 0, 128), 1);
  153. }
  154. for (int j = 0; j < element->GetNumChildren(); ++j)
  155. element_stack.push(element->GetChild(j));
  156. }
  157. }
  158. }
  159. }
  160. // Render the info element's boxes.
  161. if (info_element && info_element->IsVisible())
  162. {
  163. info_element->RenderHoverElement();
  164. info_element->RenderSourceElement();
  165. }
  166. }
  167. void DebuggerPlugin::OnShutdown()
  168. {
  169. // Release the elements before we leak track, this ensures the debugger hook has been cleared
  170. // and that we don't try send the messages to the debug log window
  171. ReleaseElements();
  172. hook_element_instancer.reset();
  173. delete this;
  174. }
  175. void DebuggerPlugin::OnContextDestroy(Context* context)
  176. {
  177. if (context == debug_context)
  178. {
  179. // The context we're debugging is being destroyed, so we need to remove our debug hook elements.
  180. SetContext(nullptr);
  181. }
  182. if (context == host_context)
  183. {
  184. // Our host is being destroyed, so we need to shut down the debugger.
  185. ReleaseElements();
  186. Geometry::SetContext(nullptr);
  187. host_context = nullptr;
  188. }
  189. }
  190. void DebuggerPlugin::OnElementDestroy(Element* element)
  191. {
  192. // Detect external destruction of the debugger documents. This can happen for example if the user calls
  193. // `Context::UnloadAllDocuments()` on the host context.
  194. if (element == menu_element || element == info_element || element == log_element || element == data_explorer_element)
  195. {
  196. ReleaseElements();
  197. Log::Message(Log::LT_ERROR,
  198. "A document owned by the Debugger plugin was destroyed externally. This is not allowed. Consider shutting down the debugger instead.");
  199. }
  200. if (info_element)
  201. info_element->OnElementDestroy(element);
  202. }
  203. void DebuggerPlugin::ProcessEvent(Event& event)
  204. {
  205. struct ButtonIdToDocumentMapping {
  206. String id;
  207. ElementDocument* document;
  208. };
  209. const ButtonIdToDocumentMapping button_mappings[] = {
  210. {"event-log-button", log_element},
  211. {"debug-info-button", info_element},
  212. {"data-models-button", data_explorer_element},
  213. };
  214. if (event == EventId::Click)
  215. {
  216. for (const ButtonIdToDocumentMapping& button_mapping : button_mappings)
  217. {
  218. if (event.GetTargetElement()->GetId() == button_mapping.id)
  219. {
  220. if (button_mapping.document->IsVisible())
  221. button_mapping.document->Hide();
  222. else
  223. button_mapping.document->Show();
  224. }
  225. }
  226. if (event.GetTargetElement()->GetId() == "outlines-button")
  227. {
  228. render_outlines = !render_outlines;
  229. event.GetTargetElement()->SetClass("open", render_outlines);
  230. }
  231. }
  232. else if (event == EventId::Hide || event == EventId::Show)
  233. {
  234. for (const ButtonIdToDocumentMapping& button_mapping : button_mappings)
  235. {
  236. if (event.GetTargetElement() == button_mapping.document)
  237. {
  238. Element* button = menu_element->GetElementById(button_mapping.id);
  239. const bool set_open = (event == EventId::Show);
  240. button->SetClass("open", set_open);
  241. }
  242. }
  243. }
  244. }
  245. DebuggerPlugin* DebuggerPlugin::GetInstance()
  246. {
  247. return instance;
  248. }
  249. bool DebuggerPlugin::LoadFont()
  250. {
  251. const String font_family_name = "rmlui-debugger-font";
  252. return (LoadFontFace({courier_prime_code, sizeof(courier_prime_code)}, font_family_name, Style::FontStyle::Normal, Style::FontWeight::Normal) &&
  253. LoadFontFace({courier_prime_code_italic, sizeof(courier_prime_code_italic)}, font_family_name, Style::FontStyle::Italic,
  254. Style::FontWeight::Normal));
  255. }
  256. bool DebuggerPlugin::LoadMenuElement()
  257. {
  258. debug_document_instancer = MakeUnique<ElementInstancerGeneric<ElementDebugDocument>>();
  259. Factory::RegisterElementInstancer("debug-document", debug_document_instancer.get());
  260. menu_element = host_context->CreateDocument("debug-document");
  261. if (!menu_element)
  262. return false;
  263. menu_element->SetId("rmlui-debug-menu");
  264. menu_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  265. menu_element->SetInnerRML(menu_rml);
  266. SharedPtr<StyleSheetContainer> style_sheet = Factory::InstanceStyleSheetString(menu_rcss);
  267. if (!style_sheet)
  268. {
  269. host_context->UnloadDocument(menu_element);
  270. menu_element = nullptr;
  271. return false;
  272. }
  273. menu_element->SetStyleSheetContainer(std::move(style_sheet));
  274. menu_element->GetElementById("version-number")->SetInnerRML(Rml::GetVersion());
  275. for (auto* id : {"event-log-button", "debug-info-button", "outlines-button", "data-models-button"})
  276. {
  277. Element* button = menu_element->GetElementById(id);
  278. button->AddEventListener(EventId::Click, this);
  279. }
  280. return true;
  281. }
  282. bool DebuggerPlugin::LoadInfoElement()
  283. {
  284. info_element_instancer = MakeUnique<ElementInstancerGeneric<ElementInfo>>();
  285. Factory::RegisterElementInstancer("debug-info", info_element_instancer.get());
  286. info_element = rmlui_dynamic_cast<ElementInfo*>(host_context->CreateDocument("debug-info"));
  287. if (!info_element)
  288. return false;
  289. info_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  290. if (!info_element->Initialise())
  291. {
  292. host_context->UnloadDocument(info_element);
  293. info_element = nullptr;
  294. return false;
  295. }
  296. info_element->AddEventListener(EventId::Hide, this);
  297. info_element->AddEventListener(EventId::Show, this);
  298. return true;
  299. }
  300. bool DebuggerPlugin::LoadLogElement()
  301. {
  302. log_element_instancer = MakeUnique<ElementInstancerGeneric<ElementLog>>();
  303. Factory::RegisterElementInstancer("debug-log", log_element_instancer.get());
  304. log_element = rmlui_dynamic_cast<ElementLog*>(host_context->CreateDocument("debug-log"));
  305. if (!log_element)
  306. return false;
  307. log_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  308. if (!log_element->Initialise())
  309. {
  310. host_context->UnloadDocument(log_element);
  311. log_element = nullptr;
  312. return false;
  313. }
  314. log_element->AddEventListener(EventId::Hide, this);
  315. log_element->AddEventListener(EventId::Show, this);
  316. // Make the system interface; this will trap the log messages for us.
  317. application_interface = Rml::GetSystemInterface();
  318. log_interface = MakeUnique<DebuggerSystemInterface>(application_interface, log_element);
  319. Rml::SetSystemInterface(log_interface.get());
  320. return true;
  321. }
  322. bool DebuggerPlugin::LoadDataExplorerElement()
  323. {
  324. data_explorer_element_instancer = MakeUnique<ElementInstancerGeneric<ElementDataModels>>();
  325. Factory::RegisterElementInstancer("debug-data-models", data_explorer_element_instancer.get());
  326. data_explorer_element = rmlui_dynamic_cast<ElementDataModels*>(host_context->CreateDocument("debug-data-models"));
  327. if (!data_explorer_element)
  328. return false;
  329. data_explorer_element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  330. if (!data_explorer_element->Initialise(debug_context))
  331. {
  332. host_context->UnloadDocument(data_explorer_element);
  333. data_explorer_element = nullptr;
  334. return false;
  335. }
  336. data_explorer_element->AddEventListener(EventId::Hide, this);
  337. data_explorer_element->AddEventListener(EventId::Show, this);
  338. return true;
  339. }
  340. void DebuggerPlugin::SetupInfoListeners(Rml::Context* new_context)
  341. {
  342. RMLUI_ASSERT(info_element);
  343. if (debug_context)
  344. {
  345. debug_context->RemoveEventListener("click", info_element, true);
  346. debug_context->RemoveEventListener("mouseover", info_element, true);
  347. debug_context->RemoveEventListener("mouseout", info_element, true);
  348. }
  349. if (new_context)
  350. {
  351. new_context->AddEventListener("click", info_element, true);
  352. new_context->AddEventListener("mouseover", info_element, true);
  353. new_context->AddEventListener("mouseout", info_element, true);
  354. }
  355. }
  356. void DebuggerPlugin::ReleaseElements()
  357. {
  358. // Erase event listeners to prevent crashes.
  359. if (info_element)
  360. SetupInfoListeners(nullptr);
  361. if (host_context)
  362. {
  363. if (menu_element)
  364. {
  365. host_context->UnloadDocument(menu_element);
  366. menu_element = nullptr;
  367. }
  368. if (info_element)
  369. {
  370. host_context->UnloadDocument(info_element);
  371. info_element = nullptr;
  372. }
  373. if (log_element)
  374. {
  375. host_context->UnloadDocument(log_element);
  376. log_element = nullptr;
  377. Rml::SetSystemInterface(application_interface);
  378. application_interface = nullptr;
  379. log_interface.reset();
  380. }
  381. if (data_explorer_element)
  382. {
  383. host_context->UnloadDocument(data_explorer_element);
  384. data_explorer_element = nullptr;
  385. }
  386. // Update to release documents before the plugin gets deleted.
  387. // Helps avoid cleanup crashes.
  388. host_context->Update();
  389. }
  390. if (debug_context)
  391. {
  392. if (hook_element)
  393. {
  394. debug_context->UnloadDocument(hook_element);
  395. hook_element = nullptr;
  396. }
  397. // Update to release documents before the plugin gets deleted.
  398. // Helps avoid cleanup crashes.
  399. debug_context->Update();
  400. }
  401. }
  402. } // namespace Debugger
  403. } // namespace Rml