123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include "EditorDefs.h"
- #include "KeyUIControls.h"
- #include "TrackViewKeyPropertiesDlg.h" // for CTrackViewKeyUIControls
- #include <AzCore/Component/EntityBus.h> // for AZ::EntitySystemBus
- #include <AzFramework/Components/CameraBus.h> // for Camera::CameraNotificationBus
- #include <CryCommon/Maestro/Types/AnimValueType.h> // for AnimValueType
- CSelectKeyUIControls::~CSelectKeyUIControls()
- {
- AZ::EntitySystemBus::Handler::BusDisconnect();
- Camera::CameraNotificationBus::Handler::BusDisconnect();
- }
- //////////////////////////////////////////////////////////////////////////
- bool CSelectKeyUIControls::OnKeySelectionChange(const CTrackViewKeyBundle& selectedKeys)
- {
- if (!selectedKeys.AreAllKeysOfSameType())
- {
- return false;
- }
- bool bAssigned = false;
- if (selectedKeys.GetKeyCount() == 1)
- {
- const CTrackViewKeyHandle& keyHandle = selectedKeys.GetKey(0);
- AnimValueType valueType = keyHandle.GetTrack()->GetValueType();
- if (valueType == AnimValueType::Select)
- {
- ResetCameraEntries();
- // Get All cameras.
- mv_camera.SetEnumList(nullptr);
- mv_camera->AddEnumItem(QObject::tr("<None>"), QString::number(static_cast<AZ::u64>(AZ::EntityId::InvalidEntityId)));
- // Find all Component Entity Cameras
- AZ::EBusAggregateResults<AZ::EntityId> cameraComponentEntities;
- Camera::CameraBus::BroadcastResult(cameraComponentEntities, &Camera::CameraRequests::GetCameras);
- // add names of all found entities with Camera Components
- for (int i = 0; i < cameraComponentEntities.values.size(); i++)
- {
- AZ::Entity* entity = nullptr;
- AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, cameraComponentEntities.values[i]);
- if (entity)
- {
- // For Camera Components the enum value is the stringified AZ::EntityId of the entity with the Camera Component
- QString entityIdString = QString::number(static_cast<AZ::u64>(entity->GetId()));
- mv_camera->AddEnumItem(entity->GetName().c_str(), entityIdString);
- }
- }
- ISelectKey selectKey;
- keyHandle.GetKey(&selectKey);
- mv_camera = QString::number(static_cast<AZ::u64>(selectKey.cameraAzEntityId));
- mv_BlendTime.GetVar()->SetLimits(0.0f, selectKey.fDuration > .0f ? selectKey.fDuration : 1.0f, 0.1f, true, false);
- mv_BlendTime = selectKey.fBlendTime;
- bAssigned = true;
- }
- }
- return bAssigned;
- }
- // Called when UI variable changes.
- void CSelectKeyUIControls::OnUIChange(IVariable* pVar, CTrackViewKeyBundle& selectedKeys)
- {
- CTrackViewSequence* sequence = GetIEditor()->GetAnimation()->GetSequence();
- if (!sequence || !selectedKeys.AreAllKeysOfSameType())
- {
- return;
- }
- for (unsigned int keyIndex = 0; keyIndex < selectedKeys.GetKeyCount(); ++keyIndex)
- {
- CTrackViewKeyHandle keyHandle = selectedKeys.GetKey(keyIndex);
- AnimValueType valueType = keyHandle.GetTrack()->GetValueType();
- if (valueType == AnimValueType::Select)
- {
- ISelectKey selectKey;
- keyHandle.GetKey(&selectKey);
- if (pVar == mv_camera.GetVar())
- {
- QString entityIdString = mv_camera;
- selectKey.cameraAzEntityId = AZ::EntityId(entityIdString.toULongLong());
- selectKey.szSelection = mv_camera.GetVar()->GetDisplayValue().toUtf8().data();
- }
- if (pVar == mv_BlendTime.GetVar())
- {
- if (mv_BlendTime < 0.0f)
- {
- mv_BlendTime = 0.0f;
- }
- selectKey.fBlendTime = mv_BlendTime;
- }
- if (!selectKey.szSelection.empty())
- {
- IMovieSystem* movieSystem = AZ::Interface<IMovieSystem>::Get();
- if (movieSystem)
- {
- IAnimSequence* pSequence = movieSystem->FindLegacySequenceByName(selectKey.szSelection.c_str());
- if (pSequence)
- {
- selectKey.fDuration = pSequence->GetTimeRange().Length();
- }
- }
- }
- bool isDuringUndo = false;
- AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(isDuringUndo, &AzToolsFramework::ToolsApplicationRequests::Bus::Events::IsDuringUndoRedo);
- if (isDuringUndo)
- {
- keyHandle.SetKey(&selectKey);
- }
- else
- {
- AzToolsFramework::ScopedUndoBatch undoBatch("Set Key Value");
- keyHandle.SetKey(&selectKey);
- undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId());
- }
- }
- }
- }
- void CSelectKeyUIControls::OnCameraAdded(const AZ::EntityId & cameraId)
- {
- // Add a single camera component
- AZ::Entity* entity = nullptr;
- AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, cameraId);
- if (entity)
- {
- // For Camera Components the enum value is the stringified AZ::EntityId of the entity with the Camera Component
- QString entityIdString = QString::number(static_cast<AZ::u64>(entity->GetId()));
- mv_camera->AddEnumItem(entity->GetName().c_str(), entityIdString);
- }
- }
- void CSelectKeyUIControls::OnCameraRemoved(const AZ::EntityId & cameraId)
- {
- mv_camera->EnableUpdateCallbacks(false);
- // We can't iterate or remove an item from the enum list, and Camera::CameraRequests::GetCameras
- // still includes the deleted camera at this point. Reset the list anyway and filter out the
- // deleted camera.
- mv_camera->SetEnumList(nullptr);
- mv_camera->AddEnumItem(QObject::tr("<None>"), QString::number(static_cast<AZ::u64>(AZ::EntityId::InvalidEntityId)));
- AZ::EBusAggregateResults<AZ::EntityId> cameraComponentEntities;
- Camera::CameraBus::BroadcastResult(cameraComponentEntities, &Camera::CameraRequests::GetCameras);
- for (int i = 0; i < cameraComponentEntities.values.size(); i++)
- {
- if (cameraId == cameraComponentEntities.values[i])
- {
- continue;
- }
- OnCameraAdded(cameraComponentEntities.values[i]);
- }
- mv_camera->EnableUpdateCallbacks(true);
- }
- void CSelectKeyUIControls::OnEntityNameChanged(const AZ::EntityId & entityId, [[maybe_unused]] const AZStd::string & name)
- {
- AZ::Entity* entity = nullptr;
- AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
- if (entity == nullptr)
- {
- return;
- }
- AZ::Entity::ComponentArrayType cameraComponents = entity->FindComponents(EditorCameraComponentTypeId);
- if (cameraComponents.size() == 0)
- {
- return;
- }
- mv_camera->EnableUpdateCallbacks(false);
- ResetCameraEntries();
- mv_camera->EnableUpdateCallbacks(true);
- }
- void CSelectKeyUIControls::ResetCameraEntries()
- {
- mv_camera.SetEnumList(nullptr);
- mv_camera->AddEnumItem(QObject::tr("<None>"), QString::number(static_cast<AZ::u64>(AZ::EntityId::InvalidEntityId)));
- // Find all Component Entity Cameras
- AZ::EBusAggregateResults<AZ::EntityId> cameraComponentEntities;
- Camera::CameraBus::BroadcastResult(cameraComponentEntities, &Camera::CameraRequests::GetCameras);
- // add names of all found entities with Camera Components
- for (int i = 0; i < cameraComponentEntities.values.size(); i++)
- {
- OnCameraAdded(cameraComponentEntities.values[i]);
- }
- }
|