DebuggerPlugin.cpp 11 KB

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