ScriptedEntityTweenerTask.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <ScriptedEntityTweener/ScriptedEntityTweenerBus.h>
  9. #include "ScriptedEntityTweenerTask.h"
  10. namespace ScriptedEntityTweener
  11. {
  12. const AZStd::any ScriptedEntityTweenerTask::QueuedSubtaskInfo::m_emptyInitialValue;
  13. const float AnimationProperties::UninitializedParamFloat = FLT_MIN;
  14. const int AnimationProperties::InvalidCallbackId = 0;
  15. const unsigned int AnimationProperties::InvalidTimelineId = 0;
  16. ScriptedEntityTweenerTask::ScriptedEntityTweenerTask(AZ::EntityId id)
  17. : m_entityId(id)
  18. {
  19. }
  20. ScriptedEntityTweenerTask::~ScriptedEntityTweenerTask()
  21. {
  22. }
  23. void ScriptedEntityTweenerTask::AddAnimation(const AnimationParameters& params, bool overwriteQueued)
  24. {
  25. float timeToDelay = params.m_animationProperties.m_timeToDelayAnim;
  26. if (timeToDelay > .0f)
  27. {
  28. m_queuedSubtasks.emplace_back(QueuedSubtaskInfo(params, params.m_animationProperties.m_timeToDelayAnim));
  29. return;
  30. }
  31. for (const auto& it : params.m_animationParameters)
  32. {
  33. const AnimationParameterAddressData& addressData = it.first;
  34. if (IsTimelineIdValid(params.m_animationProperties.m_timelineId))
  35. {
  36. ScriptedEntityTweenerNotificationsBus::Broadcast(&ScriptedEntityTweenerNotificationsBus::Events::OnTimelineAnimationStart,
  37. params.m_animationProperties.m_timelineId,
  38. params.m_animationProperties.m_animationId,
  39. addressData.m_componentName,
  40. addressData.m_virtualPropertyName);
  41. }
  42. auto subtask = m_subtasks.find(addressData);
  43. if (subtask == m_subtasks.end())
  44. {
  45. //For this property on this entity, an animation isn't already running.
  46. ScriptedEntityTweenerSubtask subtaskToAdd(m_entityId);
  47. if (params.m_animationProperties.m_timeDuration == 0.0f)
  48. {
  49. //If the animation is a set animation, immediately initialize, execute, and call callbacks related to it.
  50. if (InitializeSubtask(subtaskToAdd, it, params))
  51. {
  52. AZStd::set<CallbackData> callbacks;
  53. subtaskToAdd.Update(0, callbacks);
  54. ExecuteCallbacks(callbacks);
  55. }
  56. return;
  57. }
  58. else
  59. {
  60. //Animation will play over some time, enqueue it to play as part of the update loop.
  61. subtask = m_subtasks.emplace(addressData, subtaskToAdd).first;
  62. }
  63. }
  64. else
  65. {
  66. //An animation already exists for this virtual property
  67. //Cleanup any callbacks it may have registered.
  68. ClearCallbacks(subtask->second.GetAnimationProperties());
  69. //Overwrite any queued animations on this subtask if the animation wasn't started from the queue, as it was user specified.
  70. if (overwriteQueued)
  71. {
  72. auto queuedSubtaskIter = m_queuedSubtasks.begin();
  73. while (queuedSubtaskIter != m_queuedSubtasks.end())
  74. {
  75. auto& queuedAnimParams = queuedSubtaskIter->GetParameters();
  76. //Remove each queued animation relating to this virtual address.
  77. auto queuedAnimParamIter = queuedAnimParams.m_animationParameters.begin();
  78. while (queuedAnimParamIter != queuedAnimParams.m_animationParameters.end())
  79. {
  80. const AnimationParameterAddressData& queuedAddressData = queuedAnimParamIter->first;
  81. if (addressData == queuedAddressData)
  82. {
  83. queuedAnimParamIter = queuedAnimParams.m_animationParameters.erase(queuedAnimParamIter);
  84. }
  85. else
  86. {
  87. ++queuedAnimParamIter;
  88. }
  89. }
  90. //If queued anim subtask no longer contains any parameters to update, remove it completely.
  91. if (queuedSubtaskIter->GetParameters().m_animationParameters.size() == 0)
  92. {
  93. queuedSubtaskIter = m_queuedSubtasks.erase(queuedSubtaskIter);
  94. }
  95. else
  96. {
  97. ++queuedSubtaskIter;
  98. }
  99. }
  100. }
  101. }
  102. if (!InitializeSubtask(subtask->second, it, params))
  103. {
  104. m_subtasks.erase(subtask);
  105. }
  106. }
  107. }
  108. void ScriptedEntityTweenerTask::Update(float deltaTime)
  109. {
  110. auto queuedIter = m_queuedSubtasks.begin();
  111. while (queuedIter != m_queuedSubtasks.end())
  112. {
  113. if (queuedIter->UpdateUntilReady(deltaTime))
  114. {
  115. AddAnimation(queuedIter->GetParameters(), false);
  116. if (queuedIter->HasInitialValue())
  117. {
  118. auto& queuedAnimParams = queuedIter->GetParameters();
  119. for (const auto& it : queuedAnimParams.m_animationParameters)
  120. {
  121. const AnimationParameterAddressData& addressData = it.first;
  122. const AZStd::any& initialValue = queuedIter->GetInitialValue(addressData);
  123. if (!initialValue.empty())
  124. {
  125. auto subtaskPair = m_subtasks.find(addressData);
  126. if (subtaskPair != m_subtasks.end())
  127. {
  128. ScriptedEntityTweenerSubtask& subtask = subtaskPair->second;
  129. subtask.SetInitialValue(queuedIter->GetAnimationId(), initialValue);
  130. }
  131. }
  132. }
  133. }
  134. queuedIter = m_queuedSubtasks.erase(queuedIter);
  135. }
  136. else
  137. {
  138. ++queuedIter;
  139. }
  140. }
  141. m_callbacks.clear();
  142. for (auto& subtaskPair : m_subtasks)
  143. {
  144. ScriptedEntityTweenerSubtask& subtask = subtaskPair.second;
  145. if (subtask.IsActive())
  146. {
  147. subtask.Update(deltaTime, m_callbacks);
  148. }
  149. }
  150. // Aggregate all callbacks from the subtasks to execute them all at once, as multiple subtasks may reference the same callback
  151. ExecuteCallbacks(m_callbacks);
  152. //TODO: Possible optimization: Logic to remove "stale" animation subtasks, use a "garbage collection" method to defer set removal operations (which is O(n))
  153. for (auto it = m_subtasks.begin(); it != m_subtasks.end(); )
  154. {
  155. ScriptedEntityTweenerSubtask& subtask = it->second;
  156. if (!subtask.IsActive())
  157. {
  158. it = m_subtasks.erase(it);
  159. }
  160. else
  161. {
  162. ++it;
  163. }
  164. }
  165. }
  166. bool ScriptedEntityTweenerTask::GetIsActive()
  167. {
  168. for (auto& subtaskPair : m_subtasks)
  169. {
  170. ScriptedEntityTweenerSubtask& subtask = subtaskPair.second;
  171. if (subtask.IsActive())
  172. {
  173. return true;
  174. }
  175. }
  176. return m_queuedSubtasks.size() > 0;
  177. }
  178. void ScriptedEntityTweenerTask::Stop(int timelineId)
  179. {
  180. for (auto it = m_queuedSubtasks.begin(); it != m_queuedSubtasks.end(); )
  181. {
  182. auto& queuedSubtask = (*it);
  183. if (timelineId == 0 || queuedSubtask.GetTimelineId() == timelineId)
  184. {
  185. ClearCallbacks(queuedSubtask.GetParameters().m_animationProperties);
  186. it = m_queuedSubtasks.erase(it);
  187. }
  188. else
  189. {
  190. ++it;
  191. }
  192. }
  193. for (auto it = m_subtasks.begin(); it != m_subtasks.end(); )
  194. {
  195. ScriptedEntityTweenerSubtask& subtask = it->second;
  196. if (timelineId == 0 || subtask.GetTimelineId() == timelineId)
  197. {
  198. ClearCallbacks(subtask.GetAnimationProperties());
  199. it = m_subtasks.erase(it);
  200. }
  201. else
  202. {
  203. ++it;
  204. }
  205. }
  206. }
  207. void ScriptedEntityTweenerTask::SetPaused(const AnimationParameterAddressData& addressData, int timelineId, bool isPaused)
  208. {
  209. auto subtaskPair = m_subtasks.find(addressData);
  210. if (subtaskPair != m_subtasks.end())
  211. {
  212. ScriptedEntityTweenerSubtask& subtask = subtaskPair->second;
  213. subtask.SetPaused(timelineId, isPaused);
  214. }
  215. if (IsTimelineIdValid(timelineId))
  216. {
  217. for (auto& queuedSubtask : m_queuedSubtasks)
  218. {
  219. if (queuedSubtask.GetTimelineId() == timelineId)
  220. {
  221. queuedSubtask.SetPaused(isPaused);
  222. }
  223. }
  224. }
  225. }
  226. void ScriptedEntityTweenerTask::SetPlayDirectionReversed(const AnimationParameterAddressData& addressData, int timelineId, bool isPlayingBackward)
  227. {
  228. auto subtaskPair = m_subtasks.find(addressData);
  229. if (subtaskPair != m_subtasks.end())
  230. {
  231. ScriptedEntityTweenerSubtask& subtask = subtaskPair->second;
  232. subtask.SetPlayDirectionReversed(timelineId, isPlayingBackward);
  233. }
  234. //Remove any subtask queued for this timeline id, as now that we're rewinding, they should not play.
  235. if (IsTimelineIdValid(timelineId))
  236. {
  237. auto queuedIter = m_queuedSubtasks.begin();
  238. while (queuedIter != m_queuedSubtasks.end())
  239. {
  240. auto& queuedSubtask = (*queuedIter);
  241. if (queuedSubtask.GetTimelineId() == timelineId)
  242. {
  243. queuedIter = m_queuedSubtasks.erase(queuedIter);
  244. }
  245. else
  246. {
  247. ++queuedIter;
  248. }
  249. }
  250. }
  251. }
  252. void ScriptedEntityTweenerTask::SetSpeed(const AnimationParameterAddressData& addressData, int timelineId, float speed)
  253. {
  254. auto subtaskPair = m_subtasks.find(addressData);
  255. if (subtaskPair != m_subtasks.end())
  256. {
  257. ScriptedEntityTweenerSubtask& subtask = subtaskPair->second;
  258. subtask.SetSpeed(timelineId, speed);
  259. }
  260. if (IsTimelineIdValid(timelineId))
  261. {
  262. for (auto& queuedSubtask : m_queuedSubtasks)
  263. {
  264. if (queuedSubtask.GetTimelineId() == timelineId)
  265. {
  266. queuedSubtask.SetPlaybackSpeed(speed);
  267. }
  268. }
  269. }
  270. }
  271. void ScriptedEntityTweenerTask::SetInitialValue(const AnimationParameterAddressData& addressData, const AZ::Uuid& animationId, const AZStd::any& initialValue)
  272. {
  273. auto subtaskPair = m_subtasks.find(addressData);
  274. if (subtaskPair != m_subtasks.end())
  275. {
  276. ScriptedEntityTweenerSubtask& subtask = subtaskPair->second;
  277. subtask.SetInitialValue(animationId, initialValue);
  278. }
  279. if (!animationId.IsNull())
  280. {
  281. for (auto& queuedSubtask : m_queuedSubtasks)
  282. {
  283. if (queuedSubtask.GetAnimationId() == animationId)
  284. {
  285. queuedSubtask.SetInitialValue(addressData, initialValue);
  286. }
  287. }
  288. }
  289. }
  290. void ScriptedEntityTweenerTask::GetVirtualPropertyValue(AZStd::any& returnVal, const AnimationParameterAddressData& addressData)
  291. {
  292. auto subtaskPair = m_subtasks.find(addressData);
  293. if (subtaskPair != m_subtasks.end())
  294. {
  295. ScriptedEntityTweenerSubtask& subtask = subtaskPair->second;
  296. subtask.GetVirtualPropertyValue(returnVal, addressData);
  297. }
  298. else
  299. {
  300. ScriptedEntityTweenerSubtask tempSubtask(m_entityId);
  301. tempSubtask.GetVirtualPropertyValue(returnVal, addressData);
  302. }
  303. }
  304. bool ScriptedEntityTweenerTask::InitializeSubtask(ScriptedEntityTweenerSubtask& subtask, const AZStd::pair<AnimationParameterAddressData, AZStd::any> initData, AnimationParameters params)
  305. {
  306. if (!subtask.Initialize(initData.first, initData.second, params.m_animationProperties))
  307. {
  308. AZStd::string entityName;
  309. if (m_entityId.IsValid())
  310. {
  311. AZ::ComponentApplicationBus::BroadcastResult(entityName, &AZ::ComponentApplicationRequests::GetEntityName, m_entityId);
  312. }
  313. else
  314. {
  315. entityName = "InvalidEntity";
  316. }
  317. //AZ_Warning("ScriptedEntityTweenerTask", false, "ScriptedEntityTweenerTask::AddAnimation - Error starting set animation entity name [%s], [%s, %s]", entityName.c_str(), initData.first.m_componentName.c_str(), initData.first.m_virtualPropertyName.c_str());
  318. return false;
  319. }
  320. return true;
  321. }
  322. void ScriptedEntityTweenerTask::ExecuteCallbacks(const AZStd::set<CallbackData>& callbacks)
  323. {
  324. for (const auto& callback : callbacks)
  325. {
  326. switch (callback.m_callbackType)
  327. {
  328. case CallbackTypes::OnComplete:
  329. ScriptedEntityTweenerNotificationsBus::Broadcast(&ScriptedEntityTweenerNotificationsBus::Events::OnComplete, callback.m_callbackId);
  330. break;
  331. case CallbackTypes::OnUpdate:
  332. ScriptedEntityTweenerNotificationsBus::Broadcast(&ScriptedEntityTweenerNotificationsBus::Events::OnUpdate, callback.m_callbackId, callback.m_callbackData, callback.m_progressPercent);
  333. break;
  334. case CallbackTypes::OnLoop:
  335. ScriptedEntityTweenerNotificationsBus::Broadcast(&ScriptedEntityTweenerNotificationsBus::Events::OnLoop, callback.m_callbackId);
  336. break;
  337. case CallbackTypes::RemoveCallback:
  338. ScriptedEntityTweenerNotificationsBus::Broadcast(&ScriptedEntityTweenerNotificationsBus::Events::RemoveCallback, callback.m_callbackId);
  339. break;
  340. }
  341. }
  342. }
  343. void ScriptedEntityTweenerTask::ClearCallbacks(const AnimationProperties& animationProperties)
  344. {
  345. AZStd::vector<int> callbackIds = { animationProperties.m_onCompleteCallbackId, animationProperties.m_onLoopCallbackId, animationProperties.m_onUpdateCallbackId };
  346. for (const auto& callbackId : callbackIds)
  347. {
  348. if (callbackId != AnimationProperties::InvalidCallbackId)
  349. {
  350. ScriptedEntityTweenerNotificationsBus::Broadcast(&ScriptedEntityTweenerNotificationsBus::Events::RemoveCallback, callbackId);
  351. }
  352. }
  353. }
  354. }