IGUIElement.h 29 KB

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