123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/Serialization/EditContext.h>
- #include <AzCore/Component/ComponentApplicationBus.h>
- #include <AzCore/RTTI/BehaviorContext.h>
- #include "LyShineFeatureProcessor.h"
- #include "LyShineSystemComponent.h"
- #include "UiSerialize.h"
- #include "UiCanvasFileObject.h"
- #include "UiCanvasComponent.h"
- #include "LyShineDebug.h"
- #include "UiElementComponent.h"
- #include "UiTransform2dComponent.h"
- #include "UiImageComponent.h"
- #include "UiImageSequenceComponent.h"
- #include "UiTextComponent.h"
- #include "UiButtonComponent.h"
- #include "UiMarkupButtonComponent.h"
- #include "UiCheckboxComponent.h"
- #include "UiDraggableComponent.h"
- #include "UiDropTargetComponent.h"
- #include "UiDropdownComponent.h"
- #include "UiDropdownOptionComponent.h"
- #include "UiSliderComponent.h"
- #include "UiTextInputComponent.h"
- #include "UiScrollBarComponent.h"
- #include "UiScrollBoxComponent.h"
- #include "UiFaderComponent.h"
- #include "UiLayoutFitterComponent.h"
- #include "UiMaskComponent.h"
- #include "UiLayoutCellComponent.h"
- #include "UiLayoutColumnComponent.h"
- #include "UiLayoutRowComponent.h"
- #include "UiLayoutGridComponent.h"
- #include "UiParticleEmitterComponent.h"
- #include "UiFlipbookAnimationComponent.h"
- #include "UiRadioButtonComponent.h"
- #include "UiRadioButtonGroupComponent.h"
- #include "UiTooltipComponent.h"
- #include "UiTooltipDisplayComponent.h"
- #include "UiDynamicLayoutComponent.h"
- #include "UiDynamicScrollBoxComponent.h"
- #include "UiNavigationSettings.h"
- #include "LyShinePass.h"
- namespace LyShine
- {
- const AZStd::list<AZ::ComponentDescriptor*>* LyShineSystemComponent::m_componentDescriptors = nullptr;
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::Reflect(AZ::ReflectContext* context)
- {
- UiSerialize::ReflectUiTypes(context);
- UiCanvasFileObject::Reflect(context);
- UiNavigationSettings::Reflect(context);
- if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serialize->Class<LyShineSystemComponent, AZ::Component>()
- ->Version(1)
- ->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>({ AZ_CRC("AssetBuilder", 0xc739c7d7) }))
- ->Field("CursorImagePath", &LyShineSystemComponent::m_cursorImagePathname);
- if (AZ::EditContext* ec = serialize->GetEditContext())
- {
- auto editInfo = ec->Class<LyShineSystemComponent>("LyShine", "In-game User Interface System");
- editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
- ->Attribute(AZ::Edit::Attributes::Category, "UI")
- ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
- editInfo->DataElement(0, &LyShineSystemComponent::m_cursorImagePathname, "CursorImagePath", "The cursor image path.")
- ->Attribute(AZ::Edit::Attributes::ChangeNotify, &LyShineSystemComponent::BroadcastCursorImagePathname);
- }
- }
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->EBus<UiCanvasManagerBus>("UiCanvasManagerBus")
- ->Event("CreateCanvas", &UiCanvasManagerBus::Events::CreateCanvas)
- ->Event("LoadCanvas", &UiCanvasManagerBus::Events::LoadCanvas)
- ->Event("UnloadCanvas", &UiCanvasManagerBus::Events::UnloadCanvas)
- ->Event("FindLoadedCanvasByPathName", &UiCanvasManagerBus::Events::FindLoadedCanvasByPathName)
- ;
- behaviorContext->EBus<UiCursorBus>("UiCursorBus")
- ->Event("IncrementVisibleCounter", &UiCursorBus::Events::IncrementVisibleCounter)
- ->Event("DecrementVisibleCounter", &UiCursorBus::Events::DecrementVisibleCounter)
- ->Event("IsUiCursorVisible", &UiCursorBus::Events::IsUiCursorVisible)
- ->Event("SetUiCursor", &UiCursorBus::Events::SetUiCursor)
- ->Event("GetUiCursorPosition", &UiCursorBus::Events::GetUiCursorPosition)
- ->Event("SetUiCursorPosition", &UiCursorBus::Events::SetUiCursorPosition)
- ;
- }
-
- LyShineFeatureProcessor::Reflect(context);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
- {
- provided.push_back(AZ_CRC("LyShineService", 0xae98ab29));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
- {
- incompatible.push_back(AZ_CRC("LyShineService", 0xae98ab29));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
- {
- #if !defined(LYSHINE_BUILDER) && !defined(LYSHINE_TESTS)
- required.push_back(AZ_CRC("RPISystem", 0xf2add773));
- #endif
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
- {
- dependent.push_back(AZ_CRC("AssetDatabaseService", 0x3abf5601));
- dependent.push_back(AZ_CRC("AssetCatalogService", 0xc68ffc57));
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::SetLyShineComponentDescriptors(const AZStd::list<AZ::ComponentDescriptor*>* descriptors)
- {
- m_componentDescriptors = descriptors;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- LyShineSystemComponent::LyShineSystemComponent()
- {
- m_cursorImagePathname.SetAssetPath("Textures/Cursor_Default.tif");
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::Init()
- {
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::Activate()
- {
- UiSystemBus::Handler::BusConnect();
- UiSystemToolsBus::Handler::BusConnect();
- UiFrameworkBus::Handler::BusConnect();
- CrySystemEventBus::Handler::BusConnect();
- // register all the component types internal to the LyShine module
- // These are registered in the order we want them to appear in the Add Component menu
- RegisterComponentTypeForMenuOrdering(UiCanvasComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiElementComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiTransform2dComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiImageComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiImageSequenceComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiTextComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiButtonComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiMarkupButtonComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiCheckboxComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiRadioButtonComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiRadioButtonGroupComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiSliderComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiTextInputComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiScrollBarComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiScrollBoxComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiDraggableComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiDropTargetComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiDropdownComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiDropdownOptionComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiFaderComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiMaskComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiLayoutColumnComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiLayoutRowComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiLayoutGridComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiLayoutCellComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiLayoutFitterComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiTooltipComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiTooltipDisplayComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiDynamicLayoutComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiDynamicScrollBoxComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiParticleEmitterComponent::RTTI_Type());
- RegisterComponentTypeForMenuOrdering(UiFlipbookAnimationComponent::RTTI_Type());
- #if !defined(LYSHINE_BUILDER) && !defined(LYSHINE_TESTS)
- // Add LyShine pass
- auto* passSystem = AZ::RPI::PassSystemInterface::Get();
- AZ_Assert(passSystem, "Cannot get the pass system.");
- passSystem->AddPassCreator(AZ::Name("LyShinePass"), &LyShine::LyShinePass::Create);
- passSystem->AddPassCreator(AZ::Name("LyShineChildPass"), &LyShine::LyShineChildPass::Create);
- passSystem->AddPassCreator(AZ::Name("RttChildPass"), &LyShine::RttChildPass::Create);
- // Setup handler for load pass template mappings
- m_loadTemplatesHandler = AZ::RPI::PassSystemInterface::OnReadyLoadTemplatesEvent::Handler([this]() { this->LoadPassTemplateMappings(); });
- AZ::RPI::PassSystemInterface::Get()->ConnectEvent(m_loadTemplatesHandler);
-
- // Register feature processor
- AZ::RPI::FeatureProcessorFactory::Get()->RegisterFeatureProcessor<LyShineFeatureProcessor>();
- #endif
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::Deactivate()
- {
- #if !defined(LYSHINE_BUILDER) && !defined(LYSHINE_TESTS)
- m_loadTemplatesHandler.Disconnect();
- AZ::RPI::FeatureProcessorFactory::Get()->UnregisterFeatureProcessor<LyShineFeatureProcessor>();
- #endif
- UiSystemBus::Handler::BusDisconnect();
- UiSystemToolsBus::Handler::BusDisconnect();
- UiFrameworkBus::Handler::BusDisconnect();
- CrySystemEventBus::Handler::BusDisconnect();
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::RegisterComponentTypeForMenuOrdering(const AZ::Uuid& typeUuid)
- {
- m_componentTypes.push_back(typeUuid);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- const AZStd::vector<AZ::Uuid>* LyShineSystemComponent::GetComponentTypesForMenuOrdering()
- {
- return &m_componentTypes;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- const AZStd::list<AZ::ComponentDescriptor*>* LyShineSystemComponent::GetLyShineComponentDescriptors()
- {
- return m_componentDescriptors;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- UiSystemToolsInterface::CanvasAssetHandle* LyShineSystemComponent::LoadCanvasFromStream(AZ::IO::GenericStream& stream, const AZ::ObjectStream::FilterDescriptor& filterDesc)
- {
- return UiCanvasFileObject::LoadCanvasFromStream(stream, filterDesc);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::SaveCanvasToStream(UiSystemToolsInterface::CanvasAssetHandle* canvas, AZ::IO::FileIOStream& stream)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- UiCanvasFileObject::SaveCanvasToStream(stream, canvasFileObject);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- AZ::Entity* LyShineSystemComponent::GetRootSliceEntity(CanvasAssetHandle* canvas)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- return canvasFileObject->m_rootSliceEntity;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- AZ::Entity* LyShineSystemComponent::GetCanvasEntity(CanvasAssetHandle* canvas)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- return canvasFileObject->m_canvasEntity;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- AZ::SliceComponent* LyShineSystemComponent::GetRootSliceSliceComponent(UiSystemToolsInterface::CanvasAssetHandle* canvas)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- AZ::Entity* rootSliceEntity = canvasFileObject->m_rootSliceEntity;
- if (rootSliceEntity->GetState() == AZ::Entity::State::Constructed)
- {
- rootSliceEntity->Init();
- }
- AZ::SliceComponent* sliceComponent = rootSliceEntity->FindComponent<AZ::SliceComponent>();
- return sliceComponent;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::ReplaceRootSliceSliceComponent(UiSystemToolsInterface::CanvasAssetHandle* canvas, AZ::SliceComponent* newSliceComponent)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- AZ::Entity* oldRootSliceEntity = canvasFileObject->m_rootSliceEntity;
- AZ::EntityId idToReuse = oldRootSliceEntity->GetId();
-
- AZ::Entity* newRootSliceEntity = aznew AZ::Entity(idToReuse, AZStd::to_string(static_cast<AZ::u64>(idToReuse)).c_str());
- newRootSliceEntity->AddComponent(newSliceComponent);
- canvasFileObject->m_rootSliceEntity = newRootSliceEntity;
- delete oldRootSliceEntity;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::ReplaceCanvasEntity(UiSystemToolsInterface::CanvasAssetHandle* canvas, AZ::Entity* newCanvasEntity)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- canvasFileObject->m_canvasEntity = newCanvasEntity;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::DestroyCanvas(CanvasAssetHandle* canvas)
- {
- UiCanvasFileObject* canvasFileObject = static_cast<UiCanvasFileObject*>(canvas);
- delete canvasFileObject->m_canvasEntity;
- delete canvasFileObject->m_rootSliceEntity;
- delete canvasFileObject;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- bool LyShineSystemComponent::HasUiElementComponent(AZ::Entity* entity)
- {
- return entity->FindComponent<UiElementComponent>() != nullptr;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::AddEditorOnlyEntity(AZ::Entity* editorOnlyEntity, EntityIdSet& editorOnlyEntities)
- {
- // All descendents of an editor-only entity are considered editor-only also.
- // Iterate through all the descedents of the given entity and add their IDs
- // to the list of editor-only entities.
- AZStd::vector<AZ::Entity*> childEntities = { editorOnlyEntity };
- while (!childEntities.empty())
- {
- AZ::Entity* parentEntity = childEntities.back();
- childEntities.pop_back();
- editorOnlyEntities.insert(parentEntity->GetId());
- UiElementComponent* elementComponent = parentEntity->FindComponent<UiElementComponent>();
- if (elementComponent)
- {
- int numChildren = elementComponent->GetNumChildElements();
- for (int i = 0; i < numChildren; ++i)
- {
- childEntities.push_back(elementComponent->GetChildElement(i));
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::HandleEditorOnlyEntities(const EntityList& exportSliceEntities, const EntityIdSet& editorOnlyEntityIds)
- {
- AZStd::unordered_map<AZ::EntityId, AZStd::vector<AZ::EntityId>> parentToChildren;
- // Build a map of entity Ids to their parent Ids, for faster lookup during processing.
- for (AZ::Entity* exportParentEntity : exportSliceEntities)
- {
- UiElementComponent* exportParentComponent = exportParentEntity->FindComponent<UiElementComponent>();
- if (!exportParentComponent)
- {
- continue;
- }
- // Map the child entities to the parent ID
- int numChildElements = exportParentComponent->GetNumChildElements();
- for (int exportChildIndex = 0; exportChildIndex < numChildElements; ++exportChildIndex)
- {
- AZ::EntityId childExportEntity = exportParentComponent->GetChildEntityId(exportChildIndex);
- parentToChildren[exportParentEntity->GetId()].push_back(childExportEntity);
- }
- }
- // Remove editor-only entities from parent hierarchy
- for (AZ::Entity* exportParentEntity : exportSliceEntities)
- {
- for (AZ::EntityId exportChildEntity : parentToChildren[exportParentEntity->GetId()])
- {
- const bool childIsEditorOnly = editorOnlyEntityIds.end() != editorOnlyEntityIds.find(exportChildEntity);
- if (childIsEditorOnly)
- {
- UiElementComponent* exportParentComponent = exportParentEntity->FindComponent<UiElementComponent>();
- exportParentComponent->RemoveChild(exportChildEntity);
- }
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::OnCrySystemInitialized(ISystem& system, [[maybe_unused]] const SSystemInitParams& startupParams)
- {
- #if !defined(AZ_MONOLITHIC_BUILD)
- // When module is linked dynamically, we must set our gEnv pointer.
- // When module is linked statically, we'll share the application's gEnv pointer.
- gEnv = system.GetGlobalEnvironment();
- #endif
- m_lyShine = AZStd::make_unique<CLyShine>();
- AZ::Interface<ILyShine>::Register(m_lyShine.get());
- system.GetILevelSystem()->AddListener(this);
- BroadcastCursorImagePathname();
- if (AZ::Interface<ILyShine>::Get())
- {
- AZ::Interface<ILyShine>::Get()->PostInit();
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::OnCrySystemShutdown(ISystem& system)
- {
- system.GetILevelSystem()->RemoveListener(this);
- if (m_lyShine)
- {
- AZ::Interface<ILyShine>::Unregister(m_lyShine.get());
- m_lyShine.reset();
- }
- }
- ////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::OnUnloadComplete([[maybe_unused]] const char* levelName)
- {
- // Perform level unload procedures for the LyShine UI system
- if (AZ::Interface<ILyShine>::Get())
- {
- AZ::Interface<ILyShine>::Get()->OnLevelUnload();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::BroadcastCursorImagePathname()
- {
- UiCursorBus::Broadcast(&UiCursorInterface::SetUiCursor, m_cursorImagePathname.GetAssetPath().c_str());
- }
- #if !defined(LYSHINE_BUILDER) && !defined(LYSHINE_TESTS)
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void LyShineSystemComponent::LoadPassTemplateMappings()
- {
- const char* passTemplatesFile = "Passes/LyShinePassTemplates.azasset";
- AZ::RPI::PassSystemInterface::Get()->LoadPassTemplateMappings(passTemplatesFile);
- }
- #endif
- }
|