Element.cpp 76 KB

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