EntityTestbed.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <AzCore/Memory/AllocationRecords.h>
  9. #include <AzCore/IO/Streamer/StreamerComponent.h>
  10. #include <AzCore/Asset/AssetManagerComponent.h>
  11. #include <AzCore/Serialization/Utils.h>
  12. #include <AzCore/Component/EntityUtils.h>
  13. #include <AzCore/PlatformIncl.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. #include <AzFramework/Entity/EntityContextBus.h>
  16. #include <AzFramework/Entity/EntityContext.h>
  17. #include <AzFramework/IO/LocalFileIO.h>
  18. #include <AzFramework/Application/Application.h>
  19. #include <AzFramework/Asset/AssetCatalogComponent.h>
  20. #include <AzFramework/Asset/AssetCatalogBus.h>
  21. //#include <AzToolsFramework/UI/Outliner/OutlinerWidget.hxx>
  22. #include <AzToolsFramework/UI/PropertyEditor/PropertyManagerComponent.h>
  23. #include <AzToolsFramework/UI/PropertyEditor/EntityPropertyEditor.hxx>
  24. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  25. #include <AzToolsFramework/Application/ToolsApplication.h>
  26. #include <QtWidgets/QMainWindow>
  27. #include <QtWidgets/QApplication>
  28. #include <QtWidgets/QVBoxLayout>
  29. #include <QtWidgets/QPushButton>
  30. #include <QtWidgets/QFileDialog>
  31. #include <QtCore/QTimer>
  32. #pragma once
  33. namespace UnitTest
  34. {
  35. using namespace AZ;
  36. class EntityTestbed
  37. : public LeakDetectionFixture
  38. , public QObject
  39. {
  40. public:
  41. class TestbedApplication
  42. : public AzToolsFramework::ToolsApplication
  43. {
  44. public:
  45. AZ_CLASS_ALLOCATOR(TestbedApplication, AZ::SystemAllocator);
  46. TestbedApplication(EntityTestbed& testbed)
  47. : m_testbed(testbed) {}
  48. EntityTestbed& m_testbed;
  49. };
  50. QTimer* m_tickBusTimer = nullptr;
  51. TestbedApplication* m_componentApplication = nullptr;
  52. AZ::Entity* m_systemEntity = nullptr;
  53. QApplication* m_qtApplication = nullptr;
  54. QWidget* m_window = nullptr;
  55. //AzToolsFramework::OutlinerWidget* m_outliner = nullptr;
  56. AzToolsFramework::EntityPropertyEditor* m_propertyEditor = nullptr;
  57. AZ::u32 m_entityCounter = 0;
  58. AZ::IO::LocalFileIO m_localFileIO;
  59. virtual ~EntityTestbed()
  60. {
  61. if (m_tickBusTimer)
  62. {
  63. m_tickBusTimer->stop();
  64. delete m_tickBusTimer;
  65. m_tickBusTimer = nullptr;
  66. }
  67. Destroy();
  68. }
  69. virtual void OnSetup() {}
  70. virtual void OnAddButtons(QHBoxLayout& layout) { (void)layout; }
  71. virtual void OnEntityAdded(AZ::Entity& entity) { (void)entity; }
  72. virtual void OnEntityRemoved(AZ::Entity& entity) { (void)entity; }
  73. virtual void OnReflect(AZ::SerializeContext& context, AZ::Entity& systemEntity) { (void)context; (void)systemEntity; }
  74. virtual void OnDestroy() {}
  75. void Run(int argc = 0, char** argv = nullptr)
  76. {
  77. SetupComponentApplication();
  78. m_qtApplication = new QApplication(argc, argv);
  79. m_tickBusTimer = new QTimer(this);
  80. m_qtApplication->connect(m_tickBusTimer, &QTimer::timeout,
  81. []()
  82. {
  83. AZ::TickBus::ExecuteQueuedEvents();
  84. AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.3f, AZ::ScriptTimePoint());
  85. }
  86. );
  87. m_tickBusTimer->start();
  88. SetupUI();
  89. OnSetup();
  90. m_window->show();
  91. m_qtApplication->exec();
  92. }
  93. void SetupUI()
  94. {
  95. m_window = new QWidget();
  96. //m_outliner = aznew AzToolsFramework::OutlinerWidget(nullptr);
  97. m_propertyEditor = aznew AzToolsFramework::EntityPropertyEditor(nullptr);
  98. AZ::SerializeContext* serializeContext = nullptr;
  99. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  100. m_window->setMinimumHeight(600);
  101. m_propertyEditor->setMinimumWidth(600);
  102. //m_outliner->setMinimumWidth(100);
  103. QVBoxLayout* leftLayout = new QVBoxLayout();
  104. QHBoxLayout* outlinerLayout = new QHBoxLayout();
  105. QHBoxLayout* outlinerButtonLayout = new QHBoxLayout();
  106. //outlinerLayout->addWidget(m_outliner);
  107. leftLayout->addLayout(outlinerLayout);
  108. leftLayout->addLayout(outlinerButtonLayout);
  109. QVBoxLayout* rightLayout = new QVBoxLayout();
  110. QHBoxLayout* propertyLayout = new QHBoxLayout();
  111. QHBoxLayout* propertyButtonLayout = new QHBoxLayout();
  112. propertyLayout->addWidget(m_propertyEditor);
  113. rightLayout->addLayout(propertyLayout);
  114. rightLayout->addLayout(propertyButtonLayout);
  115. QHBoxLayout* mainLayout = new QHBoxLayout();
  116. m_window->setLayout(mainLayout);
  117. mainLayout->addLayout(leftLayout, 1);
  118. mainLayout->addLayout(rightLayout, 3);
  119. // Add default buttons.
  120. QPushButton* addEntity = new QPushButton(QString("Create"));
  121. QPushButton* deleteEntities = new QPushButton(QString("Delete"));
  122. outlinerButtonLayout->addWidget(addEntity);
  123. outlinerButtonLayout->addWidget(deleteEntities);
  124. m_qtApplication->connect(addEntity, &QPushButton::pressed, [ this ]() { this->AddEntity(); });
  125. m_qtApplication->connect(deleteEntities, &QPushButton::pressed, [ this ]() { this->DeleteSelected(); });
  126. // Test-specific buttons.
  127. OnAddButtons(*outlinerButtonLayout);
  128. }
  129. void SetupComponentApplication()
  130. {
  131. AZ::ComponentApplication::Descriptor desc;
  132. desc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
  133. desc.m_useExistingAllocator = true;
  134. m_componentApplication = aznew TestbedApplication(*this);
  135. AZ::IO::FileIOBase::SetInstance(&m_localFileIO);
  136. m_componentApplication->Start(desc);
  137. AZ::SerializeContext* serializeContext = m_componentApplication->GetSerializeContext();
  138. serializeContext->CreateEditContext();
  139. AzToolsFramework::Components::PropertyManagerComponent::CreateDescriptor();
  140. const char* dir = m_componentApplication->GetExecutableFolder();
  141. m_localFileIO.SetAlias("@products@", dir);
  142. m_localFileIO.SetAlias("@projectroot@", dir);
  143. }
  144. void Destroy()
  145. {
  146. OnDestroy();
  147. //delete m_outliner;
  148. delete m_propertyEditor;
  149. delete m_window;
  150. delete m_qtApplication;
  151. delete m_componentApplication;
  152. //m_outliner = nullptr;
  153. m_propertyEditor = nullptr;
  154. m_window = nullptr;
  155. m_qtApplication = nullptr;
  156. m_componentApplication = nullptr;
  157. if (AZ::Data::AssetManager::IsReady())
  158. {
  159. AZ::Data::AssetManager::Destroy();
  160. }
  161. AZ::IO::FileIOBase::SetInstance(nullptr);
  162. }
  163. void AddEntity()
  164. {
  165. AZStd::string entityName = AZStd::string::format("Entity%u", m_entityCounter);
  166. AZ::EntityId entityId;
  167. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(entityId, &AzToolsFramework::EditorEntityContextRequests::CreateNewEditorEntity, entityName.c_str());
  168. ++m_entityCounter;
  169. AZ::Entity* entity = nullptr;
  170. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  171. entity->Deactivate();
  172. OnEntityAdded(*entity);
  173. entity->Activate();
  174. }
  175. void DeleteSelected()
  176. {
  177. AzToolsFramework::ToolsApplicationRequests::Bus::Broadcast(
  178. &AzToolsFramework::ToolsApplicationRequests::Bus::Events::DeleteSelected);
  179. }
  180. void ResetRoot()
  181. {
  182. AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
  183. &AzToolsFramework::EditorEntityContextRequestBus::Events::ResetEditorContext);
  184. }
  185. };
  186. } // namespace UnitTest;