UIElement.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Animatable.h"
  24. #include "UIBatch.h"
  25. #include "Vector2.h"
  26. #include "XMLFile.h"
  27. namespace Urho3D
  28. {
  29. const Color DEBUG_DRAW_COLOR(Color::BLUE);
  30. /// %UI element horizontal alignment.
  31. enum HorizontalAlignment
  32. {
  33. HA_LEFT = 0,
  34. HA_CENTER,
  35. HA_RIGHT
  36. };
  37. /// %UI element vertical alignment.
  38. enum VerticalAlignment
  39. {
  40. VA_TOP = 0,
  41. VA_CENTER,
  42. VA_BOTTOM
  43. };
  44. /// %UI element corners.
  45. enum Corner
  46. {
  47. C_TOPLEFT = 0,
  48. C_TOPRIGHT,
  49. C_BOTTOMLEFT,
  50. C_BOTTOMRIGHT,
  51. MAX_UIELEMENT_CORNERS
  52. };
  53. /// %UI element orientation.
  54. enum Orientation
  55. {
  56. O_HORIZONTAL = 0,
  57. O_VERTICAL
  58. };
  59. /// %UI element focus mode.
  60. enum FocusMode
  61. {
  62. /// Is not focusable and does not affect existing focus.
  63. FM_NOTFOCUSABLE = 0,
  64. /// Resets focus when clicked.
  65. FM_RESETFOCUS,
  66. /// Is focusable.
  67. FM_FOCUSABLE,
  68. /// Is focusable and also defocusable by pressing ESC.
  69. FM_FOCUSABLE_DEFOCUSABLE
  70. };
  71. /// Layout operation mode.
  72. enum LayoutMode
  73. {
  74. /// No layout operations will be performed.
  75. LM_FREE = 0,
  76. /// Layout child elements horizontally and resize them to fit. Resize element if necessary.
  77. LM_HORIZONTAL,
  78. /// Layout child elements vertically and resize them to fit. Resize element if necessary.
  79. LM_VERTICAL
  80. };
  81. /// Traversal mode for rendering.
  82. enum TraversalMode
  83. {
  84. /// Traverse thru children having same priority first and recurse into their children before traversing children having higher priority.
  85. TM_BREADTH_FIRST = 0,
  86. /// Traverse thru each child and its children immediately after in sequence.
  87. TM_DEPTH_FIRST
  88. };
  89. /// Drag and drop disabled.
  90. static const unsigned DD_DISABLED = 0x0;
  91. /// Drag and drop source flag.
  92. static const unsigned DD_SOURCE = 0x1;
  93. /// Drag and drop target flag.
  94. static const unsigned DD_TARGET = 0x2;
  95. /// Drag and drop source and target.
  96. static const unsigned DD_SOURCE_AND_TARGET = 0x3;
  97. class Cursor;
  98. class ResourceCache;
  99. /// Base class for %UI elements.
  100. class URHO3D_API UIElement : public Animatable
  101. {
  102. OBJECT(UIElement);
  103. BASEOBJECT(UIElement);
  104. public:
  105. /// Construct.
  106. UIElement(Context* context);
  107. /// Destruct.
  108. virtual ~UIElement();
  109. /// Register object factory.
  110. static void RegisterObject(Context* context);
  111. /// Apply attribute changes that can not be applied immediately.
  112. virtual void ApplyAttributes();
  113. /// Load from XML data. Return true if successful.
  114. virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
  115. /// Load from XML data with style. Return true if successful.
  116. virtual bool LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault = false);
  117. /// Create a child by loading from XML data with style. Return true if successful.
  118. virtual bool LoadChildXML(const XMLElement& childElem, XMLFile* styleFile = 0, bool setInstanceDefault = false);
  119. /// Save as XML data. Return true if successful.
  120. virtual bool SaveXML(XMLElement& dest) const;
  121. /// Perform UI element update.
  122. virtual void Update(float timeStep);
  123. /// Return whether is visible and inside a scissor rectangle and should be rendered.
  124. virtual bool IsWithinScissor(const IntRect& currentScissor);
  125. /// Update and return screen position.
  126. virtual const IntVector2& GetScreenPosition() const;
  127. /// Return UI rendering batches.
  128. virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  129. /// Return UI rendering batches for debug draw.
  130. virtual void GetDebugDrawBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  131. /// React to mouse hover.
  132. virtual void OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  133. /// React to mouse click begin.
  134. virtual void OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor);
  135. /// React to mouse click end.
  136. virtual void OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement);
  137. /// React to double mouse click.
  138. virtual void OnDoubleClick(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor);
  139. /// React to mouse drag begin.
  140. virtual void OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  141. /// React to mouse drag motion.
  142. virtual void OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons, int qualifiers, Cursor* cursor);
  143. /// React to mouse drag end.
  144. virtual void OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, int dragButtons, int releaseButton, Cursor* cursor);
  145. /// React to a mouse drag cancel event (ie, when an extra button is pressed)
  146. virtual void OnDragCancel(const IntVector2& position, const IntVector2& screenPosition, int dragButtons, int cancelButton, Cursor* cursor);
  147. /// React to drag and drop test. Return true to signal that the drop is acceptable.
  148. virtual bool OnDragDropTest(UIElement* source);
  149. /// React to drag and drop finish. Return true to signal that the drop was accepted.
  150. virtual bool OnDragDropFinish(UIElement* source);
  151. /// React to mouse wheel.
  152. virtual void OnWheel(int delta, int buttons, int qualifiers);
  153. /// React to a key press.
  154. virtual void OnKey(int key, int buttons, int qualifiers);
  155. /// React to text input event.
  156. virtual void OnTextInput(const String& text, int buttons, int qualifiers);
  157. /// React to resize.
  158. virtual void OnResize() {}
  159. /// React to position change.
  160. virtual void OnPositionSet() {}
  161. /// React to editable status change.
  162. virtual void OnSetEditable() {}
  163. /// React to indent change.
  164. virtual void OnIndentSet() {}
  165. /// Load from an XML file. Return true if successful.
  166. bool LoadXML(Deserializer& source);
  167. /// Save to an XML file. Return true if successful.
  168. bool SaveXML(Serializer& dest) const;
  169. /// Filter attributes in serialization process.
  170. bool FilterAttributes(XMLElement& dest) const;
  171. /// Set name.
  172. void SetName(const String& name);
  173. /// Set position.
  174. void SetPosition(const IntVector2& position);
  175. /// Set position.
  176. void SetPosition(int x, int y);
  177. /// Set size.
  178. void SetSize(const IntVector2& size);
  179. /// Set size.
  180. void SetSize(int width, int height);
  181. /// Set width only.
  182. void SetWidth(int width);
  183. /// Set height only.
  184. void SetHeight(int height);
  185. /// Set minimum size.
  186. void SetMinSize(const IntVector2& minSize);
  187. /// Set minimum size.
  188. void SetMinSize(int width, int height);
  189. /// Set minimum width.
  190. void SetMinWidth(int width);
  191. /// Set minimum height.
  192. void SetMinHeight(int height);
  193. /// Set maximum size.
  194. void SetMaxSize(const IntVector2& maxSize);
  195. /// Set maximum size.
  196. void SetMaxSize(int width, int height);
  197. /// Set maximum width.
  198. void SetMaxWidth(int width);
  199. /// Set maximum height.
  200. void SetMaxHeight(int height);
  201. /// Set fixed size.
  202. void SetFixedSize(const IntVector2& size);
  203. /// Set fixed size.
  204. void SetFixedSize(int width, int height);
  205. /// Set fixed width.
  206. void SetFixedWidth(int width);
  207. /// Set fixed height.
  208. void SetFixedHeight(int height);
  209. /// Set horizontal and vertical alignment.
  210. void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign);
  211. /// Set horizontal alignment.
  212. void SetHorizontalAlignment(HorizontalAlignment align);
  213. /// Set vertical alignment.
  214. void SetVerticalAlignment(VerticalAlignment align);
  215. /// Set child element clipping border.
  216. void SetClipBorder(const IntRect& rect);
  217. /// Set color on all corners.
  218. void SetColor(const Color& color);
  219. /// Set color on one corner.
  220. void SetColor(Corner corner, const Color& color);
  221. /// Set priority.
  222. void SetPriority(int priority);
  223. /// Set opacity.
  224. void SetOpacity(float opacity);
  225. /// Set whether should be brought to front when focused.
  226. void SetBringToFront(bool enable);
  227. /// Set whether should be put to background when another element is focused.
  228. void SetBringToBack(bool enable);
  229. /// Set whether should clip child elements. Default false.
  230. void SetClipChildren(bool enable);
  231. /// Set whether should sort child elements according to priority. Default true.
  232. void SetSortChildren(bool enable);
  233. /// Set whether parent elements' opacity affects opacity. Default true.
  234. void SetUseDerivedOpacity(bool enable);
  235. /// Set whether reacts to input. Default false, but is enabled by subclasses if applicable.
  236. void SetEnabled(bool enable);
  237. /// Set enabled state on self and child elements. Elements' own enabled state is remembered (IsEnabledSelf) and can be restored.
  238. void SetDeepEnabled(bool enable);
  239. /// Reset enabled state to the element's remembered state prior to calling SetDeepEnabled.
  240. void ResetDeepEnabled();
  241. /// Set enabled state on self and child elements. Unlike SetDeepEnabled this does not remember the elements' own enabled state, but overwrites it.
  242. void SetEnabledRecursive(bool enable);
  243. /// Set whether value is editable through input. Not applicable to all elements. Default true.
  244. void SetEditable(bool enable);
  245. /// Set whether is focused. Only one element can be focused at a time.
  246. void SetFocus(bool enable);
  247. /// Set selected mode. Actual meaning is element dependent, for example constant hover or pressed effect.
  248. void SetSelected(bool enable);
  249. /// Set whether is visible.
  250. void SetVisible(bool enable);
  251. /// Set focus mode.
  252. void SetFocusMode(FocusMode mode);
  253. /// Set drag and drop flags.
  254. void SetDragDropMode(unsigned mode);
  255. /// Set style from an XML file. Find the style element by name. If the style file is not explicitly provided, use the default style from parental chain. Return true if the style is applied successfully.
  256. bool SetStyle(const String& styleName, XMLFile* file = 0);
  257. /// Set style from an XML element. Return true if the style is applied successfully.
  258. bool SetStyle(const XMLElement& element);
  259. /// Set style from an XML file. Find the style element automatically. If the style file is not explicitly provided, use the default style from parental chain. Return true if the style is applied successfully.
  260. bool SetStyleAuto(XMLFile* file = 0);
  261. /// Set default style file for later use by children elements.
  262. void SetDefaultStyle(XMLFile* style);
  263. /// Set layout.
  264. void SetLayout(LayoutMode mode, int spacing = 0, const IntRect& border = IntRect::ZERO);
  265. /// Set layout mode only.
  266. void SetLayoutMode(LayoutMode mode);
  267. /// Set layout spacing.
  268. void SetLayoutSpacing(int spacing);
  269. /// Set layout border.
  270. void SetLayoutBorder(const IntRect& border);
  271. /// Set layout flex scale.
  272. void SetLayoutFlexScale(const Vector2& scale);
  273. /// Set horizontal indentation.
  274. void SetIndent(int indent);
  275. /// Set indent spacing (number of pixels per indentation level).
  276. void SetIndentSpacing(int indentSpacing);
  277. /// Manually update layout. Should not be necessary in most cases, but is provided for completeness.
  278. void UpdateLayout();
  279. /// Disable automatic layout update. Should only be used if there are performance problems.
  280. void DisableLayoutUpdate();
  281. /// Enable automatic layout update.
  282. void EnableLayoutUpdate();
  283. /// Bring UI element to front.
  284. void BringToFront();
  285. /// Create and add a child element and return it.
  286. UIElement* CreateChild(StringHash type, const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
  287. /// Add a child element.
  288. void AddChild(UIElement* element);
  289. /// Insert a child element into a specific position in the child list.
  290. void InsertChild(unsigned index, UIElement* element);
  291. /// Remove a child element. Starting search at specified index if provided.
  292. void RemoveChild(UIElement* element, unsigned index = 0);
  293. /// Remove a child element at index.
  294. void RemoveChildAtIndex(unsigned index);
  295. /// Remove all child elements.
  296. void RemoveAllChildren();
  297. /// Remove from the parent element. If no other shared pointer references exist, causes immediate deletion.
  298. void Remove();
  299. /// Find child index. Return M_MAX_UNSIGNED if not found.
  300. unsigned FindChild(UIElement* element) const;
  301. /// Set parent element. Same as parent->InsertChild(index, this).
  302. void SetParent(UIElement* parent, unsigned index = M_MAX_UNSIGNED);
  303. /// Set a user variable.
  304. void SetVar(StringHash key, const Variant& value);
  305. /// Mark as internally (programmatically) created. Used when an element composes itself out of child elements.
  306. void SetInternal(bool enable);
  307. /// Set traversal mode for rendering. The default traversal mode is TM_BREADTH_FIRST for non-root element. Root element should be set to TM_DEPTH_FIRST to avoid artifacts during rendering.
  308. void SetTraversalMode(TraversalMode traversalMode);
  309. /// Set element event sender flag. When child element is added or deleted, the event would be sent using UIElement found in the parental chain having this flag set. If not set, the event is sent using UI's root as per normal.
  310. void SetElementEventSender(bool flag);
  311. /// Template version of creating a child element.
  312. template <class T> T* CreateChild(const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
  313. /// Return name.
  314. const String& GetName() const { return name_; }
  315. /// Return position.
  316. const IntVector2& GetPosition() const { return position_; }
  317. /// Return size.
  318. const IntVector2& GetSize() const { return size_; }
  319. /// Return width.
  320. int GetWidth() const { return size_.x_; }
  321. /// Return height.
  322. int GetHeight() const { return size_.y_; }
  323. /// Return minimum size.
  324. const IntVector2& GetMinSize() const { return minSize_; }
  325. /// Return minimum width.
  326. int GetMinWidth() const { return minSize_.x_; }
  327. /// Return minimum height.
  328. int GetMinHeight() const { return minSize_.y_; }
  329. /// Return maximum size.
  330. const IntVector2& GetMaxSize() const { return maxSize_; }
  331. /// Return minimum width.
  332. int GetMaxWidth() const { return maxSize_.x_; }
  333. /// Return minimum height.
  334. int GetMaxHeight() const { return maxSize_.y_; }
  335. /// Return true if size is fixed.
  336. bool IsFixedSize() const { return minSize_ == maxSize_; }
  337. /// Return true if width is fixed.
  338. bool IsFixedWidth() const { return minSize_.x_ == maxSize_.x_; }
  339. /// Return true if height is fixed.
  340. bool IsFixedHeight() const { return minSize_.y_ == maxSize_.y_; }
  341. /// Return child element offset.
  342. const IntVector2& GetChildOffset() const { return childOffset_; }
  343. /// Return horizontal alignment.
  344. HorizontalAlignment GetHorizontalAlignment() const { return horizontalAlignment_; }
  345. /// Return vertical alignment.
  346. VerticalAlignment GetVerticalAlignment() const { return verticalAlignment_; }
  347. /// Return child element clipping border.
  348. const IntRect& GetClipBorder() const { return clipBorder_; }
  349. /// Return corner color.
  350. const Color& GetColor(Corner corner) const { return color_[corner]; }
  351. /// Return priority.
  352. int GetPriority() const { return priority_; }
  353. /// Return opacity.
  354. float GetOpacity() const { return opacity_; }
  355. /// Return derived opacity (affected by parent elements.) If UseDerivedOpacity is false, returns same as element's own opacity.
  356. float GetDerivedOpacity() const;
  357. /// Return whether should be brought to front when focused.
  358. bool GetBringToFront() const { return bringToFront_; }
  359. /// Return whether should be put to background when another element is focused.
  360. bool GetBringToBack() const { return bringToBack_; }
  361. /// Return whether should clip child elements.
  362. bool GetClipChildren() const { return clipChildren_; }
  363. /// Return whether should sort child elements according to priority.
  364. bool GetSortChildren() const { return sortChildren_; }
  365. /// Return whether parent elements' opacity affects opacity.
  366. bool GetUseDerivedOpacity() const { return useDerivedOpacity_; }
  367. /// Return whether has focus.
  368. bool HasFocus() const;
  369. /// Return whether reacts to input.
  370. bool IsEnabled() const { return enabled_; }
  371. /// Returns the element's last own enabled state. May be different than the value returned by IsEnabled when SetDeepEnabled has been used.
  372. bool IsEnabledSelf() const { return enabledPrev_; }
  373. /// Return whether value is editable through input.
  374. bool IsEditable() const { return editable_; }
  375. /// Return whether is selected. Actual meaning is element dependent.
  376. bool IsSelected() const { return selected_; }
  377. /// Return whether is visible.
  378. bool IsVisible() const { return visible_; }
  379. /// Return whether the cursor is hovering on this element.
  380. bool IsHovering() const { return hovering_; }
  381. /// Return whether is internally created.
  382. bool IsInternal() const { return internal_; }
  383. /// Return whether has different color in at least one corner.
  384. bool HasColorGradient() const { return colorGradient_; }
  385. /// Return focus mode.
  386. FocusMode GetFocusMode() const { return focusMode_; }
  387. /// Return drag and drop flags.
  388. unsigned GetDragDropMode() const { return dragDropMode_; }
  389. /// Return applied style name. Return an empty string when the applied style is an 'auto' style (i.e. style derived from instance's type).
  390. const String& GetAppliedStyle() const;
  391. /// Return default style.
  392. XMLFile* GetDefaultStyle(bool recursiveUp = true) const;
  393. /// Return layout mode.
  394. LayoutMode GetLayoutMode() const { return layoutMode_; }
  395. /// Return layout spacing.
  396. int GetLayoutSpacing() const { return layoutSpacing_; }
  397. /// Return layout border.
  398. const IntRect& GetLayoutBorder() const { return layoutBorder_; }
  399. /// Return layout flex scale.
  400. const Vector2& GetLayoutFlexScale() const { return layoutFlexScale_; }
  401. /// Return number of child elements.
  402. unsigned GetNumChildren(bool recursive = false) const;
  403. /// Return child element by index.
  404. UIElement* GetChild(unsigned index) const;
  405. /// Return child element by name.
  406. UIElement* GetChild(const String& name, bool recursive = false) const;
  407. /// Return child element by variable. If only key is provided, return the first child having the matching variable key. If value is also provided then the actual variable value would also be checked against.
  408. UIElement* GetChild(const StringHash& key, const Variant& value = Variant::EMPTY, bool recursive = false) const;
  409. /// Return immediate child elements.
  410. const Vector<SharedPtr<UIElement> >& GetChildren() const { return children_; }
  411. /// Return child elements either recursively or non-recursively.
  412. void GetChildren(PODVector<UIElement*>& dest, bool recursive = false) const;
  413. /// Return parent element.
  414. UIElement* GetParent() const { return parent_; }
  415. /// Return root element.
  416. UIElement* GetRoot() const;
  417. /// Return derived color. Only valid when no gradient.
  418. const Color& GetDerivedColor() const;
  419. /// Return a user variable.
  420. const Variant& GetVar(const StringHash& key) const;
  421. /// Return all user variables.
  422. const VariantMap& GetVars() const { return vars_; }
  423. /// Return the drag button combo if this element is being dragged.
  424. int GetDragButtonCombo() const { return dragButtonCombo_; }
  425. /// Return the number of buttons dragging this element.
  426. unsigned GetDragButtonCount() const { return dragButtonCount_; }
  427. /// Convert screen coordinates to element coordinates.
  428. IntVector2 ScreenToElement(const IntVector2& screenPosition);
  429. /// Convert element coordinates to screen coordinates.
  430. IntVector2 ElementToScreen(const IntVector2& position);
  431. /// Return whether a point (either in element or screen coordinates) is inside the element.
  432. bool IsInside(IntVector2 position, bool isScreen);
  433. /// Return whether a point (either in element or screen coordinates) is inside the combined rect of the element and its children.
  434. bool IsInsideCombined(IntVector2 position, bool isScreen);
  435. /// Return combined screen coordinate rect of element and its children.
  436. IntRect GetCombinedScreenRect();
  437. /// Sort child elements if sorting enabled and order dirty. Called by UI.
  438. void SortChildren();
  439. /// Return minimum layout element size in the layout direction. Only valid after layout has been calculated. Used internally by UI for optimizations.
  440. int GetLayoutMinSize() const { return layoutMinSize_; }
  441. /// Return maximum layout element size in the layout direction. Only valid after layout has been calculated. Used internally by UI for optimizations.
  442. int GetLayoutMaxSize() const { return layoutMaxSize_; }
  443. /// Return horizontal indentation.
  444. int GetIndent() const { return indent_; }
  445. /// Return indent spacing (number of pixels per indentation level).
  446. int GetIndentSpacing() const { return indentSpacing_; }
  447. /// Return indent width in pixels.
  448. int GetIndentWidth() const { return indent_ * indentSpacing_; }
  449. /// Set child offset.
  450. void SetChildOffset(const IntVector2& offset);
  451. /// Set hovering state.
  452. void SetHovering(bool enable);
  453. /// Adjust scissor for rendering.
  454. void AdjustScissor(IntRect& currentScissor);
  455. /// Get UI rendering batches with a specified offset. Also recurses to child elements.
  456. void GetBatchesWithOffset(IntVector2& offset, PODVector<UIBatch>& batches, PODVector<float>& vertexData, IntRect
  457. currentScissor);
  458. /// Return color attribute. Uses just the top-left color.
  459. const Color& GetColorAttr() const { return color_[0]; }
  460. /// Return traversal mode for rendering.
  461. TraversalMode GetTraversalMode() const { return traversalMode_; }
  462. /// Return whether element should send child added / removed events by itself. If false, defers to parent element.
  463. bool IsElementEventSender() const { return elementEventSender_; }
  464. /// Get element which should send child added / removed events.
  465. UIElement* GetElementEventSender() const;
  466. protected:
  467. /// Handle attribute animation added.
  468. virtual void OnAttributeAnimationAdded();
  469. /// Handle attribute animation removed.
  470. virtual void OnAttributeAnimationRemoved();
  471. /// Set object attribute animation internal.
  472. virtual void SetObjectAttributeAnimation(const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed);
  473. /// Mark screen position as needing an update.
  474. void MarkDirty();
  475. /// Remove child XML element by matching attribute name.
  476. bool RemoveChildXML(XMLElement& parent, const String& name) const;
  477. /// Remove child XML element by matching attribute name and value.
  478. bool RemoveChildXML(XMLElement& parent, const String& name, const String& value) const;
  479. /// Filter UI-style attributes in serialization process.
  480. bool FilterUIStyleAttributes(XMLElement& dest, const XMLElement& styleElem) const;
  481. /// Filter implicit attributes in serialization process.
  482. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  483. /// Name.
  484. String name_;
  485. /// Child elements.
  486. Vector<SharedPtr<UIElement> > children_;
  487. /// Parent element.
  488. UIElement* parent_;
  489. /// Child element clipping border.
  490. IntRect clipBorder_;
  491. /// Colors.
  492. Color color_[MAX_UIELEMENT_CORNERS];
  493. /// User variables.
  494. VariantMap vars_;
  495. /// Priority.
  496. int priority_;
  497. /// Bring to front when focused flag.
  498. bool bringToFront_;
  499. /// Bring to back when defocused flag.
  500. bool bringToBack_;
  501. /// Clip children flag.
  502. bool clipChildren_;
  503. /// Sort children according to priority flag.
  504. bool sortChildren_;
  505. /// Use derived opacity flag.
  506. bool useDerivedOpacity_;
  507. /// Input enabled flag.
  508. bool enabled_;
  509. /// Last SetEnabled flag before any SetDeepEnabled.
  510. bool enabledPrev_;
  511. /// Value editable flag.
  512. bool editable_;
  513. /// Selected flag.
  514. bool selected_;
  515. /// Visible flag.
  516. bool visible_;
  517. /// Hovering flag.
  518. bool hovering_;
  519. /// Internally created flag.
  520. bool internal_;
  521. /// Focus mode.
  522. FocusMode focusMode_;
  523. /// Drag and drop flags.
  524. unsigned dragDropMode_;
  525. /// Layout mode.
  526. LayoutMode layoutMode_;
  527. /// Layout spacing.
  528. int layoutSpacing_;
  529. /// Layout borders.
  530. IntRect layoutBorder_;
  531. /// Layout flex scale.
  532. Vector2 layoutFlexScale_;
  533. /// Resize nesting level to prevent multiple events and endless loop.
  534. unsigned resizeNestingLevel_;
  535. /// Layout update nesting level to prevent endless loop.
  536. unsigned layoutNestingLevel_;
  537. /// Layout element minimum size in layout direction.
  538. int layoutMinSize_;
  539. /// Layout element maximum size in layout direction.
  540. int layoutMaxSize_;
  541. /// Horizontal indentation.
  542. int indent_;
  543. /// Indent spacing (number of pixels per indentation level).
  544. int indentSpacing_;
  545. /// Position.
  546. IntVector2 position_;
  547. /// Screen position.
  548. mutable IntVector2 screenPosition_;
  549. /// Screen position dirty flag.
  550. mutable bool positionDirty_;
  551. /// Applied style.
  552. String appliedStyle_;
  553. /// Drag button combo.
  554. int dragButtonCombo_;
  555. /// Drag button count.
  556. unsigned dragButtonCount_;
  557. private:
  558. /// Return child elements recursively.
  559. void GetChildrenRecursive(PODVector<UIElement*>& dest) const;
  560. /// Calculate layout width for resizing the parent element.
  561. int CalculateLayoutParentSize(const PODVector<int>& sizes, int begin, int end, int spacing);
  562. /// Calculate child widths/positions in the layout.
  563. void CalculateLayout(PODVector<int>& positions, PODVector<int>& sizes, const PODVector<int>& minSizes, const PODVector<int>& maxSizes, const PODVector<float>& flexScales, int targetWidth, int begin, int end, int spacing);
  564. /// Get child element constant position in a layout.
  565. IntVector2 GetLayoutChildPosition(UIElement* child);
  566. /// Detach from parent.
  567. void Detach();
  568. /// Verify that child elements have proper alignment for layout mode.
  569. void VerifyChildAlignment();
  570. /// Handle logic post-update event.
  571. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  572. /// Size.
  573. IntVector2 size_;
  574. /// Minimum size.
  575. IntVector2 minSize_;
  576. /// Maximum size.
  577. IntVector2 maxSize_;
  578. /// Child elements' offset. Used internally.
  579. IntVector2 childOffset_;
  580. /// Horizontal alignment.
  581. HorizontalAlignment horizontalAlignment_;
  582. /// Vertical alignment.
  583. VerticalAlignment verticalAlignment_;
  584. /// Opacity.
  585. float opacity_;
  586. /// Derived opacity.
  587. mutable float derivedOpacity_;
  588. /// Derived color. Only valid when no gradient.
  589. mutable Color derivedColor_;
  590. /// Derived opacity dirty flag.
  591. mutable bool opacityDirty_;
  592. /// Derived color dirty flag (only used when no gradient.)
  593. mutable bool derivedColorDirty_;
  594. /// Child priority sorting dirty flag.
  595. bool sortOrderDirty_;
  596. /// Has color gradient flag.
  597. bool colorGradient_;
  598. /// Default style file.
  599. SharedPtr<XMLFile> defaultStyle_;
  600. /// Traversal mode for rendering.
  601. TraversalMode traversalMode_;
  602. /// Flag whether node should send child added / removed events by itself.
  603. bool elementEventSender_;
  604. /// XPath query for selecting UI-style.
  605. static XPathQuery styleXPathQuery_;
  606. };
  607. template <class T> T* UIElement::CreateChild(const String& name, unsigned index) { return static_cast<T*>(CreateChild(T::GetTypeStatic(), name, index)); }
  608. }