Element.cpp 77 KB

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