3
0

ImguiAtomSystemComponent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "ImguiAtomSystemComponent.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzFramework/Windowing/WindowBus.h>
  11. #include <Atom/Feature/ImGui/SystemBus.h>
  12. #include <Atom/RPI.Public/ViewportContext.h>
  13. #if defined(IMGUI_ENABLED)
  14. #include <ImGuiBus.h>
  15. #endif
  16. namespace AZ
  17. {
  18. namespace LYIntegration
  19. {
  20. void ImguiAtomSystemComponent::Reflect(AZ::ReflectContext* context)
  21. {
  22. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<ImguiAtomSystemComponent, AZ::Component>()
  25. ->Version(0)
  26. ;
  27. }
  28. }
  29. void ImguiAtomSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  30. {
  31. provided.push_back(AZ_CRC_CE("ImguiAtomSystemComponent"));
  32. }
  33. void ImguiAtomSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  34. {
  35. required.push_back(AZ_CRC_CE("CommonService"));
  36. }
  37. void ImguiAtomSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatbile)
  38. {
  39. incompatbile.push_back(AZ_CRC_CE("ImguiAtomSystemComponent"));
  40. }
  41. void ImguiAtomSystemComponent::Activate()
  42. {
  43. auto atomViewportRequests = AZ::RPI::ViewportContextRequests::Get();
  44. AZ_Assert(atomViewportRequests, "AtomViewportContextRequests interface not found!");
  45. const AZ::Name contextName = atomViewportRequests->GetDefaultViewportContextName();
  46. AZ::RPI::ViewportContextNotificationBus::Handler::BusConnect(contextName);
  47. m_initialized = false;
  48. InitializeViewportSizeIfNeeded();
  49. }
  50. void ImguiAtomSystemComponent::Deactivate()
  51. {
  52. AZ::RPI::ViewportContextNotificationBus::Handler::BusDisconnect();
  53. }
  54. void ImguiAtomSystemComponent::InitializeViewportSizeIfNeeded()
  55. {
  56. #if defined(IMGUI_ENABLED)
  57. if (m_initialized)
  58. {
  59. return;
  60. }
  61. auto atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  62. auto defaultViewportContext = atomViewportRequests->GetDefaultViewportContext();
  63. if (defaultViewportContext)
  64. {
  65. // If this succeeds, m_initialized will be set to true.
  66. OnViewportSizeChanged(defaultViewportContext->GetViewportSize());
  67. }
  68. #endif
  69. }
  70. void ImguiAtomSystemComponent::OnRenderTick()
  71. {
  72. #if defined(IMGUI_ENABLED)
  73. InitializeViewportSizeIfNeeded();
  74. ImGui::IImGuiManager* imguiManager = AZ::Interface<ImGui::IImGuiManager>().Get();
  75. if (imguiManager != nullptr)
  76. {
  77. ImDrawData* drawData = imguiManager->GetImguiDrawData();
  78. if (drawData != nullptr)
  79. {
  80. Render::ImGuiSystemRequestBus::Broadcast(&Render::ImGuiSystemRequests::RenderImGuiBuffersToCurrentViewport, *drawData);
  81. }
  82. }
  83. #endif
  84. }
  85. void ImguiAtomSystemComponent::OnViewportSizeChanged([[maybe_unused]] AzFramework::WindowSize size)
  86. {
  87. #if defined(IMGUI_ENABLED)
  88. ImGui::ImGuiManagerBus::Broadcast([this, size](ImGui::ImGuiManagerBus::Events* imgui)
  89. {
  90. imgui->OverrideRenderWindowSize(size.m_width, size.m_height);
  91. // ImGuiManagerListenerBus may not have been connected when this system component is activated
  92. // as ImGuiManager is not part of a system component we can require and instead just listens for ESYSTEM_EVENT_GAME_POST_INIT.
  93. // Let our ImguiAtomSystemComponent know once we successfully connect and update the viewport size.
  94. if (!m_initialized)
  95. {
  96. auto atomViewportRequests = AZ::RPI::ViewportContextRequests::Get();
  97. auto defaultViewportContext = atomViewportRequests->GetDefaultViewportContext();
  98. OnViewportDpiScalingChanged(defaultViewportContext->GetDpiScalingFactor());
  99. m_initialized = true;
  100. }
  101. });
  102. #endif //define(IMGUI_ENABLED)
  103. }
  104. void ImguiAtomSystemComponent::OnViewportDpiScalingChanged([[maybe_unused]] float dpiScale)
  105. {
  106. #if defined(IMGUI_ENABLED)
  107. ImGui::ImGuiManagerBus::Broadcast(&ImGui::ImGuiManagerBus::Events::SetDpiScalingFactor, dpiScale);
  108. #endif //define(IMGUI_ENABLED)
  109. }
  110. }
  111. }