Plugin.cpp 10 KB

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