2
0

ComponentEntityEditorPlugin.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "ComponentEntityEditorPlugin.h"
  9. #include <LyViewPaneNames.h>
  10. #include "UI/QComponentEntityEditorMainWindow.h"
  11. #include "UI/QComponentEntityEditorOutlinerWindow.h"
  12. #include "UI/ComponentPalette/ComponentPaletteSettings.h"
  13. #include <AzCore/Component/ComponentApplicationBus.h>
  14. #include <AzCore/Component/EntityUtils.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/std/containers/vector.h>
  17. #include <AzFramework/API/ApplicationAPI.h>
  18. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  19. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  20. #include "SandboxIntegration.h"
  21. namespace ComponentEntityEditorPluginInternal
  22. {
  23. void RegisterSandboxObjects()
  24. {
  25. AZ::SerializeContext* serializeContext = nullptr;
  26. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  27. AZ_Assert(serializeContext, "Serialization context not available");
  28. if (serializeContext)
  29. {
  30. ComponentPaletteSettings::Reflect(serializeContext);
  31. }
  32. }
  33. void UnregisterSandboxObjects()
  34. {
  35. }
  36. void CheckComponentDeclarations()
  37. {
  38. AZ::SerializeContext* serializeContext = nullptr;
  39. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  40. if (!serializeContext)
  41. {
  42. return;
  43. }
  44. // Catch the common mistake of reflecting a Component to SerializeContext
  45. // without declaring how it inherits from AZ::Component.
  46. // Collect violators so we can list them all in one message, rather than raising N popups.
  47. AZStd::vector<AZ::ComponentDescriptor*> componentsLackingBaseClass;
  48. AZ::EBusAggregateResults<AZ::ComponentDescriptor*> allComponentDescriptors;
  49. AZ::ComponentDescriptorBus::BroadcastResult(allComponentDescriptors, &AZ::ComponentDescriptorBus::Events::GetDescriptor);
  50. for (AZ::ComponentDescriptor* componentDescriptor : allComponentDescriptors.values)
  51. {
  52. const AZ::TypeId& componentTypeId = componentDescriptor->GetUuid();
  53. const AZ::TypeId& typeOfAZComponent = azrtti_typeid<AZ::Component>();
  54. if (serializeContext->FindClassData(componentTypeId))
  55. {
  56. if (!AZ::EntityUtils::CheckIfClassIsDeprecated(serializeContext, componentTypeId)
  57. && !AZ::EntityUtils::CheckDeclaresSerializeBaseClass(serializeContext, typeOfAZComponent, componentTypeId))
  58. {
  59. componentsLackingBaseClass.push_back(componentDescriptor);
  60. }
  61. }
  62. }
  63. AZStd::string message;
  64. for (AZ::ComponentDescriptor* componentDescriptor : componentsLackingBaseClass)
  65. {
  66. message.append(AZStd::string::format("- %s %s\n",
  67. componentDescriptor->GetName(),
  68. componentDescriptor->GetUuid().ToString<AZStd::string>().c_str()));
  69. }
  70. if (!message.empty())
  71. {
  72. message.insert(0, "Programmer error:\nClasses deriving from AZ::Component are not declaring their base class to SerializeContext.\n"
  73. "This will cause unexpected behavior such as components shifting around, or duplicating themselves.\n"
  74. "Affected components:\n");
  75. message.append("\nReflection code should look something like this:\n"
  76. "serializeContext->Class<MyComponent, AZ::Component, ... (other base classes, if any) ...>()"
  77. "\nMake sure the Reflect function is called for all base classes as well.");
  78. // this happens during startup, and its a programmer error - so during startup, make it an error, so it shows as a pretty noisy
  79. // popup box. Its important that programmers fix this, before they submit their code, so that data corruption / data loss does
  80. // not occur.
  81. AZ_Error("Serialize", false, message.c_str());
  82. }
  83. }
  84. } // end namespace ComponentEntityEditorPluginInternal
  85. void ComponentPaletteSettings::Reflect(AZ::ReflectContext* context)
  86. {
  87. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  88. if (serializeContext)
  89. {
  90. serializeContext->Class<ComponentPaletteSettings>()
  91. ->Version(1)
  92. ->Field("m_favorites", &ComponentPaletteSettings::m_favorites)
  93. ;
  94. }
  95. }
  96. ComponentEntityEditorPlugin::ComponentEntityEditorPlugin([[maybe_unused]] IEditor* editor)
  97. : m_registered(false)
  98. {
  99. m_appListener = new SandboxIntegrationManager();
  100. m_appListener->Setup();
  101. using namespace AzToolsFramework;
  102. ViewPaneOptions inspectorOptions;
  103. inspectorOptions.canHaveMultipleInstances = true;
  104. inspectorOptions.preferedDockingArea = Qt::RightDockWidgetArea;
  105. // Override the default behavior for component mode enter/exit and imgui enter/exit
  106. // so that we don't automatically disable and enable the entire Entity Inspector. This will be handled separately per-component.
  107. inspectorOptions.isDisabledInComponentMode = false;
  108. inspectorOptions.isDisabledInImGuiMode = false;
  109. RegisterViewPane<QComponentEntityEditorInspectorWindow>(
  110. LyViewPane::Inspector,
  111. LyViewPane::CategoryTools,
  112. inspectorOptions);
  113. ViewPaneOptions pinnedInspectorOptions;
  114. pinnedInspectorOptions.canHaveMultipleInstances = true;
  115. pinnedInspectorOptions.preferedDockingArea = Qt::NoDockWidgetArea;
  116. pinnedInspectorOptions.paneRect = QRect(50, 50, 400, 700);
  117. pinnedInspectorOptions.showInMenu = false;
  118. // Override the default behavior for component mode enter/exit and imgui enter/exit
  119. // so that we don't automatically disable and enable the entire Pinned Entity Inspector. This will be handled separately per-component.
  120. pinnedInspectorOptions.isDisabledInComponentMode = false;
  121. pinnedInspectorOptions.isDisabledInImGuiMode = false;
  122. RegisterViewPane<QComponentEntityEditorInspectorWindow>(
  123. LyViewPane::EntityInspectorPinned,
  124. LyViewPane::CategoryTools,
  125. pinnedInspectorOptions);
  126. // Add the Outliner to the Tools Menu
  127. ViewPaneOptions outlinerOptions;
  128. outlinerOptions.canHaveMultipleInstances = true;
  129. outlinerOptions.preferedDockingArea = Qt::LeftDockWidgetArea;
  130. // Override the default behavior for component mode enter/exit and imgui enter/exit
  131. // so that we don't automatically disable and enable the Entity Outliner. This will be handled separately.
  132. outlinerOptions.isDisabledInComponentMode = false;
  133. outlinerOptions.isDisabledInImGuiMode = false;
  134. RegisterViewPane<QEntityOutlinerWindow>(
  135. LyViewPane::EntityOutliner,
  136. LyViewPane::CategoryTools,
  137. outlinerOptions);
  138. ComponentEntityEditorPluginInternal::RegisterSandboxObjects();
  139. // Check for common mistakes in component declarations
  140. ComponentEntityEditorPluginInternal::CheckComponentDeclarations();
  141. m_registered = true;
  142. }
  143. void ComponentEntityEditorPlugin::Release()
  144. {
  145. if (m_registered)
  146. {
  147. using namespace AzToolsFramework;
  148. UnregisterViewPane(LyViewPane::Inspector);
  149. UnregisterViewPane(LyViewPane::EntityOutliner);
  150. UnregisterViewPane(LyViewPane::EntityInspectorPinned);
  151. ComponentEntityEditorPluginInternal::UnregisterSandboxObjects();
  152. }
  153. m_appListener->Teardown();
  154. delete m_appListener;
  155. delete this;
  156. }