TrackEventKeyUIControls.cpp 5.2 KB

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