GNParamContext.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #include <AzCore/RTTI/ReflectContext.h>
  3. #include <AzCore/JSON/document.h>
  4. #include <AzCore/JSON/rapidjson.h>
  5. namespace GeomNodes
  6. {
  7. class GNProperty;
  8. class GNParamContextImpl;
  9. class GNParamContext;
  10. namespace Field
  11. {
  12. static constexpr char Initialized[] = "Initialized";
  13. static constexpr char Heartbeat[] = "Heartbeat";
  14. static constexpr char ObjectNames[] = "ObjectNames";
  15. static constexpr char Objects[] = "Objects";
  16. static constexpr char Object[] = "Object";
  17. static constexpr char SHMOpen[] = "SHMOpen";
  18. static constexpr char SHMClose[] = "SHMClose";
  19. static constexpr char MapId[] = "MapId";
  20. static constexpr char Export[] = "Export";
  21. static constexpr char Error[] = "Error";
  22. static constexpr char Params[] = "Params";
  23. static constexpr char Materials[] = "Materials";
  24. static constexpr char Id[] = "Id";
  25. static constexpr char Name[] = "Name";
  26. static constexpr char Type[] = "Type";
  27. static constexpr char DefaultValue[] = "DefaultValue";
  28. static constexpr char Value[] = "Value";
  29. static constexpr char MinValue[] = "MinValue";
  30. static constexpr char MaxValue[] = "MaxValue";
  31. static constexpr char FBXPath[] = "FBXPath";
  32. }
  33. enum class ParamType : AZ::u8
  34. {
  35. Bool,
  36. Int,
  37. Value,
  38. String,
  39. StringComboBox,
  40. Unknown
  41. };
  42. const char* GetEnumString(ParamType value);
  43. ParamType GetTypeFromString(const char* value);
  44. template<class T>
  45. struct GNValue
  46. {
  47. typedef typename AZStd::remove_const<typename AZStd::remove_reference<typename AZStd::remove_pointer<T>::type>::type>::type ValueType;
  48. };
  49. template<>
  50. struct GNValue<bool>
  51. {
  52. static const bool isNativeValueType = true; // We use native type for internal representation
  53. static bool Read(const rapidjson::Value& val);
  54. };
  55. template<>
  56. struct GNValue<const char*>
  57. {
  58. static const bool isNativeValueType = true; // We use native type for internal representation
  59. static const char* Read(const rapidjson::Value& val);
  60. };
  61. template<>
  62. struct GNValue<int>
  63. {
  64. static const bool isNativeValueType = true; // We use native type for internal representation
  65. static int Read(const rapidjson::Value& val);
  66. };
  67. template<>
  68. struct GNValue<double>
  69. {
  70. static const bool isNativeValueType = true; // We use native type for internal representation
  71. static double Read(const rapidjson::Value& val);
  72. };
  73. struct GNPropertyGroup
  74. {
  75. AZ_TYPE_INFO(GNPropertyGroup, "{439E8395-77B5-4BC6-94D6-5A0F51DBE9FD}");
  76. AZStd::string m_name;
  77. AZStd::vector<GNProperty*> m_properties;
  78. AZStd::vector<GNPropertyGroup> m_groups;
  79. // Get the pointer to the specified group in m_groups. Returns nullptr if not found.
  80. GNPropertyGroup* GetGroup(const char* groupName);
  81. // Get the pointer to the specified property in m_properties. Returns nullptr if not found.
  82. GNProperty* GetProperty(const char* propertyName);
  83. // Generate JSON string of all properties/parameters. NOTE: only select details are included.
  84. AZStd::string GetProperties();
  85. // Remove all properties and groups
  86. void Clear();
  87. GNPropertyGroup() = default;
  88. ~GNPropertyGroup();
  89. GNPropertyGroup(const GNPropertyGroup& rhs) = delete;
  90. GNPropertyGroup& operator=(GNPropertyGroup&) = delete;
  91. public:
  92. GNPropertyGroup(GNPropertyGroup&& rhs)
  93. {
  94. *this = AZStd::move(rhs);
  95. }
  96. GNPropertyGroup& operator=(GNPropertyGroup&& rhs);
  97. };
  98. class GNParamDataContext
  99. {
  100. friend GNParamContext;
  101. public:
  102. AZ_TYPE_INFO(GNParamDataContext, "{61ED88BA-210A-458B-A5E5-C71C05C05411}");
  103. GNParamDataContext()
  104. {
  105. }
  106. bool IsNil(int index) const;
  107. bool IsBoolean(int index) const;
  108. bool IsInt(int index) const;
  109. bool IsValue(int index) const;
  110. bool IsString(int index) const;
  111. template<class T>
  112. bool ReadValue(T& valueRef, const char* key) const;
  113. void SetParamObject(const rapidjson::Value* value)
  114. {
  115. m_curParamObj = value;
  116. }
  117. void ClearParamObject()
  118. {
  119. m_curParamObj = nullptr;
  120. }
  121. void SetReadOnlyPointer(bool* pReadOnly)
  122. {
  123. m_pReadOnly = pReadOnly;
  124. }
  125. bool* GetReadOnlyPointer()
  126. {
  127. return m_pReadOnly;
  128. }
  129. const char* GetParamName();
  130. ParamType GetParamType();
  131. protected:
  132. const rapidjson::Value* m_curParamObj;
  133. bool* m_pReadOnly;
  134. };
  135. template<class T>
  136. inline bool GNParamDataContext::ReadValue(T& valueRef, const char* key) const
  137. {
  138. if (m_curParamObj == nullptr || !(*m_curParamObj).HasMember(key))
  139. return false;
  140. valueRef = GNValue<T>::Read((*m_curParamObj)[key]);
  141. return true;
  142. }
  143. class GNParamContextImpl
  144. {
  145. public:
  146. typedef GNProperty* (*ParamTypeFactory)(GNParamDataContext& context, int valueIndex, const char* name);
  147. AZ_CLASS_ALLOCATOR(GNParamContextImpl, AZ::SystemAllocator, 0);
  148. GNParamContextImpl();
  149. ~GNParamContextImpl() = default;
  150. GNProperty* ConstructGNParam(GNParamDataContext& gndc, ParamType pType, const char* name);
  151. AZStd::vector<ParamTypeFactory> m_paramFactories;
  152. };
  153. class GNParamContext
  154. {
  155. friend class EditorGeomNodesComponent;
  156. public:
  157. AZ_CLASS_ALLOCATOR(GNParamContext, AZ::SystemAllocator, 0);
  158. AZ_TYPE_INFO(GeomNodes::GNParamContext, "{AA9713B7-70F1-43CB-9F95-5BEC9F44F556}");
  159. GNParamContext();
  160. ~GNParamContext();
  161. static void Reflect(AZ::ReflectContext* reflection);
  162. GNProperty* ConstructGNParam(GNParamDataContext& gndc, ParamType pType, const char* name);
  163. protected:
  164. GNPropertyGroup m_group;
  165. GNParamContextImpl* m_impl;
  166. };
  167. } // namespace GeomNodes