Plugin.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "Plugin.h"
  28. #include <Rocket/Core/Types.h>
  29. #include <Rocket/Core.h>
  30. #include "ElementContextHook.h"
  31. #include "ElementInfo.h"
  32. #include "ElementLog.h"
  33. #include "FontSource.h"
  34. #include "Geometry.h"
  35. #include "MenuSource.h"
  36. #include "SystemInterface.h"
  37. #include <stack>
  38. namespace Rocket {
  39. namespace Debugger {
  40. Plugin::Plugin()
  41. {
  42. host_context = NULL;
  43. debug_context = NULL;
  44. log_hook = NULL;
  45. menu_element = NULL;
  46. info_element = NULL;
  47. log_element = NULL;
  48. render_outlines = false;
  49. }
  50. Plugin::~Plugin()
  51. {
  52. }
  53. // Initialises the debugging tools into the given context.
  54. bool Plugin::Initialise(Core::Context* context)
  55. {
  56. host_context = context;
  57. Geometry::SetContext(context);
  58. if (!LoadFont())
  59. {
  60. Core::Log::Message(Core::Log::LT_ERROR, "Failed to initialise debugger, unable to load font.");
  61. return false;
  62. }
  63. if (!LoadMenuElement() ||
  64. !LoadInfoElement() ||
  65. !LoadLogElement())
  66. {
  67. Core::Log::Message(Core::Log::LT_ERROR, "Failed to initialise debugger, error while load debugger elements.");
  68. return false;
  69. }
  70. Core::Factory::RegisterElementInstancer("debug-hook", new Core::ElementInstancerGeneric< ElementContextHook >())->RemoveReference();
  71. return true;
  72. }
  73. // Sets the context to be debugged.
  74. bool Plugin::SetContext(Core::Context* context)
  75. {
  76. // Remove the debug hook from the old context.
  77. if (debug_context != NULL &&
  78. hook_element != NULL)
  79. {
  80. debug_context->UnloadDocument(hook_element);
  81. hook_element->RemoveReference();
  82. hook_element = NULL;
  83. }
  84. // Add the debug hook into the new context.
  85. if (context != NULL)
  86. {
  87. Core::ElementDocument* element = context->CreateDocument("debug-hook");
  88. if (element == NULL)
  89. return false;
  90. hook_element = dynamic_cast< ElementContextHook* >(element);
  91. if (hook_element == NULL)
  92. {
  93. element->RemoveReference();
  94. context->UnloadDocument(element);
  95. return false;
  96. }
  97. hook_element->Initialise(this);
  98. }
  99. // Attach the info element to the new context.
  100. if (info_element != NULL)
  101. {
  102. if (debug_context != NULL)
  103. {
  104. debug_context->RemoveEventListener("click", info_element, true);
  105. debug_context->RemoveEventListener("mouseover", info_element, true);
  106. }
  107. if (context != NULL)
  108. {
  109. context->AddEventListener("click", info_element, true);
  110. context->AddEventListener("mouseover", info_element, true);
  111. }
  112. info_element->Reset();
  113. }
  114. debug_context = context;
  115. return true;
  116. }
  117. // Sets the visibility of the debugger.
  118. void Plugin::SetVisible(bool visibility)
  119. {
  120. if (visibility)
  121. menu_element->SetProperty("visibility", "visible");
  122. else
  123. menu_element->SetProperty("visibility", "hidden");
  124. }
  125. // Returns the visibility of the debugger.
  126. bool Plugin::IsVisible()
  127. {
  128. return menu_element->IsVisible();
  129. }
  130. // Renders any debug elements in the debug context.
  131. void Plugin::Render()
  132. {
  133. // Render the outlines of the debug context's elements.
  134. if (render_outlines &&
  135. debug_context != NULL)
  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("rkt-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. for (int j = 0; j < element->GetNumBoxes(); ++j)
  149. {
  150. const Core::Box& box = element->GetBox(j);
  151. Geometry::RenderOutline(element->GetAbsoluteOffset(Core::Box::BORDER) + box.GetPosition(Core::Box::BORDER), box.GetSize(Core::Box::BORDER), Core::Colourb(255, 0, 0, 128), 1);
  152. }
  153. for (int j = 0; j < element->GetNumChildren(); ++j)
  154. element_stack.push(element->GetChild(j));
  155. }
  156. }
  157. }
  158. // Render the info element's boxes.
  159. if (info_element != NULL &&
  160. info_element->IsVisible())
  161. {
  162. info_element->RenderHoverElement();
  163. info_element->RenderSourceElement();
  164. }
  165. }
  166. // Called when Rocket shuts down.
  167. void Plugin::OnShutdown()
  168. {
  169. // Release the elements before we leak track, this ensures the debugger hook has been cleared
  170. // and that we don't try send the messages to the debug log window
  171. ReleaseElements();
  172. if (!elements.empty())
  173. {
  174. Core::Log::Message(Core::Log::LT_WARNING, "%u leaked elements detected.", elements.size());
  175. int count = 0;
  176. for (ElementInstanceMap::iterator i = elements.begin(); i != elements.end(); ++i)
  177. Core::Log::Message(Core::Log::LT_WARNING, "\t(%d) %s -> %s", count++, (*i)->GetTagName().CString(), (*i)->GetAddress().CString());
  178. }
  179. delete this;
  180. }
  181. // Called whenever a Rocket context is destroyed.
  182. void Plugin::OnContextDestroy(Core::Context* context)
  183. {
  184. if (context == debug_context)
  185. {
  186. // The context we're debugging is being destroyed, so we need to remove our debug hook elements.
  187. SetContext(NULL);
  188. }
  189. if (context == host_context)
  190. {
  191. // Our host is being destroyed, so we need to shut down the debugger.
  192. ReleaseElements();
  193. Geometry::SetContext(NULL);
  194. context = NULL;
  195. }
  196. }
  197. // Called whenever an element is created.
  198. void Plugin::OnElementCreate(Core::Element* element)
  199. {
  200. // Store the stack addresses for this frame.
  201. elements.insert(element);
  202. }
  203. // Called whenever an element is destroyed.
  204. void Plugin::OnElementDestroy(Core::Element* element)
  205. {
  206. elements.erase(element);
  207. if (info_element != NULL)
  208. info_element->OnElementDestroy(element);
  209. }
  210. // Event handler for events from the debugger elements.
  211. void Plugin::ProcessEvent(Core::Event& event)
  212. {
  213. if (event == "click")
  214. {
  215. if (event.GetTargetElement()->GetId() == "event-log-button")
  216. {
  217. if (log_element->IsVisible())
  218. log_element->SetProperty("visibility", "hidden");
  219. else
  220. log_element->SetProperty("visibility", "visible");
  221. }
  222. else if (event.GetTargetElement()->GetId() == "debug-info-button")
  223. {
  224. if (info_element->IsVisible())
  225. info_element->SetProperty("visibility", "hidden");
  226. else
  227. info_element->SetProperty("visibility", "visible");
  228. }
  229. else if (event.GetTargetElement()->GetId() == "outlines-button")
  230. {
  231. render_outlines = !render_outlines;
  232. }
  233. }
  234. }
  235. bool Plugin::LoadFont()
  236. {
  237. return (Core::FontDatabase::LoadFontFace(lacuna_regular, sizeof(lacuna_regular) / sizeof(unsigned char), "Lacuna", Core::Font::STYLE_NORMAL, Core::Font::WEIGHT_NORMAL) &&
  238. Core::FontDatabase::LoadFontFace(lacuna_italic, sizeof(lacuna_italic) / sizeof(unsigned char), "Lacuna", Core::Font::STYLE_ITALIC, Core::Font::WEIGHT_NORMAL));
  239. }
  240. bool Plugin::LoadMenuElement()
  241. {
  242. menu_element = host_context->CreateDocument();
  243. if (menu_element == NULL)
  244. return false;
  245. menu_element->SetId("rkt-debug-menu");
  246. menu_element->SetProperty("visibility", "hidden");
  247. menu_element->SetInnerRML(menu_rml);
  248. // Remove our reference on the document.
  249. menu_element->RemoveReference();
  250. Core::StyleSheet* style_sheet = Core::Factory::InstanceStyleSheetString(menu_rcss);
  251. if (style_sheet == NULL)
  252. {
  253. host_context->UnloadDocument(menu_element);
  254. menu_element = NULL;
  255. return false;
  256. }
  257. menu_element->SetStyleSheet(style_sheet);
  258. style_sheet->RemoveReference();
  259. menu_element->AddReference();
  260. // Set the version info in the menu.
  261. menu_element->GetElementById("version-number")->SetInnerRML("v" + Rocket::Core::GetVersion());
  262. // Attach to the buttons.
  263. Core::Element* event_log_button = menu_element->GetElementById("event-log-button");
  264. event_log_button->AddEventListener("click", this);
  265. Core::Element* element_info_button = menu_element->GetElementById("debug-info-button");
  266. element_info_button->AddEventListener("click", this);
  267. Core::Element* outlines_button = menu_element->GetElementById("outlines-button");
  268. outlines_button->AddEventListener("click", this);
  269. return true;
  270. }
  271. bool Plugin::LoadInfoElement()
  272. {
  273. Core::Factory::RegisterElementInstancer("debug-info", new Core::ElementInstancerGeneric< ElementInfo >())->RemoveReference();
  274. info_element = dynamic_cast< ElementInfo* >(host_context->CreateDocument("debug-info"));
  275. if (info_element == NULL)
  276. return false;
  277. info_element->SetProperty("visibility", "hidden");
  278. if (!info_element->Initialise())
  279. {
  280. info_element->RemoveReference();
  281. host_context->UnloadDocument(info_element);
  282. info_element = NULL;
  283. return false;
  284. }
  285. return true;
  286. }
  287. bool Plugin::LoadLogElement()
  288. {
  289. Core::Factory::RegisterElementInstancer("debug-log", new Core::ElementInstancerGeneric< ElementLog >())->RemoveReference();
  290. log_element = dynamic_cast< ElementLog* >(host_context->CreateDocument("debug-log"));
  291. if (log_element == NULL)
  292. return false;
  293. log_element->SetProperty("visibility", "hidden");
  294. if (!log_element->Initialise())
  295. {
  296. log_element->RemoveReference();
  297. host_context->UnloadDocument(log_element);
  298. log_element = NULL;
  299. return false;
  300. }
  301. // Make the system interface; this will trap the log messages for us.
  302. log_hook = new SystemInterface(log_element);
  303. return true;
  304. }
  305. void Plugin::ReleaseElements()
  306. {
  307. if (menu_element)
  308. {
  309. menu_element->RemoveReference();
  310. menu_element = NULL;
  311. }
  312. if (info_element)
  313. {
  314. info_element->RemoveReference();
  315. info_element = NULL;
  316. }
  317. if (log_element)
  318. {
  319. log_element->RemoveReference();
  320. log_element = NULL;
  321. delete log_hook;
  322. }
  323. if (hook_element)
  324. {
  325. hook_element->RemoveReference();
  326. hook_element = NULL;
  327. }
  328. }
  329. }
  330. }