UIElement.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Renderer/Renderer.h>
  5. #include <UI/UIElement.h>
  6. #include <UI/UIAnimation.h>
  7. JPH_IMPLEMENT_RTTI_VIRTUAL_BASE(UIElement)
  8. {
  9. }
  10. UIElement::UIElement() :
  11. mID(-1),
  12. mParent(nullptr),
  13. mIsVisible(true),
  14. mAnimatedIsVisible(true),
  15. mIsHighlighted(false),
  16. mIsSelected(false),
  17. mIsDisabled(false),
  18. mHasActivateAnimation(true),
  19. mHasDeactivateAnimation(true),
  20. mManager(nullptr)
  21. {
  22. }
  23. UIElement::~UIElement()
  24. {
  25. Clear();
  26. }
  27. void UIElement::Add(UIElement *inElement)
  28. {
  29. inElement->mParent = this;
  30. inElement->mManager = mManager;
  31. mChildren.push_back(inElement);
  32. inElement->OnAdded();
  33. }
  34. void UIElement::Clear()
  35. {
  36. for (UIAnimation *a : mAnimations)
  37. delete a;
  38. for (UIElement *e : mChildren)
  39. delete e;
  40. }
  41. void UIElement::StartAnimation(UIAnimation *inAnimation)
  42. {
  43. mAnimations.push_back(inAnimation);
  44. inAnimation->Init(this);
  45. inAnimation->Update(this, 0.0f);
  46. }
  47. void UIElement::StopAnimation(const RTTI *inAnimationType)
  48. {
  49. for (int i = (int)mAnimations.size() - 1; i >= 0; --i)
  50. if (mAnimations[i]->GetRTTI()->IsKindOf(inAnimationType))
  51. {
  52. mAnimations[i]->Exit(this);
  53. delete mAnimations[i];
  54. mAnimations.erase(mAnimations.begin() + i);
  55. }
  56. }
  57. UIElement *UIElement::Clone() const
  58. {
  59. UIElement *element = reinterpret_cast<UIElement *>(GetRTTI()->CreateObject());
  60. CopyTo(element);
  61. return element;
  62. }
  63. void UIElement::SetHighlighted(bool inHighlighted)
  64. {
  65. mIsHighlighted = inHighlighted;
  66. for (UIElement *e : mChildren)
  67. e->SetHighlighted(inHighlighted);
  68. }
  69. void UIElement::SetSelected(bool inSelected)
  70. {
  71. mIsSelected = inSelected;
  72. for (UIElement *e : mChildren)
  73. e->SetSelected(inSelected);
  74. }
  75. void UIElement::SetDisabled(bool inDisabled)
  76. {
  77. mIsDisabled = inDisabled;
  78. for (UIElement *e : mChildren)
  79. e->SetDisabled(inDisabled);
  80. }
  81. void UIElement::CopyTo(UIElement *ioElement) const
  82. {
  83. // Clone properties
  84. ioElement->mID = mID;
  85. ioElement->mRelativeX = mRelativeX;
  86. ioElement->mRelativeY = mRelativeY;
  87. ioElement->mWidth = mWidth;
  88. ioElement->mHeight = mHeight;
  89. ioElement->mIsVisible = mIsVisible;
  90. ioElement->mAnimatedIsVisible = mAnimatedIsVisible;
  91. ioElement->mHasActivateAnimation = mHasActivateAnimation;
  92. ioElement->mHasDeactivateAnimation = mHasDeactivateAnimation;
  93. ioElement->mManager = mManager;
  94. // Clone children
  95. for (const UIElement *e : mChildren)
  96. ioElement->Add(e->Clone());
  97. }
  98. bool UIElement::Contains(int inX, int inY) const
  99. {
  100. int x = GetX(), y = GetY();
  101. return inX >= x && inX < x + GetWidth() && inY >= y && inY < y + GetHeight();
  102. }
  103. bool UIElement::ContainsWidened(int inX, int inY, int inBorder) const
  104. {
  105. int x = GetX(), y = GetY();
  106. return inX >= x - inBorder && inX < x + GetWidth() + inBorder && inY >= y - inBorder && inY < y + GetHeight() + inBorder;
  107. }
  108. void UIElement::Update(float inDeltaTime)
  109. {
  110. for (int i = 0; i < (int)mAnimations.size(); ++i)
  111. {
  112. UIAnimation *animation = mAnimations[i];
  113. if (!animation->Update(this, inDeltaTime))
  114. {
  115. animation->Exit(this);
  116. delete animation;
  117. mAnimations.erase(mAnimations.begin() + i, mAnimations.begin() + i + 1);
  118. --i;
  119. }
  120. }
  121. for (UIElement *e : mChildren)
  122. if (e->IsVisible())
  123. e->Update(inDeltaTime);
  124. }
  125. void UIElement::Draw() const
  126. {
  127. for (const UIElement *e : mChildren)
  128. if (e->IsVisible())
  129. e->Draw();
  130. }
  131. bool UIElement::MouseDown(int inX, int inY)
  132. {
  133. for (UIElement *e : mChildren)
  134. if (e->IsVisible() && !e->mIsDisabled)
  135. if (e->MouseDown(inX, inY))
  136. return true;
  137. return false;
  138. }
  139. bool UIElement::MouseUp(int inX, int inY)
  140. {
  141. for (UIElement *e : mChildren)
  142. if (e->IsVisible() && !e->mIsDisabled)
  143. if (e->MouseUp(inX, inY))
  144. return true;
  145. return false;
  146. }
  147. bool UIElement::MouseMove(int inX, int inY)
  148. {
  149. if (Contains(inX, inY))
  150. mIsHighlighted = true;
  151. else
  152. mIsHighlighted = false;
  153. for (UIElement *e : mChildren)
  154. if (e->IsVisible() && !e->mIsDisabled)
  155. if (e->MouseMove(inX, inY))
  156. return true;
  157. return false;
  158. }
  159. void UIElement::MouseCancel()
  160. {
  161. for (UIElement *e : mChildren)
  162. if (e->IsVisible() && !e->mIsDisabled)
  163. e->MouseCancel();
  164. }
  165. UIElement *UIElement::FindByID(int inID)
  166. {
  167. if (inID == mID)
  168. return this;
  169. for (UIElement *e : mChildren)
  170. {
  171. UIElement *element = e->FindByID(inID);
  172. if (element != nullptr)
  173. return element;
  174. }
  175. return nullptr;
  176. }
  177. void UIElement::AutoLayout()
  178. {
  179. for (UIElement *e : mChildren)
  180. {
  181. // Recurse
  182. e->AutoLayout();
  183. // Encapsulate height and width of children
  184. if (e->IsVisible())
  185. {
  186. mWidth.Set(max(GetWidth(), e->GetX() + e->GetWidth() - GetX() + e->GetPaddingRight()), PIXELS);
  187. mHeight.Set(max(GetHeight(), e->GetY() + e->GetHeight() - GetY() + e->GetPaddingBottom()), PIXELS);
  188. }
  189. }
  190. }
  191. bool UIElement::HandleUIEvent(EUIEvent inEvent, UIElement *inSender)
  192. {
  193. if (mParent != nullptr)
  194. return mParent->HandleUIEvent(inEvent, inSender);
  195. return false;
  196. }
  197. void UIElement::Size::Set(int inValue, EUnits inUnits)
  198. {
  199. mUnit = inUnits;
  200. mSize = inValue;
  201. }
  202. int UIElement::Size::GetSize(const UIElement *inElement, fGetSize inGetSize) const
  203. {
  204. switch (mUnit)
  205. {
  206. case PIXELS:
  207. return mSize;
  208. case PERCENTAGE:
  209. {
  210. const UIElement *parent = inElement->GetParent();
  211. if (parent != nullptr)
  212. return (mSize * (parent->*inGetSize)()) / 100;
  213. else
  214. return 0;
  215. }
  216. default:
  217. JPH_ASSERT(false);
  218. return 0;
  219. }
  220. }
  221. void UIElement::Position::Set(int inValue, EUnits inUnits, EAlignment inAlignment)
  222. {
  223. mAlignment = inAlignment;
  224. mSize.Set(inValue, inUnits);
  225. }
  226. int UIElement::Position::GetPosition(const UIElement *inElement, fGetSize inGetSize) const
  227. {
  228. int pos = mSize.GetSize(inElement, inGetSize);
  229. switch (mAlignment)
  230. {
  231. case LEFT:
  232. return pos;
  233. case ONE_THIRD:
  234. {
  235. const UIElement *parent = inElement->GetParent();
  236. if (parent != nullptr)
  237. return ((parent->*inGetSize)() - (inElement->*inGetSize)()) / 3 + pos;
  238. else
  239. return 0;
  240. }
  241. case CENTER:
  242. {
  243. const UIElement *parent = inElement->GetParent();
  244. if (parent != nullptr)
  245. return ((parent->*inGetSize)() - (inElement->*inGetSize)()) / 2 + pos;
  246. else
  247. return 0;
  248. }
  249. case RIGHT:
  250. {
  251. const UIElement *parent = inElement->GetParent();
  252. if (parent != nullptr)
  253. return (parent->*inGetSize)() - (inElement->*inGetSize)() + pos;
  254. else
  255. return 0;
  256. }
  257. default:
  258. JPH_ASSERT(false);
  259. return 0;
  260. }
  261. }