UITextButton.cpp 2.1 KB

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