SpriteBorderEditor.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <QDialog>
  11. #endif
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. //! \brief Visual sprite editor used to configure slicing and sprite-sheet properties for a given sprite.
  14. class SpriteBorderEditor
  15. : public QDialog
  16. {
  17. Q_OBJECT
  18. public:
  19. SpriteBorderEditor(const char* path, QWidget* parent = nullptr);
  20. ~SpriteBorderEditor();
  21. bool GetHasBeenInitializedProperly();
  22. signals:
  23. //! Signals when a new cell within the sprite-sheet has been selected.
  24. void SelectedCellChanged(ISprite* sprite, AZ::u32 index = 0);
  25. //! Signals when the Sprite Editor UI is being reset.
  26. //!
  27. //! See ResetUi.
  28. void ResettingUi();
  29. private: // types
  30. //! Sprite info required to restore sprite to its original state if user cancels the dialog.
  31. struct SpritesheetRestoreInfo
  32. {
  33. ISprite::SpriteSheetCellContainer spriteSheetCells;
  34. ISprite::Borders borders;
  35. };
  36. private: // functions
  37. //! Reconstructs the UI widgets from sprite and member data.
  38. void ResetUi();
  39. //! Removes all widgets from the dialog.
  40. void ClearLayout();
  41. //! Re-calculates sprite-sheet cell UV info and resets the UI.
  42. void UpdateSpriteSheetCellInfo(int newNumRows, int newNumCols, ISprite* sprite);
  43. //! Given the cell index, updates the properties view with the selected sprite-sheet cell.
  44. void DisplaySelectedCell(AZ::u32 cellIndex);
  45. void AddConfigureSection(QGridLayout* gridLayout, int& rowNum);
  46. void AddSelectCellSection(QGridLayout* gridLayout, int& rowNum);
  47. void AddPropertiesSection(QGridLayout* gridLayout, int& rowNum);
  48. void AddButtonsSection(QGridLayout* gridLayout, int& rowNum);
  49. void AddSeparator(QGridLayout* gridLayout, int& rowNum);
  50. void SetDisplayedTextureSize(float width, float height);
  51. bool IsConfiguringSpriteSheet() const;
  52. private slots:
  53. //! Creates the window layout, populating the dialog with widget content.
  54. void CreateLayout();
  55. private: // member vars
  56. SpritesheetRestoreInfo m_restoreInfo; //!< Stores starting sprite configuration in case the user cancels the dialog
  57. static const int numSliceBorders = 4;
  58. SlicerManipulator* m_manipulators[numSliceBorders];
  59. QString m_spritePath; //!< Path to the sprite being edited.
  60. QPixmap m_unscaledSpriteSheet; //!< Unscaled sprite-sheet image, useful for determining cell locations by UV coords.
  61. QGraphicsPixmapItem* m_cellPropertiesPixmap = nullptr; //!< Image item data used by the graphics scene to display image/cell.
  62. QGraphicsScene* m_cellPropertiesGraphicsScene = nullptr; //!< Image/sprite-sheet cell "view" displayed in window
  63. QLabel* m_textureSizeLabel = nullptr; //!< Displays the selected texture/cell size info.
  64. QLineEdit* m_cellAliasLineEdit = nullptr; //!< Displays the selected cell alias.
  65. QWidget* m_prevTabField = nullptr; //!< Points to the previous field that should be tabbed from (see calls to QWidget::setTabOrder).
  66. uint m_numRows = 1; //!< Number of rows in spritesheet cell grid.
  67. uint m_numCols = 1; //!< Number of cols in spritesheet cell grid.
  68. bool m_hasBeenInitializedProperly = true; //!< True if dialog was constructed properly, false otherwise
  69. ISprite* m_sprite = nullptr; //!< Currently loaded sprite.
  70. AZ::u32 m_currentCellIndex = 0; //!< Currently selected cell index
  71. bool m_configureAsSpriteSheet = false; //!< Forces the spritesheet configuration section to display
  72. bool m_resizeOnce = true; //!< Resize the window only once (since CreateLayout is called multiple times)
  73. };
  74. //! \brief A custom rect item that allows us to get a mouse press event
  75. //!
  76. //! This provides a convenient callback to determine which cell index
  77. //! was selected within the cell selection view.
  78. class CellSelectRectItem : public QObject, public QGraphicsRectItem
  79. {
  80. Q_OBJECT
  81. public:
  82. CellSelectRectItem(const QRectF& rect, const AZStd::function<void()>& clickCallback);
  83. ~CellSelectRectItem() override;
  84. static void ClearSelection() { s_currentSelection = nullptr; }
  85. //! Activates the "selected cell" border styling.
  86. //!
  87. //! Changes the QBrush styling for this RectItem to draw the
  88. //! "selected" border style and de-selects the previously
  89. //! selected item by removing the brush styling from it.
  90. void SelectCell();
  91. public slots:
  92. void StopProcessingInput()
  93. {
  94. m_processInput = false;
  95. }
  96. protected:
  97. //! "Selects" the cell and triggers the associated click callback.
  98. void mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
  99. private:
  100. static CellSelectRectItem* s_currentSelection;
  101. //! Function to call when this cell/rectitem is selected.
  102. AZStd::function<void()> m_clickCallback;
  103. //! Determines whether input events are processed or not.
  104. //!
  105. //! It's useful to turn off further event processing such as when
  106. //! the Sprite Editor UI is being reset to apply a new configuration
  107. //! or layout.
  108. bool m_processInput = true;
  109. };