WhiteBoxToolApiReflection.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "EditorWhiteBoxComponentModeBus.h"
  9. #include "WhiteBoxToolApiReflection.h"
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <WhiteBox/EditorWhiteBoxComponentBus.h>
  13. #include <WhiteBox/WhiteBoxComponentBus.h>
  14. #include <WhiteBox/WhiteBoxToolApi.h>
  15. namespace WhiteBox
  16. {
  17. // placeholder type for white box free functions
  18. struct WhiteBoxUtil
  19. {
  20. };
  21. WhiteBoxMesh* WhiteBoxMeshFromHandle(const WhiteBoxMeshHandle& whiteBoxMeshHandle)
  22. {
  23. return reinterpret_cast<WhiteBox::WhiteBoxMesh*>(whiteBoxMeshHandle.m_whiteBoxMeshAddress);
  24. }
  25. template<typename Tag>
  26. void GenericHandleReflect(AZ::BehaviorContext* behaviorContext, const char* name)
  27. {
  28. behaviorContext->Class<typename WhiteBox::GenericHandle<Tag>>(name)
  29. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  30. ->Attribute(AZ::Script::Attributes::Module, "whitebox.api")
  31. ->template Constructor<int>()
  32. ->Method("IsValid", &GenericHandle<Tag>::IsValid)
  33. ->Method("Index", &GenericHandle<Tag>::Index);
  34. }
  35. void Reflect(AZ::ReflectContext* context)
  36. {
  37. // SerializeContext registration is needed to convert python lists into AZStd::vector
  38. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  39. {
  40. serializeContext->Class<Api::FaceHandle>();
  41. serializeContext->RegisterGenericType<Api::FaceHandles>();
  42. serializeContext->Class<Api::FaceVertHandles>();
  43. serializeContext->RegisterGenericType<Api::FaceVertHandlesList>();
  44. serializeContext->Class<Api::VertexHandle>();
  45. serializeContext->RegisterGenericType<Api::VertexHandles>();
  46. serializeContext->RegisterGenericType<AZStd::array<Api::VertexHandle, 3>>();
  47. }
  48. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  49. {
  50. GenericHandleReflect<Api::VertexHandleTag>(behaviorContext, "VertexHandle");
  51. GenericHandleReflect<Api::FaceHandleTag>(behaviorContext, "FaceHandle");
  52. GenericHandleReflect<Api::EdgeHandleTag>(behaviorContext, "EdgeHandle");
  53. GenericHandleReflect<Api::HalfedgeHandleTag>(behaviorContext, "HalfedgeHandle");
  54. behaviorContext->Class<Api::FaceVertHandles>("FaceVertHandles")
  55. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  56. ->Attribute(AZ::Script::Attributes::Module, "whitebox.api")
  57. ->Property("VertexHandles", BehaviorValueProperty(&Api::FaceVertHandles::m_vertexHandles));
  58. behaviorContext->Class<Api::EdgeTypes>()
  59. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  60. ->Attribute(AZ::Script::Attributes::Module, "whitebox.api")
  61. ->Property("Mesh", BehaviorValueProperty(&Api::EdgeTypes::m_mesh))
  62. ->Property("User", BehaviorValueProperty(&Api::EdgeTypes::m_user));
  63. behaviorContext->Class<Api::PolygonHandle>("PolygonHandle")
  64. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  65. ->Attribute(AZ::Script::Attributes::Module, "whitebox.api")
  66. ->Property("FaceHandles", BehaviorValueProperty(&Api::PolygonHandle::m_faceHandles));
  67. behaviorContext->Class<WhiteBoxMeshHandle>("WhiteBoxMeshHandle")
  68. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  69. ->Attribute(AZ::Script::Attributes::Module, "whitebox.api")
  70. ->Method("IsValid", [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  71. {
  72. return WhiteBoxMeshFromHandle(*whiteBoxMeshHandle) != nullptr;
  73. })
  74. ->Method(
  75. "InitializeAsUnitCube",
  76. [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  77. {
  78. return Api::InitializeAsUnitCube(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle));
  79. })
  80. ->Method(
  81. "MeshFaceCount",
  82. [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  83. {
  84. return Api::MeshFaceCount(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle));
  85. })
  86. ->Method(
  87. "MeshVertexCount",
  88. [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  89. {
  90. return Api::MeshVertexCount(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle));
  91. })
  92. ->Method(
  93. "FacePolygonHandle",
  94. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const Api::FaceHandle faceHandle)
  95. {
  96. return Api::FacePolygonHandle(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), faceHandle);
  97. })
  98. ->Method(
  99. "FaceVertexHandles",
  100. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const Api::FaceHandle faceHandle)
  101. {
  102. return Api::FaceVertexHandles(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), faceHandle);
  103. })
  104. ->Method(
  105. "AddVertex",
  106. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const AZ::Vector3& vertex)
  107. {
  108. return Api::AddVertex(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), vertex);
  109. })
  110. ->Method(
  111. "VertexPosition",
  112. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, Api::VertexHandle vertexHandle)
  113. {
  114. return Api::VertexPosition(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), vertexHandle);
  115. })
  116. ->Method(
  117. "VertexPositions",
  118. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const Api::VertexHandles& vertexHandles)
  119. {
  120. return Api::VertexPositions(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), vertexHandles);
  121. })
  122. ->Method(
  123. "TranslatePolygonAppend",
  124. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const Api::PolygonHandle& polygonHandle,
  125. const float distance)
  126. {
  127. return Api::TranslatePolygonAppend(
  128. *WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), polygonHandle, distance);
  129. })
  130. ->Method(
  131. "TranslatePolygon",
  132. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const Api::PolygonHandle& polygonHandle,
  133. const float distance)
  134. {
  135. return Api::TranslatePolygon(
  136. *WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), polygonHandle, distance);
  137. })
  138. ->Method(
  139. "CalculateNormals",
  140. [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  141. {
  142. return Api::CalculateNormals(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle));
  143. })
  144. ->Method(
  145. "CalculatePlanarUVs",
  146. [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  147. {
  148. return Api::CalculatePlanarUVs(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle));
  149. })
  150. ->Method(
  151. "HideEdge",
  152. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, Api::EdgeHandle edgeHandle)
  153. {
  154. return Api::HideEdge(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), edgeHandle);
  155. })
  156. ->Method(
  157. "FlipEdge",
  158. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, Api::EdgeHandle edgeHandle)
  159. {
  160. return Api::FlipEdge(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), edgeHandle);
  161. })
  162. ->Method(
  163. "AddPolygon",
  164. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, const Api::FaceVertHandlesList& faceVertHandles)
  165. {
  166. return Api::AddPolygon(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), faceVertHandles);
  167. })
  168. ->Method(
  169. "AddTriPolygon",
  170. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, Api::VertexHandle v0, Api::VertexHandle v1,
  171. Api::VertexHandle v2)
  172. {
  173. return Api::AddTriPolygon(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), v0, v1, v2);
  174. })
  175. ->Method(
  176. "AddQuadPolygon",
  177. [](WhiteBoxMeshHandle* whiteBoxMeshHandle, Api::VertexHandle v0, Api::VertexHandle v1,
  178. Api::VertexHandle v2, Api::VertexHandle v3)
  179. {
  180. return Api::AddQuadPolygon(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle), v0, v1, v2, v3);
  181. })
  182. ->Method(
  183. "Clear",
  184. [](WhiteBoxMeshHandle* whiteBoxMeshHandle)
  185. {
  186. return Api::Clear(*WhiteBoxMeshFromHandle(*whiteBoxMeshHandle));
  187. });
  188. behaviorContext->EnumProperty<(int)DefaultShapeType::Cube>("CUBE")
  189. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  190. ->Attribute(AZ::Script::Attributes::Module, "whitebox.enum");
  191. behaviorContext->EnumProperty<(int)DefaultShapeType::Tetrahedron>("TETRAHEDRON")
  192. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  193. ->Attribute(AZ::Script::Attributes::Module, "whitebox.enum");
  194. behaviorContext->EnumProperty<(int)DefaultShapeType::Icosahedron>("ICOSAHEDRON")
  195. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  196. ->Attribute(AZ::Script::Attributes::Module, "whitebox.enum");
  197. behaviorContext->EnumProperty<(int)DefaultShapeType::Cylinder>("CYLINDER")
  198. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  199. ->Attribute(AZ::Script::Attributes::Module, "whitebox.enum");
  200. behaviorContext->EnumProperty<(int)DefaultShapeType::Sphere>("SPHERE")
  201. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  202. ->Attribute(AZ::Script::Attributes::Module, "whitebox.enum");
  203. behaviorContext->EBus<EditorWhiteBoxComponentModeRequestBus>("EditorWhiteBoxComponentModeRequestBus")
  204. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  205. ->Attribute(AZ::Script::Attributes::Module, "whitebox.request.bus")
  206. ->Event(
  207. "MarkWhiteBoxIntersectionDataDirty",
  208. &EditorWhiteBoxComponentModeRequestBus::Events::MarkWhiteBoxIntersectionDataDirty);
  209. behaviorContext->EBus<EditorWhiteBoxComponentRequestBus>("EditorWhiteBoxComponentRequestBus")
  210. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  211. ->Attribute(AZ::Script::Attributes::Module, "whitebox.request.bus")
  212. ->Event("GetWhiteBoxMeshHandle", &EditorWhiteBoxComponentRequestBus::Events::GetWhiteBoxMeshHandle)
  213. ->Event("SerializeWhiteBox", &EditorWhiteBoxComponentRequestBus::Events::SerializeWhiteBox)
  214. ->Event("SetDefaultShape", &EditorWhiteBoxComponentRequestBus::Events::SetDefaultShape);
  215. behaviorContext->EBus<EditorWhiteBoxComponentNotificationBus>("EditorWhiteBoxComponentNotificationBus")
  216. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  217. ->Attribute(AZ::Script::Attributes::Module, "whitebox.notification.bus")
  218. ->Event(
  219. "OnWhiteBoxMeshModified", &EditorWhiteBoxComponentNotificationBus::Events::OnWhiteBoxMeshModified);
  220. behaviorContext->EBus<WhiteBoxComponentRequestBus>("WhiteBoxComponentRequestBus")
  221. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  222. ->Attribute(AZ::Script::Attributes::Module, "whitebox.request.bus")
  223. ->Event("WhiteBoxIsVisible", &WhiteBoxComponentRequestBus::Events::WhiteBoxIsVisible);
  224. behaviorContext->Class<WhiteBoxUtil>("util")
  225. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  226. ->Attribute(AZ::Script::Attributes::Module, "whitebox.api")
  227. ->Method(
  228. "MakeFaceVertHandles",
  229. [](Api::VertexHandle v0, Api::VertexHandle v1, Api::VertexHandle v2)
  230. {
  231. return Api::FaceVertHandles{v0, v1, v2};
  232. })
  233. ->Method(
  234. "MakeEntityComponentIdPair",
  235. [](AZ::u64 entityId, AZ::u64 componentId)
  236. {
  237. return AZ::EntityComponentIdPair(AZ::EntityId(entityId), componentId);
  238. });
  239. }
  240. }
  241. } // namespace WhiteBox
  242. namespace AZ
  243. {
  244. AZ_TYPE_INFO_SPECIALIZE(WhiteBox::WhiteBoxMeshHandle, "{95A2A7F0-C758-494E-BE1E-F673D13E812D}")
  245. AZ_TYPE_INFO_SPECIALIZE(
  246. WhiteBox::GenericHandle<WhiteBox::Api::VertexHandleTag>, "{708A5B3C-E377-40CE-9572-BEB64C849D40}")
  247. AZ_TYPE_INFO_SPECIALIZE(
  248. WhiteBox::GenericHandle<WhiteBox::Api::FaceHandleTag>, "{950009BC-8991-4749-9D5C-08C62AF34E7B}")
  249. AZ_TYPE_INFO_SPECIALIZE(
  250. WhiteBox::GenericHandle<WhiteBox::Api::EdgeHandleTag>, "{2169BFE7-8676-4572-B57E-494577059FB5}")
  251. AZ_TYPE_INFO_SPECIALIZE(
  252. WhiteBox::GenericHandle<WhiteBox::Api::HalfedgeHandleTag>, "{50AD7640-2A57-4311-B3DE-7BB08B1B70E5}")
  253. AZ_TYPE_INFO_SPECIALIZE(WhiteBox::Api::PolygonHandle, "{CE09B0D7-3076-4EAC-ADA7-7418A31EE9AE}")
  254. AZ_TYPE_INFO_SPECIALIZE(WhiteBox::Api::EdgeTypes, "{15581F2B-E80B-4264-AE26-659B5D214552}")
  255. AZ_TYPE_INFO_SPECIALIZE(WhiteBox::Api::FaceVertHandles, "{F6B9150B-CC89-48A2-AB89-D18740CC6FA2}")
  256. AZ_TYPE_INFO_SPECIALIZE(WhiteBox::WhiteBoxUtil, "{8D46CA40-1B9A-440E-A9A3-0CDD5773BB4A}")
  257. } // namespace AZ