TextureAtlasImpl.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "TextureAtlasImpl.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzFramework/API/ApplicationAPI.h>
  11. namespace TextureAtlasNamespace
  12. {
  13. const static char* s_CoordinatePairsName = "Coordinate Pairs";
  14. TextureAtlasImpl::TextureAtlasImpl()
  15. {
  16. m_image = nullptr;
  17. }
  18. TextureAtlasImpl::~TextureAtlasImpl() {}
  19. TextureAtlasImpl::TextureAtlasImpl(AtlasCoordinateSets handles)
  20. {
  21. for (int i = 0; i < handles.size(); ++i)
  22. {
  23. this->m_data[handles[i].first] = handles[i].second;
  24. }
  25. m_image = nullptr;
  26. }
  27. bool TextureAtlasImpl::TextureAtlasVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& rootElement)
  28. {
  29. if (rootElement.GetVersion() < 2)
  30. {
  31. AZStd::unordered_map<AZStd::string, AtlasCoordinates> oldData;
  32. if (!rootElement.GetChildData(AZ_CRC(s_CoordinatePairsName), oldData))
  33. {
  34. AZ_Error("TextureAtlas", false, "Failed to find old %s unordered_map element on version %u", s_CoordinatePairsName, rootElement.GetVersion());
  35. return false;
  36. }
  37. AZStd::unordered_map<AZStd::string, AtlasCoordinates, hash_case_insensitive, equal_to_case_insensitive> newData{ oldData.begin(), oldData.end() };
  38. rootElement.RemoveElementByName(AZ_CRC(s_CoordinatePairsName));
  39. rootElement.AddElementWithData(context, s_CoordinatePairsName, newData);
  40. }
  41. return true;
  42. }
  43. // Reflect The coordinates and the coordinate format
  44. void TextureAtlasImpl::Reflect(AZ::ReflectContext* context)
  45. {
  46. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  47. {
  48. serialize->ClassDeprecate("SimpleAssetReference_TextureAtlasAsset", AZ::Uuid("{6F612FE6-A054-4E49-830C-0288F3C79A52}"),
  49. [](AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& rootElement)
  50. {
  51. AZStd::vector<AZ::SerializeContext::DataElementNode> childNodeElements;
  52. for (int index = 0; index < rootElement.GetNumSubElements(); ++index)
  53. {
  54. childNodeElements.push_back(rootElement.GetSubElement(index));
  55. }
  56. // Convert the rootElement now, the existing child DataElmentNodes are now removed
  57. rootElement.Convert<AzFramework::SimpleAssetReference<TextureAtlasAsset>>(context);
  58. for (AZ::SerializeContext::DataElementNode& childNodeElement : childNodeElements)
  59. {
  60. rootElement.AddElement(AZStd::move(childNodeElement));
  61. }
  62. return true;
  63. });
  64. // Need to serialize the old AZStd::unordered_map<AZStd::string, AtlasCoordinates> type
  65. serialize->RegisterGenericType<AZStd::unordered_map<AZStd::string, AtlasCoordinates>>();
  66. serialize->Class<TextureAtlasImpl>()->Version(2, &TextureAtlasVersionConverter)
  67. ->Field(s_CoordinatePairsName, &TextureAtlasImpl::m_data)
  68. ->Field("Width", &TextureAtlasImpl::m_width)
  69. ->Field("Height", &TextureAtlasImpl::m_height);
  70. AzFramework::SimpleAssetReference<TextureAtlasAsset>::Register(*serialize);
  71. }
  72. AtlasCoordinates::Reflect(context);
  73. }
  74. // Coordinates reflect their internal properties
  75. void AtlasCoordinates::Reflect(AZ::ReflectContext* context)
  76. {
  77. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  78. {
  79. serialize->Class<AtlasCoordinates>()
  80. ->Version(1)
  81. ->Field("Left", &AtlasCoordinates::m_left)
  82. ->Field("Top", &AtlasCoordinates::m_top)
  83. ->Field("Width", &AtlasCoordinates::m_width)
  84. ->Field("Height", &AtlasCoordinates::m_height);
  85. }
  86. }
  87. // Retrieves the value that corresponds to a given handle in the atlas
  88. AtlasCoordinates TextureAtlasImpl::GetAtlasCoordinates(const AZStd::string& handle) const
  89. {
  90. AZStd::string path = handle;
  91. path = path.substr(0, path.find_last_of('.'));
  92. // Use an iterator to check if the key is being used in the hash table
  93. auto iterator = m_data.find(path);
  94. if (iterator != m_data.end())
  95. {
  96. return iterator->second;
  97. }
  98. else
  99. {
  100. return AtlasCoordinates(-1, -1, -1, -1);
  101. }
  102. }
  103. // Links this atlas to an image pointer
  104. void TextureAtlasImpl::SetTexture(AZ::Data::Instance<AZ::RPI::Image> image)
  105. {
  106. // We don't need to delete the old value because the pointer is handled elsewhere
  107. m_image = image;
  108. }
  109. // Returns the image linked to this atlas
  110. AZ::Data::Instance<AZ::RPI::Image> TextureAtlasImpl::GetTexture() const
  111. {
  112. return m_image;
  113. }
  114. // Internal to gem function for overwriting parameters
  115. void TextureAtlasImpl::OverwriteMappings(TextureAtlasImpl* source)
  116. {
  117. m_data.clear();
  118. m_data = source->m_data;
  119. m_width = source->m_width;
  120. m_height = source->m_height;
  121. }
  122. }