EditorSystemComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "EditorSystemComponent.h"
  9. #include <AzCore/Interface/Interface.h>
  10. #include <AzFramework/Physics/PhysicsSystem.h>
  11. #include <AzFramework/Physics/SystemBus.h>
  12. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  13. #include <PhysX/Debug/PhysXDebugInterface.h>
  14. #include <PhysX/Debug/PhysXDebugConfiguration.h>
  15. #include <PhysX/Configuration/PhysXConfiguration.h>
  16. #include <IEditor.h>
  17. namespace PhysXDebug
  18. {
  19. void EditorSystemComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<EditorSystemComponent, AZ::Component>()
  24. ->Version(1)
  25. ;
  26. }
  27. }
  28. // This will be called when the IEditor instance is ready
  29. void EditorSystemComponent::NotifyRegisterViews()
  30. {
  31. RegisterForEditorEvents();
  32. }
  33. void EditorSystemComponent::OnCrySystemShutdown(ISystem&)
  34. {
  35. UnregisterForEditorEvents();
  36. }
  37. void EditorSystemComponent::OnEditorNotifyEvent(const EEditorNotifyEvent editorEvent)
  38. {
  39. switch (editorEvent)
  40. {
  41. case eNotify_OnEndNewScene:
  42. case eNotify_OnEndLoad:
  43. AutoConnectPVD();
  44. break;
  45. default:
  46. // Intentionally left blank.
  47. break;
  48. }
  49. }
  50. void EditorSystemComponent::Activate()
  51. {
  52. AzToolsFramework::EditorEvents::Bus::Handler::BusConnect();
  53. AzToolsFramework::Components::EditorComponentBase::Activate();
  54. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  55. CrySystemEventBus::Handler::BusConnect();
  56. if (auto* physXDebug = AZ::Interface<PhysX::Debug::PhysXDebugInterface>::Get())
  57. {
  58. m_pvdConfigurationChangeHandler = PhysX::Debug::PvdConfigurationChangedEvent::Handler(
  59. [this](const PhysX::Debug::PvdConfiguration& config)
  60. {
  61. this->OnPvdConfigurationChanged(config);
  62. });
  63. m_colliderProximityVisualizationChangedEventHandler = PhysX::Debug::ColliderProximityVisualizationChangedEvent::Handler(
  64. [this](const PhysX::Debug::ColliderProximityVisualization& visualizationData)
  65. {
  66. this->OnColliderProximityVisualizationChanged(visualizationData);
  67. });
  68. physXDebug->RegisterPvdConfigurationChangedEvent(m_pvdConfigurationChangeHandler);
  69. physXDebug->RegisterColliderProximityVisualizationChangedEvent(m_colliderProximityVisualizationChangedEventHandler);
  70. }
  71. }
  72. void EditorSystemComponent::Deactivate()
  73. {
  74. m_pvdConfigurationChangeHandler.Disconnect();
  75. m_colliderProximityVisualizationChangedEventHandler.Disconnect();
  76. CrySystemEventBus::Handler::BusDisconnect();
  77. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  78. AzToolsFramework::Components::EditorComponentBase::Deactivate();
  79. AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect();
  80. }
  81. void EditorSystemComponent::RegisterForEditorEvents()
  82. {
  83. IEditor* editor = nullptr;
  84. AzToolsFramework::EditorRequests::Bus::BroadcastResult(editor, &AzToolsFramework::EditorRequests::GetEditor);
  85. if (editor)
  86. {
  87. editor->RegisterNotifyListener(this);
  88. }
  89. }
  90. void EditorSystemComponent::UnregisterForEditorEvents()
  91. {
  92. IEditor* editor = nullptr;
  93. AzToolsFramework::EditorRequests::Bus::BroadcastResult(editor, &AzToolsFramework::EditorRequests::GetEditor);
  94. if (editor)
  95. {
  96. editor->UnregisterNotifyListener(this);
  97. }
  98. }
  99. void EditorSystemComponent::OnPvdConfigurationChanged(const PhysX::Debug::PvdConfiguration& config)
  100. {
  101. auto* debug = AZ::Interface<PhysX::Debug::PhysXDebugInterface>::Get();
  102. if (!debug)
  103. {
  104. return;
  105. }
  106. if (config.IsAutoConnectionEditorMode())
  107. {
  108. debug->ConnectToPvd();
  109. }
  110. else
  111. {
  112. debug->DisconnectFromPvd();
  113. }
  114. }
  115. void EditorSystemComponent::OnStartPlayInEditorBegin()
  116. {
  117. auto* debug = AZ::Interface<PhysX::Debug::PhysXDebugInterface>::Get();
  118. if (!debug)
  119. {
  120. return;
  121. }
  122. if (debug->GetPhysXPvdConfiguration().IsAutoConnectionGameMode())
  123. {
  124. debug->ConnectToPvd();
  125. }
  126. }
  127. void EditorSystemComponent::OnStopPlayInEditor()
  128. {
  129. auto* debug = AZ::Interface<PhysX::Debug::PhysXDebugInterface>::Get();
  130. if (!debug)
  131. {
  132. return;
  133. }
  134. const PhysX::Debug::PvdConfiguration& pvdConfig = debug->GetPhysXPvdConfiguration();
  135. if (pvdConfig.IsAutoConnectionGameMode())
  136. {
  137. debug->DisconnectFromPvd();
  138. }
  139. if (pvdConfig.IsAutoConnectionEditorMode() && pvdConfig.m_reconnect)
  140. {
  141. debug->ConnectToPvd();
  142. }
  143. }
  144. void EditorSystemComponent::AutoConnectPVD()
  145. {
  146. auto* debug = AZ::Interface<PhysX::Debug::PhysXDebugInterface>::Get();
  147. if (!debug)
  148. {
  149. return;
  150. }
  151. if (debug->GetPhysXPvdConfiguration().IsAutoConnectionEditorMode())
  152. {
  153. debug->ConnectToPvd();
  154. }
  155. }
  156. void EditorSystemComponent::OnColliderProximityVisualizationChanged(const PhysX::Debug::ColliderProximityVisualization& visualizationData)
  157. {
  158. if (visualizationData.m_enabled &&
  159. m_cameraPositionCache.GetDistance(visualizationData.m_cameraPosition) > visualizationData.m_radius * 0.5f)
  160. {
  161. m_cameraPositionCache = visualizationData.m_cameraPosition;
  162. }
  163. }
  164. }