Element.cpp 77 KB

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