UIElement.h 5.6 KB

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