SlicerEdit.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. SlicerEdit::SlicerEdit(
  10. SpriteBorderEditor* borderEditor,
  11. SpriteBorder border,
  12. [[maybe_unused]] QSize& unscaledPixmapSize,
  13. ISprite* sprite)
  14. : QLineEdit()
  15. , m_sprite(sprite)
  16. , m_border(border)
  17. {
  18. bool isVertical = IsBorderVertical(m_border);
  19. const AZ::Vector2 cellSize = m_sprite->GetCellSize(m_currentCellIndex);
  20. const float totalUnscaledSizeInPixels = (isVertical ? cellSize.GetX() : cellSize.GetY());
  21. setPixelPosition(GetBorderValueInPixels(m_sprite, m_border, totalUnscaledSizeInPixels));
  22. setValidator(new QDoubleValidator(0.0f, totalUnscaledSizeInPixels, 1));
  23. QObject::connect(this,
  24. &SlicerEdit::editingFinished,
  25. this,
  26. [ this, totalUnscaledSizeInPixels ]()
  27. {
  28. // User text input is always interpreted as relative value
  29. const float relativeBorderValue = text().toFloat();
  30. // Whereas the on-screen manipulator position and stored values
  31. // are absolute.
  32. const float absoluteBorderValue = OffsetBorderValue(relativeBorderValue);
  33. m_manipulator->setPixelPosition(absoluteBorderValue);
  34. SetBorderValue(m_sprite, m_border, absoluteBorderValue, totalUnscaledSizeInPixels, m_currentCellIndex);
  35. });
  36. QObject::connect(
  37. borderEditor,
  38. &SpriteBorderEditor::SelectedCellChanged,
  39. this,
  40. [this]([[maybe_unused]] ISprite* sprite, AZ::u32 index)
  41. {
  42. m_currentCellIndex = index;
  43. });
  44. }
  45. void SlicerEdit::SetManipulator(SlicerManipulator* manipulator)
  46. {
  47. m_manipulator = manipulator;
  48. }
  49. void SlicerEdit::setPixelPosition(float p)
  50. {
  51. // The border values should be presented to the user as offsets from
  52. // their corresponding borders. The given pixel position is expressed
  53. // in terms of total image size, so for Top and Left borders, the given
  54. // "pixel" position is indeed the distance from those borders. But for
  55. // Right and Bottom, we need to subtract the pixel postion from the
  56. // width and height of the image size (respectively) to present the
  57. // values as offsets from their respective borders.
  58. float relativeBorderValue = OffsetBorderValue(p);
  59. // For relatively small differences most likely due to floating point
  60. // inaccuracy, lock the difference to zero.
  61. const float epsilon = 0.001f;
  62. relativeBorderValue = relativeBorderValue >= epsilon ? relativeBorderValue : 0.0f;
  63. setText(QString::number(relativeBorderValue, 'f', 1));
  64. }
  65. float SlicerEdit::OffsetBorderValue(float borderValue) const
  66. {
  67. const AZ::Vector2 cellSize = m_sprite->GetCellSize(m_currentCellIndex);
  68. if (SpriteBorder::Right == m_border)
  69. {
  70. return cellSize.GetX() - borderValue;
  71. }
  72. else if (SpriteBorder::Bottom == m_border)
  73. {
  74. return cellSize.GetY() - borderValue;
  75. }
  76. else
  77. {
  78. return borderValue;
  79. }
  80. }
  81. #include <moc_SlicerEdit.cpp>