UIElement.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Log.h"
  26. #include "ResourceCache.h"
  27. #include "Sort.h"
  28. #include "StringUtils.h"
  29. #include "UI.h"
  30. #include "UIElement.h"
  31. #include "UIEvents.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. static const char* horizontalAlignments[] =
  36. {
  37. "Left",
  38. "Center",
  39. "Right",
  40. 0
  41. };
  42. static const char* verticalAlignments[] =
  43. {
  44. "Top",
  45. "Center",
  46. "Bottom",
  47. 0
  48. };
  49. static const char* focusModes[] =
  50. {
  51. "NotFocusable",
  52. "ResetFocus",
  53. "Focusable",
  54. "FocusableDefocusable",
  55. 0
  56. };
  57. static const char* dragDropModes[] =
  58. {
  59. "Disabled",
  60. "Source",
  61. "Target",
  62. "SourceAndTarget",
  63. 0
  64. };
  65. static const char* layoutModes[] =
  66. {
  67. "Free",
  68. "Horizontal",
  69. "Vertical",
  70. 0
  71. };
  72. static bool CompareUIElements(const UIElement* lhs, const UIElement* rhs)
  73. {
  74. return lhs->GetPriority() < rhs->GetPriority();
  75. }
  76. template<> HorizontalAlignment Variant::Get<HorizontalAlignment>() const
  77. {
  78. return (HorizontalAlignment)GetInt();
  79. }
  80. template<> VerticalAlignment Variant::Get<VerticalAlignment>() const
  81. {
  82. return (VerticalAlignment)GetInt();
  83. }
  84. template<> FocusMode Variant::Get<FocusMode>() const
  85. {
  86. return (FocusMode)GetInt();
  87. }
  88. template<> LayoutMode Variant::Get<LayoutMode>() const
  89. {
  90. return (LayoutMode)GetInt();
  91. }
  92. OBJECTTYPESTATIC(UIElement);
  93. UIElement::UIElement(Context* context) :
  94. Serializable(context),
  95. parent_(0),
  96. clipBorder_(IntRect::ZERO),
  97. priority_(0),
  98. bringToFront_(false),
  99. bringToBack_(true),
  100. clipChildren_(false),
  101. sortChildren_(true),
  102. active_(false),
  103. selected_(false),
  104. visible_(true),
  105. hovering_(false),
  106. internal_(false),
  107. focusMode_(FM_NOTFOCUSABLE),
  108. dragDropMode_(DD_DISABLED),
  109. layoutMode_(LM_FREE),
  110. layoutSpacing_(0),
  111. layoutBorder_(IntRect::ZERO),
  112. resizeNestingLevel_(0),
  113. layoutNestingLevel_(0),
  114. layoutMinSize_(0),
  115. position_(IntVector2::ZERO),
  116. size_(IntVector2::ZERO),
  117. minSize_(IntVector2::ZERO),
  118. maxSize_(M_MAX_INT, M_MAX_INT),
  119. childOffset_(IntVector2::ZERO),
  120. horizontalAlignment_(HA_LEFT),
  121. verticalAlignment_(VA_TOP),
  122. opacity_(1.0f),
  123. positionDirty_(true),
  124. opacityDirty_(true),
  125. derivedColorDirty_(true),
  126. sortOrderDirty_(false),
  127. colorGradient_(false)
  128. {
  129. }
  130. UIElement::~UIElement()
  131. {
  132. // If child elements have outside references, detach them
  133. while (children_.Size())
  134. {
  135. const SharedPtr<UIElement>& element = children_.Back();
  136. if (element.Refs() > 1)
  137. {
  138. element->parent_ = 0;
  139. element->MarkDirty();
  140. }
  141. children_.Pop();
  142. }
  143. }
  144. void UIElement::RegisterObject(Context* context)
  145. {
  146. context->RegisterFactory<UIElement>();
  147. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_STRING, "Name", GetName, SetName, String, String(), AM_FILE);
  148. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Position", GetPosition, SetPosition, IntVector2, IntVector2::ZERO, AM_FILE);
  149. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Size", GetSize, SetSize, IntVector2, IntVector2::ZERO, AM_FILE);
  150. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Min Size", GetMinSize, SetMinSize, IntVector2, IntVector2::ZERO, AM_FILE);
  151. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTVECTOR2, "Max Size", GetMaxSize, SetMaxSize, IntVector2, IntVector2::ZERO, AM_FILE);
  152. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment, horizontalAlignments, HA_LEFT, AM_FILE);
  153. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments, VA_TOP, AM_FILE);
  154. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTRECT, "Clip Border", GetClipBorder, SetClipBorder, IntRect, IntRect::ZERO, AM_FILE);
  155. ACCESSOR_ATTRIBUTE(UIElement, VAR_INT, "Priority", GetPriority, SetPriority, int, 0, AM_FILE);
  156. ACCESSOR_ATTRIBUTE(UIElement, VAR_FLOAT, "Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_FILE);
  157. ATTRIBUTE(UIElement, VAR_COLOR, "Top Left Color", color_[0], Color::WHITE, AM_FILE);
  158. ATTRIBUTE(UIElement, VAR_COLOR, "Top Right Color", color_[1], Color::WHITE, AM_FILE);
  159. ATTRIBUTE(UIElement, VAR_COLOR, "Bottom Left Color", color_[2], Color::WHITE, AM_FILE);
  160. ATTRIBUTE(UIElement, VAR_COLOR, "Bottom Right Color", color_[3], Color::WHITE, AM_FILE);
  161. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Active", IsActive, SetActive, bool, false, AM_FILE);
  162. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Selected", IsSelected, SetSelected, bool, false, AM_FILE);
  163. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Is Visible", IsVisible, SetVisible, bool, true, AM_FILE);
  164. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Bring To Front", GetBringToFront, SetBringToFront, bool, false, AM_FILE);
  165. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Bring To Back", GetBringToBack, SetBringToBack, bool, true, AM_FILE);
  166. ACCESSOR_ATTRIBUTE(UIElement, VAR_BOOL, "Clip Children", GetClipChildren, SetClipChildren, bool, false, AM_FILE);
  167. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Focus Mode", GetFocusMode, SetFocusMode, FocusMode, focusModes, FM_NOTFOCUSABLE, AM_FILE);
  168. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Drag And Drop Mode", GetDragDropMode, SetDragDropMode, unsigned, dragDropModes, DD_DISABLED, AM_FILE);
  169. ENUM_ACCESSOR_ATTRIBUTE(UIElement, "Layout Mode", GetLayoutMode, SetLayoutMode, LayoutMode, layoutModes, LM_FREE, AM_FILE);
  170. ACCESSOR_ATTRIBUTE(UIElement, VAR_INT, "Layout Spacing", GetLayoutSpacing, SetLayoutSpacing, int, 0, AM_FILE);
  171. REF_ACCESSOR_ATTRIBUTE(UIElement, VAR_INTRECT, "Layout Border", GetLayoutBorder, SetLayoutBorder, IntRect, IntRect::ZERO, AM_FILE);
  172. ATTRIBUTE(UIElement, VAR_VARIANTMAP, "Variables", vars_, VariantMap(), AM_FILE);
  173. }
  174. void UIElement::ApplyAttributes()
  175. {
  176. colorGradient_ = false;
  177. derivedColorDirty_ = true;
  178. for (unsigned i = 1; i < MAX_UIELEMENT_CORNERS; ++i)
  179. {
  180. if (color_[i] != color_[0])
  181. colorGradient_ = true;
  182. }
  183. }
  184. bool UIElement::LoadXML(const XMLElement& source)
  185. {
  186. return LoadXML(source, 0);
  187. }
  188. bool UIElement::LoadXML(const XMLElement& source, XMLFile* styleFile)
  189. {
  190. // Apply the style first, but only for non-internal elements
  191. if (!internal_ && styleFile)
  192. {
  193. // Use style override if defined, otherwise type name
  194. String styleName = source.GetAttribute("style");
  195. if (styleName.Empty())
  196. styleName = GetTypeName();
  197. SetStyle(styleFile, styleName);
  198. }
  199. // Then load rest of the attributes from the source
  200. if (!Serializable::LoadXML(source))
  201. return false;
  202. unsigned nextInternalChild = 0;
  203. // Load child elements. Internal elements are not to be created as they already exist
  204. XMLElement childElem = source.GetChild("element");
  205. while (childElem)
  206. {
  207. bool internalElem = childElem.GetBool("internal");
  208. String typeName = childElem.GetAttribute("type");
  209. if (typeName.Empty())
  210. typeName = "UIElement";
  211. UIElement* child = 0;
  212. if (!internalElem)
  213. child = CreateChild(ShortStringHash(typeName));
  214. else
  215. {
  216. for (unsigned i = nextInternalChild; i < children_.Size(); ++i)
  217. {
  218. if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName)
  219. {
  220. child = children_[i];
  221. nextInternalChild = i + 1;
  222. break;
  223. }
  224. }
  225. if (!child)
  226. LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName());
  227. }
  228. if (child)
  229. {
  230. if (!child->LoadXML(childElem, styleFile))
  231. return false;
  232. }
  233. childElem = childElem.GetNext("element");
  234. }
  235. ApplyAttributes();
  236. return true;
  237. }
  238. bool UIElement::SaveXML(XMLElement& dest)
  239. {
  240. // Write type and internal flag
  241. if (!dest.SetString("type", GetTypeName()))
  242. return false;
  243. if (internal_)
  244. {
  245. if (!dest.SetBool("internal", internal_))
  246. return false;
  247. }
  248. // Write attributes
  249. if (!Serializable::SaveXML(dest))
  250. return false;
  251. // Write child elements
  252. for (unsigned i = 0; i < children_.Size(); ++i)
  253. {
  254. UIElement* element = children_[i];
  255. XMLElement childElem = dest.CreateChild("element");
  256. if (!element->SaveXML(childElem))
  257. return false;
  258. }
  259. return true;
  260. }
  261. void UIElement::Update(float timeStep)
  262. {
  263. }
  264. void UIElement::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  265. {
  266. // Reset hovering for next frame
  267. hovering_ = false;
  268. }
  269. void UIElement::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  270. {
  271. hovering_ = true;
  272. }
  273. void UIElement::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  274. {
  275. }
  276. void UIElement::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  277. {
  278. }
  279. void UIElement::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  280. {
  281. }
  282. void UIElement::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  283. {
  284. }
  285. bool UIElement::OnDragDropTest(UIElement* source)
  286. {
  287. return true;
  288. }
  289. bool UIElement::OnDragDropFinish(UIElement* source)
  290. {
  291. return true;
  292. }
  293. void UIElement::OnWheel(int delta, int buttons, int qualifiers)
  294. {
  295. }
  296. void UIElement::OnKey(int key, int buttons, int qualifiers)
  297. {
  298. }
  299. void UIElement::OnChar(unsigned c, int buttons, int qualifiers)
  300. {
  301. }
  302. void UIElement::OnResize()
  303. {
  304. }
  305. bool UIElement::LoadXML(Deserializer& source)
  306. {
  307. SharedPtr<XMLFile> xml(new XMLFile(context_));
  308. if (!xml->Load(source))
  309. return false;
  310. return LoadXML(xml->GetRoot());
  311. }
  312. bool UIElement::SaveXML(Serializer& dest)
  313. {
  314. SharedPtr<XMLFile> xml(new XMLFile(context_));
  315. XMLElement rootElem = xml->CreateRoot("element");
  316. if (!SaveXML(rootElem))
  317. return false;
  318. return xml->Save(dest);
  319. }
  320. void UIElement::SetName(const String& name)
  321. {
  322. name_ = name;
  323. }
  324. void UIElement::SetPosition(const IntVector2& position)
  325. {
  326. if (position != position_)
  327. {
  328. position_ = position;
  329. MarkDirty();
  330. }
  331. }
  332. void UIElement::SetPosition(int x, int y)
  333. {
  334. SetPosition(IntVector2(x, y));
  335. }
  336. void UIElement::SetSize(const IntVector2& size)
  337. {
  338. ++resizeNestingLevel_;
  339. IntVector2 validatedSize;
  340. validatedSize.x_ = Clamp(size.x_, minSize_.x_, maxSize_.x_);
  341. validatedSize.y_ = Clamp(size.y_, minSize_.y_, maxSize_.y_);
  342. if (validatedSize != size_)
  343. {
  344. size_ = validatedSize;
  345. if (resizeNestingLevel_ == 1)
  346. {
  347. // Check if parent element's layout needs to be updated first
  348. if (parent_)
  349. parent_->UpdateLayout();
  350. MarkDirty();
  351. OnResize();
  352. UpdateLayout();
  353. using namespace Resized;
  354. VariantMap eventData;
  355. eventData[P_ELEMENT] = (void*)this;
  356. eventData[P_WIDTH] = size_.x_;
  357. eventData[P_HEIGHT] = size_.y_;
  358. SendEvent(E_RESIZED, eventData);
  359. }
  360. }
  361. --resizeNestingLevel_;
  362. }
  363. void UIElement::SetSize(int width, int height)
  364. {
  365. SetSize(IntVector2(width, height));
  366. }
  367. void UIElement::SetWidth(int width)
  368. {
  369. SetSize(IntVector2(width, size_.y_));
  370. }
  371. void UIElement::SetHeight(int height)
  372. {
  373. SetSize(IntVector2(size_.x_, height));
  374. }
  375. void UIElement::SetMinSize(const IntVector2& minSize)
  376. {
  377. minSize_.x_ = Max(minSize.x_, 0);
  378. minSize_.y_ = Max(minSize.y_, 0);
  379. SetSize(size_);
  380. }
  381. void UIElement::SetMinSize(int width, int height)
  382. {
  383. SetMinSize(IntVector2(width, height));
  384. }
  385. void UIElement::SetMinWidth(int width)
  386. {
  387. SetMinSize(IntVector2(width, minSize_.y_));
  388. }
  389. void UIElement::SetMinHeight(int height)
  390. {
  391. SetMinSize(IntVector2(minSize_.x_, height));
  392. }
  393. void UIElement::SetMaxSize(const IntVector2& maxSize)
  394. {
  395. maxSize_.x_ = Max(maxSize.x_, 0);
  396. maxSize_.y_ = Max(maxSize.y_, 0);
  397. SetSize(size_);
  398. }
  399. void UIElement::SetMaxSize(int width, int height)
  400. {
  401. SetMaxSize(IntVector2(width, height));
  402. }
  403. void UIElement::SetMaxWidth(int width)
  404. {
  405. SetMaxSize(IntVector2(width, maxSize_.y_));
  406. }
  407. void UIElement::SetMaxHeight(int height)
  408. {
  409. SetMaxSize(IntVector2(maxSize_.x_, height));
  410. }
  411. void UIElement::SetFixedSize(const IntVector2& size)
  412. {
  413. minSize_ = maxSize_ = IntVector2(Max(size.x_, 0), Max(size.y_, 0));
  414. SetSize(size);
  415. }
  416. void UIElement::SetFixedSize(int width, int height)
  417. {
  418. SetFixedSize(IntVector2(width, height));
  419. }
  420. void UIElement::SetFixedWidth(int width)
  421. {
  422. minSize_.x_ = maxSize_.x_ = Max(width, 0);
  423. SetWidth(width);
  424. }
  425. void UIElement::SetFixedHeight(int height)
  426. {
  427. minSize_.y_ = maxSize_.y_ = Max(height, 0);
  428. SetHeight(height);
  429. }
  430. void UIElement::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  431. {
  432. horizontalAlignment_ = hAlign;
  433. verticalAlignment_ = vAlign;
  434. MarkDirty();
  435. }
  436. void UIElement::SetHorizontalAlignment(HorizontalAlignment align)
  437. {
  438. horizontalAlignment_ = align;
  439. MarkDirty();
  440. }
  441. void UIElement::SetVerticalAlignment(VerticalAlignment align)
  442. {
  443. verticalAlignment_ = align;
  444. MarkDirty();
  445. }
  446. void UIElement::SetClipBorder(const IntRect& rect)
  447. {
  448. clipBorder_.left_ = Max(rect.left_, 0);
  449. clipBorder_.top_ = Max(rect.top_, 0);
  450. clipBorder_.right_ = Max(rect.right_, 0);
  451. clipBorder_.bottom_ = Max(rect.bottom_, 0);
  452. }
  453. void UIElement::SetColor(const Color& color)
  454. {
  455. for (unsigned i = 0; i < MAX_UIELEMENT_CORNERS; ++i)
  456. color_[i] = color;
  457. colorGradient_ = false;
  458. derivedColorDirty_ = true;
  459. }
  460. void UIElement::SetColor(Corner corner, const Color& color)
  461. {
  462. color_[corner] = color;
  463. colorGradient_ = false;
  464. derivedColorDirty_ = true;
  465. for (unsigned i = 0; i < MAX_UIELEMENT_CORNERS; ++i)
  466. {
  467. if (i != corner && color_[i] != color_[corner])
  468. colorGradient_ = true;
  469. }
  470. }
  471. void UIElement::SetPriority(int priority)
  472. {
  473. priority_ = priority;
  474. if (parent_)
  475. parent_->sortOrderDirty_ = true;
  476. }
  477. void UIElement::SetOpacity(float opacity)
  478. {
  479. opacity_ = Clamp(opacity, 0.0f, 1.0f);
  480. MarkDirty();
  481. }
  482. void UIElement::SetBringToFront(bool enable)
  483. {
  484. bringToFront_ = enable;
  485. }
  486. void UIElement::SetBringToBack(bool enable)
  487. {
  488. bringToBack_ = enable;
  489. }
  490. void UIElement::SetClipChildren(bool enable)
  491. {
  492. clipChildren_ = enable;
  493. }
  494. void UIElement::SetSortChildren(bool enable)
  495. {
  496. if (!sortChildren_ && enable)
  497. sortOrderDirty_ = true;
  498. sortChildren_ = enable;
  499. }
  500. void UIElement::SetActive(bool enable)
  501. {
  502. active_ = enable;
  503. }
  504. void UIElement::SetFocusMode(FocusMode mode)
  505. {
  506. focusMode_ = mode;
  507. }
  508. void UIElement::SetFocus(bool enable)
  509. {
  510. if (focusMode_ < FM_FOCUSABLE)
  511. enable = false;
  512. UI* ui = GetSubsystem<UI>();
  513. if (!ui)
  514. return;
  515. if (enable)
  516. {
  517. if (ui->GetFocusElement() != this)
  518. ui->SetFocusElement(this);
  519. }
  520. else
  521. {
  522. if (ui->GetFocusElement() == this)
  523. ui->SetFocusElement(0);
  524. }
  525. }
  526. void UIElement::SetSelected(bool enable)
  527. {
  528. selected_ = enable;
  529. }
  530. void UIElement::SetVisible(bool enable)
  531. {
  532. if (enable != visible_)
  533. {
  534. visible_ = enable;
  535. // Parent's layout may change as a result of visibility change
  536. if (parent_)
  537. parent_->UpdateLayout();
  538. using namespace VisibleChanged;
  539. VariantMap eventData;
  540. eventData[P_ELEMENT] = (void*)this;
  541. eventData[P_VISIBLE] = visible_;
  542. SendEvent(E_VISIBLECHANGED, eventData);
  543. }
  544. }
  545. void UIElement::SetDragDropMode(unsigned mode)
  546. {
  547. dragDropMode_ = mode;
  548. }
  549. void UIElement::SetStyle(XMLFile* file, const String& typeName)
  550. {
  551. if (!file)
  552. return;
  553. XMLElement rootElem = file->GetRoot();
  554. XMLElement childElem = rootElem.GetChild("element");
  555. while (childElem)
  556. {
  557. if (typeName == childElem.GetAttribute("type"))
  558. {
  559. SetStyle(childElem);
  560. return;
  561. }
  562. childElem = childElem.GetNext("element");
  563. }
  564. }
  565. void UIElement::SetStyle(const XMLElement& element)
  566. {
  567. LoadXML(element);
  568. }
  569. void UIElement::SetStyleAuto(XMLFile* file)
  570. {
  571. SetStyle(file, GetTypeName());
  572. }
  573. void UIElement::SetLayout(LayoutMode mode, int spacing, const IntRect& border)
  574. {
  575. layoutMode_ = mode;
  576. layoutSpacing_ = Max(spacing, 0);
  577. layoutBorder_ = IntRect(Max(border.left_, 0), Max(border.top_, 0), Max(border.right_, 0), Max(border.bottom_, 0));
  578. UpdateLayout();
  579. }
  580. void UIElement::SetLayoutMode(LayoutMode mode)
  581. {
  582. layoutMode_ = mode;
  583. UpdateLayout();
  584. }
  585. void UIElement::SetLayoutSpacing(int spacing)
  586. {
  587. layoutSpacing_ = Max(spacing, 0);
  588. UpdateLayout();
  589. }
  590. void UIElement::SetLayoutBorder(const IntRect& border)
  591. {
  592. layoutBorder_ = IntRect(Max(border.left_, 0), Max(border.top_, 0), Max(border.right_, 0), Max(border.bottom_, 0));
  593. UpdateLayout();
  594. }
  595. void UIElement::UpdateLayout()
  596. {
  597. if (layoutMode_ == LM_FREE || layoutNestingLevel_)
  598. return;
  599. // Prevent further updates while this update happens
  600. DisableLayoutUpdate();
  601. PODVector<int> positions;
  602. PODVector<int> sizes;
  603. PODVector<int> minSizes;
  604. PODVector<int> maxSizes;
  605. if (layoutMode_ == LM_HORIZONTAL)
  606. {
  607. int minChildHeight = 0;
  608. for (unsigned i = 0; i < children_.Size(); ++i)
  609. {
  610. if (!children_[i]->IsVisible())
  611. continue;
  612. positions.Push(0);
  613. sizes.Push(children_[i]->GetWidth());
  614. minSizes.Push(children_[i]->GetMinWidth());
  615. maxSizes.Push(children_[i]->GetMaxWidth());
  616. minChildHeight = Max(minChildHeight, children_[i]->GetMinHeight());
  617. }
  618. CalculateLayout(positions, sizes, minSizes, maxSizes, GetWidth(), layoutBorder_.left_, layoutBorder_.right_,
  619. layoutSpacing_);
  620. int width = CalculateLayoutParentSize(sizes, layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_);
  621. int height = Max(GetHeight(), minChildHeight + layoutBorder_.top_ + layoutBorder_.bottom_);
  622. int minWidth = Min(CalculateLayoutParentSize(minSizes, layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_), maxSize_.x_);
  623. int minHeight = Min(minChildHeight + layoutBorder_.top_ + layoutBorder_.bottom_, maxSize_.y_);
  624. // Respect fixed size if already set
  625. if (minSize_.x_ != maxSize_.x_)
  626. minSize_.x_ = minWidth;
  627. if (minSize_.y_ != maxSize_.y_)
  628. minSize_.y_ = minHeight;
  629. SetSize(width, height);
  630. // Validate the size before resizing child elements, in case of min/max limits
  631. width = size_.x_;
  632. height = size_.y_;
  633. unsigned j = 0;
  634. for (unsigned i = 0; i < children_.Size(); ++i)
  635. {
  636. if (!children_[i]->IsVisible())
  637. continue;
  638. children_[i]->SetHorizontalAlignment(HA_LEFT);
  639. children_[i]->SetPosition(positions[j], GetLayoutChildPosition(children_[i]).y_);
  640. children_[i]->SetSize(sizes[j], height - layoutBorder_.top_ - layoutBorder_.bottom_);
  641. ++j;
  642. }
  643. }
  644. if (layoutMode_ == LM_VERTICAL)
  645. {
  646. int minChildWidth = 0;
  647. for (unsigned i = 0; i < children_.Size(); ++i)
  648. {
  649. if (!children_[i]->IsVisible())
  650. continue;
  651. positions.Push(0);
  652. sizes.Push(children_[i]->GetHeight());
  653. minSizes.Push(children_[i]->GetMinHeight());
  654. maxSizes.Push(children_[i]->GetMaxHeight());
  655. minChildWidth = Max(minChildWidth, children_[i]->GetMinWidth());
  656. }
  657. CalculateLayout(positions, sizes, minSizes, maxSizes, GetHeight(), layoutBorder_.top_, layoutBorder_.bottom_,
  658. layoutSpacing_);
  659. int height = CalculateLayoutParentSize(sizes, layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_);
  660. int width = Max(GetWidth(), minChildWidth + layoutBorder_.left_ + layoutBorder_.right_);
  661. int minHeight = Min(CalculateLayoutParentSize(minSizes, layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_), maxSize_.y_);
  662. int minWidth = Min(minChildWidth + layoutBorder_.left_ + layoutBorder_.right_, maxSize_.x_);
  663. if (minSize_.x_ != maxSize_.x_)
  664. minSize_.x_ = minWidth;
  665. if (minSize_.y_ != maxSize_.y_)
  666. minSize_.y_ = minHeight;
  667. SetSize(width, height);
  668. width = size_.x_;
  669. height = size_.y_;
  670. unsigned j = 0;
  671. for (unsigned i = 0; i < children_.Size(); ++i)
  672. {
  673. if (!children_[i]->IsVisible())
  674. continue;
  675. children_[i]->SetVerticalAlignment(VA_TOP);
  676. children_[i]->SetPosition(GetLayoutChildPosition(children_[i]).x_, positions[j]);
  677. children_[i]->SetSize(width - layoutBorder_.left_ - layoutBorder_.right_, sizes[j]);
  678. ++j;
  679. }
  680. }
  681. EnableLayoutUpdate();
  682. }
  683. void UIElement::DisableLayoutUpdate()
  684. {
  685. ++layoutNestingLevel_;
  686. }
  687. void UIElement::EnableLayoutUpdate()
  688. {
  689. --layoutNestingLevel_;
  690. }
  691. void UIElement::BringToFront()
  692. {
  693. // Follow the parent chain to the top level window. If it has BringToFront mode, bring it to front now
  694. UIElement* root = GetRoot();
  695. UIElement* ptr = this;
  696. while (ptr && ptr->GetParent() != root)
  697. ptr = ptr->GetParent();
  698. if (!ptr || !ptr->GetBringToFront())
  699. return;
  700. // Get the highest priority used by all other top level elements, assign that to the new front element
  701. // and decrease others' priority by one. However, take into account only active (enabled) elements
  702. // and those which have the BringToBack flag set
  703. int maxPriority = M_MIN_INT;
  704. const Vector<SharedPtr<UIElement> >& rootChildren = root->GetChildren();
  705. for (Vector<SharedPtr<UIElement> >::ConstIterator i = rootChildren.Begin(); i != rootChildren.End(); ++i)
  706. {
  707. UIElement* other = *i;
  708. if (other->IsActive() && other->bringToBack_ && other != ptr)
  709. {
  710. int priority = other->GetPriority();
  711. maxPriority = Max(priority, maxPriority);
  712. other->SetPriority(priority - 1);
  713. }
  714. }
  715. if (maxPriority != M_MIN_INT)
  716. ptr->SetPriority(maxPriority);
  717. }
  718. UIElement* UIElement::CreateChild(ShortStringHash type, const String& name)
  719. {
  720. // Check that creation succeeds and that the object in fact is a UI element
  721. SharedPtr<UIElement> newElement = DynamicCast<UIElement>(context_->CreateObject(type));
  722. if (!newElement)
  723. {
  724. LOGERROR("Could not create unknown UI element type " + type.ToString());
  725. return 0;
  726. }
  727. newElement->SetName(name);
  728. AddChild(newElement);
  729. return newElement;
  730. }
  731. void UIElement::AddChild(UIElement* element)
  732. {
  733. InsertChild(children_.Size(), element);
  734. }
  735. void UIElement::InsertChild(unsigned index, UIElement* element)
  736. {
  737. // Check for illegal or redundant parent assignment
  738. if (!element || element == this || element->parent_ == this)
  739. return;
  740. // Check for possible cyclic parent assignment
  741. UIElement* parent = parent_;
  742. while (parent)
  743. {
  744. if (parent == element)
  745. return;
  746. parent = parent->parent_;
  747. }
  748. // Add first, then remove from old parent, to ensure the element does not get deleted
  749. if (index >= children_.Size())
  750. children_.Push(SharedPtr<UIElement>(element));
  751. else
  752. children_.Insert(children_.Begin() + index, SharedPtr<UIElement>(element));
  753. if (element->parent_)
  754. element->parent_->RemoveChild(element);
  755. if (sortChildren_)
  756. sortOrderDirty_ = true;
  757. element->parent_ = this;
  758. element->MarkDirty();
  759. UpdateLayout();
  760. }
  761. void UIElement::RemoveChild(UIElement* element)
  762. {
  763. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  764. {
  765. if (*i == element)
  766. {
  767. element->parent_ = 0;
  768. element->MarkDirty();
  769. children_.Erase(i);
  770. UpdateLayout();
  771. return;
  772. }
  773. }
  774. }
  775. void UIElement::RemoveAllChildren()
  776. {
  777. while (children_.Size())
  778. {
  779. const SharedPtr<UIElement>& element = children_.Back();
  780. element->parent_ = 0;
  781. element->MarkDirty();
  782. children_.Pop();
  783. }
  784. }
  785. void UIElement::Remove()
  786. {
  787. if (parent_)
  788. parent_->RemoveChild(this);
  789. }
  790. void UIElement::SetParent(UIElement* parent)
  791. {
  792. if (parent)
  793. parent->AddChild(this);
  794. }
  795. void UIElement::SetVar(ShortStringHash key, const Variant& value)
  796. {
  797. vars_[key] = value;
  798. }
  799. void UIElement::SetInternal(bool enable)
  800. {
  801. internal_ = enable;
  802. }
  803. IntVector2 UIElement::GetScreenPosition()
  804. {
  805. if (positionDirty_)
  806. {
  807. IntVector2 pos = position_;
  808. const UIElement* parent = parent_;
  809. const UIElement* current = this;
  810. while (parent)
  811. {
  812. switch (current->horizontalAlignment_)
  813. {
  814. case HA_LEFT:
  815. pos.x_ += parent->position_.x_;
  816. break;
  817. case HA_CENTER:
  818. pos.x_ += parent->position_.x_ + parent->size_.x_ / 2 - current->size_.x_ / 2;
  819. break;
  820. case HA_RIGHT:
  821. pos.x_ += parent->position_.x_ + parent->size_.x_ - current->size_.x_;
  822. break;
  823. }
  824. switch (current->verticalAlignment_)
  825. {
  826. case VA_TOP:
  827. pos.y_ += parent->position_.y_;
  828. break;
  829. case VA_CENTER:
  830. pos.y_ += parent->position_.y_ + parent->size_.y_ / 2 - current->size_.y_ / 2;
  831. break;
  832. case VA_BOTTOM:
  833. pos.y_ += parent->position_.y_ + parent->size_.y_ - current->size_.y_;
  834. break;
  835. }
  836. pos += parent->childOffset_;
  837. current = parent;
  838. parent = parent->parent_;
  839. }
  840. screenPosition_ = pos;
  841. positionDirty_ = false;
  842. }
  843. return screenPosition_;
  844. }
  845. float UIElement::GetDerivedOpacity()
  846. {
  847. if (opacityDirty_)
  848. {
  849. float opacity = opacity_;
  850. const UIElement* parent = parent_;
  851. while (parent)
  852. {
  853. opacity *= parent->opacity_;
  854. parent = parent->parent_;
  855. }
  856. derivedOpacity_ = opacity;
  857. opacityDirty_ = false;
  858. }
  859. return derivedOpacity_;
  860. }
  861. bool UIElement::HasFocus() const
  862. {
  863. UI* ui = GetSubsystem<UI>();
  864. if (!ui)
  865. return false;
  866. else
  867. return ui->GetFocusElement() == this;
  868. }
  869. void UIElement::GetChildren(PODVector<UIElement*>& dest, bool recursive) const
  870. {
  871. dest.Clear();
  872. if (!recursive)
  873. {
  874. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  875. dest.Push(*i);
  876. }
  877. else
  878. GetChildrenRecursive(dest);
  879. }
  880. unsigned UIElement::GetNumChildren(bool recursive) const
  881. {
  882. if (!recursive)
  883. return children_.Size();
  884. else
  885. {
  886. unsigned allChildren = children_.Size();
  887. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  888. allChildren += (*i)->GetNumChildren(true);
  889. return allChildren;
  890. }
  891. }
  892. UIElement* UIElement::GetChild(unsigned index) const
  893. {
  894. return index < children_.Size() ? children_[index] : (UIElement*)0;
  895. }
  896. UIElement* UIElement::GetChild(const String& name, bool recursive) const
  897. {
  898. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  899. {
  900. if ((*i)->name_ == name)
  901. return *i;
  902. if (recursive)
  903. {
  904. UIElement* element = (*i)->GetChild(name, true);
  905. if (element)
  906. return element;
  907. }
  908. }
  909. return 0;
  910. }
  911. UIElement* UIElement::GetRoot() const
  912. {
  913. UIElement* root = parent_;
  914. if (!root)
  915. return 0;
  916. while (root->GetParent())
  917. root = root->GetParent();
  918. return root;
  919. }
  920. unsigned UIElement::GetUIntColor()
  921. {
  922. if (derivedColorDirty_)
  923. {
  924. Color color = color_[C_TOPLEFT];
  925. color.a_ *= GetDerivedOpacity();
  926. uintColor_ = color.ToUInt();
  927. derivedColorDirty_ = false;
  928. }
  929. return uintColor_;
  930. }
  931. const Variant& UIElement::GetVar(ShortStringHash key) const
  932. {
  933. VariantMap::ConstIterator i = vars_.Find(key);
  934. if (i != vars_.End())
  935. return i->second_;
  936. else
  937. return Variant::EMPTY;
  938. }
  939. IntVector2 UIElement::ScreenToElement(const IntVector2& screenPosition)
  940. {
  941. return screenPosition - GetScreenPosition();
  942. }
  943. IntVector2 UIElement::ElementToScreen(const IntVector2& position)
  944. {
  945. return position + GetScreenPosition();
  946. }
  947. bool UIElement::IsInside(IntVector2 position, bool isScreen)
  948. {
  949. if (isScreen)
  950. position = ScreenToElement(position);
  951. return position.x_ >= 0 && position.y_ >= 0 && position.x_ < size_.x_ && position.y_ < size_.y_;
  952. }
  953. bool UIElement::IsInsideCombined(IntVector2 position, bool isScreen)
  954. {
  955. // If child elements are clipped, no need to expand the rect
  956. if (clipChildren_)
  957. return IsInside(position, isScreen);
  958. if (!isScreen)
  959. position = ElementToScreen(position);
  960. IntRect combined = GetCombinedScreenRect();
  961. return position.x_ >= combined.left_ && position.y_ >= combined.top_ && position.x_ < combined.right_ &&
  962. position.y_ < combined.bottom_;
  963. }
  964. IntRect UIElement::GetCombinedScreenRect()
  965. {
  966. IntVector2 screenPosition(GetScreenPosition());
  967. IntRect combined(screenPosition.x_, screenPosition.y_, screenPosition.x_ + size_.x_, screenPosition.y_ + size_.y_);
  968. if (!clipChildren_)
  969. {
  970. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  971. {
  972. IntVector2 childPos = (*i)->GetScreenPosition();
  973. const IntVector2& childSize = (*i)->GetSize();
  974. if (childPos.x_ < combined.left_)
  975. combined.left_ = childPos.x_;
  976. if (childPos.y_ < combined.top_)
  977. combined.top_ = childPos.y_;
  978. if (childPos.x_ + childSize.x_ > combined.right_)
  979. combined.right_ = childPos.x_ + childSize.x_;
  980. if (childPos.y_ + childSize.y_ > combined.bottom_)
  981. combined.bottom_ = childPos.y_ + childSize.y_;
  982. }
  983. }
  984. return combined;
  985. }
  986. void UIElement::SortChildren()
  987. {
  988. if (sortChildren_ && sortOrderDirty_)
  989. {
  990. Sort(children_.Begin(), children_.End(), CompareUIElements);
  991. sortOrderDirty_ = false;
  992. }
  993. }
  994. void UIElement::SetChildOffset(const IntVector2& offset)
  995. {
  996. if (offset != childOffset_)
  997. {
  998. childOffset_ = offset;
  999. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1000. (*i)->MarkDirty();
  1001. }
  1002. }
  1003. void UIElement::SetHovering(bool enable)
  1004. {
  1005. hovering_ = enable;
  1006. }
  1007. void UIElement::SetTempVisible(bool enable)
  1008. {
  1009. visible_ = enable;
  1010. }
  1011. void UIElement::AdjustScissor(IntRect& currentScissor)
  1012. {
  1013. if (clipChildren_)
  1014. {
  1015. IntVector2 screenPos = GetScreenPosition();
  1016. currentScissor.left_ = Max(currentScissor.left_, screenPos.x_ + clipBorder_.left_);
  1017. currentScissor.top_ = Max(currentScissor.top_, screenPos.y_ + clipBorder_.top_);
  1018. currentScissor.right_ = Min(currentScissor.right_, screenPos.x_ + size_.x_ - clipBorder_.right_);
  1019. currentScissor.bottom_ = Min(currentScissor.bottom_, screenPos.y_ + size_.y_ - clipBorder_.bottom_);
  1020. if (currentScissor.right_ < currentScissor.left_)
  1021. currentScissor.right_ = currentScissor.left_;
  1022. if (currentScissor.bottom_ < currentScissor.top_)
  1023. currentScissor.bottom_ = currentScissor.top_;
  1024. }
  1025. }
  1026. void UIElement::GetBatchesWithOffset(IntVector2& offset, PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, IntRect
  1027. currentScissor)
  1028. {
  1029. unsigned initialSize = quads.Size();
  1030. GetBatches(batches, quads, currentScissor);
  1031. for (unsigned i = initialSize; i < quads.Size(); ++i)
  1032. {
  1033. quads[i].left_ += offset.x_;
  1034. quads[i].top_ += offset.y_;
  1035. quads[i].right_ += offset.x_;
  1036. quads[i].bottom_ += offset.y_;
  1037. }
  1038. AdjustScissor(currentScissor);
  1039. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1040. {
  1041. if ((*i)->IsVisible())
  1042. (*i)->GetBatchesWithOffset(offset, batches, quads, currentScissor);
  1043. }
  1044. }
  1045. void UIElement::MarkDirty()
  1046. {
  1047. positionDirty_ = true;
  1048. opacityDirty_ = true;
  1049. derivedColorDirty_ = true;
  1050. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1051. (*i)->MarkDirty();
  1052. }
  1053. void UIElement::GetChildrenRecursive(PODVector<UIElement*>& dest) const
  1054. {
  1055. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1056. {
  1057. UIElement* element = *i;
  1058. dest.Push(element);
  1059. if (!element->children_.Empty())
  1060. element->GetChildrenRecursive(dest);
  1061. }
  1062. }
  1063. int UIElement::CalculateLayoutParentSize(const PODVector<int>& sizes, int begin, int end, int spacing)
  1064. {
  1065. int width = begin + end;
  1066. for (unsigned i = 0; i < sizes.Size(); ++i)
  1067. {
  1068. // If calculating maximum size, and the default is specified, do not overflow it
  1069. if (sizes[i] == M_MAX_INT)
  1070. return M_MAX_INT;
  1071. width += sizes[i];
  1072. if (i < sizes.Size() - 1)
  1073. width += spacing;
  1074. }
  1075. return width;
  1076. }
  1077. void UIElement::CalculateLayout(PODVector<int>& positions, PODVector<int>& sizes, const PODVector<int>& minSizes,
  1078. const PODVector<int>& maxSizes, int targetSize, int begin, int end, int spacing)
  1079. {
  1080. int numChildren = sizes.Size();
  1081. if (!numChildren)
  1082. return;
  1083. int targetTotalSize = targetSize - begin - end - (numChildren - 1) * spacing;
  1084. if (targetTotalSize < 0)
  1085. targetTotalSize = 0;
  1086. int targetChildSize = targetTotalSize / numChildren;
  1087. int remainder = targetTotalSize % numChildren;
  1088. float add = (float)remainder / numChildren;
  1089. float acc = 0.0f;
  1090. // Initial pass
  1091. for (int i = 0; i < numChildren; ++i)
  1092. {
  1093. int targetSize = targetChildSize;
  1094. if (remainder)
  1095. {
  1096. acc += add;
  1097. if (acc >= 0.5f)
  1098. {
  1099. acc -= 1.0f;
  1100. ++targetSize;
  1101. --remainder;
  1102. }
  1103. }
  1104. sizes[i] = Clamp(targetSize, minSizes[i], maxSizes[i]);
  1105. }
  1106. // Error correction passes
  1107. for (;;)
  1108. {
  1109. int actualTotalSize = 0;
  1110. for (int i = 0; i < numChildren; ++i)
  1111. actualTotalSize += sizes[i];
  1112. int error = targetTotalSize - actualTotalSize;
  1113. // Break if no error
  1114. if (!error)
  1115. break;
  1116. // Check which of the children can be resized to correct the error. If none, must break
  1117. PODVector<unsigned> resizable;
  1118. for (int i = 0; i < numChildren; ++i)
  1119. {
  1120. if (error < 0 && sizes[i] > minSizes[i])
  1121. resizable.Push(i);
  1122. else if (error > 0 && sizes[i] < maxSizes[i])
  1123. resizable.Push(i);
  1124. }
  1125. if (resizable.Empty())
  1126. break;
  1127. int numResizable = resizable.Size();
  1128. int errorPerChild = error / numResizable;
  1129. remainder = (abs(error)) % numResizable;
  1130. add = (float)remainder / numResizable;
  1131. acc = 0.0f;
  1132. for (int i = 0; i < numResizable; ++i)
  1133. {
  1134. unsigned index = resizable[i];
  1135. int targetSize = sizes[index] + errorPerChild;
  1136. if (remainder)
  1137. {
  1138. acc += add;
  1139. if (acc >= 0.5f)
  1140. {
  1141. acc -= 1.0f;
  1142. targetSize = error < 0 ? targetSize - 1 : targetSize + 1;
  1143. --remainder;
  1144. }
  1145. }
  1146. sizes[index] = Clamp(targetSize, minSizes[index], maxSizes[index]);
  1147. }
  1148. }
  1149. // Calculate final positions and store the minimum child element size
  1150. layoutMinSize_ = M_MAX_INT;
  1151. int position = begin;
  1152. for (int i = 0; i < numChildren; ++i)
  1153. {
  1154. positions[i] = position;
  1155. position += sizes[i];
  1156. position += spacing;
  1157. if (sizes[i] < layoutMinSize_)
  1158. layoutMinSize_ = sizes[i];
  1159. }
  1160. }
  1161. IntVector2 UIElement::GetLayoutChildPosition(UIElement* child)
  1162. {
  1163. IntVector2 ret(IntVector2::ZERO);
  1164. HorizontalAlignment ha = child->GetHorizontalAlignment();
  1165. switch (ha)
  1166. {
  1167. case HA_LEFT:
  1168. ret.x_ = layoutBorder_.left_;
  1169. break;
  1170. case HA_RIGHT:
  1171. ret.x_ = -layoutBorder_.right_;
  1172. break;
  1173. }
  1174. VerticalAlignment va = child->GetVerticalAlignment();
  1175. switch (va)
  1176. {
  1177. case VA_TOP:
  1178. ret.y_ = layoutBorder_.top_;
  1179. break;
  1180. case VA_BOTTOM:
  1181. ret.y_ = -layoutBorder_.bottom_;
  1182. break;
  1183. }
  1184. return ret;
  1185. }
  1186. }