UIElement.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 "Log.h"
  25. #include "ResourceCache.h"
  26. #include "StringUtils.h"
  27. #include "UIElement.h"
  28. #include "UIEvents.h"
  29. #include "DebugNew.h"
  30. UIElement::UIElement(const std::string& name) :
  31. mName(name),
  32. mParent(0),
  33. mPosition(IntVector2::sZero),
  34. mSize(IntVector2::sZero),
  35. mHorizontalAlignment(HA_LEFT),
  36. mVerticalAlignment(VA_TOP),
  37. mPriority(0),
  38. mOpacity(1.0f),
  39. mBringToFront(false),
  40. mClipChildren(false),
  41. mEnabled(false),
  42. mFocusable(false),
  43. mFocus(false),
  44. mVisible(true),
  45. mScreenPositionDirty(true),
  46. mDerivedOpacityDirty(true),
  47. mHasColorGradient(false)
  48. {
  49. }
  50. UIElement::~UIElement()
  51. {
  52. // If child elements have outside references, detach them
  53. while (mChildren.size())
  54. {
  55. const SharedPtr<UIElement>& element = mChildren.back();
  56. if (element.getRefCount() > 1)
  57. {
  58. element->mParent = 0;
  59. element->markDirty();
  60. }
  61. mChildren.pop_back();
  62. }
  63. }
  64. XMLElement UIElement::loadParameters(XMLFile* file, const std::string& elementName, ResourceCache* cache)
  65. {
  66. if (!file)
  67. SAFE_EXCEPTION_RET("Null UI definition file", XMLElement());
  68. if (!cache)
  69. SAFE_EXCEPTION_RET("Null resource cache", XMLElement());
  70. XMLElement rootElem = file->getRootElement();
  71. XMLElement paramElem = rootElem.getChildElement("element", false);
  72. while (paramElem.notNull())
  73. {
  74. if (paramElem.getString("name") == elementName)
  75. break;
  76. paramElem = paramElem.getNextElement("element");
  77. }
  78. if (paramElem.isNull())
  79. SAFE_EXCEPTION_RET("No UI element definition " + elementName + " in " + file->getName(), XMLElement());
  80. if (paramElem.hasChildElement("position"))
  81. setPosition(paramElem.getChildElement("position").getIntVector2("value"));
  82. if (paramElem.hasChildElement("size"))
  83. setSize(paramElem.getChildElement("size").getIntVector2("value"));
  84. if (paramElem.hasChildElement("alignment"))
  85. {
  86. XMLElement alignElem = paramElem.getChildElement("alignment");
  87. std::string horiz;
  88. std::string vert;
  89. if (alignElem.hasAttribute("h"))
  90. horiz = alignElem.getStringLower("h");
  91. if (alignElem.hasAttribute("v"))
  92. vert = alignElem.getStringLower("v");
  93. if (alignElem.hasAttribute("horizontal"))
  94. horiz = alignElem.getStringLower("horizontal");
  95. if (alignElem.hasAttribute("vertical"))
  96. vert = alignElem.getStringLower("vertical");
  97. if (horiz == "left")
  98. setHorizontalAlignment(HA_LEFT);
  99. if (horiz == "center")
  100. setHorizontalAlignment(HA_CENTER);
  101. if (horiz == "right")
  102. setHorizontalAlignment(HA_RIGHT);
  103. if (vert == "top")
  104. setVerticalAlignment(VA_TOP);
  105. if (vert == "center")
  106. setVerticalAlignment(VA_CENTER);
  107. if (vert == "bottom")
  108. setVerticalAlignment(VA_BOTTOM);
  109. }
  110. if (paramElem.hasChildElement("priority"))
  111. setPriority(paramElem.getChildElement("priority").getInt("value"));
  112. if (paramElem.hasChildElement("opacity"))
  113. setOpacity(paramElem.getChildElement("opacity").getFloat("value"));
  114. if (paramElem.hasChildElement("color"))
  115. {
  116. XMLElement colorElem = paramElem.getChildElement("color");
  117. if (colorElem.hasAttribute("value"))
  118. setColor(colorElem.getColor("value"));
  119. if (colorElem.hasAttribute("topleft"))
  120. setColor(C_TOPLEFT, colorElem.getColor("topleft"));
  121. if (colorElem.hasAttribute("topright"))
  122. setColor(C_TOPRIGHT, colorElem.getColor("topright"));
  123. if (colorElem.hasAttribute("bottomleft"))
  124. setColor(C_BOTTOMLEFT, colorElem.getColor("bottomleft"));
  125. if (colorElem.hasAttribute("bottomright"))
  126. setColor(C_BOTTOMRIGHT, colorElem.getColor("bottomright"));
  127. }
  128. if (paramElem.hasChildElement("bringtofront"))
  129. setBringToFront(paramElem.getChildElement("bringtofront").getBool("enable"));
  130. if (paramElem.hasChildElement("clipchildren"))
  131. setClipChildren(paramElem.getChildElement("clipchildren").getBool("enable"));
  132. if (paramElem.hasChildElement("enabled"))
  133. setEnabled(paramElem.getChildElement("enabled").getBool("enable"));
  134. if (paramElem.hasChildElement("focusable"))
  135. setFocusable(paramElem.getChildElement("focusable").getBool("enable"));
  136. if (paramElem.hasChildElement("visible"))
  137. setVisible(paramElem.getChildElement("visible").getBool("enable"));
  138. return paramElem;
  139. }
  140. void UIElement::update(float timeStep)
  141. {
  142. }
  143. void UIElement::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  144. {
  145. }
  146. IntVector2 UIElement::getScreenPosition()
  147. {
  148. if (mScreenPositionDirty)
  149. {
  150. IntVector2 pos = mPosition;
  151. const UIElement* parent = mParent;
  152. const UIElement* current = this;
  153. while (parent)
  154. {
  155. switch (current->mHorizontalAlignment)
  156. {
  157. case HA_LEFT:
  158. pos.mX += parent->mPosition.mX;
  159. break;
  160. case HA_CENTER:
  161. pos.mX += parent->mPosition.mX + parent->mSize.mX / 2 - current->mSize.mX / 2;
  162. break;
  163. case HA_RIGHT:
  164. pos.mX += parent->mPosition.mX + parent->mSize.mX - current->mSize.mX;
  165. break;
  166. }
  167. switch (current->mVerticalAlignment)
  168. {
  169. case VA_TOP:
  170. pos.mY += parent->mPosition.mY;
  171. break;
  172. case VA_CENTER:
  173. pos.mY += parent->mPosition.mY + parent->mSize.mY / 2 - current->mSize.mY / 2;
  174. break;
  175. case VA_BOTTOM:
  176. pos.mY += parent->mPosition.mY + parent->mSize.mY - current->mSize.mY;
  177. break;
  178. }
  179. current = parent;
  180. parent = parent->mParent;
  181. }
  182. mScreenPosition = pos;
  183. mScreenPositionDirty = false;
  184. }
  185. return mScreenPosition;
  186. }
  187. float UIElement::getDerivedOpacity()
  188. {
  189. if (mDerivedOpacityDirty)
  190. {
  191. float opacity = mOpacity;
  192. const UIElement* parent = mParent;
  193. while (parent)
  194. {
  195. opacity *= parent->mOpacity;
  196. parent = parent->mParent;
  197. }
  198. mDerivedOpacity = opacity;
  199. mDerivedOpacityDirty = false;
  200. }
  201. return mDerivedOpacity;
  202. }
  203. void UIElement::onChar(unsigned key)
  204. {
  205. }
  206. void UIElement::onHover(const IntVector2& position, const IntVector2& screenPosition)
  207. {
  208. }
  209. void UIElement::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  210. {
  211. }
  212. void UIElement::onDragStart(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  213. {
  214. }
  215. void UIElement::onDragEnd(const IntVector2& position, const IntVector2& screenPosition)
  216. {
  217. }
  218. void UIElement::onDragMove(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  219. {
  220. }
  221. void UIElement::setName(const std::string& name)
  222. {
  223. mName = name;
  224. }
  225. void UIElement::setPosition(const IntVector2& position)
  226. {
  227. mPosition = position;
  228. markDirty();
  229. }
  230. void UIElement::setPosition(int x, int y)
  231. {
  232. setPosition(IntVector2(x, y));
  233. }
  234. void UIElement::setSize(const IntVector2& size)
  235. {
  236. mSize.mX = max(size.mX, 0);
  237. mSize.mY = max(size.mY, 0);
  238. markDirty();
  239. }
  240. void UIElement::setSize(int width, int height)
  241. {
  242. setSize(IntVector2(width, height));
  243. }
  244. void UIElement::setWidth(int width)
  245. {
  246. setSize(IntVector2(width, mSize.mY));
  247. }
  248. void UIElement::setHeight(int height)
  249. {
  250. setSize(IntVector2(mSize.mX, height));
  251. }
  252. void UIElement::setAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
  253. {
  254. mHorizontalAlignment = hAlign;
  255. mVerticalAlignment = vAlign;
  256. markDirty();
  257. }
  258. void UIElement::setHorizontalAlignment(HorizontalAlignment align)
  259. {
  260. mHorizontalAlignment = align;
  261. markDirty();
  262. }
  263. void UIElement::setVerticalAlignment(VerticalAlignment align)
  264. {
  265. mVerticalAlignment = align;
  266. markDirty();
  267. }
  268. void UIElement::setColor(const Color& color)
  269. {
  270. for (unsigned i = 0; i < NUM_UIELEMENT_CORNERS; ++i)
  271. mColor[i] = color;
  272. mHasColorGradient = false;
  273. }
  274. void UIElement::setColor(float r, float g, float b)
  275. {
  276. setColor(Color(r, g, b));
  277. }
  278. void UIElement::setColor(UIElementCorner corner, const Color& color)
  279. {
  280. mColor[corner] = color;
  281. mHasColorGradient = false;
  282. for (unsigned i = 0; i < NUM_UIELEMENT_CORNERS; ++i)
  283. {
  284. if ((i != corner) && (mColor[i] != mColor[corner]))
  285. mHasColorGradient = true;
  286. }
  287. }
  288. void UIElement::setColor(UIElementCorner corner, float r, float g, float b)
  289. {
  290. setColor(corner, Color(r, g, b));
  291. }
  292. void UIElement::setPriority(int priority)
  293. {
  294. mPriority = priority;
  295. }
  296. void UIElement::setOpacity(float opacity)
  297. {
  298. mOpacity = clamp(opacity, 0.0f, 1.0f);
  299. markDirty();
  300. }
  301. void UIElement::setBringToFront(bool enable)
  302. {
  303. mBringToFront = enable;
  304. }
  305. void UIElement::setClipChildren(bool enable)
  306. {
  307. mClipChildren = enable;
  308. }
  309. void UIElement::setEnabled(bool enable)
  310. {
  311. mEnabled = enable;
  312. }
  313. void UIElement::setFocusable(bool enable)
  314. {
  315. mFocusable = enable;
  316. }
  317. void UIElement::setFocus(bool enable)
  318. {
  319. if (!mFocusable)
  320. enable = false;
  321. if (enable != mFocus)
  322. {
  323. mFocus = enable;
  324. using namespace Focused;
  325. VariantMap eventData;
  326. eventData[P_ELEMENT] = (void*)this;
  327. sendEvent(mFocus ? EVENT_FOCUSED : EVENT_DEFOCUSED, eventData);
  328. }
  329. }
  330. void UIElement::setVisible(bool enable)
  331. {
  332. mVisible = enable;
  333. }
  334. void UIElement::addChild(UIElement* element)
  335. {
  336. if ((!element) || (element->mParent == this) || (mParent == element))
  337. return;
  338. if (element->mParent)
  339. element->mParent->removeChild(element);
  340. mChildren.push_back(SharedPtr<UIElement>(element));
  341. element->mParent = this;
  342. element->markDirty();
  343. }
  344. void UIElement::removeChild(UIElement* element)
  345. {
  346. for (std::vector<SharedPtr<UIElement> >::iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  347. {
  348. if ((*i) == element)
  349. {
  350. element->mParent = 0;
  351. element->markDirty();
  352. mChildren.erase(i);
  353. return;
  354. }
  355. }
  356. }
  357. void UIElement::removeAllChildren()
  358. {
  359. while (mChildren.size())
  360. {
  361. const SharedPtr<UIElement>& element = mChildren.back();
  362. element->mParent = 0;
  363. element->markDirty();
  364. mChildren.pop_back();
  365. }
  366. }
  367. std::vector<UIElement*> UIElement::getChildren(bool recursive) const
  368. {
  369. if (!recursive)
  370. {
  371. std::vector<UIElement*> ret;
  372. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  373. ret.push_back(*i);
  374. return ret;
  375. }
  376. else
  377. {
  378. std::vector<UIElement*> allChildren;
  379. getChildrenRecursive(allChildren);
  380. return allChildren;
  381. }
  382. }
  383. unsigned UIElement::getNumChildren(bool recursive) const
  384. {
  385. if (!recursive)
  386. return mChildren.size();
  387. else
  388. {
  389. unsigned allChildren = mChildren.size();
  390. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  391. allChildren += (*i)->getNumChildren(true);
  392. return allChildren;
  393. }
  394. }
  395. UIElement* UIElement::getChild(unsigned index) const
  396. {
  397. if (index >= mChildren.size())
  398. return 0;
  399. return mChildren[index];
  400. }
  401. UIElement* UIElement::getChild(const std::string& name, bool recursive) const
  402. {
  403. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  404. {
  405. if ((*i)->mName == name)
  406. return *i;
  407. if (recursive)
  408. {
  409. UIElement* element = (*i)->getChild(name, true);
  410. if (element)
  411. return element;
  412. }
  413. }
  414. return 0;
  415. }
  416. IntVector2 UIElement::screenToElement(const IntVector2& screenPosition)
  417. {
  418. return screenPosition - getScreenPosition();
  419. }
  420. IntVector2 UIElement::elementToScreen(const IntVector2& position)
  421. {
  422. return position + getScreenPosition();
  423. }
  424. void UIElement::adjustScissor(IntRect& currentScissor)
  425. {
  426. if (mClipChildren)
  427. {
  428. IntVector2 screenPos = getScreenPosition();
  429. currentScissor.mLeft = max(currentScissor.mLeft, screenPos.mX);
  430. currentScissor.mTop = max(currentScissor.mTop, screenPos.mY);
  431. currentScissor.mRight = min(currentScissor.mRight, screenPos.mX + mSize.mX);
  432. currentScissor.mBottom = min(currentScissor.mBottom, screenPos.mY + mSize.mY);
  433. }
  434. }
  435. void UIElement::markDirty()
  436. {
  437. if ((mScreenPositionDirty) && (mDerivedOpacityDirty))
  438. return;
  439. mScreenPositionDirty = true;
  440. mDerivedOpacityDirty = true;
  441. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  442. (*i)->markDirty();
  443. }
  444. void UIElement::getChildrenRecursive(std::vector<UIElement*>& dest) const
  445. {
  446. for (std::vector<SharedPtr<UIElement> >::const_iterator i = mChildren.begin(); i != mChildren.end(); ++i)
  447. {
  448. dest.push_back(*i);
  449. (*i)->getChildrenRecursive(dest);
  450. }
  451. }