Element.cpp 74 KB

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