Element.cpp 77 KB

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