UIElement.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "CoreEvents.h"
  25. #include "Cursor.h"
  26. #include "HashSet.h"
  27. #include "Log.h"
  28. #include "ObjectAnimation.h"
  29. #include "ResourceCache.h"
  30. #include "Sort.h"
  31. #include "UI.h"
  32. #include "UIElement.h"
  33. #include "UIEvents.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  37. const char* horizontalAlignments[] =
  38. {
  39. "Left",
  40. "Center",
  41. "Right",
  42. 0
  43. };
  44. const char* verticalAlignments[] =
  45. {
  46. "Top",
  47. "Center",
  48. "Bottom",
  49. 0
  50. };
  51. static const char* focusModes[] =
  52. {
  53. "NotFocusable",
  54. "ResetFocus",
  55. "Focusable",
  56. "FocusableDefocusable",
  57. 0
  58. };
  59. static const char* dragDropModes[] =
  60. {
  61. "Disabled",
  62. "Source",
  63. "Target",
  64. "SourceAndTarget",
  65. 0
  66. };
  67. static const char* layoutModes[] =
  68. {
  69. "Free",
  70. "Horizontal",
  71. "Vertical",
  72. 0
  73. };
  74. extern const char* UI_CATEGORY;
  75. static bool CompareUIElements(const UIElement* lhs, const UIElement* rhs)
  76. {
  77. return lhs->GetPriority() < rhs->GetPriority();
  78. }
  79. template<> HorizontalAlignment Variant::Get<HorizontalAlignment>() const
  80. {
  81. return (HorizontalAlignment)GetInt();
  82. }
  83. template<> VerticalAlignment Variant::Get<VerticalAlignment>() const
  84. {
  85. return (VerticalAlignment)GetInt();
  86. }
  87. template<> FocusMode Variant::Get<FocusMode>() const
  88. {
  89. return (FocusMode)GetInt();
  90. }
  91. template<> LayoutMode Variant::Get<LayoutMode>() const
  92. {
  93. return (LayoutMode)GetInt();
  94. }
  95. XPathQuery UIElement::styleXPathQuery_("/elements/element[@type=$typeName]", "typeName:String");
  96. UIElement::UIElement(Context* context) :
  97. Animatable(context),
  98. parent_(0),
  99. clipBorder_(IntRect::ZERO),
  100. priority_(0),
  101. bringToFront_(false),
  102. bringToBack_(true),
  103. clipChildren_(false),
  104. sortChildren_(true),
  105. useDerivedOpacity_(true),
  106. enabled_(false),
  107. editable_(true),
  108. selected_(false),
  109. visible_(true),
  110. hovering_(false),
  111. internal_(false),
  112. focusMode_(FM_NOTFOCUSABLE),
  113. dragDropMode_(DD_DISABLED),
  114. layoutMode_(LM_FREE),
  115. layoutSpacing_(0),
  116. layoutBorder_(IntRect::ZERO),
  117. resizeNestingLevel_(0),
  118. layoutNestingLevel_(0),
  119. layoutMinSize_(0),
  120. indent_(0),
  121. indentSpacing_(16),
  122. position_(IntVector2::ZERO),
  123. positionDirty_(true),
  124. size_(IntVector2::ZERO),
  125. minSize_(IntVector2::ZERO),
  126. maxSize_(M_MAX_INT, M_MAX_INT),
  127. childOffset_(IntVector2::ZERO),
  128. horizontalAlignment_(HA_LEFT),
  129. verticalAlignment_(VA_TOP),
  130. opacity_(1.0f),
  131. opacityDirty_(true),
  132. derivedColorDirty_(true),
  133. sortOrderDirty_(false),
  134. colorGradient_(false),
  135. traversalMode_(TM_BREADTH_FIRST),
  136. elementEventSender_(false)
  137. {
  138. }
  139. UIElement::~UIElement()
  140. {
  141. // If child elements have outside references, detach them
  142. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i < children_.End(); ++i)
  143. {
  144. if (i->Refs() > 1)
  145. (*i)->Detach();
  146. }
  147. }
  148. void UIElement::RegisterObject(Context* context)
  149. {
  150. context->RegisterFactory<UIElement>(UI_CATEGORY);
  151. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_STRING, "Name", GetName, SetName, String, String::EMPTY, AM_FILE);
  152. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Position", GetPosition, SetPosition, IntVector2, IntVector2::ZERO, AM_FILE);
  153. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Size", GetSize, SetSize, IntVector2, IntVector2::ZERO, AM_FILE);
  154. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Min Size", GetMinSize, SetMinSize, IntVector2, IntVector2::ZERO, AM_FILE);
  155. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Max Size", GetMaxSize, SetMaxSize, IntVector2, IntVector2(M_MAX_INT, M_MAX_INT), AM_FILE);
  156. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment, horizontalAlignments, HA_LEFT, AM_FILE);
  157. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments, VA_TOP, AM_FILE);
  158. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTRECT, "Clip Border", GetClipBorder, SetClipBorder, IntRect, IntRect::ZERO, AM_FILE);
  159. ACCESSOR_ATTRIBUTE(UIElement, VAR_INT, "Priority", GetPriority, SetPriority, int, 0, AM_FILE);
  160. ACCESSOR_ATTRIBUTE(UIElement, VAR_FLOAT, "Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_FILE);
  161. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_COLOR, "Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_FILE);
  162. ATTRIBUTE(UIElement, VAR_COLOR, "Top Left Color", color_[0], Color::WHITE, AM_FILE);
  163. ATTRIBUTE(UIElement, VAR_COLOR, "Top Right Color", color_[1], Color::WHITE, AM_FILE);
  164. ATTRIBUTE(UIElement, VAR_COLOR, "Bottom Left Color", color_[2], Color::WHITE, AM_FILE);
  165. ATTRIBUTE(UIElement, VAR_COLOR, "Bottom Right Color", color_[3], Color::WHITE, AM_FILE);
  166. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, false, AM_FILE);
  167. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Editable", IsEditable, SetEditable, bool, true, AM_FILE);
  168. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Selected", IsSelected, SetSelected, bool, false, AM_FILE);
  169. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Visible", IsVisible, SetVisible, bool, true, AM_FILE);
  170. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Bring To Front", GetBringToFront, SetBringToFront, bool, false, AM_FILE);
  171. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Bring To Back", GetBringToBack, SetBringToBack, bool, true, AM_FILE);
  172. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Clip Children", GetClipChildren, SetClipChildren, bool, false, AM_FILE);
  173. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Use Derived Opacity", GetUseDerivedOpacity, SetUseDerivedOpacity, bool, true, AM_FILE);
  174. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Focus Mode", GetFocusMode, SetFocusMode, FocusMode, focusModes, FM_NOTFOCUSABLE, AM_FILE);
  175. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Drag And Drop Mode", GetDragDropMode, SetDragDropMode, unsigned, dragDropModes, DD_DISABLED, AM_FILE);
  176. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Layout Mode", GetLayoutMode, SetLayoutMode, LayoutMode, layoutModes, LM_FREE, AM_FILE);
  177. ACCESSOR_ATTRIBUTE(UIElement, VAR_INT, "Layout Spacing", GetLayoutSpacing, SetLayoutSpacing, int, 0, AM_FILE);
  178. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTRECT, "Layout Border", GetLayoutBorder, SetLayoutBorder, IntRect, IntRect::ZERO, AM_FILE);
  179. ACCESSOR_ATTRIBUTE(UIElement, VAR_INT, "Indent", GetIndent, SetIndent, int, 0, AM_FILE);
  180. ACCESSOR_ATTRIBUTE(UIElement, VAR_INT, "Indent Spacing", GetIndentSpacing, SetIndentSpacing, int, 16, AM_FILE);
  181. ATTRIBUTE(UIElement, VAR_VARIANTMAP, "Variables", vars_, Variant::emptyVariantMap, AM_FILE);
  182. }
  183. void UIElement::ApplyAttributes()
  184. {
  185. colorGradient_ = false;
  186. derivedColorDirty_ = true;
  187. for (unsigned i = 1; i < MAX_UIELEMENT_CORNERS; ++i)
  188. {
  189. if (color_[i] != color_[0])
  190. colorGradient_ = true;
  191. }
  192. }
  193. bool UIElement::LoadXML(const XMLElement& source, bool setInstanceDefault)
  194. {
  195. return LoadXML(source, 0, setInstanceDefault);
  196. }
  197. bool UIElement::LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault)
  198. {
  199. // Get style override if defined
  200. String styleName = source.GetAttribute("style");
  201. // Apply the style first, if the style file is available
  202. if (styleFile)
  203. {
  204. // If not defined, use type name
  205. if (styleName.Empty())
  206. styleName = GetTypeName();
  207. SetStyle(styleName, styleFile);
  208. }
  209. // The 'style' attribute value in the style file cannot be equals to original's applied style to prevent infinite loop
  210. else if (!styleName.Empty() && styleName != appliedStyle_)
  211. {
  212. // Attempt to use the default style file
  213. styleFile = GetDefaultStyle();
  214. if (styleFile)
  215. {
  216. // Remember the original applied style
  217. String appliedStyle(appliedStyle_);
  218. SetStyle(styleName, styleFile);
  219. appliedStyle_ = appliedStyle;
  220. }
  221. }
  222. // Then load rest of the attributes from the source
  223. if (!Animatable::LoadXML(source, setInstanceDefault))
  224. return false;
  225. unsigned nextInternalChild = 0;
  226. // Load child elements. Internal elements are not to be created as they already exist
  227. XMLElement childElem = source.GetChild("element");
  228. while (childElem)
  229. {
  230. bool internalElem = childElem.GetBool("internal");
  231. String typeName = childElem.GetAttribute("type");
  232. if (typeName.Empty())
  233. typeName = "UIElement";
  234. unsigned index = childElem.HasAttribute("index") ? childElem.GetUInt("index") : M_MAX_UNSIGNED;
  235. UIElement* child = 0;
  236. if (!internalElem)
  237. child = CreateChild(typeName, String::EMPTY, index);
  238. else
  239. {
  240. for (unsigned i = nextInternalChild; i < children_.Size(); ++i)
  241. {
  242. if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName)
  243. {
  244. child = children_[i];
  245. nextInternalChild = i + 1;
  246. break;
  247. }
  248. }
  249. if (!child)
  250. LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName());
  251. }
  252. if (child)
  253. {
  254. if (!styleFile)
  255. styleFile = GetDefaultStyle();
  256. if (!child->LoadXML(childElem, styleFile, setInstanceDefault))
  257. return false;
  258. }
  259. childElem = childElem.GetNext("element");
  260. }
  261. ApplyAttributes();
  262. return true;
  263. }
  264. bool UIElement::LoadChildXML(const XMLElement& childElem, XMLFile* styleFile, bool setInstanceDefault)
  265. {
  266. bool internalElem = childElem.GetBool("internal");
  267. if (internalElem)
  268. {
  269. LOGERROR("Loading internal child element is not supported");
  270. return false;
  271. }
  272. String typeName = childElem.GetAttribute("type");
  273. if (typeName.Empty())
  274. typeName = "UIElement";
  275. unsigned index = childElem.HasAttribute("index") ? childElem.GetUInt("index") : M_MAX_UNSIGNED;
  276. UIElement* child = CreateChild(typeName, String::EMPTY, index);
  277. if (child)
  278. {
  279. if (!styleFile)
  280. styleFile = GetDefaultStyle();
  281. if (!child->LoadXML(childElem, styleFile, setInstanceDefault))
  282. return false;
  283. }
  284. return true;
  285. }
  286. bool UIElement::SaveXML(XMLElement& dest) const
  287. {
  288. // Write type
  289. if (GetTypeName() != "UIElement")
  290. {
  291. if (!dest.SetString("type", GetTypeName()))
  292. return false;
  293. }
  294. // Write internal flag
  295. if (internal_)
  296. {
  297. if (!dest.SetBool("internal", internal_))
  298. return false;
  299. }
  300. // Write style
  301. if (!appliedStyle_.Empty() && appliedStyle_ != "UIElement")
  302. {
  303. if (!dest.SetAttribute("style", appliedStyle_))
  304. return false;
  305. }
  306. else if (internal_)
  307. {
  308. if (!dest.SetAttribute("style", "none"))
  309. return false;
  310. }
  311. // Write attributes
  312. if (!Animatable::SaveXML(dest))
  313. return false;
  314. // Write child elements
  315. for (unsigned i = 0; i < children_.Size(); ++i)
  316. {
  317. UIElement* element = children_[i];
  318. if (element->IsTemporary())
  319. continue;
  320. XMLElement childElem = dest.CreateChild("element");
  321. if (!element->SaveXML(childElem))
  322. return false;
  323. }
  324. // Filter UI-style and implicit attributes
  325. if (!FilterAttributes(dest))
  326. return false;
  327. return true;
  328. }
  329. void UIElement::Update(float timeStep)
  330. {
  331. }
  332. void UIElement::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  333. {
  334. // Reset hovering for next frame
  335. hovering_ = false;
  336. }
  337. void UIElement::GetDebugDrawBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  338. {
  339. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  340. int horizontalThickness = 1;
  341. int verticalThickness = 1;
  342. if (parent_)
  343. {
  344. switch (parent_->layoutMode_)
  345. {
  346. case LM_HORIZONTAL:
  347. verticalThickness +=2;
  348. break;
  349. case LM_VERTICAL:
  350. horizontalThickness += 2;
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. batch.SetColor(DEBUG_DRAW_COLOR, true);
  357. // Left
  358. batch.AddQuad(0, 0, horizontalThickness, size_.y_, 0, 0);
  359. // Top
  360. batch.AddQuad(0, 0, size_.x_, verticalThickness, 0, 0);
  361. // Right
  362. batch.AddQuad(size_.x_ - horizontalThickness, 0, horizontalThickness, size_.y_, 0, 0);
  363. // Bottom
  364. batch.AddQuad(0, size_.y_ - verticalThickness, size_.x_, verticalThickness, 0, 0);
  365. UIBatch::AddOrMerge(batch, batches);
  366. }
  367. bool UIElement::IsWithinScissor(const IntRect& currentScissor)
  368. {
  369. if (!visible_)
  370. return false;
  371. const IntVector2& screenPos = GetScreenPosition();
  372. return screenPos.x_ < currentScissor.right_ && screenPos.x_ + GetWidth() > currentScissor.left_ &&
  373. screenPos.y_ < currentScissor.bottom_ && screenPos.y_ + GetHeight() > currentScissor.top_;
  374. }
  375. const IntVector2& UIElement::GetScreenPosition() const
  376. {
  377. if (positionDirty_)
  378. {
  379. IntVector2 pos = position_;
  380. const UIElement* parent = parent_;
  381. if (parent)
  382. {
  383. const IntVector2& parentScreenPos = parent->GetScreenPosition();
  384. switch (horizontalAlignment_)
  385. {
  386. case HA_LEFT:
  387. pos.x_ += parentScreenPos.x_;
  388. break;
  389. case HA_CENTER:
  390. pos.x_ += parentScreenPos.x_ + parent_->size_.x_ / 2 - size_.x_ / 2;
  391. break;
  392. case HA_RIGHT:
  393. pos.x_ += parentScreenPos.x_ + parent_->size_.x_ - size_.x_;
  394. break;
  395. }
  396. switch (verticalAlignment_)
  397. {
  398. case VA_TOP:
  399. pos.y_ += parentScreenPos.y_;
  400. break;
  401. case VA_CENTER:
  402. pos.y_ += parentScreenPos.y_ + parent_->size_.y_ / 2 - size_.y_ / 2;
  403. break;
  404. case VA_BOTTOM:
  405. pos.y_ += parentScreenPos.y_ + parent_->size_.y_ - size_.y_;
  406. break;
  407. }
  408. pos += parent_->childOffset_;
  409. }
  410. screenPosition_ = pos;
  411. positionDirty_ = false;
  412. }
  413. return screenPosition_;
  414. }
  415. void UIElement::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  416. {
  417. hovering_ = true;
  418. }
  419. void UIElement::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
  420. {
  421. }
  422. void UIElement::OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement)
  423. {
  424. }
  425. void UIElement::OnDoubleClick(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
  426. {
  427. }
  428. void UIElement::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  429. {
  430. }
  431. void UIElement::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  432. {
  433. }
  434. void UIElement::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  435. {
  436. }
  437. bool UIElement::OnDragDropTest(UIElement* source)
  438. {
  439. return true;
  440. }
  441. bool UIElement::OnDragDropFinish(UIElement* source)
  442. {
  443. return true;
  444. }
  445. void UIElement::OnWheel(int delta, int buttons, int qualifiers)
  446. {
  447. }
  448. void UIElement::OnKey(int key, int buttons, int qualifiers)
  449. {
  450. }
  451. void UIElement::OnTextInput(const String& text, int buttons, int qualifiers)
  452. {
  453. }
  454. bool UIElement::LoadXML(Deserializer& source)
  455. {
  456. SharedPtr<XMLFile> xml(new XMLFile(context_));
  457. return xml->Load(source) && LoadXML(xml->GetRoot());
  458. }
  459. bool UIElement::SaveXML(Serializer& dest) const
  460. {
  461. SharedPtr<XMLFile> xml(new XMLFile(context_));
  462. XMLElement rootElem = xml->CreateRoot("element");
  463. return SaveXML(rootElem) && xml->Save(dest);
  464. }
  465. bool UIElement::FilterAttributes(XMLElement& dest) const
  466. {
  467. // Filter UI styling attributes
  468. XMLFile* styleFile = GetDefaultStyle();
  469. if (styleFile)
  470. {
  471. String style = dest.GetAttribute("style");
  472. if (!style.Empty() && style != "none")
  473. {
  474. if (styleXPathQuery_.SetVariable("typeName", style))
  475. {
  476. XMLElement styleElem = GetDefaultStyle()->GetRoot().SelectSinglePrepared(styleXPathQuery_);
  477. if (styleElem && !FilterUIStyleAttributes(dest, styleElem))
  478. return false;
  479. }
  480. }
  481. }
  482. // Filter implicit attributes
  483. if (!FilterImplicitAttributes(dest))
  484. {
  485. LOGERROR("Could not remove implicit attributes");
  486. return false;
  487. }
  488. return true;
  489. }
  490. void UIElement::SetName(const String& name)
  491. {
  492. name_ = name;
  493. using namespace NameChanged;
  494. VariantMap& eventData = GetEventDataMap();
  495. eventData[P_ELEMENT] = this;
  496. SendEvent(E_NAMECHANGED, eventData);
  497. }
  498. void UIElement::SetPosition(const IntVector2& position)
  499. {
  500. if (position != position_)
  501. {
  502. position_ = position;
  503. OnPositionSet();
  504. MarkDirty();
  505. using namespace Positioned;
  506. VariantMap& eventData = GetEventDataMap();
  507. eventData[P_ELEMENT] = this;
  508. eventData[P_X] = position_.x_;
  509. eventData[P_Y] = position_.y_;
  510. SendEvent(E_POSITIONED, eventData);
  511. }
  512. }
  513. void UIElement::SetPosition(int x, int y)
  514. {
  515. SetPosition(IntVector2(x, y));
  516. }
  517. void UIElement::SetSize(const IntVector2& size)
  518. {
  519. ++resizeNestingLevel_;
  520. IntVector2 validatedSize;
  521. validatedSize.x_ = Clamp(size.x_, minSize_.x_, maxSize_.x_);
  522. validatedSize.y_ = Clamp(size.y_, minSize_.y_, maxSize_.y_);
  523. if (validatedSize != size_)
  524. {
  525. size_ = validatedSize;
  526. if (resizeNestingLevel_ == 1)
  527. {
  528. // Check if parent element's layout needs to be updated first
  529. if (parent_)
  530. parent_->UpdateLayout();
  531. MarkDirty();
  532. OnResize();
  533. UpdateLayout();
  534. using namespace Resized;
  535. VariantMap& eventData = GetEventDataMap();
  536. eventData[P_ELEMENT] = this;
  537. eventData[P_WIDTH] = size_.x_;
  538. eventData[P_HEIGHT] = size_.y_;
  539. SendEvent(E_RESIZED, eventData);
  540. }
  541. }
  542. --resizeNestingLevel_;
  543. }
  544. void UIElement::SetSize(int width, int height)
  545. {
  546. SetSize(IntVector2(width, height));
  547. }
  548. void UIElement::SetWidth(int width)
  549. {
  550. SetSize(IntVector2(width, size_.y_));
  551. }
  552. void UIElement::SetHeight(int height)
  553. {
  554. SetSize(IntVector2(size_.x_, height));
  555. }
  556. void UIElement::SetMinSize(const IntVector2& minSize)
  557. {
  558. minSize_.x_ = Max(minSize.x_, 0);
  559. minSize_.y_ = Max(minSize.y_, 0);
  560. SetSize(size_);
  561. }
  562. void UIElement::SetMinSize(int width, int height)
  563. {
  564. SetMinSize(IntVector2(width, height));
  565. }
  566. void UIElement::SetMinWidth(int width)
  567. {
  568. SetMinSize(IntVector2(width, minSize_.y_));
  569. }
  570. void UIElement::SetMinHeight(int height)
  571. {
  572. SetMinSize(IntVector2(minSize_.x_, height));
  573. }
  574. void UIElement::SetMaxSize(const IntVector2& maxSize)
  575. {
  576. maxSize_.x_ = Max(maxSize.x_, 0);
  577. maxSize_.y_ = Max(maxSize.y_, 0);
  578. SetSize(size_);
  579. }
  580. void UIElement::SetMaxSize(int width, int height)
  581. {
  582. SetMaxSize(IntVector2(width, height));
  583. }
  584. void UIElement::SetMaxWidth(int width)
  585. {
  586. SetMaxSize(IntVector2(width, maxSize_.y_));
  587. }
  588. void UIElement::SetMaxHeight(int height)
  589. {
  590. SetMaxSize(IntVector2(maxSize_.x_, height));
  591. }
  592. void UIElement::SetFixedSize(const IntVector2& size)
  593. {
  594. minSize_ = maxSize_ = IntVector2(Max(size.x_, 0), Max(size.y_, 0));
  595. SetSize(size);
  596. }
  597. void UIElement::SetFixedSize(int width, int height)
  598. {
  599. SetFixedSize(IntVector2(width, height));
  600. }
  601. void UIElement::SetFixedWidth(int width)
  602. {
  603. minSize_.x_ = maxSize_.x_ = Max(width, 0);
  604. SetWidth(width);
  605. }
  606. void UIElement::SetFixedHeight(int height)
  607. {
  608. minSize_.y_ = maxSize_.y_ = Max(height, 0);
  609. SetHeight(height);
  610. }
  611. void UIElement::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  612. {
  613. SetHorizontalAlignment(hAlign);
  614. SetVerticalAlignment(vAlign);
  615. }
  616. void UIElement::SetHorizontalAlignment(HorizontalAlignment align)
  617. {
  618. if (align != HA_LEFT && parent_ && parent_->GetLayoutMode() == LM_HORIZONTAL)
  619. {
  620. LOGWARNING("Forcing left alignment because parent element has horizontal layout");
  621. align = HA_LEFT;
  622. }
  623. if (horizontalAlignment_ != align)
  624. {
  625. horizontalAlignment_ = align;
  626. MarkDirty();
  627. }
  628. }
  629. void UIElement::SetVerticalAlignment(VerticalAlignment align)
  630. {
  631. if (align != VA_TOP && parent_ && parent_->GetLayoutMode() == LM_VERTICAL)
  632. {
  633. LOGWARNING("Forcing top alignment because parent element has vertical layout");
  634. align = VA_TOP;
  635. }
  636. if (verticalAlignment_ != align)
  637. {
  638. verticalAlignment_ = align;
  639. MarkDirty();
  640. }
  641. }
  642. void UIElement::SetClipBorder(const IntRect& rect)
  643. {
  644. clipBorder_.left_ = Max(rect.left_, 0);
  645. clipBorder_.top_ = Max(rect.top_, 0);
  646. clipBorder_.right_ = Max(rect.right_, 0);
  647. clipBorder_.bottom_ = Max(rect.bottom_, 0);
  648. }
  649. void UIElement::SetColor(const Color& color)
  650. {
  651. for (unsigned i = 0; i < MAX_UIELEMENT_CORNERS; ++i)
  652. color_[i] = color;
  653. colorGradient_ = false;
  654. derivedColorDirty_ = true;
  655. }
  656. void UIElement::SetColor(Corner corner, const Color& color)
  657. {
  658. color_[corner] = color;
  659. colorGradient_ = false;
  660. derivedColorDirty_ = true;
  661. for (unsigned i = 0; i < MAX_UIELEMENT_CORNERS; ++i)
  662. {
  663. if (i != corner && color_[i] != color_[corner])
  664. colorGradient_ = true;
  665. }
  666. }
  667. void UIElement::SetPriority(int priority)
  668. {
  669. priority_ = priority;
  670. if (parent_)
  671. parent_->sortOrderDirty_ = true;
  672. }
  673. void UIElement::SetOpacity(float opacity)
  674. {
  675. opacity_ = Clamp(opacity, 0.0f, 1.0f);
  676. MarkDirty();
  677. }
  678. void UIElement::SetBringToFront(bool enable)
  679. {
  680. bringToFront_ = enable;
  681. }
  682. void UIElement::SetBringToBack(bool enable)
  683. {
  684. bringToBack_ = enable;
  685. }
  686. void UIElement::SetClipChildren(bool enable)
  687. {
  688. clipChildren_ = enable;
  689. }
  690. void UIElement::SetSortChildren(bool enable)
  691. {
  692. if (!sortChildren_ && enable)
  693. sortOrderDirty_ = true;
  694. sortChildren_ = enable;
  695. }
  696. void UIElement::SetUseDerivedOpacity(bool enable)
  697. {
  698. useDerivedOpacity_ = enable;
  699. }
  700. void UIElement::SetEnabled(bool enable)
  701. {
  702. enabled_ = enable;
  703. }
  704. void UIElement::SetEditable(bool enable)
  705. {
  706. editable_ = enable;
  707. OnSetEditable();
  708. }
  709. void UIElement::SetFocusMode(FocusMode mode)
  710. {
  711. focusMode_ = mode;
  712. }
  713. void UIElement::SetFocus(bool enable)
  714. {
  715. if (focusMode_ < FM_FOCUSABLE)
  716. enable = false;
  717. UI* ui = GetSubsystem<UI>();
  718. if (enable)
  719. {
  720. if (ui->GetFocusElement() != this)
  721. ui->SetFocusElement(this);
  722. }
  723. else
  724. {
  725. if (ui->GetFocusElement() == this)
  726. ui->SetFocusElement(0);
  727. }
  728. }
  729. void UIElement::SetSelected(bool enable)
  730. {
  731. selected_ = enable;
  732. }
  733. void UIElement::SetVisible(bool enable)
  734. {
  735. if (enable != visible_)
  736. {
  737. visible_ = enable;
  738. // Parent's layout may change as a result of visibility change
  739. if (parent_)
  740. parent_->UpdateLayout();
  741. using namespace VisibleChanged;
  742. VariantMap& eventData = GetEventDataMap();
  743. eventData[P_ELEMENT] = this;
  744. eventData[P_VISIBLE] = visible_;
  745. SendEvent(E_VISIBLECHANGED, eventData);
  746. }
  747. }
  748. void UIElement::SetDragDropMode(unsigned mode)
  749. {
  750. dragDropMode_ = mode;
  751. }
  752. bool UIElement::SetStyle(const String& styleName, XMLFile* file)
  753. {
  754. // If empty style was requested, replace with type name
  755. String actualStyleName = !styleName.Empty() ? styleName : GetTypeName();
  756. appliedStyle_ = actualStyleName;
  757. if (styleName == "none")
  758. return true;
  759. if (!file)
  760. {
  761. file = GetDefaultStyle();
  762. if (!file)
  763. return false;
  764. }
  765. else
  766. {
  767. // If a custom style file specified, remember it
  768. defaultStyle_ = file;
  769. }
  770. styleXPathQuery_.SetVariable("typeName", actualStyleName);
  771. XMLElement styleElem = file->GetRoot().SelectSinglePrepared(styleXPathQuery_);
  772. return styleElem && SetStyle(styleElem);
  773. }
  774. bool UIElement::SetStyle(const XMLElement& element)
  775. {
  776. appliedStyle_ = element.GetAttribute("type");
  777. // Consider style attribute values as instance-level attribute default values
  778. return LoadXML(element, true);
  779. }
  780. bool UIElement::SetStyleAuto(XMLFile* file)
  781. {
  782. return SetStyle(String::EMPTY, file);
  783. }
  784. void UIElement::SetDefaultStyle(XMLFile* style)
  785. {
  786. defaultStyle_ = style;
  787. }
  788. void UIElement::SetLayout(LayoutMode mode, int spacing, const IntRect& border)
  789. {
  790. layoutMode_ = mode;
  791. layoutSpacing_ = Max(spacing, 0);
  792. layoutBorder_ = IntRect(Max(border.left_, 0), Max(border.top_, 0), Max(border.right_, 0), Max(border.bottom_, 0));
  793. VerifyChildAlignment();
  794. UpdateLayout();
  795. }
  796. void UIElement::SetLayoutMode(LayoutMode mode)
  797. {
  798. layoutMode_ = mode;
  799. VerifyChildAlignment();
  800. UpdateLayout();
  801. }
  802. void UIElement::SetLayoutSpacing(int spacing)
  803. {
  804. layoutSpacing_ = Max(spacing, 0);
  805. UpdateLayout();
  806. }
  807. void UIElement::SetLayoutBorder(const IntRect& border)
  808. {
  809. layoutBorder_ = IntRect(Max(border.left_, 0), Max(border.top_, 0), Max(border.right_, 0), Max(border.bottom_, 0));
  810. UpdateLayout();
  811. }
  812. void UIElement::SetIndent(int indent)
  813. {
  814. indent_ = indent;
  815. if (parent_)
  816. parent_->UpdateLayout();
  817. UpdateLayout();
  818. OnIndentSet();
  819. }
  820. void UIElement::SetIndentSpacing(int indentSpacing)
  821. {
  822. indentSpacing_ = Max(indentSpacing, 0);
  823. if (parent_)
  824. parent_->UpdateLayout();
  825. UpdateLayout();
  826. OnIndentSet();
  827. }
  828. void UIElement::UpdateLayout()
  829. {
  830. if (layoutMode_ == LM_FREE || layoutNestingLevel_)
  831. return;
  832. // Prevent further updates while this update happens
  833. DisableLayoutUpdate();
  834. PODVector<int> positions;
  835. PODVector<int> sizes;
  836. PODVector<int> minSizes;
  837. PODVector<int> maxSizes;
  838. int baseIndentWidth = GetIndentWidth();
  839. if (layoutMode_ == LM_HORIZONTAL)
  840. {
  841. int minChildHeight = 0;
  842. for (unsigned i = 0; i < children_.Size(); ++i)
  843. {
  844. if (!children_[i]->IsVisible())
  845. continue;
  846. positions.Push(baseIndentWidth);
  847. unsigned indent = children_[i]->GetIndentWidth();
  848. sizes.Push(children_[i]->GetWidth() + indent);
  849. minSizes.Push(children_[i]->GetMinWidth() + indent);
  850. maxSizes.Push(children_[i]->GetMaxWidth() + indent);
  851. minChildHeight = Max(minChildHeight, children_[i]->GetMinHeight());
  852. }
  853. CalculateLayout(positions, sizes, minSizes, maxSizes, GetWidth(), layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_);
  854. int width = CalculateLayoutParentSize(sizes, layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_);
  855. int height = Max(GetHeight(), minChildHeight + layoutBorder_.top_ + layoutBorder_.bottom_);
  856. int minWidth = Min(CalculateLayoutParentSize(minSizes, layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_), maxSize_.x_);
  857. int minHeight = Min(minChildHeight + layoutBorder_.top_ + layoutBorder_.bottom_, maxSize_.y_);
  858. // Respect fixed size if already set
  859. if (minSize_.x_ != maxSize_.x_)
  860. minSize_.x_ = minWidth;
  861. if (minSize_.y_ != maxSize_.y_)
  862. minSize_.y_ = minHeight;
  863. SetSize(width, height);
  864. // Validate the size before resizing child elements, in case of min/max limits
  865. width = size_.x_;
  866. height = size_.y_;
  867. unsigned j = 0;
  868. for (unsigned i = 0; i < children_.Size(); ++i)
  869. {
  870. if (!children_[i]->IsVisible())
  871. continue;
  872. children_[i]->SetPosition(positions[j], GetLayoutChildPosition(children_[i]).y_);
  873. children_[i]->SetSize(sizes[j], height - layoutBorder_.top_ - layoutBorder_.bottom_);
  874. ++j;
  875. }
  876. }
  877. else if (layoutMode_ == LM_VERTICAL)
  878. {
  879. int minChildWidth = 0;
  880. for (unsigned i = 0; i < children_.Size(); ++i)
  881. {
  882. if (!children_[i]->IsVisible())
  883. continue;
  884. positions.Push(0);
  885. sizes.Push(children_[i]->GetHeight());
  886. minSizes.Push(children_[i]->GetMinHeight());
  887. maxSizes.Push(children_[i]->GetMaxHeight());
  888. minChildWidth = Max(minChildWidth, children_[i]->GetMinWidth() + children_[i]->GetIndentWidth());
  889. }
  890. CalculateLayout(positions, sizes, minSizes, maxSizes, GetHeight(), layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_);
  891. int height = CalculateLayoutParentSize(sizes, layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_);
  892. int width = Max(GetWidth(), minChildWidth + layoutBorder_.left_ + layoutBorder_.right_);
  893. int minHeight = Min(CalculateLayoutParentSize(minSizes, layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_), maxSize_.y_);
  894. int minWidth = Min(minChildWidth + layoutBorder_.left_ + layoutBorder_.right_, maxSize_.x_);
  895. if (minSize_.x_ != maxSize_.x_)
  896. minSize_.x_ = minWidth;
  897. if (minSize_.y_ != maxSize_.y_)
  898. minSize_.y_ = minHeight;
  899. SetSize(width, height);
  900. width = size_.x_;
  901. height = size_.y_;
  902. unsigned j = 0;
  903. for (unsigned i = 0; i < children_.Size(); ++i)
  904. {
  905. if (!children_[i]->IsVisible())
  906. continue;
  907. children_[i]->SetPosition(GetLayoutChildPosition(children_[i]).x_ + baseIndentWidth, positions[j]);
  908. children_[i]->SetSize(width - layoutBorder_.left_ - layoutBorder_.right_, sizes[j]);
  909. ++j;
  910. }
  911. }
  912. using namespace LayoutUpdated;
  913. VariantMap& eventData = GetEventDataMap();
  914. eventData[P_ELEMENT] = this;
  915. SendEvent(E_LAYOUTUPDATED, eventData);
  916. EnableLayoutUpdate();
  917. }
  918. void UIElement::DisableLayoutUpdate()
  919. {
  920. ++layoutNestingLevel_;
  921. }
  922. void UIElement::EnableLayoutUpdate()
  923. {
  924. --layoutNestingLevel_;
  925. }
  926. void UIElement::BringToFront()
  927. {
  928. // Follow the parent chain to the top level window. If it has BringToFront mode, bring it to front now
  929. UIElement* root = GetRoot();
  930. // If element is detached from hierarchy, this must be a no-op
  931. if (!root)
  932. return;
  933. UIElement* ptr = this;
  934. while (ptr && ptr->GetParent() != root)
  935. ptr = ptr->GetParent();
  936. if (!ptr || !ptr->GetBringToFront())
  937. return;
  938. // Get the highest priority used by all other top level elements, assign that to the new front element
  939. // and decrease others' priority where necessary. However, take into account only input-enabled
  940. // elements and those which have the BringToBack flag set
  941. HashSet<int> usedPriorities;
  942. int maxPriority = M_MIN_INT;
  943. const Vector<SharedPtr<UIElement> >& rootChildren = root->GetChildren();
  944. for (Vector<SharedPtr<UIElement> >::ConstIterator i = rootChildren.Begin(); i != rootChildren.End(); ++i)
  945. {
  946. UIElement* other = *i;
  947. if (other->IsEnabled() && other->bringToBack_ && other != ptr)
  948. {
  949. int priority = other->GetPriority();
  950. // M_MAX_INT is used by popups and tooltips. Disregard these to avoid an "arms race" with the priorities
  951. if (priority == M_MAX_INT)
  952. continue;
  953. usedPriorities.Insert(priority);
  954. maxPriority = Max(priority, maxPriority);
  955. }
  956. }
  957. if (maxPriority != M_MIN_INT && maxPriority >= ptr->GetPriority())
  958. {
  959. ptr->SetPriority(maxPriority);
  960. int minPriority = maxPriority;
  961. while (usedPriorities.Contains(minPriority))
  962. --minPriority;
  963. for (Vector<SharedPtr<UIElement> >::ConstIterator i = rootChildren.Begin(); i != rootChildren.End(); ++i)
  964. {
  965. UIElement* other = *i;
  966. int priority = other->GetPriority();
  967. if (other->IsEnabled() && other->bringToBack_ && other != ptr && priority >= minPriority && priority <= maxPriority)
  968. other->SetPriority(priority - 1);
  969. }
  970. }
  971. }
  972. UIElement* UIElement::CreateChild(StringHash type, const String& name, unsigned index)
  973. {
  974. // Check that creation succeeds and that the object in fact is a UI element
  975. SharedPtr<UIElement> newElement = DynamicCast<UIElement>(context_->CreateObject(type));
  976. if (!newElement)
  977. {
  978. LOGERROR("Could not create unknown UI element type " + type.ToString());
  979. return 0;
  980. }
  981. if (!name.Empty())
  982. newElement->SetName(name);
  983. InsertChild(index, newElement);
  984. return newElement;
  985. }
  986. void UIElement::AddChild(UIElement* element)
  987. {
  988. InsertChild(M_MAX_UNSIGNED, element);
  989. }
  990. void UIElement::InsertChild(unsigned index, UIElement* element)
  991. {
  992. // Check for illegal or redundant parent assignment
  993. if (!element || element == this || element->parent_ == this)
  994. return;
  995. // Check for possible cyclic parent assignment
  996. UIElement* parent = parent_;
  997. while (parent)
  998. {
  999. if (parent == element)
  1000. return;
  1001. parent = parent->parent_;
  1002. }
  1003. // Add first, then remove from old parent, to ensure the element does not get deleted
  1004. if (index >= children_.Size())
  1005. children_.Push(SharedPtr<UIElement>(element));
  1006. else
  1007. children_.Insert(children_.Begin() + index, SharedPtr<UIElement>(element));
  1008. XMLFile* previousStyleFile = element->GetDefaultStyle();
  1009. element->Remove();
  1010. if (sortChildren_)
  1011. sortOrderDirty_ = true;
  1012. element->parent_ = this;
  1013. element->MarkDirty();
  1014. // If child element did not already have a style file, but has specified a style name, apply it now
  1015. if (!previousStyleFile && !element->appliedStyle_.Empty() && GetDefaultStyle())
  1016. element->SetStyle(element->appliedStyle_);
  1017. VerifyChildAlignment();
  1018. UpdateLayout();
  1019. // Send change event
  1020. UIElement* root = GetRoot();
  1021. UIElement* sender = GetElementEventSender();
  1022. if (sender)
  1023. {
  1024. using namespace ElementAdded;
  1025. VariantMap& eventData = GetEventDataMap();
  1026. eventData[P_ROOT] = root;
  1027. eventData[P_PARENT] = this;
  1028. eventData[P_ELEMENT] = element;
  1029. sender->SendEvent(E_ELEMENTADDED, eventData);
  1030. }
  1031. }
  1032. void UIElement::RemoveChild(UIElement* element, unsigned index)
  1033. {
  1034. for (unsigned i = index; i < children_.Size(); ++i)
  1035. {
  1036. if (children_[i] == element)
  1037. {
  1038. // Send change event if not already being destroyed
  1039. UIElement* sender = Refs() > 0 ? GetElementEventSender() : 0;
  1040. if (sender)
  1041. {
  1042. using namespace ElementRemoved;
  1043. VariantMap& eventData = GetEventDataMap();
  1044. eventData[P_ROOT] = GetRoot();
  1045. eventData[P_PARENT] = this;
  1046. eventData[P_ELEMENT] = element;
  1047. sender->SendEvent(E_ELEMENTREMOVED, eventData);
  1048. }
  1049. element->Detach();
  1050. children_.Erase(i);
  1051. UpdateLayout();
  1052. return;
  1053. }
  1054. }
  1055. }
  1056. void UIElement::RemoveChildAtIndex(unsigned index)
  1057. {
  1058. if (index >= children_.Size())
  1059. return;
  1060. // Send change event if not already being destroyed
  1061. UIElement* sender = Refs() > 0 ? GetElementEventSender() : 0;
  1062. if (sender)
  1063. {
  1064. using namespace ElementRemoved;
  1065. VariantMap& eventData = GetEventDataMap();
  1066. eventData[P_ROOT] = GetRoot();
  1067. eventData[P_PARENT] = this;
  1068. eventData[P_ELEMENT] = children_[index];
  1069. sender->SendEvent(E_ELEMENTREMOVED, eventData);
  1070. }
  1071. children_[index]->Detach();
  1072. children_.Erase(index);
  1073. UpdateLayout();
  1074. }
  1075. void UIElement::RemoveAllChildren()
  1076. {
  1077. UIElement* root = GetRoot();
  1078. UIElement* sender = Refs() > 0 ? GetElementEventSender() : 0;
  1079. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i < children_.End(); )
  1080. {
  1081. // Send change event if not already being destroyed
  1082. if (sender)
  1083. {
  1084. using namespace ElementRemoved;
  1085. VariantMap& eventData = GetEventDataMap();
  1086. eventData[P_ROOT] = root;
  1087. eventData[P_PARENT] = this;
  1088. eventData[P_ELEMENT] = (*i).Get();
  1089. sender->SendEvent(E_ELEMENTREMOVED, eventData);
  1090. }
  1091. (*i++)->Detach();
  1092. }
  1093. children_.Clear();
  1094. UpdateLayout();
  1095. }
  1096. void UIElement::Remove()
  1097. {
  1098. if (parent_)
  1099. parent_->RemoveChild(this);
  1100. }
  1101. unsigned UIElement::FindChild(UIElement* element) const
  1102. {
  1103. Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Find(SharedPtr<UIElement>(element));
  1104. return i != children_.End() ? i - children_.Begin() : M_MAX_UNSIGNED;
  1105. }
  1106. void UIElement::SetParent(UIElement* parent, unsigned index)
  1107. {
  1108. if (parent)
  1109. parent->InsertChild(index, this);
  1110. }
  1111. void UIElement::SetVar(StringHash key, const Variant& value)
  1112. {
  1113. vars_[key] = value;
  1114. }
  1115. void UIElement::SetInternal(bool enable)
  1116. {
  1117. internal_ = enable;
  1118. }
  1119. void UIElement::SetTraversalMode(TraversalMode traversalMode)
  1120. {
  1121. traversalMode_ = traversalMode;
  1122. }
  1123. void UIElement::SetElementEventSender(bool flag)
  1124. {
  1125. elementEventSender_ = flag;
  1126. }
  1127. float UIElement::GetDerivedOpacity() const
  1128. {
  1129. if (!useDerivedOpacity_)
  1130. return opacity_;
  1131. if (opacityDirty_)
  1132. {
  1133. derivedOpacity_ = opacity_;
  1134. const UIElement* parent = parent_;
  1135. while (parent)
  1136. {
  1137. derivedOpacity_ *= parent->opacity_;
  1138. parent = parent->parent_;
  1139. }
  1140. opacityDirty_ = false;
  1141. }
  1142. return derivedOpacity_;
  1143. }
  1144. bool UIElement::HasFocus() const
  1145. {
  1146. return GetSubsystem<UI>()->GetFocusElement() == this;
  1147. }
  1148. const String& UIElement::GetAppliedStyle() const
  1149. {
  1150. return appliedStyle_ == GetTypeName() ? String::EMPTY : appliedStyle_;
  1151. }
  1152. XMLFile* UIElement::GetDefaultStyle(bool recursiveUp) const
  1153. {
  1154. if (recursiveUp)
  1155. {
  1156. const UIElement* element = this;
  1157. while (element)
  1158. {
  1159. if (element->defaultStyle_)
  1160. return element->defaultStyle_;
  1161. element = element->parent_;
  1162. }
  1163. return 0;
  1164. }
  1165. else
  1166. return defaultStyle_;
  1167. }
  1168. void UIElement::GetChildren(PODVector<UIElement*>& dest, bool recursive) const
  1169. {
  1170. dest.Clear();
  1171. if (!recursive)
  1172. {
  1173. dest.Reserve(children_.Size());
  1174. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1175. dest.Push(*i);
  1176. }
  1177. else
  1178. GetChildrenRecursive(dest);
  1179. }
  1180. unsigned UIElement::GetNumChildren(bool recursive) const
  1181. {
  1182. if (!recursive)
  1183. return children_.Size();
  1184. else
  1185. {
  1186. unsigned allChildren = children_.Size();
  1187. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1188. allChildren += (*i)->GetNumChildren(true);
  1189. return allChildren;
  1190. }
  1191. }
  1192. UIElement* UIElement::GetChild(unsigned index) const
  1193. {
  1194. return index < children_.Size() ? children_[index] : (UIElement*)0;
  1195. }
  1196. UIElement* UIElement::GetChild(const String& name, bool recursive) const
  1197. {
  1198. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1199. {
  1200. if ((*i)->name_ == name)
  1201. return *i;
  1202. if (recursive)
  1203. {
  1204. UIElement* element = (*i)->GetChild(name, true);
  1205. if (element)
  1206. return element;
  1207. }
  1208. }
  1209. return 0;
  1210. }
  1211. UIElement* UIElement::GetChild(const StringHash& key, const Variant& value, bool recursive) const
  1212. {
  1213. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1214. {
  1215. const Variant& varValue = (*i)->GetVar(key);
  1216. if (value != Variant::EMPTY ? varValue == value : varValue != Variant::EMPTY)
  1217. return *i;
  1218. if (recursive)
  1219. {
  1220. UIElement* element = (*i)->GetChild(key, value, true);
  1221. if (element)
  1222. return element;
  1223. }
  1224. }
  1225. return 0;
  1226. }
  1227. UIElement* UIElement::GetRoot() const
  1228. {
  1229. UIElement* root = parent_;
  1230. if (!root)
  1231. return 0;
  1232. while (root->GetParent())
  1233. root = root->GetParent();
  1234. return root;
  1235. }
  1236. const Color& UIElement::GetDerivedColor() const
  1237. {
  1238. if (derivedColorDirty_)
  1239. {
  1240. derivedColor_ = color_[C_TOPLEFT];
  1241. derivedColor_.a_ *= GetDerivedOpacity();
  1242. derivedColorDirty_ = false;
  1243. }
  1244. return derivedColor_;
  1245. }
  1246. const Variant& UIElement::GetVar(const StringHash& key) const
  1247. {
  1248. VariantMap::ConstIterator i = vars_.Find(key);
  1249. return i != vars_.End() ? i->second_ : Variant::EMPTY;
  1250. }
  1251. IntVector2 UIElement::ScreenToElement(const IntVector2& screenPosition)
  1252. {
  1253. return screenPosition - GetScreenPosition();
  1254. }
  1255. IntVector2 UIElement::ElementToScreen(const IntVector2& position)
  1256. {
  1257. return position + GetScreenPosition();
  1258. }
  1259. bool UIElement::IsInside(IntVector2 position, bool isScreen)
  1260. {
  1261. if (isScreen)
  1262. position = ScreenToElement(position);
  1263. return position.x_ >= 0 && position.y_ >= 0 && position.x_ < size_.x_ && position.y_ < size_.y_;
  1264. }
  1265. bool UIElement::IsInsideCombined(IntVector2 position, bool isScreen)
  1266. {
  1267. // If child elements are clipped, no need to expand the rect
  1268. if (clipChildren_)
  1269. return IsInside(position, isScreen);
  1270. if (!isScreen)
  1271. position = ElementToScreen(position);
  1272. IntRect combined = GetCombinedScreenRect();
  1273. return position.x_ >= combined.left_ && position.y_ >= combined.top_ && position.x_ < combined.right_ &&
  1274. position.y_ < combined.bottom_;
  1275. }
  1276. IntRect UIElement::GetCombinedScreenRect()
  1277. {
  1278. IntVector2 screenPosition(GetScreenPosition());
  1279. IntRect combined(screenPosition.x_, screenPosition.y_, screenPosition.x_ + size_.x_, screenPosition.y_ + size_.y_);
  1280. if (!clipChildren_)
  1281. {
  1282. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  1283. {
  1284. IntVector2 childPos = (*i)->GetScreenPosition();
  1285. const IntVector2& childSize = (*i)->GetSize();
  1286. if (childPos.x_ < combined.left_)
  1287. combined.left_ = childPos.x_;
  1288. if (childPos.y_ < combined.top_)
  1289. combined.top_ = childPos.y_;
  1290. if (childPos.x_ + childSize.x_ > combined.right_)
  1291. combined.right_ = childPos.x_ + childSize.x_;
  1292. if (childPos.y_ + childSize.y_ > combined.bottom_)
  1293. combined.bottom_ = childPos.y_ + childSize.y_;
  1294. }
  1295. }
  1296. return combined;
  1297. }
  1298. void UIElement::SortChildren()
  1299. {
  1300. if (sortChildren_ && sortOrderDirty_)
  1301. {
  1302. // Only sort when there is no layout
  1303. if (layoutMode_ == LM_FREE)
  1304. Sort(children_.Begin(), children_.End(), CompareUIElements);
  1305. sortOrderDirty_ = false;
  1306. }
  1307. }
  1308. void UIElement::SetChildOffset(const IntVector2& offset)
  1309. {
  1310. if (offset != childOffset_)
  1311. {
  1312. childOffset_ = offset;
  1313. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1314. (*i)->MarkDirty();
  1315. }
  1316. }
  1317. void UIElement::SetHovering(bool enable)
  1318. {
  1319. hovering_ = enable;
  1320. }
  1321. void UIElement::AdjustScissor(IntRect& currentScissor)
  1322. {
  1323. if (clipChildren_)
  1324. {
  1325. IntVector2 screenPos = GetScreenPosition();
  1326. currentScissor.left_ = Max(currentScissor.left_, screenPos.x_ + clipBorder_.left_);
  1327. currentScissor.top_ = Max(currentScissor.top_, screenPos.y_ + clipBorder_.top_);
  1328. currentScissor.right_ = Min(currentScissor.right_, screenPos.x_ + size_.x_ - clipBorder_.right_);
  1329. currentScissor.bottom_ = Min(currentScissor.bottom_, screenPos.y_ + size_.y_ - clipBorder_.bottom_);
  1330. if (currentScissor.right_ < currentScissor.left_)
  1331. currentScissor.right_ = currentScissor.left_;
  1332. if (currentScissor.bottom_ < currentScissor.top_)
  1333. currentScissor.bottom_ = currentScissor.top_;
  1334. }
  1335. }
  1336. void UIElement::GetBatchesWithOffset(IntVector2& offset, PODVector<UIBatch>& batches, PODVector<float>& vertexData, IntRect
  1337. currentScissor)
  1338. {
  1339. Vector2 floatOffset((float)offset.x_, (float)offset.y_);
  1340. unsigned initialSize = vertexData.Size();
  1341. GetBatches(batches, vertexData, currentScissor);
  1342. for (unsigned i = initialSize; i < vertexData.Size(); i += 6)
  1343. {
  1344. vertexData[i] += floatOffset.x_;
  1345. vertexData[i + 1] += floatOffset.y_;
  1346. }
  1347. AdjustScissor(currentScissor);
  1348. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1349. {
  1350. if ((*i)->IsVisible())
  1351. (*i)->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  1352. }
  1353. }
  1354. UIElement* UIElement::GetElementEventSender() const
  1355. {
  1356. UIElement* element = const_cast<UIElement*>(this);
  1357. if (elementEventSender_)
  1358. return element;
  1359. while (element->parent_)
  1360. {
  1361. element = element->parent_;
  1362. if (element->elementEventSender_)
  1363. return element;
  1364. }
  1365. // If no predefined element event sender in the parental chain, return ultimate root element
  1366. return element;
  1367. }
  1368. void UIElement::OnAttributeAnimationAdded()
  1369. {
  1370. if (attributeAnimationInfos_.Size() == 1)
  1371. SubscribeToEvent(E_POSTUPDATE, HANDLER(UIElement, HandlePostUpdate));
  1372. }
  1373. void UIElement::OnAttributeAnimationRemoved()
  1374. {
  1375. if (attributeAnimationInfos_.Empty())
  1376. UnsubscribeFromEvent(E_POSTUPDATE);
  1377. }
  1378. void UIElement::SetObjectAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed)
  1379. {
  1380. Vector<String> names = name.Split('/');
  1381. // Only attribute name
  1382. if (names.Size() == 1)
  1383. SetAttributeAnimation(name, attributeAnimation, wrapMode, speed);
  1384. else
  1385. {
  1386. // Name must in following format: "#0/#1/attribute"
  1387. UIElement* element = this;
  1388. for (unsigned i = 0; i < names.Size() - 1; ++i)
  1389. {
  1390. if (names[i].Front() != '#')
  1391. {
  1392. LOGERROR("Invalid name " + name);
  1393. return;
  1394. }
  1395. unsigned index = ToInt(names[i].Substring(1, names[i].Length() - 1));
  1396. element = element->GetChild(index);
  1397. if (!element)
  1398. {
  1399. LOGERROR("Could not find element by name " + name);
  1400. return;
  1401. }
  1402. }
  1403. element->SetAttributeAnimation(names.Back(), attributeAnimation, wrapMode, speed);
  1404. }
  1405. }
  1406. void UIElement::MarkDirty()
  1407. {
  1408. positionDirty_ = true;
  1409. opacityDirty_ = true;
  1410. derivedColorDirty_ = true;
  1411. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1412. (*i)->MarkDirty();
  1413. }
  1414. bool UIElement::RemoveChildXML(XMLElement& parent, const String& name) const
  1415. {
  1416. static XPathQuery matchXPathQuery("./attribute[@name=$attributeName]", "attributeName:String");
  1417. if (!matchXPathQuery.SetVariable("attributeName", name))
  1418. return false;
  1419. XMLElement removeElem = parent.SelectSinglePrepared(matchXPathQuery);
  1420. return !removeElem || parent.RemoveChild(removeElem);
  1421. }
  1422. bool UIElement::RemoveChildXML(XMLElement& parent, const String& name, const String& value) const
  1423. {
  1424. static XPathQuery matchXPathQuery("./attribute[@name=$attributeName and @value=$attributeValue]", "attributeName:String, attributeValue:String");
  1425. if (!matchXPathQuery.SetVariable("attributeName", name))
  1426. return false;
  1427. if (!matchXPathQuery.SetVariable("attributeValue", value))
  1428. return false;
  1429. XMLElement removeElem = parent.SelectSinglePrepared(matchXPathQuery);
  1430. return !removeElem || parent.RemoveChild(removeElem);
  1431. }
  1432. bool UIElement::FilterUIStyleAttributes(XMLElement& dest, const XMLElement& styleElem) const
  1433. {
  1434. // Remove style attribute only when its value is identical to the value stored in style file
  1435. String style = styleElem.GetAttribute("style");
  1436. if (!style.Empty())
  1437. {
  1438. if (style == dest.GetAttribute("style"))
  1439. {
  1440. if (!dest.RemoveAttribute("style"))
  1441. {
  1442. LOGWARNING("Could not remove style attribute");
  1443. return false;
  1444. }
  1445. }
  1446. }
  1447. // Perform the same action recursively for internal child elements stored in style file
  1448. XMLElement childDest = dest.GetChild("element");
  1449. XMLElement childElem = styleElem.GetChild("element");
  1450. while (childDest && childElem)
  1451. {
  1452. if (!childElem.GetBool("internal"))
  1453. {
  1454. LOGERROR("Invalid style file, style element can only contain internal child elements");
  1455. return false;
  1456. }
  1457. if (!FilterUIStyleAttributes(childDest, childElem))
  1458. return false;
  1459. childDest = childDest.GetNext("element");
  1460. childElem = childElem.GetNext("element");
  1461. }
  1462. // Remove style attribute when it is the same as its type, however, if it is an internal element then replace it to "none" instead
  1463. if (!dest.GetAttribute("style").Empty() && dest.GetAttribute("style") == dest.GetAttribute("type"))
  1464. {
  1465. if (internal_)
  1466. {
  1467. if (!dest.SetAttribute("style", "none"))
  1468. return false;
  1469. }
  1470. else
  1471. {
  1472. if (!dest.RemoveAttribute("style"))
  1473. return false;
  1474. }
  1475. }
  1476. return true;
  1477. }
  1478. bool UIElement::FilterImplicitAttributes(XMLElement& dest) const
  1479. {
  1480. // Remove positioning and sizing attributes when they are under the influence of layout mode
  1481. if (layoutMode_ != LM_FREE && !IsFixedWidth() && !IsFixedHeight())
  1482. {
  1483. if (!RemoveChildXML(dest, "Min Size"))
  1484. return false;
  1485. }
  1486. if (parent_ && parent_->layoutMode_ != LM_FREE)
  1487. {
  1488. if (!RemoveChildXML(dest, "Position"))
  1489. return false;
  1490. if (!RemoveChildXML(dest, "Size"))
  1491. return false;
  1492. }
  1493. return true;
  1494. }
  1495. void UIElement::GetChildrenRecursive(PODVector<UIElement*>& dest) const
  1496. {
  1497. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1498. {
  1499. UIElement* element = *i;
  1500. dest.Push(element);
  1501. if (!element->children_.Empty())
  1502. element->GetChildrenRecursive(dest);
  1503. }
  1504. }
  1505. int UIElement::CalculateLayoutParentSize(const PODVector<int>& sizes, int begin, int end, int spacing)
  1506. {
  1507. int width = begin + end;
  1508. if (sizes.Empty())
  1509. return width;
  1510. for (unsigned i = 0; i < sizes.Size(); ++i)
  1511. {
  1512. // If calculating maximum size, and the default is specified, do not overflow it
  1513. if (sizes[i] == M_MAX_INT)
  1514. return M_MAX_INT;
  1515. width += sizes[i] + spacing;
  1516. }
  1517. // The last spacing is not needed
  1518. return width - spacing;
  1519. }
  1520. void UIElement::CalculateLayout(PODVector<int>& positions, PODVector<int>& sizes, const PODVector<int>& minSizes,
  1521. const PODVector<int>& maxSizes, int targetSize, int begin, int end, int spacing)
  1522. {
  1523. int numChildren = sizes.Size();
  1524. if (!numChildren)
  1525. return;
  1526. int targetTotalSize = targetSize - begin - end - (numChildren - 1) * spacing;
  1527. if (targetTotalSize < 0)
  1528. targetTotalSize = 0;
  1529. int targetChildSize = targetTotalSize / numChildren;
  1530. int remainder = targetTotalSize % numChildren;
  1531. float add = (float)remainder / numChildren;
  1532. float acc = 0.0f;
  1533. // Initial pass
  1534. for (int i = 0; i < numChildren; ++i)
  1535. {
  1536. int targetSize = targetChildSize;
  1537. if (remainder)
  1538. {
  1539. acc += add;
  1540. if (acc >= 0.5f)
  1541. {
  1542. acc -= 1.0f;
  1543. ++targetSize;
  1544. --remainder;
  1545. }
  1546. }
  1547. sizes[i] = Clamp(targetSize, minSizes[i], maxSizes[i]);
  1548. }
  1549. // Error correction passes
  1550. for (;;)
  1551. {
  1552. int actualTotalSize = 0;
  1553. for (int i = 0; i < numChildren; ++i)
  1554. actualTotalSize += sizes[i];
  1555. int error = targetTotalSize - actualTotalSize;
  1556. // Break if no error
  1557. if (!error)
  1558. break;
  1559. // Check which of the children can be resized to correct the error. If none, must break
  1560. PODVector<unsigned> resizable;
  1561. for (int i = 0; i < numChildren; ++i)
  1562. {
  1563. if (error < 0 && sizes[i] > minSizes[i])
  1564. resizable.Push(i);
  1565. else if (error > 0 && sizes[i] < maxSizes[i])
  1566. resizable.Push(i);
  1567. }
  1568. if (resizable.Empty())
  1569. break;
  1570. int numResizable = resizable.Size();
  1571. int errorPerChild = error / numResizable;
  1572. remainder = (abs(error)) % numResizable;
  1573. add = (float)remainder / numResizable;
  1574. acc = 0.0f;
  1575. for (int i = 0; i < numResizable; ++i)
  1576. {
  1577. unsigned index = resizable[i];
  1578. int targetSize = sizes[index] + errorPerChild;
  1579. if (remainder)
  1580. {
  1581. acc += add;
  1582. if (acc >= 0.5f)
  1583. {
  1584. acc -= 1.0f;
  1585. targetSize = error < 0 ? targetSize - 1 : targetSize + 1;
  1586. --remainder;
  1587. }
  1588. }
  1589. sizes[index] = Clamp(targetSize, minSizes[index], maxSizes[index]);
  1590. }
  1591. }
  1592. // Calculate final positions and store the minimum child element size
  1593. layoutMinSize_ = M_MAX_INT;
  1594. int position = begin;
  1595. for (int i = 0; i < numChildren; ++i)
  1596. {
  1597. positions[i] = position;
  1598. position += sizes[i] + spacing;
  1599. if (sizes[i] < layoutMinSize_)
  1600. layoutMinSize_ = sizes[i];
  1601. }
  1602. }
  1603. IntVector2 UIElement::GetLayoutChildPosition(UIElement* child)
  1604. {
  1605. IntVector2 ret(IntVector2::ZERO);
  1606. HorizontalAlignment ha = child->GetHorizontalAlignment();
  1607. switch (ha)
  1608. {
  1609. case HA_LEFT:
  1610. ret.x_ = layoutBorder_.left_;
  1611. break;
  1612. case HA_RIGHT:
  1613. ret.x_ = -layoutBorder_.right_;
  1614. break;
  1615. default:
  1616. break;
  1617. }
  1618. VerticalAlignment va = child->GetVerticalAlignment();
  1619. switch (va)
  1620. {
  1621. case VA_TOP:
  1622. ret.y_ = layoutBorder_.top_;
  1623. break;
  1624. case VA_BOTTOM:
  1625. ret.y_ = -layoutBorder_.bottom_;
  1626. break;
  1627. default:
  1628. break;
  1629. }
  1630. return ret;
  1631. }
  1632. void UIElement::Detach()
  1633. {
  1634. parent_ = 0;
  1635. MarkDirty();
  1636. }
  1637. void UIElement::VerifyChildAlignment()
  1638. {
  1639. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1640. {
  1641. // Reapply child alignments. If they are illegal compared to layout, they will be set left/top as neded
  1642. (*i)->SetHorizontalAlignment((*i)->GetHorizontalAlignment());
  1643. (*i)->SetVerticalAlignment((*i)->GetVerticalAlignment());
  1644. }
  1645. }
  1646. void UIElement::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  1647. {
  1648. using namespace PostUpdate;
  1649. UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
  1650. }
  1651. }