2
0

UIButton.cpp 1.9 KB

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