Element.cpp 76 KB

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