3
0

SequenceComponent.cpp 14 KB

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