SelectKeyUIControls.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "EditorDefs.h"
  9. #include "KeyUIControls.h"
  10. #include "TrackViewKeyPropertiesDlg.h" // for CTrackViewKeyUIControls
  11. #include <AzCore/Component/EntityBus.h> // for AZ::EntitySystemBus
  12. #include <AzFramework/Components/CameraBus.h> // for Camera::CameraNotificationBus
  13. #include <CryCommon/Maestro/Types/AnimValueType.h> // for AnimValueType
  14. CSelectKeyUIControls::~CSelectKeyUIControls()
  15. {
  16. AZ::EntitySystemBus::Handler::BusDisconnect();
  17. Camera::CameraNotificationBus::Handler::BusDisconnect();
  18. }
  19. //////////////////////////////////////////////////////////////////////////
  20. bool CSelectKeyUIControls::OnKeySelectionChange(const CTrackViewKeyBundle& selectedKeys)
  21. {
  22. if (!selectedKeys.AreAllKeysOfSameType())
  23. {
  24. return false;
  25. }
  26. bool bAssigned = false;
  27. if (selectedKeys.GetKeyCount() == 1)
  28. {
  29. const CTrackViewKeyHandle& keyHandle = selectedKeys.GetKey(0);
  30. AnimValueType valueType = keyHandle.GetTrack()->GetValueType();
  31. if (valueType == AnimValueType::Select)
  32. {
  33. ResetCameraEntries();
  34. // Get All cameras.
  35. mv_camera.SetEnumList(nullptr);
  36. mv_camera->AddEnumItem(QObject::tr("<None>"), QString::number(static_cast<AZ::u64>(AZ::EntityId::InvalidEntityId)));
  37. // Find all Component Entity Cameras
  38. AZ::EBusAggregateResults<AZ::EntityId> cameraComponentEntities;
  39. Camera::CameraBus::BroadcastResult(cameraComponentEntities, &Camera::CameraRequests::GetCameras);
  40. // add names of all found entities with Camera Components
  41. for (int i = 0; i < cameraComponentEntities.values.size(); i++)
  42. {
  43. AZ::Entity* entity = nullptr;
  44. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, cameraComponentEntities.values[i]);
  45. if (entity)
  46. {
  47. // For Camera Components the enum value is the stringified AZ::EntityId of the entity with the Camera Component
  48. QString entityIdString = QString::number(static_cast<AZ::u64>(entity->GetId()));
  49. mv_camera->AddEnumItem(entity->GetName().c_str(), entityIdString);
  50. }
  51. }
  52. ISelectKey selectKey;
  53. keyHandle.GetKey(&selectKey);
  54. mv_camera = QString::number(static_cast<AZ::u64>(selectKey.cameraAzEntityId));
  55. mv_BlendTime.GetVar()->SetLimits(0.0f, selectKey.fDuration > .0f ? selectKey.fDuration : 1.0f, 0.1f, true, false);
  56. mv_BlendTime = selectKey.fBlendTime;
  57. bAssigned = true;
  58. }
  59. }
  60. return bAssigned;
  61. }
  62. // Called when UI variable changes.
  63. void CSelectKeyUIControls::OnUIChange(IVariable* pVar, CTrackViewKeyBundle& selectedKeys)
  64. {
  65. CTrackViewSequence* sequence = GetIEditor()->GetAnimation()->GetSequence();
  66. if (!sequence || !selectedKeys.AreAllKeysOfSameType())
  67. {
  68. return;
  69. }
  70. for (unsigned int keyIndex = 0; keyIndex < selectedKeys.GetKeyCount(); ++keyIndex)
  71. {
  72. CTrackViewKeyHandle keyHandle = selectedKeys.GetKey(keyIndex);
  73. AnimValueType valueType = keyHandle.GetTrack()->GetValueType();
  74. if (valueType == AnimValueType::Select)
  75. {
  76. ISelectKey selectKey;
  77. keyHandle.GetKey(&selectKey);
  78. if (pVar == mv_camera.GetVar())
  79. {
  80. QString entityIdString = mv_camera;
  81. selectKey.cameraAzEntityId = AZ::EntityId(entityIdString.toULongLong());
  82. selectKey.szSelection = mv_camera.GetVar()->GetDisplayValue().toUtf8().data();
  83. }
  84. if (pVar == mv_BlendTime.GetVar())
  85. {
  86. if (mv_BlendTime < 0.0f)
  87. {
  88. mv_BlendTime = 0.0f;
  89. }
  90. selectKey.fBlendTime = mv_BlendTime;
  91. }
  92. if (!selectKey.szSelection.empty())
  93. {
  94. IMovieSystem* movieSystem = AZ::Interface<IMovieSystem>::Get();
  95. if (movieSystem)
  96. {
  97. IAnimSequence* pSequence = movieSystem->FindLegacySequenceByName(selectKey.szSelection.c_str());
  98. if (pSequence)
  99. {
  100. selectKey.fDuration = pSequence->GetTimeRange().Length();
  101. }
  102. }
  103. }
  104. bool isDuringUndo = false;
  105. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(isDuringUndo, &AzToolsFramework::ToolsApplicationRequests::Bus::Events::IsDuringUndoRedo);
  106. if (isDuringUndo)
  107. {
  108. keyHandle.SetKey(&selectKey);
  109. }
  110. else
  111. {
  112. AzToolsFramework::ScopedUndoBatch undoBatch("Set Key Value");
  113. keyHandle.SetKey(&selectKey);
  114. undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId());
  115. }
  116. }
  117. }
  118. }
  119. void CSelectKeyUIControls::OnCameraAdded(const AZ::EntityId & cameraId)
  120. {
  121. // Add a single camera component
  122. AZ::Entity* entity = nullptr;
  123. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, cameraId);
  124. if (entity)
  125. {
  126. // For Camera Components the enum value is the stringified AZ::EntityId of the entity with the Camera Component
  127. QString entityIdString = QString::number(static_cast<AZ::u64>(entity->GetId()));
  128. mv_camera->AddEnumItem(entity->GetName().c_str(), entityIdString);
  129. }
  130. }
  131. void CSelectKeyUIControls::OnCameraRemoved(const AZ::EntityId & cameraId)
  132. {
  133. mv_camera->EnableUpdateCallbacks(false);
  134. // We can't iterate or remove an item from the enum list, and Camera::CameraRequests::GetCameras
  135. // still includes the deleted camera at this point. Reset the list anyway and filter out the
  136. // deleted camera.
  137. mv_camera->SetEnumList(nullptr);
  138. mv_camera->AddEnumItem(QObject::tr("<None>"), QString::number(static_cast<AZ::u64>(AZ::EntityId::InvalidEntityId)));
  139. AZ::EBusAggregateResults<AZ::EntityId> cameraComponentEntities;
  140. Camera::CameraBus::BroadcastResult(cameraComponentEntities, &Camera::CameraRequests::GetCameras);
  141. for (int i = 0; i < cameraComponentEntities.values.size(); i++)
  142. {
  143. if (cameraId == cameraComponentEntities.values[i])
  144. {
  145. continue;
  146. }
  147. OnCameraAdded(cameraComponentEntities.values[i]);
  148. }
  149. mv_camera->EnableUpdateCallbacks(true);
  150. }
  151. void CSelectKeyUIControls::OnEntityNameChanged(const AZ::EntityId & entityId, [[maybe_unused]] const AZStd::string & name)
  152. {
  153. AZ::Entity* entity = nullptr;
  154. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
  155. if (entity == nullptr)
  156. {
  157. return;
  158. }
  159. AZ::Entity::ComponentArrayType cameraComponents = entity->FindComponents(EditorCameraComponentTypeId);
  160. if (cameraComponents.size() == 0)
  161. {
  162. return;
  163. }
  164. mv_camera->EnableUpdateCallbacks(false);
  165. ResetCameraEntries();
  166. mv_camera->EnableUpdateCallbacks(true);
  167. }
  168. void CSelectKeyUIControls::ResetCameraEntries()
  169. {
  170. mv_camera.SetEnumList(nullptr);
  171. mv_camera->AddEnumItem(QObject::tr("<None>"), QString::number(static_cast<AZ::u64>(AZ::EntityId::InvalidEntityId)));
  172. // Find all Component Entity Cameras
  173. AZ::EBusAggregateResults<AZ::EntityId> cameraComponentEntities;
  174. Camera::CameraBus::BroadcastResult(cameraComponentEntities, &Camera::CameraRequests::GetCameras);
  175. // add names of all found entities with Camera Components
  176. for (int i = 0; i < cameraComponentEntities.values.size(); i++)
  177. {
  178. OnCameraAdded(cameraComponentEntities.values[i]);
  179. }
  180. }