Element.cpp 77 KB

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