2
0

UIButton.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <Renderer/Renderer.h>
  6. #include <UI/UIButton.h>
  7. #include <UI/UIManager.h>
  8. JPH_IMPLEMENT_RTTI_VIRTUAL(UIButton)
  9. {
  10. JPH_ADD_BASE_CLASS(UIButton, UITextButton)
  11. }
  12. void UIButton::CopyTo(UIElement *ioElement) const
  13. {
  14. UITextButton::CopyTo(ioElement);
  15. UIButton *element = StaticCast<UIButton>(ioElement);
  16. element->mUpQuad = mUpQuad;
  17. element->mUpColor = mUpColor;
  18. element->mDownQuad = mDownQuad;
  19. element->mDownColor = mDownColor;
  20. element->mSelectedQuad = mSelectedQuad;
  21. element->mSelectedColor = mSelectedColor;
  22. element->mDisabledQuad = mDisabledQuad;
  23. element->mDisabledColor = mDisabledColor;
  24. }
  25. void UIButton::Draw() const
  26. {
  27. if (mUpQuad.mTexture != nullptr)
  28. {
  29. int x = GetX(), y = GetY();
  30. const UITexturedQuad &q = IsDisabled()? mDisabledQuad : (mPressed? mDownQuad : (mIsHighlighted? mHighlightQuad : mUpQuad));
  31. Color c = IsDisabled()? mDisabledColor : (mPressed? mDownColor : (mIsHighlighted? mHighlightColor : mUpColor));
  32. int ew = GetWidth();
  33. int eh = GetHeight();
  34. if (!q.HasInnerPart())
  35. {
  36. // Center image in button if it is smaller than the button
  37. int w = min(ew, q.mWidth);
  38. int h = min(eh, q.mHeight);
  39. int x2 = x + (ew - w) / 2;
  40. int y2 = y + (eh - h) / 2;
  41. GetManager()->DrawQuad(x2, y2, w, h, q, c);
  42. }
  43. else
  44. {
  45. // This is a scale-9 quad, it will scale itself
  46. GetManager()->DrawQuad(x, y, ew, eh, q, c);
  47. }
  48. // Draw selected quad
  49. if (mIsSelected)
  50. GetManager()->DrawQuad(x, y, ew, eh, mSelectedQuad, mSelectedColor);
  51. }
  52. DrawCustom();
  53. // Skip direct base classes, we modify text color
  54. UIElement::Draw();
  55. }
  56. void UIButton::SetButtonQuad(const UITexturedQuad &inQuad)
  57. {
  58. mUpQuad = inQuad;
  59. mDownQuad = inQuad;
  60. mHighlightQuad = inQuad;
  61. mDisabledQuad = inQuad;
  62. if (GetWidth() <= 0)
  63. SetWidth(inQuad.mWidth);
  64. if (GetHeight() <= 0)
  65. SetHeight(inQuad.mHeight);
  66. }