TextureAtlasSystemComponent.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <AzCore/Component/Component.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include "TextureAtlas/TextureAtlasBus.h"
  12. #include "TextureAtlas/TextureAtlas.h"
  13. #include "TextureAtlas/TextureAtlasNotificationBus.h"
  14. #include <AzFramework/Asset/AssetCatalogBus.h>
  15. namespace TextureAtlasNamespace
  16. {
  17. class TextureAtlasSystemComponent : public AZ::Component, protected TextureAtlasRequestBus::Handler, public AzFramework::AssetCatalogEventBus::Handler
  18. {
  19. public:
  20. AZ_COMPONENT(TextureAtlasSystemComponent, "{436E8E5A-76CA-458D-8DAD-835C30D8C41B}");
  21. static void Reflect(AZ::ReflectContext* context);
  22. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  23. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  24. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  25. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  26. protected:
  27. ////////////////////////////////////////////////////////////////////////
  28. // TextureAtlasRequestBus interface implementation
  29. //! Saves a texture atlas to file
  30. void SaveAtlasToFile(const AZStd::string& outputPath,
  31. AtlasCoordinateSets& handles, int width, int height) override;
  32. //! Tells the TextureAtlas system to load an Atlas and return a pointer for the atlas
  33. TextureAtlas* LoadAtlas(const AZStd::string& filePath) override;
  34. //! Returns a pointer to the first Atlas that contains the image, or nullptr if no atlas contains it
  35. TextureAtlas* FindAtlasContainingImage(const AZStd::string& filePath) override;
  36. //! Tells the TextureAtlas system to unload an Atlas
  37. void UnloadAtlas(TextureAtlas* atlas) override;
  38. ////////////////////////////////////////////////////////////////////////
  39. ////////////////////////////////////////////////////////////////////////
  40. // AZ::Component interface implementation
  41. void Init() override;
  42. void Activate() override;
  43. void Deactivate() override;
  44. ////////////////////////////////////////////////////////////////////////
  45. void OnCatalogAssetChanged(const AZ::Data::AssetId& assetId) override;
  46. //! A struct that aids in the management of texture atlases
  47. struct AtlasInfo
  48. {
  49. TextureAtlas* m_atlas;
  50. AZStd::string m_path;
  51. int m_refs;
  52. AZ::Data::AssetId m_atlasAssetId;
  53. //! A simple constructor that generates the AtlasInfo based on its parameters in a one to one fashion.
  54. AtlasInfo(TextureAtlas* atlas, AZStd::string path)
  55. {
  56. m_atlas = atlas;
  57. m_path = path;
  58. m_refs = 0;
  59. }
  60. AtlasInfo()
  61. {
  62. m_atlas = nullptr;
  63. m_path = "";
  64. m_refs = 0;
  65. }
  66. };
  67. private:
  68. AZStd::unordered_map<AZStd::string, AtlasInfo> m_atlases;
  69. };
  70. }