DebuggerPlugin.cpp 12 KB

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