2
0

IGUIElement.h 25 KB

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