2
0

UIElement.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/RTTI.h>
  5. #include <Jolt/Core/Color.h>
  6. #include <UI/UIEventListener.h>
  7. class UIManager;
  8. class UIElement;
  9. class UIAnimation;
  10. using UIElementVector = Array<UIElement *>;
  11. using UIAnimationVector = Array<UIAnimation *>;
  12. /// Base class UI element. Forms a tree of UI elements.
  13. class UIElement : public UIEventListener
  14. {
  15. public:
  16. JPH_DECLARE_RTTI_VIRTUAL_BASE(UIElement)
  17. /// Constructor
  18. UIElement();
  19. virtual ~UIElement() override;
  20. /// Add / remove child elements
  21. void Add(UIElement *inElement);
  22. void Clear();
  23. virtual void OnAdded() { }
  24. /// Start / stop animations
  25. void StartAnimation(UIAnimation *inAnimation);
  26. void StopAnimation(const RTTI *inAnimationType);
  27. /// Cloning / copying
  28. UIElement * Clone() const;
  29. virtual void CopyTo(UIElement *ioElement) const;
  30. /// Units
  31. enum EUnits
  32. {
  33. PIXELS,
  34. PERCENTAGE,
  35. };
  36. /// Alignment
  37. enum EAlignment
  38. {
  39. LEFT,
  40. ONE_THIRD,
  41. CENTER,
  42. RIGHT
  43. };
  44. /// Properties
  45. int GetID() const { return mID; }
  46. void SetID(int inID) { mID = inID; }
  47. int GetX() const { return GetRelativeX() + (mParent != nullptr? mParent->GetX() : 0); }
  48. int GetY() const { return GetRelativeY() + (mParent != nullptr? mParent->GetY() : 0); }
  49. int GetRelativeX() const { return mRelativeX.GetPosition(this, &UIElement::GetWidth); }
  50. void SetRelativeX(int inX, EUnits inUnits = PIXELS, EAlignment inAlignment = LEFT) { mRelativeX.Set(inX, inUnits, inAlignment); }
  51. int GetRelativeY() const { return mRelativeY.GetPosition(this, &UIElement::GetHeight); }
  52. void SetRelativeY(int inY, EUnits inUnits = PIXELS, EAlignment inAlignment = LEFT) { mRelativeY.Set(inY, inUnits, inAlignment); }
  53. int GetWidth() const { return mWidth.GetSize(this, &UIElement::GetWidth); }
  54. void SetWidth(int inWidth, EUnits inUnits = PIXELS) { mWidth.Set(inWidth, inUnits); }
  55. int GetHeight() const { return mHeight.GetSize(this, &UIElement::GetHeight); }
  56. void SetHeight(int inHeight, EUnits inUnits = PIXELS) { mHeight.Set(inHeight, inUnits); }
  57. int GetPaddingRight() const { return mPaddingRight.GetSize(this, &UIElement::GetWidth); }
  58. void SetPaddingRight(int inY, EUnits inUnits = PIXELS) { mPaddingRight.Set(inY, inUnits); }
  59. int GetPaddingBottom() const { return mPaddingBottom.GetSize(this, &UIElement::GetHeight); }
  60. void SetPaddingBottom(int inY, EUnits inUnits = PIXELS) { mPaddingBottom.Set(inY, inUnits); }
  61. void SetVisible(bool inShow) { mIsVisible = inShow; }
  62. bool IsVisible() const { return mIsVisible && mAnimatedIsVisible; }
  63. void SetDisabled(bool inDisabled);
  64. bool IsDisabled() const { return mIsDisabled; }
  65. void SetHighlighted(bool inHighlighted);
  66. bool IsHighlighted() const { return mIsHighlighted; }
  67. void SetSelected(bool inSelected);
  68. bool IsSelected() const { return mIsSelected; }
  69. /// Animation
  70. void SetAnimatedVisible(bool inShow) { mAnimatedIsVisible = inShow; } ///< Visibility flag that can be set by UIAnimations
  71. bool HasActivateAnimation() const { return mHasActivateAnimation; }
  72. bool HasDeactivateAnimation() const { return mHasDeactivateAnimation; }
  73. /// Manager
  74. UIManager * GetManager() const { return mManager; }
  75. /// Parent child linking
  76. UIElement * GetParent() const { return mParent; }
  77. int GetNumChildren() const { return (int)mChildren.size(); }
  78. UIElement * GetChild(int inIdx) const { return mChildren[inIdx]; }
  79. const UIElementVector &GetChildren() const { return mChildren; }
  80. /// Hit testing
  81. bool Contains(int inX, int inY) const;
  82. bool ContainsWidened(int inX, int inY, int inBorder) const;
  83. /// Calculate auto layout
  84. virtual void AutoLayout();
  85. /// Find element by ID
  86. virtual UIElement * FindByID(int inID);
  87. /// Update element
  88. virtual void Update(float inDeltaTime);
  89. /// Draw element
  90. virtual void Draw() const;
  91. /// Actions
  92. virtual bool MouseDown(int inX, int inY);
  93. virtual bool MouseUp(int inX, int inY);
  94. virtual bool MouseMove(int inX, int inY);
  95. virtual void MouseCancel();
  96. /// Event handling (returns true if the event has been handled)
  97. virtual bool HandleUIEvent(EUIEvent inEvent, UIElement *inSender) override;
  98. protected:
  99. /// ID
  100. int mID;
  101. /// Hierarchy
  102. UIElement * mParent;
  103. UIElementVector mChildren;
  104. /// Abstract GetSize function
  105. using fGetSize = int (UIElement::*)() const;
  106. /// Size
  107. class Size
  108. {
  109. public:
  110. /// Constructor
  111. Size() : mSize(0), mUnit(PIXELS) { }
  112. /// Get size
  113. int GetSize(const UIElement *inElement, fGetSize inGetSize) const;
  114. /// Assignment
  115. void Set(int inValue, EUnits inUnits);
  116. private:
  117. int mSize;
  118. EUnits mUnit;
  119. };
  120. /// Position
  121. class Position
  122. {
  123. public:
  124. /// Constructor
  125. Position() : mAlignment(LEFT) { }
  126. /// Get position
  127. int GetPosition(const UIElement *inElement, fGetSize inGetSize) const;
  128. /// Assignment
  129. void Set(int inValue, EUnits inUnits, EAlignment inAlignment);
  130. private:
  131. EAlignment mAlignment;
  132. Size mSize;
  133. };
  134. /// Position
  135. Position mRelativeX;
  136. Position mRelativeY;
  137. Size mWidth;
  138. Size mHeight;
  139. Size mPaddingRight;
  140. Size mPaddingBottom;
  141. bool mIsVisible;
  142. bool mAnimatedIsVisible;
  143. bool mIsHighlighted;
  144. bool mIsSelected;
  145. bool mIsDisabled;
  146. /// Animations
  147. bool mHasActivateAnimation;
  148. bool mHasDeactivateAnimation;
  149. UIAnimationVector mAnimations;
  150. /// Manager
  151. UIManager * mManager;
  152. };