DebuggerPlugin.cpp 11 KB

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