UICheckBox.cpp 2.2 KB

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