IGUIElement.h 25 KB

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