UIElement.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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. enabled_(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 Enabled", IsEnabled, SetEnabled, 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::SetEnabled(bool enable)
  562. {
  563. enabled_ = 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. if (parent_)
  664. parent_->UpdateLayout();
  665. }
  666. void UIElement::SetIndentSpacing(int indentSpacing)
  667. {
  668. indentSpacing_ = Max(indentSpacing, 0);
  669. if (parent_)
  670. parent_->UpdateLayout();
  671. }
  672. void UIElement::UpdateLayout()
  673. {
  674. if (layoutMode_ == LM_FREE || layoutNestingLevel_)
  675. return;
  676. // Prevent further updates while this update happens
  677. DisableLayoutUpdate();
  678. PODVector<int> positions;
  679. PODVector<int> sizes;
  680. PODVector<int> minSizes;
  681. PODVector<int> maxSizes;
  682. int baseIndent = GetIndentWidth();
  683. if (layoutMode_ == LM_HORIZONTAL)
  684. {
  685. int minChildHeight = 0;
  686. for (unsigned i = 0; i < children_.Size(); ++i)
  687. {
  688. if (!children_[i]->IsVisible())
  689. continue;
  690. positions.Push(baseIndent);
  691. unsigned indent = children_[i]->GetIndentWidth();
  692. sizes.Push(children_[i]->GetWidth() + indent);
  693. minSizes.Push(children_[i]->GetMinWidth() + indent);
  694. maxSizes.Push(children_[i]->GetMaxWidth() + indent);
  695. minChildHeight = Max(minChildHeight, children_[i]->GetMinHeight());
  696. }
  697. CalculateLayout(positions, sizes, minSizes, maxSizes, GetWidth(), layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_);
  698. int width = CalculateLayoutParentSize(sizes, layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_);
  699. int height = Max(GetHeight(), minChildHeight + layoutBorder_.top_ + layoutBorder_.bottom_);
  700. int minWidth = Min(CalculateLayoutParentSize(minSizes, layoutBorder_.left_, layoutBorder_.right_, layoutSpacing_), maxSize_.x_);
  701. int minHeight = Min(minChildHeight + layoutBorder_.top_ + layoutBorder_.bottom_, maxSize_.y_);
  702. // Respect fixed size if already set
  703. if (minSize_.x_ != maxSize_.x_)
  704. minSize_.x_ = minWidth;
  705. if (minSize_.y_ != maxSize_.y_)
  706. minSize_.y_ = minHeight;
  707. SetSize(width, height);
  708. // Validate the size before resizing child elements, in case of min/max limits
  709. width = size_.x_;
  710. height = size_.y_;
  711. unsigned j = 0;
  712. for (unsigned i = 0; i < children_.Size(); ++i)
  713. {
  714. if (!children_[i]->IsVisible())
  715. continue;
  716. HorizontalAlignment horizontalAlignment = children_[i]->horizontalAlignment_;
  717. children_[i]->horizontalAlignment_ = HA_LEFT;
  718. children_[i]->SetPosition(positions[j], GetLayoutChildPosition(children_[i]).y_);
  719. children_[i]->horizontalAlignment_ = horizontalAlignment;
  720. children_[i]->SetSize(sizes[j], height - layoutBorder_.top_ - layoutBorder_.bottom_);
  721. ++j;
  722. }
  723. }
  724. else if (layoutMode_ == LM_VERTICAL)
  725. {
  726. int minChildWidth = 0;
  727. for (unsigned i = 0; i < children_.Size(); ++i)
  728. {
  729. if (!children_[i]->IsVisible())
  730. continue;
  731. positions.Push(baseIndent);
  732. sizes.Push(children_[i]->GetHeight());
  733. minSizes.Push(children_[i]->GetMinHeight());
  734. maxSizes.Push(children_[i]->GetMaxHeight());
  735. minChildWidth = Max(minChildWidth, children_[i]->GetMinWidth() + children_[i]->GetIndentWidth());
  736. }
  737. CalculateLayout(positions, sizes, minSizes, maxSizes, GetHeight(), layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_);
  738. int height = CalculateLayoutParentSize(sizes, layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_);
  739. int width = Max(GetWidth(), minChildWidth + layoutBorder_.left_ + layoutBorder_.right_);
  740. int minHeight = Min(CalculateLayoutParentSize(minSizes, layoutBorder_.top_, layoutBorder_.bottom_, layoutSpacing_), maxSize_.y_);
  741. int minWidth = Min(minChildWidth + layoutBorder_.left_ + layoutBorder_.right_, maxSize_.x_);
  742. if (minSize_.x_ != maxSize_.x_)
  743. minSize_.x_ = minWidth;
  744. if (minSize_.y_ != maxSize_.y_)
  745. minSize_.y_ = minHeight;
  746. SetSize(width, height);
  747. width = size_.x_;
  748. height = size_.y_;
  749. unsigned j = 0;
  750. for (unsigned i = 0; i < children_.Size(); ++i)
  751. {
  752. if (!children_[i]->IsVisible())
  753. continue;
  754. VerticalAlignment verticalAlignment = children_[i]->verticalAlignment_;
  755. children_[i]->verticalAlignment_ = VA_TOP;
  756. children_[i]->SetPosition(GetLayoutChildPosition(children_[i]).x_, positions[j]);
  757. children_[i]->verticalAlignment_ = verticalAlignment;
  758. children_[i]->SetSize(width - layoutBorder_.left_ - layoutBorder_.right_, sizes[j]);
  759. ++j;
  760. }
  761. }
  762. using namespace LayoutUpdated;
  763. VariantMap eventData;
  764. eventData[P_ELEMENT] = (void*)this;
  765. SendEvent(E_LAYOUTUPDATED, eventData);
  766. EnableLayoutUpdate();
  767. }
  768. void UIElement::DisableLayoutUpdate()
  769. {
  770. ++layoutNestingLevel_;
  771. }
  772. void UIElement::EnableLayoutUpdate()
  773. {
  774. --layoutNestingLevel_;
  775. }
  776. void UIElement::BringToFront()
  777. {
  778. // Follow the parent chain to the top level window. If it has BringToFront mode, bring it to front now
  779. UIElement* root = GetRoot();
  780. // If element is detached from hierarchy, this must be a no-op
  781. if (!root)
  782. return;
  783. UIElement* ptr = this;
  784. while (ptr && ptr->GetParent() != root)
  785. ptr = ptr->GetParent();
  786. if (!ptr || !ptr->GetBringToFront())
  787. return;
  788. // Get the highest priority used by all other top level elements, assign that to the new front element
  789. // and decrease others' priority where necessary. However, take into account only input-enabled
  790. // elements and those which have the BringToBack flag set
  791. HashSet<int> usedPriorities;
  792. int maxPriority = M_MIN_INT;
  793. const Vector<SharedPtr<UIElement> >& rootChildren = root->GetChildren();
  794. for (Vector<SharedPtr<UIElement> >::ConstIterator i = rootChildren.Begin(); i != rootChildren.End(); ++i)
  795. {
  796. UIElement* other = *i;
  797. if (other->IsEnabled() && other->bringToBack_ && other != ptr)
  798. {
  799. int priority = other->GetPriority();
  800. usedPriorities.Insert(priority);
  801. maxPriority = Max(priority, maxPriority);
  802. }
  803. }
  804. if (maxPriority != M_MIN_INT && maxPriority >= ptr->GetPriority())
  805. {
  806. ptr->SetPriority(maxPriority);
  807. int minPriority = maxPriority;
  808. while (usedPriorities.Contains(minPriority))
  809. --minPriority;
  810. for (Vector<SharedPtr<UIElement> >::ConstIterator i = rootChildren.Begin(); i != rootChildren.End(); ++i)
  811. {
  812. UIElement* other = *i;
  813. int priority = other->GetPriority();
  814. if (other->IsEnabled() && other->bringToBack_ && other != ptr && priority >= minPriority && priority <= maxPriority)
  815. other->SetPriority(priority - 1);
  816. }
  817. }
  818. }
  819. UIElement* UIElement::CreateChild(ShortStringHash type, const String& name)
  820. {
  821. // Check that creation succeeds and that the object in fact is a UI element
  822. SharedPtr<UIElement> newElement = DynamicCast<UIElement>(context_->CreateObject(type));
  823. if (!newElement)
  824. {
  825. LOGERROR("Could not create unknown UI element type " + type.ToString());
  826. return 0;
  827. }
  828. newElement->SetName(name);
  829. AddChild(newElement);
  830. return newElement;
  831. }
  832. void UIElement::AddChild(UIElement* element)
  833. {
  834. InsertChild(children_.Size(), element);
  835. }
  836. void UIElement::InsertChild(unsigned index, UIElement* element)
  837. {
  838. // Check for illegal or redundant parent assignment
  839. if (!element || element == this || element->parent_ == this)
  840. return;
  841. // Check for possible cyclic parent assignment
  842. UIElement* parent = parent_;
  843. while (parent)
  844. {
  845. if (parent == element)
  846. return;
  847. parent = parent->parent_;
  848. }
  849. // Add first, then remove from old parent, to ensure the element does not get deleted
  850. if (index >= children_.Size())
  851. children_.Push(SharedPtr<UIElement>(element));
  852. else
  853. children_.Insert(children_.Begin() + index, SharedPtr<UIElement>(element));
  854. element->Remove();
  855. if (sortChildren_)
  856. sortOrderDirty_ = true;
  857. element->parent_ = this;
  858. element->MarkDirty();
  859. UpdateLayout();
  860. // Send change event
  861. UIElement* root = GetRoot();
  862. if (root)
  863. {
  864. using namespace ElementAdded;
  865. VariantMap eventData;
  866. eventData[P_ROOT] = (void*)root;
  867. eventData[P_PARENT] = (void*)this;
  868. eventData[P_ELEMENT] = (void*)element;
  869. root->SendEvent(E_ELEMENTADDED, eventData);
  870. }
  871. }
  872. void UIElement::RemoveChild(UIElement* element, unsigned index)
  873. {
  874. UIElement* root = GetRoot();
  875. for (unsigned i = index; i < children_.Size(); ++i)
  876. {
  877. if (children_[i] == element)
  878. {
  879. // Send change event if not already being destroyed
  880. if (Refs() > 0 && root)
  881. {
  882. using namespace ElementRemoved;
  883. VariantMap eventData;
  884. eventData[P_ROOT] = (void*)root;
  885. eventData[P_PARENT] = (void*)this;
  886. eventData[P_ELEMENT] = (void*)element;
  887. root->SendEvent(E_ELEMENTREMOVED, eventData);
  888. }
  889. element->Detach();
  890. children_.Erase(i);
  891. UpdateLayout();
  892. return;
  893. }
  894. }
  895. }
  896. void UIElement::RemoveChildAtIndex(unsigned index)
  897. {
  898. if (index >= children_.Size())
  899. return;
  900. children_[index]->Detach();
  901. children_.Erase(index);
  902. UpdateLayout();
  903. }
  904. void UIElement::RemoveAllChildren()
  905. {
  906. UIElement* root = GetRoot();
  907. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i < children_.End(); )
  908. {
  909. // Send change event if not already being destroyed
  910. if (Refs() > 0 && root)
  911. {
  912. using namespace ElementRemoved;
  913. VariantMap eventData;
  914. eventData[P_ROOT] = (void*)root;
  915. eventData[P_PARENT] = (void*)this;
  916. eventData[P_ELEMENT] = (void*)(*i).Get();
  917. root->SendEvent(E_ELEMENTREMOVED, eventData);
  918. }
  919. (*i++)->Detach();
  920. }
  921. children_.Clear();
  922. }
  923. void UIElement::Remove()
  924. {
  925. if (parent_)
  926. parent_->RemoveChild(this);
  927. }
  928. void UIElement::SetParent(UIElement* parent)
  929. {
  930. if (parent)
  931. parent->AddChild(this);
  932. }
  933. void UIElement::SetVar(ShortStringHash key, const Variant& value)
  934. {
  935. vars_[key] = value;
  936. }
  937. void UIElement::SetInternal(bool enable)
  938. {
  939. internal_ = enable;
  940. }
  941. float UIElement::GetDerivedOpacity() const
  942. {
  943. if (!useDerivedOpacity_)
  944. return opacity_;
  945. if (opacityDirty_)
  946. {
  947. derivedOpacity_ = opacity_;
  948. const UIElement* parent = parent_;
  949. while (parent)
  950. {
  951. derivedOpacity_ *= parent->opacity_;
  952. parent = parent->parent_;
  953. }
  954. opacityDirty_ = false;
  955. }
  956. return derivedOpacity_;
  957. }
  958. bool UIElement::HasFocus() const
  959. {
  960. UI* ui = GetSubsystem<UI>();
  961. if (!ui)
  962. return false;
  963. else
  964. return ui->GetFocusElement() == this;
  965. }
  966. XMLFile* UIElement::GetDefaultStyle(bool recursiveUp) const
  967. {
  968. if (recursiveUp)
  969. {
  970. const UIElement* element = this;
  971. while (element)
  972. {
  973. if (element->defaultStyle_)
  974. return element->defaultStyle_;
  975. element = element->parent_;
  976. }
  977. return 0;
  978. }
  979. else
  980. return defaultStyle_;
  981. }
  982. void UIElement::GetChildren(PODVector<UIElement*>& dest, bool recursive) const
  983. {
  984. dest.Clear();
  985. if (!recursive)
  986. {
  987. dest.Reserve(children_.Size());
  988. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  989. dest.Push(*i);
  990. }
  991. else
  992. GetChildrenRecursive(dest);
  993. }
  994. unsigned UIElement::GetNumChildren(bool recursive) const
  995. {
  996. if (!recursive)
  997. return children_.Size();
  998. else
  999. {
  1000. unsigned allChildren = children_.Size();
  1001. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1002. allChildren += (*i)->GetNumChildren(true);
  1003. return allChildren;
  1004. }
  1005. }
  1006. UIElement* UIElement::GetChild(unsigned index) const
  1007. {
  1008. return index < children_.Size() ? children_[index] : (UIElement*)0;
  1009. }
  1010. UIElement* UIElement::GetChild(const String& name, bool recursive) const
  1011. {
  1012. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1013. {
  1014. if ((*i)->name_ == name)
  1015. return *i;
  1016. if (recursive)
  1017. {
  1018. UIElement* element = (*i)->GetChild(name, true);
  1019. if (element)
  1020. return element;
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. UIElement* UIElement::GetRoot() const
  1026. {
  1027. UIElement* root = parent_;
  1028. if (!root)
  1029. return 0;
  1030. while (root->GetParent())
  1031. root = root->GetParent();
  1032. return root;
  1033. }
  1034. const Color& UIElement::GetDerivedColor() const
  1035. {
  1036. if (derivedColorDirty_)
  1037. {
  1038. derivedColor_ = color_[C_TOPLEFT];
  1039. derivedColor_.a_ *= GetDerivedOpacity();
  1040. derivedColorDirty_ = false;
  1041. }
  1042. return derivedColor_;
  1043. }
  1044. const Variant& UIElement::GetVar(ShortStringHash key) const
  1045. {
  1046. VariantMap::ConstIterator i = vars_.Find(key);
  1047. if (i != vars_.End())
  1048. return i->second_;
  1049. else
  1050. return Variant::EMPTY;
  1051. }
  1052. IntVector2 UIElement::ScreenToElement(const IntVector2& screenPosition)
  1053. {
  1054. return screenPosition - GetScreenPosition();
  1055. }
  1056. IntVector2 UIElement::ElementToScreen(const IntVector2& position)
  1057. {
  1058. return position + GetScreenPosition();
  1059. }
  1060. bool UIElement::IsInside(IntVector2 position, bool isScreen)
  1061. {
  1062. if (isScreen)
  1063. position = ScreenToElement(position);
  1064. return position.x_ >= 0 && position.y_ >= 0 && position.x_ < size_.x_ && position.y_ < size_.y_;
  1065. }
  1066. bool UIElement::IsInsideCombined(IntVector2 position, bool isScreen)
  1067. {
  1068. // If child elements are clipped, no need to expand the rect
  1069. if (clipChildren_)
  1070. return IsInside(position, isScreen);
  1071. if (!isScreen)
  1072. position = ElementToScreen(position);
  1073. IntRect combined = GetCombinedScreenRect();
  1074. return position.x_ >= combined.left_ && position.y_ >= combined.top_ && position.x_ < combined.right_ &&
  1075. position.y_ < combined.bottom_;
  1076. }
  1077. IntRect UIElement::GetCombinedScreenRect()
  1078. {
  1079. IntVector2 screenPosition(GetScreenPosition());
  1080. IntRect combined(screenPosition.x_, screenPosition.y_, screenPosition.x_ + size_.x_, screenPosition.y_ + size_.y_);
  1081. if (!clipChildren_)
  1082. {
  1083. for (Vector<SharedPtr<UIElement> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  1084. {
  1085. IntVector2 childPos = (*i)->GetScreenPosition();
  1086. const IntVector2& childSize = (*i)->GetSize();
  1087. if (childPos.x_ < combined.left_)
  1088. combined.left_ = childPos.x_;
  1089. if (childPos.y_ < combined.top_)
  1090. combined.top_ = childPos.y_;
  1091. if (childPos.x_ + childSize.x_ > combined.right_)
  1092. combined.right_ = childPos.x_ + childSize.x_;
  1093. if (childPos.y_ + childSize.y_ > combined.bottom_)
  1094. combined.bottom_ = childPos.y_ + childSize.y_;
  1095. }
  1096. }
  1097. return combined;
  1098. }
  1099. void UIElement::SortChildren()
  1100. {
  1101. if (sortChildren_ && sortOrderDirty_)
  1102. {
  1103. Sort(children_.Begin(), children_.End(), CompareUIElements);
  1104. sortOrderDirty_ = false;
  1105. }
  1106. }
  1107. void UIElement::SetChildOffset(const IntVector2& offset)
  1108. {
  1109. if (offset != childOffset_)
  1110. {
  1111. childOffset_ = offset;
  1112. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1113. (*i)->MarkDirty();
  1114. }
  1115. }
  1116. void UIElement::SetHovering(bool enable)
  1117. {
  1118. hovering_ = enable;
  1119. }
  1120. void UIElement::SetTempVisible(bool enable)
  1121. {
  1122. visible_ = enable;
  1123. }
  1124. void UIElement::AdjustScissor(IntRect& currentScissor)
  1125. {
  1126. if (clipChildren_)
  1127. {
  1128. IntVector2 screenPos = GetScreenPosition();
  1129. currentScissor.left_ = Max(currentScissor.left_, screenPos.x_ + clipBorder_.left_);
  1130. currentScissor.top_ = Max(currentScissor.top_, screenPos.y_ + clipBorder_.top_);
  1131. currentScissor.right_ = Min(currentScissor.right_, screenPos.x_ + size_.x_ - clipBorder_.right_);
  1132. currentScissor.bottom_ = Min(currentScissor.bottom_, screenPos.y_ + size_.y_ - clipBorder_.bottom_);
  1133. if (currentScissor.right_ < currentScissor.left_)
  1134. currentScissor.right_ = currentScissor.left_;
  1135. if (currentScissor.bottom_ < currentScissor.top_)
  1136. currentScissor.bottom_ = currentScissor.top_;
  1137. }
  1138. }
  1139. void UIElement::GetBatchesWithOffset(IntVector2& offset, PODVector<UIBatch>& batches, PODVector<float>& vertexData, IntRect
  1140. currentScissor)
  1141. {
  1142. Vector2 floatOffset((float)offset.x_, (float)offset.y_);
  1143. unsigned initialSize = vertexData.Size();
  1144. GetBatches(batches, vertexData, currentScissor);
  1145. for (unsigned i = initialSize; i < vertexData.Size(); i += 6)
  1146. {
  1147. vertexData[i] += floatOffset.x_;
  1148. vertexData[i + 1] += floatOffset.y_;
  1149. }
  1150. AdjustScissor(currentScissor);
  1151. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1152. {
  1153. if ((*i)->IsVisible())
  1154. (*i)->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  1155. }
  1156. }
  1157. void UIElement::MarkDirty()
  1158. {
  1159. positionDirty_ = true;
  1160. opacityDirty_ = true;
  1161. derivedColorDirty_ = true;
  1162. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1163. (*i)->MarkDirty();
  1164. }
  1165. void UIElement::GetChildrenRecursive(PODVector<UIElement*>& dest) const
  1166. {
  1167. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1168. {
  1169. UIElement* element = *i;
  1170. dest.Push(element);
  1171. if (!element->children_.Empty())
  1172. element->GetChildrenRecursive(dest);
  1173. }
  1174. }
  1175. int UIElement::CalculateLayoutParentSize(const PODVector<int>& sizes, int begin, int end, int spacing)
  1176. {
  1177. int width = begin + end;
  1178. if (sizes.Empty())
  1179. return width;
  1180. for (unsigned i = 0; i < sizes.Size(); ++i)
  1181. {
  1182. // If calculating maximum size, and the default is specified, do not overflow it
  1183. if (sizes[i] == M_MAX_INT)
  1184. return M_MAX_INT;
  1185. width += sizes[i] + spacing;
  1186. }
  1187. // The last spacing is not needed
  1188. return width - spacing;
  1189. }
  1190. void UIElement::CalculateLayout(PODVector<int>& positions, PODVector<int>& sizes, const PODVector<int>& minSizes,
  1191. const PODVector<int>& maxSizes, int targetSize, int begin, int end, int spacing)
  1192. {
  1193. int numChildren = sizes.Size();
  1194. if (!numChildren)
  1195. return;
  1196. int targetTotalSize = targetSize - begin - end - (numChildren - 1) * spacing;
  1197. if (targetTotalSize < 0)
  1198. targetTotalSize = 0;
  1199. int targetChildSize = targetTotalSize / numChildren;
  1200. int remainder = targetTotalSize % numChildren;
  1201. float add = (float)remainder / numChildren;
  1202. float acc = 0.0f;
  1203. // Initial pass
  1204. for (int i = 0; i < numChildren; ++i)
  1205. {
  1206. int targetSize = targetChildSize;
  1207. if (remainder)
  1208. {
  1209. acc += add;
  1210. if (acc >= 0.5f)
  1211. {
  1212. acc -= 1.0f;
  1213. ++targetSize;
  1214. --remainder;
  1215. }
  1216. }
  1217. sizes[i] = Clamp(targetSize, minSizes[i], maxSizes[i]);
  1218. }
  1219. // Error correction passes
  1220. for (;;)
  1221. {
  1222. int actualTotalSize = 0;
  1223. for (int i = 0; i < numChildren; ++i)
  1224. actualTotalSize += sizes[i];
  1225. int error = targetTotalSize - actualTotalSize;
  1226. // Break if no error
  1227. if (!error)
  1228. break;
  1229. // Check which of the children can be resized to correct the error. If none, must break
  1230. PODVector<unsigned> resizable;
  1231. for (int i = 0; i < numChildren; ++i)
  1232. {
  1233. if (error < 0 && sizes[i] > minSizes[i])
  1234. resizable.Push(i);
  1235. else if (error > 0 && sizes[i] < maxSizes[i])
  1236. resizable.Push(i);
  1237. }
  1238. if (resizable.Empty())
  1239. break;
  1240. int numResizable = resizable.Size();
  1241. int errorPerChild = error / numResizable;
  1242. remainder = (abs(error)) % numResizable;
  1243. add = (float)remainder / numResizable;
  1244. acc = 0.0f;
  1245. for (int i = 0; i < numResizable; ++i)
  1246. {
  1247. unsigned index = resizable[i];
  1248. int targetSize = sizes[index] + errorPerChild;
  1249. if (remainder)
  1250. {
  1251. acc += add;
  1252. if (acc >= 0.5f)
  1253. {
  1254. acc -= 1.0f;
  1255. targetSize = error < 0 ? targetSize - 1 : targetSize + 1;
  1256. --remainder;
  1257. }
  1258. }
  1259. sizes[index] = Clamp(targetSize, minSizes[index], maxSizes[index]);
  1260. }
  1261. }
  1262. // Calculate final positions and store the minimum child element size
  1263. layoutMinSize_ = M_MAX_INT;
  1264. int position = begin;
  1265. for (int i = 0; i < numChildren; ++i)
  1266. {
  1267. positions[i] = position;
  1268. position += sizes[i] + spacing;
  1269. if (sizes[i] < layoutMinSize_)
  1270. layoutMinSize_ = sizes[i];
  1271. }
  1272. }
  1273. IntVector2 UIElement::GetLayoutChildPosition(UIElement* child)
  1274. {
  1275. IntVector2 ret(IntVector2::ZERO);
  1276. HorizontalAlignment ha = child->GetHorizontalAlignment();
  1277. switch (ha)
  1278. {
  1279. case HA_LEFT:
  1280. ret.x_ = layoutBorder_.left_;
  1281. break;
  1282. case HA_RIGHT:
  1283. ret.x_ = -layoutBorder_.right_;
  1284. break;
  1285. default:
  1286. break;
  1287. }
  1288. VerticalAlignment va = child->GetVerticalAlignment();
  1289. switch (va)
  1290. {
  1291. case VA_TOP:
  1292. ret.y_ = layoutBorder_.top_;
  1293. break;
  1294. case VA_BOTTOM:
  1295. ret.y_ = -layoutBorder_.bottom_;
  1296. break;
  1297. default:
  1298. break;
  1299. }
  1300. return ret;
  1301. }
  1302. void UIElement::Detach()
  1303. {
  1304. parent_ = 0;
  1305. MarkDirty();
  1306. }
  1307. }