TrackEventKeyUIControls.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // CryCommon
  10. #include <CryCommon/Maestro/Types/AnimParamType.h>
  11. // Editor
  12. #include "KeyUIControls.h"
  13. #include "TrackViewKeyPropertiesDlg.h"
  14. #include "TVEventsDialog.h"
  15. //////////////////////////////////////////////////////////////////////////
  16. bool CTrackEventKeyUIControls::OnKeySelectionChange(const CTrackViewKeyBundle& selectedKeys)
  17. {
  18. if (!selectedKeys.AreAllKeysOfSameType())
  19. {
  20. return false;
  21. }
  22. bool bAssigned = false;
  23. if (selectedKeys.GetKeyCount() == 1)
  24. {
  25. const CTrackViewKeyHandle& keyHandle = selectedKeys.GetKey(0);
  26. CAnimParamType paramType = keyHandle.GetTrack()->GetParameterType();
  27. if (paramType == AnimParamType::TrackEvent)
  28. {
  29. IEventKey eventKey;
  30. keyHandle.GetKey(&eventKey);
  31. // Provide builder with current event value to ensure
  32. // dropdown is displayed properly and value is updated if not found
  33. QString event = eventKey.event.c_str();
  34. BuildEventDropDown(event);
  35. mv_event = event;
  36. mv_value = eventKey.eventValue.c_str();
  37. bAssigned = true;
  38. }
  39. }
  40. m_lastEvent = mv_event;
  41. return bAssigned;
  42. }
  43. // Called when UI variable changes.
  44. void CTrackEventKeyUIControls::OnUIChange(IVariable* pVar, CTrackViewKeyBundle& selectedKeys)
  45. {
  46. CTrackViewSequence* pSequence = GetIEditor()->GetAnimation()->GetSequence();
  47. if (!pSequence || !selectedKeys.AreAllKeysOfSameType())
  48. {
  49. return;
  50. }
  51. if (mv_event == GetAddEventString())
  52. {
  53. mv_event = m_lastEvent;
  54. OnEventEdit();
  55. return;
  56. }
  57. if (mv_event == "___spacer___")
  58. {
  59. mv_event = m_lastEvent;
  60. return;
  61. }
  62. for (unsigned int keyIndex = 0; keyIndex < selectedKeys.GetKeyCount(); ++keyIndex)
  63. {
  64. CTrackViewKeyHandle keyHandle = selectedKeys.GetKey(keyIndex);
  65. CAnimParamType paramType = keyHandle.GetTrack()->GetParameterType();
  66. if (paramType == AnimParamType::TrackEvent)
  67. {
  68. IEventKey eventKey;
  69. keyHandle.GetKey(&eventKey);
  70. QByteArray event, value;
  71. event = static_cast<QString>(mv_event).toUtf8();
  72. value = static_cast<QString>(mv_value).toUtf8();
  73. if (pVar == mv_event.GetVar())
  74. {
  75. eventKey.event = event.data();
  76. }
  77. if (pVar == mv_value.GetVar())
  78. {
  79. eventKey.eventValue = value.data();
  80. }
  81. eventKey.animation = "";
  82. eventKey.duration = 0;
  83. keyHandle.SetKey(&eventKey);
  84. }
  85. }
  86. m_lastEvent = mv_event;
  87. }
  88. void CTrackEventKeyUIControls::OnEventEdit()
  89. {
  90. // Create dialog
  91. CTVEventsDialog dlg;
  92. dlg.exec();
  93. QString event = mv_event;
  94. BuildEventDropDown(event, dlg.GetLastAddedEvent());
  95. // The step below is necessary to make the event drop-down up-to-date.
  96. mv_event.GetVar()->EnableNotifyWithoutValueChange(true);
  97. mv_event = event;
  98. mv_event.GetVar()->EnableNotifyWithoutValueChange(false);
  99. }
  100. void CTrackEventKeyUIControls::BuildEventDropDown(QString& curEvent, const QString& addedEvent)
  101. {
  102. if (CAnimationContext* context = GetIEditor()->GetAnimation())
  103. {
  104. CTrackViewSequence* sequence = context->GetSequence();
  105. if (sequence)
  106. {
  107. bool curEventExists = false;
  108. bool addedEventExists = false;
  109. mv_event.SetEnumList(nullptr);
  110. const int eventCount = sequence->GetTrackEventsCount();
  111. // Need to check if event exists before adding all events
  112. // This handles the case where the current event got deleted in the dialog but no new events were added
  113. for (int i = 0; i < eventCount; ++i)
  114. {
  115. const char* trackEvent = sequence->GetTrackEvent(i);
  116. if (curEvent == trackEvent)
  117. {
  118. curEventExists = true;
  119. }
  120. if (addedEvent == trackEvent)
  121. {
  122. addedEventExists = true;
  123. }
  124. if (curEventExists && addedEventExists)
  125. {
  126. break;
  127. }
  128. }
  129. if (!curEventExists)
  130. {
  131. if (addedEventExists)
  132. {
  133. // Set added event if key not set
  134. curEvent = addedEvent;
  135. }
  136. else
  137. {
  138. mv_event->AddEnumItem(QObject::tr("<None>"), "");
  139. curEvent = "";
  140. }
  141. }
  142. // Add Events
  143. for (int i = 0; i < eventCount; ++i)
  144. {
  145. const char* trackEvent = sequence->GetTrackEvent(i);
  146. mv_event->AddEnumItem(trackEvent, trackEvent);
  147. }
  148. // Used as a spacer to make Add a new event... standout
  149. mv_event->AddEnumItem(QObject::tr(""), "___spacer___");
  150. // Add a new event... to open event editor when selected
  151. mv_event->AddEnumItem(QObject::tr(GetAddEventString()), GetAddEventString());
  152. }
  153. }
  154. }