UIElement.cpp 6.2 KB

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