UIHorizontalStack.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <UI/UIHorizontalStack.h>
  5. JPH_IMPLEMENT_RTTI_VIRTUAL(UIHorizontalStack)
  6. {
  7. JPH_ADD_BASE_CLASS(UIHorizontalStack, UIElement)
  8. }
  9. void UIHorizontalStack::sUniformChildWidth(UIElement *inParent)
  10. {
  11. Array<int> sizes;
  12. sizes.resize(1);
  13. for (UIElement *e : inParent->GetChildren())
  14. {
  15. e->AutoLayout();
  16. UIHorizontalStack *horiz = DynamicCast<UIHorizontalStack>(e);
  17. if (horiz != nullptr)
  18. {
  19. if (horiz->GetNumChildren() > (int)sizes.size())
  20. sizes.resize(horiz->GetNumChildren());
  21. for (int i = 0; i < horiz->GetNumChildren(); ++i)
  22. sizes[i] = max(sizes[i], horiz->GetChild(i)->GetWidth());
  23. }
  24. else
  25. {
  26. sizes[0] = max(sizes[0], e->GetWidth());
  27. }
  28. }
  29. for (UIElement *e : inParent->GetChildren())
  30. {
  31. UIHorizontalStack *horiz = DynamicCast<UIHorizontalStack>(e);
  32. if (horiz != nullptr)
  33. {
  34. for (int i = 0; i < horiz->GetNumChildren(); ++i)
  35. horiz->GetChild(i)->SetWidth(sizes[i]);
  36. }
  37. else
  38. {
  39. e->SetWidth(sizes[0]);
  40. }
  41. }
  42. }
  43. void UIHorizontalStack::AutoLayout()
  44. {
  45. UIElement::AutoLayout();
  46. mWidth.Set(0, PIXELS);
  47. for (UIElement *e : mChildren)
  48. if (e->IsVisible() || mPlaceInvisibleChildren)
  49. {
  50. e->SetRelativeX(GetWidth());
  51. mWidth.Set(GetWidth() + e->GetWidth() + e->GetPaddingRight() + mDeltaX, PIXELS);
  52. }
  53. }