UIElement.cpp 37 KB

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