SelectKeyUIControls.cpp 7.9 KB

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