IGUIElement.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "IReferenceCounted.h"
  6. #include "rect.h"
  7. #include "irrString.h"
  8. #include "IEventReceiver.h"
  9. #include "EGUIElementTypes.h"
  10. #include "EGUIAlignment.h"
  11. #include "IAttributes.h"
  12. #include "IGUIEnvironment.h"
  13. #include <cassert>
  14. #include <algorithm>
  15. #include <list>
  16. #include <vector>
  17. namespace irr
  18. {
  19. namespace gui
  20. {
  21. //! Base class of all GUI elements.
  22. class IGUIElement : virtual public IReferenceCounted, public IEventReceiver
  23. {
  24. public:
  25. //! Constructor
  26. IGUIElement(EGUI_ELEMENT_TYPE type, IGUIEnvironment *environment, IGUIElement *parent,
  27. s32 id, const core::rect<s32> &rectangle) :
  28. Parent(0),
  29. RelativeRect(rectangle), AbsoluteRect(rectangle),
  30. AbsoluteClippingRect(rectangle), DesiredRect(rectangle),
  31. MaxSize(0, 0), MinSize(1, 1), IsVisible(true), IsEnabled(true),
  32. IsSubElement(false), NoClip(false), ID(id), IsTabStop(false), TabOrder(-1), IsTabGroup(false),
  33. AlignLeft(EGUIA_UPPERLEFT), AlignRight(EGUIA_UPPERLEFT), AlignTop(EGUIA_UPPERLEFT), AlignBottom(EGUIA_UPPERLEFT),
  34. Environment(environment), Type(type)
  35. {
  36. #ifdef _DEBUG
  37. setDebugName("IGUIElement");
  38. #endif
  39. // if we were given a parent to attach to
  40. if (parent) {
  41. parent->addChildToEnd(this);
  42. recalculateAbsolutePosition(true);
  43. }
  44. }
  45. //! Destructor
  46. virtual ~IGUIElement()
  47. {
  48. for (auto child : Children) {
  49. child->Parent = nullptr;
  50. child->drop();
  51. }
  52. }
  53. //! Returns parent of this element.
  54. IGUIElement *getParent() const
  55. {
  56. return Parent;
  57. }
  58. //! Returns the relative rectangle of this element.
  59. core::rect<s32> getRelativePosition() const
  60. {
  61. return RelativeRect;
  62. }
  63. //! Sets the relative rectangle of this element.
  64. /** \param r The absolute position to set */
  65. void setRelativePosition(const core::rect<s32> &r)
  66. {
  67. if (Parent) {
  68. const core::rect<s32> &r2 = Parent->getAbsolutePosition();
  69. core::dimension2df d((f32)(r2.getSize().Width), (f32)(r2.getSize().Height));
  70. if (AlignLeft == EGUIA_SCALE)
  71. ScaleRect.UpperLeftCorner.X = (f32)r.UpperLeftCorner.X / d.Width;
  72. if (AlignRight == EGUIA_SCALE)
  73. ScaleRect.LowerRightCorner.X = (f32)r.LowerRightCorner.X / d.Width;
  74. if (AlignTop == EGUIA_SCALE)
  75. ScaleRect.UpperLeftCorner.Y = (f32)r.UpperLeftCorner.Y / d.Height;
  76. if (AlignBottom == EGUIA_SCALE)
  77. ScaleRect.LowerRightCorner.Y = (f32)r.LowerRightCorner.Y / d.Height;
  78. }
  79. DesiredRect = r;
  80. updateAbsolutePosition();
  81. }
  82. //! Sets the relative rectangle of this element, maintaining its current width and height
  83. /** \param position The new relative position to set. Width and height will not be changed. */
  84. void setRelativePosition(const core::position2di &position)
  85. {
  86. const core::dimension2di mySize = RelativeRect.getSize();
  87. const core::rect<s32> rectangle(position.X, position.Y,
  88. position.X + mySize.Width, position.Y + mySize.Height);
  89. setRelativePosition(rectangle);
  90. }
  91. //! Sets the relative rectangle of this element as a proportion of its parent's area.
  92. /** \note This method used to be 'void setRelativePosition(const core::rect<f32>& r)'
  93. \param r The rectangle to set, interpreted as a proportion of the parent's area.
  94. Meaningful values are in the range [0...1], unless you intend this element to spill
  95. outside its parent. */
  96. void setRelativePositionProportional(const core::rect<f32> &r)
  97. {
  98. if (!Parent)
  99. return;
  100. const core::dimension2di &d = Parent->getAbsolutePosition().getSize();
  101. DesiredRect = core::rect<s32>(
  102. core::floor32((f32)d.Width * r.UpperLeftCorner.X),
  103. core::floor32((f32)d.Height * r.UpperLeftCorner.Y),
  104. core::floor32((f32)d.Width * r.LowerRightCorner.X),
  105. core::floor32((f32)d.Height * r.LowerRightCorner.Y));
  106. ScaleRect = r;
  107. updateAbsolutePosition();
  108. }
  109. //! Gets the absolute rectangle of this element
  110. core::rect<s32> getAbsolutePosition() const
  111. {
  112. return AbsoluteRect;
  113. }
  114. //! Returns the visible area of the element.
  115. core::rect<s32> getAbsoluteClippingRect() const
  116. {
  117. return AbsoluteClippingRect;
  118. }
  119. //! Sets whether the element will ignore its parent's clipping rectangle
  120. /** \param noClip If true, the element will not be clipped by its parent's clipping rectangle. */
  121. void setNotClipped(bool noClip)
  122. {
  123. NoClip = noClip;
  124. updateAbsolutePosition();
  125. }
  126. //! Gets whether the element will ignore its parent's clipping rectangle
  127. /** \return true if the element is not clipped by its parent's clipping rectangle. */
  128. bool isNotClipped() const
  129. {
  130. return NoClip;
  131. }
  132. //! Sets the maximum size allowed for this element
  133. /** If set to 0,0, there is no maximum size */
  134. void setMaxSize(core::dimension2du size)
  135. {
  136. MaxSize = size;
  137. updateAbsolutePosition();
  138. }
  139. //! Sets the minimum size allowed for this element
  140. void setMinSize(core::dimension2du size)
  141. {
  142. MinSize = size;
  143. if (MinSize.Width < 1)
  144. MinSize.Width = 1;
  145. if (MinSize.Height < 1)
  146. MinSize.Height = 1;
  147. updateAbsolutePosition();
  148. }
  149. //! The alignment defines how the borders of this element will be positioned when the parent element is resized.
  150. void setAlignment(EGUI_ALIGNMENT left, EGUI_ALIGNMENT right, EGUI_ALIGNMENT top, EGUI_ALIGNMENT bottom)
  151. {
  152. AlignLeft = left;
  153. AlignRight = right;
  154. AlignTop = top;
  155. AlignBottom = bottom;
  156. if (Parent) {
  157. core::rect<s32> r(Parent->getAbsolutePosition());
  158. core::dimension2df d((f32)r.getSize().Width, (f32)r.getSize().Height);
  159. if (AlignLeft == EGUIA_SCALE)
  160. ScaleRect.UpperLeftCorner.X = (f32)DesiredRect.UpperLeftCorner.X / d.Width;
  161. if (AlignRight == EGUIA_SCALE)
  162. ScaleRect.LowerRightCorner.X = (f32)DesiredRect.LowerRightCorner.X / d.Width;
  163. if (AlignTop == EGUIA_SCALE)
  164. ScaleRect.UpperLeftCorner.Y = (f32)DesiredRect.UpperLeftCorner.Y / d.Height;
  165. if (AlignBottom == EGUIA_SCALE)
  166. ScaleRect.LowerRightCorner.Y = (f32)DesiredRect.LowerRightCorner.Y / d.Height;
  167. }
  168. }
  169. //! How left element border is aligned when parent is resized
  170. EGUI_ALIGNMENT getAlignLeft() const
  171. {
  172. return AlignLeft;
  173. }
  174. //! How right element border is aligned when parent is resized
  175. EGUI_ALIGNMENT getAlignRight() const
  176. {
  177. return AlignRight;
  178. }
  179. //! How top element border is aligned when parent is resized
  180. EGUI_ALIGNMENT getAlignTop() const
  181. {
  182. return AlignTop;
  183. }
  184. //! How bottom element border is aligned when parent is resized
  185. EGUI_ALIGNMENT getAlignBottom() const
  186. {
  187. return AlignBottom;
  188. }
  189. //! Updates the absolute position.
  190. virtual void updateAbsolutePosition()
  191. {
  192. recalculateAbsolutePosition(false);
  193. // update all children
  194. for (auto child : Children) {
  195. child->updateAbsolutePosition();
  196. }
  197. }
  198. //! Returns the topmost GUI element at the specific position.
  199. /**
  200. This will check this GUI element and all of its descendants, so it
  201. may return this GUI element. To check all GUI elements, call this
  202. function on device->getGUIEnvironment()->getRootGUIElement(). Note
  203. that the root element is the size of the screen, so doing so (with
  204. an on-screen point) will always return the root element if no other
  205. element is above it at that point.
  206. \param point: The point at which to find a GUI element.
  207. \return The topmost GUI element at that point, or 0 if there are
  208. no candidate elements at this point.
  209. */
  210. virtual IGUIElement *getElementFromPoint(const core::position2d<s32> &point)
  211. {
  212. IGUIElement *target = 0;
  213. if (isVisible()) {
  214. // we have to search from back to front, because later children
  215. // might be drawn over the top of earlier ones.
  216. auto it = Children.rbegin();
  217. auto ie = Children.rend();
  218. while (it != ie) {
  219. target = (*it)->getElementFromPoint(point);
  220. if (target)
  221. return target;
  222. ++it;
  223. }
  224. }
  225. if (isVisible() && isPointInside(point))
  226. target = this;
  227. return target;
  228. }
  229. //! Returns true if a point is within this element.
  230. /** Elements with a shape other than a rectangle should override this method */
  231. virtual bool isPointInside(const core::position2d<s32> &point) const
  232. {
  233. return AbsoluteClippingRect.isPointInside(point);
  234. }
  235. //! Adds a GUI element as new child of this element.
  236. virtual void addChild(IGUIElement *child)
  237. {
  238. if (child && child != this) {
  239. addChildToEnd(child);
  240. child->updateAbsolutePosition();
  241. }
  242. }
  243. //! Removes a child.
  244. virtual void removeChild(IGUIElement *child)
  245. {
  246. assert(child->Parent == this);
  247. Children.erase(child->ParentPos);
  248. child->Parent = nullptr;
  249. child->drop();
  250. }
  251. //! Removes all children.
  252. virtual void removeAllChildren()
  253. {
  254. while (!Children.empty()) {
  255. auto child = Children.back();
  256. child->remove();
  257. }
  258. }
  259. //! Removes this element from its parent.
  260. virtual void remove()
  261. {
  262. if (Parent)
  263. Parent->removeChild(this);
  264. }
  265. //! Draws the element and its children.
  266. virtual void draw()
  267. {
  268. if (isVisible()) {
  269. for (auto child : Children)
  270. child->draw();
  271. }
  272. }
  273. //! animate the element and its children.
  274. virtual void OnPostRender(u32 timeMs)
  275. {
  276. if (isVisible()) {
  277. for (auto child : Children)
  278. child->OnPostRender(timeMs);
  279. }
  280. }
  281. //! Moves this element.
  282. virtual void move(core::position2d<s32> absoluteMovement)
  283. {
  284. setRelativePosition(DesiredRect + absoluteMovement);
  285. }
  286. //! Returns true if element is visible.
  287. virtual bool isVisible() const
  288. {
  289. return IsVisible;
  290. }
  291. //! Check whether the element is truly visible, taking into accounts its parents' visibility
  292. /** \return true if the element and all its parents are visible,
  293. false if this or any parent element is invisible. */
  294. virtual bool isTrulyVisible() const
  295. {
  296. if (!IsVisible)
  297. return false;
  298. if (!Parent)
  299. return true;
  300. return Parent->isTrulyVisible();
  301. }
  302. //! Sets the visible state of this element.
  303. virtual void setVisible(bool visible)
  304. {
  305. IsVisible = visible;
  306. }
  307. //! Returns true if this element was created as part of its parent control
  308. virtual bool isSubElement() const
  309. {
  310. return IsSubElement;
  311. }
  312. //! Sets whether this control was created as part of its parent.
  313. /** For example, it is true when a scrollbar is part of a listbox.
  314. SubElements are not saved to disk when calling guiEnvironment->saveGUI() */
  315. virtual void setSubElement(bool subElement)
  316. {
  317. IsSubElement = subElement;
  318. }
  319. //! If set to true, the focus will visit this element when using the tab key to cycle through elements.
  320. /** If this element is a tab group (see isTabGroup/setTabGroup) then
  321. ctrl+tab will be used instead. */
  322. void setTabStop(bool enable)
  323. {
  324. IsTabStop = enable;
  325. }
  326. //! Returns true if this element can be focused by navigating with the tab key
  327. bool isTabStop() const
  328. {
  329. return IsTabStop;
  330. }
  331. //! Sets the priority of focus when using the tab key to navigate between a group of elements.
  332. /** See setTabGroup, isTabGroup and getTabGroup for information on tab groups.
  333. Elements with a lower number are focused first */
  334. void setTabOrder(s32 index)
  335. {
  336. // negative = autonumber
  337. if (index < 0) {
  338. TabOrder = 0;
  339. IGUIElement *el = getTabGroup();
  340. while (IsTabGroup && el && el->Parent)
  341. el = el->Parent;
  342. IGUIElement *first = 0, *closest = 0;
  343. if (el) {
  344. // find the highest element number
  345. el->getNextElement(-1, true, IsTabGroup, first, closest, true, true);
  346. if (first) {
  347. TabOrder = first->getTabOrder() + 1;
  348. }
  349. }
  350. } else
  351. TabOrder = index;
  352. }
  353. //! Returns the number in the tab order sequence
  354. s32 getTabOrder() const
  355. {
  356. return TabOrder;
  357. }
  358. //! Sets whether this element is a container for a group of elements which can be navigated using the tab key.
  359. /** For example, windows are tab groups.
  360. Groups can be navigated using ctrl+tab, providing isTabStop is true. */
  361. void setTabGroup(bool isGroup)
  362. {
  363. IsTabGroup = isGroup;
  364. }
  365. //! Returns true if this element is a tab group.
  366. bool isTabGroup() const
  367. {
  368. return IsTabGroup;
  369. }
  370. //! Returns the container element which holds all elements in this element's tab group.
  371. IGUIElement *getTabGroup()
  372. {
  373. IGUIElement *ret = this;
  374. while (ret && !ret->isTabGroup())
  375. ret = ret->getParent();
  376. return ret;
  377. }
  378. //! Returns true if element is enabled
  379. /** Currently elements do _not_ care about parent-states.
  380. So if you want to affect children you have to enable/disable them all.
  381. The only exception to this are sub-elements which also check their parent.
  382. */
  383. virtual bool isEnabled() const
  384. {
  385. if (isSubElement() && IsEnabled && getParent())
  386. return getParent()->isEnabled();
  387. return IsEnabled;
  388. }
  389. //! Sets the enabled state of this element.
  390. virtual void setEnabled(bool enabled)
  391. {
  392. IsEnabled = enabled;
  393. }
  394. //! Sets the new caption of this element.
  395. virtual void setText(const wchar_t *text)
  396. {
  397. Text = text;
  398. }
  399. //! Returns caption of this element.
  400. virtual const wchar_t *getText() const
  401. {
  402. return Text.c_str();
  403. }
  404. //! Sets the new caption of this element.
  405. virtual void setToolTipText(const wchar_t *text)
  406. {
  407. ToolTipText = text;
  408. }
  409. //! Returns caption of this element.
  410. virtual const core::stringw &getToolTipText() const
  411. {
  412. return ToolTipText;
  413. }
  414. //! Returns id. Can be used to identify the element.
  415. virtual s32 getID() const
  416. {
  417. return ID;
  418. }
  419. //! Sets the id of this element
  420. virtual void setID(s32 id)
  421. {
  422. ID = id;
  423. }
  424. //! Called if an event happened.
  425. bool OnEvent(const SEvent &event) override
  426. {
  427. return Parent ? Parent->OnEvent(event) : false;
  428. }
  429. //! Brings a child to front
  430. /** \return True if successful, false if not. */
  431. virtual bool bringToFront(IGUIElement *child)
  432. {
  433. if (child->Parent != this)
  434. return false;
  435. if (std::next(child->ParentPos) == Children.end()) // already there
  436. return true;
  437. Children.erase(child->ParentPos);
  438. child->ParentPos = Children.insert(Children.end(), child);
  439. return true;
  440. }
  441. //! Moves a child to the back, so it's siblings are drawn on top of it
  442. /** \return True if successful, false if not. */
  443. virtual bool sendToBack(IGUIElement *child)
  444. {
  445. if (child->Parent != this)
  446. return false;
  447. if (child->ParentPos == Children.begin()) // already there
  448. return true;
  449. Children.erase(child->ParentPos);
  450. child->ParentPos = Children.insert(Children.begin(), child);
  451. return true;
  452. }
  453. //! Returns list with children of this element
  454. virtual const std::list<IGUIElement *> &getChildren() const
  455. {
  456. return Children;
  457. }
  458. //! Finds the first element with the given id.
  459. /** \param id: Id to search for.
  460. \param searchchildren: Set this to true, if also children of this
  461. element may contain the element with the searched id and they
  462. should be searched too.
  463. \return Returns the first element with the given id. If no element
  464. with this id was found, 0 is returned. */
  465. virtual IGUIElement *getElementFromId(s32 id, bool searchchildren = false) const
  466. {
  467. IGUIElement *e = 0;
  468. for (auto child : Children) {
  469. if (child->getID() == id)
  470. return child;
  471. if (searchchildren)
  472. e = child->getElementFromId(id, true);
  473. if (e)
  474. return e;
  475. }
  476. return e;
  477. }
  478. //! returns true if the given element is a child of this one.
  479. //! \param child: The child element to check
  480. bool isMyChild(IGUIElement *child) const
  481. {
  482. if (!child)
  483. return false;
  484. do {
  485. if (child->Parent)
  486. child = child->Parent;
  487. } while (child->Parent && child != this);
  488. return child == this;
  489. }
  490. //! searches elements to find the closest next element to tab to
  491. /** \param startOrder: The TabOrder of the current element, -1 if none
  492. \param reverse: true if searching for a lower number
  493. \param group: true if searching for a higher one
  494. \param first: element with the highest/lowest known tab order depending on search direction
  495. \param closest: the closest match, depending on tab order and direction
  496. \param includeInvisible: includes invisible elements in the search (default=false)
  497. \param includeDisabled: includes disabled elements in the search (default=false)
  498. \return true if successfully found an element, false to continue searching/fail */
  499. bool getNextElement(s32 startOrder, bool reverse, bool group,
  500. IGUIElement *&first, IGUIElement *&closest, bool includeInvisible = false,
  501. bool includeDisabled = false) const
  502. {
  503. // we'll stop searching if we find this number
  504. s32 wanted = startOrder + (reverse ? -1 : 1);
  505. if (wanted == -2)
  506. wanted = 1073741824; // maximum s32
  507. auto it = Children.begin();
  508. s32 closestOrder, currentOrder;
  509. while (it != Children.end()) {
  510. // ignore invisible elements and their children
  511. if (((*it)->isVisible() || includeInvisible) &&
  512. (group == true || (*it)->isTabGroup() == false)) {
  513. // ignore disabled, but children are checked (disabled is currently per element ignoring parent states)
  514. if ((*it)->isEnabled() || includeDisabled) {
  515. // only check tab stops and those with the same group status
  516. if ((*it)->isTabStop() && ((*it)->isTabGroup() == group)) {
  517. currentOrder = (*it)->getTabOrder();
  518. // is this what we're looking for?
  519. if (currentOrder == wanted) {
  520. closest = *it;
  521. return true;
  522. }
  523. // is it closer than the current closest?
  524. if (closest) {
  525. closestOrder = closest->getTabOrder();
  526. if ((reverse && currentOrder > closestOrder && currentOrder < startOrder) || (!reverse && currentOrder < closestOrder && currentOrder > startOrder)) {
  527. closest = *it;
  528. }
  529. } else if ((reverse && currentOrder < startOrder) || (!reverse && currentOrder > startOrder)) {
  530. closest = *it;
  531. }
  532. // is it before the current first?
  533. if (first) {
  534. closestOrder = first->getTabOrder();
  535. if ((reverse && closestOrder < currentOrder) || (!reverse && closestOrder > currentOrder)) {
  536. first = *it;
  537. }
  538. } else {
  539. first = *it;
  540. }
  541. }
  542. }
  543. // search within children
  544. if ((*it)->getNextElement(startOrder, reverse, group, first, closest, includeInvisible, includeDisabled)) {
  545. return true;
  546. }
  547. }
  548. ++it;
  549. }
  550. return false;
  551. }
  552. //! Returns the type of the gui element.
  553. /** This is needed for the .NET wrapper but will be used
  554. later for serializing and deserializing.
  555. If you wrote your own GUIElements, you need to set the type for your element as first parameter
  556. in the constructor of IGUIElement. For own (=unknown) elements, simply use EGUIET_ELEMENT as type */
  557. EGUI_ELEMENT_TYPE getType() const
  558. {
  559. return Type;
  560. }
  561. //! Returns true if the gui element supports the given type.
  562. /** This is mostly used to check if you can cast a gui element to the class that goes with the type.
  563. Most gui elements will only support their own type, but if you derive your own classes from interfaces
  564. you can overload this function and add a check for the type of the base-class additionally.
  565. This allows for checks comparable to the dynamic_cast of c++ with enabled rtti.
  566. Note that you can't do that by calling BaseClass::hasType(type), but you have to do an explicit
  567. comparison check, because otherwise the base class usually just checks for the member variable
  568. Type which contains the type of your derived class.
  569. */
  570. virtual bool hasType(EGUI_ELEMENT_TYPE type) const
  571. {
  572. return type == Type;
  573. }
  574. //! Returns the type name of the gui element.
  575. /** This is needed serializing elements. */
  576. virtual const c8 *getTypeName() const
  577. {
  578. return GUIElementTypeNames[Type];
  579. }
  580. //! Returns the name of the element.
  581. /** \return Name as character string. */
  582. virtual const c8 *getName() const
  583. {
  584. return Name.c_str();
  585. }
  586. //! Sets the name of the element.
  587. /** \param name New name of the gui element. */
  588. virtual void setName(const c8 *name)
  589. {
  590. Name = name;
  591. }
  592. //! Sets the name of the element.
  593. /** \param name New name of the gui element. */
  594. virtual void setName(const core::stringc &name)
  595. {
  596. Name = name;
  597. }
  598. //! Returns whether the element takes input from the IME
  599. virtual bool acceptsIME()
  600. {
  601. return false;
  602. }
  603. protected:
  604. // not virtual because needed in constructor
  605. void addChildToEnd(IGUIElement *child)
  606. {
  607. if (child) {
  608. child->grab(); // prevent destruction when removed
  609. child->remove(); // remove from old parent
  610. child->LastParentRect = getAbsolutePosition();
  611. child->Parent = this;
  612. child->ParentPos = Children.insert(Children.end(), child);
  613. }
  614. }
  615. #ifndef NDEBUG
  616. template <typename Iterator>
  617. static size_t _fastSetChecksum(Iterator begin, Iterator end)
  618. {
  619. std::hash<typename Iterator::value_type> hasher;
  620. size_t checksum = 0;
  621. for (Iterator it = begin; it != end; ++it) {
  622. size_t h = hasher(*it);
  623. checksum ^= 966073049 + (h * 3432918353) + ((h >> 16) * 461845907);
  624. }
  625. return checksum;
  626. }
  627. #endif
  628. // Reorder children [from, to) to the order given by `neworder`
  629. void reorderChildren(
  630. std::list<IGUIElement *>::iterator from,
  631. std::list<IGUIElement *>::iterator to,
  632. const std::vector<IGUIElement *> &neworder)
  633. {
  634. assert(_fastSetChecksum(from, to) == _fastSetChecksum(neworder.begin(), neworder.end()));
  635. for (auto e : neworder) {
  636. *from = e;
  637. e->ParentPos = from;
  638. ++from;
  639. }
  640. assert(from == to);
  641. }
  642. // not virtual because needed in constructor
  643. void recalculateAbsolutePosition(bool recursive)
  644. {
  645. core::rect<s32> parentAbsolute(0, 0, 0, 0);
  646. core::rect<s32> parentAbsoluteClip;
  647. f32 fw = 0.f, fh = 0.f;
  648. if (Parent) {
  649. parentAbsolute = Parent->AbsoluteRect;
  650. if (NoClip) {
  651. IGUIElement *p = this;
  652. while (p->Parent)
  653. p = p->Parent;
  654. parentAbsoluteClip = p->AbsoluteClippingRect;
  655. } else
  656. parentAbsoluteClip = Parent->AbsoluteClippingRect;
  657. }
  658. const s32 diffx = parentAbsolute.getWidth() - LastParentRect.getWidth();
  659. const s32 diffy = parentAbsolute.getHeight() - LastParentRect.getHeight();
  660. if (AlignLeft == EGUIA_SCALE || AlignRight == EGUIA_SCALE)
  661. fw = (f32)parentAbsolute.getWidth();
  662. if (AlignTop == EGUIA_SCALE || AlignBottom == EGUIA_SCALE)
  663. fh = (f32)parentAbsolute.getHeight();
  664. switch (AlignLeft) {
  665. case EGUIA_UPPERLEFT:
  666. break;
  667. case EGUIA_LOWERRIGHT:
  668. DesiredRect.UpperLeftCorner.X += diffx;
  669. break;
  670. case EGUIA_CENTER:
  671. DesiredRect.UpperLeftCorner.X += diffx / 2;
  672. break;
  673. case EGUIA_SCALE:
  674. DesiredRect.UpperLeftCorner.X = core::round32(ScaleRect.UpperLeftCorner.X * fw);
  675. break;
  676. }
  677. switch (AlignRight) {
  678. case EGUIA_UPPERLEFT:
  679. break;
  680. case EGUIA_LOWERRIGHT:
  681. DesiredRect.LowerRightCorner.X += diffx;
  682. break;
  683. case EGUIA_CENTER:
  684. DesiredRect.LowerRightCorner.X += diffx / 2;
  685. break;
  686. case EGUIA_SCALE:
  687. DesiredRect.LowerRightCorner.X = core::round32(ScaleRect.LowerRightCorner.X * fw);
  688. break;
  689. }
  690. switch (AlignTop) {
  691. case EGUIA_UPPERLEFT:
  692. break;
  693. case EGUIA_LOWERRIGHT:
  694. DesiredRect.UpperLeftCorner.Y += diffy;
  695. break;
  696. case EGUIA_CENTER:
  697. DesiredRect.UpperLeftCorner.Y += diffy / 2;
  698. break;
  699. case EGUIA_SCALE:
  700. DesiredRect.UpperLeftCorner.Y = core::round32(ScaleRect.UpperLeftCorner.Y * fh);
  701. break;
  702. }
  703. switch (AlignBottom) {
  704. case EGUIA_UPPERLEFT:
  705. break;
  706. case EGUIA_LOWERRIGHT:
  707. DesiredRect.LowerRightCorner.Y += diffy;
  708. break;
  709. case EGUIA_CENTER:
  710. DesiredRect.LowerRightCorner.Y += diffy / 2;
  711. break;
  712. case EGUIA_SCALE:
  713. DesiredRect.LowerRightCorner.Y = core::round32(ScaleRect.LowerRightCorner.Y * fh);
  714. break;
  715. }
  716. RelativeRect = DesiredRect;
  717. const s32 w = RelativeRect.getWidth();
  718. const s32 h = RelativeRect.getHeight();
  719. // make sure the desired rectangle is allowed
  720. if (w < (s32)MinSize.Width)
  721. RelativeRect.LowerRightCorner.X = RelativeRect.UpperLeftCorner.X + MinSize.Width;
  722. if (h < (s32)MinSize.Height)
  723. RelativeRect.LowerRightCorner.Y = RelativeRect.UpperLeftCorner.Y + MinSize.Height;
  724. if (MaxSize.Width && w > (s32)MaxSize.Width)
  725. RelativeRect.LowerRightCorner.X = RelativeRect.UpperLeftCorner.X + MaxSize.Width;
  726. if (MaxSize.Height && h > (s32)MaxSize.Height)
  727. RelativeRect.LowerRightCorner.Y = RelativeRect.UpperLeftCorner.Y + MaxSize.Height;
  728. RelativeRect.repair();
  729. AbsoluteRect = RelativeRect + parentAbsolute.UpperLeftCorner;
  730. if (!Parent)
  731. parentAbsoluteClip = AbsoluteRect;
  732. AbsoluteClippingRect = AbsoluteRect;
  733. AbsoluteClippingRect.clipAgainst(parentAbsoluteClip);
  734. LastParentRect = parentAbsolute;
  735. if (recursive) {
  736. // update all children
  737. for (auto child : Children) {
  738. child->recalculateAbsolutePosition(recursive);
  739. }
  740. }
  741. }
  742. protected:
  743. //! List of all children of this element
  744. std::list<IGUIElement *> Children;
  745. //! Pointer to the parent
  746. IGUIElement *Parent;
  747. //! Our position in the parent list. Only valid when Parent != nullptr
  748. std::list<IGUIElement *>::iterator ParentPos;
  749. //! relative rect of element
  750. core::rect<s32> RelativeRect;
  751. //! absolute rect of element
  752. core::rect<s32> AbsoluteRect;
  753. //! absolute clipping rect of element
  754. core::rect<s32> AbsoluteClippingRect;
  755. //! the rectangle the element would prefer to be,
  756. //! if it was not constrained by parent or max/min size
  757. core::rect<s32> DesiredRect;
  758. //! for calculating the difference when resizing parent
  759. core::rect<s32> LastParentRect;
  760. //! relative scale of the element inside its parent
  761. core::rect<f32> ScaleRect;
  762. //! maximum and minimum size of the element
  763. core::dimension2du MaxSize, MinSize;
  764. //! is visible?
  765. bool IsVisible;
  766. //! is enabled?
  767. bool IsEnabled;
  768. //! is a part of a larger whole and should not be serialized?
  769. bool IsSubElement;
  770. //! does this element ignore its parent's clipping rectangle?
  771. bool NoClip;
  772. //! caption
  773. core::stringw Text;
  774. //! tooltip
  775. core::stringw ToolTipText;
  776. //! users can set this for identifying the element by string
  777. core::stringc Name;
  778. //! users can set this for identifying the element by integer
  779. s32 ID;
  780. //! tab stop like in windows
  781. bool IsTabStop;
  782. //! tab order
  783. s32 TabOrder;
  784. //! tab groups are containers like windows, use ctrl+tab to navigate
  785. bool IsTabGroup;
  786. //! tells the element how to act when its parent is resized
  787. EGUI_ALIGNMENT AlignLeft, AlignRight, AlignTop, AlignBottom;
  788. //! GUI Environment
  789. IGUIEnvironment *Environment;
  790. //! type of element
  791. EGUI_ELEMENT_TYPE Type;
  792. };
  793. } // end namespace gui
  794. } // end namespace irr