GeomNodesSystem.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "GeomNodesSystem.h"
  2. #include "Editor/UI/PropertyFileSelect.h"
  3. #include "Editor/UI/ValidationHandler.h"
  4. #include "Editor/UI/GeomNodesValidator.h"
  5. #include <Editor/Common/GNAPI.h>
  6. #include <Editor/EBus/IpcHandlerBus.h>
  7. namespace GeomNodes
  8. {
  9. long IpcHandlerCB(AZ::u64 id, const char* data, AZ::u64 length)
  10. {
  11. /*AZStd::string msg;
  12. msg.assign(data, length);
  13. AZ_Printf("GeomNodesSystem", "id: %llu data: %s length: %llu", id, msg.c_str(), length);*/
  14. Ipc::IpcHandlerNotificationBus::Event(
  15. AZ::EntityId(id), &Ipc::IpcHandlerNotificationBus::Events::OnMessageReceived, reinterpret_cast<const AZ::u8*>(data), length);
  16. return 0;
  17. }
  18. GeomNodesSystem::GeomNodesSystem(AZStd::unique_ptr<GNSettingsRegistryManager> registryManager)
  19. : m_registryManager(AZStd::move(registryManager))
  20. , m_propertyHandlers()
  21. , m_validator(AZStd::make_unique<Validator>())
  22. , m_validationHandler(AZStd::make_unique<ValidationHandler>())
  23. {
  24. }
  25. GeomNodesSystem::~GeomNodesSystem()
  26. {
  27. Shutdown();
  28. }
  29. void GeomNodesSystem::Initialize(const GNConfiguration* config)
  30. {
  31. if (m_state == State::Initialized)
  32. {
  33. AZ_Warning("GeomNodesSystem", false, "GeomNodes system already initialized, Shutdown must be called first");
  34. return;
  35. }
  36. m_systemConfig = *config;
  37. API::Init(SERVER_ID, IpcHandlerCB);
  38. RegisterHandlersAndBuses();
  39. m_state = State::Initialized;
  40. m_initializeEvent.Signal(&m_systemConfig);
  41. }
  42. void GeomNodesSystem::Shutdown()
  43. {
  44. if (m_state != State::Initialized)
  45. {
  46. return;
  47. }
  48. UnregisterHandlersAndBuses();
  49. API::Uninitialize();
  50. m_state = State::Shutdown;
  51. }
  52. AZStd::string_view GeomNodesSystem::GetBlenderPath()
  53. {
  54. return m_systemConfig.m_blenderPath;
  55. }
  56. const GNConfiguration* GeomNodesSystem::GetConfiguration() const
  57. {
  58. return &m_systemConfig;
  59. }
  60. const GNConfiguration& GeomNodesSystem::GetSystemConfiguration() const
  61. {
  62. return m_systemConfig;
  63. }
  64. void GeomNodesSystem::SetLastPath(const AZStd::string& lastPath)
  65. {
  66. m_systemConfig.m_lastFilePath = lastPath;
  67. const GNSettingsRegistryManager& settingsRegManager = GetSettingsRegistryManager();
  68. auto saveCallback = [](const GNConfiguration& config, GNSettingsRegistryManager::Result result)
  69. {
  70. AZ_Warning("GeomNodes", result == GNSettingsRegistryManager::Result::Success, "Unable to save the GeomNodes configuration. Any changes have not been applied.");
  71. if (result == GNSettingsRegistryManager::Result::Success)
  72. {
  73. if (auto* gnSystem = GetGNSystem())
  74. {
  75. gnSystem->UpdateConfiguration(&config);
  76. }
  77. }
  78. };
  79. settingsRegManager.SaveSystemConfiguration(m_systemConfig, saveCallback);
  80. }
  81. AZStd::string GeomNodesSystem::GetLastPath()
  82. {
  83. return m_systemConfig.m_lastFilePath;
  84. }
  85. void GeomNodesSystem::UpdateConfiguration(const GNConfiguration* newConfig)
  86. {
  87. if(m_systemConfig != *newConfig)
  88. {
  89. m_systemConfig = (*newConfig);
  90. m_configChangeEvent.Signal(newConfig);
  91. }
  92. }
  93. const GNSettingsRegistryManager& GeomNodesSystem::GetSettingsRegistryManager() const
  94. {
  95. return *m_registryManager;
  96. }
  97. FunctorValidator* GeomNodesSystem::GetValidator(FunctorValidator::FunctorType functor)
  98. {
  99. return m_validator->GetQValidator(functor);
  100. }
  101. void GeomNodesSystem::TrackValidator(FunctorValidator* validator)
  102. {
  103. m_validator->TrackThisValidator(validator);
  104. }
  105. void GeomNodesSystem::RegisterHandlersAndBuses()
  106. {
  107. m_propertyHandlers.push_back(PropertyFuncValBrowseEditHandler::Register(m_validationHandler.get()));
  108. m_propertyHandlers.push_back(PropertyFileSelectHandler::Register(m_validationHandler.get()));
  109. ValidatorBus::Handler::BusConnect();
  110. //TODO: setup our IPC system. Since this is the gem it will be the server.
  111. // only one handler. Then messages will have the sender id(EntityID) which will be used when sending
  112. // the messages via the EBUS. when a component is registered as an EBUS handler they will receive
  113. // the correct messages.
  114. }
  115. void GeomNodesSystem::UnregisterHandlersAndBuses()
  116. {
  117. ValidatorBus::Handler::BusDisconnect();
  118. for (AzToolsFramework::PropertyHandlerBase* handler : m_propertyHandlers)
  119. {
  120. AzToolsFramework::PropertyTypeRegistrationMessages::Bus::Broadcast(
  121. &AzToolsFramework::PropertyTypeRegistrationMessages::Bus::Handler::UnregisterPropertyType,
  122. handler);
  123. delete handler;
  124. }
  125. }
  126. GeomNodesSystem* GetGNSystem()
  127. {
  128. return azdynamic_cast<GeomNodesSystem*>(AZ::Interface<GeomNodes::GNSystemInterface>::Get());
  129. }
  130. }