SpriteBorderEditorCommon.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "SpriteBorderEditorCommon.h"
  9. bool IsBorderVertical(SpriteBorder border)
  10. {
  11. return ((border == SpriteBorder::Left) ||
  12. (border == SpriteBorder::Right));
  13. }
  14. float GetBorderValueInPixels(ISprite* sprite, SpriteBorder b, float totalSizeInPixels, AZ::u32 cellIndex)
  15. {
  16. // IMPORTANT: We CAN'T replace totalSizeInPixels with
  17. // sprite->GetTexture()->GetWidth()/GetHeight() because
  18. // it DOESN'T return the original texture file's size.
  19. ISprite::Borders sb = sprite->GetCellUvBorders(cellIndex);
  20. float* f = nullptr;
  21. if (b == SpriteBorder::Top)
  22. {
  23. f = &sb.m_top;
  24. }
  25. else if (b == SpriteBorder::Bottom)
  26. {
  27. f = &sb.m_bottom;
  28. }
  29. else if (b == SpriteBorder::Left)
  30. {
  31. f = &sb.m_left;
  32. }
  33. else if (b == SpriteBorder::Right)
  34. {
  35. f = &sb.m_right;
  36. }
  37. else
  38. {
  39. AZ_Assert(false, "Invalid border enum value");
  40. f = nullptr;
  41. }
  42. return (*f * totalSizeInPixels);
  43. }
  44. void SetBorderValue(ISprite* sprite, SpriteBorder b, float pixelPosition, float totalSizeInPixels, AZ::u32 cellIndex)
  45. {
  46. // IMPORTANT: We CAN'T replace totalSizeInPixels with
  47. // sprite->GetTexture()->GetWidth()/GetHeight() because
  48. // it DOESN'T return the original texture file's size.
  49. ISprite::Borders sb = sprite->GetCellUvBorders(cellIndex);
  50. float* f = nullptr;
  51. if (b == SpriteBorder::Top)
  52. {
  53. f = &sb.m_top;
  54. }
  55. else if (b == SpriteBorder::Bottom)
  56. {
  57. f = &sb.m_bottom;
  58. }
  59. else if (b == SpriteBorder::Left)
  60. {
  61. f = &sb.m_left;
  62. }
  63. else if (b == SpriteBorder::Right)
  64. {
  65. f = &sb.m_right;
  66. }
  67. else
  68. {
  69. AZ_Assert(false, "Invalid border enum value");
  70. f = nullptr;
  71. }
  72. *f = AZ::GetMin<float>(pixelPosition / totalSizeInPixels, 1.0f);
  73. sprite->SetCellBorders(cellIndex, sb);
  74. }
  75. const char* SpriteBorderToString(SpriteBorder b)
  76. {
  77. if (b == SpriteBorder::Top)
  78. {
  79. return "Top";
  80. }
  81. else if (b == SpriteBorder::Bottom)
  82. {
  83. return "Bottom";
  84. }
  85. else if (b == SpriteBorder::Left)
  86. {
  87. return "Left";
  88. }
  89. else if (b == SpriteBorder::Right)
  90. {
  91. return "Right";
  92. }
  93. else
  94. {
  95. AZ_Assert(false, "Invalid border enum value");
  96. return "UNKNOWN";
  97. }
  98. }