ElementInfo.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 "ElementInfo.h"
  29. #include "../../Include/RmlUi/Core.h"
  30. #include "../../Include/RmlUi/Core/Property.h"
  31. #include "../../Include/RmlUi/Core/PropertiesIteratorView.h"
  32. #include "../../Include/RmlUi/Core/Factory.h"
  33. #include "../../Include/RmlUi/Core/StyleSheet.h"
  34. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  35. #include "Geometry.h"
  36. #include "CommonSource.h"
  37. #include "InfoSource.h"
  38. #include <map>
  39. namespace Rml {
  40. namespace Debugger {
  41. ElementInfo::ElementInfo(const Core::String& tag) : Core::ElementDocument(tag)
  42. {
  43. hover_element = nullptr;
  44. source_element = nullptr;
  45. previous_update_time = 0.0;
  46. }
  47. ElementInfo::~ElementInfo()
  48. {
  49. }
  50. // Initialises the info element.
  51. bool ElementInfo::Initialise()
  52. {
  53. SetInnerRML(info_rml);
  54. SetId("rmlui-debug-info");
  55. AddEventListener(Core::EventId::Click, this);
  56. AddEventListener(Core::EventId::Mouseover, this);
  57. Core::SharedPtr<Core::StyleSheet> style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(info_rcss));
  58. if (!style_sheet)
  59. return false;
  60. SetStyleSheet(std::move(style_sheet));
  61. return true;
  62. }
  63. // Clears the element references.
  64. void ElementInfo::Reset()
  65. {
  66. hover_element = nullptr;
  67. SetSourceElement(nullptr);
  68. }
  69. void ElementInfo::OnUpdate()
  70. {
  71. if (source_element && IsVisible())
  72. {
  73. const double t = Core::GetSystemInterface()->GetElapsedTime();
  74. const float dt = (float)(t - previous_update_time);
  75. constexpr float update_interval = 0.3f;
  76. if (dt > update_interval)
  77. {
  78. UpdateSourceElement();
  79. }
  80. }
  81. }
  82. // Called when an element is destroyed.
  83. void ElementInfo::OnElementDestroy(Core::Element* element)
  84. {
  85. if (hover_element == element)
  86. hover_element = nullptr;
  87. if (source_element == element)
  88. source_element = nullptr;
  89. }
  90. void ElementInfo::RenderHoverElement()
  91. {
  92. if (hover_element)
  93. {
  94. Core::ElementUtilities::ApplyTransform(*hover_element);
  95. for (int i = 0; i < hover_element->GetNumBoxes(); i++)
  96. {
  97. // Render the content area.
  98. const Core::Box element_box = hover_element->GetBox(i);
  99. Geometry::RenderOutline(
  100. hover_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::BORDER),
  101. element_box.GetSize(Core::Box::BORDER),
  102. Core::Colourb(255, 0, 0, 255),
  103. 1
  104. );
  105. }
  106. Core::ElementUtilities::UnapplyTransform(*hover_element);
  107. }
  108. }
  109. void ElementInfo::RenderSourceElement()
  110. {
  111. if (source_element)
  112. {
  113. Core::ElementUtilities::ApplyTransform(*source_element);
  114. for (int i = 0; i < source_element->GetNumBoxes(); i++)
  115. {
  116. const Core::Box element_box = source_element->GetBox(i);
  117. // Content area:
  118. Geometry::RenderBox(source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::CONTENT), element_box.GetSize(), Core::Colourb(158, 214, 237, 128));
  119. // Padding area:
  120. Geometry::RenderBox(source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::PADDING), element_box.GetSize(Core::Box::PADDING), source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::CONTENT), element_box.GetSize(), Core::Colourb(135, 122, 214, 128));
  121. // Border area:
  122. Geometry::RenderBox(source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::BORDER), element_box.GetSize(Core::Box::BORDER), source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::PADDING), element_box.GetSize(Core::Box::PADDING), Core::Colourb(133, 133, 133, 128));
  123. // Border area:
  124. Geometry::RenderBox(source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::MARGIN), element_box.GetSize(Core::Box::MARGIN), source_element->GetAbsoluteOffset(Core::Box::BORDER) + element_box.GetPosition(Core::Box::BORDER), element_box.GetSize(Core::Box::BORDER), Core::Colourb(240, 255, 131, 128));
  125. }
  126. Core::ElementUtilities::UnapplyTransform(*source_element);
  127. }
  128. }
  129. void ElementInfo::ProcessEvent(Core::Event& event)
  130. {
  131. // Only process events if we're visible
  132. if (IsVisible())
  133. {
  134. if (event == Core::EventId::Click)
  135. {
  136. Core::Element* target_element = event.GetTargetElement();
  137. // Deal with clicks on our own elements differently.
  138. if (target_element->GetOwnerDocument() == this)
  139. {
  140. // If it's a pane title, then we need to toggle the visibility of its sibling (the contents pane underneath it).
  141. if (target_element->GetTagName() == "h2")
  142. {
  143. Core::Element* panel = target_element->GetNextSibling();
  144. if (panel->IsVisible())
  145. panel->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::None));
  146. else
  147. panel->SetProperty(Core::PropertyId::Display, Core::Property(Core::Style::Display::Block));
  148. event.StopPropagation();
  149. }
  150. else if (event.GetTargetElement()->GetId() == "close_button")
  151. {
  152. if (IsVisible())
  153. SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
  154. }
  155. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  156. else
  157. {
  158. int element_index;
  159. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  160. {
  161. Core::Element* new_source_element = source_element;
  162. for (int i = 0; i < element_index; i++)
  163. {
  164. if (new_source_element != nullptr)
  165. new_source_element = new_source_element->GetParentNode();
  166. }
  167. SetSourceElement(new_source_element);
  168. }
  169. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  170. {
  171. if (source_element != nullptr)
  172. SetSourceElement(source_element->GetChild(element_index));
  173. }
  174. event.StopPropagation();
  175. }
  176. }
  177. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  178. else if (target_element->GetOwnerDocument() != nullptr && !IsDebuggerElement(target_element))
  179. {
  180. Core::Element* new_source_element = target_element;
  181. if (new_source_element != source_element)
  182. {
  183. SetSourceElement(new_source_element);
  184. event.StopPropagation();
  185. }
  186. }
  187. }
  188. else if (event == Core::EventId::Mouseover)
  189. {
  190. Core::Element* target_element = event.GetTargetElement();
  191. // Deal with clicks on our own elements differently.
  192. Core::ElementDocument* owner_document = target_element->GetOwnerDocument();
  193. if (owner_document == this)
  194. {
  195. // Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
  196. int element_index;
  197. if (sscanf(target_element->GetId().c_str(), "a %d", &element_index) == 1)
  198. {
  199. hover_element = source_element;
  200. for (int i = 0; i < element_index; i++)
  201. {
  202. if (hover_element != nullptr)
  203. hover_element = hover_element->GetParentNode();
  204. }
  205. }
  206. else if (sscanf(target_element->GetId().c_str(), "c %d", &element_index) == 1)
  207. {
  208. if (source_element != nullptr)
  209. hover_element = source_element->GetChild(element_index);
  210. }
  211. }
  212. // Otherwise we just want to focus on the clicked element (unless it's on a debug element)
  213. else if (owner_document != nullptr && owner_document->GetId().find("rmlui-debug-") != 0)
  214. {
  215. hover_element = target_element;
  216. }
  217. }
  218. }
  219. }
  220. void ElementInfo::SetSourceElement(Core::Element* new_source_element)
  221. {
  222. source_element = new_source_element;
  223. UpdateSourceElement();
  224. }
  225. void ElementInfo::UpdateSourceElement()
  226. {
  227. previous_update_time = Core::GetSystemInterface()->GetElapsedTime();
  228. // Set the title:
  229. Core::Element* title_content = GetElementById("title-content");
  230. if (title_content != nullptr)
  231. {
  232. if (source_element != nullptr)
  233. title_content->SetInnerRML(source_element->GetTagName());
  234. else
  235. title_content->SetInnerRML("Element Information");
  236. }
  237. // Set the attributes:
  238. Core::Element* attributes_content = GetElementById("attributes-content");
  239. if (attributes_content)
  240. {
  241. Core::String attributes;
  242. if (source_element != nullptr)
  243. {
  244. {
  245. Core::String name;
  246. Core::String value;
  247. // The element's attribute list is not always synchronized with its internal values, fetch
  248. // them manually here (see e.g. Element::OnAttributeChange for relevant attributes)
  249. {
  250. name = "id";
  251. value = source_element->GetId();
  252. if (!value.empty())
  253. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  254. }
  255. {
  256. name = "class";
  257. value = source_element->GetClassNames();
  258. if (!value.empty())
  259. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  260. }
  261. {
  262. // Not actually an attribute, but may be useful
  263. name = "pseudo";
  264. value.clear();
  265. for (auto str : source_element->GetActivePseudoClasses())
  266. value += " :" + str;
  267. if (!value.empty())
  268. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  269. }
  270. }
  271. for(const auto& pair : source_element->GetAttributes())
  272. {
  273. auto& name = pair.first;
  274. auto& variant = pair.second;
  275. Core::String value = variant.Get<Core::String>();
  276. if(name != "class" && name != "style" && name != "id")
  277. attributes += Core::CreateString(name.size() + value.size() + 32, "%s: <em>%s</em><br />", name.c_str(), value.c_str());
  278. }
  279. }
  280. if (attributes.empty())
  281. {
  282. while (attributes_content->HasChildNodes())
  283. attributes_content->RemoveChild(attributes_content->GetChild(0));
  284. }
  285. else if (attributes != attributes_rml)
  286. {
  287. attributes_content->SetInnerRML(attributes);
  288. attributes_rml = std::move(attributes);
  289. }
  290. }
  291. // Set the properties:
  292. Core::Element* properties_content = GetElementById("properties-content");
  293. if (properties_content)
  294. {
  295. Core::String properties;
  296. if (source_element != nullptr)
  297. BuildElementPropertiesRML(properties, source_element, source_element);
  298. if (properties.empty())
  299. {
  300. while (properties_content->HasChildNodes())
  301. properties_content->RemoveChild(properties_content->GetChild(0));
  302. }
  303. else if (properties != properties_rml)
  304. {
  305. properties_content->SetInnerRML(properties);
  306. properties_rml = std::move(properties);
  307. }
  308. }
  309. // Set the events:
  310. Core::Element* events_content = GetElementById("events-content");
  311. if (events_content)
  312. {
  313. Core::String events;
  314. if (source_element != nullptr)
  315. {
  316. events = source_element->GetEventDispatcherSummary();
  317. }
  318. if (events.empty())
  319. {
  320. while (events_content->HasChildNodes())
  321. events_content->RemoveChild(events_content->GetChild(0));
  322. }
  323. else if (events != events_rml)
  324. {
  325. events_content->SetInnerRML(events);
  326. events_rml = std::move(events);
  327. }
  328. }
  329. // Set the position:
  330. Core::Element* position_content = GetElementById("position-content");
  331. if (position_content)
  332. {
  333. // left, top, width, height.
  334. if (source_element != nullptr)
  335. {
  336. Core::Vector2f element_offset = source_element->GetRelativeOffset(Core::Box::BORDER);
  337. Core::Vector2f element_size = source_element->GetBox().GetSize(Core::Box::BORDER);
  338. Core::String positions;
  339. positions += Core::CreateString(64, "left: <em>%.0fpx</em><br />", element_offset.x);
  340. positions += Core::CreateString(64, "top: <em>%.0fpx</em><br />", element_offset.y);
  341. positions += Core::CreateString(64, "width: <em>%.0fpx</em><br />", element_size.x);
  342. positions += Core::CreateString(64, "height: <em>%.0fpx</em><br />", element_size.y);
  343. position_content->SetInnerRML(positions);
  344. }
  345. else
  346. {
  347. while (position_content->HasChildNodes())
  348. position_content->RemoveChild(position_content->GetFirstChild());
  349. }
  350. }
  351. // Set the ancestors:
  352. Core::Element* ancestors_content = GetElementById("ancestors-content");
  353. if (ancestors_content)
  354. {
  355. Core::String ancestors;
  356. Core::Element* element_ancestor = nullptr;
  357. if (source_element != nullptr)
  358. element_ancestor = source_element->GetParentNode();
  359. int ancestor_depth = 1;
  360. while (element_ancestor)
  361. {
  362. Core::String ancestor_name = element_ancestor->GetAddress(false, false);
  363. ancestors += Core::CreateString(ancestor_name.size() + 32, "<p id=\"a %d\">%s</p>", ancestor_depth, ancestor_name.c_str());
  364. element_ancestor = element_ancestor->GetParentNode();
  365. ancestor_depth++;
  366. }
  367. if (ancestors.empty())
  368. {
  369. while (ancestors_content->HasChildNodes())
  370. ancestors_content->RemoveChild(ancestors_content->GetFirstChild());
  371. }
  372. else if (ancestors != ancestors_rml)
  373. {
  374. ancestors_content->SetInnerRML(ancestors);
  375. ancestors_rml = std::move(ancestors);
  376. }
  377. }
  378. // Set the children:
  379. Core::Element* children_content = GetElementById("children-content");
  380. if (children_content)
  381. {
  382. Core::String children;
  383. if (source_element != nullptr)
  384. {
  385. for (int i = 0; i < source_element->GetNumChildren(); i++)
  386. {
  387. Core::Element* child = source_element->GetChild(i);
  388. // If this is a debugger document, do not show it.
  389. if (IsDebuggerElement(child))
  390. continue;
  391. Core::String child_name = child->GetTagName();
  392. const Core::String child_id = child->GetId();
  393. if (!child_id.empty())
  394. {
  395. child_name += "#";
  396. child_name += child_id;
  397. }
  398. children += Core::CreateString(child_name.size() + 32, "<p id=\"c %d\">%s</p>", i, child_name.c_str());
  399. }
  400. }
  401. if (children.empty())
  402. {
  403. while (children_content->HasChildNodes())
  404. children_content->RemoveChild(children_content->GetChild(0));
  405. }
  406. else if(children != children_rml)
  407. {
  408. children_content->SetInnerRML(children);
  409. children_rml = std::move(children);
  410. }
  411. }
  412. }
  413. void ElementInfo::BuildElementPropertiesRML(Core::String& property_rml, Core::Element* element, Core::Element* primary_element)
  414. {
  415. NamedPropertyList property_list;
  416. for(auto it = element->IterateLocalProperties(); !it.AtEnd(); ++it)
  417. {
  418. Core::PropertyId property_id = it.GetId();
  419. const Core::String& property_name = it.GetName();
  420. const Core::Property* prop = &it.GetProperty();
  421. // Check that this property isn't overridden or just not inherited.
  422. if (primary_element->GetLocalProperty(property_id) != prop)
  423. continue;
  424. property_list.push_back(NamedProperty{ property_name, prop });
  425. }
  426. std::sort(property_list.begin(), property_list.end(),
  427. [](const NamedProperty& a, const NamedProperty& b) {
  428. if (a.second->source && !b.second->source) return false;
  429. if (!a.second->source && b.second->source) return true;
  430. return a.second->specificity > b.second->specificity;
  431. }
  432. );
  433. if (!property_list.empty())
  434. {
  435. // Print the 'inherited from ...' header if we're not the primary element.
  436. if (element != primary_element)
  437. {
  438. property_rml += "<h3 class='mark'>inherited from " + element->GetAddress(false, false) + "</h3>";
  439. }
  440. const Core::PropertySource* previous_source = nullptr;
  441. bool first_iteration = true;
  442. for (auto& named_property : property_list)
  443. {
  444. auto& source = named_property.second->source;
  445. if(source.get() != previous_source || first_iteration)
  446. {
  447. previous_source = source.get();
  448. first_iteration = false;
  449. // Print the rule name header.
  450. if(source)
  451. {
  452. Core::String str_line_number;
  453. Core::TypeConverter<int, Core::String>::Convert(source->line_number, str_line_number);
  454. property_rml += "<h3>" + source->rule_name + "</h3>";
  455. property_rml += "<h4>" + source->path + " : " + str_line_number + "</h4>";
  456. }
  457. else
  458. {
  459. property_rml += "<h3><em>inline</em></h3>";
  460. }
  461. }
  462. BuildPropertyRML(property_rml, named_property.first, named_property.second);
  463. }
  464. }
  465. if (element->GetParentNode() != nullptr)
  466. BuildElementPropertiesRML(property_rml, element->GetParentNode(), primary_element);
  467. }
  468. void ElementInfo::BuildPropertyRML(Core::String& property_rml, const Core::String& name, const Core::Property* property)
  469. {
  470. Core::String property_value = property->ToString();
  471. RemoveTrailingZeroes(property_value);
  472. property_rml += Core::CreateString(name.size() + property_value.size() + 32, "%s: <em>%s;</em><br />", name.c_str(), property_value.c_str());
  473. }
  474. void ElementInfo::RemoveTrailingZeroes(Core::String& string)
  475. {
  476. if (string.empty())
  477. {
  478. return;
  479. }
  480. // First, check for a decimal point. No point, no chance of trailing zeroes!
  481. size_t decimal_point_position = string.find(".");
  482. if (decimal_point_position != Core::String::npos)
  483. {
  484. // Ok, so now we start at the back of the string and find the first
  485. // numeral. If the character we find is a zero, then we start counting
  486. // back till we find something that isn't a zero or a decimal point -
  487. // and then remove all that we've counted.
  488. size_t last_zero = string.size() - 1;
  489. while ((string[last_zero] < '0' || string[last_zero] > '9') && string[last_zero] != '.')
  490. {
  491. if (last_zero == 0)
  492. {
  493. return;
  494. }
  495. if (string[last_zero] == '.')
  496. {
  497. break;
  498. }
  499. last_zero--;
  500. }
  501. if (!(string[last_zero] == '0' || string[last_zero] == '.'))
  502. {
  503. return;
  504. }
  505. // Now find the first character that isn't a zero (unless we're just
  506. // chopping off the dangling decimal point)
  507. size_t first_zero = last_zero;
  508. if (string[last_zero] == '0')
  509. {
  510. while (first_zero > 0 && string[first_zero - 1] == '0')
  511. {
  512. first_zero--;
  513. }
  514. // Check for a preceeding decimal point - if it's all zeroes until the
  515. // decimal, then we should remove it too.
  516. if (string[first_zero - 1] == '.')
  517. {
  518. first_zero--;
  519. }
  520. }
  521. // Now remove everything between first_zero and last_zero, inclusive.
  522. if (last_zero > first_zero)
  523. string.erase(first_zero, (last_zero - first_zero) + 1);
  524. }
  525. }
  526. bool ElementInfo::IsDebuggerElement(Core::Element* element)
  527. {
  528. return element->GetOwnerDocument()->GetId().find("rmlui-debug-") == 0;
  529. }
  530. }
  531. }