Element.cpp 77 KB

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