Files
o3de/Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweenerSystemComponent.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

297 lines
13 KiB
C++

/*
* 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/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <ScriptedEntityTweener/ScriptedEntityTweenerBus.h>
#include <ScriptedEntityTweener/ScriptedEntityTweenerEnums.h>
#include "ScriptedEntityTweenerSystemComponent.h"
namespace ScriptedEntityTweener
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//! ScriptedEntityTweenerNotificationBusHandler Behavior context handler class
class ScriptedEntityTweenerNotificationBusHandler :
public ScriptedEntityTweenerNotificationsBus::Handler,
public AZ::BehaviorEBusHandler
{
public:
AZ_EBUS_BEHAVIOR_BINDER(ScriptedEntityTweenerNotificationBusHandler, "{118D3961-17C7-46E9-B9AC-61682D57E8D3}", AZ::SystemAllocator,
OnComplete, OnUpdate, OnLoop, RemoveCallback, OnTimelineAnimationStart);
void OnComplete(int callbackId) override
{
Call(FN_OnComplete, callbackId);
}
void OnUpdate(int callbackId, const AZStd::any& currentVal, float progressPercent) override
{
Call(FN_OnUpdate, callbackId, currentVal, progressPercent);
}
void OnLoop(int callbackId) override
{
Call(FN_OnLoop, callbackId);
}
void RemoveCallback(int callbackId) override
{
Call(FN_RemoveCallback, callbackId);
}
void OnTimelineAnimationStart(int timelineId, const AZ::Uuid& uuid, const AZStd::string& componentName, const AZStd::string& propertyName)
{
Call(FN_OnTimelineAnimationStart, timelineId, uuid, componentName, propertyName);
}
};
void ScriptedEntityTweenerSystemComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
{
serialize->Class<ScriptedEntityTweenerSystemComponent, AZ::Component>()
->Version(0)
;
}
AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
if (behaviorContext)
{
behaviorContext->EBus<ScriptedEntityTweenerBus>("ScriptedEntityTweenerBus")
->Event("AnimateEntity", &ScriptedEntityTweenerBus::Events::AnimateEntityScript)
->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::List)
->Event("SetOptionalParams", &ScriptedEntityTweenerBus::Events::SetOptionalParams)
->Event("Stop", &ScriptedEntityTweenerBus::Events::Stop)
->Event("Pause", &ScriptedEntityTweenerBus::Events::Pause)
->Event("Resume", &ScriptedEntityTweenerBus::Events::Resume)
->Event("SetPlayDirectionReversed", &ScriptedEntityTweenerBus::Events::SetPlayDirectionReversed)
->Event("SetSpeed", &ScriptedEntityTweenerBus::Events::SetSpeed)
->Event("SetInitialValue", &ScriptedEntityTweenerBus::Events::SetInitialValue)
->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::List)
->Event("GetVirtualPropertyValue", &ScriptedEntityTweenerBus::Events::GetVirtualPropertyValue);
behaviorContext->EBus<ScriptedEntityTweenerNotificationsBus>("ScriptedEntityTweenerNotificationBus")
->Handler<ScriptedEntityTweenerNotificationBusHandler>();
behaviorContext->Enum<(int)ScriptedEntityTweener::EasingMethod::Linear>("ScriptedEntityTweenerEasingMethod_Linear")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Quad>("ScriptedEntityTweenerEasingMethod_Quad")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Cubic>("ScriptedEntityTweenerEasingMethod_Cubic")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Quart>("ScriptedEntityTweenerEasingMethod_Quart")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Quint>("ScriptedEntityTweenerEasingMethod_Quint")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Sine>("ScriptedEntityTweenerEasingMethod_Sine")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Expo>("ScriptedEntityTweenerEasingMethod_Expo")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Circ>("ScriptedEntityTweenerEasingMethod_Circ")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Elastic>("ScriptedEntityTweenerEasingMethod_Elastic")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Back>("ScriptedEntityTweenerEasingMethod_Back")
->Enum<(int)ScriptedEntityTweener::EasingMethod::Bounce>("ScriptedEntityTweenerEasingMethod_Bounce")
->Enum<(int)ScriptedEntityTweener::EasingType::In>("ScriptedEntityTweenerEasingType_In")
->Enum<(int)ScriptedEntityTweener::EasingType::Out>("ScriptedEntityTweenerEasingType_Out")
->Enum<(int)ScriptedEntityTweener::EasingType::InOut>("ScriptedEntityTweenerEasingType_InOut");
}
}
void ScriptedEntityTweenerSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("ScriptedEntityTweenerService"));
}
void ScriptedEntityTweenerSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
{
incompatible.push_back(AZ_CRC("ScriptedEntityTweenerService"));
}
void ScriptedEntityTweenerSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
(void)required;
}
void ScriptedEntityTweenerSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
{
(void)dependent;
}
void ScriptedEntityTweenerSystemComponent::Init()
{
}
void ScriptedEntityTweenerSystemComponent::Activate()
{
ScriptedEntityTweenerBus::Handler::BusConnect();
AZ::TickBus::Handler::BusConnect();
}
void ScriptedEntityTweenerSystemComponent::Deactivate()
{
ScriptedEntityTweenerBus::Handler::BusDisconnect();
AZ::TickBus::Handler::BusDisconnect();
}
void ScriptedEntityTweenerSystemComponent::AnimateEntity(const AZ::EntityId& entityId, const AnimationParameters& params)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
animationTask = m_animationTasks.insert(ScriptedEntityTweenerTask(entityId)).first;
}
animationTask->AddAnimation(params);
}
void ScriptedEntityTweenerSystemComponent::SetOptionalParams(float timeIntoAnimation, float duration, int easingMethod, int easingType, float delayTime, int timesToPlay, bool isFrom, bool isPlayingBackward, const AZ::Uuid& animationId, int timelineId, int onCompleteCallbackId, int onUpdateCallbackId, int onLoopCallbackId)
{
AnimationParameters& params = m_tempParams;
params.m_animationProperties.m_easeMethod = static_cast<EasingMethod>(easingMethod);
params.m_animationProperties.m_easeType = static_cast<EasingType>(easingType);
params.m_animationProperties.m_timeIntoAnimation = timeIntoAnimation;
params.m_animationProperties.m_timeDuration = duration;
params.m_animationProperties.m_timeToDelayAnim = delayTime;
params.m_animationProperties.m_timesToPlay = timesToPlay;
params.m_animationProperties.m_isFrom = isFrom;
params.m_animationProperties.m_isPlayingBackward = isPlayingBackward;
params.m_animationProperties.m_animationId = animationId;
params.m_animationProperties.m_timelineId = timelineId;
params.m_animationProperties.m_onCompleteCallbackId = onCompleteCallbackId;
params.m_animationProperties.m_onUpdateCallbackId = onUpdateCallbackId;
params.m_animationProperties.m_onLoopCallbackId = onLoopCallbackId;
}
void ScriptedEntityTweenerSystemComponent::AnimateEntityScript(const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, const AZStd::any& paramTarget)
{
AnimationParameters& params = m_tempParams;
AnimationParameterAddressData data(componentName, virtualPropertyName);
params.m_animationParameters.emplace(data, paramTarget);
AnimateEntity(entityId, params);
m_tempParams.Reset();
}
void ScriptedEntityTweenerSystemComponent::Stop(int timelineId, const AZ::EntityId& entityId)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
return;
}
animationTask->Stop(timelineId);
}
void ScriptedEntityTweenerSystemComponent::Pause(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
return;
}
AnimationParameterAddressData data(componentName, virtualPropertyName);
animationTask->SetPaused(data, timelineId, true);
}
void ScriptedEntityTweenerSystemComponent::Resume(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
return;
}
AnimationParameterAddressData data(componentName, virtualPropertyName);
animationTask->SetPaused(data, timelineId, false);
}
void ScriptedEntityTweenerSystemComponent::SetPlayDirectionReversed(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, bool rewind)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
return;
}
AnimationParameterAddressData data(componentName, virtualPropertyName);
animationTask->SetPlayDirectionReversed(data, timelineId, rewind);
}
void ScriptedEntityTweenerSystemComponent::SetSpeed(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, float speed)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
return;
}
AnimationParameterAddressData data(componentName, virtualPropertyName);
animationTask->SetSpeed(data, timelineId, speed);
}
void ScriptedEntityTweenerSystemComponent::SetInitialValue(const AZ::Uuid& animationId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, const AZStd::any& initialValue)
{
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask == m_animationTasks.end())
{
return;
}
AnimationParameterAddressData data(componentName, virtualPropertyName);
animationTask->SetInitialValue(data, animationId, initialValue);
}
AZStd::any ScriptedEntityTweenerSystemComponent::GetVirtualPropertyValue(const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName)
{
AZStd::any toReturn;
AnimationParameterAddressData data(componentName, virtualPropertyName);
auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
if (animationTask != m_animationTasks.end())
{
animationTask->GetVirtualPropertyValue(toReturn, data);
}
else
{
ScriptedEntityTweenerTask task(entityId);
task.GetVirtualPropertyValue(toReturn, data);
}
return toReturn;
}
void ScriptedEntityTweenerSystemComponent::Reset()
{
m_animationTasks.clear();
}
void ScriptedEntityTweenerSystemComponent::OnTick(float deltaTime, AZ::ScriptTimePoint /*time*/)
{
for (auto& animTask : m_animationTasks)
{
animTask.Update(deltaTime);
}
//TODO: Possible optimization: Logic to remove "stale" animation tasks, use a "garbage collection" method to defer set removal operations (which is O(n))
for (auto it = m_animationTasks.begin(); it != m_animationTasks.end(); )
{
if (!((*it).GetIsActive()))
{
it = m_animationTasks.erase(it);
}
else
{
++it;
}
}
}
int ScriptedEntityTweenerSystemComponent::GetTickOrder()
{
return AZ::TICK_LAST;
}
}