UIRect.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "Component.h"
  24. namespace Urho3D
  25. {
  26. class UIRoot;
  27. /// Layout mode.
  28. enum UILayoutMode
  29. {
  30. UILM_FREE = 0,
  31. UILM_ANCHOR,
  32. };
  33. /// X anchor pivot.
  34. enum UIXAnchorPivot
  35. {
  36. AP_XCENTER = 0,
  37. AP_LEFT,
  38. AP_RIGHT,
  39. };
  40. /// Y anchor pivot.
  41. enum UIYAnchorPivot
  42. {
  43. AP_YCENTER = 0,
  44. AP_TOP,
  45. AP_BOTTOM,
  46. };
  47. /// UI rectangle component.
  48. class URHO3D_API UIRect : public Component
  49. {
  50. OBJECT(UIRect);
  51. public:
  52. /// Construct.
  53. UIRect(Context* scontext);
  54. /// Destruct.
  55. virtual ~UIRect();
  56. /// Register object factory.
  57. static void RegisterObject(Context* context);
  58. /// Visualize the component as debug geometry.
  59. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  60. /// Set layout mode.
  61. void SetLayoutMode(UILayoutMode layoutMode);
  62. /// Set position (center).
  63. void SetPosition(const Vector2& position);
  64. /// Set size.
  65. void SetSize(const Vector2& size);
  66. /// Set left anchor pivot.
  67. void SetLeftAnchorPivot(UIXAnchorPivot anchorPivot);
  68. /// Set right anchor pivot.
  69. void SetRightAnchorPivot(UIXAnchorPivot anchorPivot);
  70. /// Set top anchor pivot.
  71. void SetTopAnchorPivot(UIYAnchorPivot anchorPivot);
  72. /// Set bottom anchor pivot.
  73. void SetBottomAnchorPivot(UIYAnchorPivot anchorPivot);
  74. /// Set left offset.
  75. void SetLeftOffset(float offset);
  76. /// Set right offset.
  77. void SetRightOffset(float offset);
  78. /// Set top offset.
  79. void SetTopOffset(float offset);
  80. /// Set bottom offset.
  81. void SetBottomOffset(float offset);
  82. /// Return layout mode.
  83. UILayoutMode GetLayoutMode() const { return layoutMode_; }
  84. /// Return position (center).
  85. const Vector2& GetPosition() const { return position_; }
  86. /// Return size.
  87. const Vector2& GetSize() const { return size_; }
  88. /// Return left anchor pivot.
  89. UIXAnchorPivot GetLeftAnchorPivot() const { return leftAnchorPivot_; }
  90. /// Return right anchor pivot
  91. UIXAnchorPivot GetRightAnchorPivot() const { return rightAnchorPivot_; }
  92. /// Return top anchor pivot
  93. UIYAnchorPivot GetTopAnchorPivot() const { return topAnchorPivot_; }
  94. /// Return bottom anchor pivot.
  95. UIYAnchorPivot GetBottomAnchorPivot() const { return bottomAnchorPivot_; }
  96. /// Return left offset.
  97. float GetLeftOffset() const { return leftOffset_; }
  98. /// Return right offset.
  99. float GetRightOffset() const { return rightOffset_; }
  100. /// Return top offset.
  101. float GetTopOffset() const { return topOffset_; }
  102. /// Return bottom offset.
  103. float GetBottomOffset() const { return bottomOffset_; }
  104. /// Return rectangle.
  105. const Rect& GetRect() const;
  106. /// Get x.
  107. float GetX() const { return GetRect().Center().x_; }
  108. /// Get y.
  109. float GetY() const { return GetRect().Center().y_; }
  110. /// Get center.
  111. Vector2 GetCenter() const { return GetRect().Center(); }
  112. /// Return left.
  113. float GetLeft() const { return GetRect().min_.x_; }
  114. /// Return right.
  115. float GetRight() const { return GetRect().max_.x_; }
  116. /// Return top.
  117. float GetTop() const { return GetRect().max_.y_; }
  118. /// Return bottom.
  119. float GetBottom() const { return GetRect().min_.y_; }
  120. /// Return width.
  121. float GetWidth() const { return GetRect().max_.x_ - GetRect().min_.x_; }
  122. /// Return height.
  123. float GetHeight() const { return GetRect().max_.y_ - GetRect().min_.y_; }
  124. protected:
  125. /// Handle node being assigned.
  126. virtual void OnNodeSet(Node* node);
  127. /// Handle node transform being dirtied.
  128. virtual void OnMarkedDirty(Node* node);
  129. /// Mark rectangle dirty.
  130. void MarkRectDirty();
  131. /// Mark children rectangle dirty.
  132. void MarkChildrenRectDirty();
  133. /// Calculate rectangle free mode.
  134. Rect CalculateRectFreeMode() const;
  135. /// Calculate free mode parameters.
  136. void CalculateFreeModeParameters(const Rect& rect);
  137. /// Calculate rectangle anchor mode.
  138. Rect CalculateRectAnchorMode() const;
  139. /// Calculate anchor offsets from rectangle.
  140. void CalculateAnchorModeParameters(const Rect& rect);
  141. /// Draw rectangle.
  142. void DrawRect(DebugRenderer* debug, bool depthTest);
  143. /// UIRoot.
  144. WeakPtr<UIRoot> root_;
  145. /// Parent.
  146. WeakPtr<UIRect> parent_;
  147. /// Layout mode.
  148. UILayoutMode layoutMode_;
  149. /// Position.
  150. Vector2 position_;
  151. /// Size.
  152. Vector2 size_;
  153. /// Left anchor pivot.
  154. UIXAnchorPivot leftAnchorPivot_;
  155. /// Right anchor pivot.
  156. UIXAnchorPivot rightAnchorPivot_;
  157. /// Top anchor pivot.
  158. UIYAnchorPivot topAnchorPivot_;
  159. /// Bottom anchor pivot.
  160. UIYAnchorPivot bottomAnchorPivot_;
  161. /// Left offset.
  162. float leftOffset_;
  163. /// Right offset.
  164. float rightOffset_;
  165. /// Top offset.
  166. float topOffset_;
  167. /// Bottom offset.
  168. float bottomOffset_;
  169. /// Handle node dirty.
  170. bool handleNodeDirty_;
  171. /// Rectangle.
  172. mutable Rect rectangle_;
  173. /// Rectangle dirty flag.
  174. mutable bool rectangleDirty_;
  175. };
  176. }