3
0

Sprite.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <LyShine/ISprite.h>
  10. #include <platform.h>
  11. #include <StlUtils.h>
  12. #include "TextureAtlas/TextureAtlas.h"
  13. #include "TextureAtlas/TextureAtlasBus.h"
  14. #include "TextureAtlas/TextureAtlasNotificationBus.h"
  15. #include <Atom/RPI.Reflect/Image/Image.h>
  16. #include <AtomCore/Instance/Instance.h>
  17. namespace AZ::RPI
  18. {
  19. class AttachmentImageAsset;
  20. }
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////
  22. class CSprite
  23. : public ISprite
  24. , public TextureAtlasNamespace::TextureAtlasNotificationBus::Handler
  25. {
  26. public: // member functions
  27. //! Construct an empty sprite
  28. CSprite();
  29. // ISprite
  30. ~CSprite() override;
  31. const AZStd::string& GetPathname() const override;
  32. const AZStd::string& GetTexturePathname() const override;
  33. Borders GetBorders() const override;
  34. void SetBorders(Borders borders) override;
  35. void SetCellBorders(int cellIndex, Borders borders) override;
  36. void Serialize(TSerialize ser) override;
  37. bool SaveToXml(const AZStd::string& pathname) override;
  38. bool AreBordersZeroWidth() const override;
  39. bool AreCellBordersZeroWidth(int index) const override;
  40. AZ::Vector2 GetSize() override;
  41. AZ::Vector2 GetCellSize(int cellIndex) override;
  42. const SpriteSheetCellContainer& GetSpriteSheetCells() const override;
  43. void SetSpriteSheetCells(const SpriteSheetCellContainer& cells) override;
  44. void ClearSpriteSheetCells() override;
  45. void AddSpriteSheetCell(const SpriteSheetCell& spriteSheetCell) override;
  46. AZ::Vector2 GetCellUvSize(int cellIndex) const override;
  47. UiTransformInterface::RectPoints GetCellUvCoords(int cellIndex) const override;
  48. UiTransformInterface::RectPoints GetSourceCellUvCoords(int cellIndex) const override;
  49. Borders GetCellUvBorders(int cellIndex) const override;
  50. Borders GetTextureSpaceCellUvBorders(int cellIndex) const override;
  51. const AZStd::string& GetCellAlias(int cellIndex) const override;
  52. void SetCellAlias(int cellIndex, const AZStd::string& cellAlias) override;
  53. int GetCellIndexFromAlias(const AZStd::string& cellAlias) const override;
  54. bool IsSpriteSheet() const override;
  55. AZ::Data::Instance<AZ::RPI::Image> GetImage() override;
  56. // ~ISprite
  57. // TextureAtlasNotifications
  58. void OnAtlasLoaded(const TextureAtlasNamespace::TextureAtlas* atlas) override;
  59. void OnAtlasUnloaded(const TextureAtlasNamespace::TextureAtlas* atlas) override;
  60. // ~TextureAtlasNotifications
  61. public: // static member functions
  62. static void Initialize();
  63. static void Shutdown();
  64. static CSprite* LoadSprite(const AZStd::string& pathname);
  65. static CSprite* CreateSprite(const AZ::Data::Asset<AZ::RPI::AttachmentImageAsset>& attachmentImageAsset);
  66. static bool DoesSpriteTextureAssetExist(const AZStd::string& pathname);
  67. //! Replaces baseSprite with newSprite with proper ref-count handling and null-checks.
  68. static void ReplaceSprite(ISprite** baseSprite, ISprite* newSprite);
  69. //! Pathname allows any of the following:
  70. //! 1. image source/product path (will use pathname to look for an existing .sprite sidecar file)
  71. //! 2. .sprite source/product path (will use pathname to look for an existing image file with a supported extension)
  72. //! 3. legacy .dds product path (will use pathname to look for an existing texture file with a supported extension)
  73. static bool FixUpSourceImagePathFromUserDefinedPath(const AZStd::string& userDefinedPath, AZStd::string& sourceImagePath);
  74. static AZStd::string GetImageSourcePathFromProductPath(const AZStd::string& productPathname);
  75. private:
  76. static bool LoadImage(const AZStd::string& nameTex, AZ::Data::Instance<AZ::RPI::Image>& image);
  77. static void ReleaseImage(AZ::Data::Instance<AZ::RPI::Image>& image);
  78. protected: // member functions
  79. bool CellIndexWithinRange(int cellIndex) const;
  80. private: // types
  81. using CSpriteHashMap = AZStd::unordered_map<AZStd::string, CSprite*, stl::hash_string_caseless<AZStd::string>, stl::equality_string_caseless<AZStd::string> >;
  82. private: // member functions
  83. bool LoadFromXmlFile();
  84. void NotifyChanged();
  85. AZ_DISABLE_COPY_MOVE(CSprite);
  86. private: // data
  87. SpriteSheetCellContainer m_spriteSheetCells; //!< Stores information for each cell defined within the sprite-sheet.
  88. AZStd::string m_pathname;
  89. AZStd::string m_texturePathname;
  90. Borders m_borders;
  91. AZ::Data::Instance<AZ::RPI::Image> m_image;
  92. int m_numSpriteSheetCellTags; //!< Number of Cell child-tags in sprite XML; unfortunately needed to help with serialization.
  93. //! Texture atlas data
  94. const TextureAtlasNamespace::TextureAtlas* m_atlas;
  95. TextureAtlasNamespace::AtlasCoordinates m_atlasCoordinates;
  96. private: // static data
  97. //! Used to keep track of all loaded sprites. Sprites are refcounted.
  98. static CSpriteHashMap* s_loadedSprites;
  99. static AZStd::string s_emptyString;
  100. };