You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.4 KiB
C++
109 lines
3.4 KiB
C++
/*
|
|
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
|
* its licensors.
|
|
*
|
|
* For complete copyright and license terms please see the LICENSE at the root of this
|
|
* distribution (the "License"). All use of this software is governed by the License,
|
|
* or, if provided, by the license below or the license accompanying this file. Do not
|
|
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
*
|
|
*/
|
|
|
|
namespace AzFramework
|
|
{
|
|
template<typename T>
|
|
bool Scene::SetSubsystem(T&& system)
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
for (const AZ::TypeId& key : m_systemKeys)
|
|
{
|
|
if (key == targetType)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
m_systemKeys.push_back(targetType);
|
|
m_systemObjects.emplace_back(AZStd::forward<T>(system));
|
|
m_subsystemEvent.Signal(*this, SubsystemEventType::Added, targetType);
|
|
return true;
|
|
}
|
|
|
|
template<typename T>
|
|
bool Scene::UnsetSubsystem()
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
const size_t m_systemKeysCount = m_systemKeys.size();
|
|
for (size_t i = 0; i < m_systemKeysCount; ++i)
|
|
{
|
|
if (m_systemKeys[i] != targetType)
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
RemoveSubsystem(i, targetType);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template<typename T>
|
|
bool Scene::UnsetSubsystem([[maybe_unused]] const T& system)
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
const size_t systemKeysCount = m_systemKeys.size();
|
|
for (size_t i = 0; i < systemKeysCount; ++i)
|
|
{
|
|
if (m_systemKeys[i] != targetType)
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
[[maybe_unused]] T* instance = AZStd::any_cast<T>(&m_systemObjects[i]);
|
|
AZ_Assert(
|
|
instance && *instance == system,
|
|
"Subsystem being released matched type, but wasn't pointing to the same system that was stored.");
|
|
RemoveSubsystem(i, targetType);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template<typename T>
|
|
T* Scene::FindSubsystem()
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
AZStd::any* subSystem = FindSubsystem(targetType);
|
|
return subSystem ? AZStd::any_cast<T>(subSystem) : nullptr;
|
|
}
|
|
|
|
template<typename T>
|
|
const T* Scene::FindSubsystem() const
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
const AZStd::any* subSystem = FindSubsystem(targetType);
|
|
return subSystem ? AZStd::any_cast<const T>(subSystem) : nullptr;
|
|
}
|
|
|
|
template<typename T>
|
|
T* Scene::FindSubsystemInScene()
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
AZStd::any* subSystem = FindSubsystemInScene(targetType);
|
|
return subSystem ? AZStd::any_cast<T>(subSystem) : nullptr;
|
|
}
|
|
|
|
template<typename T>
|
|
const T* Scene::FindSubsystemInScene() const
|
|
{
|
|
const AZ::TypeId& targetType = azrtti_typeid<T>();
|
|
const AZStd::any* subSystem = FindSubsystemInScene(targetType);
|
|
return subSystem ? AZStd::any_cast<const T>(subSystem) : nullptr;
|
|
}
|
|
} // namespace AzFramework
|