Element.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  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(true);
  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(true);
  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. return style->SetProperty(name, value);
  497. }
  498. // Removes a local property override on the element.
  499. void Element::RemoveProperty(const String& name)
  500. {
  501. style->RemoveProperty(name);
  502. }
  503. // Sets a local property override on the element to a pre-parsed value.
  504. bool Element::SetProperty(const String& name, const Property& property)
  505. {
  506. return style->SetProperty(name, property);
  507. }
  508. // Returns one of this element's properties.
  509. const Property* Element::GetProperty(const String& name)
  510. {
  511. return style->GetProperty(name);
  512. }
  513. // Returns one of this element's properties.
  514. const Property* Element::GetLocalProperty(const String& name)
  515. {
  516. return style->GetLocalProperty(name);
  517. }
  518. const PropertyMap * Element::GetLocalProperties()
  519. {
  520. return style->GetLocalProperties();
  521. }
  522. // Resolves one of this element's style.
  523. float Element::ResolveLengthPercentage(const Property *property, float base_value)
  524. {
  525. return style->ResolveLengthPercentage(property, base_value);
  526. }
  527. Vector2f Element::GetContainingBlock()
  528. {
  529. Vector2f containing_block(0, 0);
  530. if (offset_parent != NULL)
  531. {
  532. using namespace Style;
  533. Position position_property = GetPosition();
  534. const Box& parent_box = offset_parent->GetBox();
  535. if (position_property == Position::Static || position_property == Position::Relative)
  536. {
  537. containing_block = parent_box.GetSize();
  538. }
  539. else if(position_property == Position::Absolute || position_property == Position::Fixed)
  540. {
  541. containing_block = parent_box.GetSize(Box::PADDING);
  542. }
  543. }
  544. return containing_block;
  545. }
  546. Style::Position Element::GetPosition()
  547. {
  548. return element_meta->computed_values.position;
  549. }
  550. Style::Float Element::GetFloat()
  551. {
  552. return element_meta->computed_values.float_;
  553. }
  554. Style::Display Element::GetDisplay()
  555. {
  556. return element_meta->computed_values.display;
  557. }
  558. float Element::GetLineHeight()
  559. {
  560. return element_meta->computed_values.line_height.value;
  561. }
  562. // Returns this element's TransformState
  563. const TransformState *Element::GetTransformState() const noexcept
  564. {
  565. return transform_state.get();
  566. }
  567. // Returns the TransformStates that are effective for this element.
  568. void Element::GetEffectiveTransformState(
  569. const TransformState **local_perspective,
  570. const TransformState **perspective,
  571. const TransformState **transform
  572. ) const noexcept
  573. {
  574. if (local_perspective)
  575. {
  576. *local_perspective = nullptr;
  577. }
  578. if (perspective)
  579. {
  580. *perspective = nullptr;
  581. }
  582. if (transform)
  583. {
  584. *transform = nullptr;
  585. }
  586. const Element *perspective_node = nullptr, *transform_node = nullptr;
  587. // Find the TransformState to use for unprojecting.
  588. if (transform_state.get() && transform_state->GetLocalPerspective(nullptr))
  589. {
  590. if (local_perspective)
  591. {
  592. *local_perspective = transform_state.get();
  593. }
  594. }
  595. else
  596. {
  597. const Element *node = nullptr;
  598. for (node = parent; node; node = node->parent)
  599. {
  600. if (node->transform_state.get() && node->transform_state->GetPerspective(nullptr))
  601. {
  602. if (perspective)
  603. {
  604. *perspective = node->transform_state.get();
  605. }
  606. perspective_node = node;
  607. break;
  608. }
  609. }
  610. }
  611. // Find the TransformState to use for transforming.
  612. const Element *node = nullptr;
  613. for (node = this; node; node = node->parent)
  614. {
  615. if (node->transform_state.get() && node->transform_state->GetRecursiveTransform(nullptr))
  616. {
  617. if (transform)
  618. {
  619. *transform = node->transform_state.get();
  620. }
  621. transform_node = node;
  622. break;
  623. }
  624. }
  625. }
  626. // Project a 2D point in pixel coordinates onto the element's plane.
  627. Vector2f Element::Project(const Vector2f& point) const noexcept
  628. {
  629. const Context *context = GetContext();
  630. if (!context)
  631. {
  632. return point;
  633. }
  634. const TransformState *local_perspective, *perspective, *transform;
  635. GetEffectiveTransformState(&local_perspective, &perspective, &transform);
  636. Vector2i view_pos(0, 0);
  637. Vector2i view_size = context->GetDimensions();
  638. // Compute the line segment for ray picking, one point on the near and one on the far plane.
  639. // These need to be in clip space coordinates ([-1; 1]³) so that we an unproject them.
  640. Vector3f line_segment[2] =
  641. {
  642. // When unprojected, the intersection point on the near plane
  643. Vector3f(
  644. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  645. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  646. -1
  647. ),
  648. // When unprojected, the intersection point on the far plane
  649. Vector3f(
  650. (point.x - view_pos.x) / (0.5f * view_size.x) - 1.0f,
  651. (view_size.y - point.y - view_pos.y) / (0.5f * view_size.y) - 1.0f,
  652. 1
  653. )
  654. };
  655. // Find the TransformState to use for unprojecting.
  656. if (local_perspective)
  657. {
  658. TransformState::LocalPerspective the_local_perspective;
  659. local_perspective->GetLocalPerspective(&the_local_perspective);
  660. line_segment[0] = the_local_perspective.Unproject(line_segment[0]);
  661. line_segment[1] = the_local_perspective.Unproject(line_segment[1]);
  662. }
  663. else if (perspective)
  664. {
  665. TransformState::Perspective the_perspective;
  666. perspective->GetPerspective(&the_perspective);
  667. line_segment[0] = the_perspective.Unproject(line_segment[0]);
  668. line_segment[1] = the_perspective.Unproject(line_segment[1]);
  669. }
  670. else
  671. {
  672. line_segment[0] = context->GetViewState().Unproject(line_segment[0]);
  673. line_segment[1] = context->GetViewState().Unproject(line_segment[1]);
  674. }
  675. // Compute three points on the context's corners to define the element's plane.
  676. // It may seem elegant to base this computation on the element's size, but
  677. // there are elements with zero length or height.
  678. Vector3f element_rect[3] =
  679. {
  680. // Top-left corner
  681. Vector3f(0, 0, 0),
  682. // Top-right corner
  683. Vector3f((float)view_size.x, 0, 0),
  684. // Bottom-left corner
  685. Vector3f(0, (float)view_size.y, 0)
  686. };
  687. // Transform by the correct matrix
  688. if (transform)
  689. {
  690. element_rect[0] = transform->Transform(element_rect[0]);
  691. element_rect[1] = transform->Transform(element_rect[1]);
  692. element_rect[2] = transform->Transform(element_rect[2]);
  693. }
  694. Vector3f u = line_segment[0] - line_segment[1];
  695. Vector3f v = element_rect[1] - element_rect[0];
  696. Vector3f w = element_rect[2] - element_rect[0];
  697. // Now compute the intersection point of the line segment and the element's rectangle.
  698. // This is based on the algorithm discussed at Wikipedia
  699. // (http://en.wikipedia.org/wiki/Line-plane_intersection).
  700. Matrix4f A = Matrix4f::FromColumns(
  701. Vector4f(u, 0),
  702. Vector4f(v, 0),
  703. Vector4f(w, 0),
  704. Vector4f(0, 0, 0, 1)
  705. );
  706. if (A.Invert())
  707. {
  708. Vector3f factors = A * (line_segment[0] - element_rect[0]);
  709. Vector3f intersection3d = element_rect[0] + v * factors[1] + w * factors[2];
  710. Vector3f projected;
  711. if (transform)
  712. {
  713. projected = transform->Untransform(intersection3d);
  714. //ROCKET_ASSERT(fabs(projected.z) < 0.0001);
  715. }
  716. else
  717. {
  718. // FIXME: Is this correct?
  719. projected = intersection3d;
  720. }
  721. return Vector2f(projected.x, projected.y);
  722. }
  723. else
  724. {
  725. // The line segment is parallel to the element's plane.
  726. // Although, mathematically, it could also lie within the plane
  727. // (yielding infinitely many intersection points), we still
  728. // return a value that's pretty sure to not match anything,
  729. // since this case has nothing to do with the user `picking'
  730. // anything.
  731. float inf = std::numeric_limits< float >::infinity();
  732. return Vector2f(-inf, -inf);
  733. }
  734. }
  735. ElementStyleIterator Element::IteratePropertiesBegin() const
  736. {
  737. return style->begin();
  738. }
  739. ElementStyleIterator Element::IteratePropertiesEnd() const
  740. {
  741. return style->end();
  742. }
  743. // Sets or removes a pseudo-class on the element.
  744. void Element::SetPseudoClass(const String& pseudo_class, bool activate)
  745. {
  746. style->SetPseudoClass(pseudo_class, activate);
  747. }
  748. // Checks if a specific pseudo-class has been set on the element.
  749. bool Element::IsPseudoClassSet(const String& pseudo_class) const
  750. {
  751. return style->IsPseudoClassSet(pseudo_class);
  752. }
  753. // Checks if a complete set of pseudo-classes are set on the element.
  754. bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
  755. {
  756. for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
  757. {
  758. if (!IsPseudoClassSet(*i))
  759. return false;
  760. }
  761. return true;
  762. }
  763. // Gets a list of the current active pseudo classes
  764. const PseudoClassList& Element::GetActivePseudoClasses() const
  765. {
  766. return style->GetActivePseudoClasses();
  767. }
  768. /// Get the named attribute
  769. Variant* Element::GetAttribute(const String& name)
  770. {
  771. return GetIf(attributes, name);
  772. }
  773. // Checks if the element has a certain attribute.
  774. bool Element::HasAttribute(const String& name) const
  775. {
  776. return attributes.find(name) != attributes.end();
  777. }
  778. // Removes an attribute from the element
  779. void Element::RemoveAttribute(const String& name)
  780. {
  781. auto it = attributes.find(name);
  782. if (it != attributes.end())
  783. {
  784. attributes.erase(it);
  785. ElementAttributes changed_attributes;
  786. changed_attributes.emplace(name, Variant());
  787. OnAttributeChange(changed_attributes);
  788. }
  789. }
  790. // Gets the outer most focus element down the tree from this node
  791. Element* Element::GetFocusLeafNode()
  792. {
  793. // If there isn't a focus, then we are the leaf.
  794. if (!focus)
  795. {
  796. return this;
  797. }
  798. // Recurse down the tree until we found the leaf focus element
  799. Element* focus_element = focus;
  800. while (focus_element->focus)
  801. focus_element = focus_element->focus;
  802. return focus_element;
  803. }
  804. // Returns the element's context.
  805. Context* Element::GetContext() const
  806. {
  807. ElementDocument* document = GetOwnerDocument();
  808. if (document != NULL)
  809. return document->GetContext();
  810. return NULL;
  811. }
  812. // Set a group of attributes
  813. void Element::SetAttributes(const ElementAttributes& _attributes)
  814. {
  815. attributes.reserve(attributes.size() + _attributes.size());
  816. for (auto& pair : _attributes)
  817. attributes[pair.first] = pair.second;
  818. OnAttributeChange(_attributes);
  819. }
  820. // Returns the number of attributes on the element.
  821. int Element::GetNumAttributes() const
  822. {
  823. return (int)attributes.size();
  824. }
  825. // Iterates over all decorators attached to the element.
  826. bool Element::IterateDecorators(int& index, PseudoClassList& pseudo_classes, String& name, Decorator*& decorator, DecoratorDataHandle& decorator_data)
  827. {
  828. return decoration->IterateDecorators(index, pseudo_classes, name, decorator, decorator_data);
  829. }
  830. // Gets the name of the element.
  831. const String& Element::GetTagName() const
  832. {
  833. return tag;
  834. }
  835. // Gets the ID of the element.
  836. const String& Element::GetId() const
  837. {
  838. return id;
  839. }
  840. // Sets the ID of the element.
  841. void Element::SetId(const String& _id)
  842. {
  843. SetAttribute("id", _id);
  844. }
  845. // Gets the horizontal offset from the context's left edge to element's left border edge.
  846. float Element::GetAbsoluteLeft()
  847. {
  848. return GetAbsoluteOffset(Box::BORDER).x;
  849. }
  850. // Gets the vertical offset from the context's top edge to element's top border edge.
  851. float Element::GetAbsoluteTop()
  852. {
  853. return GetAbsoluteOffset(Box::BORDER).y;
  854. }
  855. // Gets the width of the left border of an element.
  856. float Element::GetClientLeft()
  857. {
  858. return GetBox().GetPosition(client_area).x;
  859. }
  860. // Gets the height of the top border of an element.
  861. float Element::GetClientTop()
  862. {
  863. return GetBox().GetPosition(client_area).y;
  864. }
  865. // Gets the inner width of the element.
  866. float Element::GetClientWidth()
  867. {
  868. return GetBox().GetSize(client_area).x - scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  869. }
  870. // Gets the inner height of the element.
  871. float Element::GetClientHeight()
  872. {
  873. return GetBox().GetSize(client_area).y - scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  874. }
  875. // Returns the element from which all offset calculations are currently computed.
  876. Element* Element::GetOffsetParent()
  877. {
  878. return offset_parent;
  879. }
  880. // Gets the distance from this element's left border to its offset parent's left border.
  881. float Element::GetOffsetLeft()
  882. {
  883. return relative_offset_base.x + relative_offset_position.x;
  884. }
  885. // Gets the distance from this element's top border to its offset parent's top border.
  886. float Element::GetOffsetTop()
  887. {
  888. return relative_offset_base.y + relative_offset_position.y;
  889. }
  890. // Gets the width of the element, including the client area, padding, borders and scrollbars, but not margins.
  891. float Element::GetOffsetWidth()
  892. {
  893. return GetBox().GetSize(Box::BORDER).x;
  894. }
  895. // Gets the height of the element, including the client area, padding, borders and scrollbars, but not margins.
  896. float Element::GetOffsetHeight()
  897. {
  898. return GetBox().GetSize(Box::BORDER).y;
  899. }
  900. // Gets the left scroll offset of the element.
  901. float Element::GetScrollLeft()
  902. {
  903. return scroll_offset.x;
  904. }
  905. // Sets the left scroll offset of the element.
  906. void Element::SetScrollLeft(float scroll_left)
  907. {
  908. const float new_offset = Math::Clamp(scroll_left, 0.0f, GetScrollWidth() - GetClientWidth());
  909. if (new_offset != scroll_offset.x)
  910. {
  911. scroll_offset.x = new_offset;
  912. scroll->UpdateScrollbar(ElementScroll::HORIZONTAL);
  913. DirtyOffset();
  914. DispatchEvent(EventId::Scroll, Dictionary());
  915. }
  916. }
  917. // Gets the top scroll offset of the element.
  918. float Element::GetScrollTop()
  919. {
  920. return scroll_offset.y;
  921. }
  922. // Sets the top scroll offset of the element.
  923. void Element::SetScrollTop(float scroll_top)
  924. {
  925. const float new_offset = Math::Clamp(scroll_top, 0.0f, GetScrollHeight() - GetClientHeight());
  926. if(new_offset != scroll_offset.y)
  927. {
  928. scroll_offset.y = new_offset;
  929. scroll->UpdateScrollbar(ElementScroll::VERTICAL);
  930. DirtyOffset();
  931. DispatchEvent(EventId::Scroll, Dictionary());
  932. }
  933. }
  934. // Gets the width of the scrollable content of the element; it includes the element padding but not its margin.
  935. float Element::GetScrollWidth()
  936. {
  937. return Math::Max(content_box.x, GetClientWidth());
  938. }
  939. // Gets the height of the scrollable content of the element; it includes the element padding but not its margin.
  940. float Element::GetScrollHeight()
  941. {
  942. return Math::Max(content_box.y, GetClientHeight());
  943. }
  944. // Gets the object representing the declarations of an element's style attributes.
  945. ElementStyle* Element::GetStyle() const
  946. {
  947. return style;
  948. }
  949. // Gets the document this element belongs to.
  950. ElementDocument* Element::GetOwnerDocument() const
  951. {
  952. #ifdef ROCKET_DEBUG
  953. if (parent && !owner_document)
  954. {
  955. // Since we have a parent but no owner_document, then we must be a 'loose' element -- that is, constructed
  956. // outside of a document and not attached to a child of any element in the hierarchy of a document.
  957. // This check ensures that we didn't just forget to set the owner document.
  958. ROCKET_ASSERT(!parent->GetOwnerDocument());
  959. }
  960. #endif
  961. return owner_document;
  962. }
  963. // Gets this element's parent node.
  964. Element* Element::GetParentNode() const
  965. {
  966. return parent;
  967. }
  968. // Gets the element immediately following this one in the tree.
  969. Element* Element::GetNextSibling() const
  970. {
  971. if (parent == NULL)
  972. return NULL;
  973. for (size_t i = 0; i < parent->children.size() - (parent->num_non_dom_children + 1); i++)
  974. {
  975. if (parent->children[i] == this)
  976. return parent->children[i + 1];
  977. }
  978. return NULL;
  979. }
  980. // Gets the element immediately preceding this one in the tree.
  981. Element* Element::GetPreviousSibling() const
  982. {
  983. if (parent == NULL)
  984. return NULL;
  985. for (size_t i = 1; i < parent->children.size() - parent->num_non_dom_children; i++)
  986. {
  987. if (parent->children[i] == this)
  988. return parent->children[i - 1];
  989. }
  990. return NULL;
  991. }
  992. // Returns the first child of this element.
  993. Element* Element::GetFirstChild() const
  994. {
  995. if (GetNumChildren() > 0)
  996. return children[0];
  997. return NULL;
  998. }
  999. // Gets the last child of this element.
  1000. Element* Element::GetLastChild() const
  1001. {
  1002. if (GetNumChildren() > 0)
  1003. return *(children.end() - (num_non_dom_children + 1));
  1004. return NULL;
  1005. }
  1006. Element* Element::GetChild(int index) const
  1007. {
  1008. if (index < 0 || index >= (int) children.size())
  1009. return NULL;
  1010. return children[index];
  1011. }
  1012. int Element::GetNumChildren(bool include_non_dom_elements) const
  1013. {
  1014. return (int) children.size() - (include_non_dom_elements ? 0 : num_non_dom_children);
  1015. }
  1016. // Gets the markup and content of the element.
  1017. void Element::GetInnerRML(String& content) const
  1018. {
  1019. for (int i = 0; i < GetNumChildren(); i++)
  1020. {
  1021. children[i]->GetRML(content);
  1022. }
  1023. }
  1024. // Gets the markup and content of the element.
  1025. String Element::GetInnerRML() const {
  1026. String result;
  1027. GetInnerRML(result);
  1028. return result;
  1029. }
  1030. // Sets the markup and content of the element. All existing children will be replaced.
  1031. void Element::SetInnerRML(const String& rml)
  1032. {
  1033. // Remove all DOM children.
  1034. while ((int) children.size() > num_non_dom_children)
  1035. RemoveChild(children.front());
  1036. Factory::InstanceElementText(this, rml);
  1037. }
  1038. // Sets the current element as the focus object.
  1039. bool Element::Focus()
  1040. {
  1041. // Are we allowed focus?
  1042. Style::Focus focus_property = element_meta->computed_values.focus;
  1043. if (focus_property == Style::Focus::None)
  1044. return false;
  1045. // Ask our context if we can switch focus.
  1046. Context* context = GetContext();
  1047. if (context == NULL)
  1048. return false;
  1049. if (!context->OnFocusChange(this))
  1050. return false;
  1051. // Set this as the end of the focus chain.
  1052. focus = NULL;
  1053. // Update the focus chain up the hierarchy.
  1054. Element* element = this;
  1055. while (element->GetParentNode())
  1056. {
  1057. element->GetParentNode()->focus = element;
  1058. element = element->GetParentNode();
  1059. }
  1060. return true;
  1061. }
  1062. // Removes focus from from this element.
  1063. void Element::Blur()
  1064. {
  1065. if (parent)
  1066. {
  1067. Context* context = GetContext();
  1068. if (context == NULL)
  1069. return;
  1070. if (context->GetFocusElement() == this)
  1071. {
  1072. parent->Focus();
  1073. }
  1074. else if (parent->focus == this)
  1075. {
  1076. parent->focus = NULL;
  1077. }
  1078. }
  1079. }
  1080. // Fakes a mouse click on this element.
  1081. void Element::Click()
  1082. {
  1083. Context* context = GetContext();
  1084. if (context == NULL)
  1085. return;
  1086. context->GenerateClickEvent(this);
  1087. }
  1088. // Adds an event listener
  1089. void Element::AddEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1090. {
  1091. EventId id = EventSpecificationInterface::GetIdOrInsert(event);
  1092. event_dispatcher->AttachEvent(id, listener, in_capture_phase);
  1093. }
  1094. // Adds an event listener
  1095. void Element::AddEventListener(EventId id, EventListener* listener, bool in_capture_phase)
  1096. {
  1097. event_dispatcher->AttachEvent(id, listener, in_capture_phase);
  1098. }
  1099. // Removes an event listener from this element.
  1100. void Element::RemoveEventListener(const String& event, EventListener* listener, bool in_capture_phase)
  1101. {
  1102. EventId id = EventSpecificationInterface::GetIdOrInsert(event);
  1103. event_dispatcher->DetachEvent(id, listener, in_capture_phase);
  1104. }
  1105. // Removes an event listener from this element.
  1106. void Element::RemoveEventListener(EventId id, EventListener* listener, bool in_capture_phase)
  1107. {
  1108. event_dispatcher->DetachEvent(id, listener, in_capture_phase);
  1109. }
  1110. // Dispatches the specified event
  1111. bool Element::DispatchEvent(const String& type, const Dictionary& parameters)
  1112. {
  1113. const EventSpecification& specification = EventSpecificationInterface::GetOrInsert(type);
  1114. return event_dispatcher->DispatchEvent(this, specification.id, type, parameters, specification.interruptible, specification.bubbles, specification.default_action_phase);
  1115. }
  1116. // Dispatches the specified event
  1117. bool Element::DispatchEvent(const String& type, const Dictionary& parameters, bool interruptible, bool bubbles)
  1118. {
  1119. const EventSpecification& specification = EventSpecificationInterface::GetOrInsert(type);
  1120. return event_dispatcher->DispatchEvent(this, specification.id, type, parameters, interruptible, bubbles, specification.default_action_phase);
  1121. }
  1122. // Dispatches the specified event
  1123. bool Element::DispatchEvent(EventId id, const Dictionary& parameters)
  1124. {
  1125. const EventSpecification& specification = EventSpecificationInterface::Get(id);
  1126. return event_dispatcher->DispatchEvent(this, specification.id, specification.type, parameters, specification.interruptible, specification.bubbles, specification.default_action_phase);
  1127. }
  1128. // Scrolls the parent element's contents so that this element is visible.
  1129. void Element::ScrollIntoView(bool align_with_top)
  1130. {
  1131. Vector2f size(0, 0);
  1132. if (!align_with_top)
  1133. {
  1134. size.y = main_box.GetOffset().y +
  1135. main_box.GetSize(Box::BORDER).y;
  1136. }
  1137. Element* scroll_parent = parent;
  1138. while (scroll_parent != NULL)
  1139. {
  1140. Style::Overflow overflow_x_property = scroll_parent->GetComputedValues().overflow_x;
  1141. Style::Overflow overflow_y_property = scroll_parent->GetComputedValues().overflow_y;
  1142. if ((overflow_x_property != Style::Overflow::Visible &&
  1143. scroll_parent->GetScrollWidth() > scroll_parent->GetClientWidth()) ||
  1144. (overflow_y_property != Style::Overflow::Visible &&
  1145. scroll_parent->GetScrollHeight() > scroll_parent->GetClientHeight()))
  1146. {
  1147. Vector2f offset = scroll_parent->GetAbsoluteOffset(Box::BORDER) - GetAbsoluteOffset(Box::BORDER);
  1148. Vector2f scroll_offset(scroll_parent->GetScrollLeft(), scroll_parent->GetScrollTop());
  1149. scroll_offset -= offset;
  1150. scroll_offset.x += scroll_parent->GetClientLeft();
  1151. scroll_offset.y += scroll_parent->GetClientTop();
  1152. if (!align_with_top)
  1153. scroll_offset.y -= (scroll_parent->GetClientHeight() - size.y);
  1154. if (overflow_x_property != Style::Overflow::Visible)
  1155. scroll_parent->SetScrollLeft(scroll_offset.x);
  1156. if (overflow_y_property != Style::Overflow::Visible)
  1157. scroll_parent->SetScrollTop(scroll_offset.y);
  1158. }
  1159. scroll_parent = scroll_parent->GetParentNode();
  1160. }
  1161. }
  1162. // Appends a child to this element
  1163. void Element::AppendChild(Element* child, bool dom_element)
  1164. {
  1165. child->AddReference();
  1166. child->SetParent(this);
  1167. if (dom_element)
  1168. children.insert(children.end() - num_non_dom_children, child);
  1169. else
  1170. {
  1171. children.push_back(child);
  1172. num_non_dom_children++;
  1173. }
  1174. child->GetStyle()->DirtyDefinition();
  1175. Element* ancestor = child;
  1176. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1177. ancestor->OnChildAdd(child);
  1178. DirtyStackingContext();
  1179. DirtyStructure();
  1180. if (dom_element)
  1181. DirtyLayout();
  1182. }
  1183. // Adds a child to this element, directly after the adjacent element. Inherits
  1184. // the dom/non-dom status from the adjacent element.
  1185. void Element::InsertBefore(Element* child, Element* adjacent_element)
  1186. {
  1187. // Find the position in the list of children of the adjacent element. If
  1188. // it's NULL or we can't find it, then we insert it at the end of the dom
  1189. // children, as a dom element.
  1190. size_t child_index = 0;
  1191. bool found_child = false;
  1192. if (adjacent_element)
  1193. {
  1194. for (child_index = 0; child_index < children.size(); child_index++)
  1195. {
  1196. if (children[child_index] == adjacent_element)
  1197. {
  1198. found_child = true;
  1199. break;
  1200. }
  1201. }
  1202. }
  1203. if (found_child)
  1204. {
  1205. child->AddReference();
  1206. child->SetParent(this);
  1207. if ((int) child_index >= GetNumChildren())
  1208. num_non_dom_children++;
  1209. else
  1210. DirtyLayout();
  1211. children.insert(children.begin() + child_index, child);
  1212. child->GetStyle()->DirtyDefinition();
  1213. Element* ancestor = child;
  1214. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1215. ancestor->OnChildAdd(child);
  1216. DirtyStackingContext();
  1217. DirtyStructure();
  1218. }
  1219. else
  1220. {
  1221. AppendChild(child);
  1222. }
  1223. }
  1224. // Replaces the second node with the first node.
  1225. bool Element::ReplaceChild(Element* inserted_element, Element* replaced_element)
  1226. {
  1227. ElementList::iterator insertion_point = children.begin();
  1228. while (insertion_point != children.end() && *insertion_point != replaced_element)
  1229. {
  1230. ++insertion_point;
  1231. }
  1232. if (insertion_point == children.end())
  1233. {
  1234. AppendChild(inserted_element);
  1235. return false;
  1236. }
  1237. inserted_element->AddReference();
  1238. inserted_element->SetParent(this);
  1239. children.insert(insertion_point, inserted_element);
  1240. RemoveChild(replaced_element);
  1241. inserted_element->GetStyle()->DirtyDefinition();
  1242. Element* ancestor = inserted_element;
  1243. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1244. ancestor->OnChildAdd(inserted_element);
  1245. return true;
  1246. }
  1247. // Removes the specified child
  1248. bool Element::RemoveChild(Element* child)
  1249. {
  1250. size_t child_index = 0;
  1251. for (ElementList::iterator itr = children.begin(); itr != children.end(); ++itr)
  1252. {
  1253. // Add the element to the delete list
  1254. if ((*itr) == child)
  1255. {
  1256. // Inform the context of the element's pending removal (if we have a valid context).
  1257. Context* context = GetContext();
  1258. if (context)
  1259. context->OnElementRemove(child);
  1260. Element* ancestor = child;
  1261. for (int i = 0; i <= ChildNotifyLevels && ancestor; i++, ancestor = ancestor->GetParentNode())
  1262. ancestor->OnChildRemove(child);
  1263. if (child_index >= children.size() - num_non_dom_children)
  1264. num_non_dom_children--;
  1265. deleted_children.push_back(child);
  1266. children.erase(itr);
  1267. // Remove the child element as the focussed child of this element.
  1268. if (child == focus)
  1269. {
  1270. focus = NULL;
  1271. // If this child (or a descendant of this child) is the context's currently
  1272. // focussed element, set the focus to us instead.
  1273. Context* context = GetContext();
  1274. if (context != NULL)
  1275. {
  1276. Element* focus_element = context->GetFocusElement();
  1277. while (focus_element != NULL)
  1278. {
  1279. if (focus_element == child)
  1280. {
  1281. Focus();
  1282. break;
  1283. }
  1284. focus_element = focus_element->GetParentNode();
  1285. }
  1286. }
  1287. }
  1288. DirtyLayout();
  1289. DirtyStackingContext();
  1290. DirtyStructure();
  1291. return true;
  1292. }
  1293. child_index++;
  1294. }
  1295. return false;
  1296. }
  1297. bool Element::HasChildNodes() const
  1298. {
  1299. return (int) children.size() > num_non_dom_children;
  1300. }
  1301. Element* Element::GetElementById(const String& id)
  1302. {
  1303. // Check for special-case tokens.
  1304. if (id == "#self")
  1305. return this;
  1306. else if (id == "#document")
  1307. return GetOwnerDocument();
  1308. else if (id == "#parent")
  1309. return this->parent;
  1310. else
  1311. {
  1312. Element* search_root = GetOwnerDocument();
  1313. if (search_root == NULL)
  1314. search_root = this;
  1315. return ElementUtilities::GetElementById(search_root, id);
  1316. }
  1317. }
  1318. // Get all elements with the given tag.
  1319. void Element::GetElementsByTagName(ElementList& elements, const String& tag)
  1320. {
  1321. return ElementUtilities::GetElementsByTagName(elements, this, tag);
  1322. }
  1323. // Get all elements with the given class set on them.
  1324. void Element::GetElementsByClassName(ElementList& elements, const String& class_name)
  1325. {
  1326. return ElementUtilities::GetElementsByClassName(elements, this, class_name);
  1327. }
  1328. // Access the event dispatcher
  1329. EventDispatcher* Element::GetEventDispatcher() const
  1330. {
  1331. return event_dispatcher;
  1332. }
  1333. String Element::GetEventDispatcherSummary() const
  1334. {
  1335. return event_dispatcher->ToString();
  1336. }
  1337. // Access the element background.
  1338. ElementBackground* Element::GetElementBackground() const
  1339. {
  1340. return background;
  1341. }
  1342. // Access the element border.
  1343. ElementBorder* Element::GetElementBorder() const
  1344. {
  1345. return border;
  1346. }
  1347. // Access the element decorators
  1348. ElementDecoration* Element::GetElementDecoration() const
  1349. {
  1350. return decoration;
  1351. }
  1352. // Returns the element's scrollbar functionality.
  1353. ElementScroll* Element::GetElementScroll() const
  1354. {
  1355. return scroll;
  1356. }
  1357. int Element::GetClippingIgnoreDepth()
  1358. {
  1359. if (clipping_state_dirty)
  1360. {
  1361. IsClippingEnabled();
  1362. }
  1363. return clipping_ignore_depth;
  1364. }
  1365. bool Element::IsClippingEnabled()
  1366. {
  1367. if (clipping_state_dirty)
  1368. {
  1369. const auto& computed = GetComputedValues();
  1370. // Is clipping enabled for this element, yes unless both overlow properties are set to visible
  1371. clipping_enabled = computed.overflow_x != Style::Overflow::Visible
  1372. || computed.overflow_y != Style::Overflow::Visible;
  1373. // Get the clipping ignore depth from the clip property
  1374. clipping_ignore_depth = computed.clip.number;
  1375. clipping_state_dirty = false;
  1376. }
  1377. return clipping_enabled;
  1378. }
  1379. // Gets the render interface owned by this element's context.
  1380. RenderInterface* Element::GetRenderInterface()
  1381. {
  1382. Context* context = GetContext();
  1383. if (context != NULL)
  1384. return context->GetRenderInterface();
  1385. return Rocket::Core::GetRenderInterface();
  1386. }
  1387. void Element::SetInstancer(ElementInstancer* _instancer)
  1388. {
  1389. // Only record the first instancer being set as some instancers call other instancers to do their dirty work, in
  1390. // which case we don't want to update the lowest level instancer.
  1391. if (instancer == NULL)
  1392. {
  1393. instancer = _instancer;
  1394. instancer->AddReference();
  1395. }
  1396. }
  1397. // Forces the element to generate a local stacking context, regardless of the value of its z-index property.
  1398. void Element::ForceLocalStackingContext()
  1399. {
  1400. local_stacking_context_forced = true;
  1401. local_stacking_context = true;
  1402. DirtyStackingContext();
  1403. }
  1404. // Called during the update loop after children are rendered.
  1405. void Element::OnUpdate()
  1406. {
  1407. }
  1408. // Called during render after backgrounds, borders, decorators, but before children, are rendered.
  1409. void Element::OnRender()
  1410. {
  1411. }
  1412. void Element::OnResize()
  1413. {
  1414. }
  1415. // Called during a layout operation, when the element is being positioned and sized.
  1416. void Element::OnLayout()
  1417. {
  1418. }
  1419. // Called when attributes on the element are changed.
  1420. void Element::OnAttributeChange(const ElementAttributes& changed_attributes)
  1421. {
  1422. auto it = changed_attributes.find("id");
  1423. if (it != changed_attributes.end())
  1424. {
  1425. id = it->second.Get<String>();
  1426. style->DirtyDefinition();
  1427. }
  1428. it = changed_attributes.find("class");
  1429. if (it != changed_attributes.end())
  1430. {
  1431. style->SetClassNames(it->second.Get<String>());
  1432. }
  1433. // Add any inline style declarations.
  1434. it = changed_attributes.find("style");
  1435. if (it != changed_attributes.end())
  1436. {
  1437. PropertyDictionary properties;
  1438. StyleSheetParser parser;
  1439. parser.ParseProperties(properties, it->second.Get<String>());
  1440. Rocket::Core::PropertyMap property_map = properties.GetProperties();
  1441. for (Rocket::Core::PropertyMap::iterator i = property_map.begin(); i != property_map.end(); ++i)
  1442. {
  1443. SetProperty((*i).first, (*i).second);
  1444. }
  1445. }
  1446. }
  1447. // Called when properties on the element are changed.
  1448. void Element::OnPropertyChange(const PropertyNameList& changed_properties)
  1449. {
  1450. bool all_dirty = false;
  1451. {
  1452. auto& registered_properties = StyleSheetSpecification::GetRegisteredProperties();
  1453. if (&registered_properties == &changed_properties || registered_properties == changed_properties)
  1454. all_dirty = true;
  1455. }
  1456. if (!IsLayoutDirty())
  1457. {
  1458. if (all_dirty)
  1459. {
  1460. DirtyLayout();
  1461. }
  1462. else
  1463. {
  1464. // Force a relayout if any of the changed properties require it.
  1465. for (PropertyNameList::const_iterator i = changed_properties.begin(); i != changed_properties.end(); ++i)
  1466. {
  1467. const PropertyDefinition* property_definition = StyleSheetSpecification::GetProperty(*i);
  1468. if (property_definition)
  1469. {
  1470. if (property_definition->IsLayoutForced())
  1471. {
  1472. DirtyLayout();
  1473. break;
  1474. }
  1475. }
  1476. }
  1477. }
  1478. }
  1479. // Update the visibility.
  1480. if (all_dirty || changed_properties.find(VISIBILITY) != changed_properties.end() ||
  1481. changed_properties.find(DISPLAY) != changed_properties.end())
  1482. {
  1483. bool new_visibility = (element_meta->computed_values.display != Style::Display::None && element_meta->computed_values.visibility == Style::Visibility::Visible);
  1484. if (visible != new_visibility)
  1485. {
  1486. visible = new_visibility;
  1487. if (parent != NULL)
  1488. parent->DirtyStackingContext();
  1489. }
  1490. if (all_dirty ||
  1491. changed_properties.find(DISPLAY) != changed_properties.end())
  1492. {
  1493. if (parent != NULL)
  1494. parent->DirtyStructure();
  1495. }
  1496. }
  1497. // Fetch a new font face if it has been changed.
  1498. if (all_dirty ||
  1499. changed_properties.find(FONT_FAMILY) != changed_properties.end() ||
  1500. changed_properties.find(FONT_CHARSET) != changed_properties.end() ||
  1501. changed_properties.find(FONT_WEIGHT) != changed_properties.end() ||
  1502. changed_properties.find(FONT_STYLE) != changed_properties.end() ||
  1503. changed_properties.find(FONT_SIZE) != changed_properties.end())
  1504. {
  1505. // Fetch the new font face.
  1506. FontFaceHandle* new_font_face_handle = ElementUtilities::GetFontFaceHandle(element_meta->computed_values);
  1507. // If this is different from our current font face, then we've got to nuke
  1508. // all our characters and tell our parent that we have to be re-laid out.
  1509. if (new_font_face_handle != font_face_handle)
  1510. {
  1511. if (font_face_handle)
  1512. font_face_handle->RemoveReference();
  1513. font_face_handle = new_font_face_handle;
  1514. }
  1515. else if (new_font_face_handle != NULL)
  1516. new_font_face_handle->RemoveReference();
  1517. }
  1518. // Update the position.
  1519. if (all_dirty ||
  1520. changed_properties.find(LEFT) != changed_properties.end() ||
  1521. changed_properties.find(RIGHT) != changed_properties.end() ||
  1522. changed_properties.find(TOP) != changed_properties.end() ||
  1523. changed_properties.find(BOTTOM) != changed_properties.end())
  1524. {
  1525. // TODO: This should happen during/after layout, as the containing box is not properly defined yet
  1526. UpdateOffset();
  1527. DirtyOffset();
  1528. }
  1529. // Update the z-index.
  1530. if (all_dirty ||
  1531. changed_properties.find(Z_INDEX) != changed_properties.end())
  1532. {
  1533. Style::ZIndex z_index_property = element_meta->computed_values.z_index;
  1534. if (z_index_property.type == Style::ZIndex::Auto)
  1535. {
  1536. if (local_stacking_context &&
  1537. !local_stacking_context_forced)
  1538. {
  1539. // We're no longer acting as a stacking context.
  1540. local_stacking_context = false;
  1541. stacking_context_dirty = false;
  1542. stacking_context.clear();
  1543. }
  1544. // If our old z-index was not zero, then we must dirty our stacking context so we'll be re-indexed.
  1545. if (z_index != 0)
  1546. {
  1547. z_index = 0;
  1548. DirtyStackingContext();
  1549. }
  1550. }
  1551. else
  1552. {
  1553. float new_z_index = z_index_property.value;
  1554. if (new_z_index != z_index)
  1555. {
  1556. z_index = new_z_index;
  1557. if (parent != NULL)
  1558. parent->DirtyStackingContext();
  1559. }
  1560. if (!local_stacking_context)
  1561. {
  1562. local_stacking_context = true;
  1563. stacking_context_dirty = true;
  1564. }
  1565. }
  1566. }
  1567. // Dirty the background if it's changed.
  1568. if (all_dirty ||
  1569. changed_properties.find(BACKGROUND_COLOR) != changed_properties.end() ||
  1570. changed_properties.find(OPACITY) != changed_properties.end() ||
  1571. changed_properties.find(IMAGE_COLOR) != changed_properties.end()) {
  1572. background->DirtyBackground();
  1573. decoration->DirtyDecorators(true);
  1574. }
  1575. // Dirty the border if it's changed.
  1576. if (all_dirty ||
  1577. changed_properties.find(BORDER_TOP_WIDTH) != changed_properties.end() ||
  1578. changed_properties.find(BORDER_RIGHT_WIDTH) != changed_properties.end() ||
  1579. changed_properties.find(BORDER_BOTTOM_WIDTH) != changed_properties.end() ||
  1580. changed_properties.find(BORDER_LEFT_WIDTH) != changed_properties.end() ||
  1581. changed_properties.find(BORDER_TOP_COLOR) != changed_properties.end() ||
  1582. changed_properties.find(BORDER_RIGHT_COLOR) != changed_properties.end() ||
  1583. changed_properties.find(BORDER_BOTTOM_COLOR) != changed_properties.end() ||
  1584. changed_properties.find(BORDER_LEFT_COLOR) != changed_properties.end() ||
  1585. changed_properties.find(OPACITY) != changed_properties.end())
  1586. border->DirtyBorder();
  1587. // Check for clipping state changes
  1588. if (all_dirty ||
  1589. changed_properties.find(CLIP) != changed_properties.end() ||
  1590. changed_properties.find(OVERFLOW_X) != changed_properties.end() ||
  1591. changed_properties.find(OVERFLOW_Y) != changed_properties.end())
  1592. {
  1593. clipping_state_dirty = true;
  1594. }
  1595. // Check for `perspective' and `perspective-origin' changes
  1596. if (all_dirty ||
  1597. changed_properties.find(PERSPECTIVE) != changed_properties.end() ||
  1598. changed_properties.find(PERSPECTIVE_ORIGIN_X) != changed_properties.end() ||
  1599. changed_properties.find(PERSPECTIVE_ORIGIN_Y) != changed_properties.end())
  1600. {
  1601. DirtyTransformState(true, false, false);
  1602. }
  1603. // Check for `transform' and `transform-origin' changes
  1604. if (all_dirty ||
  1605. changed_properties.find(TRANSFORM) != changed_properties.end() ||
  1606. changed_properties.find(TRANSFORM_ORIGIN_X) != changed_properties.end() ||
  1607. changed_properties.find(TRANSFORM_ORIGIN_Y) != changed_properties.end() ||
  1608. changed_properties.find(TRANSFORM_ORIGIN_Z) != changed_properties.end())
  1609. {
  1610. DirtyTransformState(false, true, false);
  1611. }
  1612. // Check for `animation' changes
  1613. if (all_dirty || changed_properties.find(ANIMATION) != changed_properties.end())
  1614. {
  1615. DirtyAnimation();
  1616. }
  1617. }
  1618. // Called when a child node has been added somewhere in the hierarchy
  1619. void Element::OnChildAdd(Element* child)
  1620. {
  1621. }
  1622. // Called when a child node has been removed somewhere in the hierarchy
  1623. void Element::OnChildRemove(Element* child)
  1624. {
  1625. }
  1626. // Forces a re-layout of this element, and any other children required.
  1627. void Element::DirtyLayout()
  1628. {
  1629. Element* document = GetOwnerDocument();
  1630. if (document != NULL)
  1631. document->DirtyLayout();
  1632. }
  1633. // Forces a re-layout of this element, and any other children required.
  1634. bool Element::IsLayoutDirty()
  1635. {
  1636. Element* document = GetOwnerDocument();
  1637. if (document != NULL)
  1638. return document->IsLayoutDirty();
  1639. return false;
  1640. }
  1641. // Forces a reevaluation of applicable font effects.
  1642. void Element::DirtyFont()
  1643. {
  1644. for (size_t i = 0; i < children.size(); ++i)
  1645. children[i]->DirtyFont();
  1646. }
  1647. void Element::OnReferenceDeactivate()
  1648. {
  1649. if (instancer)
  1650. {
  1651. instancer->ReleaseElement(this);
  1652. }
  1653. else
  1654. {
  1655. // Hopefully we can just delete ourselves.
  1656. //delete this;
  1657. Log::Message(Log::LT_WARNING, "Leak detected: element %s not instanced via Rocket Factory. Unable to release.", GetAddress().c_str());
  1658. }
  1659. }
  1660. void Element::ProcessDefaultAction(Event& event)
  1661. {
  1662. if (event == EventId::Mousedown && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
  1663. event.GetParameter< int >("button", 0) == 0)
  1664. SetPseudoClass("active", true);
  1665. if (event == EventId::Mousescroll)
  1666. {
  1667. if (GetScrollHeight() > GetClientHeight())
  1668. {
  1669. Style::Overflow overflow_property = element_meta->computed_values.overflow_y;
  1670. if (overflow_property == Style::Overflow::Auto ||
  1671. overflow_property == Style::Overflow::Scroll)
  1672. {
  1673. // Stop the propagation if the current element has scrollbars.
  1674. // This prevents scrolling in parent elements, which is often unintended. If instead desired behavior is
  1675. // to scroll in parent elements when reaching top/bottom, move StopPropagation inside the next if statement.
  1676. event.StopPropagation();
  1677. const float wheel_delta = event.GetParameter< float >("wheel_delta", 0.f);
  1678. if ((wheel_delta < 0 && GetScrollTop() > 0) ||
  1679. (wheel_delta > 0 && GetScrollHeight() > GetScrollTop() + GetClientHeight()))
  1680. {
  1681. // Defined as three times the default line-height, multiplied by the dp ratio.
  1682. float default_scroll_length = 3.f * DefaultComputedValues.line_height.value;
  1683. if (const Context* context = GetContext())
  1684. default_scroll_length *= context->GetDensityIndependentPixelRatio();
  1685. SetScrollTop(GetScrollTop() + Math::RoundFloat(wheel_delta * default_scroll_length));
  1686. }
  1687. }
  1688. }
  1689. return;
  1690. }
  1691. if (event.GetPhase() == EventPhase::Target)
  1692. {
  1693. switch (event.GetId())
  1694. {
  1695. case EventId::Mouseover:
  1696. SetPseudoClass("hover", true);
  1697. break;
  1698. case EventId::Mouseout:
  1699. SetPseudoClass("hover", false);
  1700. break;
  1701. case EventId::Focus:
  1702. SetPseudoClass(FOCUS, true);
  1703. break;
  1704. case EventId::Blur:
  1705. SetPseudoClass(FOCUS, false);
  1706. break;
  1707. default:
  1708. break;
  1709. }
  1710. }
  1711. }
  1712. const Style::ComputedValues& Element::GetComputedValues() const
  1713. {
  1714. return element_meta->computed_values;
  1715. }
  1716. void Element::GetRML(String& content)
  1717. {
  1718. // First we start the open tag, add the attributes then close the open tag.
  1719. // Then comes the children in order, then we add our close tag.
  1720. content += "<";
  1721. content += tag;
  1722. for( auto& pair : attributes)
  1723. {
  1724. auto& name = pair.first;
  1725. auto& variant = pair.second;
  1726. String value;
  1727. if (variant.GetInto(value))
  1728. content += " " + name + "=\"" + value + "\"";
  1729. }
  1730. if (HasChildNodes())
  1731. {
  1732. content += ">";
  1733. GetInnerRML(content);
  1734. content += "</";
  1735. content += tag;
  1736. content += ">";
  1737. }
  1738. else
  1739. {
  1740. content += " />";
  1741. }
  1742. }
  1743. void Element::SetOwnerDocument(ElementDocument* document)
  1744. {
  1745. // If this element is a document, then never change owner_document. Otherwise, this can happen when attaching to the root element.
  1746. if(owner_document != document && owner_document != this)
  1747. {
  1748. owner_document = document;
  1749. for (Element* child : children)
  1750. child->SetOwnerDocument(document);
  1751. }
  1752. }
  1753. void Element::SetParent(Element* _parent)
  1754. {
  1755. // If there's an old parent, detach from it first.
  1756. if (parent &&
  1757. parent != _parent)
  1758. parent->RemoveChild(this);
  1759. parent = _parent;
  1760. SetOwnerDocument(parent ? parent->GetOwnerDocument() : nullptr);
  1761. }
  1762. void Element::ReleaseElements(ElementList& released_elements)
  1763. {
  1764. // Remove deleted children from this element.
  1765. while (!released_elements.empty())
  1766. {
  1767. Element* element = released_elements.back();
  1768. released_elements.pop_back();
  1769. // If this element has been added back into our list, then we remove our previous oustanding reference on it
  1770. // and continue.
  1771. if (std::find(children.begin(), children.end(), element) != children.end())
  1772. {
  1773. element->RemoveReference();
  1774. continue;
  1775. }
  1776. // Set the parent to NULL unless it's been reparented already.
  1777. if (element->GetParentNode() == this)
  1778. {
  1779. element->parent = nullptr;
  1780. element->SetOwnerDocument(nullptr);
  1781. }
  1782. element->RemoveReference();
  1783. }
  1784. }
  1785. void Element::DirtyOffset()
  1786. {
  1787. if(!offset_dirty)
  1788. {
  1789. offset_dirty = true;
  1790. if(transform_state)
  1791. DirtyTransformState(true, true, false);
  1792. // Not strictly true ... ?
  1793. for (size_t i = 0; i < children.size(); i++)
  1794. children[i]->DirtyOffset();
  1795. }
  1796. }
  1797. void Element::UpdateOffset()
  1798. {
  1799. using namespace Style;
  1800. const auto& computed = element_meta->computed_values;
  1801. Position position_property = computed.position;
  1802. if (position_property == Position::Absolute ||
  1803. position_property == Position::Fixed)
  1804. {
  1805. if (offset_parent != NULL)
  1806. {
  1807. const Box& parent_box = offset_parent->GetBox();
  1808. Vector2f containing_block = parent_box.GetSize(Box::PADDING);
  1809. // If the element is anchored left, then the position is offset by that resolved value.
  1810. if (computed.left.type != Left::Auto)
  1811. relative_offset_base.x = parent_box.GetEdge(Box::BORDER, Box::LEFT) + (ResolveValue(computed.left, containing_block.x) + GetBox().GetEdge(Box::MARGIN, Box::LEFT));
  1812. // If the element is anchored right, then the position is set first so the element's right-most edge
  1813. // (including margins) will render up against the containing box's right-most content edge, and then
  1814. // offset by the resolved value.
  1815. else if (computed.right.type != Right::Auto)
  1816. 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));
  1817. // If the element is anchored top, then the position is offset by that resolved value.
  1818. if (computed.top.type != Top::Auto)
  1819. relative_offset_base.y = parent_box.GetEdge(Box::BORDER, Box::TOP) + (ResolveValue(computed.top, containing_block.y) + GetBox().GetEdge(Box::MARGIN, Box::TOP));
  1820. // If the element is anchored bottom, then the position is set first so the element's right-most edge
  1821. // (including margins) will render up against the containing box's right-most content edge, and then
  1822. // offset by the resolved value.
  1823. else if (computed.bottom.type != Bottom::Auto)
  1824. 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));
  1825. }
  1826. }
  1827. else if (position_property == Position::Relative)
  1828. {
  1829. if (offset_parent != NULL)
  1830. {
  1831. const Box& parent_box = offset_parent->GetBox();
  1832. Vector2f containing_block = parent_box.GetSize();
  1833. if (computed.left.type != Left::Auto)
  1834. relative_offset_position.x = ResolveValue(computed.left, containing_block.x);
  1835. else if (computed.right.type != Right::Auto)
  1836. relative_offset_position.x = -1 * ResolveValue(computed.right, containing_block.x);
  1837. else
  1838. relative_offset_position.x = 0;
  1839. if (computed.top.type != Top::Auto)
  1840. relative_offset_position.y = ResolveValue(computed.top, containing_block.y);
  1841. else if (computed.bottom.type != Bottom::Auto)
  1842. relative_offset_position.y = -1 * ResolveValue(computed.bottom, containing_block.y);
  1843. else
  1844. relative_offset_position.y = 0;
  1845. }
  1846. }
  1847. else
  1848. {
  1849. relative_offset_position.x = 0;
  1850. relative_offset_position.y = 0;
  1851. }
  1852. }
  1853. void Element::BuildLocalStackingContext()
  1854. {
  1855. stacking_context_dirty = false;
  1856. stacking_context.clear();
  1857. BuildStackingContext(&stacking_context);
  1858. std::stable_sort(stacking_context.begin(), stacking_context.end(), ElementSortZIndex());
  1859. }
  1860. void Element::BuildStackingContext(ElementList* new_stacking_context)
  1861. {
  1862. // Build the list of ordered children. Our child list is sorted within the stacking context so stacked elements
  1863. // will render in the right order; ie, positioned elements will render on top of inline elements, which will render
  1864. // on top of floated elements, which will render on top of block elements.
  1865. std::vector< std::pair< Element*, float > > ordered_children;
  1866. for (size_t i = 0; i < children.size(); ++i)
  1867. {
  1868. Element* child = children[i];
  1869. if (!child->IsVisible())
  1870. continue;
  1871. std::pair< Element*, float > ordered_child;
  1872. ordered_child.first = child;
  1873. if (child->GetPosition() != Style::Position::Static)
  1874. ordered_child.second = 3;
  1875. else if (child->GetFloat() != Style::Float::None)
  1876. ordered_child.second = 1;
  1877. else if (child->GetDisplay() == Style::Display::Block)
  1878. ordered_child.second = 0;
  1879. else
  1880. ordered_child.second = 2;
  1881. ordered_children.push_back(ordered_child);
  1882. }
  1883. // Sort the list!
  1884. std::stable_sort(ordered_children.begin(), ordered_children.end(), ElementSortZOrder());
  1885. // Add the list of ordered children into the stacking context in order.
  1886. for (size_t i = 0; i < ordered_children.size(); ++i)
  1887. {
  1888. new_stacking_context->push_back(ordered_children[i].first);
  1889. if (!ordered_children[i].first->local_stacking_context)
  1890. ordered_children[i].first->BuildStackingContext(new_stacking_context);
  1891. }
  1892. }
  1893. void Element::DirtyStackingContext()
  1894. {
  1895. // The first ancestor of ours that doesn't have an automatic z-index is the ancestor that is establishing our local
  1896. // stacking context.
  1897. Element* stacking_context_parent = this;
  1898. while (stacking_context_parent != NULL &&
  1899. !stacking_context_parent->local_stacking_context)
  1900. stacking_context_parent = stacking_context_parent->GetParentNode();
  1901. if (stacking_context_parent != NULL)
  1902. stacking_context_parent->stacking_context_dirty = true;
  1903. }
  1904. void Element::DirtyStructure()
  1905. {
  1906. // Clear the cached owner document
  1907. structure_dirty = true;
  1908. }
  1909. void Element::DirtyParentStructure()
  1910. {
  1911. parent_structure_dirty = true;
  1912. }
  1913. void Element::UpdateStructure()
  1914. {
  1915. if (parent_structure_dirty)
  1916. {
  1917. // If children depend on structured selectors, they may need to be updated
  1918. const ElementDefinition* element_definition = GetStyle()->GetDefinition();
  1919. if (element_definition != NULL && element_definition->IsStructurallyVolatile())
  1920. {
  1921. GetStyle()->DirtyDefinition();
  1922. }
  1923. }
  1924. if (structure_dirty || parent_structure_dirty)
  1925. {
  1926. for (size_t i = 0; i < children.size(); ++i)
  1927. children[i]->DirtyParentStructure();
  1928. structure_dirty = false;
  1929. parent_structure_dirty = false;
  1930. }
  1931. }
  1932. 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)
  1933. {
  1934. bool result = false;
  1935. auto it_animation = StartAnimation(property_name, start_value, num_iterations, alternate_direction, delay);
  1936. if (it_animation != animations.end())
  1937. {
  1938. result = it_animation->AddKey(duration, target_value, *this, tween, true);
  1939. if (!result)
  1940. animations.erase(it_animation);
  1941. }
  1942. return result;
  1943. }
  1944. bool Element::AddAnimationKey(const String & property_name, const Property & target_value, float duration, Tween tween)
  1945. {
  1946. ElementAnimation* animation = nullptr;
  1947. for (auto& existing_animation : animations) {
  1948. if (existing_animation.GetPropertyName() == property_name) {
  1949. animation = &existing_animation;
  1950. break;
  1951. }
  1952. }
  1953. if (!animation)
  1954. return false;
  1955. bool result = animation->AddKey(animation->GetDuration() + duration, target_value, *this, tween, true);
  1956. return result;
  1957. }
  1958. ElementAnimationList::iterator Element::StartAnimation(const String & property_name, const Property* start_value, int num_iterations, bool alternate_direction, float delay)
  1959. {
  1960. auto it = std::find_if(animations.begin(), animations.end(), [&](const ElementAnimation& el) { return el.GetPropertyName() == property_name; });
  1961. if (it == animations.end())
  1962. {
  1963. animations.emplace_back();
  1964. it = animations.end() - 1;
  1965. }
  1966. Property value;
  1967. if (start_value)
  1968. {
  1969. value = *start_value;
  1970. if (!value.definition)
  1971. if(auto default_value = GetProperty(property_name))
  1972. value.definition = default_value->definition;
  1973. }
  1974. else if (auto default_value = GetProperty(property_name))
  1975. {
  1976. value = *default_value;
  1977. }
  1978. if (value.definition)
  1979. {
  1980. double start_time = Clock::GetElapsedTime() + (double)delay;
  1981. *it = ElementAnimation{ property_name, value, start_time, 0.0f, num_iterations, alternate_direction, false };
  1982. }
  1983. else
  1984. {
  1985. animations.erase(it);
  1986. it = animations.end();
  1987. }
  1988. return it;
  1989. }
  1990. bool Element::AddAnimationKeyTime(const String & property_name, const Property* target_value, float time, Tween tween)
  1991. {
  1992. if (!target_value)
  1993. target_value = GetProperty(property_name);
  1994. if (!target_value)
  1995. return false;
  1996. ElementAnimation* animation = nullptr;
  1997. for (auto& existing_animation : animations) {
  1998. if (existing_animation.GetPropertyName() == property_name) {
  1999. animation = &existing_animation;
  2000. break;
  2001. }
  2002. }
  2003. if (!animation)
  2004. return false;
  2005. bool result = animation->AddKey(time, *target_value, *this, tween, true);
  2006. return result;
  2007. }
  2008. bool Element::StartTransition(const Transition & transition, const Property& start_value, const Property & target_value)
  2009. {
  2010. auto it = std::find_if(animations.begin(), animations.end(), [&](const ElementAnimation& el) { return el.GetPropertyName() == transition.name; });
  2011. if (it != animations.end() && !it->IsTransition())
  2012. return false;
  2013. float duration = transition.duration;
  2014. double start_time = Clock::GetElapsedTime() + (double)transition.delay;
  2015. if (it == animations.end())
  2016. {
  2017. // Add transition as new animation
  2018. animations.push_back(
  2019. ElementAnimation{ transition.name, start_value, start_time, 0.0f, 1, false, true }
  2020. );
  2021. it = (animations.end() - 1);
  2022. }
  2023. else
  2024. {
  2025. // Compress the duration based on the progress of the current animation
  2026. float f = it->GetInterpolationFactor();
  2027. f = 1.0f - (1.0f - f)*transition.reverse_adjustment_factor;
  2028. duration = duration * f;
  2029. // Replace old transition
  2030. *it = ElementAnimation{ transition.name, start_value, start_time, 0.0f, 1, false, true };
  2031. }
  2032. bool result = it->AddKey(duration, target_value, *this, transition.tween, true);
  2033. if (result)
  2034. SetProperty(transition.name, start_value);
  2035. else
  2036. animations.erase(it);
  2037. return result;
  2038. }
  2039. void Element::DirtyAnimation()
  2040. {
  2041. dirty_animation = true;
  2042. }
  2043. void Element::UpdateAnimation()
  2044. {
  2045. if (dirty_animation)
  2046. {
  2047. const auto& animation_list = element_meta->computed_values.animation;
  2048. StyleSheet* stylesheet = nullptr;
  2049. if (!animation_list.empty() && (stylesheet = GetStyleSheet()))
  2050. {
  2051. for (auto& animation : animation_list)
  2052. {
  2053. Keyframes* keyframes_ptr = stylesheet->GetKeyframes(animation.name);
  2054. if (keyframes_ptr && keyframes_ptr->blocks.size() >= 1 && !animation.paused)
  2055. {
  2056. auto& property_names = keyframes_ptr->property_names;
  2057. auto& blocks = keyframes_ptr->blocks;
  2058. bool has_from_key = (blocks[0].normalized_time == 0);
  2059. bool has_to_key = (blocks.back().normalized_time == 1);
  2060. // If the first key defines initial conditions for a given property, use those values, else, use this element's current values.
  2061. for (auto& property : property_names)
  2062. StartAnimation(property, (has_from_key ? blocks[0].properties.GetProperty(property) : nullptr), animation.num_iterations, animation.alternate, animation.delay);
  2063. // Need to skip the first and last keys if they set the initial and end conditions, respectively.
  2064. for (int i = (has_from_key ? 1 : 0); i < (int)blocks.size() + (has_to_key ? -1 : 0); i++)
  2065. {
  2066. // Add properties of current key to animation
  2067. float time = blocks[i].normalized_time * animation.duration;
  2068. for (auto& property : blocks[i].properties.GetProperties())
  2069. AddAnimationKeyTime(property.first, &property.second, time, animation.tween);
  2070. }
  2071. // If the last key defines end conditions for a given property, use those values, else, use this element's current values.
  2072. float time = animation.duration;
  2073. for (auto& property : property_names)
  2074. AddAnimationKeyTime(property, (has_to_key ? blocks.back().properties.GetProperty(property) : nullptr), time, animation.tween);
  2075. }
  2076. }
  2077. }
  2078. dirty_animation = false;
  2079. }
  2080. }
  2081. void Element::AdvanceAnimations()
  2082. {
  2083. if (!animations.empty())
  2084. {
  2085. double time = Clock::GetElapsedTime();
  2086. for (auto& animation : animations)
  2087. {
  2088. Property property = animation.UpdateAndGetProperty(time, *this);
  2089. if (property.unit != Property::UNKNOWN)
  2090. SetProperty(animation.GetPropertyName(), property);
  2091. }
  2092. auto it_completed = std::remove_if(animations.begin(), animations.end(), [](const ElementAnimation& animation) { return animation.IsComplete(); });
  2093. std::vector<Dictionary> dictionary_list;
  2094. std::vector<bool> is_transition;
  2095. dictionary_list.reserve(animations.end() - it_completed);
  2096. is_transition.reserve(animations.end() - it_completed);
  2097. for (auto it = it_completed; it != animations.end(); ++it)
  2098. {
  2099. dictionary_list.emplace_back();
  2100. dictionary_list.back().emplace("property", it->GetPropertyName());
  2101. is_transition.push_back(it->IsTransition());
  2102. }
  2103. // Need to erase elements before submitting event, as iterators might be invalidated when calling external code.
  2104. animations.erase(it_completed, animations.end());
  2105. for (size_t i = 0; i < dictionary_list.size(); i++)
  2106. DispatchEvent(is_transition[i] ? EventId::Transitionend : EventId::Animationend, dictionary_list[i]);
  2107. }
  2108. }
  2109. void Element::DirtyTransformState(bool perspective_changed, bool transform_changed, bool parent_transform_changed)
  2110. {
  2111. for (size_t i = 0; i < children.size(); ++i)
  2112. {
  2113. children[i]->DirtyTransformState(false, false, transform_changed || parent_transform_changed);
  2114. }
  2115. if (perspective_changed)
  2116. {
  2117. this->transform_state_perspective_dirty = true;
  2118. }
  2119. if (transform_changed)
  2120. {
  2121. this->transform_state_transform_dirty = true;
  2122. }
  2123. if (parent_transform_changed)
  2124. {
  2125. this->transform_state_parent_transform_dirty = true;
  2126. }
  2127. }
  2128. void Element::UpdateTransformState()
  2129. {
  2130. if (!(transform_state_perspective_dirty || transform_state_transform_dirty || transform_state_parent_transform_dirty))
  2131. {
  2132. return;
  2133. }
  2134. const ComputedValues& computed = element_meta->computed_values;
  2135. if (!computed.transform && computed.perspective <= 0)
  2136. {
  2137. transform_state.reset();
  2138. transform_state_perspective_dirty = false;
  2139. transform_state_transform_dirty = false;
  2140. transform_state_parent_transform_dirty = false;
  2141. return;
  2142. }
  2143. if(transform_state_perspective_dirty || transform_state_transform_dirty)
  2144. {
  2145. Context *context = GetContext();
  2146. Vector2f pos = GetAbsoluteOffset(Box::BORDER);
  2147. Vector2f size = GetBox().GetSize(Box::BORDER);
  2148. if (transform_state_perspective_dirty)
  2149. {
  2150. bool have_perspective = false;
  2151. TransformState::Perspective perspective_value;
  2152. perspective_value.vanish = Vector2f(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f);
  2153. if (computed.perspective > 0.0f)
  2154. {
  2155. have_perspective = true;
  2156. // Compute the perspective value
  2157. perspective_value.distance = computed.perspective;
  2158. // Compute the perspective origin, if necessary
  2159. if (computed.perspective_origin_x.type == Style::PerspectiveOrigin::Percentage)
  2160. perspective_value.vanish.x = pos.x + computed.perspective_origin_x.value * 0.01f * size.x;
  2161. else
  2162. perspective_value.vanish.x = pos.x + computed.perspective_origin_x.value;
  2163. if (computed.perspective_origin_y.type == Style::PerspectiveOrigin::Percentage)
  2164. perspective_value.vanish.y = pos.y + computed.perspective_origin_y.value * 0.01f * size.y;
  2165. else
  2166. perspective_value.vanish.y = pos.y + computed.perspective_origin_y.value;
  2167. }
  2168. if (have_perspective && context)
  2169. {
  2170. if (!transform_state)
  2171. transform_state.reset(new TransformState);
  2172. perspective_value.view_size = context->GetDimensions();
  2173. transform_state->SetPerspective(&perspective_value);
  2174. }
  2175. else if (transform_state)
  2176. {
  2177. transform_state->SetPerspective(nullptr);
  2178. }
  2179. transform_state_perspective_dirty = false;
  2180. }
  2181. if (transform_state_transform_dirty)
  2182. {
  2183. bool have_local_perspective = false;
  2184. TransformState::LocalPerspective local_perspective;
  2185. bool have_transform = false;
  2186. Matrix4f transform_value = Matrix4f::Identity();
  2187. Vector3f transform_origin(pos.x + size.x * 0.5f, pos.y + size.y * 0.5f, 0);
  2188. if (computed.transform)
  2189. {
  2190. int n = computed.transform->GetNumPrimitives();
  2191. for (int i = 0; i < n; ++i)
  2192. {
  2193. const Transforms::Primitive &primitive = computed.transform->GetPrimitive(i);
  2194. if (primitive.ResolvePerspective(local_perspective.distance, *this))
  2195. {
  2196. have_local_perspective = true;
  2197. }
  2198. Matrix4f matrix;
  2199. if (primitive.ResolveTransform(matrix, *this))
  2200. {
  2201. transform_value *= matrix;
  2202. have_transform = true;
  2203. }
  2204. }
  2205. // Compute the transform origin
  2206. if (computed.transform_origin_x.type == Style::TransformOrigin::Percentage)
  2207. transform_origin.x = pos.x + computed.transform_origin_x.value * size.x * 0.01f;
  2208. else
  2209. transform_origin.x = pos.x + computed.transform_origin_x.value;
  2210. if (computed.transform_origin_y.type == Style::TransformOrigin::Percentage)
  2211. transform_origin.y = pos.y + computed.transform_origin_y.value * size.y * 0.01f;
  2212. else
  2213. transform_origin.y = pos.y + computed.transform_origin_y.value;
  2214. transform_origin.z = computed.transform_origin_z;
  2215. }
  2216. if (have_local_perspective && context)
  2217. {
  2218. if (!transform_state)
  2219. transform_state.reset(new TransformState);
  2220. local_perspective.view_size = context->GetDimensions();
  2221. transform_state->SetLocalPerspective(&local_perspective);
  2222. }
  2223. else if(transform_state)
  2224. {
  2225. transform_state->SetLocalPerspective(nullptr);
  2226. }
  2227. if (have_transform)
  2228. {
  2229. // TODO: If we're using the global projection matrix
  2230. // (perspective < 0), then scale the coordinates from
  2231. // pixel space to 3D unit space.
  2232. // Transform the Rocket context so that the computed `transform_origin'
  2233. // lies at the coordinate system origin.
  2234. transform_value =
  2235. Matrix4f::Translate(transform_origin)
  2236. * transform_value
  2237. * Matrix4f::Translate(-transform_origin);
  2238. if (!transform_state)
  2239. transform_state.reset(new TransformState);
  2240. transform_state->SetTransform(&transform_value);
  2241. }
  2242. else if (transform_state)
  2243. {
  2244. transform_state->SetTransform(nullptr);
  2245. }
  2246. transform_state_transform_dirty = false;
  2247. }
  2248. }
  2249. if (transform_state_parent_transform_dirty)
  2250. {
  2251. // We need to clean up from the top-most to the bottom-most dirt.
  2252. if (parent)
  2253. {
  2254. parent->UpdateTransformState();
  2255. }
  2256. if (transform_state)
  2257. {
  2258. // Store the parent's new full transform as our parent transform
  2259. Element *node = nullptr;
  2260. Matrix4f parent_transform;
  2261. for (node = parent; node; node = node->parent)
  2262. {
  2263. if (node->GetTransformState() && node->GetTransformState()->GetRecursiveTransform(&parent_transform))
  2264. {
  2265. transform_state->SetParentRecursiveTransform(&parent_transform);
  2266. break;
  2267. }
  2268. }
  2269. if (!node)
  2270. {
  2271. transform_state->SetParentRecursiveTransform(nullptr);
  2272. }
  2273. }
  2274. transform_state_parent_transform_dirty = false;
  2275. }
  2276. // If we neither have a local perspective, nor a perspective nor a
  2277. // transform, we don't need to keep the large TransformState object
  2278. // around. GetEffectiveTransformState() will then recursively visit
  2279. // parents in order to find a non-trivial TransformState.
  2280. if (transform_state && !transform_state->GetLocalPerspective(nullptr) && !transform_state->GetPerspective(nullptr) && !transform_state->GetTransform(nullptr))
  2281. {
  2282. transform_state.reset();
  2283. }
  2284. }
  2285. }
  2286. }