Element.cpp 76 KB

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