Element.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Dictionary.h"
  31. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  32. #include "../../Include/RmlUi/Core/TransformPrimitive.h"
  33. #include <algorithm>
  34. #include <limits>
  35. #include "Clock.h"
  36. #include "ComputeProperty.h"
  37. #include "ElementAnimation.h"
  38. #include "ElementBackground.h"
  39. #include "ElementBorder.h"
  40. #include "ElementDefinition.h"
  41. #include "ElementStyle.h"
  42. #include "EventDispatcher.h"
  43. #include "EventSpecification.h"
  44. #include "ElementDecoration.h"
  45. #include "FontFaceHandle.h"
  46. #include "LayoutEngine.h"
  47. #include "PluginRegistry.h"
  48. #include "PropertiesIterator.h"
  49. #include "Pool.h"
  50. #include "StyleSheetParser.h"
  51. #include "XMLParseTools.h"
  52. #include "../../Include/RmlUi/Core/Core.h"
  53. namespace Rml {
  54. namespace Core {
  55. /**
  56. STL function object for sorting elements by z-type (ie, float-types before general types, etc).
  57. @author Peter Curry
  58. */
  59. class ElementSortZOrder
  60. {
  61. public:
  62. bool operator()(const std::pair< Element*, float >& lhs, const std::pair< Element*, float >& rhs) const
  63. {
  64. return lhs.second < rhs.second;
  65. }
  66. };
  67. /**
  68. STL function object for sorting elements by z-index property.
  69. @author Peter Curry
  70. */
  71. class ElementSortZIndex
  72. {
  73. public:
  74. bool operator()(const Element* lhs, const Element* rhs) const
  75. {
  76. // Check the z-index.
  77. return lhs->GetZIndex() < rhs->GetZIndex();
  78. }
  79. };
  80. // Determines how many levels up in the hierarchy the OnChildAdd and OnChildRemove are called (starting at the child itself)
  81. static constexpr int ChildNotifyLevels = 2;
  82. struct ElementMetaChunk;
  83. static Pool< ElementMetaChunk > element_meta_chunk_pool(200, true);
  84. // Meta objects for element collected in a single struct to reduce memory allocations
  85. struct ElementMeta
  86. {
  87. ElementMeta(Element* el) : event_dispatcher(el), style(el), background(el), border(el), decoration(el), scroll(el) {}
  88. EventDispatcher event_dispatcher;
  89. ElementStyle style;
  90. ElementBackground background;
  91. ElementBorder border;
  92. ElementDecoration decoration;
  93. ElementScroll scroll;
  94. Style::ComputedValues computed_values;
  95. void* operator new(size_t size)
  96. {
  97. void* memory = element_meta_chunk_pool.AllocateObject();
  98. return memory;
  99. }
  100. void operator delete(void* chunk)
  101. {
  102. element_meta_chunk_pool.DeallocateObject((ElementMetaChunk*)chunk);
  103. }
  104. };
  105. struct alignas(ElementMeta) ElementMetaChunk
  106. {
  107. ElementMetaChunk() { memset(buffer, 0, size); }
  108. static constexpr size_t size = sizeof(ElementMeta);
  109. char buffer[size];
  110. };
  111. /// Constructs a new RmlUi element.
  112. Element::Element(const String& tag) : tag(tag), relative_offset_base(0, 0), relative_offset_position(0, 0), absolute_offset(0, 0), scroll_offset(0, 0), content_offset(0, 0), content_box(0, 0),
  113. transform_state(), transform_state_perspective_dirty(true), transform_state_transform_dirty(true), transform_state_parent_transform_dirty(true), dirty_animation(false), dirty_transition(false)
  114. {
  115. RMLUI_ASSERT(tag == StringUtilities::ToLower(tag));
  116. parent = nullptr;
  117. focus = nullptr;
  118. instancer = nullptr;
  119. owner_document = nullptr;
  120. offset_fixed = false;
  121. offset_parent = nullptr;
  122. offset_dirty = true;
  123. client_area = Box::PADDING;
  124. num_non_dom_children = 0;
  125. visible = true;
  126. z_index = 0;
  127. local_stacking_context = false;
  128. local_stacking_context_forced = false;
  129. stacking_context_dirty = false;
  130. structure_dirty = false;
  131. computed_values_are_default_initialized = true;
  132. box_dirty = false;
  133. clipping_ignore_depth = 0;
  134. clipping_enabled = false;
  135. clipping_state_dirty = true;
  136. element_meta = new ElementMeta(this);
  137. event_dispatcher = &element_meta->event_dispatcher;
  138. style = &element_meta->style;
  139. background = &element_meta->background;
  140. border = &element_meta->border;
  141. decoration = &element_meta->decoration;
  142. scroll = &element_meta->scroll;
  143. }
  144. Element::~Element()
  145. {
  146. RMLUI_ASSERT(parent == nullptr);
  147. PluginRegistry::NotifyElementDestroy(this);
  148. // Remove scrollbar elements before we delete the children!
  149. scroll->ClearScrollbars();
  150. // A simplified version of RemoveChild() for destruction.
  151. for (ElementPtr& child : children)
  152. {
  153. Element* child_ancestor = child.get();
  154. for (int i = 0; i <= ChildNotifyLevels && child_ancestor; i++, child_ancestor = child_ancestor->GetParentNode())
  155. child_ancestor->OnChildRemove(child.get());
  156. child->SetParent(nullptr);
  157. }
  158. children.clear();
  159. num_non_dom_children = 0;
  160. delete element_meta;
  161. }
  162. void Element::Update(float dp_ratio)
  163. {
  164. OnUpdate();
  165. UpdateStructure();
  166. UpdateTransition();
  167. UpdateAnimation();
  168. AdvanceAnimations();
  169. scroll->Update();
  170. UpdateProperties();
  171. if (box_dirty)
  172. {
  173. box_dirty = false;
  174. OnResize();
  175. }
  176. UpdateTransformState();
  177. for (size_t i = 0; i < children.size(); i++)
  178. children[i]->Update(dp_ratio);
  179. }
  180. void Element::UpdateProperties()
  181. {
  182. style->UpdateDefinition();
  183. if (style->AnyPropertiesDirty())
  184. {
  185. const ComputedValues* parent_values = nullptr;
  186. if (parent)
  187. parent_values = &parent->GetComputedValues();
  188. const ComputedValues* document_values = nullptr;
  189. float dp_ratio = 1.0f;
  190. if (auto doc = GetOwnerDocument())
  191. {
  192. document_values = &doc->GetComputedValues();
  193. if (Context * context = doc->GetContext())
  194. dp_ratio = context->GetDensityIndependentPixelRatio();
  195. }
  196. // Compute values and clear dirty properties
  197. PropertyIdSet dirty_properties = style->ComputeValues(element_meta->computed_values, parent_values, document_values, computed_values_are_default_initialized, dp_ratio);
  198. computed_values_are_default_initialized = false;
  199. // Computed values are just calculated and can safely be used in OnPropertyChange.
  200. // However, new properties set during this call will not be available until the next update loop.
  201. if (!dirty_properties.Empty())
  202. OnPropertyChange(dirty_properties);
  203. }
  204. }
  205. void Element::Render()
  206. {
  207. // Rebuild our stacking context if necessary.
  208. if (stacking_context_dirty)
  209. BuildLocalStackingContext();
  210. // Apply our transform
  211. ElementUtilities::ApplyTransform(*this);
  212. // Render all elements in our local stacking context that have a z-index beneath our local index of 0.
  213. size_t i = 0;
  214. for (; i < stacking_context.size() && stacking_context[i]->z_index < 0; ++i)
  215. stacking_context[i]->Render();
  216. // Set up the clipping region for this element.
  217. if (ElementUtilities::SetClippingRegion(this))
  218. {
  219. background->RenderBackground();
  220. border->RenderBorder();
  221. decoration->RenderDecorators();
  222. OnRender();
  223. }
  224. // Render the rest of the elements in the stacking context.
  225. for (; i < stacking_context.size(); ++i)
  226. stacking_context[i]->Render();
  227. // Unapply our transform
  228. ElementUtilities::UnapplyTransform(*this);
  229. }
  230. // Clones this element, returning a new, unparented element.
  231. ElementPtr Element::Clone() const
  232. {
  233. ElementPtr clone;
  234. if (instancer)
  235. {
  236. clone = instancer->InstanceElement(nullptr, GetTagName(), attributes);
  237. if (clone)
  238. clone->SetInstancer(instancer);
  239. }
  240. else
  241. clone = Factory::InstanceElement(nullptr, GetTagName(), GetTagName(), attributes);
  242. if (clone != nullptr)
  243. {
  244. String inner_rml;
  245. GetInnerRML(inner_rml);
  246. clone->SetInnerRML(inner_rml);
  247. }
  248. return clone;
  249. }
  250. // Sets or removes a class on the element.
  251. void Element::SetClass(const String& class_name, bool activate)
  252. {
  253. style->SetClass(class_name, activate);
  254. }
  255. // Checks if a class is set on the element.
  256. bool Element::IsClassSet(const String& class_name) const
  257. {
  258. return style->IsClassSet(class_name);
  259. }
  260. // Specifies the entire list of classes for this element. This will replace any others specified.
  261. void Element::SetClassNames(const String& class_names)
  262. {
  263. SetAttribute("class", class_names);
  264. }
  265. /// Return the active class list
  266. String Element::GetClassNames() const
  267. {
  268. return style->GetClassNames();
  269. }
  270. // Returns the active style sheet for this element. This may be nullptr.
  271. const SharedPtr<StyleSheet>& Element::GetStyleSheet() const
  272. {
  273. if (ElementDocument * document = GetOwnerDocument())
  274. return document->GetStyleSheet();
  275. static SharedPtr<StyleSheet> null_style_sheet;
  276. return null_style_sheet;
  277. }
  278. // Returns the element's definition.
  279. const ElementDefinition* Element::GetDefinition()
  280. {
  281. return style->GetDefinition();
  282. }
  283. // Fills an String with the full address of this element.
  284. String Element::GetAddress(bool include_pseudo_classes, bool include_parents) const
  285. {
  286. // Add the tag name onto the address.
  287. String address(tag);
  288. // Add the ID if we have one.
  289. if (!id.empty())
  290. {
  291. address += "#";
  292. address += id;
  293. }
  294. String classes = style->GetClassNames();
  295. if (!classes.empty())
  296. {
  297. classes = StringUtilities::Replace(classes, ' ', '.');
  298. address += ".";
  299. address += classes;
  300. }
  301. if (include_pseudo_classes)
  302. {
  303. const PseudoClassList& pseudo_classes = style->GetActivePseudoClasses();
  304. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  305. {
  306. address += ":";
  307. address += (*i);
  308. }
  309. }
  310. if (include_parents && parent)
  311. {
  312. address += " < ";
  313. return address + parent->GetAddress(true, true);
  314. }
  315. else
  316. return address;
  317. }
  318. // Sets the position of this element, as a two-dimensional offset from another element.
  319. void Element::SetOffset(const Vector2f& offset, Element* _offset_parent, bool _offset_fixed)
  320. {
  321. _offset_fixed |= GetPosition() == Style::Position::Fixed;
  322. // If our offset has definitely changed, or any of our parenting has, then these are set and
  323. // updated based on our left / right / top / bottom properties.
  324. if (relative_offset_base != offset ||
  325. offset_parent != _offset_parent ||
  326. offset_fixed != _offset_fixed)
  327. {
  328. relative_offset_base = offset;
  329. offset_fixed = _offset_fixed;
  330. offset_parent = _offset_parent;
  331. UpdateOffset();
  332. DirtyOffset();
  333. }
  334. // Otherwise, our offset is updated in case left / right / top / bottom will have an impact on
  335. // our final position, and our children are dirtied if they do.
  336. else
  337. {
  338. Vector2f& old_base = relative_offset_base;
  339. Vector2f& old_position = relative_offset_position;
  340. UpdateOffset();
  341. if (old_base != relative_offset_base ||
  342. old_position != relative_offset_position)
  343. DirtyOffset();
  344. }
  345. }
  346. // Returns the position of the top-left corner of one of the areas of this element's primary box.
  347. Vector2f Element::GetRelativeOffset(Box::Area area)
  348. {
  349. return relative_offset_base + relative_offset_position + GetBox().GetPosition(area);
  350. }
  351. // Returns the position of the top-left corner of one of the areas of this element's primary box.
  352. Vector2f Element::GetAbsoluteOffset(Box::Area area)
  353. {
  354. if (offset_dirty)
  355. {
  356. offset_dirty = false;
  357. if (offset_parent != nullptr)
  358. absolute_offset = offset_parent->GetAbsoluteOffset(Box::BORDER) + relative_offset_base + relative_offset_position;
  359. else
  360. absolute_offset = relative_offset_base + relative_offset_position;
  361. // Add any parent scrolling onto our position as well. Could cache this if required.
  362. if (!offset_fixed)
  363. {
  364. Element* scroll_parent = parent;
  365. while (scroll_parent != nullptr)
  366. {
  367. absolute_offset -= (scroll_parent->scroll_offset + scroll_parent->content_offset);
  368. if (scroll_parent == offset_parent)
  369. break;
  370. else
  371. scroll_parent = scroll_parent->parent;
  372. }
  373. }
  374. }
  375. return absolute_offset + GetBox().GetPosition(area);
  376. }
  377. // Sets an alternate area to use as the client area.
  378. void Element::SetClientArea(Box::Area _client_area)
  379. {
  380. client_area = _client_area;
  381. }
  382. // Returns the area the element uses as its client area.
  383. Box::Area Element::GetClientArea() const
  384. {
  385. return client_area;
  386. }
  387. // Sets the dimensions of the element's internal content.
  388. void Element::SetContentBox(const Vector2f& _content_offset, const Vector2f& _content_box)
  389. {
  390. if (content_offset != _content_offset ||
  391. content_box != _content_box)
  392. {
  393. // Seems to be jittering a wee bit; might need to be looked at.
  394. scroll_offset.x += (content_offset.x - _content_offset.x);
  395. scroll_offset.y += (content_offset.y - _content_offset.y);
  396. content_offset = _content_offset;
  397. content_box = _content_box;
  398. scroll_offset.x = Math::Min(scroll_offset.x, GetScrollWidth() - GetClientWidth());
  399. scroll_offset.y = Math::Min(scroll_offset.y, GetScrollHeight() - GetClientHeight());
  400. DirtyOffset();
  401. }
  402. }
  403. // Sets the box describing the size of the element.
  404. void Element::SetBox(const Box& box)
  405. {
  406. if (box != main_box || additional_boxes.size() > 0)
  407. {
  408. main_box = box;
  409. additional_boxes.clear();
  410. box_dirty = true;
  411. background->DirtyBackground();
  412. border->DirtyBorder();
  413. decoration->DirtyDecorators();
  414. }
  415. }
  416. // Adds a box to the end of the list describing this element's geometry.
  417. void Element::AddBox(const Box& box)
  418. {
  419. additional_boxes.push_back(box);
  420. box_dirty = true;
  421. background->DirtyBackground();
  422. border->DirtyBorder();
  423. decoration->DirtyDecorators();
  424. }
  425. // Returns one of the boxes describing the size of the element.
  426. const Box& Element::GetBox()
  427. {
  428. return main_box;
  429. }
  430. // Returns one of the boxes describing the size of the element.
  431. const Box& Element::GetBox(int index)
  432. {
  433. if (index < 1)
  434. return main_box;
  435. int additional_box_index = index - 1;
  436. if (additional_box_index >= (int)additional_boxes.size())
  437. return main_box;
  438. return additional_boxes[additional_box_index];
  439. }
  440. // Returns the number of boxes making up this element's geometry.
  441. int Element::GetNumBoxes()
  442. {
  443. return 1 + (int)additional_boxes.size();
  444. }
  445. // Returns the baseline of the element, in pixels offset from the bottom of the element's content area.
  446. float Element::GetBaseline() const
  447. {
  448. return 0;
  449. }
  450. // Gets the intrinsic dimensions of this element, if it is of a type that has an inherent size.
  451. bool Element::GetIntrinsicDimensions(Vector2f& RMLUI_UNUSED_PARAMETER(dimensions))
  452. {
  453. RMLUI_UNUSED(dimensions);
  454. return false;
  455. }
  456. // Checks if a given point in screen coordinates lies within the bordered area of this element.
  457. bool Element::IsPointWithinElement(const Vector2f& point)
  458. {
  459. Vector2f position = GetAbsoluteOffset(Box::BORDER);
  460. for (int i = 0; i < GetNumBoxes(); ++i)
  461. {
  462. const Box& box = GetBox(i);
  463. Vector2f box_position = position + box.GetOffset();
  464. Vector2f box_dimensions = box.GetSize(Box::BORDER);
  465. if (point.x >= box_position.x &&
  466. point.x <= (box_position.x + box_dimensions.x) &&
  467. point.y >= box_position.y &&
  468. point.y <= (box_position.y + box_dimensions.y))
  469. {
  470. return true;
  471. }
  472. }
  473. return false;
  474. }
  475. // Returns the visibility of the element.
  476. bool Element::IsVisible() const
  477. {
  478. return visible;
  479. }
  480. // Returns the z-index of the element.
  481. float Element::GetZIndex() const
  482. {
  483. return z_index;
  484. }
  485. // Returns the element's font face handle.
  486. FontFaceHandle* Element::GetFontFaceHandle() const
  487. {
  488. return element_meta->computed_values.font_face_handle.get();
  489. }
  490. // Sets a local property override on the element.
  491. bool Element::SetProperty(const String& name, const String& value)
  492. {
  493. // The name may be a shorthand giving us multiple underlying properties
  494. PropertyDictionary properties;
  495. if (!StyleSheetSpecification::ParsePropertyDeclaration(properties, name, value))
  496. {
  497. Log::Message(Log::LT_WARNING, "Syntax error parsing inline property declaration '%s: %s;'.", name.c_str(), value.c_str());
  498. return false;
  499. }
  500. for (auto& property : properties.GetProperties())
  501. {
  502. if (!style->SetProperty(property.first, property.second))
  503. return false;
  504. }
  505. return true;
  506. }
  507. // Removes a local property override on the element.
  508. void Element::RemoveProperty(const String& name)
  509. {
  510. style->RemoveProperty(StyleSheetSpecification::GetPropertyId(name));
  511. }
  512. // Removes a local property override on the element.
  513. void Element::RemoveProperty(PropertyId id)
  514. {
  515. style->RemoveProperty(id);
  516. }
  517. // Sets a local property override on the element to a pre-parsed value.
  518. bool Element::SetProperty(PropertyId id, const Property& property)
  519. {
  520. return style->SetProperty(id, property);
  521. }
  522. // Returns one of this element's properties.
  523. const Property* Element::GetProperty(const String& name)
  524. {
  525. return style->GetProperty(StyleSheetSpecification::GetPropertyId(name));
  526. }
  527. // Returns one of this element's properties.
  528. const Property* Element::GetProperty(PropertyId id)
  529. {
  530. return style->GetProperty(id);
  531. }
  532. // Returns one of this element's properties.
  533. const Property* Element::GetLocalProperty(const String& name)
  534. {
  535. return style->GetLocalProperty(StyleSheetSpecification::GetPropertyId(name));
  536. }
  537. const Property* Element::GetLocalProperty(PropertyId id)
  538. {
  539. return style->GetLocalProperty(id);
  540. }
  541. const PropertyMap& Element::GetLocalStyleProperties()
  542. {
  543. return style->GetLocalStyleProperties();
  544. }
  545. // Resolves one of this element's style.
  546. float Element::ResolveLengthPercentage(const Property *property, float base_value)
  547. {
  548. return style->ResolveLengthPercentage(property, base_value);
  549. }
  550. Vector2f Element::GetContainingBlock()
  551. {
  552. Vector2f containing_block(0, 0);
  553. if (offset_parent != nullptr)
  554. {
  555. using namespace Style;
  556. Position position_property = GetPosition();
  557. const Box& parent_box = offset_parent->GetBox();
  558. if (position_property == Position::Static || position_property == Position::Relative)
  559. {
  560. containing_block = parent_box.GetSize();
  561. }
  562. else if(position_property == Position::Absolute || position_property == Position::Fixed)
  563. {
  564. containing_block = parent_box.GetSize(Box::PADDING);
  565. }
  566. }
  567. return containing_block;
  568. }
  569. Style::Position Element::GetPosition()
  570. {
  571. return element_meta->computed_values.position;
  572. }
  573. Style::Float Element::GetFloat()
  574. {
  575. return element_meta->computed_values.float_;
  576. }
  577. Style::Display Element::GetDisplay()
  578. {
  579. return element_meta->computed_values.display;
  580. }
  581. float Element::GetLineHeight()
  582. {
  583. return element_meta->computed_values.line_height.value;
  584. }
  585. // Returns this element's TransformState
  586. const TransformState *Element::GetTransformState() const noexcept
  587. {
  588. return transform_state.get();
  589. }
  590. // Returns the TransformStates that are effective for this element.
  591. void Element::GetEffectiveTransformState(
  592. const TransformState **local_perspective,
  593. const TransformState **perspective,
  594. const TransformState **transform
  595. ) const noexcept
  596. {
  597. if (local_perspective)
  598. {
  599. *local_perspective = nullptr;
  600. }
  601. if (perspective)
  602. {
  603. *perspective = nullptr;
  604. }
  605. if (transform)
  606. {
  607. *transform = nullptr;
  608. }
  609. const Element *perspective_node = nullptr, *transform_node = nullptr;
  610. // Find the TransformState to use for unprojecting.
  611. if (transform_state.get() && transform_state->GetLocalPerspective(nullptr))
  612. {
  613. if (local_perspective)
  614. {
  615. *local_perspective = transform_state.get();
  616. }
  617. }
  618. else
  619. {
  620. const Element *node = nullptr;
  621. for (node = parent; node; node = node->parent)
  622. {
  623. if (node->transform_state.get() && node->transform_state->GetPerspective(nullptr))
  624. {
  625. if (perspective)
  626. {
  627. *perspective = node->transform_state.get();
  628. }
  629. perspective_node = node;
  630. break;
  631. }
  632. }
  633. }
  634. // Find the TransformState to use for transforming.
  635. const Element *node = nullptr;
  636. for (node = this; node; node = node->parent)
  637. {
  638. if (node->transform_state.get() && node->transform_state->GetRecursiveTransform(nullptr))
  639. {
  640. if (transform)
  641. {
  642. *transform = node->transform_state.get();
  643. }
  644. transform_node = node;
  645. break;
  646. }
  647. }
  648. }
  649. // Project a 2D point in pixel coordinates onto the element's plane.
  650. Vector2f Element::Project(const Vector2f& point) const noexcept
  651. {
  652. const Context *context = GetContext();
  653. if (!context)
  654. {
  655. return point;
  656. }
  657. const TransformState *local_perspective, *perspective, *transform;
  658. GetEffectiveTransformState(&local_perspective, &perspective, &transform);
  659. Vector2i view_pos(0, 0);
  660. Vector2i view_size = context->GetDimensions();
  661. // Compute the line segment for ray picking, one point on the near and one on the far plane.
  662. // These need to be in clip space coordinates ([-1; 1]³) so that we an unproject them.
  663. Vector3f line_segment[2] =
  664. {
  665. // When unprojected, the intersection point on the near plane
  666. Vector3f(
  667. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  668. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  669. -1
  670. ),
  671. // When unprojected, the intersection point on the far plane
  672. Vector3f(
  673. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  674. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  675. 1
  676. )
  677. };
  678. // Find the TransformState to use for unprojecting.
  679. if (local_perspective)
  680. {
  681. TransformState::LocalPerspective the_local_perspective;
  682. local_perspective->GetLocalPerspective(&the_local_perspective);
  683. line_segment[0] = the_local_perspective.Unproject(line_segment[0]);
  684. line_segment[1] = the_local_perspective.Unproject(line_segment[1]);
  685. }
  686. else if (perspective)
  687. {
  688. TransformState::Perspective the_perspective;
  689. perspective->GetPerspective(&the_perspective);
  690. line_segment[0] = the_perspective.Unproject(line_segment[0]);
  691. line_segment[1] = the_perspective.Unproject(line_segment[1]);
  692. }
  693. else
  694. {
  695. line_segment[0] = context->GetViewState().Unproject(line_segment[0]);
  696. line_segment[1] = context->GetViewState().Unproject(line_segment[1]);
  697. }
  698. // Compute three points on the context's corners to define the element's plane.
  699. // It may seem elegant to base this computation on the element's size, but
  700. // there are elements with zero length or height.
  701. Vector3f element_rect[3] =
  702. {
  703. // Top-left corner
  704. Vector3f(0, 0, 0),
  705. // Top-right corner
  706. Vector3f((float)view_size.x, 0, 0),
  707. // Bottom-left corner
  708. Vector3f(0, (float)view_size.y, 0)
  709. };
  710. // Transform by the correct matrix
  711. if (transform)
  712. {
  713. element_rect[0] = transform->Transform(element_rect[0]);
  714. element_rect[1] = transform->Transform(element_rect[1]);
  715. element_rect[2] = transform->Transform(element_rect[2]);
  716. }
  717. Vector3f u = line_segment[0] - line_segment[1];
  718. Vector3f v = element_rect[1] - element_rect[0];
  719. Vector3f w = element_rect[2] - element_rect[0];
  720. // Now compute the intersection point of the line segment and the element's rectangle.
  721. // This is based on the algorithm discussed at Wikipedia
  722. // (http://en.wikipedia.org/wiki/Line-plane_intersection).
  723. Matrix4f A = Matrix4f::FromColumns(
  724. Vector4f(u, 0),
  725. Vector4f(v, 0),
  726. Vector4f(w, 0),
  727. Vector4f(0, 0, 0, 1)
  728. );
  729. if (A.Invert())
  730. {
  731. Vector3f factors = A * (line_segment[0] - element_rect[0]);
  732. Vector3f intersection3d = element_rect[0] + v * factors[1] + w * factors[2];
  733. Vector3f projected;
  734. if (transform)
  735. {
  736. projected = transform->Untransform(intersection3d);
  737. //RMLUI_ASSERT(fabs(projected.z) < 0.0001);
  738. }
  739. else
  740. {
  741. // FIXME: Is this correct?
  742. projected = intersection3d;
  743. }
  744. return Vector2f(projected.x, projected.y);
  745. }
  746. else
  747. {
  748. // The line segment is parallel to the element's plane.
  749. // Although, mathematically, it could also lie within the plane
  750. // (yielding infinitely many intersection points), we still
  751. // return a value that's pretty sure to not match anything,
  752. // since this case has nothing to do with the user `picking'
  753. // anything.
  754. float inf = std::numeric_limits< float >::infinity();
  755. return Vector2f(-inf, -inf);
  756. }
  757. }
  758. PropertiesIteratorView Element::IterateLocalProperties() const
  759. {
  760. return PropertiesIteratorView(std::make_unique<PropertiesIterator>(style->Iterate()));
  761. }
  762. // Sets or removes a pseudo-class on the element.
  763. void Element::SetPseudoClass(const String& pseudo_class, bool activate)
  764. {
  765. style->SetPseudoClass(pseudo_class, activate);
  766. }
  767. // Checks if a specific pseudo-class has been set on the element.
  768. bool Element::IsPseudoClassSet(const String& pseudo_class) const
  769. {
  770. return style->IsPseudoClassSet(pseudo_class);
  771. }
  772. // Checks if a complete set of pseudo-classes are set on the element.
  773. bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
  774. {
  775. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  776. {
  777. if (!IsPseudoClassSet(*i))
  778. return false;
  779. }
  780. return true;
  781. }
  782. // Gets a list of the current active pseudo classes
  783. const PseudoClassList& Element::GetActivePseudoClasses() const
  784. {
  785. return style->GetActivePseudoClasses();
  786. }
  787. /// Get the named attribute
  788. Variant* Element::GetAttribute(const String& name)
  789. {
  790. return GetIf(attributes, name);
  791. }
  792. // Checks if the element has a certain attribute.
  793. bool Element::HasAttribute(const String& name) const
  794. {
  795. return attributes.find(name) != attributes.end();
  796. }
  797. // Removes an attribute from the element
  798. void Element::RemoveAttribute(const String& name)
  799. {
  800. auto it = attributes.find(name);
  801. if (it != attributes.end())
  802. {
  803. attributes.erase(it);
  804. ElementAttributes changed_attributes;
  805. changed_attributes.emplace(name, Variant());
  806. OnAttributeChange(changed_attributes);
  807. }
  808. }
  809. // Gets the outer most focus element down the tree from this node
  810. Element* Element::GetFocusLeafNode()
  811. {
  812. // If there isn't a focus, then we are the leaf.
  813. if (!focus)
  814. {
  815. return this;
  816. }
  817. // Recurse down the tree until we found the leaf focus element
  818. Element* focus_element = focus;
  819. while (focus_element->focus)
  820. focus_element = focus_element->focus;
  821. return focus_element;
  822. }
  823. // Returns the element's context.
  824. Context* Element::GetContext() const
  825. {
  826. ElementDocument* document = GetOwnerDocument();
  827. if (document != nullptr)
  828. return document->GetContext();
  829. return nullptr;
  830. }
  831. // Set a group of attributes
  832. void Element::SetAttributes(const ElementAttributes& _attributes)
  833. {
  834. attributes.reserve(attributes.size() + _attributes.size());
  835. for (auto& pair : _attributes)
  836. attributes[pair.first] = pair.second;
  837. OnAttributeChange(_attributes);
  838. }
  839. // Returns the number of attributes on the element.
  840. int Element::GetNumAttributes() const
  841. {
  842. return (int)attributes.size();
  843. }
  844. // Gets the name of the element.
  845. const String& Element::GetTagName() const
  846. {
  847. return tag;
  848. }
  849. // Gets the ID of the element.
  850. const String& Element::GetId() const
  851. {
  852. return id;
  853. }
  854. // Sets the ID of the element.
  855. void Element::SetId(const String& _id)
  856. {
  857. SetAttribute("id", _id);
  858. }
  859. // Gets the horizontal offset from the context's left edge to element's left border edge.
  860. float Element::GetAbsoluteLeft()
  861. {
  862. return GetAbsoluteOffset(Box::BORDER).x;
  863. }
  864. // Gets the vertical offset from the context's top edge to element's top border edge.
  865. float Element::GetAbsoluteTop()
  866. {
  867. return GetAbsoluteOffset(Box::BORDER).y;
  868. }
  869. // Gets the width of the left border of an element.
  870. float Element::GetClientLeft()
  871. {
  872. return GetBox().GetPosition(client_area).x;
  873. }
  874. // Gets the height of the top border of an element.
  875. float Element::GetClientTop()
  876. {
  877. return GetBox().GetPosition(client_area).y;
  878. }
  879. // Gets the inner width of the element.
  880. float Element::GetClientWidth()
  881. {
  882. return GetBox().GetSize(client_area).x - scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  883. }
  884. // Gets the inner height of the element.
  885. float Element::GetClientHeight()
  886. {
  887. return GetBox().GetSize(client_area).y - scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  888. }
  889. // Returns the element from which all offset calculations are currently computed.
  890. Element* Element::GetOffsetParent()
  891. {
  892. return offset_parent;
  893. }
  894. // Gets the distance from this element's left border to its offset parent's left border.
  895. float Element::GetOffsetLeft()
  896. {
  897. return relative_offset_base.x + relative_offset_position.x;
  898. }
  899. // Gets the distance from this element's top border to its offset parent's top border.
  900. float Element::GetOffsetTop()
  901. {
  902. return relative_offset_base.y + relative_offset_position.y;
  903. }
  904. // Gets the width of the element, including the client area, padding, borders and scrollbars, but not margins.
  905. float Element::GetOffsetWidth()
  906. {
  907. return GetBox().GetSize(Box::BORDER).x;
  908. }
  909. // Gets the height of the element, including the client area, padding, borders and scrollbars, but not margins.
  910. float Element::GetOffsetHeight()
  911. {
  912. return GetBox().GetSize(Box::BORDER).y;
  913. }
  914. // Gets the left scroll offset of the element.
  915. float Element::GetScrollLeft()
  916. {
  917. return scroll_offset.x;
  918. }
  919. // Sets the left scroll offset of the element.
  920. void Element::SetScrollLeft(float scroll_left)
  921. {
  922. const float new_offset = Math::Clamp(scroll_left, 0.0f, GetScrollWidth() - GetClientWidth());
  923. if (new_offset != scroll_offset.x)
  924. {
  925. scroll_offset.x = new_offset;
  926. scroll->UpdateScrollbar(ElementScroll::HORIZONTAL);
  927. DirtyOffset();
  928. DispatchEvent(EventId::Scroll, Dictionary());
  929. }
  930. }
  931. // Gets the top scroll offset of the element.
  932. float Element::GetScrollTop()
  933. {
  934. return scroll_offset.y;
  935. }
  936. // Sets the top scroll offset of the element.
  937. void Element::SetScrollTop(float scroll_top)
  938. {
  939. const float new_offset = Math::Clamp(scroll_top, 0.0f, GetScrollHeight() - GetClientHeight());
  940. if(new_offset != scroll_offset.y)
  941. {
  942. scroll_offset.y = new_offset;
  943. scroll->UpdateScrollbar(ElementScroll::VERTICAL);
  944. DirtyOffset();
  945. DispatchEvent(EventId::Scroll, Dictionary());
  946. }
  947. }
  948. // Gets the width of the scrollable content of the element; it includes the element padding but not its margin.
  949. float Element::GetScrollWidth()
  950. {
  951. return Math::Max(content_box.x, GetClientWidth());
  952. }
  953. // Gets the height of the scrollable content of the element; it includes the element padding but not its margin.
  954. float Element::GetScrollHeight()
  955. {
  956. return Math::Max(content_box.y, GetClientHeight());
  957. }
  958. // Gets the object representing the declarations of an element's style attributes.
  959. ElementStyle* Element::GetStyle() const
  960. {
  961. return style;
  962. }
  963. // Gets the document this element belongs to.
  964. ElementDocument* Element::GetOwnerDocument() const
  965. {
  966. #ifdef RMLUI_DEBUG
  967. if (parent && !owner_document)
  968. {
  969. // Since we have a parent but no owner_document, then we must be a 'loose' element -- that is, constructed
  970. // outside of a document and not attached to a child of any element in the hierarchy of a document.
  971. // This check ensures that we didn't just forget to set the owner document.
  972. RMLUI_ASSERT(!parent->GetOwnerDocument());
  973. }
  974. #endif
  975. return owner_document;
  976. }
  977. // Gets this element's parent node.
  978. Element* Element::GetParentNode() const
  979. {
  980. return parent;
  981. }
  982. // Gets the element immediately following this one in the tree.
  983. Element* Element::GetNextSibling() const
  984. {
  985. if (parent == nullptr)
  986. return nullptr;
  987. for (size_t i = 0; i < parent->children.size() - (parent->num_non_dom_children + 1); i++)
  988. {
  989. if (parent->children[i].get() == this)
  990. return parent->children[i + 1].get();
  991. }
  992. return nullptr;
  993. }
  994. // Gets the element immediately preceding this one in the tree.
  995. Element* Element::GetPreviousSibling() const
  996. {
  997. if (parent == nullptr)
  998. return nullptr;
  999. for (size_t i = 1; i < parent->children.size() - parent->num_non_dom_children; i++)
  1000. {
  1001. if (parent->children[i].get() == this)
  1002. return parent->children[i - 1].get();
  1003. }
  1004. return nullptr;
  1005. }
  1006. // Returns the first child of this element.
  1007. Element* Element::GetFirstChild() const
  1008. {
  1009. if (GetNumChildren() > 0)
  1010. return children[0].get();
  1011. return nullptr;
  1012. }
  1013. // Gets the last child of this element.
  1014. Element* Element::GetLastChild() const
  1015. {
  1016. if (GetNumChildren() > 0)
  1017. return (children.end() - (num_non_dom_children + 1))->get();
  1018. return nullptr;
  1019. }
  1020. Element* Element::GetChild(int index) const
  1021. {
  1022. if (index < 0 || index >= (int) children.size())
  1023. return nullptr;
  1024. return children[index].get();
  1025. }
  1026. int Element::GetNumChildren(bool include_non_dom_elements) const
  1027. {
  1028. return (int) children.size() - (include_non_dom_elements ? 0 : num_non_dom_children);
  1029. }
  1030. // Gets the markup and content of the element.
  1031. void Element::GetInnerRML(String& content) const
  1032. {
  1033. for (int i = 0; i < GetNumChildren(); i++)
  1034. {
  1035. children[i]->GetRML(content);
  1036. }
  1037. }
  1038. // Gets the markup and content of the element.
  1039. String Element::GetInnerRML() const {
  1040. String result;
  1041. GetInnerRML(result);
  1042. return result;
  1043. }
  1044. // Sets the markup and content of the element. All existing children will be replaced.
  1045. void Element::SetInnerRML(const String& rml)
  1046. {
  1047. // Remove all DOM children.
  1048. while ((int) children.size() > num_non_dom_children)
  1049. RemoveChild(children.front().get());
  1050. if(!rml.empty())
  1051. Factory::InstanceElementText(this, rml);
  1052. }
  1053. // Sets the current element as the focus object.
  1054. bool Element::Focus()
  1055. {
  1056. // Are we allowed focus?
  1057. Style::Focus focus_property = element_meta->computed_values.focus;
  1058. if (focus_property == Style::Focus::None)
  1059. return false;
  1060. // Ask our context if we can switch focus.
  1061. Context* context = GetContext();
  1062. if (context == nullptr)
  1063. return false;
  1064. if (!context->OnFocusChange(this))
  1065. return false;
  1066. // Set this as the end of the focus chain.
  1067. focus = nullptr;
  1068. // Update the focus chain up the hierarchy.
  1069. Element* element = this;
  1070. while (Element* parent = element->GetParentNode())
  1071. {
  1072. parent->focus = element;
  1073. element = parent;
  1074. }
  1075. return true;
  1076. }
  1077. // Removes focus from from this element.
  1078. void Element::Blur()
  1079. {
  1080. if (parent)
  1081. {
  1082. Context* context = GetContext();
  1083. if (context == nullptr)
  1084. return;
  1085. if (context->GetFocusElement() == this)
  1086. {
  1087. parent->Focus();
  1088. }
  1089. else if (parent->focus == this)
  1090. {
  1091. parent->focus = nullptr;
  1092. }
  1093. }
  1094. }
  1095. // Fakes a mouse click on this element.
  1096. void Element::Click()
  1097. {
  1098. Context* context = GetContext();
  1099. if (context == nullptr)
  1100. return;
  1101. context->GenerateClickEvent(this);
  1102. }
  1103. // Adds an event listener
  1104. void Element::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1105. {
  1106. EventId id = EventSpecificationInterface::GetIdOrInsert(event);
  1107. event_dispatcher->AttachEvent(id, listener, in_capture_phase);
  1108. }
  1109. // Adds an event listener
  1110. void Element::AddEventListener(EventId id, EventListener* listener, bool in_capture_phase)
  1111. {
  1112. event_dispatcher->AttachEvent(id, listener, in_capture_phase);
  1113. }
  1114. // Removes an event listener from this element.
  1115. void Element::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1116. {
  1117. EventId id = EventSpecificationInterface::GetIdOrInsert(event);
  1118. event_dispatcher->DetachEvent(id, listener, in_capture_phase);
  1119. }
  1120. // Removes an event listener from this element.
  1121. void Element::RemoveEventListener(EventId id, EventListener* listener, bool in_capture_phase)
  1122. {
  1123. event_dispatcher->DetachEvent(id, listener, in_capture_phase);
  1124. }
  1125. // Dispatches the specified event
  1126. bool Element::DispatchEvent(const String& type, const Dictionary& parameters)
  1127. {
  1128. const EventSpecification& specification = EventSpecificationInterface::GetOrInsert(type);
  1129. return event_dispatcher->DispatchEvent(this, specification.id, type, parameters, specification.interruptible, specification.bubbles, specification.default_action_phase);
  1130. }
  1131. // Dispatches the specified event
  1132. bool Element::DispatchEvent(const String& type, const Dictionary& parameters, bool interruptible, bool bubbles)
  1133. {
  1134. const EventSpecification& specification = EventSpecificationInterface::GetOrInsert(type);
  1135. return event_dispatcher->DispatchEvent(this, specification.id, type, parameters, interruptible, bubbles, specification.default_action_phase);
  1136. }
  1137. // Dispatches the specified event
  1138. bool Element::DispatchEvent(EventId id, const Dictionary& parameters)
  1139. {
  1140. const EventSpecification& specification = EventSpecificationInterface::Get(id);
  1141. return event_dispatcher->DispatchEvent(this, specification.id, specification.type, parameters, specification.interruptible, specification.bubbles, specification.default_action_phase);
  1142. }
  1143. // Scrolls the parent element's contents so that this element is visible.
  1144. void Element::ScrollIntoView(bool align_with_top)
  1145. {
  1146. Vector2f size(0, 0);
  1147. if (!align_with_top)
  1148. {
  1149. size.y = main_box.GetOffset().y +
  1150. main_box.GetSize(Box::BORDER).y;
  1151. }
  1152. Element* scroll_parent = parent;
  1153. while (scroll_parent != nullptr)
  1154. {
  1155. Style::Overflow overflow_x_property = scroll_parent->GetComputedValues().overflow_x;
  1156. Style::Overflow overflow_y_property = scroll_parent->GetComputedValues().overflow_y;
  1157. if ((overflow_x_property != Style::Overflow::Visible &&
  1158. scroll_parent->GetScrollWidth() > scroll_parent->GetClientWidth()) ||
  1159. (overflow_y_property != Style::Overflow::Visible &&
  1160. scroll_parent->GetScrollHeight() > scroll_parent->GetClientHeight()))
  1161. {
  1162. Vector2f offset = scroll_parent->GetAbsoluteOffset(Box::BORDER) - GetAbsoluteOffset(Box::BORDER);
  1163. Vector2f scroll_offset(scroll_parent->GetScrollLeft(), scroll_parent->GetScrollTop());
  1164. scroll_offset -= offset;
  1165. scroll_offset.x += scroll_parent->GetClientLeft();
  1166. scroll_offset.y += scroll_parent->GetClientTop();
  1167. if (!align_with_top)
  1168. scroll_offset.y -= (scroll_parent->GetClientHeight() - size.y);
  1169. if (overflow_x_property != Style::Overflow::Visible)
  1170. scroll_parent->SetScrollLeft(scroll_offset.x);
  1171. if (overflow_y_property != Style::Overflow::Visible)
  1172. scroll_parent->SetScrollTop(scroll_offset.y);
  1173. }
  1174. scroll_parent = scroll_parent->GetParentNode();
  1175. }
  1176. }
  1177. // Appends a child to this element
  1178. Element* Element::AppendChild(ElementPtr child, bool dom_element)
  1179. {
  1180. RMLUI_ASSERT(child);
  1181. Element* child_ptr = child.get();
  1182. child_ptr->SetParent(this);
  1183. if (dom_element)
  1184. children.insert(children.end() - num_non_dom_children, std::move(child));
  1185. else
  1186. {
  1187. children.push_back(std::move(child));
  1188. num_non_dom_children++;
  1189. }
  1190. Element* ancestor = child_ptr;
  1191. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1192. ancestor->OnChildAdd(child_ptr);
  1193. DirtyStackingContext();
  1194. DirtyStructure();
  1195. if (dom_element)
  1196. DirtyLayout();
  1197. return child_ptr;
  1198. }
  1199. // Adds a child to this element, directly after the adjacent element. Inherits
  1200. // the dom/non-dom status from the adjacent element.
  1201. Element* Element::InsertBefore(ElementPtr child, Element* adjacent_element)
  1202. {
  1203. RMLUI_ASSERT(child);
  1204. // Find the position in the list of children of the adjacent element. If
  1205. // it's nullptr or we can't find it, then we insert it at the end of the dom
  1206. // children, as a dom element.
  1207. size_t child_index = 0;
  1208. bool found_child = false;
  1209. if (adjacent_element)
  1210. {
  1211. for (child_index = 0; child_index < children.size(); child_index++)
  1212. {
  1213. if (children[child_index].get() == adjacent_element)
  1214. {
  1215. found_child = true;
  1216. break;
  1217. }
  1218. }
  1219. }
  1220. Element* child_ptr = nullptr;
  1221. if (found_child)
  1222. {
  1223. child_ptr = child.get();
  1224. child_ptr->SetParent(this);
  1225. if ((int) child_index >= GetNumChildren())
  1226. num_non_dom_children++;
  1227. else
  1228. DirtyLayout();
  1229. children.insert(children.begin() + child_index, std::move(child));
  1230. Element* ancestor = child_ptr;
  1231. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1232. ancestor->OnChildAdd(child_ptr);
  1233. DirtyStackingContext();
  1234. DirtyStructure();
  1235. }
  1236. else
  1237. {
  1238. child_ptr = AppendChild(std::move(child));
  1239. }
  1240. return child_ptr;
  1241. }
  1242. // Replaces the second node with the first node.
  1243. ElementPtr Element::ReplaceChild(ElementPtr inserted_element, Element* replaced_element)
  1244. {
  1245. RMLUI_ASSERT(inserted_element);
  1246. auto insertion_point = children.begin();
  1247. while (insertion_point != children.end() && insertion_point->get() != replaced_element)
  1248. {
  1249. ++insertion_point;
  1250. }
  1251. Element* inserted_element_ptr = inserted_element.get();
  1252. if (insertion_point == children.end())
  1253. {
  1254. AppendChild(std::move(inserted_element));
  1255. return nullptr;
  1256. }
  1257. inserted_element_ptr->SetParent(this);
  1258. children.insert(insertion_point, std::move(inserted_element));
  1259. ElementPtr result = RemoveChild(replaced_element);
  1260. Element* ancestor = inserted_element_ptr;
  1261. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1262. ancestor->OnChildAdd(inserted_element_ptr);
  1263. return result;
  1264. }
  1265. // Removes the specified child
  1266. ElementPtr Element::RemoveChild(Element* child)
  1267. {
  1268. size_t child_index = 0;
  1269. for (auto itr = children.begin(); itr != children.end(); ++itr)
  1270. {
  1271. // Add the element to the delete list
  1272. if (itr->get() == child)
  1273. {
  1274. Element* ancestor = child;
  1275. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1276. ancestor->OnChildRemove(child);
  1277. if (child_index >= children.size() - num_non_dom_children)
  1278. num_non_dom_children--;
  1279. ElementPtr detached_child = std::move(*itr);
  1280. children.erase(itr);
  1281. // Remove the child element as the focused child of this element.
  1282. if (child == focus)
  1283. {
  1284. focus = nullptr;
  1285. // If this child (or a descendant of this child) is the context's currently
  1286. // focused element, set the focus to us instead.
  1287. if (Context * context = GetContext())
  1288. {
  1289. Element* focus_element = context->GetFocusElement();
  1290. while (focus_element)
  1291. {
  1292. if (focus_element == child)
  1293. {
  1294. Focus();
  1295. break;
  1296. }
  1297. focus_element = focus_element->GetParentNode();
  1298. }
  1299. }
  1300. }
  1301. detached_child->SetParent(nullptr);
  1302. DirtyLayout();
  1303. DirtyStackingContext();
  1304. DirtyStructure();
  1305. return detached_child;
  1306. }
  1307. child_index++;
  1308. }
  1309. return nullptr;
  1310. }
  1311. bool Element::HasChildNodes() const
  1312. {
  1313. return (int) children.size() > num_non_dom_children;
  1314. }
  1315. Element* Element::GetElementById(const String& id)
  1316. {
  1317. // Check for special-case tokens.
  1318. if (id == "#self")
  1319. return this;
  1320. else if (id == "#document")
  1321. return GetOwnerDocument();
  1322. else if (id == "#parent")
  1323. return this->parent;
  1324. else
  1325. {
  1326. Element* search_root = GetOwnerDocument();
  1327. if (search_root == nullptr)
  1328. search_root = this;
  1329. return ElementUtilities::GetElementById(search_root, id);
  1330. }
  1331. }
  1332. // Get all elements with the given tag.
  1333. void Element::GetElementsByTagName(ElementList& elements, const String& tag)
  1334. {
  1335. return ElementUtilities::GetElementsByTagName(elements, this, tag);
  1336. }
  1337. // Get all elements with the given class set on them.
  1338. void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
  1339. {
  1340. return ElementUtilities::GetElementsByClassName(elements, this, class_name);
  1341. }
  1342. // Access the event dispatcher
  1343. EventDispatcher* Element::GetEventDispatcher() const
  1344. {
  1345. return event_dispatcher;
  1346. }
  1347. String Element::GetEventDispatcherSummary() const
  1348. {
  1349. return event_dispatcher->ToString();
  1350. }
  1351. // Access the element background.
  1352. ElementBackground* Element::GetElementBackground() const
  1353. {
  1354. return background;
  1355. }
  1356. // Access the element border.
  1357. ElementBorder* Element::GetElementBorder() const
  1358. {
  1359. return border;
  1360. }
  1361. // Access the element decorators
  1362. ElementDecoration* Element::GetElementDecoration() const
  1363. {
  1364. return decoration;
  1365. }
  1366. // Returns the element's scrollbar functionality.
  1367. ElementScroll* Element::GetElementScroll() const
  1368. {
  1369. return scroll;
  1370. }
  1371. int Element::GetClippingIgnoreDepth()
  1372. {
  1373. if (clipping_state_dirty)
  1374. {
  1375. IsClippingEnabled();
  1376. }
  1377. return clipping_ignore_depth;
  1378. }
  1379. bool Element::IsClippingEnabled()
  1380. {
  1381. if (clipping_state_dirty)
  1382. {
  1383. const auto& computed = GetComputedValues();
  1384. // Is clipping enabled for this element, yes unless both overlow properties are set to visible
  1385. clipping_enabled = computed.overflow_x != Style::Overflow::Visible
  1386. || computed.overflow_y != Style::Overflow::Visible;
  1387. // Get the clipping ignore depth from the clip property
  1388. clipping_ignore_depth = computed.clip.number;
  1389. clipping_state_dirty = false;
  1390. }
  1391. return clipping_enabled;
  1392. }
  1393. // Gets the render interface owned by this element's context.
  1394. RenderInterface* Element::GetRenderInterface()
  1395. {
  1396. if (Context* context = GetContext())
  1397. return context->GetRenderInterface();
  1398. return Rml::Core::GetRenderInterface();
  1399. }
  1400. void Element::SetInstancer(ElementInstancer* _instancer)
  1401. {
  1402. // Only record the first instancer being set as some instancers call other instancers to do their dirty work, in
  1403. // which case we don't want to update the lowest level instancer.
  1404. if (!instancer)
  1405. {
  1406. instancer = _instancer;
  1407. }
  1408. }
  1409. // Forces the element to generate a local stacking context, regardless of the value of its z-index property.
  1410. void Element::ForceLocalStackingContext()
  1411. {
  1412. local_stacking_context_forced = true;
  1413. local_stacking_context = true;
  1414. DirtyStackingContext();
  1415. }
  1416. // Called during the update loop after children are rendered.
  1417. void Element::OnUpdate()
  1418. {
  1419. }
  1420. // Called during render after backgrounds, borders, decorators, but before children, are rendered.
  1421. void Element::OnRender()
  1422. {
  1423. }
  1424. void Element::OnResize()
  1425. {
  1426. }
  1427. // Called during a layout operation, when the element is being positioned and sized.
  1428. void Element::OnLayout()
  1429. {
  1430. }
  1431. // Called when attributes on the element are changed.
  1432. void Element::OnAttributeChange(const ElementAttributes& changed_attributes)
  1433. {
  1434. auto it = changed_attributes.find("id");
  1435. if (it != changed_attributes.end())
  1436. {
  1437. id = it->second.Get<String>();
  1438. style->DirtyDefinition();
  1439. }
  1440. it = changed_attributes.find("class");
  1441. if (it != changed_attributes.end())
  1442. {
  1443. style->SetClassNames(it->second.Get<String>());
  1444. }
  1445. // Add any inline style declarations.
  1446. it = changed_attributes.find("style");
  1447. if (it != changed_attributes.end())
  1448. {
  1449. PropertyDictionary properties;
  1450. StyleSheetParser parser;
  1451. parser.ParseProperties(properties, it->second.Get<String>());
  1452. Rml::Core::PropertyMap property_map = properties.GetProperties();
  1453. for (Rml::Core::PropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  1454. {
  1455. style->SetProperty((*i).first, (*i).second);
  1456. }
  1457. }
  1458. }
  1459. // Called when properties on the element are changed.
  1460. void Element::OnPropertyChange(const PropertyIdSet& changed_properties)
  1461. {
  1462. if (!IsLayoutDirty())
  1463. {
  1464. // Force a relayout if any of the changed properties require it.
  1465. const PropertyIdSet changed_properties_forcing_layout = (changed_properties & StyleSheetSpecification::GetRegisteredPropertiesForcingLayout());
  1466. if(!changed_properties_forcing_layout.Empty())
  1467. DirtyLayout();
  1468. }
  1469. // Update the visibility.
  1470. if (changed_properties.Contains(PropertyId::Visibility) ||
  1471. changed_properties.Contains(PropertyId::Display))
  1472. {
  1473. bool new_visibility = (element_meta->computed_values.display != Style::Display::None && element_meta->computed_values.visibility == Style::Visibility::Visible);
  1474. if (visible != new_visibility)
  1475. {
  1476. visible = new_visibility;
  1477. if (parent != nullptr)
  1478. parent->DirtyStackingContext();
  1479. }
  1480. if (changed_properties.Contains(PropertyId::Display))
  1481. {
  1482. // Due to structural pseudo-classes, this may change the element definition in siblings and parent.
  1483. // However, the definitions will only be changed on the next update loop which may result in jarring behavior for one @frame.
  1484. // A possible workaround is to add the parent to a list of elements that need to be updated again.
  1485. if (parent != nullptr)
  1486. parent->DirtyStructure();
  1487. }
  1488. }
  1489. // Update the position.
  1490. if (changed_properties.Contains(PropertyId::Left) ||
  1491. changed_properties.Contains(PropertyId::Right) ||
  1492. changed_properties.Contains(PropertyId::Top) ||
  1493. changed_properties.Contains(PropertyId::Bottom))
  1494. {
  1495. // TODO: This should happen during/after layout, as the containing box is not properly defined yet. Off-by-one @frame issue.
  1496. UpdateOffset();
  1497. DirtyOffset();
  1498. }
  1499. // Update the z-index.
  1500. if (changed_properties.Contains(PropertyId::ZIndex))
  1501. {
  1502. Style::ZIndex z_index_property = element_meta->computed_values.z_index;
  1503. if (z_index_property.type == Style::ZIndex::Auto)
  1504. {
  1505. if (local_stacking_context &&
  1506. !local_stacking_context_forced)
  1507. {
  1508. // We're no longer acting as a stacking context.
  1509. local_stacking_context = false;
  1510. stacking_context_dirty = false;
  1511. stacking_context.clear();
  1512. }
  1513. // If our old z-index was not zero, then we must dirty our stacking context so we'll be re-indexed.
  1514. if (z_index != 0)
  1515. {
  1516. z_index = 0;
  1517. DirtyStackingContext();
  1518. }
  1519. }
  1520. else
  1521. {
  1522. float new_z_index = z_index_property.value;
  1523. if (new_z_index != z_index)
  1524. {
  1525. z_index = new_z_index;
  1526. if (parent != nullptr)
  1527. parent->DirtyStackingContext();
  1528. }
  1529. if (!local_stacking_context)
  1530. {
  1531. local_stacking_context = true;
  1532. stacking_context_dirty = true;
  1533. }
  1534. }
  1535. }
  1536. // Dirty the background if it's changed.
  1537. if (changed_properties.Contains(PropertyId::BackgroundColor) ||
  1538. changed_properties.Contains(PropertyId::Opacity) ||
  1539. changed_properties.Contains(PropertyId::ImageColor)) {
  1540. background->DirtyBackground();
  1541. }
  1542. // Dirty the decoration if it's changed.
  1543. if (changed_properties.Contains(PropertyId::Decorator) ||
  1544. changed_properties.Contains(PropertyId::Opacity) ||
  1545. changed_properties.Contains(PropertyId::ImageColor)) {
  1546. decoration->DirtyDecorators();
  1547. }
  1548. // Dirty the border if it's changed.
  1549. if (changed_properties.Contains(PropertyId::BorderTopWidth) ||
  1550. changed_properties.Contains(PropertyId::BorderRightWidth) ||
  1551. changed_properties.Contains(PropertyId::BorderBottomWidth) ||
  1552. changed_properties.Contains(PropertyId::BorderLeftWidth) ||
  1553. changed_properties.Contains(PropertyId::BorderTopColor) ||
  1554. changed_properties.Contains(PropertyId::BorderRightColor) ||
  1555. changed_properties.Contains(PropertyId::BorderBottomColor) ||
  1556. changed_properties.Contains(PropertyId::BorderLeftColor) ||
  1557. changed_properties.Contains(PropertyId::Opacity))
  1558. border->DirtyBorder();
  1559. // Check for clipping state changes
  1560. if (changed_properties.Contains(PropertyId::Clip) ||
  1561. changed_properties.Contains(PropertyId::OverflowX) ||
  1562. changed_properties.Contains(PropertyId::OverflowY))
  1563. {
  1564. clipping_state_dirty = true;
  1565. }
  1566. // Check for `perspective' and `perspective-origin' changes
  1567. if (changed_properties.Contains(PropertyId::Perspective) ||
  1568. changed_properties.Contains(PropertyId::PerspectiveOriginX) ||
  1569. changed_properties.Contains(PropertyId::PerspectiveOriginY))
  1570. {
  1571. DirtyTransformState(true, false, false);
  1572. }
  1573. // Check for `transform' and `transform-origin' changes
  1574. if (changed_properties.Contains(PropertyId::Transform) ||
  1575. changed_properties.Contains(PropertyId::TransformOriginX) ||
  1576. changed_properties.Contains(PropertyId::TransformOriginY) ||
  1577. changed_properties.Contains(PropertyId::TransformOriginZ))
  1578. {
  1579. DirtyTransformState(false, true, false);
  1580. }
  1581. // Check for `animation' changes
  1582. if (changed_properties.Contains(PropertyId::Animation))
  1583. {
  1584. dirty_animation = true;
  1585. }
  1586. // Check for `transition' changes
  1587. if (changed_properties.Contains(PropertyId::Transition))
  1588. {
  1589. dirty_transition = true;
  1590. }
  1591. }
  1592. // Called when a child node has been added somewhere in the hierarchy
  1593. void Element::OnChildAdd(Element* child)
  1594. {
  1595. }
  1596. // Called when a child node has been removed somewhere in the hierarchy
  1597. void Element::OnChildRemove(Element* child)
  1598. {
  1599. }
  1600. // Forces a re-layout of this element, and any other children required.
  1601. void Element::DirtyLayout()
  1602. {
  1603. Element* document = GetOwnerDocument();
  1604. if (document != nullptr)
  1605. document->DirtyLayout();
  1606. }
  1607. // Forces a re-layout of this element, and any other children required.
  1608. bool Element::IsLayoutDirty()
  1609. {
  1610. Element* document = GetOwnerDocument();
  1611. if (document != nullptr)
  1612. return document->IsLayoutDirty();
  1613. return false;
  1614. }
  1615. void Element::ProcessDefaultAction(Event& event)
  1616. {
  1617. if (event == EventId::Mousedown && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
  1618. event.GetParameter< int >("button", 0) == 0)
  1619. SetPseudoClass("active", true);
  1620. if (event == EventId::Mousescroll)
  1621. {
  1622. if (GetScrollHeight() > GetClientHeight())
  1623. {
  1624. Style::Overflow overflow_property = element_meta->computed_values.overflow_y;
  1625. if (overflow_property == Style::Overflow::Auto ||
  1626. overflow_property == Style::Overflow::Scroll)
  1627. {
  1628. // Stop the propagation if the current element has scrollbars.
  1629. // This prevents scrolling in parent elements, which is often unintended. If instead desired behavior is
  1630. // to scroll in parent elements when reaching top/bottom, move StopPropagation inside the next if statement.
  1631. event.StopPropagation();
  1632. const float wheel_delta = event.GetParameter< float >("wheel_delta", 0.f);
  1633. if ((wheel_delta < 0 && GetScrollTop() > 0) ||
  1634. (wheel_delta > 0 && GetScrollHeight() > GetScrollTop() + GetClientHeight()))
  1635. {
  1636. // Defined as three times the default line-height, multiplied by the dp ratio.
  1637. float default_scroll_length = 3.f * DefaultComputedValues.line_height.value;
  1638. if (const Context* context = GetContext())
  1639. default_scroll_length *= context->GetDensityIndependentPixelRatio();
  1640. SetScrollTop(GetScrollTop() + Math::RoundFloat(wheel_delta * default_scroll_length));
  1641. }
  1642. }
  1643. }
  1644. return;
  1645. }
  1646. if (event.GetPhase() == EventPhase::Target)
  1647. {
  1648. switch (event.GetId())
  1649. {
  1650. case EventId::Mouseover:
  1651. SetPseudoClass("hover", true);
  1652. break;
  1653. case EventId::Mouseout:
  1654. SetPseudoClass("hover", false);
  1655. break;
  1656. case EventId::Focus:
  1657. SetPseudoClass(FOCUS, true);
  1658. break;
  1659. case EventId::Blur:
  1660. SetPseudoClass(FOCUS, false);
  1661. break;
  1662. default:
  1663. break;
  1664. }
  1665. }
  1666. }
  1667. const Style::ComputedValues& Element::GetComputedValues() const
  1668. {
  1669. return element_meta->computed_values;
  1670. }
  1671. void Element::GetRML(String& content)
  1672. {
  1673. // First we start the open tag, add the attributes then close the open tag.
  1674. // Then comes the children in order, then we add our close tag.
  1675. content += "<";
  1676. content += tag;
  1677. for( auto& pair : attributes)
  1678. {
  1679. auto& name = pair.first;
  1680. auto& variant = pair.second;
  1681. String value;
  1682. if (variant.GetInto(value))
  1683. content += " " + name + "=\"" + value + "\"";
  1684. }
  1685. if (HasChildNodes())
  1686. {
  1687. content += ">";
  1688. GetInnerRML(content);
  1689. content += "</";
  1690. content += tag;
  1691. content += ">";
  1692. }
  1693. else
  1694. {
  1695. content += " />";
  1696. }
  1697. }
  1698. void Element::SetOwnerDocument(ElementDocument* document)
  1699. {
  1700. // If this element is a document, then never change owner_document.
  1701. if (owner_document != this)
  1702. {
  1703. if (owner_document && !document)
  1704. {
  1705. // We are detaching from the document and thereby also the context.
  1706. if (Context * context = owner_document->GetContext())
  1707. context->OnElementDetach(this);
  1708. }
  1709. if (owner_document != document)
  1710. {
  1711. owner_document = document;
  1712. for (ElementPtr& child : children)
  1713. child->SetOwnerDocument(document);
  1714. }
  1715. }
  1716. }
  1717. void Element::Release()
  1718. {
  1719. if (instancer)
  1720. instancer->ReleaseElement(this);
  1721. else
  1722. Log::Message(Log::LT_WARNING, "Leak detected: element %s not instanced via RmlUi Factory. Unable to release.", GetAddress().c_str());
  1723. }
  1724. void Element::SetParent(Element* _parent)
  1725. {
  1726. // Assumes we are already detached from the hierarchy or we are detaching now.
  1727. RMLUI_ASSERT(!parent || !_parent);
  1728. parent = _parent;
  1729. if (parent)
  1730. {
  1731. // We need to update our definition and make sure we inherit the properties of our new parent.
  1732. style->DirtyDefinition();
  1733. style->DirtyInheritedProperties();
  1734. }
  1735. SetOwnerDocument(parent ? parent->GetOwnerDocument() : nullptr);
  1736. }
  1737. void Element::DirtyOffset()
  1738. {
  1739. if(!offset_dirty)
  1740. {
  1741. offset_dirty = true;
  1742. if(transform_state)
  1743. DirtyTransformState(true, true, false);
  1744. // Not strictly true ... ?
  1745. for (size_t i = 0; i < children.size(); i++)
  1746. children[i]->DirtyOffset();
  1747. }
  1748. }
  1749. void Element::UpdateOffset()
  1750. {
  1751. using namespace Style;
  1752. const auto& computed = element_meta->computed_values;
  1753. Position position_property = computed.position;
  1754. if (position_property == Position::Absolute ||
  1755. position_property == Position::Fixed)
  1756. {
  1757. if (offset_parent != nullptr)
  1758. {
  1759. const Box& parent_box = offset_parent->GetBox();
  1760. Vector2f containing_block = parent_box.GetSize(Box::PADDING);
  1761. // If the element is anchored left, then the position is offset by that resolved value.
  1762. if (computed.left.type != Left::Auto)
  1763. relative_offset_base.x = parent_box.GetEdge(Box::BORDER, Box::LEFT) + (ResolveValue(computed.left, containing_block.x) + GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  1764. // If the element is anchored right, then the position is set first so the element's right-most edge
  1765. // (including margins) will render up against the containing box's right-most content edge, and then
  1766. // offset by the resolved value.
  1767. else if (computed.right.type != Right::Auto)
  1768. relative_offset_base.x = containing_block.x + parent_box.GetEdge(Box::BORDER, Box::LEFT) - (ResolveValue(computed.right, containing_block.x) + GetBox().GetSize(Box::BORDER).x + GetBox().GetEdge(Box::MARGIN, Box::RIGHT));
  1769. // If the element is anchored top, then the position is offset by that resolved value.
  1770. if (computed.top.type != Top::Auto)
  1771. relative_offset_base.y = parent_box.GetEdge(Box::BORDER, Box::TOP) + (ResolveValue(computed.top, containing_block.y) + GetBox().GetEdge(Box::MARGIN, Box::TOP));
  1772. // If the element is anchored bottom, then the position is set first so the element's right-most edge
  1773. // (including margins) will render up against the containing box's right-most content edge, and then
  1774. // offset by the resolved value.
  1775. else if (computed.bottom.type != Bottom::Auto)
  1776. relative_offset_base.y = containing_block.y + parent_box.GetEdge(Box::BORDER, Box::TOP) - (ResolveValue(computed.bottom, containing_block.y) + GetBox().GetSize(Box::BORDER).y + GetBox().GetEdge(Box::MARGIN, Box::BOTTOM));
  1777. }
  1778. }
  1779. else if (position_property == Position::Relative)
  1780. {
  1781. if (offset_parent != nullptr)
  1782. {
  1783. const Box& parent_box = offset_parent->GetBox();
  1784. Vector2f containing_block = parent_box.GetSize();
  1785. if (computed.left.type != Left::Auto)
  1786. relative_offset_position.x = ResolveValue(computed.left, containing_block.x);
  1787. else if (computed.right.type != Right::Auto)
  1788. relative_offset_position.x = -1 * ResolveValue(computed.right, containing_block.x);
  1789. else
  1790. relative_offset_position.x = 0;
  1791. if (computed.top.type != Top::Auto)
  1792. relative_offset_position.y = ResolveValue(computed.top, containing_block.y);
  1793. else if (computed.bottom.type != Bottom::Auto)
  1794. relative_offset_position.y = -1 * ResolveValue(computed.bottom, containing_block.y);
  1795. else
  1796. relative_offset_position.y = 0;
  1797. }
  1798. }
  1799. else
  1800. {
  1801. relative_offset_position.x = 0;
  1802. relative_offset_position.y = 0;
  1803. }
  1804. }
  1805. void Element::BuildLocalStackingContext()
  1806. {
  1807. stacking_context_dirty = false;
  1808. stacking_context.clear();
  1809. BuildStackingContext(&stacking_context);
  1810. std::stable_sort(stacking_context.begin(), stacking_context.end(), ElementSortZIndex());
  1811. }
  1812. void Element::BuildStackingContext(ElementList* new_stacking_context)
  1813. {
  1814. // Build the list of ordered children. Our child list is sorted within the stacking context so stacked elements
  1815. // will render in the right order; ie, positioned elements will render on top of inline elements, which will render
  1816. // on top of floated elements, which will render on top of block elements.
  1817. std::vector< std::pair< Element*, float > > ordered_children;
  1818. for (size_t i = 0; i < children.size(); ++i)
  1819. {
  1820. Element* child = children[i].get();
  1821. if (!child->IsVisible())
  1822. continue;
  1823. std::pair< Element*, float > ordered_child;
  1824. ordered_child.first = child;
  1825. if (child->GetPosition() != Style::Position::Static)
  1826. ordered_child.second = 3;
  1827. else if (child->GetFloat() != Style::Float::None)
  1828. ordered_child.second = 1;
  1829. else if (child->GetDisplay() == Style::Display::Block)
  1830. ordered_child.second = 0;
  1831. else
  1832. ordered_child.second = 2;
  1833. ordered_children.push_back(ordered_child);
  1834. }
  1835. // Sort the list!
  1836. std::stable_sort(ordered_children.begin(), ordered_children.end(), ElementSortZOrder());
  1837. // Add the list of ordered children into the stacking context in order.
  1838. for (size_t i = 0; i < ordered_children.size(); ++i)
  1839. {
  1840. new_stacking_context->push_back(ordered_children[i].first);
  1841. if (!ordered_children[i].first->local_stacking_context)
  1842. ordered_children[i].first->BuildStackingContext(new_stacking_context);
  1843. }
  1844. }
  1845. void Element::DirtyStackingContext()
  1846. {
  1847. // The first ancestor of ours that doesn't have an automatic z-index is the ancestor that is establishing our local
  1848. // stacking context.
  1849. Element* stacking_context_parent = this;
  1850. while (stacking_context_parent != nullptr &&
  1851. !stacking_context_parent->local_stacking_context)
  1852. stacking_context_parent = stacking_context_parent->GetParentNode();
  1853. if (stacking_context_parent != nullptr)
  1854. stacking_context_parent->stacking_context_dirty = true;
  1855. }
  1856. void Element::DirtyStructure()
  1857. {
  1858. structure_dirty = true;
  1859. }
  1860. void Element::UpdateStructure()
  1861. {
  1862. if (structure_dirty)
  1863. {
  1864. structure_dirty = false;
  1865. // If this element or its children depend on structured selectors, they may need to be updated.
  1866. GetStyle()->DirtyDefinition();
  1867. }
  1868. }
  1869. bool Element::Animate(const String & property_name, const Property & target_value, float duration, Tween tween, int num_iterations, bool alternate_direction, float delay, const Property* start_value)
  1870. {
  1871. bool result = false;
  1872. PropertyId property_id = StyleSheetSpecification::GetPropertyId(property_name);
  1873. auto it_animation = StartAnimation(property_id, start_value, num_iterations, alternate_direction, delay, false);
  1874. if (it_animation != animations.end())
  1875. {
  1876. result = it_animation->AddKey(duration, target_value, *this, tween, true);
  1877. if (!result)
  1878. animations.erase(it_animation);
  1879. }
  1880. return result;
  1881. }
  1882. bool Element::AddAnimationKey(const String & property_name, const Property & target_value, float duration, Tween tween)
  1883. {
  1884. ElementAnimation* animation = nullptr;
  1885. PropertyId property_id = StyleSheetSpecification::GetPropertyId(property_name);
  1886. for (auto& existing_animation : animations) {
  1887. if (existing_animation.GetPropertyId() == property_id) {
  1888. animation = &existing_animation;
  1889. break;
  1890. }
  1891. }
  1892. if (!animation)
  1893. return false;
  1894. bool result = animation->AddKey(animation->GetDuration() + duration, target_value, *this, tween, true);
  1895. return result;
  1896. }
  1897. ElementAnimationList::iterator Element::StartAnimation(PropertyId property_id, const Property* start_value, int num_iterations, bool alternate_direction, float delay, bool origin_is_animation_property)
  1898. {
  1899. auto it = std::find_if(animations.begin(), animations.end(), [&](const ElementAnimation& el) { return el.GetPropertyId() == property_id; });
  1900. if (it == animations.end())
  1901. {
  1902. animations.emplace_back();
  1903. it = animations.end() - 1;
  1904. }
  1905. Property value;
  1906. if (start_value)
  1907. {
  1908. value = *start_value;
  1909. if (!value.definition)
  1910. if(auto default_value = GetProperty(property_id))
  1911. value.definition = default_value->definition;
  1912. }
  1913. else if (auto default_value = GetProperty(property_id))
  1914. {
  1915. value = *default_value;
  1916. }
  1917. if (value.definition)
  1918. {
  1919. ElementAnimationOrigin origin = (origin_is_animation_property ? ElementAnimationOrigin::Animation : ElementAnimationOrigin::User);
  1920. double start_time = Clock::GetElapsedTime() + (double)delay;
  1921. *it = ElementAnimation{ property_id, origin, value, start_time, 0.0f, num_iterations, alternate_direction };
  1922. }
  1923. else
  1924. {
  1925. animations.erase(it);
  1926. it = animations.end();
  1927. }
  1928. return it;
  1929. }
  1930. bool Element::AddAnimationKeyTime(PropertyId property_id, const Property* target_value, float time, Tween tween)
  1931. {
  1932. if (!target_value)
  1933. target_value = style->GetProperty(property_id);
  1934. if (!target_value)
  1935. return false;
  1936. ElementAnimation* animation = nullptr;
  1937. for (auto& existing_animation : animations) {
  1938. if (existing_animation.GetPropertyId() == property_id) {
  1939. animation = &existing_animation;
  1940. break;
  1941. }
  1942. }
  1943. if (!animation)
  1944. return false;
  1945. bool result = animation->AddKey(time, *target_value, *this, tween, true);
  1946. return result;
  1947. }
  1948. bool Element::StartTransition(const Transition & transition, const Property& start_value, const Property & target_value)
  1949. {
  1950. auto it = std::find_if(animations.begin(), animations.end(), [&](const ElementAnimation& el) { return el.GetPropertyId() == transition.id; });
  1951. if (it != animations.end() && !it->IsTransition())
  1952. return false;
  1953. float duration = transition.duration;
  1954. double start_time = Clock::GetElapsedTime() + (double)transition.delay;
  1955. if (it == animations.end())
  1956. {
  1957. // Add transition as new animation
  1958. animations.push_back(
  1959. ElementAnimation{ transition.id, ElementAnimationOrigin::Transition, start_value, start_time, 0.0f, 1, false }
  1960. );
  1961. it = (animations.end() - 1);
  1962. }
  1963. else
  1964. {
  1965. // Compress the duration based on the progress of the current animation
  1966. float f = it->GetInterpolationFactor();
  1967. f = 1.0f - (1.0f - f)*transition.reverse_adjustment_factor;
  1968. duration = duration * f;
  1969. // Replace old transition
  1970. *it = ElementAnimation{ transition.id, ElementAnimationOrigin::Transition, start_value, start_time, 0.0f, 1, false };
  1971. }
  1972. bool result = it->AddKey(duration, target_value, *this, transition.tween, true);
  1973. if (result)
  1974. SetProperty(transition.id, start_value);
  1975. else
  1976. animations.erase(it);
  1977. return result;
  1978. }
  1979. void Element::UpdateTransition()
  1980. {
  1981. if(dirty_transition)
  1982. {
  1983. dirty_transition = false;
  1984. // Remove all transitions that are no longer in our local list
  1985. const TransitionList& keep_transitions = GetComputedValues().transition;
  1986. if (keep_transitions.all)
  1987. return;
  1988. auto it_remove = animations.end();
  1989. if (keep_transitions.none)
  1990. {
  1991. // All transitions should be removed, but only touch the animations that originate from the 'transition' property.
  1992. // Move all animations to be erased in a valid state at the end of the list, and erase later.
  1993. it_remove = std::partition(animations.begin(), animations.end(),
  1994. [](const ElementAnimation& animation) -> bool { return !animation.IsTransition(); }
  1995. );
  1996. }
  1997. else
  1998. {
  1999. // Only remove the transitions that are not in our keep list.
  2000. const auto& keep_transitions_list = keep_transitions.transitions;
  2001. it_remove = std::partition(animations.begin(), animations.end(),
  2002. [&keep_transitions_list](const ElementAnimation& animation) -> bool {
  2003. if (!animation.IsTransition())
  2004. return true;
  2005. auto it = std::find_if(keep_transitions_list.begin(), keep_transitions_list.end(),
  2006. [&animation](const Transition& transition) { return animation.GetPropertyId() == transition.id; }
  2007. );
  2008. bool keep_animation = (it != keep_transitions_list.end());
  2009. return keep_animation;
  2010. }
  2011. );
  2012. }
  2013. // We can decide what to do with ended animations here, just removing them seems to be the CSS approach.
  2014. for (auto it = it_remove; it != animations.end(); ++it)
  2015. RemoveProperty(it->GetPropertyId());
  2016. animations.erase(it_remove, animations.end());
  2017. }
  2018. }
  2019. void Element::UpdateAnimation()
  2020. {
  2021. if (dirty_animation)
  2022. {
  2023. dirty_animation = false;
  2024. const AnimationList& animation_list = element_meta->computed_values.animation;
  2025. bool element_has_animations = (!animation_list.empty() || !animations.empty());
  2026. StyleSheet* stylesheet = nullptr;
  2027. if (element_has_animations && (stylesheet = GetStyleSheet().get()))
  2028. {
  2029. // Remove existing animations
  2030. // Note: We are effectively restarting all animations whenever 'drty_animation' is set. Use the dirty flag with care,
  2031. // or find another approach which only updates actual "dirty" animations.
  2032. {
  2033. // We only touch the animations that originate from the 'animation' property.
  2034. auto it_remove = std::partition(animations.begin(), animations.end(),
  2035. [](const ElementAnimation & animation) { return animation.GetOrigin() != ElementAnimationOrigin::Animation; }
  2036. );
  2037. // We can decide what to do with ended animations here, should be consistent with removal of transitions.
  2038. for (auto it = it_remove; it != animations.end(); ++it)
  2039. RemoveProperty(it->GetPropertyId());
  2040. animations.erase(it_remove, animations.end());
  2041. }
  2042. // Start animations
  2043. for (const auto& animation : animation_list)
  2044. {
  2045. const Keyframes* keyframes_ptr = stylesheet->GetKeyframes(animation.name);
  2046. if (keyframes_ptr && keyframes_ptr->blocks.size() >= 1 && !animation.paused)
  2047. {
  2048. auto& property_ids = keyframes_ptr->property_ids;
  2049. auto& blocks = keyframes_ptr->blocks;
  2050. bool has_from_key = (blocks[0].normalized_time == 0);
  2051. bool has_to_key = (blocks.back().normalized_time == 1);
  2052. // If the first key defines initial conditions for a given property, use those values, else, use this element's current values.
  2053. for (PropertyId id : property_ids)
  2054. StartAnimation(id, (has_from_key ? blocks[0].properties.GetProperty(id) : nullptr), animation.num_iterations, animation.alternate, animation.delay, true);
  2055. // Need to skip the first and last keys if they set the initial and end conditions, respectively.
  2056. for (int i = (has_from_key ? 1 : 0); i < (int)blocks.size() + (has_to_key ? -1 : 0); i++)
  2057. {
  2058. // Add properties of current key to animation
  2059. float time = blocks[i].normalized_time * animation.duration;
  2060. for (auto& property : blocks[i].properties.GetProperties())
  2061. AddAnimationKeyTime(property.first, &property.second, time, animation.tween);
  2062. }
  2063. // If the last key defines end conditions for a given property, use those values, else, use this element's current values.
  2064. float time = animation.duration;
  2065. for (PropertyId id : property_ids)
  2066. AddAnimationKeyTime(id, (has_to_key ? blocks.back().properties.GetProperty(id) : nullptr), time, animation.tween);
  2067. }
  2068. }
  2069. }
  2070. }
  2071. }
  2072. void Element::AdvanceAnimations()
  2073. {
  2074. if (!animations.empty())
  2075. {
  2076. double time = Clock::GetElapsedTime();
  2077. for (auto& animation : animations)
  2078. {
  2079. Property property = animation.UpdateAndGetProperty(time, *this);
  2080. if (property.unit != Property::UNKNOWN)
  2081. SetProperty(animation.GetPropertyId(), property);
  2082. }
  2083. // Move all completed animations to the end of the list
  2084. auto it_completed = std::partition(animations.begin(), animations.end(), [](const ElementAnimation& animation) { return !animation.IsComplete(); });
  2085. std::vector<Dictionary> dictionary_list;
  2086. std::vector<bool> is_transition;
  2087. dictionary_list.reserve(animations.end() - it_completed);
  2088. is_transition.reserve(animations.end() - it_completed);
  2089. for (auto it = it_completed; it != animations.end(); ++it)
  2090. {
  2091. dictionary_list.emplace_back();
  2092. dictionary_list.back().emplace("property", StyleSheetSpecification::GetPropertyName(it->GetPropertyId()));
  2093. is_transition.push_back(it->IsTransition());
  2094. }
  2095. // Need to erase elements before submitting event, as iterators might be invalidated when calling external code.
  2096. animations.erase(it_completed, animations.end());
  2097. for (size_t i = 0; i < dictionary_list.size(); i++)
  2098. DispatchEvent(is_transition[i] ? EventId::Transitionend : EventId::Animationend, dictionary_list[i]);
  2099. }
  2100. }
  2101. void Element::DirtyTransformState(bool perspective_changed, bool transform_changed, bool parent_transform_changed)
  2102. {
  2103. for (size_t i = 0; i < children.size(); ++i)
  2104. {
  2105. children[i]->DirtyTransformState(false, false, transform_changed || parent_transform_changed);
  2106. }
  2107. if (perspective_changed)
  2108. {
  2109. this->transform_state_perspective_dirty = true;
  2110. }
  2111. if (transform_changed)
  2112. {
  2113. this->transform_state_transform_dirty = true;
  2114. }
  2115. if (parent_transform_changed)
  2116. {
  2117. this->transform_state_parent_transform_dirty = true;
  2118. }
  2119. }
  2120. void Element::UpdateTransformState()
  2121. {
  2122. if (!(transform_state_perspective_dirty || transform_state_transform_dirty || transform_state_parent_transform_dirty))
  2123. {
  2124. return;
  2125. }
  2126. const ComputedValues& computed = element_meta->computed_values;
  2127. if (!computed.transform && computed.perspective <= 0)
  2128. {
  2129. transform_state.reset();
  2130. transform_state_perspective_dirty = false;
  2131. transform_state_transform_dirty = false;
  2132. transform_state_parent_transform_dirty = false;
  2133. return;
  2134. }
  2135. if(transform_state_perspective_dirty || transform_state_transform_dirty)
  2136. {
  2137. Context *context = GetContext();
  2138. Vector2f pos = GetAbsoluteOffset(Box::BORDER);
  2139. Vector2f size = GetBox().GetSize(Box::BORDER);
  2140. if (transform_state_perspective_dirty)
  2141. {
  2142. bool have_perspective = false;
  2143. TransformState::Perspective perspective_value;
  2144. perspective_value.vanish = Vector2f(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f);
  2145. if (computed.perspective > 0.0f)
  2146. {
  2147. have_perspective = true;
  2148. // Compute the perspective value
  2149. perspective_value.distance = computed.perspective;
  2150. // Compute the perspective origin, if necessary
  2151. if (computed.perspective_origin_x.type == Style::PerspectiveOrigin::Percentage)
  2152. perspective_value.vanish.x = pos.x + computed.perspective_origin_x.value * 0.01f * size.x;
  2153. else
  2154. perspective_value.vanish.x = pos.x + computed.perspective_origin_x.value;
  2155. if (computed.perspective_origin_y.type == Style::PerspectiveOrigin::Percentage)
  2156. perspective_value.vanish.y = pos.y + computed.perspective_origin_y.value * 0.01f * size.y;
  2157. else
  2158. perspective_value.vanish.y = pos.y + computed.perspective_origin_y.value;
  2159. }
  2160. if (have_perspective && context)
  2161. {
  2162. if (!transform_state)
  2163. transform_state.reset(new TransformState);
  2164. perspective_value.view_size = context->GetDimensions();
  2165. transform_state->SetPerspective(&perspective_value);
  2166. }
  2167. else if (transform_state)
  2168. {
  2169. transform_state->SetPerspective(nullptr);
  2170. }
  2171. transform_state_perspective_dirty = false;
  2172. }
  2173. if (transform_state_transform_dirty)
  2174. {
  2175. bool have_local_perspective = false;
  2176. TransformState::LocalPerspective local_perspective;
  2177. bool have_transform = false;
  2178. Matrix4f transform_value = Matrix4f::Identity();
  2179. Vector3f transform_origin(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f, 0);
  2180. if (computed.transform)
  2181. {
  2182. int n = computed.transform->GetNumPrimitives();
  2183. for (int i = 0; i < n; ++i)
  2184. {
  2185. const Transforms::Primitive &primitive = computed.transform->GetPrimitive(i);
  2186. if (primitive.ResolvePerspective(local_perspective.distance, *this))
  2187. {
  2188. have_local_perspective = true;
  2189. }
  2190. Matrix4f matrix;
  2191. if (primitive.ResolveTransform(matrix, *this))
  2192. {
  2193. transform_value *= matrix;
  2194. have_transform = true;
  2195. }
  2196. }
  2197. // Compute the transform origin
  2198. if (computed.transform_origin_x.type == Style::TransformOrigin::Percentage)
  2199. transform_origin.x = pos.x + computed.transform_origin_x.value * size.x * 0.01f;
  2200. else
  2201. transform_origin.x = pos.x + computed.transform_origin_x.value;
  2202. if (computed.transform_origin_y.type == Style::TransformOrigin::Percentage)
  2203. transform_origin.y = pos.y + computed.transform_origin_y.value * size.y * 0.01f;
  2204. else
  2205. transform_origin.y = pos.y + computed.transform_origin_y.value;
  2206. transform_origin.z = computed.transform_origin_z;
  2207. }
  2208. if (have_local_perspective && context)
  2209. {
  2210. if (!transform_state)
  2211. transform_state.reset(new TransformState);
  2212. local_perspective.view_size = context->GetDimensions();
  2213. transform_state->SetLocalPerspective(&local_perspective);
  2214. }
  2215. else if(transform_state)
  2216. {
  2217. transform_state->SetLocalPerspective(nullptr);
  2218. }
  2219. if (have_transform)
  2220. {
  2221. // TODO: If we're using the global projection matrix
  2222. // (perspective < 0), then scale the coordinates from
  2223. // pixel space to 3D unit space.
  2224. // Transform the RmlUi context so that the computed `transform_origin'
  2225. // lies at the coordinate system origin.
  2226. transform_value =
  2227. Matrix4f::Translate(transform_origin)
  2228. * transform_value
  2229. * Matrix4f::Translate(-transform_origin);
  2230. if (!transform_state)
  2231. transform_state.reset(new TransformState);
  2232. transform_state->SetTransform(&transform_value);
  2233. }
  2234. else if (transform_state)
  2235. {
  2236. transform_state->SetTransform(nullptr);
  2237. }
  2238. transform_state_transform_dirty = false;
  2239. }
  2240. }
  2241. if (transform_state_parent_transform_dirty)
  2242. {
  2243. // We need to clean up from the top-most to the bottom-most dirt.
  2244. if (parent)
  2245. {
  2246. parent->UpdateTransformState();
  2247. }
  2248. if (transform_state)
  2249. {
  2250. // Store the parent's new full transform as our parent transform
  2251. Element *node = nullptr;
  2252. Matrix4f parent_transform;
  2253. for (node = parent; node; node = node->parent)
  2254. {
  2255. if (node->GetTransformState() && node->GetTransformState()->GetRecursiveTransform(&parent_transform))
  2256. {
  2257. transform_state->SetParentRecursiveTransform(&parent_transform);
  2258. break;
  2259. }
  2260. }
  2261. if (!node)
  2262. {
  2263. transform_state->SetParentRecursiveTransform(nullptr);
  2264. }
  2265. }
  2266. transform_state_parent_transform_dirty = false;
  2267. }
  2268. // If we neither have a local perspective, nor a perspective nor a
  2269. // transform, we don't need to keep the large TransformState object
  2270. // around. GetEffectiveTransformState() will then recursively visit
  2271. // parents in order to find a non-trivial TransformState.
  2272. if (transform_state && !transform_state->GetLocalPerspective(nullptr) && !transform_state->GetPerspective(nullptr) && !transform_state->GetTransform(nullptr))
  2273. {
  2274. transform_state.reset();
  2275. }
  2276. }
  2277. }
  2278. }