SequenceComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 "SequenceComponent.h"
  9. #include <IMovieSystem.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <Maestro/Bus/SequenceAgentComponentBus.h>
  13. #include <Cinematics/Movie.h>
  14. #include <Cinematics/AnimSplineTrack.h>
  15. #include <Maestro/Types/AssetBlends.h>
  16. #include <Maestro/Types/AssetBlendKey.h>
  17. #include <Cinematics/AssetBlendTrack.h>
  18. #include <Cinematics/CompoundSplineTrack.h>
  19. #include <Cinematics/BoolTrack.h>
  20. #include <Cinematics/CharacterTrack.h>
  21. #include <Cinematics/CaptureTrack.h>
  22. #include <Cinematics/CommentTrack.h>
  23. #include <Cinematics/ConsoleTrack.h>
  24. #include <Cinematics/EventTrack.h>
  25. #include <Cinematics/GotoTrack.h>
  26. #include <Cinematics/LookAtTrack.h>
  27. #include <Cinematics/ScreenFaderTrack.h>
  28. #include <Cinematics/SelectTrack.h>
  29. #include <Cinematics/SequenceTrack.h>
  30. #include <Cinematics/SoundTrack.h>
  31. #include <Cinematics/StringTrack.h>
  32. #include <Cinematics/TimeRangesTrack.h>
  33. #include <Cinematics/TrackEventTrack.h>
  34. #include <Cinematics/AnimSequence.h>
  35. #include <Cinematics/AnimNode.h>
  36. #include <Cinematics/AnimNodeGroup.h>
  37. #include <Cinematics/SceneNode.h>
  38. #include <Cinematics/AnimAZEntityNode.h>
  39. #include <Cinematics/AnimComponentNode.h>
  40. #include <Cinematics/AnimScreenFaderNode.h>
  41. #include <Cinematics/CommentNode.h>
  42. #include <Cinematics/CVarNode.h>
  43. #include <Cinematics/ScriptVarNode.h>
  44. #include <Cinematics/AnimPostFXNode.h>
  45. #include <Cinematics/EventNode.h>
  46. #include <Cinematics/LayerNode.h>
  47. #include <Cinematics/ShadowsSetupNode.h>
  48. namespace Maestro
  49. {
  50. /**
  51. * Reflect the SequenceComponentNotificationBus to the Behavior Context
  52. */
  53. class BehaviorSequenceComponentNotificationBusHandler : public SequenceComponentNotificationBus::Handler, public AZ::BehaviorEBusHandler
  54. {
  55. public:
  56. AZ_EBUS_BEHAVIOR_BINDER(BehaviorSequenceComponentNotificationBusHandler, "{3EC0FB38-4649-41E7-8409-0D351FE99A64}", AZ::SystemAllocator
  57. , OnStart
  58. , OnStop
  59. , OnPause
  60. , OnResume
  61. , OnAbort
  62. , OnUpdate
  63. , OnTrackEventTriggered
  64. );
  65. void OnStart(float startTime) override
  66. {
  67. Call(FN_OnStart, startTime);
  68. }
  69. void OnStop(float stopTime) override
  70. {
  71. Call(FN_OnStop, stopTime);
  72. }
  73. void OnPause() override
  74. {
  75. Call(FN_OnPause);
  76. }
  77. void OnResume() override
  78. {
  79. Call(FN_OnResume);
  80. }
  81. void OnAbort(float abortTime) override
  82. {
  83. Call(FN_OnAbort, abortTime);
  84. }
  85. void OnUpdate(float updateTime) override
  86. {
  87. Call(FN_OnUpdate, updateTime);
  88. }
  89. void OnTrackEventTriggered(const char* eventName, const char* eventValue) override
  90. {
  91. Call(FN_OnTrackEventTriggered, eventName, eventValue);
  92. }
  93. };
  94. SequenceComponent::SequenceComponent()
  95. {
  96. AZ_Trace("SequenceComponent", "SequenceComponent");
  97. }
  98. SequenceComponent::~SequenceComponent()
  99. {
  100. AZ_Trace("SequenceComponent", "~SequenceComponent");
  101. }
  102. void SequenceComponent::Reflect(AZ::ReflectContext* context)
  103. {
  104. // Reflect the Cinematics library
  105. ReflectCinematicsLib(context);
  106. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  107. {
  108. serializeContext->Class<SequenceComponent, AZ::Component>()
  109. ->Version(2)
  110. ->Field("Sequence", &SequenceComponent::m_sequence);
  111. }
  112. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  113. {
  114. behaviorContext->EBus<SequenceComponentRequestBus>("SequenceComponentRequestBus")
  115. ->Event("Play", &SequenceComponentRequestBus::Events::Play)
  116. ->Event("PlayBetweenTimes", &SequenceComponentRequestBus::Events::PlayBetweenTimes)
  117. ->Event("Stop", &SequenceComponentRequestBus::Events::Stop)
  118. ->Event("Pause", &SequenceComponentRequestBus::Events::Pause)
  119. ->Event("Resume", &SequenceComponentRequestBus::Events::Resume)
  120. ->Event("SetPlaySpeed", &SequenceComponentRequestBus::Events::SetPlaySpeed)
  121. ->Event("JumpToTime", &SequenceComponentRequestBus::Events::JumpToTime)
  122. ->Event("JumpToBeginning", &SequenceComponentRequestBus::Events::JumpToBeginning)
  123. ->Event("JumpToEnd", &SequenceComponentRequestBus::Events::JumpToEnd)
  124. ->Event("GetCurrentPlayTime", &SequenceComponentRequestBus::Events::GetCurrentPlayTime)
  125. ->Event("GetPlaySpeed", &SequenceComponentRequestBus::Events::GetPlaySpeed)
  126. ;
  127. behaviorContext->Class<SequenceComponent>()->RequestBus("SequenceComponentRequestBus");
  128. behaviorContext->EBus<SequenceComponentNotificationBus>("SequenceComponentNotificationBus")->
  129. Handler<BehaviorSequenceComponentNotificationBusHandler>();
  130. }
  131. }
  132. void SequenceComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  133. {
  134. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  135. }
  136. void SequenceComponent::ReflectCinematicsLib(AZ::ReflectContext* context)
  137. {
  138. // The Movie System itself
  139. CMovieSystem::Reflect(context);
  140. // Tracks
  141. IAnimTrack::Reflect(context);
  142. TAnimSplineTrack<Vec2>::Reflect(context);
  143. CBoolTrack::Reflect(context);
  144. CCaptureTrack::Reflect(context);
  145. CCharacterTrack::Reflect(context);
  146. CCompoundSplineTrack::Reflect(context);
  147. CCommentTrack::Reflect(context);
  148. CConsoleTrack::Reflect(context);
  149. CEventTrack::Reflect(context);
  150. CGotoTrack::Reflect(context);
  151. CLookAtTrack::Reflect(context);
  152. CScreenFaderTrack::Reflect(context);
  153. CSelectTrack::Reflect(context);
  154. CSequenceTrack::Reflect(context);
  155. CSoundTrack::Reflect(context);
  156. CStringTrack::Reflect(context);
  157. CTrackEventTrack::Reflect(context);
  158. CAssetBlendTrack::Reflect(context);
  159. CTimeRangesTrack::Reflect(context);
  160. // Nodes
  161. IAnimSequence::Reflect(context);
  162. CAnimSequence::Reflect(context);
  163. CAnimSceneNode::Reflect(context);
  164. IAnimNode::Reflect(context);
  165. CAnimNode::Reflect(context);
  166. CAnimAzEntityNode::Reflect(context);
  167. CAnimComponentNode::Reflect(context);
  168. CAnimScreenFaderNode::Reflect(context);
  169. CCommentNode::Reflect(context);
  170. CAnimCVarNode::Reflect(context);
  171. CAnimScriptVarNode::Reflect(context);
  172. CAnimNodeGroup::Reflect(context);
  173. CAnimPostFXNode::Reflect(context);
  174. CAnimEventNode::Reflect(context);
  175. CLayerNode::Reflect(context);
  176. CShadowsSetupNode::Reflect(context);
  177. }
  178. void SequenceComponent::Init()
  179. {
  180. Component::Init();
  181. m_movieSystem = AZ::Interface<IMovieSystem>::Get();
  182. if (m_movieSystem)
  183. {
  184. if (m_sequence)
  185. {
  186. // Fix up the internal pointers in the sequence to match the deserialized structure
  187. m_sequence->InitPostLoad();
  188. // Register our deserialized sequence with the MovieSystem
  189. m_movieSystem->AddSequence(m_sequence.get());
  190. }
  191. }
  192. else
  193. {
  194. AZ_Warning("TrackView", false, "SequenceComponent::Init() called without m_movieSystem initialized yet, skipping creation of %s sequence.", m_entity->GetName().c_str());
  195. }
  196. }
  197. void SequenceComponent::Activate()
  198. {
  199. SequenceComponentRequestBus::Handler::BusConnect(GetEntityId());
  200. AZ_Trace("SequenceComponent::Activate", "SequenceComponentRequestBus connected to %s", GetEntityId().ToString().c_str())
  201. if (m_movieSystem)
  202. {
  203. if (m_sequence && (m_sequence->GetFlags() & IAnimSequence::eSeqFlags_PlayOnReset))
  204. {
  205. m_movieSystem->OnSequenceActivated(m_sequence.get());
  206. }
  207. }
  208. }
  209. void SequenceComponent::Deactivate()
  210. {
  211. SequenceComponentRequestBus::Handler::BusDisconnect();
  212. AZ_Trace(
  213. "SequenceComponent::Deactivate",
  214. "SequenceComponentRequestBus disconnected from %s",
  215. GetEntityId().ToString().c_str());
  216. // Remove this sequence from the game movie system.
  217. if (nullptr != m_movieSystem)
  218. {
  219. if (m_sequence)
  220. {
  221. m_movieSystem->RemoveSequence(m_sequence.get());
  222. }
  223. }
  224. }
  225. bool SequenceComponent::SetAnimatedPropertyValue(const AZ::EntityId& animatedEntityId, const AnimatablePropertyAddress& animatableAddress, const AnimatedValue& value)
  226. {
  227. const SequenceAgentEventBusId ebusId(GetEntityId(), animatedEntityId);
  228. bool changed = false;
  229. SequenceAgentComponentRequestBus::EventResult(
  230. changed, ebusId, &SequenceAgentComponentRequestBus::Events::SetAnimatedPropertyValue, animatableAddress, value);
  231. return changed;
  232. }
  233. bool SequenceComponent::GetAnimatedPropertyValue(AnimatedValue& returnValue, const AZ::EntityId& animatedEntityId, const AnimatablePropertyAddress& animatableAddress)
  234. {
  235. const SequenceAgentEventBusId ebusId(GetEntityId(), animatedEntityId);
  236. SequenceAgentComponentRequestBus::Event(
  237. ebusId, &SequenceAgentComponentRequestBus::Events::GetAnimatedPropertyValue, returnValue, animatableAddress);
  238. return true;
  239. }
  240. AZ::Uuid SequenceComponent::GetAnimatedAddressTypeId(const AZ::EntityId& animatedEntityId, const SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress)
  241. {
  242. AZ::Uuid typeId = AZ::Uuid::CreateNull();
  243. const SequenceAgentEventBusId ebusId(GetEntityId(), animatedEntityId);
  244. SequenceAgentComponentRequestBus::EventResult(typeId, ebusId, &SequenceAgentComponentRequestBus::Events::GetAnimatedAddressTypeId, animatableAddress);
  245. return typeId;
  246. }
  247. void SequenceComponent::GetAssetDuration(AnimatedValue& returnValue, const AZ::EntityId& animatedEntityId, AZ::ComponentId componentId, const AZ::Data::AssetId& assetId)
  248. {
  249. const SequenceAgentEventBusId ebusId(GetEntityId(), animatedEntityId);
  250. SequenceAgentComponentRequestBus::Event(
  251. ebusId, &SequenceAgentComponentRequestBus::Events::GetAssetDuration, returnValue, componentId, assetId);
  252. }
  253. void SequenceComponent::Play()
  254. {
  255. if (m_sequence)
  256. {
  257. m_movieSystem->PlaySequence(m_sequence.get(), /*parentSeq =*/ nullptr, /*bResetFX =*/ true,/*bTrackedSequence =*/ false, /*float startTime =*/ -FLT_MAX, /*float endTime =*/ -FLT_MAX);
  258. }
  259. }
  260. void SequenceComponent::PlayBetweenTimes(float startTime, float endTime)
  261. {
  262. if (m_sequence)
  263. {
  264. m_movieSystem->PlaySequence(m_sequence.get(), /*parentSeq =*/ nullptr, /*bResetFX =*/ true,/*bTrackedSequence =*/ false, startTime, endTime);
  265. }
  266. }
  267. void SequenceComponent::Stop()
  268. {
  269. if (m_sequence)
  270. {
  271. m_movieSystem->StopSequence(m_sequence.get());
  272. }
  273. }
  274. void SequenceComponent::Pause()
  275. {
  276. if (m_sequence)
  277. {
  278. m_sequence.get()->Pause();
  279. }
  280. }
  281. void SequenceComponent::Resume()
  282. {
  283. if (m_sequence)
  284. {
  285. m_sequence.get()->Resume();
  286. }
  287. }
  288. void SequenceComponent::SetPlaySpeed(float newSpeed)
  289. {
  290. if (m_sequence)
  291. {
  292. m_movieSystem->SetPlayingSpeed(m_sequence.get(), newSpeed);
  293. }
  294. }
  295. void SequenceComponent::JumpToTime(float newTime)
  296. {
  297. if (m_sequence)
  298. {
  299. newTime = clamp_tpl(newTime, m_sequence.get()->GetTimeRange().start, m_sequence.get()->GetTimeRange().end);
  300. m_movieSystem->SetPlayingTime(m_sequence.get(), newTime);
  301. }
  302. }
  303. void SequenceComponent::JumpToEnd()
  304. {
  305. if (m_sequence)
  306. {
  307. m_movieSystem->SetPlayingTime(m_sequence.get(), m_sequence.get()->GetTimeRange().end);
  308. }
  309. }
  310. void SequenceComponent::JumpToBeginning()
  311. {
  312. if (m_sequence)
  313. {
  314. m_movieSystem->SetPlayingTime(m_sequence.get(), m_sequence.get()->GetTimeRange().start);
  315. }
  316. }
  317. float SequenceComponent::GetCurrentPlayTime()
  318. {
  319. if (m_sequence)
  320. {
  321. return m_movieSystem->GetPlayingTime(m_sequence.get());
  322. }
  323. return .0f;
  324. }
  325. float SequenceComponent::GetPlaySpeed()
  326. {
  327. if (m_sequence)
  328. {
  329. return m_movieSystem->GetPlayingSpeed(m_sequence.get());
  330. }
  331. return 1.0f;
  332. }
  333. } // namespace Maestro