From 9f934a6f63e6fb880b26e995e5c976d0c1ca72bf Mon Sep 17 00:00:00 2001 From: amzn-sean <75276488+amzn-sean@users.noreply.github.com> Date: Mon, 21 Jun 2021 16:25:45 +0100 Subject: [PATCH] add Draw Helpers changed notification + physx draw helpers listens to it (#1456) --- .../Viewport/ViewportMessages.h | 13 ++++ Code/Sandbox/Editor/ViewportTitleDlg.cpp | 8 +- Gems/PhysX/Code/Editor/DebugDraw.cpp | 78 ++++++++++++++++--- Gems/PhysX/Code/Editor/DebugDraw.h | 20 ++++- 4 files changed, 104 insertions(+), 15 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h index 85250f2a32..b4d0342c44 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h @@ -210,6 +210,19 @@ namespace AzToolsFramework //! Type to inherit to implement ViewportInteractionRequests. using ViewportInteractionRequestBus = AZ::EBus; + //! An interface to notify when changes to viewport settings have happened. + class ViewportSettingNotifications + { + public: + virtual void OnGridSnappingChanged([[maybe_unused]] bool enabled) {} + virtual void OnDrawHelpersChanged([[maybe_unused]] bool enabled) {} + + protected: + ViewportSettingNotifications() = default; + }; + + using ViewportSettingsNotificationBus = AZ::EBus; + //! Requests to freeze the Viewport Input //! Added to prevent a bug with the legacy CryEngine Viewport code that would //! keep doing raycast tests even when no level is loaded, causing a crash. diff --git a/Code/Sandbox/Editor/ViewportTitleDlg.cpp b/Code/Sandbox/Editor/ViewportTitleDlg.cpp index dc4815eb6a..491d31a2ff 100644 --- a/Code/Sandbox/Editor/ViewportTitleDlg.cpp +++ b/Code/Sandbox/Editor/ViewportTitleDlg.cpp @@ -45,6 +45,7 @@ #include #include +#include AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING #include "ui_ViewportTitleDlg.h" @@ -57,13 +58,16 @@ inline namespace Helpers { void ToggleHelpers() { - GetIEditor()->GetDisplaySettings()->DisplayHelpers(!GetIEditor()->GetDisplaySettings()->IsDisplayHelpers()); + const bool newValue = !GetIEditor()->GetDisplaySettings()->IsDisplayHelpers(); + GetIEditor()->GetDisplaySettings()->DisplayHelpers(newValue); GetIEditor()->Notify(eNotify_OnDisplayRenderUpdate); - if (GetIEditor()->GetDisplaySettings()->IsDisplayHelpers() == false) + if (newValue == false) { GetIEditor()->GetObjectManager()->SendEvent(EVENT_HIDE_HELPER); } + AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Broadcast( + &AzToolsFramework::ViewportInteraction::ViewportSettingNotifications::OnDrawHelpersChanged, newValue); } bool IsHelpersShown() diff --git a/Gems/PhysX/Code/Editor/DebugDraw.cpp b/Gems/PhysX/Code/Editor/DebugDraw.cpp index 829b776e28..1f1c24b9ad 100644 --- a/Gems/PhysX/Code/Editor/DebugDraw.cpp +++ b/Gems/PhysX/Code/Editor/DebugDraw.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,15 @@ namespace PhysX return false; } + bool IsDrawColliderReadOnly() + { + bool helpersVisible = false; + AzToolsFramework::EditorRequestBus::BroadcastResult(helpersVisible, + &AzToolsFramework::EditorRequests::DisplayHelpersVisible); + // if helpers are visible, draw colliders is NOT read only and can be changed. + return !helpersVisible; + } + static void BuildAABBVerts(const AZ::Aabb& aabb, AZStd::vector& verts, AZStd::vector& points, @@ -145,28 +155,42 @@ namespace PhysX "PhysX Collider Debug Draw", "Manages global and per-collider debug draw settings and logic") ->DataElement(AZ::Edit::UIHandlers::CheckBox, &Collider::m_locallyEnabled, "Draw collider", "Shows the geometry for the collider in the viewport") - ->Attribute(AZ::Edit::Attributes::CheckboxTooltip, - "If set, the geometry of this collider is visible in the viewport") - ->Attribute(AZ::Edit::Attributes::Visibility, - VisibilityFunc{ []() { return IsGlobalColliderDebugCheck(GlobalCollisionDebugState::Manual); } }) + ->Attribute(AZ::Edit::Attributes::CheckboxTooltip, + "If set, the geometry of this collider is visible in the viewport. 'Draw Helpers' needs to be enabled to use.") + ->Attribute(AZ::Edit::Attributes::Visibility, + VisibilityFunc{ []() { return IsGlobalColliderDebugCheck(GlobalCollisionDebugState::Manual); } }) + ->Attribute(AZ::Edit::Attributes::ReadOnly, &IsDrawColliderReadOnly) ->DataElement(AZ::Edit::UIHandlers::Button, &Collider::m_globalButtonState, "Draw collider", "Shows the geometry for the collider in the viewport") - ->Attribute(AZ::Edit::Attributes::ButtonText, "Global override") - ->Attribute(AZ::Edit::Attributes::ButtonTooltip, - "A global setting is overriding this property (to disable the override, " - "set the Global Collision Debug setting to \"Set manually\" in the PhysX Configuration)") - ->Attribute(AZ::Edit::Attributes::Visibility, - VisibilityFunc{ []() { return !IsGlobalColliderDebugCheck(GlobalCollisionDebugState::Manual); } }) - ->Attribute(AZ::Edit::Attributes::ChangeNotify, &OpenPhysXSettingsWindow) + ->Attribute(AZ::Edit::Attributes::ButtonText, "Global override") + ->Attribute(AZ::Edit::Attributes::ButtonTooltip, + "A global setting is overriding this property (to disable the override, " + "set the Global Collision Debug setting to \"Set manually\" in the PhysX Configuration)." + "'Draw Helpers' needs to be enabled to use.") + ->Attribute(AZ::Edit::Attributes::Visibility, + VisibilityFunc{ []() { return !IsGlobalColliderDebugCheck(GlobalCollisionDebugState::Manual); } }) + ->Attribute(AZ::Edit::Attributes::ChangeNotify, &OpenPhysXSettingsWindow) + ->Attribute(AZ::Edit::Attributes::ReadOnly, &IsDrawColliderReadOnly) ; } } } + Collider::Collider() + : m_debugDisplayDataChangedEvent( + [this]([[maybe_unused]] const PhysX::Debug::DebugDisplayData& data) + { + this->RefreshTreeHelper(); + }) + { + + } + void Collider::Connect(AZ::EntityId entityId) { m_entityId = entityId; AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(m_entityId); + AzToolsFramework::EntitySelectionEvents::Bus::Handler::BusConnect(m_entityId); } void Collider::SetDisplayCallback(const DisplayCallback* callback) @@ -176,6 +200,11 @@ namespace PhysX void Collider::Disconnect() { + if (AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler::BusIsConnected()) + { + AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler::BusDisconnect(); + } + AzToolsFramework::EntitySelectionEvents::Bus::Handler::BusDisconnect(); AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect(); m_displayCallback = nullptr; m_entityId = AZ::EntityId(); @@ -731,6 +760,33 @@ namespace PhysX } } + void Collider::OnDrawHelpersChanged([[maybe_unused]] bool enabled) + { + RefreshTreeHelper(); + } + + void Collider::OnSelected() + { + AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler::BusConnect( + AzFramework::g_defaultSceneEntityDebugDisplayId); + if (auto* physXDebug = AZ::Interface::Get()) + { + physXDebug->RegisterDebugDisplayDataChangedEvent(m_debugDisplayDataChangedEvent); + } + } + + void Collider::OnDeselected() + { + AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler::BusDisconnect(); + m_debugDisplayDataChangedEvent.Disconnect(); + } + + void Collider::RefreshTreeHelper() + { + AzToolsFramework::ToolsApplicationEvents::Bus::Broadcast( + &AzToolsFramework::ToolsApplicationEvents::Bus::Events::InvalidatePropertyDisplay, AzToolsFramework::Refresh_AttributesAndValues); + } + AZStd::string Collider::GetEntityName() const { AZStd::string entityName; diff --git a/Gems/PhysX/Code/Editor/DebugDraw.h b/Gems/PhysX/Code/Editor/DebugDraw.h index c43634717a..34b3b57667 100644 --- a/Gems/PhysX/Code/Editor/DebugDraw.h +++ b/Gems/PhysX/Code/Editor/DebugDraw.h @@ -15,8 +15,11 @@ #include #include #include +#include +#include #include #include +#include namespace PhysX { @@ -40,13 +43,15 @@ namespace PhysX class Collider : protected AzFramework::EntityDebugDisplayEventBus::Handler + , protected AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler + , protected AzToolsFramework::EntitySelectionEvents::Bus::Handler { public: AZ_CLASS_ALLOCATOR(Collider, AZ::SystemAllocator, 0); AZ_RTTI(Collider, "{7DE9CA01-DF1E-4D72-BBF4-76C9136BE6A2}"); static void Reflect(AZ::ReflectContext* context); - Collider() = default; + Collider(); void Connect(AZ::EntityId entityId); void SetDisplayCallback(const DisplayCallback* callback); @@ -109,11 +114,20 @@ namespace PhysX const AZStd::vector& GetIndices(AZ::u32 geomIndex) const; protected: - // AzFramework::EntityDebugDisplayEventBus + // AzFramework::EntityDebugDisplayEventBus overrides ... void DisplayEntityViewport( const AzFramework::ViewportInfo& viewportInfo, AzFramework::DebugDisplayRequests& debugDisplay) override; + // AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler overrides ... + void OnDrawHelpersChanged(bool enabled) override; + + // AzToolsFramework::EntitySelectionEvents::Bus::Handler overrides ... + void OnSelected() override; + void OnDeselected() override; + + void RefreshTreeHelper(); + // Internal mesh drawing subroutines void DrawTriangleMesh( AzFramework::DebugDisplayRequests& debugDisplay, const Physics::ColliderConfiguration& colliderConfig, AZ::u32 geomIndex, @@ -143,6 +157,8 @@ namespace PhysX }; mutable AZStd::vector m_geometry; + + PhysX::Debug::DebugDisplayDataChangedEvent::Handler m_debugDisplayDataChangedEvent; }; } // namespace DebugDraw } // namespace PhysX