Element.cpp 75 KB

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