UIElement.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "ResourceCache.h"
  25. #include "StringUtils.h"
  26. #include "UIElement.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. std::string UIElement::sClipBoard;
  30. static const std::string horizontalAlignments[] =
  31. {
  32. "left",
  33. "center",
  34. "right"
  35. };
  36. static const std::string verticalAlignments[] =
  37. {
  38. "top",
  39. "center",
  40. "bottom"
  41. };
  42. static const std::string focusModes[] =
  43. {
  44. "notfocusable",
  45. "resetfocus",
  46. "focusable",
  47. "focusabledefocusable"
  48. };
  49. static const std::string dragDropModes[] =
  50. {
  51. "disabled",
  52. "source",
  53. "target",
  54. "sourceandtarget"
  55. };
  56. UIElement::UIElement(const std::string& name) :
  57. mName(name),
  58. mParent(0),
  59. mClipBorder(IntRect::sZero),
  60. mPriority(0),
  61. mOpacity(1.0f),
  62. mBringToFront(false),
  63. mBringToBack(true),
  64. mClipChildren(false),
  65. mEnabled(false),
  66. mFocusMode(FM_NOTFOCUSABLE),
  67. mFocus(false),
  68. mSelected(false),
  69. mVisible(true),
  70. mHovering(false),
  71. mDragDropMode(DD_DISABLED),
  72. mLayoutMode(LM_FREE),
  73. mLayoutSpacing(0),
  74. mLayoutBorder(IntRect::sZero),
  75. mResizeNestingLevel(0),
  76. mUpdateLayoutNestingLevel(0),
  77. mPosition(IntVector2::sZero),
  78. mSize(IntVector2::sZero),
  79. mMinSize(IntVector2::sZero),
  80. mMaxSize(M_MAX_INT, M_MAX_INT),
  81. mChildOffset(IntVector2::sZero),
  82. mHorizontalAlignment(HA_LEFT),
  83. mVerticalAlignment(VA_TOP),
  84. mScreenPositionDirty(true),
  85. mDerivedOpacityDirty(true),
  86. mHasColorGradient(false)
  87. {
  88. }
  89. UIElement::~UIElement()
  90. {
  91. // If child elements have outside references, detach them
  92. while (mChildren.size())
  93. {
  94. const SharedPtr<UIElement>& element = mChildren.back();
  95. if (element.getRefCount() > 1)
  96. {
  97. element->mParent = 0;
  98. element->markDirty();
  99. }
  100. mChildren.pop_back();
  101. }
  102. }
  103. void UIElement::setStyle(const XMLElement& element, ResourceCache* cache)
  104. {
  105. if (!cache)
  106. return;
  107. if (element.hasAttribute("name"))
  108. mName = element.getString("name");
  109. if (element.hasChildElement("position"))
  110. setPosition(element.getChildElement("position").getIntVector2("value"));
  111. if (element.hasChildElement("size"))
  112. setSize(element.getChildElement("size").getIntVector2("value"));
  113. if (element.hasChildElement("width"))
  114. setWidth(element.getChildElement("width").getInt("value"));
  115. if (element.hasChildElement("height"))
  116. setHeight(element.getChildElement("height").getInt("value"));
  117. if (element.hasChildElement("minsize"))
  118. setMinSize(element.getChildElement("minsize").getIntVector2("value"));
  119. if (element.hasChildElement("minwidth"))
  120. setMinWidth(element.getChildElement("minwidth").getInt("value"));
  121. if (element.hasChildElement("minheight"))
  122. setMinHeight(element.getChildElement("minheight").getInt("value"));
  123. if (element.hasChildElement("maxsize"))
  124. setMaxSize(element.getChildElement("maxsize").getIntVector2("value"));
  125. if (element.hasChildElement("maxwidth"))
  126. setMinWidth(element.getChildElement("maxwidth").getInt("value"));
  127. if (element.hasChildElement("maxheight"))
  128. setMinHeight(element.getChildElement("maxheight").getInt("value"));
  129. if (element.hasChildElement("fixedsize"))
  130. setFixedSize(element.getChildElement("fixedsize").getIntVector2("value"));
  131. if (element.hasChildElement("fixedwidth"))
  132. setFixedWidth(element.getChildElement("fixedwidth").getInt("value"));
  133. if (element.hasChildElement("fixedheight"))
  134. setFixedHeight(element.getChildElement("fixedheight").getInt("value"));
  135. if (element.hasChildElement("alignment"))
  136. {
  137. XMLElement alignElem = element.getChildElement("alignment");
  138. std::string horiz;
  139. std::string vert;
  140. if (alignElem.hasAttribute("horizontal"))
  141. horiz = alignElem.getStringLower("horizontal");
  142. if (alignElem.hasAttribute("vertical"))
  143. vert = alignElem.getStringLower("vertical");
  144. if (alignElem.hasAttribute("h"))
  145. horiz = alignElem.getStringLower("h");
  146. if (alignElem.hasAttribute("v"))
  147. vert = alignElem.getStringLower("v");
  148. if (!horiz.empty())
  149. setHorizontalAlignment((HorizontalAlignment)getIndexFromStringList(horiz, horizontalAlignments, 3, 0));
  150. if (!vert.empty())
  151. setVerticalAlignment((VerticalAlignment)getIndexFromStringList(vert, verticalAlignments, 3, 0));
  152. }
  153. if (element.hasChildElement("clipborder"))
  154. setClipBorder(element.getChildElement("clipborder").getIntRect("value"));
  155. if (element.hasChildElement("priority"))
  156. setPriority(element.getChildElement("priority").getInt("value"));
  157. if (element.hasChildElement("opacity"))
  158. setOpacity(element.getChildElement("opacity").getFloat("value"));
  159. if (element.hasChildElement("color"))
  160. {
  161. XMLElement colorElem = element.getChildElement("color");
  162. if (colorElem.hasAttribute("value"))
  163. setColor(colorElem.getColor("value"));
  164. if (colorElem.hasAttribute("topleft"))
  165. setColor(C_TOPLEFT, colorElem.getColor("topleft"));
  166. if (colorElem.hasAttribute("topright"))
  167. setColor(C_TOPRIGHT, colorElem.getColor("topright"));
  168. if (colorElem.hasAttribute("bottomleft"))
  169. setColor(C_BOTTOMLEFT, colorElem.getColor("bottomleft"));
  170. if (colorElem.hasAttribute("bottomright"))
  171. setColor(C_BOTTOMRIGHT, colorElem.getColor("bottomright"));
  172. }
  173. if (element.hasChildElement("bringtofront"))
  174. setBringToFront(element.getChildElement("bringtofront").getBool("enable"));
  175. if (element.hasChildElement("bringtoback"))
  176. setBringToBack(element.getChildElement("bringtoback").getBool("enable"));
  177. if (element.hasChildElement("clipchildren"))
  178. setClipChildren(element.getChildElement("clipchildren").getBool("enable"));
  179. if (element.hasChildElement("enabled"))
  180. setEnabled(element.getChildElement("enabled").getBool("enable"));
  181. if (element.hasChildElement("selected"))
  182. setSelected(element.getChildElement("selected").getBool("enable"));
  183. if (element.hasChildElement("visible"))
  184. setVisible(element.getChildElement("visible").getBool("enable"));
  185. if (element.hasChildElement("focusmode"))
  186. {
  187. std::string focusMode = element.getChildElement("focusmode").getStringLower("value");
  188. setFocusMode((FocusMode)getIndexFromStringList(focusMode, focusModes, 4, 0));
  189. if (focusMode == "defocusable")
  190. setFocusMode(FM_FOCUSABLE_DEFOCUSABLE);
  191. }
  192. if (element.hasChildElement("dragdropmode"))
  193. {
  194. std::string dragDropMode = element.getChildElement("dragdropmode").getStringLower("value");
  195. setDragDropMode(getIndexFromStringList(dragDropMode, dragDropModes, 4, 0));
  196. }
  197. if (element.hasChildElement("layout"))
  198. {
  199. XMLElement layoutElem = element.getChildElement("layout");
  200. std::string mode = layoutElem.getStringLower("mode");
  201. if (mode == "free")
  202. mLayoutMode = LM_FREE;
  203. if ((mode == "horizontal") || (mode == "h"))
  204. mLayoutMode = LM_HORIZONTAL;
  205. if ((mode == "vertical") || (mode == "v"))
  206. mLayoutMode = LM_VERTICAL;
  207. if (layoutElem.hasAttribute("spacing"))
  208. mLayoutSpacing = max(layoutElem.getInt("spacing"), 0);
  209. if (layoutElem.hasAttribute("border"))
  210. setLayoutBorder(layoutElem.getIntRect("border"));
  211. else
  212. updateLayout();
  213. }
  214. if (element.hasChildElement("userdata"))
  215. setUserData(element.getChildElement("userdat").getVariantMap());
  216. }
  217. void UIElement::update(float timeStep)
  218. {
  219. }
  220. void UIElement::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  221. {
  222. // Reset hovering for next frame
  223. mHovering = false;
  224. }
  225. void UIElement::onHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  226. {
  227. mHovering = true;
  228. }
  229. void UIElement::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  230. {
  231. }
  232. void UIElement::onDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  233. {
  234. }
  235. void UIElement::onDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  236. {
  237. }
  238. void UIElement::onDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  239. {
  240. }
  241. bool UIElement::onDragDropTest(UIElement* source)
  242. {
  243. return true;
  244. }
  245. bool UIElement::onDragDropFinish(UIElement* source)
  246. {
  247. return true;
  248. }
  249. void UIElement::onWheel(int delta, int buttons, int qualifiers)
  250. {
  251. }
  252. void UIElement::onKey(int key, int buttons, int qualifiers)
  253. {
  254. }
  255. void UIElement::onChar(unsigned char c, int buttons, int qualifiers)
  256. {
  257. }
  258. void UIElement::onResize()
  259. {
  260. }
  261. void UIElement::onFocus()
  262. {
  263. }
  264. void UIElement::onDefocus()
  265. {
  266. }
  267. void UIElement::setName(const std::string& name)
  268. {
  269. mName = name;
  270. }
  271. void UIElement::setPosition(const IntVector2& position)
  272. {
  273. if (position != mPosition)
  274. {
  275. mPosition = position;
  276. markDirty();
  277. }
  278. }
  279. void UIElement::setPosition(int x, int y)
  280. {
  281. setPosition(IntVector2(x, y));
  282. }
  283. void UIElement::setSize(const IntVector2& size)
  284. {
  285. ++mResizeNestingLevel;
  286. IntVector2 validatedSize;
  287. validatedSize.mX = clamp(size.mX, mMinSize.mX, mMaxSize.mX);
  288. validatedSize.mY = clamp(size.mY, mMinSize.mY, mMaxSize.mY);
  289. if (validatedSize != mSize)
  290. {
  291. mSize = validatedSize;
  292. if (mResizeNestingLevel == 1)
  293. {
  294. // Check if parent element's layout needs to be updated first
  295. if (mParent)
  296. mParent->updateLayout();
  297. markDirty();
  298. onResize();
  299. updateLayout();
  300. using namespace Resized;
  301. VariantMap eventData;
  302. eventData[P_ELEMENT] = (void*)this;
  303. eventData[P_WIDTH] = mSize.mX;
  304. eventData[P_HEIGHT] = mSize.mY;
  305. sendEvent(EVENT_RESIZED, eventData);
  306. }
  307. }
  308. --mResizeNestingLevel;
  309. }
  310. void UIElement::setSize(int width, int height)
  311. {
  312. setSize(IntVector2(width, height));
  313. }
  314. void UIElement::setWidth(int width)
  315. {
  316. setSize(IntVector2(width, mSize.mY));
  317. }
  318. void UIElement::setHeight(int height)
  319. {
  320. setSize(IntVector2(mSize.mX, height));
  321. }
  322. void UIElement::setMinSize(const IntVector2& minSize)
  323. {
  324. mMinSize.mX = max(minSize.mX, 0);
  325. mMinSize.mY = max(minSize.mY, 0);
  326. mMaxSize.mX = max(minSize.mX, mMaxSize.mX);
  327. mMaxSize.mY = max(minSize.mY, mMaxSize.mY);
  328. setSize(mSize);
  329. }
  330. void UIElement::setMinSize(int width, int height)
  331. {
  332. setMinSize(IntVector2(width, height));
  333. }
  334. void UIElement::setMinWidth(int width)
  335. {
  336. setMinSize(IntVector2(width, mMinSize.mY));
  337. }
  338. void UIElement::setMinHeight(int height)
  339. {
  340. setMinSize(IntVector2(mMinSize.mX, height));
  341. }
  342. void UIElement::setMaxSize(const IntVector2& maxSize)
  343. {
  344. mMaxSize.mX = max(mMinSize.mX, maxSize.mX);
  345. mMaxSize.mY = max(mMinSize.mY, maxSize.mY);
  346. setSize(mSize);
  347. }
  348. void UIElement::setMaxSize(int width, int height)
  349. {
  350. setMaxSize(IntVector2(width, height));
  351. }
  352. void UIElement::setMaxWidth(int width)
  353. {
  354. setMaxSize(IntVector2(width, mMaxSize.mY));
  355. }
  356. void UIElement::setMaxHeight(int height)
  357. {
  358. setMaxSize(IntVector2(mMaxSize.mX, height));
  359. }
  360. void UIElement::setFixedSize(const IntVector2& size)
  361. {
  362. mMinSize = mMaxSize = IntVector2(max(size.mX, 0), max(size.mY, 0));
  363. setSize(size);
  364. }
  365. void UIElement::setFixedSize(int width, int height)
  366. {
  367. setFixedSize(IntVector2(width, height));
  368. }
  369. void UIElement::setFixedWidth(int width)
  370. {
  371. mMinSize.mX = mMaxSize.mX = max(width, 0);
  372. setWidth(width);
  373. }
  374. void UIElement::setFixedHeight(int height)
  375. {
  376. mMinSize.mY = mMaxSize.mY = max(height, 0);
  377. setHeight(height);
  378. }
  379. void UIElement::setAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  380. {
  381. mHorizontalAlignment = hAlign;
  382. mVerticalAlignment = vAlign;
  383. markDirty();
  384. }
  385. void UIElement::setHorizontalAlignment(HorizontalAlignment align)
  386. {
  387. mHorizontalAlignment = align;
  388. markDirty();
  389. }
  390. void UIElement::setVerticalAlignment(VerticalAlignment align)
  391. {
  392. mVerticalAlignment = align;
  393. markDirty();
  394. }
  395. void UIElement::setClipBorder(const IntRect& rect)
  396. {
  397. mClipBorder.mLeft = max(rect.mLeft, 0);
  398. mClipBorder.mTop = max(rect.mTop, 0);
  399. mClipBorder.mRight = max(rect.mRight, 0);
  400. mClipBorder.mBottom = max(rect.mBottom, 0);
  401. }
  402. void UIElement::setClipBorder(int left, int top, int right, int bottom)
  403. {
  404. setClipBorder(IntRect(left, top, right, bottom));
  405. }
  406. void UIElement::setColor(const Color& color)
  407. {
  408. for (unsigned i = 0; i < MAX_UIELEMENT_CORNERS; ++i)
  409. mColor[i] = color;
  410. mHasColorGradient = false;
  411. }
  412. void UIElement::setColor(Corner corner, const Color& color)
  413. {
  414. mColor[corner] = color;
  415. mHasColorGradient = false;
  416. for (unsigned i = 0; i < MAX_UIELEMENT_CORNERS; ++i)
  417. {
  418. if ((i != corner) && (mColor[i] != mColor[corner]))
  419. mHasColorGradient = true;
  420. }
  421. }
  422. void UIElement::setPriority(int priority)
  423. {
  424. mPriority = priority;
  425. }
  426. void UIElement::setOpacity(float opacity)
  427. {
  428. mOpacity = clamp(opacity, 0.0f, 1.0f);
  429. markDirty();
  430. }
  431. void UIElement::setBringToFront(bool enable)
  432. {
  433. mBringToFront = enable;
  434. }
  435. void UIElement::setBringToBack(bool enable)
  436. {
  437. mBringToBack = enable;
  438. }
  439. void UIElement::setClipChildren(bool enable)
  440. {
  441. mClipChildren = enable;
  442. }
  443. void UIElement::setEnabled(bool enable)
  444. {
  445. mEnabled = enable;
  446. }
  447. void UIElement::setFocusMode(FocusMode mode)
  448. {
  449. mFocusMode = mode;
  450. }
  451. void UIElement::setFocus(bool enable)
  452. {
  453. if (mFocusMode < FM_FOCUSABLE)
  454. enable = false;
  455. if (enable != mFocus)
  456. {
  457. mFocus = enable;
  458. if (enable)
  459. onFocus();
  460. else
  461. onDefocus();
  462. using namespace Focused;
  463. VariantMap eventData;
  464. eventData[P_ELEMENT] = (void*)this;
  465. sendEvent(mFocus ? EVENT_FOCUSED : EVENT_DEFOCUSED, eventData);
  466. }
  467. }
  468. void UIElement::setSelected(bool enable)
  469. {
  470. mSelected = enable;
  471. }
  472. void UIElement::setVisible(bool enable)
  473. {
  474. if (enable != mVisible)
  475. {
  476. mVisible = enable;
  477. // Parent's layout may change as a result of visibility change
  478. if (mParent)
  479. mParent->updateLayout();
  480. using namespace VisibleChanged;
  481. VariantMap eventData;
  482. eventData[P_ELEMENT] = (void*)this;
  483. eventData[P_VISIBLE] = mVisible;
  484. sendEvent(EVENT_VISIBLECHANGED, eventData);
  485. }
  486. }
  487. void UIElement::setDragDropMode(unsigned mode)
  488. {
  489. mDragDropMode = mode;
  490. }
  491. void UIElement::setStyleAuto(XMLFile* file, ResourceCache* cache)
  492. {
  493. XMLElement element = getStyleElement(file);
  494. setStyle(element, cache);
  495. }
  496. void UIElement::setLayout(LayoutMode mode, int spacing, const IntRect& border)
  497. {
  498. mLayoutMode = mode;
  499. mLayoutSpacing = max(spacing, 0);
  500. mLayoutBorder = IntRect(max(border.mLeft, 0), max(border.mTop, 0), max(border.mRight, 0), max(border.mBottom, 0));
  501. updateLayout();
  502. }
  503. void UIElement::setLayoutSpacing(int spacing)
  504. {
  505. mLayoutSpacing = max(spacing, 0);
  506. updateLayout();
  507. }
  508. void UIElement::setLayoutBorder(const IntRect& border)
  509. {
  510. mLayoutBorder = IntRect(max(border.mLeft, 0), max(border.mTop, 0), max(border.mRight, 0), max(border.mBottom, 0));
  511. updateLayout();
  512. }
  513. void UIElement::setUserData(const VariantMap& userData)
  514. {
  515. mUserData = userData;
  516. }
  517. void UIElement::updateLayout()
  518. {
  519. if ((mLayoutMode == LM_FREE) || (mUpdateLayoutNestingLevel))
  520. return;
  521. // Prevent further updates while this update happens
  522. disableLayoutUpdate();
  523. std::vector<int> positions;
  524. std::vector<int> sizes;
  525. std::vector<int> minSizes;
  526. std::vector<int> maxSizes;
  527. if (mLayoutMode == LM_HORIZONTAL)
  528. {
  529. int minChildHeight = 0;
  530. for (unsigned i = 0; i < mChildren.size(); ++i)
  531. {
  532. if (!mChildren[i]->isVisible())
  533. continue;
  534. positions.push_back(0);
  535. sizes.push_back(mChildren[i]->getWidth());
  536. minSizes.push_back(mChildren[i]->getMinWidth());
  537. maxSizes.push_back(mChildren[i]->getMaxWidth());
  538. minChildHeight = max(minChildHeight, mChildren[i]->getMinHeight());
  539. }
  540. calculateLayout(positions, sizes, minSizes, maxSizes, getWidth(), mLayoutBorder.mLeft, mLayoutBorder.mRight,
  541. mLayoutSpacing);
  542. int width = calculateLayoutParentSize(sizes, mLayoutBorder.mLeft, mLayoutBorder.mRight, mLayoutSpacing);
  543. int height = max(getHeight(), minChildHeight + mLayoutBorder.mTop + mLayoutBorder.mBottom);
  544. int minWidth = calculateLayoutParentSize(minSizes, mLayoutBorder.mLeft, mLayoutBorder.mRight, mLayoutSpacing);
  545. int minHeight = minChildHeight + mLayoutBorder.mTop + mLayoutBorder.mBottom;
  546. setMinSize(minWidth, minHeight);
  547. setSize(width, height);
  548. unsigned j = 0;
  549. for (unsigned i = 0; i < mChildren.size(); ++i)
  550. {
  551. if (!mChildren[i]->isVisible())
  552. continue;
  553. mChildren[i]->setHorizontalAlignment(HA_LEFT);
  554. mChildren[i]->setPosition(positions[j], getLayoutChildPosition(mChildren[i]).mY);
  555. mChildren[i]->setSize(sizes[j], height - mLayoutBorder.mTop - mLayoutBorder.mBottom);
  556. ++j;
  557. }
  558. }
  559. if (mLayoutMode == LM_VERTICAL)
  560. {
  561. int minChildWidth = 0;
  562. int maxChildWidth = M_MAX_INT;
  563. for (unsigned i = 0; i < mChildren.size(); ++i)
  564. {
  565. if (!mChildren[i]->isVisible())
  566. continue;
  567. positions.push_back(0);
  568. sizes.push_back(mChildren[i]->getHeight());
  569. minSizes.push_back(mChildren[i]->getMinHeight());
  570. maxSizes.push_back(mChildren[i]->getMaxHeight());
  571. minChildWidth = max(minChildWidth, mChildren[i]->getMinWidth());
  572. }
  573. calculateLayout(positions, sizes, minSizes, maxSizes, getHeight(), mLayoutBorder.mTop, mLayoutBorder.mBottom,
  574. mLayoutSpacing);
  575. int height = calculateLayoutParentSize(sizes, mLayoutBorder.mTop, mLayoutBorder.mBottom, mLayoutSpacing);
  576. int width = max(getWidth(), minChildWidth + mLayoutBorder.mLeft + mLayoutBorder.mRight);
  577. int minHeight = calculateLayoutParentSize(minSizes, mLayoutBorder.mTop, mLayoutBorder.mBottom, mLayoutSpacing);
  578. int minWidth = minChildWidth + mLayoutBorder.mLeft + mLayoutBorder.mRight;
  579. setMinSize(minWidth, minHeight);
  580. setSize(width, height);
  581. unsigned j = 0;
  582. for (unsigned i = 0; i < mChildren.size(); ++i)
  583. {
  584. if (!mChildren[i]->isVisible())
  585. continue;
  586. mChildren[i]->setVerticalAlignment(VA_TOP);
  587. mChildren[i]->setPosition(getLayoutChildPosition(mChildren[i]).mX, positions[j]);
  588. mChildren[i]->setSize(width - mLayoutBorder.mLeft - mLayoutBorder.mRight, sizes[j]);
  589. ++j;
  590. }
  591. }
  592. enableLayoutUpdate();
  593. }
  594. void UIElement::disableLayoutUpdate()
  595. {
  596. ++mUpdateLayoutNestingLevel;
  597. }
  598. void UIElement::enableLayoutUpdate()
  599. {
  600. --mUpdateLayoutNestingLevel;
  601. }
  602. void UIElement::bringToFront()
  603. {
  604. // Follow the parent chain to the top level window. If it has BringToFront mode, bring it to front now
  605. UIElement* root = getRootElement();
  606. UIElement* ptr = this;
  607. while ((ptr) && (ptr->getParent() != root))
  608. ptr = ptr->getParent();
  609. if ((!ptr) || (!ptr->getBringToFront()))
  610. return;
  611. // Get the highest priority used by all other top level elements, decrease their priority by one,
  612. // and assign that to new front element. However, take into account only active (enabled) elements
  613. // and those which have the BringToBack flag set
  614. int maxPriority = M_MIN_INT;
  615. std::vector<UIElement*> topLevelElements = root->getChildren();
  616. for (std::vector<UIElement*>::iterator i = topLevelElements.begin(); i != topLevelElements.end(); ++i)
  617. {
  618. UIElement* other = *i;
  619. if ((other->isEnabled()) && (other->getBringToBack()) && (other != ptr))
  620. {
  621. int priority = other->getPriority();
  622. maxPriority = max(priority, maxPriority);
  623. other->setPriority(priority - 1);
  624. }
  625. }
  626. ptr->setPriority(maxPriority);
  627. }
  628. void UIElement::addChild(UIElement* element)
  629. {
  630. if ((!element) || (element->mParent == this) || (mParent == element))
  631. return;
  632. // Add first, then remove from old parent, to ensure the elemen does not get deleted
  633. mChildren.push_back(SharedPtr<UIElement>(element));
  634. if (element->mParent)
  635. element->mParent->removeChild(element);
  636. element->mParent = this;
  637. element->markDirty();
  638. updateLayout();
  639. }
  640. void UIElement::removeChild(UIElement* element)
  641. {
  642. for (std::vector<SharedPtr<UIElement> >::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  643. {
  644. if ((*i) == element)
  645. {
  646. element->mParent = 0;
  647. element->markDirty();
  648. mChildren.erase(i);
  649. updateLayout();
  650. return;
  651. }
  652. }
  653. }
  654. void UIElement::removeAllChildren()
  655. {
  656. while (mChildren.size())
  657. {
  658. const SharedPtr<UIElement>& element = mChildren.back();
  659. element->mParent = 0;
  660. element->markDirty();
  661. mChildren.pop_back();
  662. }
  663. }
  664. IntVector2 UIElement::getScreenPosition()
  665. {
  666. if (mScreenPositionDirty)
  667. {
  668. IntVector2 pos = mPosition;
  669. const UIElement* parent = mParent;
  670. const UIElement* current = this;
  671. while (parent)
  672. {
  673. switch (current->mHorizontalAlignment)
  674. {
  675. case HA_LEFT:
  676. pos.mX += parent->mPosition.mX;
  677. break;
  678. case HA_CENTER:
  679. pos.mX += parent->mPosition.mX + parent->mSize.mX / 2 - current->mSize.mX / 2;
  680. break;
  681. case HA_RIGHT:
  682. pos.mX += parent->mPosition.mX + parent->mSize.mX - current->mSize.mX;
  683. break;
  684. }
  685. switch (current->mVerticalAlignment)
  686. {
  687. case VA_TOP:
  688. pos.mY += parent->mPosition.mY;
  689. break;
  690. case VA_CENTER:
  691. pos.mY += parent->mPosition.mY + parent->mSize.mY / 2 - current->mSize.mY / 2;
  692. break;
  693. case VA_BOTTOM:
  694. pos.mY += parent->mPosition.mY + parent->mSize.mY - current->mSize.mY;
  695. break;
  696. }
  697. pos += parent->mChildOffset;
  698. current = parent;
  699. parent = parent->mParent;
  700. }
  701. mScreenPosition = pos;
  702. mScreenPositionDirty = false;
  703. }
  704. return mScreenPosition;
  705. }
  706. float UIElement::getDerivedOpacity()
  707. {
  708. if (mDerivedOpacityDirty)
  709. {
  710. float opacity = mOpacity;
  711. const UIElement* parent = mParent;
  712. while (parent)
  713. {
  714. opacity *= parent->mOpacity;
  715. parent = parent->mParent;
  716. }
  717. mDerivedOpacity = opacity;
  718. mDerivedOpacityDirty = false;
  719. }
  720. return mDerivedOpacity;
  721. }
  722. std::vector<UIElement*> UIElement::getChildren(bool recursive) const
  723. {
  724. if (!recursive)
  725. {
  726. std::vector<UIElement*> ret;
  727. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  728. ret.push_back(*i);
  729. return ret;
  730. }
  731. else
  732. {
  733. std::vector<UIElement*> allChildren;
  734. getChildrenRecursive(allChildren);
  735. return allChildren;
  736. }
  737. }
  738. unsigned UIElement::getNumChildren(bool recursive) const
  739. {
  740. if (!recursive)
  741. return mChildren.size();
  742. else
  743. {
  744. unsigned allChildren = mChildren.size();
  745. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  746. allChildren += (*i)->getNumChildren(true);
  747. return allChildren;
  748. }
  749. }
  750. UIElement* UIElement::getChild(unsigned index) const
  751. {
  752. return index < mChildren.size() ? mChildren[index] : (UIElement*)0;
  753. }
  754. UIElement* UIElement::getChild(const std::string& name, bool recursive) const
  755. {
  756. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  757. {
  758. if ((*i)->mName == name)
  759. return *i;
  760. if (recursive)
  761. {
  762. UIElement* element = (*i)->getChild(name, true);
  763. if (element)
  764. return element;
  765. }
  766. }
  767. return 0;
  768. }
  769. UIElement* UIElement::getRootElement() const
  770. {
  771. UIElement* root = mParent;
  772. if (!root)
  773. return 0;
  774. while (root->getParent())
  775. root = root->getParent();
  776. return root;
  777. }
  778. XMLElement UIElement::getStyleElement(XMLFile* file) const
  779. {
  780. if (file)
  781. {
  782. XMLElement rootElem = file->getRootElement();
  783. XMLElement childElem = rootElem.getChildElement("element");
  784. while (childElem)
  785. {
  786. if (childElem.getString("type") == getTypeName())
  787. return childElem;
  788. childElem = childElem.getNextElement("element");
  789. }
  790. }
  791. return XMLElement();
  792. }
  793. IntVector2 UIElement::screenToElement(const IntVector2& screenPosition)
  794. {
  795. return screenPosition - getScreenPosition();
  796. }
  797. IntVector2 UIElement::elementToScreen(const IntVector2& position)
  798. {
  799. return position + getScreenPosition();
  800. }
  801. bool UIElement::isInside(IntVector2 position, bool isScreen)
  802. {
  803. if (isScreen)
  804. position = screenToElement(position);
  805. return (position.mX >= 0) && (position.mY >= 0) && (position.mX < mSize.mX) && (position.mY < mSize.mY);
  806. }
  807. bool UIElement::isInsideCombined(IntVector2 position, bool isScreen)
  808. {
  809. // If child elements are clipped, no need to expand the rect
  810. if (mClipChildren)
  811. return isInside(position, isScreen);
  812. if (!isScreen)
  813. position = elementToScreen(position);
  814. IntRect combined = getCombinedScreenRect();
  815. return (position.mX >= combined.mLeft) && (position.mY >= combined.mTop) && (position.mX < combined.mRight) &&
  816. (position.mY < combined.mBottom);
  817. }
  818. IntRect UIElement::getCombinedScreenRect()
  819. {
  820. IntVector2 screenPosition(getScreenPosition());
  821. IntRect combined(screenPosition.mX, screenPosition.mY, screenPosition.mX + mSize.mX, screenPosition.mY + mSize.mY);
  822. for (std::vector<SharedPtr<UIElement> >::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  823. {
  824. IntVector2 childPos = (*i)->getScreenPosition();
  825. const IntVector2& childSize = (*i)->getSize();
  826. if (childPos.mX < combined.mLeft)
  827. combined.mLeft = childPos.mX;
  828. if (childPos.mY < combined.mTop)
  829. combined.mTop = childPos.mY;
  830. if (childPos.mX + childSize.mX > combined.mRight)
  831. combined.mRight = childPos.mX + childSize.mX;
  832. if (childPos.mY + childSize.mY > combined.mBottom)
  833. combined.mBottom = childPos.mY + childSize.mY;
  834. }
  835. return combined;
  836. }
  837. void UIElement::setChildOffset(const IntVector2& offset)
  838. {
  839. if (offset != mChildOffset)
  840. {
  841. mChildOffset = offset;
  842. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  843. (*i)->markDirty();
  844. }
  845. }
  846. void UIElement::setHovering(bool enable)
  847. {
  848. mHovering = enable;
  849. }
  850. void UIElement::adjustScissor(IntRect& currentScissor)
  851. {
  852. if (mClipChildren)
  853. {
  854. IntVector2 screenPos = getScreenPosition();
  855. currentScissor.mLeft = max(currentScissor.mLeft, screenPos.mX + mClipBorder.mLeft);
  856. currentScissor.mTop = max(currentScissor.mTop, screenPos.mY + mClipBorder.mTop);
  857. currentScissor.mRight = min(currentScissor.mRight, screenPos.mX + mSize.mX - mClipBorder.mRight);
  858. currentScissor.mBottom = min(currentScissor.mBottom, screenPos.mY + mSize.mY - mClipBorder.mBottom);
  859. if (currentScissor.mRight < currentScissor.mLeft)
  860. currentScissor.mRight = currentScissor.mLeft;
  861. if (currentScissor.mBottom < currentScissor.mTop)
  862. currentScissor.mBottom = currentScissor.mTop;
  863. }
  864. }
  865. void UIElement::getBatchesWithOffset(IntVector2& offset, std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, IntRect
  866. currentScissor)
  867. {
  868. unsigned initialSize = quads.size();
  869. getBatches(batches, quads, currentScissor);
  870. for (unsigned i = initialSize; i < quads.size(); ++i)
  871. {
  872. quads[i].mLeft += offset.mX;
  873. quads[i].mTop += offset.mY;
  874. quads[i].mRight += offset.mX;
  875. quads[i].mBottom += offset.mY;
  876. }
  877. adjustScissor(currentScissor);
  878. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  879. {
  880. if ((*i)->isVisible())
  881. (*i)->getBatchesWithOffset(offset, batches, quads, currentScissor);
  882. }
  883. }
  884. XMLElement UIElement::getStyleElement(XMLFile* file, const std::string& typeName)
  885. {
  886. if (file)
  887. {
  888. XMLElement rootElem = file->getRootElement();
  889. XMLElement childElem = rootElem.getChildElement("element");
  890. while (childElem)
  891. {
  892. if (childElem.getString("type") == typeName)
  893. return childElem;
  894. childElem = childElem.getNextElement("element");
  895. }
  896. }
  897. return XMLElement();
  898. }
  899. void UIElement::markDirty()
  900. {
  901. mScreenPositionDirty = true;
  902. mDerivedOpacityDirty = true;
  903. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  904. (*i)->markDirty();
  905. }
  906. void UIElement::getChildrenRecursive(std::vector<UIElement*>& dest) const
  907. {
  908. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  909. {
  910. dest.push_back(*i);
  911. (*i)->getChildrenRecursive(dest);
  912. }
  913. }
  914. int UIElement::calculateLayoutParentSize(const std::vector<int>& sizes, int begin, int end, int spacing)
  915. {
  916. int width = begin + end;
  917. for (unsigned i = 0; i < sizes.size(); ++i)
  918. {
  919. // If we are calculating maximum size, and the default is specified, do not overflow it
  920. if (sizes[i] == M_MAX_INT)
  921. return M_MAX_INT;
  922. width += sizes[i];
  923. if (i < sizes.size() - 1)
  924. width += spacing;
  925. }
  926. return width;
  927. }
  928. void UIElement::calculateLayout(std::vector<int>& positions, std::vector<int>& sizes, const std::vector<int>& minSizes,
  929. const std::vector<int>& maxSizes, int targetSize, int begin, int end, int spacing)
  930. {
  931. int numChildren = sizes.size();
  932. if (!numChildren)
  933. return;
  934. int targetTotalSize = targetSize - begin - end - (numChildren - 1) * spacing;
  935. if (targetTotalSize < 0)
  936. targetTotalSize = 0;
  937. int targetChildSize = targetTotalSize / numChildren;
  938. int remainder = targetTotalSize % numChildren;
  939. float add = (float)remainder / numChildren;
  940. float acc = 0.0f;
  941. // Initial pass
  942. for (int i = 0; i < numChildren; ++i)
  943. {
  944. int targetSize = targetChildSize;
  945. if (remainder)
  946. {
  947. acc += add;
  948. if (acc >= 0.5f)
  949. {
  950. acc -= 1.0f;
  951. ++targetSize;
  952. --remainder;
  953. }
  954. }
  955. sizes[i] = clamp(targetSize, minSizes[i], maxSizes[i]);
  956. }
  957. // Error correction passes
  958. for (;;)
  959. {
  960. int actualTotalSize = 0;
  961. for (int i = 0; i < numChildren; ++i)
  962. actualTotalSize += sizes[i];
  963. int error = targetTotalSize - actualTotalSize;
  964. // Break if no error
  965. if (!error)
  966. break;
  967. // Check which of the children can be resized to correct the error. If none, must break
  968. static std::vector<unsigned> resizable;
  969. resizable.clear();
  970. for (int i = 0; i < numChildren; ++i)
  971. {
  972. if ((error < 0) && (sizes[i] > minSizes[i]))
  973. resizable.push_back(i);
  974. else if ((error > 0) && (sizes[i] < maxSizes[i]))
  975. resizable.push_back(i);
  976. }
  977. if (resizable.empty())
  978. break;
  979. int numResizable = resizable.size();
  980. int errorPerChild = error / numResizable;
  981. remainder = (abs(error)) % numResizable;
  982. add = (float)remainder / numResizable;
  983. acc = 0.0f;
  984. for (int i = 0; i < numResizable; ++i)
  985. {
  986. unsigned idx = resizable[i];
  987. int targetSize = sizes[idx] + errorPerChild;
  988. if (remainder)
  989. {
  990. acc += add;
  991. if (acc >= 0.5f)
  992. {
  993. acc -= 1.0f;
  994. targetSize = error < 0 ? targetSize - 1 : targetSize + 1;
  995. --remainder;
  996. }
  997. }
  998. sizes[idx] = clamp(targetSize, minSizes[idx], maxSizes[idx]);
  999. }
  1000. }
  1001. // Calculate final positions
  1002. int position = begin;
  1003. for (int i = 0; i < numChildren; ++i)
  1004. {
  1005. positions[i] = position;
  1006. position += sizes[i];
  1007. position += spacing;
  1008. }
  1009. }
  1010. IntVector2 UIElement::getLayoutChildPosition(UIElement* child)
  1011. {
  1012. IntVector2 ret(IntVector2::sZero);
  1013. HorizontalAlignment ha = child->getHorizontalAlignment();
  1014. switch (ha)
  1015. {
  1016. case HA_LEFT:
  1017. ret.mX = mLayoutBorder.mLeft;
  1018. break;
  1019. case HA_RIGHT:
  1020. ret.mX = -mLayoutBorder.mRight;
  1021. break;
  1022. }
  1023. VerticalAlignment va = child->getVerticalAlignment();
  1024. switch (va)
  1025. {
  1026. case VA_TOP:
  1027. ret.mY = mLayoutBorder.mTop;
  1028. break;
  1029. case VA_BOTTOM:
  1030. ret.mY = -mLayoutBorder.mBottom;
  1031. break;
  1032. }
  1033. return ret;
  1034. }