2
0

UITextButton.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/UITextButton.h>
  6. #include <UI/UIManager.h>
  7. JPH_IMPLEMENT_RTTI_VIRTUAL(UITextButton)
  8. {
  9. JPH_ADD_BASE_CLASS(UITextButton, UIStaticText)
  10. }
  11. void UITextButton::CopyTo(UIElement *ioElement) const
  12. {
  13. UIStaticText::CopyTo(ioElement);
  14. UITextButton *element = StaticCast<UITextButton>(ioElement);
  15. element->mDownTextColor = mDownTextColor;
  16. element->mHighlightTextColor = mHighlightTextColor;
  17. element->mSelectedTextColor = mSelectedTextColor;
  18. element->mRepeatStartTime = mRepeatStartTime;
  19. element->mRepeatTime = mRepeatTime;
  20. element->mClickAction = mClickAction;
  21. }
  22. bool UITextButton::MouseDown(int inX, int inY)
  23. {
  24. if (UIStaticText::MouseDown(inX, inY))
  25. return true;
  26. if (Contains(inX, inY))
  27. {
  28. mPressed = true;
  29. mIsRepeating = false;
  30. mRepeatTimeLeft = mRepeatStartTime;
  31. return true;
  32. }
  33. return false;
  34. }
  35. bool UITextButton::MouseUp(int inX, int inY)
  36. {
  37. if (UIStaticText::MouseUp(inX, inY))
  38. return true;
  39. if (mPressed)
  40. {
  41. mPressed = false;
  42. if (!mIsRepeating && Contains(inX, inY))
  43. {
  44. HandleUIEvent(EVENT_BUTTON_DOWN, this);
  45. if (mClickAction)
  46. mClickAction();
  47. }
  48. return true;
  49. }
  50. return false;
  51. }
  52. bool UITextButton::MouseMove(int inX, int inY)
  53. {
  54. if (UIStaticText::MouseMove(inX, inY))
  55. return true;
  56. return mPressed;
  57. }
  58. void UITextButton::MouseCancel()
  59. {
  60. UIStaticText::MouseCancel();
  61. mPressed = false;
  62. }
  63. void UITextButton::Update(float inDeltaTime)
  64. {
  65. UIStaticText::Update(inDeltaTime);
  66. if (mPressed && mRepeatStartTime > 0)
  67. {
  68. // Check repeat
  69. mRepeatTimeLeft -= inDeltaTime;
  70. if (mRepeatTimeLeft <= 0.0f)
  71. {
  72. // We're repeating
  73. mIsRepeating = true;
  74. mRepeatTimeLeft = mRepeatTime;
  75. HandleUIEvent(EVENT_BUTTON_DOWN, this);
  76. if (mClickAction)
  77. mClickAction();
  78. }
  79. }
  80. }
  81. void UITextButton::DrawCustom() const
  82. {
  83. UIStaticText::DrawCustom(IsDisabled()? mDisabledTextColor : (mPressed? mDownTextColor : (mIsHighlighted? mHighlightTextColor : (mIsSelected? mSelectedTextColor : mTextColor))));
  84. }
  85. void UITextButton::Draw() const
  86. {
  87. DrawCustom();
  88. // Skip direct base class, we modify the text color
  89. UIElement::Draw();
  90. }