UIComboBox.cpp 2.0 KB

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