UIElement.cpp 44 KB

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