UICheckBox.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <UI/UICheckBox.h>
  5. #include <UI/UIManager.h>
  6. JPH_IMPLEMENT_RTTI_VIRTUAL(UICheckBox)
  7. {
  8. JPH_ADD_BASE_CLASS(UICheckBox, UIStaticText)
  9. }
  10. void UICheckBox::CopyTo(UIElement *ioElement) const
  11. {
  12. UIStaticText::CopyTo(ioElement);
  13. UICheckBox *element = StaticCast<UICheckBox>(ioElement);
  14. element->mDownTextColor = mDownTextColor;
  15. element->mHighlightTextColor = mHighlightTextColor;
  16. element->mPaddingBetweenCheckboxAndText = mPaddingBetweenCheckboxAndText;
  17. element->mState = mState;
  18. element->mUncheckedState = mUncheckedState;
  19. element->mCheckedState = mCheckedState;
  20. element->mClickAction = mClickAction;
  21. }
  22. void UICheckBox::OnAdded()
  23. {
  24. mTextPadLeft = max(mUncheckedState.mWidth, mCheckedState.mWidth) + mPaddingBetweenCheckboxAndText;
  25. }
  26. bool UICheckBox::MouseDown(int inX, int inY)
  27. {
  28. if (UIStaticText::MouseDown(inX, inY))
  29. return true;
  30. if (Contains(inX, inY))
  31. {
  32. mPressed = true;
  33. return true;
  34. }
  35. return false;
  36. }
  37. bool UICheckBox::MouseUp(int inX, int inY)
  38. {
  39. if (UIStaticText::MouseUp(inX, inY))
  40. return true;
  41. if (mPressed)
  42. {
  43. mPressed = false;
  44. if (Contains(inX, inY))
  45. {
  46. mState = mState == STATE_CHECKED? STATE_UNCHECKED : STATE_CHECKED;
  47. if (mClickAction)
  48. mClickAction(mState);
  49. }
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool UICheckBox::MouseMove(int inX, int inY)
  55. {
  56. if (UIStaticText::MouseMove(inX, inY))
  57. return true;
  58. return mPressed;
  59. }
  60. void UICheckBox::MouseCancel()
  61. {
  62. UIStaticText::MouseCancel();
  63. mPressed = false;
  64. }
  65. void UICheckBox::Draw() const
  66. {
  67. Color color = IsDisabled()? mDisabledTextColor : (mPressed? mDownTextColor : (mIsHighlighted? mHighlightTextColor : mTextColor));
  68. UIStaticText::DrawCustom(color);
  69. if (mState == STATE_UNCHECKED)
  70. GetManager()->DrawQuad(GetX(), GetY() + (GetHeight() - mUncheckedState.mHeight) / 2, mUncheckedState.mWidth, mUncheckedState.mHeight, mUncheckedState, color);
  71. else if (mState == STATE_CHECKED)
  72. GetManager()->DrawQuad(GetX(), GetY() + (GetHeight() - mCheckedState.mHeight) / 2, mCheckedState.mWidth, mCheckedState.mHeight, mCheckedState, color);
  73. // Skip direct base class, we modify the text color
  74. UIElement::Draw();
  75. }