Plugin.cpp 11 KB

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