LyShineExamplesSerialize.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "LyShineExamplesSerialize.h"
  9. #include <LyShine/UiSerializeHelpers.h>
  10. #include <LyShineExamples/UiCustomImageBus.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. // NAMESPACE FUNCTIONS
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. namespace LyShineExamplesSerialize
  16. {
  17. //////////////////////////////////////////////////////////////////////////
  18. void UVCoordsScriptConstructor(UiCustomImageInterface::UVRect* thisPtr, AZ::ScriptDataContext& dc)
  19. {
  20. int numArgs = dc.GetNumArguments();
  21. const int noArgsGiven = 0;
  22. const int allArgsGiven = 4;
  23. switch (numArgs)
  24. {
  25. case noArgsGiven:
  26. {
  27. *thisPtr = UiCustomImageInterface::UVRect();
  28. }
  29. break;
  30. case allArgsGiven:
  31. {
  32. if (dc.IsNumber(0) && dc.IsNumber(1) && dc.IsNumber(2) && dc.IsNumber(3))
  33. {
  34. float left = 0;
  35. float top = 0;
  36. float right = 0;
  37. float bottom = 0;
  38. dc.ReadArg(0, left);
  39. dc.ReadArg(1, top);
  40. dc.ReadArg(2, right);
  41. dc.ReadArg(3, bottom);
  42. *thisPtr = UiCustomImageInterface::UVRect(left, top, right, bottom);
  43. }
  44. else
  45. {
  46. dc.GetScriptContext()->Error(AZ::ScriptContext::ErrorType::Error, true, "When providing 4 arguments to UVCoords(), all must be numbers!");
  47. }
  48. }
  49. break;
  50. default:
  51. {
  52. dc.GetScriptContext()->Error(AZ::ScriptContext::ErrorType::Error, true, "UVCoords() accepts only 0 or 4 arguments, not %d!", numArgs);
  53. }
  54. break;
  55. }
  56. }
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////
  58. void ReflectTypes(AZ::ReflectContext* context)
  59. {
  60. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  61. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  62. // Serialize the UVs struct
  63. {
  64. if (serializeContext)
  65. {
  66. serializeContext->Class<UiCustomImageInterface::UVRect>()->
  67. Field("left", &UiCustomImageInterface::UVRect::m_left)->
  68. Field("top", &UiCustomImageInterface::UVRect::m_top)->
  69. Field("right", &UiCustomImageInterface::UVRect::m_right)->
  70. Field("bottom", &UiCustomImageInterface::UVRect::m_bottom);
  71. AZ::EditContext* ec = serializeContext->GetEditContext();
  72. if (ec)
  73. {
  74. auto editInfo = ec->Class<UiCustomImageInterface::UVRect>(0, "");
  75. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "UVRect")
  76. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  77. editInfo->DataElement(0, &UiCustomImageInterface::UVRect::m_left, "Left", "The lower X UV coordinate.");
  78. editInfo->DataElement(0, &UiCustomImageInterface::UVRect::m_top, "Top", "The higher Y UV coordinate.");
  79. editInfo->DataElement(0, &UiCustomImageInterface::UVRect::m_right, "Right", "The higher X UV coordinate.");
  80. editInfo->DataElement(0, &UiCustomImageInterface::UVRect::m_bottom, "Bottom", "The lower Y UV coordinate.");
  81. }
  82. }
  83. if (behaviorContext)
  84. {
  85. behaviorContext->Class<UiCustomImageInterface::UVRect>("UVCoords")
  86. ->Constructor<>()
  87. ->Constructor<float, float, float, float>()
  88. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  89. ->Attribute(AZ::Script::Attributes::ConstructorOverride, &UVCoordsScriptConstructor)
  90. ->Property("left", BehaviorValueProperty(&UiCustomImageInterface::UVRect::m_left))
  91. ->Property("top", BehaviorValueProperty(&UiCustomImageInterface::UVRect::m_top))
  92. ->Property("right", BehaviorValueProperty(&UiCustomImageInterface::UVRect::m_right))
  93. ->Property("bottom", BehaviorValueProperty(&UiCustomImageInterface::UVRect::m_bottom))
  94. ->Method("SetLeft", [](UiCustomImageInterface::UVRect* thisPtr, float left) { thisPtr->m_left = left; })
  95. ->Method("SetTop", [](UiCustomImageInterface::UVRect* thisPtr, float top) { thisPtr->m_top = top; })
  96. ->Method("SetRight", [](UiCustomImageInterface::UVRect* thisPtr, float right) { thisPtr->m_right = right; })
  97. ->Method("SetBottom", [](UiCustomImageInterface::UVRect* thisPtr, float bottom) { thisPtr->m_bottom= bottom; })
  98. ->Method("SetUVCoords", [](UiCustomImageInterface::UVRect* thisPtr, float left, float top, float right, float bottom)
  99. {
  100. thisPtr->m_left = left;
  101. thisPtr->m_top = top;
  102. thisPtr->m_right = right;
  103. thisPtr->m_bottom = bottom;
  104. });
  105. }
  106. }
  107. }
  108. }