Element.cpp 76 KB

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