UiAnimViewDopeSheetBase.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124
  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 "Editor/Resource.h"
  10. #include "UiEditorAnimationBus.h"
  11. #include "UiAnimViewDopeSheetBase.h"
  12. #include "UiAnimViewDialog.h"
  13. #include "AnimationContext.h"
  14. #include "UiAnimViewUndo.h"
  15. #include "UiAVCustomizeTrackColorsDlg.h"
  16. #include "UiAnimViewAnimNode.h"
  17. #include "UiAnimViewTrack.h"
  18. #include "UiAnimViewSequence.h"
  19. #include "Clipboard.h"
  20. #include <Editor/Util/fastlib.h>
  21. #include <QMenu>
  22. #include <QPainter>
  23. #include <QPaintEvent>
  24. #include <QRubberBand>
  25. #include <QScrollBar>
  26. #include <QStaticText>
  27. #include <QToolTip>
  28. #if defined(Q_OS_WIN)
  29. #include <QtWinExtras/QtWin>
  30. #endif
  31. #include <AzQtComponents/Components/Widgets/ColorPicker.h>
  32. #define EDIT_DISABLE_GRAY_COLOR QColor(128, 128, 128)
  33. #define KEY_TEXT_COLOR QColor(0, 0, 50)
  34. #define INACTIVE_TEXT_COLOR QColor(128, 128, 128)
  35. namespace
  36. {
  37. const int kMarginForMagnetSnapping = 10;
  38. const unsigned int kDefaultTrackHeight = 16;
  39. }
  40. enum EUiAVMouseMode
  41. {
  42. eUiAVMouseMode_None = 0,
  43. eUiAVMouseMode_Select = 1,
  44. eUiAVMouseMode_Move,
  45. eUiAVMouseMode_Clone,
  46. eUiAVMouseMode_DragTime,
  47. eUiAVMouseMode_DragStartMarker,
  48. eUiAVMouseMode_DragEndMarker,
  49. eUiAVMouseMode_Paste,
  50. eUiAVMouseMode_SelectWithinTime,
  51. eUiAVMouseMode_StartTimeAdjust,
  52. eUiAVMouseMode_EndTimeAdjust
  53. };
  54. //////////////////////////////////////////////////////////////////////////
  55. CUiAnimViewDopeSheetBase::CUiAnimViewDopeSheetBase(QWidget* parent)
  56. : QWidget(parent)
  57. {
  58. m_bkgrBrush = palette().color(QPalette::Window);
  59. m_bkgrBrushEmpty = QColor(190, 190, 190);
  60. m_timeBkgBrush = QColor(0xE0, 0xE0, 0xE0);
  61. m_timeHighlightBrush = QColor(0xFF, 0x0, 0x0);
  62. m_selectedBrush = QColor(200, 200, 230);
  63. m_visibilityBrush = QColor(120, 120, 255);
  64. m_selectTrackBrush = QColor(100, 190, 255);
  65. m_timeScale = 1.0f;
  66. m_ticksStep = 10;
  67. m_bZoomDrag = false;
  68. m_bMoveDrag = false;
  69. m_leftOffset = 30;
  70. m_scrollOffset = QPoint(0, 0);
  71. m_mouseMode = eUiAVMouseMode_None;
  72. m_currentTime = 0.0f;
  73. m_storedTime = m_currentTime;
  74. m_rcSelect = QRect(0, 0, 0, 0);
  75. m_rubberBand = 0;
  76. m_scrollBar = new QScrollBar(Qt::Horizontal, this);
  77. connect(m_scrollBar, &QScrollBar::valueChanged, this, &CUiAnimViewDopeSheetBase::OnHScroll);
  78. m_keyTimeOffset = 0;
  79. m_currCursor = QCursor(Qt::ArrowCursor);
  80. m_mouseActionMode = eUiAVActionMode_MoveKey;
  81. m_scrollMin = 0;
  82. m_scrollMax = 1000;
  83. m_descriptionFont = QFont(QStringLiteral("Verdana"), 7);
  84. m_bCursorWasInKey = false;
  85. m_bJustSelected = false;
  86. m_snappingMode = eSnappingMode_SnapNone;
  87. m_snapFrameTime = 0.033333f;
  88. m_bMouseMovedAfterRButtonDown = false;
  89. m_tickDisplayMode = eUiAVTickMode_InSeconds;
  90. m_bEditLock = false;
  91. m_bFastRedraw = false;
  92. m_pLastTrackSelectedOnSpot = NULL;
  93. #ifdef DEBUG
  94. m_redrawCount = 0;
  95. #endif
  96. m_bKeysMoved = false;
  97. ComputeFrameSteps(GetVisibleRange());
  98. m_crsLeftRight = Qt::SizeHorCursor;
  99. m_crsAddKey = CMFCUtils::LoadCursor(IDC_ARROW_ADDKEY);
  100. m_crsCross = CMFCUtils::LoadCursor(IDC_POINTER_OBJHIT);
  101. m_crsAdjustLR = CMFCUtils::LoadCursor(IDC_LEFTRIGHT);
  102. setMouseTracking(true);
  103. setFocusPolicy(Qt::StrongFocus);
  104. }
  105. //////////////////////////////////////////////////////////////////////////
  106. CUiAnimViewDopeSheetBase::~CUiAnimViewDopeSheetBase()
  107. {
  108. CUiAnimationContext* pAnimationContext = nullptr;
  109. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  110. pAnimationContext->RemoveListener(this);
  111. }
  112. //////////////////////////////////////////////////////////////////////////
  113. int CUiAnimViewDopeSheetBase::TimeToClient(float time) const
  114. {
  115. return static_cast<int>(m_leftOffset - m_scrollOffset.x() + (time * m_timeScale));
  116. }
  117. //////////////////////////////////////////////////////////////////////////
  118. Range CUiAnimViewDopeSheetBase::GetVisibleRange() const
  119. {
  120. Range r;
  121. r.start = (m_scrollOffset.x() - m_leftOffset) / m_timeScale;
  122. r.end = r.start + (m_rcClient.width()) / m_timeScale;
  123. Range extendedTimeRange(0.0f, m_timeRange.end);
  124. r = extendedTimeRange & r;
  125. return r;
  126. }
  127. //////////////////////////////////////////////////////////////////////////
  128. Range CUiAnimViewDopeSheetBase::GetTimeRange(const QRect& rc) const
  129. {
  130. Range r;
  131. r.start = (rc.left() - m_leftOffset + m_scrollOffset.x()) / m_timeScale;
  132. r.end = r.start + (rc.width()) / m_timeScale;
  133. r.start = TickSnap(r.start);
  134. r.end = TickSnap(r.end);
  135. // Intersect range with global time range.
  136. r = m_timeRange & r;
  137. return r;
  138. }
  139. //////////////////////////////////////////////////////////////////////////
  140. void CUiAnimViewDopeSheetBase::SetTimeRange(float start, float end)
  141. {
  142. if (m_timeMarked.start < start)
  143. {
  144. m_timeMarked.start = start;
  145. }
  146. if (m_timeMarked.end > end)
  147. {
  148. m_timeMarked.end = end;
  149. }
  150. m_timeRange.Set(start, end);
  151. SetHorizontalExtent(-m_leftOffset, static_cast<int>(m_timeRange.end * m_timeScale - m_leftOffset));
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. void CUiAnimViewDopeSheetBase::SetTimeScale(float timeScale, float fAnchorTime)
  155. {
  156. const double fOldOffset = -fAnchorTime * m_timeScale;
  157. timeScale = std::max(timeScale, 0.001f);
  158. timeScale = std::min(timeScale, 100000.0f);
  159. m_timeScale = timeScale;
  160. int steps = 0;
  161. if (GetTickDisplayMode() == eUiAVTickMode_InSeconds)
  162. {
  163. m_ticksStep = 10;
  164. }
  165. else if (GetTickDisplayMode() == eUiAVTickMode_InFrames)
  166. {
  167. m_ticksStep = 1 / m_snapFrameTime;
  168. }
  169. else
  170. {
  171. assert(0);
  172. }
  173. double fPixelsPerTick;
  174. do
  175. {
  176. fPixelsPerTick = (1.0 / m_ticksStep) * (double)m_timeScale;
  177. if (fPixelsPerTick < 6.0)
  178. {
  179. m_ticksStep /= 2;
  180. }
  181. if (m_ticksStep <= 0)
  182. {
  183. m_ticksStep = 1;
  184. break;
  185. }
  186. steps++;
  187. }
  188. while (fPixelsPerTick < 6.0 && steps < 100);
  189. steps = 0;
  190. do
  191. {
  192. fPixelsPerTick = (1.0 / m_ticksStep) * (double)m_timeScale;
  193. if (fPixelsPerTick >= 12.0)
  194. {
  195. m_ticksStep *= 2;
  196. }
  197. if (m_ticksStep <= 0)
  198. {
  199. m_ticksStep = 1;
  200. break;
  201. }
  202. steps++;
  203. }
  204. while (fPixelsPerTick >= 12.0 && steps < 100);
  205. float fCurrentOffset = -fAnchorTime * m_timeScale;
  206. m_scrollOffset.rx() += static_cast<int>(fOldOffset - fCurrentOffset);
  207. update();
  208. SetHorizontalExtent(-m_leftOffset, static_cast<int>(m_timeRange.end * m_timeScale));
  209. ComputeFrameSteps(GetVisibleRange());
  210. }
  211. void CUiAnimViewDopeSheetBase::showEvent(QShowEvent* event)
  212. {
  213. QWidget::showEvent(event);
  214. CUiAnimationContext* pAnimationContext = nullptr;
  215. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  216. pAnimationContext->AddListener(this);
  217. }
  218. //////////////////////////////////////////////////////////////////////////
  219. void CUiAnimViewDopeSheetBase::resizeEvent(QResizeEvent* event)
  220. {
  221. QWidget::resizeEvent(event);
  222. m_rcClient = rect();
  223. m_offscreenBitmap = QPixmap(m_rcClient.width(), m_rcClient.height());
  224. m_offscreenBitmap.fill(Qt::transparent);
  225. m_rcTimeline = rect();
  226. m_rcTimeline.setHeight(kDefaultTrackHeight);
  227. m_rcSummary = m_rcTimeline;
  228. m_rcSummary.setTop(m_rcTimeline.bottom());
  229. m_rcSummary.setBottom(m_rcSummary.top() + 8);
  230. SetHorizontalExtent(m_scrollMin, m_scrollMax);
  231. m_scrollBar->setGeometry(0, height() - m_scrollBar->sizeHint().height(), width(), m_scrollBar->sizeHint().height());
  232. QToolTip::hideText();
  233. }
  234. //////////////////////////////////////////////////////////////////////////
  235. void CUiAnimViewDopeSheetBase::wheelEvent(QWheelEvent* event)
  236. {
  237. CUiAnimViewSequence* pSequence = nullptr;
  238. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  239. if (!pSequence)
  240. {
  241. event->ignore();
  242. return;
  243. }
  244. float z = (event->angleDelta().y() > 0) ? (m_timeScale * 1.25f) : (m_timeScale * 0.8f);
  245. const QPoint pt = event->position().toPoint();
  246. float fAnchorTime = TimeFromPointUnsnapped(pt);
  247. SetTimeScale(z, fAnchorTime);
  248. event->accept();
  249. }
  250. //////////////////////////////////////////////////////////////////////////
  251. void CUiAnimViewDopeSheetBase::OnHScroll()
  252. {
  253. // Get the current position of scroll box.
  254. int curpos = m_scrollBar->value();
  255. m_scrollOffset.setX(curpos);
  256. update();
  257. }
  258. int CUiAnimViewDopeSheetBase::GetScrollPos() const
  259. {
  260. return m_scrollBar->value();
  261. }
  262. //////////////////////////////////////////////////////////////////////////
  263. double CUiAnimViewDopeSheetBase::GetTickTime() const
  264. {
  265. if (GetTickDisplayMode() == eUiAVTickMode_InFrames)
  266. {
  267. return m_fFrameTickStep;
  268. }
  269. else
  270. {
  271. return 1.0f / m_ticksStep;
  272. }
  273. }
  274. //////////////////////////////////////////////////////////////////////////
  275. float CUiAnimViewDopeSheetBase::TickSnap(float time) const
  276. {
  277. double tickTime = GetTickTime();
  278. double t = floor(((double)time / tickTime) + 0.5);
  279. t *= tickTime;
  280. return static_cast<float>(t);
  281. }
  282. //////////////////////////////////////////////////////////////////////////
  283. float CUiAnimViewDopeSheetBase::TimeFromPoint(const QPoint& point) const
  284. {
  285. int x = point.x() - m_leftOffset + m_scrollOffset.x();
  286. float t = static_cast<float>(x) / m_timeScale;
  287. return TickSnap(t);
  288. }
  289. //////////////////////////////////////////////////////////////////////////
  290. float CUiAnimViewDopeSheetBase::TimeFromPointUnsnapped(const QPoint& point) const
  291. {
  292. int x = point.x() - m_leftOffset + m_scrollOffset.x();
  293. double t = (double)x / m_timeScale;
  294. return static_cast<float>(t);
  295. }
  296. void CUiAnimViewDopeSheetBase::mousePressEvent(QMouseEvent* event)
  297. {
  298. switch (event->button())
  299. {
  300. case Qt::LeftButton:
  301. OnLButtonDown(event->modifiers(), event->pos());
  302. break;
  303. case Qt::RightButton:
  304. OnRButtonDown(event->modifiers(), event->pos());
  305. break;
  306. case Qt::MiddleButton:
  307. OnMButtonDown(event->modifiers(), event->pos());
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. void CUiAnimViewDopeSheetBase::mouseReleaseEvent(QMouseEvent* event)
  314. {
  315. switch (event->button())
  316. {
  317. case Qt::LeftButton:
  318. OnLButtonUp(event->modifiers(), event->pos());
  319. break;
  320. case Qt::RightButton:
  321. OnRButtonUp(event->modifiers(), event->pos());
  322. break;
  323. case Qt::MiddleButton:
  324. OnMButtonUp(event->modifiers(), event->pos());
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. void CUiAnimViewDopeSheetBase::mouseDoubleClickEvent(QMouseEvent* event)
  331. {
  332. switch (event->button())
  333. {
  334. case Qt::LeftButton:
  335. OnLButtonDblClk(event->modifiers(), event->pos());
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. //////////////////////////////////////////////////////////////////////////
  342. void CUiAnimViewDopeSheetBase::OnLButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point)
  343. {
  344. CUiAnimViewSequence* pSequence = nullptr;
  345. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  346. if (!pSequence)
  347. {
  348. return;
  349. }
  350. // KDAB: workaround until the Key Properties is fully ported to Qt
  351. clearFocus();
  352. setFocus(Qt::MouseFocusReason);
  353. if (m_rcTimeline.contains(point))
  354. {
  355. m_mouseDownPos = point;
  356. // Clicked inside timeline.
  357. m_mouseMode = eUiAVMouseMode_DragTime;
  358. // If mouse over selected key, change cursor to left-right arrows.
  359. SetMouseCursor(m_crsLeftRight);
  360. SetCurrTime(TimeFromPoint(point));
  361. return;
  362. }
  363. if (m_bEditLock)
  364. {
  365. m_mouseDownPos = point;
  366. return;
  367. }
  368. if (m_mouseMode == eUiAVMouseMode_Paste)
  369. {
  370. m_mouseMode = eUiAVMouseMode_None;
  371. CUiAnimViewAnimNode* pAnimNode = GetAnimNodeFromPoint(m_mouseOverPos);
  372. CUiAnimViewTrack* pTrack = GetTrackFromPoint(m_mouseOverPos);
  373. if (pAnimNode)
  374. {
  375. UiAnimUndo undo("Paste Keys");
  376. UiAnimUndo::Record(new CUndoAnimKeySelection(pSequence));
  377. pSequence->DeselectAllKeys();
  378. pSequence->PasteKeysFromClipboard(pAnimNode, pTrack, ComputeSnappedMoveOffset());
  379. }
  380. SetMouseCursor(Qt::ArrowCursor);
  381. OnCaptureChanged();
  382. return;
  383. }
  384. m_mouseDownPos = point;
  385. // The summary region is used for moving already selected keys.
  386. if (m_rcSummary.contains(point))
  387. {
  388. CUiAnimViewKeyBundle selectedKeys = pSequence->GetSelectedKeys();
  389. if (selectedKeys.GetKeyCount() > 0)
  390. {
  391. /// Move/Clone Key Undo Begin
  392. UiAnimUndoManager::Get()->Begin();
  393. pSequence->StoreUndoForTracksWithSelectedKeys();
  394. StoreMementoForTracksWithSelectedKeys();
  395. m_keyTimeOffset = 0;
  396. m_mouseMode = eUiAVMouseMode_Move;
  397. SetMouseCursor(m_crsLeftRight);
  398. return;
  399. }
  400. }
  401. bool bStart = false;
  402. CUiAnimViewKeyHandle keyHandle = CheckCursorOnStartEndTimeAdjustBar(point, bStart);
  403. if (keyHandle.IsValid())
  404. {
  405. return LButtonDownOnTimeAdjustBar(point, keyHandle, bStart);
  406. }
  407. keyHandle = FirstKeyFromPoint(point);
  408. if (!keyHandle.IsValid())
  409. {
  410. keyHandle = DurationKeyFromPoint(point);
  411. }
  412. else
  413. {
  414. return LButtonDownOnKey(point, keyHandle, modifiers);
  415. }
  416. if (m_mouseActionMode == eUiAVActionMode_AddKeys)
  417. {
  418. AddKeys(point, modifiers & Qt::ShiftModifier);
  419. return;
  420. }
  421. if (modifiers & Qt::ShiftModifier)
  422. {
  423. m_mouseMode = eUiAVMouseMode_SelectWithinTime;
  424. }
  425. else
  426. {
  427. m_mouseMode = eUiAVMouseMode_Select;
  428. }
  429. }
  430. //////////////////////////////////////////////////////////////////////////
  431. void CUiAnimViewDopeSheetBase::OnLButtonUp(Qt::KeyboardModifiers modifiers, [[maybe_unused]] const QPoint& point)
  432. {
  433. CUiAnimViewSequence* pSequence = nullptr;
  434. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  435. if (!pSequence)
  436. {
  437. return;
  438. }
  439. if (m_mouseMode == eUiAVMouseMode_Select)
  440. {
  441. // Check if any key are selected.
  442. m_rcSelect.translate(-m_scrollOffset);
  443. SelectKeys(m_rcSelect, modifiers & Qt::ControlModifier);
  444. m_rcSelect = QRect();
  445. m_rubberBand->deleteLater();
  446. m_rubberBand = 0;
  447. }
  448. else if (m_mouseMode == eUiAVMouseMode_SelectWithinTime)
  449. {
  450. m_rcSelect.translate(-m_scrollOffset);
  451. SelectAllKeysWithinTimeFrame(m_rcSelect, modifiers & Qt::ControlModifier);
  452. m_rcSelect = QRect();
  453. m_rubberBand->deleteLater();
  454. m_rubberBand = 0;
  455. }
  456. else if (m_mouseMode == eUiAVMouseMode_DragTime)
  457. {
  458. SetMouseCursor(Qt::ArrowCursor);
  459. }
  460. else if (m_mouseMode == eUiAVMouseMode_Paste)
  461. {
  462. SetMouseCursor(Qt::ArrowCursor);
  463. }
  464. OnCaptureChanged();
  465. m_keyTimeOffset = 0;
  466. m_keyForTimeAdjust = CUiAnimViewKeyHandle();
  467. AcceptUndo();
  468. update();
  469. }
  470. //////////////////////////////////////////////////////////////////////////
  471. void CUiAnimViewDopeSheetBase::OnLButtonDblClk(Qt::KeyboardModifiers modifiers, const QPoint& point)
  472. {
  473. CUiAnimViewSequence* pSequence = nullptr;
  474. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  475. if (!pSequence || m_rcTimeline.contains(point) || m_bEditLock)
  476. {
  477. return;
  478. }
  479. CUiAnimViewKeyHandle keyHandle = FirstKeyFromPoint(point);
  480. if (!keyHandle.IsValid())
  481. {
  482. keyHandle = DurationKeyFromPoint(point);
  483. }
  484. else
  485. {
  486. UiAnimUndoManager::Get()->Begin();
  487. CUndoAnimKeySelection* pUndoKeySelection = new CUndoAnimKeySelection(pSequence);
  488. UiAnimUndo::Record(pUndoKeySelection);
  489. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  490. if (pTrack)
  491. {
  492. CUiAnimViewSequenceNotificationContext context(pSequence);
  493. pSequence->DeselectAllKeys();
  494. keyHandle.Select(true);
  495. m_keyTimeOffset = 0;
  496. if (pUndoKeySelection->IsSelectionChanged())
  497. {
  498. UiAnimUndoManager::Get()->Accept("Select Key");
  499. }
  500. else
  501. {
  502. UiAnimUndoManager::Get()->Cancel();
  503. }
  504. }
  505. return;
  506. }
  507. const bool bTryAddKeysInGroup = modifiers & Qt::ShiftModifier;
  508. AddKeys(point, bTryAddKeysInGroup);
  509. m_mouseMode = eUiAVMouseMode_None;
  510. }
  511. //////////////////////////////////////////////////////////////////////////
  512. void CUiAnimViewDopeSheetBase::OnMButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point)
  513. {
  514. OnRButtonDown(modifiers, point);
  515. }
  516. //////////////////////////////////////////////////////////////////////////
  517. void CUiAnimViewDopeSheetBase::OnMButtonUp(Qt::KeyboardModifiers modifiers, const QPoint& point)
  518. {
  519. OnRButtonUp(modifiers, point);
  520. }
  521. //////////////////////////////////////////////////////////////////////////
  522. void CUiAnimViewDopeSheetBase::OnRButtonDown(Qt::KeyboardModifiers modifiers, const QPoint& point)
  523. {
  524. CUiAnimViewSequence* pSequence = nullptr;
  525. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  526. if (!pSequence)
  527. {
  528. return;
  529. }
  530. m_bCursorWasInKey = false;
  531. m_bMouseMovedAfterRButtonDown = false;
  532. // KDAB: workaround until the Key Properties is fully ported to Qt
  533. clearFocus();
  534. setFocus(Qt::MouseFocusReason);
  535. if (m_rcTimeline.contains(point))
  536. {
  537. // Clicked inside timeline.
  538. // adjust markers.
  539. int nMarkerStart = TimeToClient(m_timeMarked.start);
  540. int nMarkerEnd = TimeToClient(m_timeMarked.end);
  541. if ((abs(point.x() - nMarkerStart)) < (abs(point.x() - nMarkerEnd)))
  542. {
  543. SetStartMarker(TimeFromPoint(point));
  544. m_mouseMode = eUiAVMouseMode_DragStartMarker;
  545. }
  546. else
  547. {
  548. SetEndMarker(TimeFromPoint(point));
  549. m_mouseMode = eUiAVMouseMode_DragEndMarker;
  550. }
  551. return;
  552. }
  553. m_mouseDownPos = point;
  554. if (modifiers & Qt::ShiftModifier) // alternative zoom
  555. {
  556. m_bZoomDrag = true;
  557. return;
  558. }
  559. CUiAnimViewKeyHandle keyHandle = FirstKeyFromPoint(point);
  560. if (!keyHandle.IsValid())
  561. {
  562. keyHandle = DurationKeyFromPoint(point);
  563. }
  564. if (keyHandle.IsValid())
  565. {
  566. m_bCursorWasInKey = true;
  567. keyHandle.Select(true);
  568. m_keyTimeOffset = 0;
  569. update();
  570. // Show a little pop-up menu for copy & delete.
  571. QMenu menu;
  572. QAction* actionCopy = menu.addAction(tr("Copy"));
  573. QAction* actionDelete = menu.addAction(tr("Delete"));
  574. const QPoint p = QCursor::pos();
  575. QAction* action = menu.exec(p);
  576. if (action == actionCopy)
  577. {
  578. pSequence->CopyKeysToClipboard(true, false);
  579. }
  580. else if (action == actionDelete)
  581. {
  582. UiAnimUndo undo("Delete Keys");
  583. pSequence->DeleteSelectedKeys();
  584. }
  585. }
  586. else
  587. {
  588. m_bMoveDrag = true;
  589. }
  590. }
  591. //////////////////////////////////////////////////////////////////////////
  592. void CUiAnimViewDopeSheetBase::OnRButtonUp([[maybe_unused]] Qt::KeyboardModifiers modifiers, [[maybe_unused]] const QPoint& point)
  593. {
  594. CUiAnimViewSequence* pSequence = nullptr;
  595. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  596. if (!pSequence)
  597. {
  598. return;
  599. }
  600. m_bZoomDrag = false;
  601. m_bMoveDrag = false;
  602. OnCaptureChanged();
  603. m_mouseMode = eUiAVMouseMode_None;
  604. if (!m_bCursorWasInKey)
  605. {
  606. const bool bHasCopiedKey = (GetKeysInClickboard() != NULL);
  607. if (bHasCopiedKey && m_bMouseMovedAfterRButtonDown == false) // Once moved, it means the user wanted to scroll, so no paste pop-up.
  608. {
  609. // Show a little pop-up menu for paste.
  610. QMenu menu;
  611. QAction* actionPaste = menu.addAction(tr("Paste"));
  612. QAction* action = menu.exec(QCursor::pos());
  613. if (action == actionPaste)
  614. {
  615. StartPasteKeys();
  616. }
  617. }
  618. }
  619. }
  620. //////////////////////////////////////////////////////////////////////////
  621. void CUiAnimViewDopeSheetBase::mouseMoveEvent(QMouseEvent* event)
  622. {
  623. CUiAnimViewSequence* pSequence = nullptr;
  624. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  625. if (!pSequence)
  626. {
  627. return;
  628. }
  629. // To prevent the key moving while selecting
  630. if (m_bJustSelected)
  631. {
  632. m_bJustSelected = false;
  633. return;
  634. }
  635. m_bMouseMovedAfterRButtonDown = true;
  636. m_mouseOverPos = event->pos();
  637. if (m_bZoomDrag && (event->modifiers() & Qt::ShiftModifier))
  638. {
  639. float fAnchorTime = TimeFromPointUnsnapped(m_mouseDownPos);
  640. SetTimeScale(m_timeScale * (1.0f + (event->pos().x() - m_mouseDownPos.x()) * 0.0025f), fAnchorTime);
  641. m_mouseDownPos = event->pos();
  642. return;
  643. }
  644. else
  645. {
  646. m_bZoomDrag = false;
  647. }
  648. if (m_bMoveDrag)
  649. {
  650. m_scrollOffset.setX(qBound(m_scrollMin, m_scrollOffset.x() + m_mouseDownPos.x() - event->pos().x(), m_scrollMax));
  651. m_mouseDownPos = event->pos();
  652. // Set the new position of the thumb (scroll box).
  653. m_scrollBar->setValue(m_scrollOffset.x());
  654. update();
  655. SetMouseCursor(m_crsLeftRight);
  656. return;
  657. }
  658. if (m_mouseMode == eUiAVMouseMode_Select
  659. || m_mouseMode == eUiAVMouseMode_SelectWithinTime)
  660. {
  661. MouseMoveSelect(event->pos());
  662. }
  663. else if (m_mouseMode == eUiAVMouseMode_Move)
  664. {
  665. MouseMoveMove(event->pos(), event->modifiers());
  666. }
  667. else if (m_mouseMode == eUiAVMouseMode_Clone)
  668. {
  669. pSequence->CloneSelectedKeys();
  670. m_mouseMode = eUiAVMouseMode_Move;
  671. }
  672. else if (m_mouseMode == eUiAVMouseMode_DragTime)
  673. {
  674. MouseMoveDragTime(event->pos(), event->modifiers());
  675. }
  676. else if (m_mouseMode == eUiAVMouseMode_DragStartMarker)
  677. {
  678. MouseMoveDragStartMarker(event->pos(), event->modifiers());
  679. }
  680. else if (m_mouseMode == eUiAVMouseMode_DragEndMarker)
  681. {
  682. MouseMoveDragEndMarker(event->pos(), event->modifiers());
  683. }
  684. else if (m_mouseMode == eUiAVMouseMode_Paste)
  685. {
  686. update();
  687. }
  688. else if (m_mouseMode == eUiAVMouseMode_StartTimeAdjust)
  689. {
  690. MouseMoveStartEndTimeAdjust(event->pos(), true);
  691. }
  692. else if (m_mouseMode == eUiAVMouseMode_EndTimeAdjust)
  693. {
  694. MouseMoveStartEndTimeAdjust(event->pos(), false);
  695. }
  696. else
  697. {
  698. //////////////////////////////////////////////////////////////////////////
  699. if (m_mouseActionMode == eUiAVActionMode_AddKeys)
  700. {
  701. SetMouseCursor(m_crsAddKey);
  702. }
  703. else
  704. {
  705. MouseMoveOver(event->pos());
  706. }
  707. }
  708. }
  709. //////////////////////////////////////////////////////////////////////////
  710. void CUiAnimViewDopeSheetBase::paintEvent(QPaintEvent* event)
  711. {
  712. QPainter painter(this);
  713. CUiAnimViewSequence* pSequence = nullptr;
  714. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  715. {
  716. // In case of the fast-redraw mode, just draw the saved bitmap.
  717. // Otherwise, actually redraw all things.
  718. // This mode is helpful when playing a sequence if the sequence has a lot of keys.
  719. if (!m_bFastRedraw)
  720. {
  721. QLinearGradient gradient(rect().topLeft(), rect().bottomLeft());
  722. gradient.setColorAt(0, QColor(250, 250, 250));
  723. gradient.setColorAt(1, QColor(220, 220, 220));
  724. painter.fillRect(rect(), gradient);
  725. if (pSequence)
  726. {
  727. if (m_bEditLock)
  728. {
  729. painter.fillRect(event->rect(), EDIT_DISABLE_GRAY_COLOR);
  730. }
  731. DrawControl(&painter, event->rect());
  732. }
  733. }
  734. }
  735. if (pSequence)
  736. {
  737. // Drawing the timeline is handled separately. In other words, it's not saved to the 'm_offscreenBitmap'.
  738. // This is for the fast-redraw mode mentioned above.
  739. DrawTimeline(&painter, event->rect());
  740. }
  741. #ifdef DEBUG
  742. painter.setFont(m_descriptionFont);
  743. painter.setPen(QColor(255, 255, 255));
  744. painter.setBrush(QColor(0, 0, 0));
  745. const QString redrawCountStr = QString::fromLatin1("Redraw Count: %1").arg(m_redrawCount);
  746. QRect redrawCountRect(0, 0, 150, 20);
  747. QRect bounds;
  748. painter.drawText(redrawCountRect, Qt::AlignLeft | Qt::TextSingleLine, redrawCountStr, &bounds);
  749. painter.fillRect(bounds, Qt::black);
  750. painter.drawText(redrawCountRect, Qt::AlignLeft | Qt::TextSingleLine, redrawCountStr);
  751. ++m_redrawCount;
  752. #endif
  753. }
  754. //////////////////////////////////////////////////////////////////////////
  755. void CUiAnimViewDopeSheetBase::SelectAllKeysWithinTimeFrame(const QRect& rc, const bool bMultiSelection)
  756. {
  757. CUiAnimViewSequence* pSequence = nullptr;
  758. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  759. if (!pSequence)
  760. {
  761. return;
  762. }
  763. UiAnimUndoManager::Get()->Begin();
  764. CUndoAnimKeySelection* pUndoKeySelection = new CUndoAnimKeySelection(pSequence);
  765. UiAnimUndo::Record(pUndoKeySelection);
  766. if (!bMultiSelection)
  767. {
  768. pSequence->DeselectAllKeys();
  769. }
  770. // put selection rectangle from client to track space.
  771. QRect trackRect = rc;
  772. trackRect.translate(m_scrollOffset);
  773. Range selTime = GetTimeRange(trackRect);
  774. CUiAnimViewTrackBundle tracks = pSequence->GetAllTracks();
  775. CUiAnimViewSequenceNotificationContext context(pSequence);
  776. for (unsigned int i = 0; i < tracks.GetCount(); ++i)
  777. {
  778. CUiAnimViewTrack* pTrack = tracks.GetTrack(i);
  779. // Check which keys we intersect.
  780. for (unsigned int j = 0; j < pTrack->GetKeyCount(); j++)
  781. {
  782. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(j);
  783. const float time = keyHandle.GetTime();
  784. if (selTime.IsInside(time))
  785. {
  786. keyHandle.Select(true);
  787. }
  788. }
  789. }
  790. if (pUndoKeySelection->IsSelectionChanged())
  791. {
  792. UiAnimUndoManager::Get()->Accept("Select keys");
  793. }
  794. else
  795. {
  796. UiAnimUndoManager::Get()->Cancel();
  797. }
  798. }
  799. //////////////////////////////////////////////////////////////////////////
  800. void CUiAnimViewDopeSheetBase::SetMouseCursor(const QCursor& cursor)
  801. {
  802. m_currCursor = cursor;
  803. setCursor(m_currCursor);
  804. }
  805. //////////////////////////////////////////////////////////////////////////
  806. void CUiAnimViewDopeSheetBase::SetCurrTime(float time)
  807. {
  808. if (time < m_timeRange.start)
  809. {
  810. time = m_timeRange.start;
  811. }
  812. if (time > m_timeRange.end)
  813. {
  814. time = m_timeRange.end;
  815. }
  816. CUiAnimationContext* pAnimationContext = nullptr;
  817. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  818. pAnimationContext->SetTime(time);
  819. }
  820. //////////////////////////////////////////////////////////////////////////
  821. void CUiAnimViewDopeSheetBase::OnTimeChanged(float newTime)
  822. {
  823. int x1 = TimeToClient(m_currentTime);
  824. int x2 = TimeToClient(newTime);
  825. m_currentTime = newTime;
  826. m_bFastRedraw = true;
  827. const QRect rc(QPoint(x1 - 3, m_rcClient.top()), QPoint(x1 + 4, m_rcClient.bottom()));
  828. update(rc);
  829. const QRect rc1(QPoint(x2 - 3, m_rcClient.top()), QPoint(x2 + 4, m_rcClient.bottom()));
  830. update(rc1);
  831. m_bFastRedraw = false;
  832. }
  833. //////////////////////////////////////////////////////////////////////////
  834. void CUiAnimViewDopeSheetBase::SetStartMarker(float fTime)
  835. {
  836. m_timeMarked.start = fTime;
  837. if (m_timeMarked.start < m_timeRange.start)
  838. {
  839. m_timeMarked.start = m_timeRange.start;
  840. }
  841. if (m_timeMarked.start > m_timeRange.end)
  842. {
  843. m_timeMarked.start = m_timeRange.end;
  844. }
  845. if (m_timeMarked.start > m_timeMarked.end)
  846. {
  847. m_timeMarked.end = m_timeMarked.start;
  848. }
  849. CUiAnimationContext* pAnimationContext = nullptr;
  850. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  851. pAnimationContext->SetMarkers(m_timeMarked);
  852. update();
  853. }
  854. //////////////////////////////////////////////////////////////////////////
  855. void CUiAnimViewDopeSheetBase::SetEndMarker(float fTime)
  856. {
  857. m_timeMarked.end = fTime;
  858. if (m_timeMarked.end < m_timeRange.start)
  859. {
  860. m_timeMarked.end = m_timeRange.start;
  861. }
  862. if (m_timeMarked.end > m_timeRange.end)
  863. {
  864. m_timeMarked.end = m_timeRange.end;
  865. }
  866. if (m_timeMarked.start > m_timeMarked.end)
  867. {
  868. m_timeMarked.start = m_timeMarked.end;
  869. }
  870. CUiAnimationContext* pAnimationContext = nullptr;
  871. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  872. pAnimationContext->SetMarkers(m_timeMarked);
  873. update();
  874. }
  875. //////////////////////////////////////////////////////////////////////////
  876. void CUiAnimViewDopeSheetBase::SetMouseActionMode(EUiAVActionMode mode)
  877. {
  878. m_mouseActionMode = mode;
  879. if (mode == eUiAVActionMode_AddKeys)
  880. {
  881. setCursor(m_crsAddKey);
  882. }
  883. }
  884. //////////////////////////////////////////////////////////////////////////
  885. CUiAnimViewNode* CUiAnimViewDopeSheetBase::GetNodeFromPointRec(CUiAnimViewNode* pCurrentNode, const QPoint& point)
  886. {
  887. QRect currentNodeRect = GetNodeRect(pCurrentNode);
  888. if (currentNodeRect.top() > point.y())
  889. {
  890. return nullptr;
  891. }
  892. if (currentNodeRect.bottom() >= point.y())
  893. {
  894. return pCurrentNode;
  895. }
  896. if (pCurrentNode->IsExpanded())
  897. {
  898. unsigned int childCount = pCurrentNode->GetChildCount();
  899. for (unsigned int i = 0; i < childCount; ++i)
  900. {
  901. CUiAnimViewNode* pFoundNode = GetNodeFromPointRec(pCurrentNode->GetChild(i), point);
  902. if (pFoundNode)
  903. {
  904. return pFoundNode;
  905. }
  906. }
  907. }
  908. return nullptr;
  909. }
  910. //////////////////////////////////////////////////////////////////////////
  911. CUiAnimViewNode* CUiAnimViewDopeSheetBase::GetNodeFromPoint(const QPoint& point)
  912. {
  913. CUiAnimViewSequence* pSequence = nullptr;
  914. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  915. return GetNodeFromPointRec(pSequence, point);
  916. }
  917. //////////////////////////////////////////////////////////////////////////
  918. CUiAnimViewAnimNode* CUiAnimViewDopeSheetBase::GetAnimNodeFromPoint(const QPoint& point)
  919. {
  920. CUiAnimViewNode* pNode = GetNodeFromPoint(point);
  921. if (pNode)
  922. {
  923. if (pNode->GetNodeType() == eUiAVNT_Track)
  924. {
  925. CUiAnimViewTrack* pTrack = static_cast<CUiAnimViewTrack*>(pNode);
  926. return static_cast<CUiAnimViewAnimNode*>(pTrack->GetAnimNode());
  927. }
  928. else if (pNode->GetNodeType() == eUiAVNT_AnimNode)
  929. {
  930. return static_cast<CUiAnimViewAnimNode*>(pNode);
  931. }
  932. }
  933. return nullptr;
  934. }
  935. //////////////////////////////////////////////////////////////////////////
  936. CUiAnimViewTrack* CUiAnimViewDopeSheetBase::GetTrackFromPoint(const QPoint& point)
  937. {
  938. CUiAnimViewNode* pNode = GetNodeFromPoint(point);
  939. if (pNode && pNode->GetNodeType() == eUiAVNT_Track)
  940. {
  941. return static_cast<CUiAnimViewTrack*>(pNode);
  942. }
  943. return nullptr;
  944. }
  945. //////////////////////////////////////////////////////////////////////////
  946. void CUiAnimViewDopeSheetBase::SetHorizontalExtent(int min, int max)
  947. {
  948. m_scrollMin = min;
  949. m_scrollMax = max;
  950. m_scrollBar->setPageStep(m_rcClient.width() / 2);
  951. m_scrollBar->setRange(min, max - m_scrollBar->pageStep() * 2 + m_leftOffset);
  952. };
  953. //////////////////////////////////////////////////////////////////////////
  954. XmlNodeRef CUiAnimViewDopeSheetBase::GetKeysInClickboard()
  955. {
  956. CClipboard clip(this);
  957. if (clip.IsEmpty())
  958. {
  959. return NULL;
  960. }
  961. if (clip.GetTitle() != "Track view keys")
  962. {
  963. return NULL;
  964. }
  965. XmlNodeRef copyNode = clip.Get();
  966. if (copyNode == NULL || strcmp(copyNode->getTag(), "CopyKeysNode"))
  967. {
  968. return NULL;
  969. }
  970. int nNumTracksToPaste = copyNode->getChildCount();
  971. if (nNumTracksToPaste == 0)
  972. {
  973. return NULL;
  974. }
  975. return copyNode;
  976. }
  977. //////////////////////////////////////////////////////////////////////////
  978. void CUiAnimViewDopeSheetBase::StartPasteKeys()
  979. {
  980. m_clipboardKeys = GetKeysInClickboard();
  981. if (m_clipboardKeys)
  982. {
  983. m_mouseMode = eUiAVMouseMode_Paste;
  984. // If mouse over selected key, change cursor to left-right arrows.
  985. SetMouseCursor(m_crsLeftRight);
  986. m_mouseDownPos = m_mouseOverPos;
  987. }
  988. }
  989. void CUiAnimViewDopeSheetBase::keyPressEvent(QKeyEvent* event)
  990. {
  991. CUiAnimViewSequence* pSequence = nullptr;
  992. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  993. if (!pSequence)
  994. {
  995. return;
  996. }
  997. if (event->matches(QKeySequence::Delete))
  998. {
  999. UiAnimUndo undo("Delete Keys");
  1000. pSequence->DeleteSelectedKeys();
  1001. return;
  1002. }
  1003. if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down || event->key() == Qt::Key_Right || event->key() == Qt::Key_Left)
  1004. {
  1005. CUiAnimViewKeyBundle keyBundle = pSequence->GetSelectedKeys();
  1006. CUiAnimViewKeyHandle keyHandle = keyBundle.GetSingleSelectedKey();
  1007. if (keyHandle.IsValid())
  1008. {
  1009. switch (event->key())
  1010. {
  1011. case Qt::Key_Up:
  1012. keyHandle = keyHandle.GetAboveKey();
  1013. break;
  1014. case Qt::Key_Down:
  1015. keyHandle = keyHandle.GetBelowKey();
  1016. break;
  1017. case Qt::Key_Right:
  1018. keyHandle = keyHandle.GetNextKey();
  1019. break;
  1020. case Qt::Key_Left:
  1021. keyHandle = keyHandle.GetPrevKey();
  1022. break;
  1023. }
  1024. if (keyHandle.IsValid())
  1025. {
  1026. UiAnimUndoManager::Get()->Begin();
  1027. CUndoAnimKeySelection* pUndoKeySelection = new CUndoAnimKeySelection(pSequence);
  1028. UiAnimUndo::Record(pUndoKeySelection);
  1029. CUiAnimViewSequenceNotificationContext context(pSequence);
  1030. pSequence->DeselectAllKeys();
  1031. keyHandle.Select(true);
  1032. if (pUndoKeySelection->IsSelectionChanged())
  1033. {
  1034. UiAnimUndoManager::Get()->Accept("Select Key");
  1035. }
  1036. else
  1037. {
  1038. UiAnimUndoManager::Get()->Cancel();
  1039. }
  1040. }
  1041. }
  1042. return;
  1043. }
  1044. if (event->matches(QKeySequence::Copy))
  1045. {
  1046. pSequence->CopyKeysToClipboard(true, false);
  1047. }
  1048. else if (event->matches(QKeySequence::Paste))
  1049. {
  1050. StartPasteKeys();
  1051. }
  1052. else if (event->matches(QKeySequence::Undo))
  1053. {
  1054. UiAnimUndoManager::Get()->Undo();
  1055. }
  1056. else if (event->matches(QKeySequence::Redo))
  1057. {
  1058. UiAnimUndoManager::Get()->Redo();
  1059. }
  1060. else
  1061. {
  1062. return QWidget::keyPressEvent(event);
  1063. }
  1064. }
  1065. //////////////////////////////////////////////////////////////////////////
  1066. void CUiAnimViewDopeSheetBase::RecordTrackUndo(CUiAnimViewTrack* pTrack)
  1067. {
  1068. CUiAnimViewSequence* pSequence = nullptr;
  1069. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1070. if (pTrack && pSequence)
  1071. {
  1072. UiAnimUndo undo("Track Modify");
  1073. UiAnimUndo::Record(new CUndoTrackObject(pTrack, pSequence));
  1074. }
  1075. }
  1076. //////////////////////////////////////////////////////////////////////////
  1077. void CUiAnimViewDopeSheetBase::ShowKeyTooltip(const CUiAnimViewKeyHandle& keyHandle, const QPoint& point)
  1078. {
  1079. if (m_lastTooltipPos == point)
  1080. {
  1081. return;
  1082. }
  1083. m_lastTooltipPos = point;
  1084. const float time = keyHandle.GetTime();
  1085. const char* desc = keyHandle.GetDescription();
  1086. QString tipText;
  1087. if (GetTickDisplayMode() == eUiAVTickMode_InSeconds)
  1088. {
  1089. tipText = tr("%1, {%2}").arg(time, 0, 'f', 3).arg(desc);
  1090. }
  1091. else
  1092. {
  1093. tipText = tr("%1, {%2}").arg(ftoi(time / m_snapFrameTime)).arg(desc);
  1094. }
  1095. QToolTip::showText(point, tipText);
  1096. }
  1097. //////////////////////////////////////////////////////////////////////////
  1098. void CUiAnimViewDopeSheetBase::OnCaptureChanged()
  1099. {
  1100. AcceptUndo();
  1101. m_bZoomDrag = false;
  1102. m_bMoveDrag = false;
  1103. }
  1104. //////////////////////////////////////////////////////////////////////////
  1105. bool CUiAnimViewDopeSheetBase::IsOkToAddKeyHere(const CUiAnimViewTrack* pTrack, float time) const
  1106. {
  1107. for (unsigned int i = 0; i < pTrack->GetKeyCount(); ++i)
  1108. {
  1109. CUiAnimViewKeyHandle keyHandle = const_cast<CUiAnimViewTrack*>(pTrack)->GetKey(i);
  1110. if (keyHandle.GetTime() == time)
  1111. {
  1112. return false;
  1113. }
  1114. }
  1115. return true;
  1116. }
  1117. //////////////////////////////////////////////////////////////////////////
  1118. void CUiAnimViewDopeSheetBase::MouseMoveSelect(const QPoint& point)
  1119. {
  1120. SetMouseCursor(Qt::ArrowCursor);
  1121. QRect rc(m_mouseDownPos, point);
  1122. rc = rc.normalized();
  1123. QRect rcClient = rect();
  1124. rc = rc.intersected(rcClient);
  1125. if (m_rubberBand == nullptr)
  1126. {
  1127. m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  1128. }
  1129. m_rubberBand->show();
  1130. if (m_mouseMode == eUiAVMouseMode_SelectWithinTime)
  1131. {
  1132. rc.setTop(m_rcClient.top());
  1133. rc.setBottom(m_rcClient.bottom());
  1134. }
  1135. m_rcSelect = rc;
  1136. m_rubberBand->setGeometry(m_rcSelect);
  1137. }
  1138. //////////////////////////////////////////////////////////////////////////
  1139. void CUiAnimViewDopeSheetBase::MouseMoveStartEndTimeAdjust(const QPoint& p, bool bStart)
  1140. {
  1141. CUiAnimViewSequence* pSequence = nullptr;
  1142. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1143. if (!pSequence)
  1144. {
  1145. return;
  1146. }
  1147. SetMouseCursor(m_crsAdjustLR);
  1148. const QPoint point(qBound(m_rcClient.left(), p.x(), m_rcClient.right()), p.y());
  1149. const QPoint ofs = point - m_mouseDownPos;
  1150. CUiAnimViewKeyHandle& keyHandle = m_keyForTimeAdjust;
  1151. ICharacterKey characterKey;
  1152. keyHandle.GetKey(&characterKey);
  1153. float& timeToAdjust = bStart ? characterKey.m_startTime : characterKey.m_endTime;
  1154. // Undo the last offset.
  1155. timeToAdjust += -m_keyTimeOffset;
  1156. // Apply a new offset.
  1157. m_keyTimeOffset = (ofs.x() / m_timeScale) * characterKey.m_speed;
  1158. timeToAdjust += m_keyTimeOffset;
  1159. // Check the validity.
  1160. if (bStart)
  1161. {
  1162. if (timeToAdjust < 0)
  1163. {
  1164. timeToAdjust = 0;
  1165. }
  1166. else if (timeToAdjust > characterKey.GetValidEndTime())
  1167. {
  1168. timeToAdjust = characterKey.GetValidEndTime();
  1169. }
  1170. }
  1171. else
  1172. {
  1173. if (timeToAdjust < characterKey.m_startTime)
  1174. {
  1175. timeToAdjust = characterKey.m_startTime;
  1176. }
  1177. else if (timeToAdjust > characterKey.GetValidEndTime())
  1178. {
  1179. timeToAdjust = characterKey.GetValidEndTime();
  1180. }
  1181. }
  1182. UiAnimUndo::Record(new CUndoTrackObject(m_keyForTimeAdjust.GetTrack(), pSequence));
  1183. keyHandle.SetKey(&characterKey);
  1184. update();
  1185. }
  1186. //////////////////////////////////////////////////////////////////////////
  1187. void CUiAnimViewDopeSheetBase::MouseMoveMove(const QPoint& p, [[maybe_unused]] Qt::KeyboardModifiers modifiers)
  1188. {
  1189. CUiAnimViewSequence* pSequence = nullptr;
  1190. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1191. CUiAnimViewSequenceNotificationContext context(pSequence);
  1192. SetMouseCursor(m_crsLeftRight);
  1193. const QPoint point(qBound(m_rcClient.left(), p.x(), m_rcClient.right()), p.y());
  1194. // Reset tracks to their initial state before starting the move
  1195. for (auto iter = m_trackMementos.begin(); iter != m_trackMementos.end(); ++iter)
  1196. {
  1197. CUiAnimViewTrack* pTrack = iter->first;
  1198. const TrackMemento& trackMemento = iter->second;
  1199. pTrack->RestoreFromMemento(trackMemento.m_memento);
  1200. const size_t numKeys = trackMemento.m_keySelectionStates.size();
  1201. for (size_t i = 0; i < numKeys; ++i)
  1202. {
  1203. pTrack->GetKey(static_cast<unsigned int>(i)).Select(trackMemento.m_keySelectionStates[i]);
  1204. }
  1205. }
  1206. CUiAnimViewKeyHandle keyHandle = FirstKeyFromPoint(m_mouseDownPos);
  1207. if (!keyHandle.IsValid())
  1208. {
  1209. keyHandle = DurationKeyFromPoint(m_mouseDownPos);
  1210. }
  1211. float oldTime;
  1212. if (keyHandle.IsValid())
  1213. {
  1214. oldTime = keyHandle.GetTime();
  1215. }
  1216. else
  1217. {
  1218. oldTime = TimeFromPointUnsnapped(m_mouseDownPos);
  1219. }
  1220. QPoint ofs = point - m_mouseDownPos;
  1221. float timeOffset = ofs.x() / m_timeScale;
  1222. float newTime = oldTime + timeOffset;
  1223. // Snap it, if necessary.
  1224. ESnappingMode snappingMode = GetKeyModifiedSnappingMode();
  1225. if (snappingMode == eSnappingMode_SnapFrame)
  1226. {
  1227. snappingMode = m_snappingMode;
  1228. }
  1229. if (snappingMode == eSnappingMode_SnapMagnet)
  1230. {
  1231. newTime = MagnetSnap(newTime, GetAnimNodeFromPoint(m_mouseOverPos));
  1232. }
  1233. else if (snappingMode == eSnappingMode_SnapTick)
  1234. {
  1235. newTime = TickSnap(newTime);
  1236. }
  1237. else if (snappingMode == eSnappingMode_SnapFrame)
  1238. {
  1239. newTime = FrameSnap(newTime);
  1240. }
  1241. Range extendedTimeRange(0.0f, m_timeRange.end);
  1242. extendedTimeRange.ClipValue(newTime);
  1243. timeOffset = newTime - oldTime; // Re-compute the time offset using snapped & clipped 'newTime'.
  1244. if (timeOffset == 0.0f)
  1245. {
  1246. return;
  1247. }
  1248. m_bKeysMoved = true;
  1249. if (m_mouseActionMode == eUiAVActionMode_ScaleKey)
  1250. {
  1251. float tscale = 0.005f;
  1252. float tofs = ofs.x() * tscale;
  1253. tofs = pSequence->ClipTimeOffsetForScaling(1 + tofs) - 1;
  1254. // Offset all selected keys by this offset.
  1255. pSequence->ScaleSelectedKeys(1 + tofs);
  1256. m_keyTimeOffset = tofs;
  1257. }
  1258. else
  1259. {
  1260. // Offset all selected keys by this offset.
  1261. if (m_mouseActionMode == eUiAVActionMode_SlideKey)
  1262. {
  1263. timeOffset = pSequence->ClipTimeOffsetForSliding(timeOffset);
  1264. pSequence->SlideKeys(timeOffset);
  1265. }
  1266. else
  1267. {
  1268. timeOffset = pSequence->ClipTimeOffsetForOffsetting(timeOffset);
  1269. pSequence->OffsetSelectedKeys(timeOffset);
  1270. }
  1271. if (CheckVirtualKey(Qt::Key_Menu))
  1272. {
  1273. CUiAnimViewKeyBundle selectedKeys = pSequence->GetSelectedKeys();
  1274. CUiAnimViewKeyHandle selectedKey = selectedKeys.GetSingleSelectedKey();
  1275. if (selectedKey.IsValid())
  1276. {
  1277. CUiAnimationContext* pAnimationContext = nullptr;
  1278. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  1279. pAnimationContext->SetTime(selectedKey.GetTime());
  1280. }
  1281. }
  1282. m_keyTimeOffset = timeOffset;
  1283. }
  1284. }
  1285. void CUiAnimViewDopeSheetBase::MouseMoveDragTime(const QPoint& point, Qt::KeyboardModifiers modifiers)
  1286. {
  1287. const QPoint p(qBound(m_rcClient.left(), point.x(), m_rcClient.right()),
  1288. qBound(m_rcClient.top(), point.y(), m_rcClient.bottom()));
  1289. float time = TimeFromPointUnsnapped(p);
  1290. m_timeRange.ClipValue(time);
  1291. bool bSnap = (modifiers & Qt::ControlModifier);
  1292. if (bSnap)
  1293. {
  1294. time = TickSnap(time);
  1295. }
  1296. SetCurrTime(time);
  1297. }
  1298. void CUiAnimViewDopeSheetBase::MouseMoveDragStartMarker(const QPoint& point, Qt::KeyboardModifiers modifiers)
  1299. {
  1300. const QPoint p(qBound(m_rcClient.left(), point.x(), m_rcClient.right()),
  1301. qBound(m_rcClient.top(), point.y(), m_rcClient.bottom()));
  1302. bool bNoSnap = (modifiers & Qt::ControlModifier);
  1303. float time = TimeFromPointUnsnapped(p);
  1304. m_timeRange.ClipValue(time);
  1305. if (!bNoSnap)
  1306. {
  1307. time = TickSnap(time);
  1308. }
  1309. SetStartMarker(time);
  1310. }
  1311. void CUiAnimViewDopeSheetBase::MouseMoveDragEndMarker(const QPoint& point, Qt::KeyboardModifiers modifiers)
  1312. {
  1313. const QPoint p(qBound(m_rcClient.left(), point.x(), m_rcClient.right()),
  1314. qBound(m_rcClient.top(), point.y(), m_rcClient.bottom()));
  1315. bool bNoSnap = (modifiers & Qt::ControlModifier);
  1316. float time = TimeFromPointUnsnapped(p);
  1317. m_timeRange.ClipValue(time);
  1318. if (!bNoSnap)
  1319. {
  1320. time = TickSnap(time);
  1321. }
  1322. SetEndMarker(time);
  1323. }
  1324. void CUiAnimViewDopeSheetBase::MouseMoveOver(const QPoint& point)
  1325. {
  1326. // No mouse mode.
  1327. SetMouseCursor(Qt::ArrowCursor);
  1328. bool bStart = false;
  1329. CUiAnimViewKeyHandle keyHandle = CheckCursorOnStartEndTimeAdjustBar(point, bStart);
  1330. if (keyHandle.IsValid())
  1331. {
  1332. SetMouseCursor(m_crsAdjustLR);
  1333. return;
  1334. }
  1335. keyHandle = FirstKeyFromPoint(point);
  1336. if (!keyHandle.IsValid())
  1337. {
  1338. keyHandle = DurationKeyFromPoint(point);
  1339. }
  1340. if (keyHandle.IsValid())
  1341. {
  1342. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  1343. if (pTrack && keyHandle.IsSelected())
  1344. {
  1345. // If mouse over selected key, change cursor to left-right arrows.
  1346. SetMouseCursor(m_crsLeftRight);
  1347. }
  1348. else
  1349. {
  1350. SetMouseCursor(m_crsCross);
  1351. }
  1352. if (pTrack)
  1353. {
  1354. ShowKeyTooltip(keyHandle, mapToGlobal(point));
  1355. }
  1356. }
  1357. else
  1358. {
  1359. QToolTip::hideText();
  1360. }
  1361. }
  1362. float CUiAnimViewDopeSheetBase::MagnetSnap(float newTime, const CUiAnimViewAnimNode* pNode) const
  1363. {
  1364. CUiAnimViewSequence* pSequence = nullptr;
  1365. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1366. if (!pSequence)
  1367. {
  1368. return newTime;
  1369. }
  1370. CUiAnimViewKeyBundle keys = pSequence->GetKeysInTimeRange(newTime - kMarginForMagnetSnapping / m_timeScale,
  1371. newTime + kMarginForMagnetSnapping / m_timeScale);
  1372. if (keys.GetKeyCount() > 0)
  1373. {
  1374. // By default, just use the first key that belongs to the time range as a magnet.
  1375. newTime = keys.GetKey(0).GetTime();
  1376. // But if there is an in-range key in a sibling track, use it instead.
  1377. // Here a 'sibling' means a track that belongs to a same node.
  1378. for (unsigned int i = 0; i < keys.GetKeyCount(); ++i)
  1379. {
  1380. CUiAnimViewKeyHandle keyHandle = keys.GetKey(i);
  1381. if (keyHandle.GetTrack()->GetAnimNode() == pNode)
  1382. {
  1383. newTime = keyHandle.GetTime();
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. return newTime;
  1389. }
  1390. //////////////////////////////////////////////////////////////////////////
  1391. float CUiAnimViewDopeSheetBase::FrameSnap(float time) const
  1392. {
  1393. double t = floor((double)time / m_snapFrameTime + 0.5);
  1394. t = t * m_snapFrameTime;
  1395. return static_cast<float>(t);
  1396. }
  1397. //////////////////////////////////////////////////////////////////////////
  1398. void CUiAnimViewDopeSheetBase::SetScrollOffset(int hpos)
  1399. {
  1400. m_scrollBar->setValue(hpos);
  1401. m_scrollOffset.setX(hpos);
  1402. update();
  1403. }
  1404. //////////////////////////////////////////////////////////////////////////
  1405. int CUiAnimViewDopeSheetBase::GetScrollOffset()
  1406. {
  1407. return m_scrollOffset.x();
  1408. }
  1409. //////////////////////////////////////////////////////////////////////////
  1410. void CUiAnimViewDopeSheetBase::LButtonDownOnTimeAdjustBar([[maybe_unused]] const QPoint& point, CUiAnimViewKeyHandle& keyHandle, bool bStart)
  1411. {
  1412. CUiAnimViewSequence* pSequence = nullptr;
  1413. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1414. m_keyTimeOffset = 0;
  1415. m_keyForTimeAdjust = keyHandle;
  1416. UiAnimUndoManager::Get()->Begin();
  1417. if (bStart)
  1418. {
  1419. m_mouseMode = eUiAVMouseMode_StartTimeAdjust;
  1420. }
  1421. else
  1422. {
  1423. // In case of the end time, make it have a valid (not zero)
  1424. // end time, first.
  1425. ICharacterKey animKey;
  1426. keyHandle.GetKey(&animKey);
  1427. if (animKey.m_endTime == 0)
  1428. {
  1429. animKey.m_endTime = animKey.m_duration;
  1430. UiAnimUndo::Record(new CUndoTrackObject(keyHandle.GetTrack(), pSequence));
  1431. keyHandle.SetKey(&animKey);
  1432. }
  1433. m_mouseMode = eUiAVMouseMode_EndTimeAdjust;
  1434. }
  1435. SetMouseCursor(m_crsAdjustLR);
  1436. }
  1437. //////////////////////////////////////////////////////////////////////////
  1438. void CUiAnimViewDopeSheetBase::LButtonDownOnKey([[maybe_unused]] const QPoint& point, CUiAnimViewKeyHandle& keyHandle, Qt::KeyboardModifiers modifiers)
  1439. {
  1440. CUiAnimViewSequence* pSequence = nullptr;
  1441. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1442. if (!keyHandle.IsSelected() && !(modifiers & Qt::ControlModifier))
  1443. {
  1444. UiAnimUndo undo("Select Keys");
  1445. CUndoAnimKeySelection* pUndoKeySelection = new CUndoAnimKeySelection(pSequence);
  1446. UiAnimUndo::Record(pUndoKeySelection);
  1447. CUiAnimViewSequenceNotificationContext context(pSequence);
  1448. pSequence->DeselectAllKeys();
  1449. m_bJustSelected = true;
  1450. m_keyTimeOffset = 0;
  1451. keyHandle.Select(true);
  1452. if (!pUndoKeySelection->IsSelectionChanged())
  1453. {
  1454. undo.Cancel();
  1455. }
  1456. }
  1457. else
  1458. {
  1459. UiAnimUndoManager::Get()->Cancel();
  1460. }
  1461. /// Move/Clone Key Undo Begin
  1462. UiAnimUndoManager::Get()->Begin();
  1463. pSequence->StoreUndoForTracksWithSelectedKeys();
  1464. StoreMementoForTracksWithSelectedKeys();
  1465. if (modifiers & Qt::ShiftModifier)
  1466. {
  1467. m_mouseMode = eUiAVMouseMode_Clone;
  1468. SetMouseCursor(m_crsLeftRight);
  1469. }
  1470. else
  1471. {
  1472. m_mouseMode = eUiAVMouseMode_Move;
  1473. SetMouseCursor(m_crsLeftRight);
  1474. }
  1475. update();
  1476. }
  1477. //////////////////////////////////////////////////////////////////////////
  1478. bool CUiAnimViewDopeSheetBase::CreateColorKey(CUiAnimViewTrack* pTrack, float keyTime)
  1479. {
  1480. bool keyCreated = false;
  1481. Vec3 vColor(0, 0, 0);
  1482. pTrack->GetValue(keyTime, vColor);
  1483. const AZ::Color defaultColor = AZ::Color::CreateFromRgba(
  1484. clamp_tpl(static_cast<AZ::u8>(FloatToIntRet(vColor.x)), AZ::u8(0), AZ::u8(255)),
  1485. clamp_tpl(static_cast<AZ::u8>(FloatToIntRet(vColor.y)), AZ::u8(0), AZ::u8(255)),
  1486. clamp_tpl(static_cast<AZ::u8>(FloatToIntRet(vColor.z)), AZ::u8(0), AZ::u8(255)), 255);
  1487. AzQtComponents::ColorPicker dlg(AzQtComponents::ColorPicker::Configuration::RGB, tr("Select Color"), this);
  1488. dlg.setCurrentColor(defaultColor);
  1489. dlg.setSelectedColor(defaultColor);
  1490. if (dlg.exec() == QDialog::Accepted)
  1491. {
  1492. const AZ::Color col = dlg.selectedColor().GammaToLinear();
  1493. const ColorF colArray(col.GetR(), col.GetG(), col.GetB(), col.GetA());
  1494. RecordTrackUndo(pTrack);
  1495. CUiAnimViewSequenceNotificationContext context(pTrack->GetSequence());
  1496. const unsigned int numChildNodes = pTrack->GetChildCount();
  1497. for (unsigned int i = 0; i < numChildNodes; ++i)
  1498. {
  1499. CUiAnimViewTrack* subTrack = static_cast<CUiAnimViewTrack*>(pTrack->GetChild(i));
  1500. if (IsOkToAddKeyHere(subTrack, keyTime))
  1501. {
  1502. CUiAnimViewKeyHandle newKey = subTrack->CreateKey(keyTime);
  1503. I2DBezierKey bezierKey;
  1504. newKey.GetKey(&bezierKey);
  1505. bezierKey.value = Vec2(keyTime, colArray[i]);
  1506. newKey.SetKey(&bezierKey);
  1507. keyCreated = true;
  1508. }
  1509. }
  1510. }
  1511. return keyCreated;
  1512. }
  1513. //////////////////////////////////////////////////////////////////////////
  1514. void CUiAnimViewDopeSheetBase::AcceptUndo()
  1515. {
  1516. if (UiAnimUndo::IsRecording())
  1517. {
  1518. if (m_mouseMode == eUiAVMouseMode_Paste)
  1519. {
  1520. UiAnimUndoManager::Get()->Cancel();
  1521. }
  1522. else if (m_mouseMode == eUiAVMouseMode_Move || m_mouseMode == eUiAVMouseMode_Clone)
  1523. {
  1524. CUiAnimViewSequence* pSequence = nullptr;
  1525. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1526. if (pSequence && m_bKeysMoved)
  1527. {
  1528. UiAnimUndo::Record(new CUndoAnimKeySelection(pSequence));
  1529. UiAnimUndoManager::Get()->Accept("Move/Clone Keys");
  1530. }
  1531. else
  1532. {
  1533. UiAnimUndoManager::Get()->Cancel();
  1534. }
  1535. }
  1536. else if (m_mouseMode == eUiAVMouseMode_StartTimeAdjust
  1537. || m_mouseMode == eUiAVMouseMode_EndTimeAdjust)
  1538. {
  1539. UiAnimUndoManager::Get()->Accept("Adjust Start/End Time of an Animation Key");
  1540. }
  1541. }
  1542. m_mouseMode = eUiAVMouseMode_None;
  1543. m_trackMementos.clear();
  1544. }
  1545. //////////////////////////////////////////////////////////////////////////
  1546. float CUiAnimViewDopeSheetBase::ComputeSnappedMoveOffset()
  1547. {
  1548. // Compute time offset
  1549. const QPoint currentMousePos(qBound(m_rcClient.left(), m_mouseOverPos.x(), m_rcClient.right()), m_mouseOverPos.y());
  1550. float time0 = TimeFromPointUnsnapped(m_mouseDownPos);
  1551. float time = TimeFromPointUnsnapped(currentMousePos);
  1552. if (GetKeyModifiedSnappingMode() == eSnappingMode_SnapTick)
  1553. {
  1554. time0 = TickSnap(time0);
  1555. time = TickSnap(time);
  1556. }
  1557. return time - time0;
  1558. }
  1559. //////////////////////////////////////////////////////////////////////////
  1560. void CUiAnimViewDopeSheetBase::AddKeys(const QPoint& point, const bool bTryAddKeysInGroup)
  1561. {
  1562. CUiAnimViewSequence* pSequence = nullptr;
  1563. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1564. if (!pSequence)
  1565. {
  1566. return;
  1567. }
  1568. // Add keys here.
  1569. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  1570. if (!pTrack)
  1571. {
  1572. return;
  1573. }
  1574. CUiAnimViewSequenceNotificationContext context(pSequence);
  1575. CUiAnimViewAnimNode* pNode = pTrack->GetAnimNode();
  1576. float keyTime = TimeFromPoint(point);
  1577. bool inRange = m_timeRange.IsInside(keyTime);
  1578. if (pTrack && inRange)
  1579. {
  1580. if (bTryAddKeysInGroup && pNode->GetParentNode()) // Add keys in group
  1581. {
  1582. CUiAnimViewTrackBundle tracksInGroup = pNode->GetTracksByParam(pTrack->GetParameterType());
  1583. for (int i = 0; i < (int)tracksInGroup.GetCount(); ++i)
  1584. {
  1585. CUiAnimViewTrack* pCurrTrack = tracksInGroup.GetTrack(i);
  1586. if (pCurrTrack->GetChildCount() == 0) // A simple track
  1587. {
  1588. if (IsOkToAddKeyHere(pCurrTrack, keyTime))
  1589. {
  1590. RecordTrackUndo(pCurrTrack);
  1591. pCurrTrack->CreateKey(keyTime);
  1592. }
  1593. }
  1594. else // A compound track
  1595. {
  1596. for (unsigned int k = 0; k < pCurrTrack->GetChildCount(); ++k)
  1597. {
  1598. CUiAnimViewTrack* pSubTrack = static_cast<CUiAnimViewTrack*>(pCurrTrack->GetChild(k));
  1599. if (IsOkToAddKeyHere(pSubTrack, keyTime))
  1600. {
  1601. RecordTrackUndo(pSubTrack);
  1602. pSubTrack->CreateKey(keyTime);
  1603. }
  1604. }
  1605. }
  1606. }
  1607. }
  1608. else if (pTrack->GetChildCount() == 0) // A simple track
  1609. {
  1610. if (IsOkToAddKeyHere(pTrack, keyTime))
  1611. {
  1612. RecordTrackUndo(pTrack);
  1613. pTrack->CreateKey(keyTime);
  1614. }
  1615. }
  1616. else // A compound track
  1617. {
  1618. if (pTrack->GetValueType() == eUiAnimValue_RGB)
  1619. {
  1620. CreateColorKey(pTrack, keyTime);
  1621. }
  1622. else
  1623. {
  1624. RecordTrackUndo(pTrack);
  1625. for (unsigned int i = 0; i < pTrack->GetChildCount(); ++i)
  1626. {
  1627. CUiAnimViewTrack* pSubTrack = static_cast<CUiAnimViewTrack*>(pTrack->GetChild(i));
  1628. if (IsOkToAddKeyHere(pSubTrack, keyTime))
  1629. {
  1630. pSubTrack->CreateKey(keyTime);
  1631. }
  1632. }
  1633. }
  1634. }
  1635. }
  1636. }
  1637. //////////////////////////////////////////////////////////////////////////
  1638. void CUiAnimViewDopeSheetBase::DrawControl(QPainter* painter, const QRect& rcUpdate)
  1639. {
  1640. CUiAnimViewSequence* pSequence = nullptr;
  1641. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1642. DrawNodesRecursive(pSequence, painter, rcUpdate);
  1643. DrawSummary(painter, rcUpdate);
  1644. DrawSelectedKeyIndicators(painter);
  1645. if (m_mouseMode == eUiAVMouseMode_Paste)
  1646. {
  1647. // If in paste mode draw keys that are in clipboard
  1648. DrawClipboardKeys(painter, QRect());
  1649. }
  1650. }
  1651. //////////////////////////////////////////////////////////////////////////
  1652. void CUiAnimViewDopeSheetBase::DrawNodesRecursive(CUiAnimViewNode* pNode, QPainter* painter, const QRect& rcUpdate)
  1653. {
  1654. const QRect rect = GetNodeRect(pNode);
  1655. if (!rect.isEmpty())
  1656. {
  1657. switch (pNode->GetNodeType())
  1658. {
  1659. case eUiAVNT_AnimNode:
  1660. DrawNodeTrack(static_cast<CUiAnimViewAnimNode*>(pNode), painter, rect);
  1661. break;
  1662. case eUiAVNT_Track:
  1663. DrawTrack(static_cast<CUiAnimViewTrack*>(pNode), painter, rect);
  1664. break;
  1665. }
  1666. }
  1667. if (pNode->IsExpanded())
  1668. {
  1669. unsigned int numChildren = pNode->GetChildCount();
  1670. for (unsigned int i = 0; i < numChildren; ++i)
  1671. {
  1672. DrawNodesRecursive(pNode->GetChild(i), painter, rcUpdate);
  1673. }
  1674. }
  1675. }
  1676. //////////////////////////////////////////////////////////////////////////
  1677. void CUiAnimViewDopeSheetBase::DrawTicks(QPainter* painter, const QRect& rc, Range& timeRange)
  1678. {
  1679. // Draw time ticks every tick step seconds.
  1680. const QPen dkgray(QColor(90, 90, 90));
  1681. const QPen ltgray(QColor(120, 120, 120));
  1682. const QPen prevPen = painter->pen();
  1683. painter->setPen(dkgray);
  1684. Range VisRange = GetVisibleRange();
  1685. int nNumberTicks = 10;
  1686. if (GetTickDisplayMode() == eUiAVTickMode_InFrames)
  1687. {
  1688. nNumberTicks = 8;
  1689. }
  1690. float start = TickSnap(timeRange.start);
  1691. float step = 1.0f / static_cast<float>(m_ticksStep);
  1692. for (float t = 0.0f; t <= timeRange.end + step; t += step)
  1693. {
  1694. float st = TickSnap(t);
  1695. if (st > timeRange.end)
  1696. {
  1697. st = timeRange.end;
  1698. }
  1699. if (st < VisRange.start)
  1700. {
  1701. continue;
  1702. }
  1703. if (st > VisRange.end)
  1704. {
  1705. break;
  1706. }
  1707. int x = TimeToClient(st);
  1708. if (x < 0)
  1709. {
  1710. continue;
  1711. }
  1712. int k = RoundFloatToInt(st * static_cast<float>(m_ticksStep));
  1713. if (k % nNumberTicks == 0)
  1714. {
  1715. if (st >= start)
  1716. {
  1717. painter->setPen(Qt::black);
  1718. }
  1719. else
  1720. {
  1721. painter->setPen(dkgray);
  1722. }
  1723. painter->drawLine(x, rc.bottom() - 1, x, rc.bottom() - 5);
  1724. painter->setPen(dkgray);
  1725. }
  1726. else
  1727. {
  1728. if (st >= start)
  1729. {
  1730. painter->setPen(dkgray);
  1731. }
  1732. else
  1733. {
  1734. painter->setPen(ltgray);
  1735. }
  1736. painter->drawLine(x, rc.bottom() - 1, x, rc.bottom() - 3);
  1737. }
  1738. }
  1739. painter->setPen(prevPen);
  1740. }
  1741. //////////////////////////////////////////////////////////////////////////
  1742. void CUiAnimViewDopeSheetBase::DrawTrack(CUiAnimViewTrack* pTrack, QPainter* painter, const QRect& trackRect)
  1743. {
  1744. CUiAnimViewSequence* pSequence = nullptr;
  1745. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1746. const QPen prevPen = painter->pen();
  1747. painter->setPen(QColor(120, 120, 120));
  1748. painter->drawLine(trackRect.bottomLeft(), trackRect.bottomRight());
  1749. painter->setPen(prevPen);
  1750. QRect rcInner = trackRect;
  1751. rcInner.setLeft(max(trackRect.left(), m_leftOffset - m_scrollOffset.x()));
  1752. rcInner.setRight(min(trackRect.right(), (m_scrollMax + m_scrollMin) - m_scrollOffset.x() + m_leftOffset * 2));
  1753. bool bLightAnimationSetActive = pSequence->GetFlags() & IUiAnimSequence::eSeqFlags_LightAnimationSet;
  1754. if (bLightAnimationSetActive && pTrack->GetKeyCount() > 0)
  1755. {
  1756. // In the case of the light animation set, the time of of the last key
  1757. // determines the end of the track.
  1758. float lastKeyTime = pTrack->GetKey(pTrack->GetKeyCount() - 1).GetTime();
  1759. rcInner.setRight(min(rcInner.right(), TimeToClient(lastKeyTime)));
  1760. }
  1761. QRect rcInnerDraw(QPoint(rcInner.left() - 6, rcInner.top()), QPoint(rcInner.right() + 6, rcInner.bottom()));
  1762. QColor trackColor = CUiAVCustomizeTrackColorsDlg::GetTrackColor(pTrack->GetParameterType());
  1763. if (pTrack->HasCustomColor())
  1764. {
  1765. ColorB customColor = pTrack->GetCustomColor();
  1766. trackColor = QColor(customColor.r, customColor.g, customColor.b);
  1767. }
  1768. // For the case of tracks belonging to an inactive director node,
  1769. // changes the track color to a custom one.
  1770. const QColor colorForDisabled = CUiAVCustomizeTrackColorsDlg::GetColorForDisabledTracks();
  1771. const QColor colorForMuted = CUiAVCustomizeTrackColorsDlg::GetColorForMutedTracks();
  1772. CUiAnimViewAnimNode* pDirectorNode = pTrack->GetDirector();
  1773. if (!pDirectorNode->IsActiveDirector())
  1774. {
  1775. trackColor = colorForDisabled;
  1776. }
  1777. // A disabled/muted track or any track in a disabled node also uses a custom color.
  1778. CUiAnimViewAnimNode* pAnimNode = pTrack->GetAnimNode();
  1779. bool bTrackDisabled = pTrack->GetFlags() & IUiAnimTrack::eUiAnimTrackFlags_Disabled;
  1780. bool bTrackMuted = pTrack->GetFlags() & IUiAnimTrack::eUiAnimTrackFlags_Muted;
  1781. bool bTrackInvalid = !pTrack->IsSubTrack() && !pAnimNode->IsParamValid(pTrack->GetParameterType());
  1782. bool bTrackInDisabledNode = pAnimNode->GetFlags() & eUiAnimNodeFlags_Disabled;
  1783. if (bTrackDisabled || bTrackInDisabledNode || bTrackInvalid)
  1784. {
  1785. trackColor = colorForDisabled;
  1786. }
  1787. else if (bTrackMuted)
  1788. {
  1789. trackColor = colorForMuted;
  1790. }
  1791. const QRect rc = rcInnerDraw.adjusted(0, 1, 0, 0);
  1792. const EUiAnimCurveType trackType = pTrack->GetCurveType();
  1793. if (trackType == eUiAnimCurveType_TCBFloat || trackType == eUiAnimCurveType_TCBQuat || trackType == eUiAnimCurveType_TCBVector)
  1794. {
  1795. trackColor = QColor(245, 80, 70);
  1796. }
  1797. if (pTrack->IsSelected())
  1798. {
  1799. QLinearGradient gradient(rc.topLeft(), rc.bottomLeft());
  1800. gradient.setColorAt(0, trackColor);
  1801. gradient.setColorAt(1, QColor(trackColor.red() / 2, trackColor.green() / 2, trackColor.blue() / 2));
  1802. painter->fillRect(rc, gradient);
  1803. }
  1804. else if (pTrack->GetValueType() == eUiAnimValue_RGB && pTrack->GetKeyCount() > 0)
  1805. {
  1806. DrawColorGradient(painter, rc, pTrack);
  1807. }
  1808. else
  1809. {
  1810. painter->fillRect(rc, trackColor);
  1811. }
  1812. // Left outside
  1813. QRect rcOutside = trackRect;
  1814. rcOutside.setRight(rcInnerDraw.left() - 1);
  1815. rcOutside.adjust(1, 1, -1, 0);
  1816. QLinearGradient gradient(rcOutside.topLeft(), rcOutside.bottomLeft());
  1817. gradient.setColorAt(0, QColor(210, 210, 210));
  1818. gradient.setColorAt(1, QColor(180, 180, 180));
  1819. painter->fillRect(rcOutside, gradient);
  1820. // Right outside.
  1821. rcOutside = trackRect;
  1822. rcOutside.setLeft(rcInnerDraw.right() + 1);
  1823. rcOutside.adjust(1, 1, -1, 0);
  1824. gradient = QLinearGradient(rcOutside.topLeft(), rcOutside.bottomLeft());
  1825. gradient.setColorAt(0, QColor(210, 210, 210));
  1826. gradient.setColorAt(1, QColor(180, 180, 180));
  1827. painter->fillRect(rcOutside, gradient);
  1828. // Get time range of update rectangle.
  1829. Range timeRange = GetTimeRange(trackRect);
  1830. // Draw tick marks in time range.
  1831. DrawTicks(painter, rcInner, timeRange);
  1832. // Draw special track features
  1833. EUiAnimValue trackValueType = pTrack->GetValueType();
  1834. CUiAnimParamType trackParamType = pTrack->GetParameterType();
  1835. if (trackValueType == eUiAnimValue_Bool)
  1836. {
  1837. // If this track is bool Track draw bars where track is true
  1838. DrawBoolTrack(timeRange, painter, pTrack, rc);
  1839. }
  1840. else if (trackValueType == eUiAnimValue_Select)
  1841. {
  1842. // If this track is Select Track draw bars to show where selection is active.
  1843. DrawSelectTrack(timeRange, painter, pTrack, rc);
  1844. }
  1845. // Draw keys in time range.
  1846. DrawKeys(pTrack, painter, rcInner, timeRange);
  1847. }
  1848. //////////////////////////////////////////////////////////////////////////
  1849. void CUiAnimViewDopeSheetBase::DrawSelectTrack(const Range& timeRange, QPainter* painter, CUiAnimViewTrack* pTrack, const QRect& rc)
  1850. {
  1851. const QBrush prevBrush = painter->brush();
  1852. painter->setBrush(m_selectTrackBrush);
  1853. const int numKeys = pTrack->GetKeyCount();
  1854. for (int i = 0; i < numKeys; ++i)
  1855. {
  1856. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  1857. ISelectKey selectKey;
  1858. keyHandle.GetKey(&selectKey);
  1859. if (!selectKey.szSelection.empty())
  1860. {
  1861. float time = keyHandle.GetTime();
  1862. float nextTime = timeRange.end;
  1863. if (i < numKeys - 1)
  1864. {
  1865. nextTime = pTrack->GetKey(i + 1).GetTime();
  1866. }
  1867. time = clamp_tpl(time, timeRange.start, timeRange.end);
  1868. nextTime = clamp_tpl(nextTime, timeRange.start, timeRange.end);
  1869. int x0 = TimeToClient(time);
  1870. float fBlendTime = selectKey.fBlendTime;
  1871. int blendTimeEnd = 0;
  1872. if (fBlendTime > 0.0f && fBlendTime < (nextTime - time))
  1873. {
  1874. blendTimeEnd = TimeToClient(nextTime);
  1875. nextTime -= fBlendTime;
  1876. }
  1877. int x = TimeToClient(nextTime);
  1878. if (x != x0)
  1879. {
  1880. QLinearGradient gradient(x0, rc.top() + 1, x0, rc.bottom());
  1881. gradient.setColorAt(0, Qt::white);
  1882. gradient.setColorAt(1, QColor(100, 190, 255));
  1883. painter->fillRect(QRect(QPoint(x0, rc.top() + 1), QPoint(x, rc.bottom())), gradient);
  1884. }
  1885. if (fBlendTime > 0.0f)
  1886. {
  1887. QLinearGradient gradient(x, rc.top() + 1, x, rc.bottom());
  1888. gradient.setColorAt(0, Qt::white);
  1889. gradient.setColorAt(1, QColor(0, 115, 230));
  1890. painter->fillRect(QRect(QPoint(x, rc.top() + 1), QPoint(blendTimeEnd, rc.bottom())), gradient);
  1891. }
  1892. }
  1893. }
  1894. painter->setBrush(prevBrush);
  1895. }
  1896. //////////////////////////////////////////////////////////////////////////
  1897. void CUiAnimViewDopeSheetBase::DrawBoolTrack(const Range& timeRange, QPainter* painter, CUiAnimViewTrack* pTrack, const QRect& rc)
  1898. {
  1899. int x0 = TimeToClient(timeRange.start);
  1900. const QBrush prevBrush = painter->brush();
  1901. painter->setBrush(m_visibilityBrush);
  1902. const int numKeys = pTrack->GetKeyCount();
  1903. for (int i = 0; i < numKeys; ++i)
  1904. {
  1905. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  1906. const float time = keyHandle.GetTime();
  1907. if (time < timeRange.start)
  1908. {
  1909. continue;
  1910. }
  1911. if (time > timeRange.end)
  1912. {
  1913. break;
  1914. }
  1915. int x = TimeToClient(time);
  1916. bool val = false;
  1917. pTrack->GetValue(time - 0.001f, val);
  1918. if (val)
  1919. {
  1920. QLinearGradient gradient(x0, rc.top() + 4, x0, rc.bottom() - 4);
  1921. gradient.setColorAt(0, QColor(250, 250, 250));
  1922. gradient.setColorAt(1, QColor(0, 80, 255));
  1923. painter->fillRect(QRect(QPoint(x0, rc.top() + 4), QPoint(x, rc.bottom() - 4)), gradient);
  1924. }
  1925. x0 = x;
  1926. }
  1927. int x = TimeToClient(timeRange.end);
  1928. bool val = false;
  1929. pTrack->GetValue(timeRange.end - 0.001f, val);
  1930. if (val)
  1931. {
  1932. QLinearGradient gradient(x0, rc.top() + 4, x0, rc.bottom() - 4);
  1933. gradient.setColorAt(0, QColor(250, 250, 250));
  1934. gradient.setColorAt(1, QColor(0, 80, 255));
  1935. painter->fillRect(QRect(QPoint(x0, rc.top() + 4), QPoint(x, rc.bottom() - 4)), gradient);
  1936. }
  1937. painter->setBrush(prevBrush);
  1938. }
  1939. //////////////////////////////////////////////////////////////////////////
  1940. void CUiAnimViewDopeSheetBase::DrawKeys(CUiAnimViewTrack* pTrack, QPainter* painter, QRect& rect, [[maybe_unused]] Range& timeRange)
  1941. {
  1942. int numKeys = pTrack->GetKeyCount();
  1943. const QFont prevFont = painter->font();
  1944. painter->setFont(m_descriptionFont);
  1945. painter->setPen(KEY_TEXT_COLOR);
  1946. int prevKeyPixel = -10000;
  1947. const int kDefaultWidthForDescription = 200;
  1948. const int kSmallMargin = 10;
  1949. // Draw keys.
  1950. for (int i = 0; i < numKeys; ++i)
  1951. {
  1952. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  1953. const float time = keyHandle.GetTime();
  1954. int x = TimeToClient(time);
  1955. if (x - kSmallMargin > rect.right())
  1956. {
  1957. continue;
  1958. }
  1959. int x1 = x + kDefaultWidthForDescription;
  1960. CUiAnimViewKeyHandle nextKey = keyHandle.GetNextKey();
  1961. if (nextKey.IsValid())
  1962. {
  1963. x1 = TimeToClient(nextKey.GetTime()) - kSmallMargin;
  1964. }
  1965. if (x1 > x + kSmallMargin) // Enough space for description text or duration bar
  1966. {
  1967. // Get info about that key.
  1968. const char* pDescription = keyHandle.GetDescription();
  1969. const float duration = keyHandle.GetDuration();
  1970. int xlast = x;
  1971. if (duration > 0)
  1972. {
  1973. xlast = TimeToClient(time + duration);
  1974. }
  1975. if (xlast + kSmallMargin < rect.left())
  1976. {
  1977. continue;
  1978. }
  1979. if (duration > 0)
  1980. {
  1981. DrawKeyDuration(pTrack, painter, rect, i);
  1982. }
  1983. if (pDescription && pDescription[0] != 0)
  1984. {
  1985. char keydesc[1024];
  1986. bool bSelectedAndBeingMoved = m_mouseMode == eUiAVMouseMode_Move && keyHandle.IsSelected();
  1987. if (bSelectedAndBeingMoved)
  1988. {
  1989. // Show its time or frame number additionally.
  1990. if (GetTickDisplayMode() == eUiAVTickMode_InSeconds)
  1991. {
  1992. sprintf_s(keydesc, "%.3f, {", time);
  1993. }
  1994. else
  1995. {
  1996. sprintf_s(keydesc, "%d, {", ftoi(time / m_snapFrameTime));
  1997. }
  1998. }
  1999. else
  2000. {
  2001. azstrcpy(keydesc, AZ_ARRAY_SIZE(keydesc), "{");
  2002. }
  2003. azstrcat(keydesc, AZ_ARRAY_SIZE(keydesc), pDescription);
  2004. azstrcat(keydesc, AZ_ARRAY_SIZE(keydesc), "}");
  2005. // Draw key description text.
  2006. // Find next key.
  2007. const QRect textRect(QPoint(x + 10, rect.top()), QPoint(x1, rect.bottom()));
  2008. painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, painter->fontMetrics().elidedText(keydesc, Qt::ElideRight, textRect.width()));
  2009. }
  2010. }
  2011. if (x < 0)
  2012. {
  2013. continue;
  2014. }
  2015. if (pTrack->GetChildCount() == 0 // At compound tracks, keys are all green.
  2016. && abs(x - prevKeyPixel) < 2)
  2017. {
  2018. // If multiple keys on the same time.
  2019. painter->drawPixmap(QPoint(x - 6, rect.top() + 2), QPixmap(":/Trackview/trackview_keys_02.png"));
  2020. }
  2021. else
  2022. {
  2023. if (keyHandle.IsSelected())
  2024. {
  2025. painter->drawPixmap(QPoint(x - 6, rect.top() + 2), QPixmap(":/Trackview/trackview_keys_01.png"));
  2026. }
  2027. else
  2028. {
  2029. painter->drawPixmap(QPoint(x - 6, rect.top() + 2), QPixmap(":/Trackview/trackview_keys_00.png"));
  2030. }
  2031. }
  2032. prevKeyPixel = x;
  2033. }
  2034. painter->setFont(prevFont);
  2035. }
  2036. //////////////////////////////////////////////////////////////////////////
  2037. void CUiAnimViewDopeSheetBase::DrawClipboardKeys(QPainter* painter, [[maybe_unused]] const QRect& rc)
  2038. {
  2039. CUiAnimViewSequence* pSequence = nullptr;
  2040. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  2041. const float timeOffset = ComputeSnappedMoveOffset();
  2042. // Get node & track under cursor
  2043. CUiAnimViewAnimNode* pAnimNode = GetAnimNodeFromPoint(m_mouseOverPos);
  2044. CUiAnimViewTrack* pTrack = GetTrackFromPoint(m_mouseOverPos);
  2045. auto matchedLocations = pSequence->GetMatchedPasteLocations(m_clipboardKeys, pAnimNode, pTrack);
  2046. for (size_t i = 0; i < matchedLocations.size(); ++i)
  2047. {
  2048. auto& matchedLocation = matchedLocations[i];
  2049. CUiAnimViewTrack* pMatchedTrack = matchedLocation.first;
  2050. XmlNodeRef trackNode = matchedLocation.second;
  2051. if (pMatchedTrack->IsCompoundTrack())
  2052. {
  2053. // Both child counts should be the same, but make sure
  2054. const unsigned int numSubTrack = std::min(pMatchedTrack->GetChildCount(), (unsigned int)trackNode->getChildCount());
  2055. for (unsigned int subTrackIndex = 0; subTrackIndex < numSubTrack; ++subTrackIndex)
  2056. {
  2057. CUiAnimViewTrack* pSubTrack = static_cast<CUiAnimViewTrack*>(pMatchedTrack->GetChild(subTrackIndex));
  2058. XmlNodeRef subTrackNode = trackNode->getChild(subTrackIndex);
  2059. DrawTrackClipboardKeys(painter, pSubTrack, subTrackNode, timeOffset);
  2060. // Also draw to parent track. This is intentional
  2061. DrawTrackClipboardKeys(painter, pMatchedTrack, subTrackNode, timeOffset);
  2062. }
  2063. }
  2064. else
  2065. {
  2066. DrawTrackClipboardKeys(painter, pMatchedTrack, trackNode, timeOffset);
  2067. }
  2068. }
  2069. }
  2070. //////////////////////////////////////////////////////////////////////////
  2071. void CUiAnimViewDopeSheetBase::DrawTrackClipboardKeys(QPainter* painter, CUiAnimViewTrack* pTrack, XmlNodeRef trackNode, const float timeOffset)
  2072. {
  2073. const QPen prevPen = painter->pen();
  2074. painter->setPen(Qt::green);
  2075. const QRect trackRect = GetNodeRect(pTrack);
  2076. const int numKeysToPaste = trackNode->getChildCount();
  2077. for (int i = 0; i < numKeysToPaste; ++i)
  2078. {
  2079. XmlNodeRef keyNode = trackNode->getChild(i);
  2080. float time;
  2081. if (keyNode->getAttr("time", time))
  2082. {
  2083. int x = TimeToClient(time + timeOffset);
  2084. painter->drawPixmap(QPoint(x - 6, trackRect.top() + 2), QPixmap(":/Trackview/trackview_keys_03.png"));
  2085. painter->drawLine(x, m_rcClient.top(), x, m_rcClient.bottom());
  2086. }
  2087. }
  2088. painter->setPen(prevPen);
  2089. }
  2090. //////////////////////////////////////////////////////////////////////////
  2091. CUiAnimViewKeyHandle CUiAnimViewDopeSheetBase::FirstKeyFromPoint(const QPoint& point)
  2092. {
  2093. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  2094. if (!pTrack)
  2095. {
  2096. return CUiAnimViewKeyHandle();
  2097. }
  2098. float t1 = TimeFromPointUnsnapped(QPoint(point.x() - 4, point.y()));
  2099. float t2 = TimeFromPointUnsnapped(QPoint(point.x() + 4, point.y()));
  2100. int numKeys = pTrack->GetKeyCount();
  2101. for (int i = 0; i < numKeys; ++i)
  2102. {
  2103. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  2104. float time = keyHandle.GetTime();
  2105. if (time >= t1 && time <= t2)
  2106. {
  2107. return keyHandle;
  2108. }
  2109. }
  2110. return CUiAnimViewKeyHandle();
  2111. }
  2112. //////////////////////////////////////////////////////////////////////////
  2113. CUiAnimViewKeyHandle CUiAnimViewDopeSheetBase::DurationKeyFromPoint(const QPoint& point)
  2114. {
  2115. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  2116. if (!pTrack)
  2117. {
  2118. return CUiAnimViewKeyHandle();
  2119. }
  2120. float t = TimeFromPointUnsnapped(point);
  2121. int numKeys = pTrack->GetKeyCount();
  2122. // Iterate in a reverse order to prioritize later nodes.
  2123. for (int i = numKeys - 1; i >= 0; --i)
  2124. {
  2125. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  2126. const float time = keyHandle.GetTime();
  2127. const float duration = keyHandle.GetDuration();
  2128. if (t >= time && t <= time + duration)
  2129. {
  2130. return keyHandle;
  2131. }
  2132. }
  2133. return CUiAnimViewKeyHandle();
  2134. }
  2135. //////////////////////////////////////////////////////////////////////////
  2136. CUiAnimViewKeyHandle CUiAnimViewDopeSheetBase::CheckCursorOnStartEndTimeAdjustBar(const QPoint& point, bool& bStart)
  2137. {
  2138. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  2139. if (!pTrack)
  2140. {
  2141. return CUiAnimViewKeyHandle();
  2142. }
  2143. int numKeys = pTrack->GetKeyCount();
  2144. for (int i = 0; i < numKeys; ++i)
  2145. {
  2146. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  2147. if (!keyHandle.IsSelected())
  2148. {
  2149. continue;
  2150. }
  2151. const float time = keyHandle.GetTime();
  2152. const float duration = keyHandle.GetDuration();
  2153. if (duration == 0)
  2154. {
  2155. continue;
  2156. }
  2157. int stime = TimeToClient(time);
  2158. int etime = TimeToClient(time + duration);
  2159. if (point.x() >= stime - 3 && point.x() <= stime)
  2160. {
  2161. bStart = true;
  2162. return keyHandle;
  2163. }
  2164. else if (point.x() >= etime && point.x() <= etime + 3)
  2165. {
  2166. bStart = false;
  2167. return keyHandle;
  2168. }
  2169. }
  2170. return CUiAnimViewKeyHandle();
  2171. }
  2172. //////////////////////////////////////////////////////////////////////////
  2173. int CUiAnimViewDopeSheetBase::NumKeysFromPoint(const QPoint& point)
  2174. {
  2175. CUiAnimViewTrack* pTrack = GetTrackFromPoint(point);
  2176. if (!pTrack)
  2177. {
  2178. return -1;
  2179. }
  2180. float t1 = TimeFromPointUnsnapped(QPoint(point.x() - 4, point.y()));
  2181. float t2 = TimeFromPointUnsnapped(QPoint(point.x() + 4, point.y()));
  2182. int count = 0;
  2183. int numKeys = pTrack->GetKeyCount();
  2184. for (int i = 0; i < numKeys; ++i)
  2185. {
  2186. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  2187. const float time = keyHandle.GetTime();
  2188. if (time >= t1 && time <= t2)
  2189. {
  2190. ++count;
  2191. }
  2192. }
  2193. return count;
  2194. }
  2195. //////////////////////////////////////////////////////////////////////////
  2196. void CUiAnimViewDopeSheetBase::SelectKeys(const QRect& rc, const bool bMultiSelection)
  2197. {
  2198. CUiAnimViewSequence* pSequence = nullptr;
  2199. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  2200. UiAnimUndoManager::Get()->Begin();
  2201. CUndoAnimKeySelection* pUndoKeySelection = new CUndoAnimKeySelection(pSequence);
  2202. UiAnimUndo::Record(pUndoKeySelection);
  2203. CUiAnimViewSequenceNotificationContext context(pSequence);
  2204. if (!bMultiSelection)
  2205. {
  2206. pSequence->DeselectAllKeys();
  2207. }
  2208. // put selection rectangle from client to track space.
  2209. const QRect rci = rc.translated(m_scrollOffset);
  2210. Range selTime = GetTimeRange(rci);
  2211. CUiAnimViewTrackBundle tracks = pSequence->GetAllTracks();
  2212. for (unsigned int i = 0; i < tracks.GetCount(); ++i)
  2213. {
  2214. CUiAnimViewTrack* pTrack = tracks.GetTrack(i);
  2215. QRect trackRect = GetNodeRect(pTrack);
  2216. // Decrease item rectangle a bit.
  2217. trackRect.adjust(4, 4, -4, -4);
  2218. // Check if item rectangle intersects with selection rectangle in y axis.
  2219. if ((trackRect.top() >= rc.top() && trackRect.top() <= rc.bottom()) ||
  2220. (trackRect.bottom() >= rc.top() && trackRect.bottom() <= rc.bottom()) ||
  2221. (rc.top() >= trackRect.top() && rc.top() <= trackRect.bottom()) ||
  2222. (rc.bottom() >= trackRect.top() && rc.bottom() <= trackRect.bottom()))
  2223. {
  2224. // Check which keys we intersect.
  2225. for (unsigned int j = 0; j < pTrack->GetKeyCount(); j++)
  2226. {
  2227. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(j);
  2228. const float time = keyHandle.GetTime();
  2229. if (selTime.IsInside(time))
  2230. {
  2231. keyHandle.Select(true);
  2232. }
  2233. }
  2234. }
  2235. }
  2236. if (pUndoKeySelection->IsSelectionChanged())
  2237. {
  2238. UiAnimUndoManager::Get()->Accept("Select keys");
  2239. }
  2240. else
  2241. {
  2242. UiAnimUndoManager::Get()->Cancel();
  2243. }
  2244. }
  2245. //////////////////////////////////////////////////////////////////////////
  2246. void CUiAnimViewDopeSheetBase::SetTickDisplayMode(EUiAVTickMode mode)
  2247. {
  2248. m_tickDisplayMode = mode;
  2249. SetTimeScale(GetTimeScale(), 0); // for refresh
  2250. }
  2251. //////////////////////////////////////////////////////////////////////////
  2252. void CUiAnimViewDopeSheetBase::SetSnapFPS(UINT fps)
  2253. {
  2254. m_snapFrameTime = (fps == 0) ? 0.033333f : (1.0f / float(fps));
  2255. }
  2256. //////////////////////////////////////////////////////////////////////////
  2257. ESnappingMode CUiAnimViewDopeSheetBase::GetKeyModifiedSnappingMode()
  2258. {
  2259. ESnappingMode snappingMode = m_snappingMode;
  2260. if (qApp->keyboardModifiers() & Qt::ControlModifier)
  2261. {
  2262. snappingMode = eSnappingMode_SnapNone;
  2263. }
  2264. else if (qApp->keyboardModifiers() & Qt::ShiftModifier)
  2265. {
  2266. snappingMode = eSnappingMode_SnapMagnet;
  2267. }
  2268. else if (qApp->keyboardModifiers() & Qt::AltModifier)
  2269. {
  2270. snappingMode = eSnappingMode_SnapFrame;
  2271. }
  2272. return snappingMode;
  2273. }
  2274. //////////////////////////////////////////////////////////////////////////
  2275. void CUiAnimViewDopeSheetBase::DrawSelectedKeyIndicators(QPainter* painter)
  2276. {
  2277. CUiAnimViewSequence* pSequence = nullptr;
  2278. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  2279. const QPen prevPen = painter->pen();
  2280. painter->setPen(Qt::green);
  2281. CUiAnimViewKeyBundle keys = pSequence->GetSelectedKeys();
  2282. for (unsigned int i = 0; i < keys.GetKeyCount(); ++i)
  2283. {
  2284. CUiAnimViewKeyHandle keyHandle = keys.GetKey(i);
  2285. int x = TimeToClient(keyHandle.GetTime());
  2286. painter->drawLine(x, m_rcClient.top(), x, m_rcClient.bottom());
  2287. }
  2288. painter->setPen(prevPen);
  2289. }
  2290. //////////////////////////////////////////////////////////////////////////
  2291. void CUiAnimViewDopeSheetBase::ComputeFrameSteps(const Range& visRange)
  2292. {
  2293. float fNbFrames = fabsf ((visRange.end - visRange.start) / m_snapFrameTime);
  2294. float afStepTable [4] = { 1.0f, 0.5f, 0.2f, 0.1f };
  2295. bool bDone = false;
  2296. float fFact = 1.0f;
  2297. unsigned int nStepIdx = 0;
  2298. for (unsigned int nAttempts = 0; nAttempts < 10 && !bDone; ++nAttempts)
  2299. {
  2300. bool bLess = true;
  2301. for (nStepIdx = 0; nStepIdx < 4; ++nStepIdx)
  2302. {
  2303. float fFactNbFrames = fNbFrames / (afStepTable[nStepIdx] * fFact);
  2304. if (fFactNbFrames >= 3 && fFactNbFrames <= 9)
  2305. {
  2306. bDone = true;
  2307. break;
  2308. }
  2309. else
  2310. {
  2311. bLess = (fFactNbFrames < 3);
  2312. }
  2313. }
  2314. if (!bDone)
  2315. {
  2316. fFact *= (bLess) ? 0.1f : 10.0f;
  2317. }
  2318. }
  2319. float nBIntermediateTicks = 5;
  2320. m_fFrameLabelStep = fFact * afStepTable[nStepIdx];
  2321. if (TimeToClient(static_cast<float>(m_fFrameLabelStep)) - TimeToClient(0) > 1300)
  2322. {
  2323. nBIntermediateTicks = 10;
  2324. }
  2325. m_fFrameTickStep = m_fFrameLabelStep * double (m_snapFrameTime) / double(nBIntermediateTicks);
  2326. }
  2327. void CUiAnimViewDopeSheetBase::DrawTimeLineInFrames(QPainter* painter, const QRect& rc, [[maybe_unused]] const QColor& lineCol, const QColor& textCol, [[maybe_unused]] double step)
  2328. {
  2329. float fFramesPerSec = 1.0f / m_snapFrameTime;
  2330. float fInvFrameLabelStep = 1.0f / static_cast<float>(m_fFrameLabelStep);
  2331. Range VisRange = GetVisibleRange();
  2332. const Range& timeRange = m_timeRange;
  2333. const QPen ltgray(QColor(90, 90, 90));
  2334. const QPen black(textCol);
  2335. for (float t = TickSnap(timeRange.start); t <= timeRange.end + static_cast<float>(m_fFrameTickStep); t += static_cast<float>(m_fFrameTickStep))
  2336. {
  2337. float st = t;
  2338. if (st > timeRange.end)
  2339. {
  2340. st = timeRange.end;
  2341. }
  2342. if (st < VisRange.start)
  2343. {
  2344. continue;
  2345. }
  2346. if (st > VisRange.end)
  2347. {
  2348. break;
  2349. }
  2350. if (st < m_timeRange.start || st > m_timeRange.end)
  2351. {
  2352. continue;
  2353. }
  2354. const int x = TimeToClient(st);
  2355. float fFrame = st * fFramesPerSec;
  2356. float fFrameScaled = fFrame * fInvFrameLabelStep;
  2357. if (fabsf(fFrameScaled - RoundFloatToInt(fFrameScaled)) < 0.001f)
  2358. {
  2359. painter->setPen(black);
  2360. painter->drawLine(x, rc.bottom() - 2, x, rc.bottom() - 14);
  2361. painter->drawText(x + 2, rc.top(), QString::number(fFrame));
  2362. painter->setPen(ltgray);
  2363. }
  2364. else
  2365. {
  2366. painter->drawLine(x, rc.bottom() - 2, x, rc.bottom() - 6);
  2367. }
  2368. }
  2369. }
  2370. void CUiAnimViewDopeSheetBase::DrawTimeLineInSeconds(QPainter* painter, const QRect& rc, [[maybe_unused]] const QColor& lineCol, const QColor& textCol, double step)
  2371. {
  2372. Range VisRange = GetVisibleRange();
  2373. const Range& timeRange = m_timeRange;
  2374. int nNumberTicks = 10;
  2375. const QPen ltgray(QColor(90, 90, 90));
  2376. const QPen black(textCol);
  2377. for (float t = TickSnap(timeRange.start); t <= timeRange.end + static_cast<float>(step); t += static_cast<float>(step))
  2378. {
  2379. float st = TickSnap(t);
  2380. if (st > timeRange.end)
  2381. {
  2382. st = timeRange.end;
  2383. }
  2384. if (st < VisRange.start)
  2385. {
  2386. continue;
  2387. }
  2388. if (st > VisRange.end)
  2389. {
  2390. break;
  2391. }
  2392. if (st < m_timeRange.start || st > m_timeRange.end)
  2393. {
  2394. continue;
  2395. }
  2396. int x = TimeToClient(st);
  2397. int k = RoundFloatToInt(st * static_cast<float>(m_ticksStep));
  2398. if (k % nNumberTicks == 0)
  2399. {
  2400. painter->setPen(black);
  2401. painter->drawLine(x, rc.bottom() - 2, x, rc.bottom() - 14);
  2402. painter->drawText(x + 2, rc.top(), QString::number(st));
  2403. painter->setPen(ltgray);
  2404. }
  2405. else
  2406. {
  2407. painter->drawLine(x, rc.bottom() - 2, x, rc.bottom() - 6);
  2408. }
  2409. }
  2410. }
  2411. //////////////////////////////////////////////////////////////////////////
  2412. void CUiAnimViewDopeSheetBase::DrawTimeline(QPainter* painter, const QRect& rcUpdate)
  2413. {
  2414. CUiAnimationContext* pAnimationContext = nullptr;
  2415. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  2416. bool recording = pAnimationContext->IsRecording();
  2417. QColor lineCol(255, 0, 255);
  2418. const QColor textCol(Qt::black);
  2419. const QColor dkgrayCol(90, 90, 90);
  2420. const QColor ltgrayCol(150, 150, 150);
  2421. if (recording)
  2422. {
  2423. lineCol = Qt::red;
  2424. }
  2425. // Draw vertical line showing current time.
  2426. {
  2427. int x = TimeToClient(m_currentTime);
  2428. if (x > m_rcClient.left() && x < m_rcClient.right())
  2429. {
  2430. const QPen prevPen = painter->pen();
  2431. painter->setPen(lineCol);
  2432. painter->drawLine(x, 0, x, m_rcClient.bottom());
  2433. painter->setPen(prevPen);
  2434. }
  2435. }
  2436. const QRect rc = m_rcTimeline;
  2437. if (!rc.intersects(rcUpdate))
  2438. {
  2439. return;
  2440. }
  2441. QLinearGradient gradient(rc.topLeft(), rc.bottomLeft());
  2442. gradient.setColorAt(0, QColor(250, 250, 250));
  2443. gradient.setColorAt(1, QColor(180, 180, 180));
  2444. painter->fillRect(rc, gradient);
  2445. const QPen prevPen = painter->pen();
  2446. const QPen dkgray(dkgrayCol);
  2447. const QPen ltgray(ltgrayCol);
  2448. const QPen black(textCol);
  2449. const QPen redpen(lineCol);
  2450. // Draw time ticks every tick step seconds.
  2451. painter->setPen(dkgray);
  2452. double step = 1.0 / double(m_ticksStep);
  2453. if (GetTickDisplayMode() == eUiAVTickMode_InFrames)
  2454. {
  2455. DrawTimeLineInFrames(painter, rc, lineCol, textCol, step);
  2456. }
  2457. else if (GetTickDisplayMode() == eUiAVTickMode_InSeconds)
  2458. {
  2459. DrawTimeLineInSeconds(painter, rc, lineCol, textCol, step);
  2460. }
  2461. else
  2462. {
  2463. assert (0);
  2464. }
  2465. // Draw time markers.
  2466. int x;
  2467. x = TimeToClient(m_timeMarked.start);
  2468. painter->drawPixmap(QPoint(x, m_rcTimeline.bottom() - 9), QPixmap(":/Trackview/marker/bmp00016_01.png"));
  2469. x = TimeToClient(m_timeMarked.end);
  2470. painter->drawPixmap(QPoint(x - 7, m_rcTimeline.bottom() - 9), QPixmap(":/Trackview/marker/bmp00016_00.png"));
  2471. painter->setPen(redpen);
  2472. x = TimeToClient(m_currentTime);
  2473. painter->setBrush(Qt::NoBrush);
  2474. painter->drawRect(QRect(QPoint(x - 3, rc.top()), QPoint(x + 3, rc.bottom())));
  2475. painter->setPen(redpen);
  2476. painter->drawLine(x, rc.top(), x, rc.bottom());
  2477. painter->setPen(prevPen);
  2478. }
  2479. //////////////////////////////////////////////////////////////////////////
  2480. void CUiAnimViewDopeSheetBase::DrawSummary(QPainter* painter, const QRect& rcUpdate)
  2481. {
  2482. CUiAnimViewSequence* pSequence = nullptr;
  2483. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  2484. const QColor lineCol = Qt::black;
  2485. const QColor fillCol(150, 100, 220);
  2486. const QRect rc = m_rcSummary;
  2487. if (!rc.intersects(rcUpdate))
  2488. {
  2489. return;
  2490. }
  2491. painter->fillRect(rc, fillCol);
  2492. const QPen prevPen = painter->pen();
  2493. painter->setPen(QPen(lineCol, 3));
  2494. // Draw a short thick line at each place where there is a key in any tracks.
  2495. CUiAnimViewKeyBundle keys = pSequence->GetAllKeys();
  2496. for (unsigned int i = 0; i < keys.GetKeyCount(); ++i)
  2497. {
  2498. CUiAnimViewKeyHandle keyHandle = keys.GetKey(i);
  2499. int x = TimeToClient(keyHandle.GetTime());
  2500. painter->drawLine(x, rc.bottom() - 2, x, rc.top() + 2);
  2501. }
  2502. painter->setPen(prevPen);
  2503. }
  2504. //////////////////////////////////////////////////////////////////////////
  2505. void CUiAnimViewDopeSheetBase::DrawNodeTrack(CUiAnimViewAnimNode* pAnimNode, QPainter* painter, const QRect& trackRect)
  2506. {
  2507. const QFont prevFont = painter->font();
  2508. painter->setFont(m_descriptionFont);
  2509. CUiAnimViewAnimNode* pDirectorNode = pAnimNode->GetDirector();
  2510. if (pDirectorNode->GetNodeType() != eUiAVNT_Sequence && !pDirectorNode->IsActiveDirector())
  2511. {
  2512. painter->setPen(INACTIVE_TEXT_COLOR);
  2513. }
  2514. else
  2515. {
  2516. painter->setPen(KEY_TEXT_COLOR);
  2517. }
  2518. const QRect textRect = trackRect.adjusted(4, 0, -4, 0);
  2519. QString sAnimNodeName = QString::fromUtf8(pAnimNode->GetName().c_str());
  2520. const bool hasObsoleteTrack = pAnimNode->HasObsoleteTrack();
  2521. if (hasObsoleteTrack)
  2522. {
  2523. painter->setPen(QColor(245, 80, 70));
  2524. sAnimNodeName += tr(": Some of the sub-tracks contains obsoleted TCB splines (marked in red), thus cannot be copied or pasted.");
  2525. }
  2526. painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, painter->fontMetrics().elidedText(sAnimNodeName, Qt::ElideRight, textRect.width()));
  2527. painter->setFont(prevFont);
  2528. }
  2529. //////////////////////////////////////////////////////////////////////////
  2530. void CUiAnimViewDopeSheetBase::DrawGoToTrackArrow(CUiAnimViewTrack* pTrack, QPainter* painter, const QRect& rc)
  2531. {
  2532. int numKeys = pTrack->GetKeyCount();
  2533. const QColor colorLine(150, 150, 150);
  2534. const QColor colorHeader(50, 50, 50);
  2535. const int tickness = 2;
  2536. const int halfMargin = (rc.height() - tickness) / 2;
  2537. for (int i = 0; i < numKeys; ++i)
  2538. {
  2539. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  2540. IDiscreteFloatKey discreteFloatKey;
  2541. keyHandle.GetKey(&discreteFloatKey);
  2542. int arrowStart = TimeToClient(discreteFloatKey.time);
  2543. int arrowEnd = TimeToClient(discreteFloatKey.m_fValue);
  2544. if (discreteFloatKey.m_fValue < 0.f)
  2545. {
  2546. continue;
  2547. }
  2548. // draw arrow body line
  2549. if (arrowStart < arrowEnd)
  2550. {
  2551. painter->fillRect(QRect(QPoint(arrowStart, rc.top() + halfMargin), QPoint(arrowEnd, rc.bottom() - halfMargin)), colorLine);
  2552. }
  2553. else if (arrowStart > arrowEnd)
  2554. {
  2555. painter->fillRect(QRect(QPoint(arrowEnd, rc.top() + halfMargin), QPoint(arrowStart, rc.bottom() - halfMargin)), colorLine);
  2556. }
  2557. // draw arrow head
  2558. if (arrowStart != arrowEnd)
  2559. {
  2560. painter->fillRect(QRect(QPoint(arrowEnd, rc.top() + 2), QPoint(arrowEnd + 1, rc.bottom() - 2)), colorHeader);
  2561. }
  2562. }
  2563. }
  2564. //////////////////////////////////////////////////////////////////////////
  2565. void CUiAnimViewDopeSheetBase::DrawKeyDuration(CUiAnimViewTrack* pTrack, QPainter* painter, const QRect& rc, int keyIndex)
  2566. {
  2567. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(keyIndex);
  2568. const float time = keyHandle.GetTime();
  2569. const float duration = keyHandle.GetDuration();
  2570. int x = TimeToClient(time);
  2571. // Draw key duration.
  2572. float endt = min(time + duration, m_timeRange.end);
  2573. int x1 = TimeToClient(endt);
  2574. if (x1 < 0)
  2575. {
  2576. if (x > 0)
  2577. {
  2578. x1 = rc.right();
  2579. }
  2580. }
  2581. const QBrush prevBrush = painter->brush();
  2582. painter->setBrush(m_visibilityBrush);
  2583. QColor colorFrom(120, 120, 255);
  2584. QLinearGradient gradient(x, rc.top() + 3, x, rc.bottom() - 3);
  2585. gradient.setColorAt(0, colorFrom);
  2586. gradient.setColorAt(1, QColor(250, 250, 250));
  2587. const int width = x1 + 1 - x;
  2588. painter->fillRect(QRect(x, rc.top() + 3, width, rc.height() - 3), gradient);
  2589. painter->setBrush(prevBrush);
  2590. painter->drawLine(x1, rc.top(), x1, rc.bottom());
  2591. }
  2592. //////////////////////////////////////////////////////////////////////////
  2593. void CUiAnimViewDopeSheetBase::DrawColorGradient(QPainter* painter, const QRect& rc, const CUiAnimViewTrack* pTrack)
  2594. {
  2595. const QPen pOldPen = painter->pen();
  2596. for (int x = rc.left(); x < rc.right(); ++x)
  2597. {
  2598. // This is really slow. Is there a better way?
  2599. Vec3 vColor(0, 0, 0);
  2600. pTrack->GetValue(TimeFromPointUnsnapped(QPoint(x, rc.top())), vColor);
  2601. painter->setPen(ColorLinearToGamma(vColor / 255.0f));
  2602. painter->drawLine(x, rc.top(), x, rc.bottom());
  2603. }
  2604. painter->setPen(pOldPen);
  2605. }
  2606. //////////////////////////////////////////////////////////////////////////
  2607. QRect CUiAnimViewDopeSheetBase::GetNodeRect(const CUiAnimViewNode* pNode) const
  2608. {
  2609. CUiAnimViewNodesCtrl::CRecord* pRecord = m_pNodesCtrl->GetNodeRecord(pNode);
  2610. if (pRecord && pRecord->IsVisible())
  2611. {
  2612. QRect recordRect = pRecord->GetRect();
  2613. return QRect(0, recordRect.top(), m_rcClient.width(), recordRect.height());
  2614. }
  2615. return QRect();
  2616. }
  2617. //////////////////////////////////////////////////////////////////////////
  2618. void CUiAnimViewDopeSheetBase::StoreMementoForTracksWithSelectedKeys()
  2619. {
  2620. CUiAnimViewSequence* pSequence = nullptr;
  2621. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  2622. CUiAnimViewKeyBundle selectedKeys = pSequence->GetSelectedKeys();
  2623. m_trackMementos.clear();
  2624. // Construct the set of tracks that have selected keys
  2625. std::set<CUiAnimViewTrack*> tracks;
  2626. const unsigned int numKeys = selectedKeys.GetKeyCount();
  2627. for (unsigned int keyIndex = 0; keyIndex < numKeys; ++keyIndex)
  2628. {
  2629. CUiAnimViewKeyHandle keyHandle = selectedKeys.GetKey(keyIndex);
  2630. tracks.insert(keyHandle.GetTrack());
  2631. }
  2632. // For each of those tracks store an undo object
  2633. for (auto iter = tracks.begin(); iter != tracks.end(); ++iter)
  2634. {
  2635. CUiAnimViewTrack* pTrack = *iter;
  2636. TrackMemento trackMemento;
  2637. trackMemento.m_memento = pTrack->GetMemento();
  2638. const unsigned int trackNumKeys = pTrack->GetKeyCount();
  2639. for (unsigned int i = 0; i < trackNumKeys; ++i)
  2640. {
  2641. trackMemento.m_keySelectionStates.push_back(pTrack->GetKey(i).IsSelected());
  2642. }
  2643. m_trackMementos[pTrack] = trackMemento;
  2644. }
  2645. }