EditorMaterialComponentSerializer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <AzCore/Serialization/Json/JsonSerializationResult.h>
  9. #include <Material/EditorMaterialComponent.h>
  10. #include <Material/EditorMaterialComponentSerializer.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. AZ_CLASS_ALLOCATOR_IMPL(JsonEditorMaterialComponentSerializer, AZ::SystemAllocator);
  16. AZ::JsonSerializationResult::Result JsonEditorMaterialComponentSerializer::Load(
  17. void* outputValue,
  18. [[maybe_unused]] const AZ::Uuid& outputValueTypeId,
  19. const rapidjson::Value& inputValue,
  20. AZ::JsonDeserializerContext& context)
  21. {
  22. namespace JSR = AZ::JsonSerializationResult;
  23. AZ_Assert(
  24. azrtti_typeid<EditorMaterialComponent>() == outputValueTypeId,
  25. "Unable to deserialize EditorMaterialComponent from json because the provided type is %s.",
  26. outputValueTypeId.ToString<AZStd::string>().c_str());
  27. auto componentInstance = reinterpret_cast<EditorMaterialComponent*>(outputValue);
  28. AZ_Assert(componentInstance, "Output value for JsonEditorMaterialComponentSerializer can't be null.");
  29. JSR::ResultCode result(JSR::Tasks::ReadField);
  30. result.Combine(ContinueLoadingFromJsonObjectField(
  31. &componentInstance->m_id, azrtti_typeid<decltype(componentInstance->m_id)>(), inputValue, "Id", context));
  32. result.Combine(ContinueLoadingFromJsonObjectField(
  33. &componentInstance->m_controller, azrtti_typeid<decltype(componentInstance->m_controller)>(), inputValue, "Controller",
  34. context));
  35. result.Combine(ContinueLoadingFromJsonObjectField(
  36. &componentInstance->m_materialSlotsByLodEnabled, azrtti_typeid<decltype(componentInstance->m_materialSlotsByLodEnabled)>(),
  37. inputValue, "materialSlotsByLodEnabled", context));
  38. return context.Report(
  39. result,
  40. result.GetProcessing() != JSR::Processing::Halted ? "Successfully loaded EditorMaterialComponent information."
  41. : "Failed to load EditorMaterialComponent information.");
  42. }
  43. AZ::JsonSerializationResult::Result JsonEditorMaterialComponentSerializer::Store(
  44. rapidjson::Value& outputValue,
  45. const void* inputValue,
  46. const void* defaultValue,
  47. [[maybe_unused]] const AZ::Uuid& valueTypeId,
  48. AZ::JsonSerializerContext& context)
  49. {
  50. namespace JSR = AZ::JsonSerializationResult;
  51. AZ_Assert(
  52. azrtti_typeid<EditorMaterialComponent>() == valueTypeId,
  53. "Unable to Serialize EditorMaterialComponent because the provided type is %s.",
  54. valueTypeId.ToString<AZStd::string>().c_str());
  55. auto componentInstance = reinterpret_cast<const EditorMaterialComponent*>(inputValue);
  56. AZ_Assert(componentInstance, "Input value for JsonEditorMaterialComponentSerializer can't be null.");
  57. auto defaultComponentInstance = reinterpret_cast<const EditorMaterialComponent*>(defaultValue);
  58. JSR::ResultCode result(JSR::Tasks::WriteValue);
  59. {
  60. AZ::ScopedContextPath subPathName(context, "m_id");
  61. const auto componentId = &componentInstance->m_id;
  62. const auto defaultComponentId = defaultComponentInstance ? &defaultComponentInstance->m_id : nullptr;
  63. result.Combine(ContinueStoringToJsonObjectField(
  64. outputValue, "Id", componentId, defaultComponentId, azrtti_typeid<decltype(componentInstance->m_id)>(), context));
  65. }
  66. {
  67. AZ::ScopedContextPath subPathName(context, "Controller");
  68. const auto controller = &componentInstance->m_controller;
  69. const auto defaultController = defaultComponentInstance ? &defaultComponentInstance->m_controller : nullptr;
  70. result.Combine(ContinueStoringToJsonObjectField(
  71. outputValue, "Controller", controller, defaultController, azrtti_typeid<decltype(componentInstance->m_controller)>(),
  72. context));
  73. }
  74. {
  75. AZ::ScopedContextPath subPathName(context, "materialSlotsByLodEnabled");
  76. const auto enabled = &componentInstance->m_materialSlotsByLodEnabled;
  77. const auto defaultEnabled = defaultComponentInstance ? &defaultComponentInstance->m_materialSlotsByLodEnabled : nullptr;
  78. result.Combine(ContinueStoringToJsonObjectField(
  79. outputValue, "materialSlotsByLodEnabled", enabled, defaultEnabled,
  80. azrtti_typeid<decltype(componentInstance->m_materialSlotsByLodEnabled)>(), context));
  81. }
  82. return context.Report(
  83. result,
  84. result.GetProcessing() != JSR::Processing::Halted ? "Successfully stored EditorMaterialComponent information."
  85. : "Failed to store EditorMaterialComponent information.");
  86. }
  87. } // namespace Render
  88. } // namespace AZ