Element.cpp 77 KB

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