TrackViewKeyPropertiesDlg.cpp 14 KB

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