Element.cpp 75 KB

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