UIComboBox.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <UI/UIComboBox.h>
  5. #include <UI/UIManager.h>
  6. JPH_IMPLEMENT_RTTI_VIRTUAL(UIComboBox)
  7. {
  8. JPH_ADD_BASE_CLASS(UIComboBox, UIElement)
  9. }
  10. void UIComboBox::CopyTo(UIElement *ioElement) const
  11. {
  12. UIElement::CopyTo(ioElement);
  13. UIComboBox *element = StaticCast<UIComboBox>(ioElement);
  14. element->mCurrentItem = mCurrentItem;
  15. element->mItems = mItems;
  16. element->mPreviousButton = mPreviousButton;
  17. element->mNextButton = mNextButton;
  18. element->mStaticText = mStaticText;
  19. element->mItemChangedAction = mItemChangedAction;
  20. }
  21. bool UIComboBox::HandleUIEvent(EUIEvent inEvent, UIElement *inSender)
  22. {
  23. if (inEvent == EVENT_BUTTON_DOWN)
  24. {
  25. if (inSender == mPreviousButton)
  26. {
  27. SetItemInternal(mCurrentItem - 1);
  28. return true;
  29. }
  30. else if (inSender == mNextButton)
  31. {
  32. SetItemInternal(mCurrentItem + 1);
  33. return true;
  34. }
  35. }
  36. return UIElement::HandleUIEvent(inEvent, inSender);
  37. }
  38. void UIComboBox::AutoLayout()
  39. {
  40. UIElement::AutoLayout();
  41. // Position previous button
  42. mPreviousButton->SetRelativeX(0);
  43. mPreviousButton->SetRelativeY((GetHeight() - mPreviousButton->GetHeight()) / 2);
  44. // Position static text
  45. mStaticText->SetRelativeX((GetWidth() - mStaticText->GetWidth()) / 2);
  46. mStaticText->SetRelativeY((GetHeight() - mStaticText->GetHeight()) / 2);
  47. // Position next button
  48. mNextButton->SetRelativeX(GetWidth() - mNextButton->GetWidth());
  49. mNextButton->SetRelativeY((GetHeight() - mNextButton->GetHeight()) / 2);
  50. }
  51. void UIComboBox::SetItemInternal(int inItem)
  52. {
  53. int old_item = mCurrentItem;
  54. if (inItem < 0)
  55. mCurrentItem = 0;
  56. else if (inItem > int(mItems.size()) - 1)
  57. mCurrentItem = int(mItems.size()) - 1;
  58. else
  59. mCurrentItem = inItem;
  60. if (mCurrentItem != old_item)
  61. {
  62. if (mItemChangedAction)
  63. mItemChangedAction(mCurrentItem);
  64. UpdateStaticText();
  65. }
  66. }
  67. void UIComboBox::UpdateStaticText()
  68. {
  69. if (mStaticText != nullptr)
  70. mStaticText->SetText(mItems[mCurrentItem]);
  71. }