Plugin.cpp 11 KB

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