Element.cpp 75 KB

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