Plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/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 "SystemInterface.h"
  42. #include <stack>
  43. namespace Rml {
  44. namespace Debugger {
  45. Plugin* Plugin::instance = nullptr;
  46. Plugin::Plugin()
  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. Plugin::~Plugin()
  61. {
  62. instance = nullptr;
  63. }
  64. // Initialises the debugging tools into the given context.
  65. bool Plugin::Initialise(Core::Context* context)
  66. {
  67. host_context = context;
  68. Geometry::SetContext(context);
  69. if (!LoadFont())
  70. {
  71. Core::Log::Message(Core::Log::LT_ERROR, "Failed to initialise debugger, unable to load font.");
  72. return false;
  73. }
  74. if (!LoadMenuElement() ||
  75. !LoadInfoElement() ||
  76. !LoadLogElement())
  77. {
  78. Core::Log::Message(Core::Log::LT_ERROR, "Failed to initialise debugger, error while load debugger elements.");
  79. return false;
  80. }
  81. hook_element_instancer = std::make_unique< Core::ElementInstancerGeneric<ElementContextHook> >();
  82. Core::Factory::RegisterElementInstancer("debug-hook", hook_element_instancer.get());
  83. return true;
  84. }
  85. // Sets the context to be debugged.
  86. bool Plugin::SetContext(Core::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. Core::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 Plugin::SetVisible(bool visibility)
  129. {
  130. if (visibility)
  131. menu_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  132. else
  133. menu_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  134. }
  135. // Returns the visibility of the debugger.
  136. bool Plugin::IsVisible()
  137. {
  138. return menu_element->IsVisible();
  139. }
  140. // Renders any debug elements in the debug context.
  141. void Plugin::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. Core::ElementDocument* document = debug_context->GetDocument(i);
  149. if (document->GetId().find("rmlui-debug-") == 0)
  150. continue;
  151. std::stack< Core::Element* > element_stack;
  152. element_stack.push(document);
  153. while (!element_stack.empty())
  154. {
  155. Core::Element* element = element_stack.top();
  156. element_stack.pop();
  157. if (element->IsVisible())
  158. {
  159. Core::ElementUtilities::ApplyTransform(*element);
  160. for (int j = 0; j < element->GetNumBoxes(); ++j)
  161. {
  162. const Core::Box& box = element->GetBox(j);
  163. Geometry::RenderOutline(
  164. element->GetAbsoluteOffset(Core::Box::BORDER) + box.GetPosition(Core::Box::BORDER),
  165. box.GetSize(Core::Box::BORDER),
  166. Core::Colourb(255, 0, 0, 128),
  167. 1
  168. );
  169. }
  170. for (int j = 0; j < element->GetNumChildren(); ++j)
  171. element_stack.push(element->GetChild(j));
  172. }
  173. }
  174. }
  175. }
  176. // Render the info element's boxes.
  177. if (info_element && info_element->IsVisible())
  178. {
  179. info_element->RenderHoverElement();
  180. info_element->RenderSourceElement();
  181. }
  182. }
  183. // Called when RmlUi shuts down.
  184. void Plugin::OnShutdown()
  185. {
  186. // Release the elements before we leak track, this ensures the debugger hook has been cleared
  187. // and that we don't try send the messages to the debug log window
  188. ReleaseElements();
  189. hook_element_instancer.reset();
  190. delete this;
  191. }
  192. // Called whenever a RmlUi context is destroyed.
  193. void Plugin::OnContextDestroy(Core::Context* context)
  194. {
  195. if (context == debug_context)
  196. {
  197. // The context we're debugging is being destroyed, so we need to remove our debug hook elements.
  198. SetContext(nullptr);
  199. }
  200. if (context == host_context)
  201. {
  202. // Our host is being destroyed, so we need to shut down the debugger.
  203. ReleaseElements();
  204. Geometry::SetContext(nullptr);
  205. host_context = nullptr;
  206. }
  207. }
  208. // Called whenever an element is destroyed.
  209. void Plugin::OnElementDestroy(Core::Element* element)
  210. {
  211. if (info_element)
  212. info_element->OnElementDestroy(element);
  213. }
  214. // Event handler for events from the debugger elements.
  215. void Plugin::ProcessEvent(Core::Event& event)
  216. {
  217. if (event == Core::EventId::Click)
  218. {
  219. if (event.GetTargetElement()->GetId() == "event-log-button")
  220. {
  221. if (log_element->IsVisible())
  222. log_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  223. else
  224. log_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  225. }
  226. else if (event.GetTargetElement()->GetId() == "debug-info-button")
  227. {
  228. if (info_element->IsVisible())
  229. info_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  230. else
  231. info_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Visible));
  232. }
  233. else if (event.GetTargetElement()->GetId() == "outlines-button")
  234. {
  235. render_outlines = !render_outlines;
  236. }
  237. }
  238. }
  239. Plugin* Plugin::GetInstance()
  240. {
  241. return instance;
  242. }
  243. bool Plugin::LoadFont()
  244. {
  245. const Core::String font_family_name = "rmlui-debugger-font";
  246. return (Core::LoadFontFace(courier_prime_code, sizeof(courier_prime_code)/sizeof(courier_prime_code[0]), font_family_name, Core::Style::FontStyle::Normal, Core::Style::FontWeight::Normal) &&
  247. Core::LoadFontFace(courier_prime_code_italic, sizeof(courier_prime_code_italic)/sizeof(courier_prime_code_italic[0]), font_family_name, Core::Style::FontStyle::Italic, Core::Style::FontWeight::Normal));
  248. }
  249. bool Plugin::LoadMenuElement()
  250. {
  251. menu_element = host_context->CreateDocument();
  252. if (!menu_element)
  253. return false;
  254. menu_element->SetId("rmlui-debug-menu");
  255. menu_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  256. menu_element->SetInnerRML(menu_rml);
  257. Core::SharedPtr<Core::StyleSheet> style_sheet = Core::Factory::InstanceStyleSheetString(menu_rcss);
  258. if (!style_sheet)
  259. {
  260. host_context->UnloadDocument(menu_element);
  261. menu_element = nullptr;
  262. return false;
  263. }
  264. menu_element->SetStyleSheet(std::move(style_sheet));
  265. // Set the version info in the menu.
  266. menu_element->GetElementById("version-number")->SetInnerRML("v" + Rml::Core::GetVersion());
  267. // Attach to the buttons.
  268. Core::Element* event_log_button = menu_element->GetElementById("event-log-button");
  269. event_log_button->AddEventListener(Rml::Core::EventId::Click, this);
  270. Core::Element* element_info_button = menu_element->GetElementById("debug-info-button");
  271. element_info_button->AddEventListener(Rml::Core::EventId::Click, this);
  272. Core::Element* outlines_button = menu_element->GetElementById("outlines-button");
  273. outlines_button->AddEventListener(Rml::Core::EventId::Click, this);
  274. return true;
  275. }
  276. bool Plugin::LoadInfoElement()
  277. {
  278. info_element_instancer = std::make_unique< Core::ElementInstancerGeneric<ElementInfo> >();
  279. Core::Factory::RegisterElementInstancer("debug-info", info_element_instancer.get());
  280. info_element = rmlui_dynamic_cast< ElementInfo* >(host_context->CreateDocument("debug-info"));
  281. if (!info_element)
  282. return false;
  283. info_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  284. if (!info_element->Initialise())
  285. {
  286. host_context->UnloadDocument(info_element);
  287. info_element = nullptr;
  288. return false;
  289. }
  290. return true;
  291. }
  292. bool Plugin::LoadLogElement()
  293. {
  294. log_element_instancer = std::make_unique< Core::ElementInstancerGeneric<ElementLog> >();
  295. Core::Factory::RegisterElementInstancer("debug-log", log_element_instancer.get());
  296. log_element = rmlui_dynamic_cast< ElementLog* >(host_context->CreateDocument("debug-log"));
  297. if (!log_element)
  298. return false;
  299. log_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  300. if (!log_element->Initialise())
  301. {
  302. host_context->UnloadDocument(log_element);
  303. log_element = nullptr;
  304. return false;
  305. }
  306. // Make the system interface; this will trap the log messages for us.
  307. application_interface = Core::GetSystemInterface();
  308. log_interface = std::make_unique<SystemInterface>(application_interface, log_element);
  309. Core::SetSystemInterface(log_interface.get());
  310. return true;
  311. }
  312. void Plugin::ReleaseElements()
  313. {
  314. if (host_context)
  315. {
  316. if (menu_element)
  317. {
  318. host_context->UnloadDocument(menu_element);
  319. menu_element = nullptr;
  320. }
  321. if (info_element)
  322. {
  323. host_context->UnloadDocument(info_element);
  324. info_element = nullptr;
  325. }
  326. if (log_element)
  327. {
  328. host_context->UnloadDocument(log_element);
  329. log_element = nullptr;
  330. Core::SetSystemInterface(application_interface);
  331. application_interface = nullptr;
  332. log_interface.reset();
  333. }
  334. }
  335. if (debug_context)
  336. {
  337. if (hook_element)
  338. {
  339. debug_context->UnloadDocument(hook_element);
  340. hook_element = nullptr;
  341. }
  342. }
  343. }
  344. }
  345. }