TrackViewKeyPropertiesDlg.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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"
  11. // Qt
  12. #include <QMessageBox>
  13. // AzCore
  14. #include <AzCore/std/sort.h>
  15. // CryCommon
  16. #include <CryCommon/Maestro/Types/AnimValueType.h>
  17. // Editor
  18. #include "Controls/ReflectedPropertyControl/ReflectedPropertyCtrl.h"
  19. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  20. #include <TrackView/ui_TrackViewTrackPropsDlg.h>
  21. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  22. void CTrackViewKeyUIControls::OnInternalVariableChange(IVariable* var)
  23. {
  24. CTrackViewSequence* sequence = GetIEditor()->GetAnimation()->GetSequence();
  25. AZ_Assert(sequence, "Expected valid sequence.");
  26. if (sequence)
  27. {
  28. CTrackViewKeyBundle keys = sequence->GetSelectedKeys();
  29. OnUIChange(var, keys);
  30. }
  31. }
  32. CTrackViewKeyPropertiesDlg::CTrackViewKeyPropertiesDlg(QWidget* hParentWnd)
  33. : QWidget(hParentWnd)
  34. , m_pLastTrackSelected(nullptr)
  35. , m_sequence(nullptr)
  36. {
  37. QVBoxLayout* l = new QVBoxLayout();
  38. l->setMargin(0);
  39. m_wndTrackProps = new CTrackViewTrackPropsDlg(this);
  40. l->addWidget(m_wndTrackProps);
  41. m_wndProps = new ReflectedPropertyControl(this);
  42. m_wndProps->Setup(true, 120);
  43. m_wndProps->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
  44. l->addWidget(m_wndProps);
  45. m_wndProps->SetStoreUndoByItems(false);
  46. setLayout(l);
  47. m_pVarBlock = new CVarBlock;
  48. // Add key UI classes
  49. // Compound tracks
  50. m_keyControls.push_back(new CQuatKeyUIControls());
  51. m_keyControls.push_back(new CRgbKeyUIControls());
  52. m_keyControls.push_back(new CVectorKeyUIControls());
  53. m_keyControls.push_back(new CVector4KeyUIControls());
  54. // Simple tracks
  55. m_keyControls.push_back(new C2DBezierKeyUIControls());
  56. m_keyControls.push_back(new CAssetBlendKeyUIControls());
  57. m_keyControls.push_back(new CCaptureKeyUIControls());
  58. m_keyControls.push_back(new CCommentKeyUIControls());
  59. m_keyControls.push_back(new CConsoleKeyUIControls());
  60. m_keyControls.push_back(new CEventKeyUIControls());
  61. m_keyControls.push_back(new CGotoKeyUIControls());
  62. m_keyControls.push_back(new CScreenFaderKeyUIControls());
  63. m_keyControls.push_back(new CSelectKeyUIControls());
  64. m_keyControls.push_back(new CSequenceKeyUIControls());
  65. m_keyControls.push_back(new CSoundKeyUIControls());
  66. m_keyControls.push_back(new CStringKeyUIControls());
  67. m_keyControls.push_back(new CTimeRangeKeyUIControls());
  68. m_keyControls.push_back(new CTrackEventKeyUIControls());
  69. // Sort key controls by descending priority
  70. AZStd::stable_sort(m_keyControls.begin(), m_keyControls.end(),
  71. [](const _smart_ptr<CTrackViewKeyUIControls>& a, const _smart_ptr<CTrackViewKeyUIControls>& b)
  72. {
  73. return a->GetPriority() > b->GetPriority();
  74. }
  75. );
  76. CreateAllVars();
  77. }
  78. void CTrackViewKeyPropertiesDlg::OnVarChange(IVariable* pVar)
  79. {
  80. // If it was a motion that just changed, we need to rebuild the controls
  81. // so the min/max on the sliders update correctly.
  82. if (m_sequence && pVar->GetDataType() == IVariable::DT_MOTION)
  83. {
  84. OnKeySelectionChanged(m_sequence);
  85. }
  86. }
  87. void CTrackViewKeyPropertiesDlg::CreateAllVars()
  88. {
  89. for (const auto& keyControl : m_keyControls)
  90. {
  91. keyControl->SetKeyPropertiesDlg(this);
  92. keyControl->OnCreateVars();
  93. }
  94. }
  95. void CTrackViewKeyPropertiesDlg::PopulateVariables()
  96. {
  97. // Must first clear any selection in properties window.
  98. m_wndProps->RemoveAllItems();
  99. m_wndProps->AddVarBlock(m_pVarBlock);
  100. m_wndProps->SetUpdateCallback([this](IVariable* var) { OnVarChange(var); });
  101. //m_wndProps->m_props.ExpandAll();
  102. ReloadValues();
  103. }
  104. void CTrackViewKeyPropertiesDlg::PopulateVariables(ReflectedPropertyControl* propCtrl)
  105. {
  106. propCtrl->RemoveAllItems();
  107. propCtrl->AddVarBlock(m_pVarBlock);
  108. propCtrl->ReloadValues();
  109. }
  110. void CTrackViewKeyPropertiesDlg::OnKeysChanged(CTrackViewSequence* pSequence)
  111. {
  112. const CTrackViewKeyBundle& selectedKeys = pSequence->GetSelectedKeys();
  113. const auto numSelectedKeys = selectedKeys.GetKeyCount();
  114. if (numSelectedKeys > 0 && selectedKeys.AreAllKeysOfSameType())
  115. {
  116. auto pTrack = selectedKeys.GetKey(0).GetTrack();
  117. if (!pTrack)
  118. {
  119. return;
  120. }
  121. const bool areSubTrackKeysSelected = (numSelectedKeys > 1) && pTrack->IsSubTrack();
  122. if (areSubTrackKeysSelected)
  123. {
  124. if (const auto pParentNode = pTrack->GetParentNode())
  125. {
  126. if (pParentNode->GetNodeType() == ETrackViewNodeType::eTVNT_Track)
  127. {
  128. pTrack = static_cast<CTrackViewTrack*>(pParentNode);
  129. }
  130. }
  131. }
  132. const CAnimParamType paramType = pTrack->GetParameterType();
  133. const EAnimCurveType trackType = pTrack->GetCurveType();
  134. const AnimValueType valueType = pTrack->GetValueType();
  135. for (const auto& keyControl : m_keyControls)
  136. {
  137. if (keyControl->SupportTrackType(paramType, trackType, valueType))
  138. {
  139. keyControl->OnKeySelectionChange(selectedKeys);
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. void CTrackViewKeyPropertiesDlg::OnKeySelectionChanged(CTrackViewSequence* sequence)
  146. {
  147. auto cleanup = [this]()
  148. {
  149. m_wndProps->ClearSelection();
  150. m_pVarBlock->DeleteAllVariables();
  151. m_wndProps->setEnabled(false);
  152. m_wndTrackProps->setEnabled(false);
  153. m_pLastTrackSelected = nullptr;
  154. };
  155. m_sequence = sequence;
  156. if (!sequence)
  157. {
  158. cleanup();
  159. return;
  160. }
  161. const CTrackViewKeyBundle& selectedKeys = sequence->GetSelectedKeys();
  162. const auto numSelectedKeys = selectedKeys.GetKeyCount();
  163. if (numSelectedKeys < 1 || !selectedKeys.AreAllKeysOfSameType())
  164. {
  165. cleanup();
  166. return;
  167. }
  168. m_wndTrackProps->OnKeySelectionChange(selectedKeys);
  169. const auto pFirstTrack = selectedKeys.GetKey(0).GetTrack();
  170. if (!pFirstTrack)
  171. {
  172. cleanup();
  173. return;
  174. }
  175. auto pTrack = pFirstTrack;
  176. // Check if a Compound track is selected with keys selected in sub-tracks?
  177. if ((numSelectedKeys > 1) && pFirstTrack->IsSubTrack())
  178. {
  179. if (const auto pParentNode = pFirstTrack->GetParentNode())
  180. {
  181. if (pParentNode->GetNodeType() == ETrackViewNodeType::eTVNT_Track)
  182. {
  183. pTrack = static_cast<CTrackViewTrack*>(pParentNode);
  184. }
  185. }
  186. }
  187. const bool bSelectChangedInSameTrack = m_pLastTrackSelected && pTrack == m_pLastTrackSelected;
  188. // Every Key in an Asset Blend track can have different min/max values on the float sliders
  189. // because it's based on the duration of the motion that is set. So don't try to
  190. // reuse the controls when the selection changes, otherwise the tooltips may be wrong.
  191. const bool reuseControls = bSelectChangedInSameTrack && m_pLastTrackSelected && (m_pLastTrackSelected->GetValueType() != AnimValueType::AssetBlend);
  192. m_pLastTrackSelected = pTrack;
  193. if (reuseControls)
  194. {
  195. m_wndProps->ClearSelection();
  196. }
  197. else
  198. {
  199. m_pVarBlock->DeleteAllVariables();
  200. }
  201. m_wndProps->setEnabled(false);
  202. m_wndTrackProps->setEnabled(false);
  203. if (selectedKeys.GetKeyCount() > 0 && selectedKeys.AreAllKeysOfSameType())
  204. {
  205. if (!reuseControls)
  206. {
  207. const CAnimParamType paramType = pTrack->GetParameterType();
  208. const EAnimCurveType trackType = pTrack->GetCurveType();
  209. const AnimValueType valueType = pTrack->GetValueType();
  210. for (const auto& keyControl : m_keyControls)
  211. {
  212. if (keyControl->SupportTrackType(paramType, trackType, valueType))
  213. {
  214. AddVars(keyControl);
  215. break;
  216. }
  217. }
  218. }
  219. m_wndProps->setEnabled(true);
  220. m_wndTrackProps->setEnabled(true);
  221. }
  222. else
  223. {
  224. m_wndProps->setEnabled(false);
  225. m_wndTrackProps->setEnabled(false);
  226. }
  227. if (reuseControls)
  228. {
  229. ReloadValues();
  230. }
  231. else
  232. {
  233. PopulateVariables();
  234. }
  235. OnKeysChanged(sequence);
  236. }
  237. void CTrackViewKeyPropertiesDlg::AddVars(CTrackViewKeyUIControls* pUI)
  238. {
  239. CVarBlock* pVB = pUI->GetVarBlock();
  240. for (int i = 0, num = pVB->GetNumVariables(); i < num; i++)
  241. {
  242. IVariable* pVar = pVB->GetVariable(i);
  243. m_pVarBlock->AddVariable(pVar);
  244. }
  245. }
  246. void CTrackViewKeyPropertiesDlg::ReloadValues()
  247. {
  248. m_wndProps->ReloadValues();
  249. }
  250. void CTrackViewKeyPropertiesDlg::OnSequenceChanged(CTrackViewSequence* sequence)
  251. {
  252. OnKeySelectionChanged(sequence);
  253. m_wndTrackProps->OnSequenceChanged();
  254. }
  255. CTrackViewTrackPropsDlg::CTrackViewTrackPropsDlg(QWidget* parent /* = 0 */)
  256. : QWidget(parent)
  257. , ui(new Ui::CTrackViewTrackPropsDlg)
  258. {
  259. ui->setupUi(this);
  260. // Use editingFinished and a custom signal stepByFinished (and not valueChanged)
  261. // so the time will be updated when the user finishes editing the time field (hits enter)
  262. // or if the arrow keys (or mouse click on the arrow buttons) in the spinner box are
  263. // pressed. Don't just use valueChanged because we don't want intermediate values
  264. // like 1 as the user is typing 10 to register as updates to the key values. Keys
  265. // are identified by time, so the keys jumping around like that can stomp existing keys
  266. // that happen to live at the intermediate values.
  267. connect(ui->TIME, SIGNAL(editingFinished()), this, SLOT(OnUpdateTime()));
  268. connect(ui->TIME, SIGNAL(stepByFinished()), this, SLOT(OnUpdateTime()));
  269. }
  270. CTrackViewTrackPropsDlg::~CTrackViewTrackPropsDlg()
  271. {
  272. }
  273. void CTrackViewTrackPropsDlg::OnSequenceChanged()
  274. {
  275. CTrackViewSequence* pSequence = GetIEditor()->GetAnimation()->GetSequence();
  276. if (pSequence)
  277. {
  278. Range range = pSequence->GetTimeRange();
  279. ui->TIME->setRange(range.start, range.end);
  280. }
  281. }
  282. bool CTrackViewTrackPropsDlg::OnKeySelectionChange(const CTrackViewKeyBundle& selectedKeys)
  283. {
  284. auto keyHandle = selectedKeys.GetSingleSelectedKey();
  285. if (keyHandle.IsValid())
  286. {
  287. m_selectedKeys = selectedKeys;
  288. // Block the callback, the values is already set in m_keyHandle.GetTime(), no need to
  289. // reset it and create an undo even like the user was setting it via the UI.
  290. ui->TIME->blockSignals(true);
  291. ui->TIME->setValue(keyHandle.GetTime());
  292. ui->TIME->blockSignals(false);
  293. ui->PREVNEXT->setText(QString::number(keyHandle.GetIndex() + 1));
  294. ui->PREVNEXT->setEnabled(true);
  295. ui->TIME->setEnabled(true);
  296. }
  297. else
  298. {
  299. m_selectedKeys = CTrackViewKeyBundle();
  300. ui->PREVNEXT->setText(QString());
  301. ui->PREVNEXT->setEnabled(false);
  302. ui->TIME->setEnabled(false);
  303. }
  304. return keyHandle.IsValid();
  305. }
  306. void CTrackViewTrackPropsDlg::OnUpdateTime()
  307. {
  308. if (m_selectedKeys.GetKeyCount() < 1)
  309. {
  310. return;
  311. }
  312. const bool isDuringUndo = AzToolsFramework::UndoRedoOperationInProgress();
  313. AZStd::unique_ptr<AzToolsFramework::ScopedUndoBatch> undoBatch;
  314. if (!isDuringUndo)
  315. {
  316. undoBatch = AZStd::make_unique<AzToolsFramework::ScopedUndoBatch>("Change Keys Time");
  317. }
  318. const float time = (float)ui->TIME->value();
  319. bool allowDeletingKeys = false;
  320. for (unsigned int i = 0; i < m_selectedKeys.GetKeyCount(); ++i)
  321. {
  322. auto keyHandle = m_selectedKeys.GetKey(i);
  323. auto track = keyHandle.GetTrack();
  324. if (!keyHandle.IsValid() || !track)
  325. {
  326. AZ_Assert(false, "Invalid key handle for a selected key %u.", i);
  327. continue;
  328. }
  329. if (AZ::IsClose(keyHandle.GetTime(), time, AZ::Constants::FloatEpsilon))
  330. {
  331. continue; // nothing to do
  332. }
  333. // Check if the sequence is legacy
  334. const auto sequence = track->GetSequence();
  335. if (!sequence)
  336. {
  337. AZ_Assert(false, "Each track should have a sequence, invalid key handle for a selected key %u.", i);
  338. continue;
  339. }
  340. const auto sequenceId = sequence->GetSequenceComponentEntityId();
  341. if (!isDuringUndo)
  342. {
  343. CTrackViewKeyHandle existingKey = track->GetKeyByTime(time);
  344. // If there is an existing key at this time, remove it so the
  345. // new key at this time is the only one here. Make sure it's actually a different
  346. // key, because time can "change" but then be quantized (or snapped) to the same time by track->GetKeyByTime(time).
  347. if (existingKey.IsValid() && (existingKey.GetIndex() != keyHandle.GetIndex()))
  348. {
  349. // Save the old time before we set the new time so we
  350. // can reselect the m_keyHandle after the Delete.
  351. float currentTime = keyHandle.GetTime();
  352. if (!allowDeletingKeys) // Open dialog asking to allow deleting existing key - only once.
  353. {
  354. // There is a bug in QT where editingFinished will get fired a second time if we show a QMessageBox
  355. // so work around it by blocking signal before we do it.
  356. ui->TIME->blockSignals(true);
  357. QString msgBody = "There is an existing key at the specified time. If you continue, the existing key will be removed.";
  358. if (QMessageBox::warning(this, "Overwrite Existing Key?", msgBody, QMessageBox::Cancel | QMessageBox::Yes) ==
  359. QMessageBox::Cancel)
  360. {
  361. // Restore the old value and return.
  362. ui->TIME->setValue(currentTime);
  363. ui->TIME->blockSignals(false);
  364. return;
  365. }
  366. else
  367. {
  368. allowDeletingKeys = true;
  369. ui->TIME->blockSignals(false);
  370. }
  371. }
  372. // Delete the key that is able to get replaced. This will
  373. // cause a sort and may cause m_keyHandle to become invalid.
  374. existingKey.Delete();
  375. // Reselect the key handle by time.
  376. keyHandle = track->GetKeyByTime(currentTime);
  377. keyHandle.SetTime(time);
  378. if (undoBatch)
  379. {
  380. undoBatch->MarkEntityDirty(sequenceId);
  381. }
  382. m_selectedKeys = sequence->GetSelectedKeys(); // Re-save selected keys as key index has been changed.
  383. }
  384. else
  385. {
  386. keyHandle.SetTime(time);
  387. if (undoBatch)
  388. {
  389. undoBatch->MarkEntityDirty(sequenceId);
  390. }
  391. }
  392. }
  393. else // During Undo/Redo
  394. {
  395. keyHandle.SetTime(time);
  396. }
  397. }
  398. }
  399. #include <TrackView/moc_TrackViewKeyPropertiesDlg.cpp>