LyShineEditorSystemComponent.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "LyShineEditorSystemComponent.h"
  9. #include "EditorWindow.h"
  10. #include "PropertyHandlerCanvasAsset.h"
  11. // UI_ANIMATION_REVISIT, added includes so that we can register the UI Animation system on startup
  12. #include "Animation/UiAnimViewSequenceManager.h"
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Serialization/EditContext.h>
  15. #include <AzCore/std/string/wildcard.h>
  16. #include <AzToolsFramework/API/ViewPaneOptions.h>
  17. #include <LyViewPaneNames.h>
  18. #include <QScreen>
  19. #include <QApplication>
  20. namespace LyShineEditor
  21. {
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////
  23. void LyShineEditorSystemComponent::Reflect(AZ::ReflectContext* context)
  24. {
  25. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  26. {
  27. serialize->Class<LyShineEditorSystemComponent, AZ::Component>()
  28. ->Version(1)
  29. ;
  30. if (AZ::EditContext* ec = serialize->GetEditContext())
  31. {
  32. auto editInfo = ec->Class<LyShineEditorSystemComponent>("UI Canvas Editor", "UI Canvas Editor System Component");
  33. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  34. ->Attribute(AZ::Edit::Attributes::Category, "UI")
  35. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  36. ;
  37. }
  38. }
  39. }
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////
  41. void LyShineEditorSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  42. {
  43. provided.push_back(AZ_CRC_CE("UiCanvasEditorService"));
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////
  46. void LyShineEditorSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  47. {
  48. incompatible.push_back(AZ_CRC_CE("UiCanvasEditorService"));
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////
  51. void LyShineEditorSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  52. {
  53. required.push_back(AZ_CRC_CE("LyShineService"));
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////
  56. void LyShineEditorSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  57. {
  58. (void)dependent;
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////
  61. LyShineEditorSystemComponent::LyShineEditorSystemComponent()
  62. {
  63. }
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////
  65. LyShineEditorSystemComponent::~LyShineEditorSystemComponent()
  66. {
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////
  69. void LyShineEditorSystemComponent::Init()
  70. {
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////
  73. void LyShineEditorSystemComponent::Activate()
  74. {
  75. AzToolsFramework::EditorEventsBus::Handler::BusConnect();
  76. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  77. LyShine::LyShineRequestBus::Handler::BusConnect();
  78. CanvasAssetPropertyHandler::Register();
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////////////////////////
  81. void LyShineEditorSystemComponent::Deactivate()
  82. {
  83. AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Handler::BusDisconnect();
  84. AzToolsFramework::UnregisterViewPane(LyViewPane::UiEditor);
  85. CUiAnimViewSequenceManager::Destroy();
  86. LyShine::LyShineRequestBus::Handler::BusDisconnect();
  87. AzToolsFramework::EditorEventsBus::Handler::BusDisconnect();
  88. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  89. }
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////
  91. void LyShineEditorSystemComponent::NotifyRegisterViews()
  92. {
  93. // Calculate default editor size and position.
  94. // For landscape screens, use 75% of the screen. For portrait screens, use 95% of screen width and 4:3 aspect ratio
  95. QRect deskRect = QApplication::primaryScreen()->availableGeometry();
  96. float availableWidth = (float)deskRect.width() * ((deskRect.width() > deskRect.height()) ? 0.75f : 0.95f);
  97. float availableHeight = (float)deskRect.height() * 0.75f;
  98. float editorWidth = availableWidth;
  99. float editorHeight = (deskRect.width() > deskRect.height()) ? availableHeight : (editorWidth * 3.0f / 4.0f);
  100. if ((availableWidth / availableHeight) > (editorWidth / editorHeight))
  101. {
  102. editorWidth = editorWidth * availableHeight / editorHeight;
  103. editorHeight = availableHeight;
  104. }
  105. else
  106. {
  107. editorWidth = availableWidth;
  108. editorHeight = editorHeight * availableWidth / editorWidth;
  109. }
  110. int x = (int)((float)deskRect.left() + (((float)deskRect.width() - availableWidth) / 2.0f) + ((availableWidth - editorWidth) / 2.0f));
  111. int y = (int)((float)deskRect.top() + (((float)deskRect.height() - availableHeight) / 2.0f) + ((availableHeight - editorHeight) / 2.0f));
  112. AzToolsFramework::ViewPaneOptions opt;
  113. opt.isPreview = true;
  114. opt.paneRect = QRect(x, y, (int)editorWidth, (int)editorHeight);
  115. #if defined(AZ_PLATFORM_LINUX)
  116. // Work-around for issue on Linux where closing (and destroying) the window and re-opening causes the Editor
  117. // to hang or crash. So instead of closing this window, replicate the action of unchecking UI Editor from the
  118. // Editor toolbar by hiding the parent view pane instead
  119. opt.isDeletable = false;
  120. #else
  121. opt.isDeletable = true;
  122. #endif
  123. opt.showOnToolsToolbar = true;
  124. opt.toolbarIcon = ":/Menu/ui_editor.svg";
  125. // opt.canHaveMultipleInstances = true; // uncomment this when CUiAnimViewSequenceManager::CanvasUnloading supports multiple canvases
  126. AzToolsFramework::RegisterViewPane<EditorWindow>(LyViewPane::UiEditor, LyViewPane::CategoryTools, opt);
  127. CUiAnimViewSequenceManager::Create();
  128. AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Handler::BusConnect();
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////////////////////
  131. void LyShineEditorSystemComponent::AddSourceFileOpeners(const char* fullSourceFileName, const AZ::Uuid& /*sourceUUID*/, AzToolsFramework::AssetBrowser::SourceFileOpenerList& openers)
  132. {
  133. if (AZStd::wildcard_match("*.uicanvas", fullSourceFileName))
  134. {
  135. openers.push_back({ "O3DE_UICanvas_Editor",
  136. "Open in UI Canvas Editor...",
  137. QIcon(),
  138. [](const char* fullSourceFileNameInCallback, const AZ::Uuid& /*sourceUUID*/)
  139. {
  140. AzToolsFramework::OpenViewPane(LyViewPane::UiEditor);
  141. QString absoluteName = QString::fromUtf8(fullSourceFileNameInCallback);
  142. UiEditorDLLBus::Broadcast(&UiEditorDLLInterface::OpenSourceCanvasFile, absoluteName);
  143. } });
  144. }
  145. }
  146. ////////////////////////////////////////////////////////////////////////////////////////////////
  147. AzToolsFramework::AssetBrowser::SourceFileDetails LyShineEditorSystemComponent::GetSourceFileDetails(const char* fullSourceFileName)
  148. {
  149. if (AZStd::wildcard_match("*.uicanvas", fullSourceFileName))
  150. {
  151. return AzToolsFramework::AssetBrowser::SourceFileDetails("Editor/Icons/AssetBrowser/UICanvas_80.svg");
  152. }
  153. if (AZStd::wildcard_match("*.sprite", fullSourceFileName))
  154. {
  155. return AzToolsFramework::AssetBrowser::SourceFileDetails("Editor/Icons/AssetBrowser/Sprite_80.svg");
  156. }
  157. return AzToolsFramework::AssetBrowser::SourceFileDetails();
  158. }
  159. ////////////////////////////////////////////////////////////////////////////////////////////////
  160. void LyShineEditorSystemComponent::EditUICanvas([[maybe_unused]] const AZStd::string_view& canvasPath)
  161. {
  162. AzToolsFramework::OpenViewPane(LyViewPane::UiEditor);
  163. AZStd::string stringPath = canvasPath;
  164. if (!stringPath.empty())
  165. {
  166. QString absoluteName = stringPath.c_str();
  167. UiEditorDLLBus::Broadcast(&UiEditorDLLInterface::OpenSourceCanvasFile, absoluteName);
  168. }
  169. }
  170. ////////////////////////////////////////////////////////////////////////////////////////////////
  171. void LyShineEditorSystemComponent::OnStopPlayInEditor()
  172. {
  173. // reset UI system
  174. if (AZ::Interface<ILyShine>::Get())
  175. {
  176. AZ::Interface<ILyShine>::Get()->Reset();
  177. }
  178. }
  179. }