3
0

TextureAtlasImpl.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/Serialization/SerializeContext.h>
  11. #include <AzCore/Asset/AssetCommon.h>
  12. #include <AzFramework/StringFunc/StringFunc.h>
  13. #include "TextureAtlas/TextureAtlas.h"
  14. #include "TextureAtlas/TextureAtlasBus.h"
  15. #include <Atom/RPI.Reflect/Image/Image.h>
  16. #include <AtomCore/Instance/Instance.h>
  17. namespace TextureAtlasNamespace
  18. {
  19. struct equal_to_case_insensitive
  20. {
  21. AZ_TYPE_INFO(equal_to_case_insensitive, "{92DE03B1-B84F-4DEB-905D-CBE41DD6939D}");
  22. bool operator()(const AZStd::string& left, const AZStd::string& right) const
  23. {
  24. return AzFramework::StringFunc::Equal(left.data(), right.data());
  25. }
  26. };
  27. struct hash_case_insensitive : public AZStd::hash<AZStd::string>
  28. {
  29. AZ_TYPE_INFO(hash_case_insensitive, "{FE0F4349-D80D-4286-8874-733966A32B29}");
  30. inline size_t operator()(const AZStd::string& value) const
  31. {
  32. AZStd::string lowerStr = value;
  33. AZStd::to_lower(lowerStr.begin(), lowerStr.end());
  34. return hash::operator()(lowerStr);
  35. }
  36. };
  37. class TextureAtlasImpl: public TextureAtlas
  38. {
  39. public:
  40. AZ_CLASS_ALLOCATOR(TextureAtlasImpl, AZ::SystemAllocator);
  41. AZ_TYPE_INFO(TextureAtlasImpl, "{2CA51C61-1B5F-4480-A257-F28D8944AA35}");
  42. TextureAtlasImpl();
  43. TextureAtlasImpl(AtlasCoordinateSets handles);
  44. ~TextureAtlasImpl();
  45. static bool TextureAtlasVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& rootElement);
  46. static void Reflect(AZ::ReflectContext* context);
  47. //! Retrieve a coordinate set from the Atlas by its handle
  48. AtlasCoordinates GetAtlasCoordinates(const AZStd::string& handle) const override;
  49. //! Links this atlas to an image pointer
  50. void SetTexture(AZ::Data::Instance<AZ::RPI::Image> image) override;
  51. //! Returns the image linked to this atlas
  52. AZ::Data::Instance<AZ::RPI::Image> GetTexture() const override;
  53. //! Replaces the mappings of this Texture Atlas Object, with the source's mappings
  54. void OverwriteMappings(TextureAtlasImpl* source);
  55. //! Returns the width of the atlas
  56. int GetWidth() const override { return m_width; }
  57. //! Sets the width of the atlas
  58. void SetWidth(int value) { m_width = value; }
  59. //! Returns the height of the atlas
  60. int GetHeight() const override { return m_height; }
  61. //! Sets the height of the atlas
  62. void SetHeight(int value) { m_height = value; }
  63. private:
  64. AZStd::unordered_map<AZStd::string, AtlasCoordinates, hash_case_insensitive, equal_to_case_insensitive> m_data;
  65. AZ::Data::Instance<AZ::RPI::Image> m_image;
  66. int m_width;
  67. int m_height;
  68. };
  69. } // namespace TextureAtlasNamespace