SplineCtrlEx.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341
  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 "SplineCtrlEx.h"
  10. // Qt
  11. #include <QPainter>
  12. #include <QPainterPath>
  13. #include <QToolTip>
  14. #include <QRubberBand>
  15. // Editor
  16. #include "TimelineCtrl.h"
  17. #include "Clipboard.h"
  18. #include "GridUtils.h"
  19. #define DEFAULT_MIN_TIME_EPSILON 0.001f
  20. #define MIN_TIME_EPSILON_FOR_SCALING 0.1f
  21. #define ACTIVE_BKG_COLOR QColor(190, 190, 190)
  22. #define GRID_COLOR QColor(110, 110, 110)
  23. #define EDIT_SPLINE_COLOR QColor(128, 255, 128)
  24. #define MIN_PIXEL_PER_GRID_X 50
  25. #define MIN_PIXEL_PER_GRID_Y 10
  26. #define LEFT_BORDER_OFFSET 40
  27. const float AbstractSplineWidget::threshold = 0.015f;
  28. static const float minViewRange = 0.1f;
  29. //////////////////////////////////////////////////////////////////////////
  30. static void SetKeyTangentType(ISplineInterpolator* pSpline, int key, ESplineKeyTangentType type)
  31. {
  32. int flags = (pSpline->GetKeyFlags(key) & ~SPLINE_KEY_TANGENT_IN_MASK) & ~SPLINE_KEY_TANGENT_OUT_MASK;
  33. pSpline->SetKeyFlags(key, flags | (type << SPLINE_KEY_TANGENT_IN_SHIFT) | (type << SPLINE_KEY_TANGENT_OUT_SHIFT));
  34. }
  35. //////////////////////////////////////////////////////////////////////////
  36. class CUndoSplineCtrlEx
  37. : public ISplineCtrlUndo
  38. {
  39. public:
  40. CUndoSplineCtrlEx(AbstractSplineWidget* pCtrl, std::vector<ISplineInterpolator*>& splineContainer)
  41. {
  42. m_pCtrl = FindControl(pCtrl);
  43. for (size_t splineIndex = 0; splineIndex < splineContainer.size(); ++splineIndex)
  44. {
  45. AddSpline(splineContainer[splineIndex]);
  46. }
  47. SerializeSplines(&SplineEntry::undo, false);
  48. }
  49. protected:
  50. void AddSpline(ISplineInterpolator* pSpline)
  51. {
  52. AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
  53. m_splineEntries.resize(m_splineEntries.size() + 1);
  54. SplineEntry& entry = m_splineEntries.back();
  55. ISplineSet* pSplineSet = (pCtrl ? pCtrl->m_pSplineSet : nullptr);
  56. entry.id = (pSplineSet ? pSplineSet->GetIDFromSpline(pSpline) : AZStd::string{});
  57. entry.pSpline = pSpline;
  58. const int numKeys = pSpline->GetKeyCount();
  59. entry.keySelectionFlags.reserve(numKeys);
  60. for (int i = 0; i < numKeys; ++i)
  61. {
  62. entry.keySelectionFlags.push_back(pSpline->GetKeyFlags(i) & ESPLINE_KEY_UI_SELECTED_MASK);
  63. }
  64. }
  65. int GetSize() override { return sizeof(*this); }
  66. void Undo(bool bUndo) override
  67. {
  68. AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
  69. if (pCtrl)
  70. {
  71. pCtrl->SendNotifyEvent(SPLN_BEFORE_CHANGE);
  72. }
  73. if (bUndo)
  74. {
  75. SerializeSplines(&SplineEntry::redo, false);
  76. }
  77. SerializeSplines(&SplineEntry::undo, true);
  78. if (pCtrl && bUndo)
  79. {
  80. pCtrl->m_bKeyTimesDirty = true;
  81. pCtrl->SendNotifyEvent(SPLN_CHANGE);
  82. pCtrl->update();
  83. }
  84. }
  85. void Redo() override
  86. {
  87. AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
  88. if (pCtrl)
  89. {
  90. pCtrl->SendNotifyEvent(SPLN_BEFORE_CHANGE);
  91. }
  92. SerializeSplines(&SplineEntry::redo, true);
  93. if (pCtrl)
  94. {
  95. pCtrl->m_bKeyTimesDirty = true;
  96. pCtrl->SendNotifyEvent(SPLN_CHANGE);
  97. pCtrl->update();
  98. }
  99. }
  100. private:
  101. class SplineEntry
  102. {
  103. public:
  104. std::vector<int> keySelectionFlags;
  105. _smart_ptr<ISplineBackup> undo;
  106. _smart_ptr<ISplineBackup> redo;
  107. AZStd::string id;
  108. ISplineInterpolator* pSpline;
  109. };
  110. void SerializeSplines(_smart_ptr<ISplineBackup> SplineEntry::* backup, bool bLoading)
  111. {
  112. AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
  113. ISplineSet* pSplineSet = (pCtrl ? pCtrl->m_pSplineSet : nullptr);
  114. for (auto it = m_splineEntries.begin(); it != m_splineEntries.end(); ++it)
  115. {
  116. SplineEntry& entry = *it;
  117. ISplineInterpolator* pSpline = (pSplineSet ? pSplineSet->GetSplineFromID(entry.id) : entry.pSpline);
  118. if (!pSpline && pCtrl && pCtrl->GetSplineCount() > 0)
  119. {
  120. pSpline = pCtrl->GetSpline(0);
  121. }
  122. if (pSpline && bLoading)
  123. {
  124. pSpline->Restore(entry.*backup);
  125. }
  126. else if (pSpline)
  127. {
  128. (entry.*backup) = pSpline->Backup();
  129. }
  130. }
  131. }
  132. public:
  133. using CSplineCtrls = std::list<AbstractSplineWidget *>;
  134. static AbstractSplineWidget* FindControl(AbstractSplineWidget* pCtrl)
  135. {
  136. if (!pCtrl)
  137. {
  138. return nullptr;
  139. }
  140. auto iter = std::find(s_activeCtrls.begin(), s_activeCtrls.end(), pCtrl);
  141. if (iter == s_activeCtrls.end())
  142. {
  143. return nullptr;
  144. }
  145. return *iter;
  146. }
  147. static void RegisterControl(AbstractSplineWidget* pCtrl)
  148. {
  149. if (!FindControl(pCtrl))
  150. {
  151. s_activeCtrls.push_back(pCtrl);
  152. }
  153. }
  154. static void UnregisterControl(AbstractSplineWidget* pCtrl)
  155. {
  156. if (FindControl(pCtrl))
  157. {
  158. s_activeCtrls.remove(pCtrl);
  159. }
  160. }
  161. static CSplineCtrls s_activeCtrls;
  162. bool IsSelectionChanged() const override
  163. {
  164. AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
  165. ISplineSet* pSplineSet = (pCtrl ? pCtrl->m_pSplineSet : nullptr);
  166. for (auto it = m_splineEntries.begin(); it != m_splineEntries.end(); ++it)
  167. {
  168. const SplineEntry& entry = *it;
  169. ISplineInterpolator* pSpline = (pSplineSet ? pSplineSet->GetSplineFromID(entry.id) : entry.pSpline);
  170. if (!pSpline && pCtrl && pCtrl->GetSplineCount() > 0)
  171. {
  172. pSpline = pCtrl->GetSpline(0);
  173. }
  174. if (!pSpline)
  175. {
  176. return false;
  177. }
  178. if (pSpline->GetKeyCount() != entry.keySelectionFlags.size())
  179. {
  180. return true;
  181. }
  182. for (int i = 0; i < pSpline->GetKeyCount(); ++i)
  183. {
  184. if (entry.keySelectionFlags[i] != (pSpline->GetKeyFlags(i) & ESPLINE_KEY_UI_SELECTED_MASK))
  185. {
  186. return true;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. private:
  193. AbstractSplineWidget* m_pCtrl;
  194. std::vector<SplineEntry> m_splineEntries;
  195. std::vector<float> m_keyTimes;
  196. };
  197. CUndoSplineCtrlEx::CSplineCtrls CUndoSplineCtrlEx::s_activeCtrls;
  198. SplineWidget::SplineWidget(QWidget* parent)
  199. : QWidget(parent)
  200. , m_rubberBand(new QRubberBand(QRubberBand::Rectangle, this))
  201. {
  202. m_rubberBand->setVisible(false);
  203. setMouseTracking(true);
  204. setFocusPolicy(Qt::StrongFocus);
  205. }
  206. SplineWidget::~SplineWidget()
  207. {
  208. }
  209. //////////////////////////////////////////////////////////////////////////
  210. AbstractSplineWidget::AbstractSplineWidget()
  211. : m_defaultKeyTangentType(SPLINE_KEY_TANGENT_NONE)
  212. {
  213. m_pTimelineCtrl = nullptr;
  214. m_totalSplineCount = 0;
  215. m_pHitSpline = nullptr;
  216. m_pHitDetailSpline = nullptr;
  217. m_nHitKeyIndex = -1;
  218. m_nHitDimension = -1;
  219. m_bHitIncomingHandle = true;
  220. m_nKeyDrawRadius = 3;
  221. m_gridX = 10;
  222. m_gridY = 10;
  223. m_timeRange.start = 0;
  224. m_timeRange.end = 1;
  225. m_fMinValue = -1;
  226. m_fMaxValue = 1;
  227. m_valueRange.Set(-1, 1);
  228. m_fTooltipScaleX = 1;
  229. m_fTooltipScaleY = 1;
  230. m_cMousePos = QPoint(0, 0);
  231. m_fTimeScale = 1;
  232. m_fValueScale = 1;
  233. m_fGridTimeScale = 30.0f;
  234. m_ticksStep = 10;
  235. m_fTimeMarker = -10;
  236. m_editMode = NothingMode;
  237. m_bSnapTime = false;
  238. m_bSnapValue = false;
  239. m_bBitmapValid = false;
  240. m_nLeftOffset = LEFT_BORDER_OFFSET;
  241. m_grid.zoom = AZ::Vector2(200,100);
  242. m_bKeyTimesDirty = false;
  243. m_rcSelect = QRect();
  244. m_rcSpline = QRect();
  245. m_boLeftMouseButtonDown = false;
  246. m_pSplineSet = nullptr;
  247. m_controlAmplitude = false;
  248. m_fMinTimeEpsilon = DEFAULT_MIN_TIME_EPSILON;
  249. m_defaultValueRange.Set(-1.1f, 1.1f);
  250. m_bEditLock = false;
  251. m_pCurrentUndo = nullptr;
  252. CUndoSplineCtrlEx::RegisterControl(this);
  253. }
  254. //////////////////////////////////////////////////////////////////////////
  255. AbstractSplineWidget::~AbstractSplineWidget()
  256. {
  257. CUndoSplineCtrlEx::UnregisterControl(this);
  258. }
  259. //////////////////////////////////////////////////////////////////////////
  260. Vec2 AbstractSplineWidget::GetZoom()
  261. {
  262. return Vec2(m_grid.zoom.GetX(), m_grid.zoom.GetY());
  263. }
  264. Vec2 AbstractSplineWidget::GetScrollOffset()
  265. {
  266. return Vec2(m_grid.origin.GetX(), m_grid.origin.GetY());
  267. }
  268. //////////////////////////////////////////////////////////////////////////
  269. void AbstractSplineWidget::SetZoom(Vec2 zoom, const QPoint& center)
  270. {
  271. m_grid.SetZoom(zoom, QPoint(center.x(), m_rcSpline.bottom() + 1 - center.y()));
  272. SetScrollOffset(Vec2(m_grid.origin.GetX(), m_grid.origin.GetY()));
  273. if (m_pTimelineCtrl)
  274. {
  275. m_pTimelineCtrl->setZoom(zoom.x, m_grid.origin.GetX());
  276. }
  277. update();
  278. }
  279. //////////////////////////////////////////////////////////////////////////
  280. void AbstractSplineWidget::SetZoom(Vec2 zoom)
  281. {
  282. m_grid.zoom = AZ::Vector2(zoom.x,zoom.y);
  283. SetScrollOffset(Vec2(m_grid.origin.GetX(), m_grid.origin.GetY()));
  284. if (m_pTimelineCtrl)
  285. {
  286. m_pTimelineCtrl->setZoom(zoom.x, m_grid.origin.GetX());
  287. }
  288. SendNotifyEvent(SPLN_SCROLL_ZOOM);
  289. update();
  290. }
  291. //////////////////////////////////////////////////////////////////////////
  292. void AbstractSplineWidget::SetScrollOffset(Vec2 ofs)
  293. {
  294. m_grid.origin = AZ::Vector2(ofs.x, ofs.y);
  295. if (m_pTimelineCtrl)
  296. {
  297. m_pTimelineCtrl->setZoom(m_grid.zoom.GetX(), m_grid.origin.GetX());
  298. }
  299. SendNotifyEvent(SPLN_SCROLL_ZOOM);
  300. update();
  301. }
  302. //////////////////////////////////////////////////////////////////////////
  303. float AbstractSplineWidget::SnapTime(float time)
  304. {
  305. if (m_bSnapTime)
  306. {
  307. const float step = m_grid.step.GetX() / 10.0f;
  308. return floor((time / step) + 0.5f) * step;
  309. }
  310. return time;
  311. }
  312. //////////////////////////////////////////////////////////////////////////
  313. float AbstractSplineWidget::SnapValue(float val)
  314. {
  315. if (m_bSnapValue)
  316. {
  317. const float step = m_grid.step.GetY();
  318. return floor((val / step) + 0.5f) * step;
  319. }
  320. return val;
  321. }
  322. //////////////////////////////////////////////////////////////////////////
  323. int AbstractSplineWidget::GetKeyTimeCount() const
  324. {
  325. UpdateKeyTimes();
  326. return int(m_keyTimes.size());
  327. }
  328. //////////////////////////////////////////////////////////////////////////
  329. float AbstractSplineWidget::GetKeyTime(int index) const
  330. {
  331. UpdateKeyTimes();
  332. return m_keyTimes[index].time;
  333. }
  334. //////////////////////////////////////////////////////////////////////////
  335. bool AbstractSplineWidget::GetKeyTimeSelected(int index) const
  336. {
  337. UpdateKeyTimes();
  338. return m_keyTimes[index].selected;
  339. }
  340. //////////////////////////////////////////////////////////////////////////
  341. void AbstractSplineWidget::SetKeyTimeSelected(int index, bool selected)
  342. {
  343. m_keyTimes[index].selected = selected;
  344. }
  345. //////////////////////////////////////////////////////////////////////////
  346. int AbstractSplineWidget::GetKeyCount(int index) const
  347. {
  348. UpdateKeyTimes();
  349. return m_keyTimes[index].count;
  350. }
  351. //////////////////////////////////////////////////////////////////////////
  352. int AbstractSplineWidget::GetKeyCountBound() const
  353. {
  354. UpdateKeyTimes();
  355. return m_totalSplineCount;
  356. }
  357. //////////////////////////////////////////////////////////////////////////
  358. void AbstractSplineWidget::BeginEdittingKeyTimes()
  359. {
  360. if (CUndo::IsRecording())
  361. {
  362. GetIEditor()->CancelUndo();
  363. m_pCurrentUndo = nullptr;
  364. }
  365. GetIEditor()->BeginUndo();
  366. for (int keyTimeIndex = 0; keyTimeIndex < int(m_keyTimes.size()); ++keyTimeIndex)
  367. {
  368. m_keyTimes[keyTimeIndex].oldTime = m_keyTimes[keyTimeIndex].time;
  369. }
  370. }
  371. //////////////////////////////////////////////////////////////////////////
  372. void AbstractSplineWidget::EndEdittingKeyTimes()
  373. {
  374. if (CUndo::IsRecording())
  375. {
  376. GetIEditor()->AcceptUndo("Batch key move");
  377. m_pCurrentUndo = nullptr;
  378. }
  379. m_bKeyTimesDirty = true;
  380. update();
  381. if (m_pTimelineCtrl)
  382. {
  383. m_pTimelineCtrl->update();
  384. }
  385. }
  386. //////////////////////////////////////////////////////////////////////////
  387. void AbstractSplineWidget::MoveKeyTimes(int numChanges, int* indices, float scale, float offset, bool copyKeys)
  388. {
  389. if (CUndo::IsRecording())
  390. {
  391. GetIEditor()->RestoreUndo();
  392. std::vector<ISplineInterpolator*> splines;
  393. for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
  394. {
  395. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  396. if (pSpline)
  397. {
  398. splines.push_back(pSpline);
  399. }
  400. }
  401. CUndo::Record(m_pCurrentUndo = CreateSplineCtrlUndoObject(splines));
  402. for (int keyTimeIndex = 0; keyTimeIndex < int(m_keyTimes.size()); ++keyTimeIndex)
  403. {
  404. m_keyTimes[keyTimeIndex].time = m_keyTimes[keyTimeIndex].oldTime;
  405. }
  406. }
  407. class KeyChange
  408. {
  409. public:
  410. KeyChange(ISplineInterpolator* pSpline, int keyIndex, float oldTime, float newTime, int flags)
  411. : pSpline(pSpline)
  412. , keyIndex(keyIndex)
  413. , oldTime(oldTime)
  414. , newTime(newTime)
  415. , flags(flags) {}
  416. ISplineInterpolator* pSpline;
  417. int keyIndex;
  418. float oldTime;
  419. float newTime;
  420. ISplineInterpolator::ValueType value;
  421. int flags;
  422. ISplineInterpolator::ValueType tin, tout;
  423. };
  424. std::vector<KeyChange> individualKeyChanges;
  425. for (int changeIndex = 0; indices && changeIndex < numChanges; ++changeIndex)
  426. {
  427. int index = (indices ? indices[changeIndex] : 0);
  428. float oldTime = m_keyTimes[index].time;
  429. float time = __max(m_timeRange.start, __min(m_timeRange.end, scale * oldTime + offset));
  430. for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
  431. {
  432. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  433. for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
  434. {
  435. float keyTime = pSpline->GetKeyTime(keyIndex);
  436. KeyChange change(pSpline, keyIndex, keyTime, SnapTimeToGridVertical(time), pSpline->GetKeyFlags(keyIndex));
  437. pSpline->GetKeyValue(keyIndex, change.value);
  438. pSpline->GetKeyTangents(keyIndex, change.tin, change.tout);
  439. if (fabsf(keyTime - oldTime) < threshold)
  440. {
  441. individualKeyChanges.push_back(change);
  442. }
  443. }
  444. }
  445. m_keyTimes[index].time = SnapTimeToGridVertical(time);
  446. }
  447. for (std::vector<KeyChange>::iterator itChange = individualKeyChanges.begin(); itChange != individualKeyChanges.end(); ++itChange)
  448. {
  449. (*itChange).pSpline->SetKeyTime((*itChange).keyIndex, (*itChange).newTime);
  450. }
  451. if (copyKeys)
  452. {
  453. for (std::vector<KeyChange>::iterator keyToAdd = individualKeyChanges.begin(), endKeysToAdd = individualKeyChanges.end(); keyToAdd != endKeysToAdd; ++keyToAdd)
  454. {
  455. int keyIndex = (*keyToAdd).pSpline->InsertKey((*keyToAdd).oldTime, (*keyToAdd).value);
  456. (*keyToAdd).pSpline->SetKeyTangents(keyIndex, (*keyToAdd).tin, (*keyToAdd).tout);
  457. (*keyToAdd).pSpline->SetKeyFlags(keyIndex, (*keyToAdd).flags & (~ESPLINE_KEY_UI_SELECTED_MASK));
  458. }
  459. }
  460. // Loop through all moved keys, checking whether there are multiple keys on the same frame.
  461. for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
  462. {
  463. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  464. float lastKeyTime = -FLT_MAX;
  465. pSpline->Update();
  466. for (int keyIndex = 0, keys = pSpline->GetKeyCount(); keyIndex <= keys; )
  467. {
  468. float keyTime = pSpline->GetKeyTime(keyIndex);
  469. if (fabsf(keyTime - lastKeyTime) < m_fMinTimeEpsilon)
  470. {
  471. --keys;
  472. pSpline->RemoveKey(keyIndex);
  473. }
  474. else
  475. {
  476. ++keyIndex;
  477. lastKeyTime = keyTime;
  478. }
  479. }
  480. }
  481. SendNotifyEvent(SPLN_CHANGE);
  482. update();
  483. if (m_pTimelineCtrl)
  484. {
  485. m_pTimelineCtrl->update();
  486. }
  487. }
  488. //////////////////////////////////////////////////////////////////////////
  489. void SplineWidget::resizeEvent(QResizeEvent* event)
  490. {
  491. QRect oldRect = m_rcSpline;
  492. QWidget::resizeEvent(event);
  493. m_rcClient = rect();
  494. m_rcSpline = m_rcClient;
  495. if (m_pTimelineCtrl)
  496. {
  497. QRect rct = m_rcSpline;
  498. const int timelineControlHeight = 16;
  499. rct.setHeight(timelineControlHeight);
  500. m_rcSpline.setTop(rct.bottom() + 1);
  501. rct.setLeft(rct.left() + m_nLeftOffset);
  502. m_pTimelineCtrl->setGeometry(rct);
  503. }
  504. m_rcSpline.setLeft(m_rcSpline.left() + m_nLeftOffset);
  505. m_grid.rect = m_rcSpline;
  506. int oldW = oldRect.width();
  507. int oldH = oldRect.height();
  508. if (width() > 1 && height() > 1 && oldW > 1 && oldH > 1 && m_rcSpline.width() > 0 && m_rcSpline.height())
  509. {
  510. SetZoom(Vec2(float(m_rcSpline.width()) / oldW * GetZoom().x, float(m_rcSpline.height()) / oldH * GetZoom().y));
  511. }
  512. }
  513. //////////////////////////////////////////////////////////////////////////
  514. QPoint AbstractSplineWidget::TimeToPoint(float time, ISplineInterpolator* pSpline)
  515. {
  516. float val = 0;
  517. if (pSpline)
  518. {
  519. pSpline->InterpolateFloat(time, val);
  520. }
  521. return WorldToClient(Vec2(time, val));
  522. ;
  523. }
  524. //////////////////////////////////////////////////////////////////////////
  525. float AbstractSplineWidget::TimeToXOfs(float x)
  526. {
  527. return static_cast<float>(WorldToClient(Vec2(float(x), 0.0f)).x());
  528. }
  529. //////////////////////////////////////////////////////////////////////////
  530. void AbstractSplineWidget::PointToTimeValue(QPoint point, float& time, float& value)
  531. {
  532. Vec2 v = ClientToWorld(point);
  533. value = v.y;
  534. time = XOfsToTime(point.x());
  535. }
  536. //////////////////////////////////////////////////////////////////////////
  537. float AbstractSplineWidget::XOfsToTime(int x)
  538. {
  539. Vec2 v = ClientToWorld(QPoint(x, 0));
  540. float time = v.x;
  541. return time;
  542. }
  543. //////////////////////////////////////////////////////////////////////////
  544. QPoint AbstractSplineWidget::XOfsToPoint(int x, ISplineInterpolator* pSpline)
  545. {
  546. return TimeToPoint(XOfsToTime(x), pSpline);
  547. }
  548. //////////////////////////////////////////////////////////////////////////
  549. QPoint AbstractSplineWidget::WorldToClient(Vec2 v)
  550. {
  551. QPoint p = m_grid.WorldToClient(v);
  552. p.setY(m_rcSpline.bottom() - p.y());
  553. return p;
  554. }
  555. //////////////////////////////////////////////////////////////////////////
  556. Vec2 AbstractSplineWidget::ClientToWorld(const QPoint& point)
  557. {
  558. Vec2 v = m_grid.ClientToWorld(QPoint(point.x(), m_rcSpline.bottom() + 1 - point.y()));
  559. return v;
  560. }
  561. void SplineWidget::paintEvent(QPaintEvent* event)
  562. {
  563. QPainter painter(this);
  564. if (m_TimeUpdateRect != event->rect())
  565. {
  566. painter.fillRect(event->rect(), QColor(160, 160, 160));
  567. m_grid.CalculateGridLines();
  568. //Draw Grid
  569. DrawGrid(&painter);
  570. const QRect drawSplineRect = event->rect().intersected(m_rcSpline);
  571. // Calculate the times corresponding to the left and right of the area to be painted -
  572. // we can use this to draw only the necessary parts of the splines.
  573. float startTime = XOfsToTime(drawSplineRect.left());
  574. float endTime = XOfsToTime(drawSplineRect.right());
  575. //Draw Keys and Curve
  576. for (int i = 0; i < int(m_splines.size()); ++i)
  577. {
  578. DrawSpline(&painter, m_splines[i], startTime, endTime);
  579. DrawKeys(&painter, i, startTime, endTime);
  580. }
  581. }
  582. m_TimeUpdateRect = QRect();
  583. DrawTimeMarker(&painter);
  584. }
  585. //////////////////////////////////////////////////////////////////////////
  586. class SplineControlVerticalLineDrawer
  587. {
  588. public:
  589. SplineControlVerticalLineDrawer(QPainter* painter, const QRect& rect)
  590. : rect(rect)
  591. , painter(painter)
  592. {
  593. }
  594. void operator()([[maybe_unused]] int frameIndex, int x)
  595. {
  596. if (painter)
  597. {
  598. painter->drawLine(x, rect.top(), x, rect.bottom());
  599. }
  600. }
  601. QPainter* painter;
  602. QRect rect;
  603. };
  604. void SplineWidget::DrawGrid(QPainter* painter)
  605. {
  606. QPoint ptTop = WorldToClient(Vec2(0.0f, m_valueRange.end));
  607. QPoint ptBottom = WorldToClient(Vec2(0.0f, m_valueRange.start));
  608. QPoint pt0 = WorldToClient(Vec2(m_timeRange.start, 0));
  609. QPoint pt1 = WorldToClient(Vec2(m_timeRange.end, 0));
  610. QRect timeRc = QRect(QPoint(pt0.x() - 2, ptTop.y()), QPoint(pt1.x() + 2, ptBottom.y()) - QPoint(1, 1));
  611. timeRc = timeRc.intersected(m_rcSpline);
  612. painter->fillRect(timeRc, ACTIVE_BKG_COLOR);
  613. //////////////////////////////////////////////////////////////////////////
  614. QPen pOldPen = painter->pen();
  615. painter->setPen(GRID_COLOR);
  616. /// Draw Left Separator.
  617. painter->fillRect(QRect(QPoint(m_rcClient.left(), m_rcClient.top()), QPoint(m_rcClient.left() + m_nLeftOffset - 1, m_rcClient.bottom()) - QPoint(1, 1)), ACTIVE_BKG_COLOR);
  618. painter->drawLine(m_rcClient.left() + m_nLeftOffset, m_rcClient.bottom(), m_rcClient.left() + m_nLeftOffset, m_rcClient.top());
  619. //////////////////////////////////////////////////////////////////////////
  620. int gy;
  621. QPen pen(QColor(GRID_COLOR), 1, Qt::DotLine);
  622. pen.setCosmetic(true);
  623. painter->setPen(pen);
  624. //if (m_grid.pixelsPerGrid.y >= MIN_PIXEL_PER_GRID_Y)
  625. {
  626. // Draw horizontal grid lines.
  627. for (gy = m_grid.firstGridLine.y(); gy < m_grid.firstGridLine.y() + m_grid.numGridLines.y() + 1; gy++)
  628. {
  629. int y = m_grid.GetGridLineY(gy);
  630. if (y < 0)
  631. {
  632. continue;
  633. }
  634. int py = m_rcSpline.height() - y;
  635. if (py < m_rcSpline.top() || py > m_rcSpline.bottom() + 1)
  636. {
  637. continue;
  638. }
  639. painter->setPen(pen);
  640. painter->drawLine(m_rcSpline.left(), py, m_rcSpline.right(), py);
  641. float v = m_grid.GetGridLineYValue(gy);
  642. v = floor(v * 1000.0f + 0.5f) / 1000.0f;
  643. if ((v >= m_valueRange.start && v <= m_valueRange.end) || fabs(v - m_valueRange.start) < 0.01f || fabs(v - m_valueRange.end) < 0.01f)
  644. {
  645. painter->setPen(Qt::black);
  646. painter->drawText(m_rcClient.left() + 2, py - 8, QString::number(v));
  647. }
  648. }
  649. }
  650. // Draw vertical grid lines.
  651. SplineControlVerticalLineDrawer verticalLineDrawer(painter, m_rcSpline);
  652. GridUtils::IterateGrid(verticalLineDrawer, 50.0f, m_grid.zoom.GetX(), m_grid.origin.GetX(), m_fGridTimeScale, m_grid.rect.left(), m_grid.rect.right() + 1);
  653. //////////////////////////////////////////////////////////////////////////
  654. {
  655. const QPen pen0(QColor(110, 100, 100), 2);
  656. const QPoint p = WorldToClient(Vec2(0, 0));
  657. painter->setPen(pen0);
  658. /// Draw X axis.
  659. painter->drawLine(m_rcSpline.left(), p.y(), m_rcSpline.right() + 1, p.y());
  660. // Draw Y Axis.
  661. if (p.x() > m_rcSpline.left() && p.y() < m_rcSpline.right() + 1)
  662. {
  663. painter->drawLine(p.x(), m_rcSpline.top(), p.x(), m_rcSpline.bottom() + 1);
  664. }
  665. }
  666. //////////////////////////////////////////////////////////////////////////
  667. painter->setPen(pOldPen);
  668. }
  669. //////////////////////////////////////////////////////////////////////////
  670. void SplineWidget::DrawSpline(QPainter* painter, SSplineInfo& splineInfo, float startTime, float endTime)
  671. {
  672. const QPen pOldPen = painter->pen();
  673. //////////////////////////////////////////////////////////////////////////
  674. ISplineInterpolator* pSpline = splineInfo.pSpline;
  675. ISplineInterpolator* pDetailSpline = splineInfo.pDetailSpline;
  676. if (!pSpline)
  677. {
  678. return;
  679. }
  680. int nTotalNumberOfDimensions(0);
  681. int nCurrentDimension(0);
  682. int left = static_cast<int>(TimeToXOfs(startTime));//rcClip.left;
  683. int right = static_cast<int>(TimeToXOfs(endTime));//rcClip.right;
  684. QPoint p0 = TimeToPoint(pSpline->GetKeyTime(0), pSpline);
  685. QPoint p1 = TimeToPoint(pSpline->GetKeyTime(pSpline->GetKeyCount() - 1), pSpline);
  686. nTotalNumberOfDimensions = pSpline->GetNumDimensions();
  687. for (nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
  688. {
  689. QColor splineColor = EDIT_SPLINE_COLOR;
  690. splineColor = splineInfo.anColorArray[nCurrentDimension];
  691. const QPen pen(splineColor, 2);
  692. if (p0.x() > left && !pDetailSpline)
  693. {
  694. QPen alternatePen(splineColor, 1, Qt::DotLine);
  695. alternatePen.setCosmetic(true);
  696. painter->setPen(alternatePen);
  697. painter->drawLine(m_rcSpline.left(), p0.y(), p0.x(), p0.y());
  698. left = p0.x();
  699. }
  700. if (p1.x() < right && !pDetailSpline)
  701. {
  702. QPen alternatePen(splineColor, 1, Qt::DotLine);
  703. alternatePen.setCosmetic(true);
  704. painter->setPen(alternatePen);
  705. painter->drawLine(p1.x(), p1.y(), m_rcSpline.right() + 1, p1.y());
  706. right = p1.x();
  707. }
  708. painter->setPen(pen);
  709. float gradient = 0.0f;
  710. int pointsInLine = -1;
  711. QPoint lineStart;
  712. QPainterPath path;
  713. for (int x = left; x <= right; x++)
  714. {
  715. float time = XOfsToTime(x);
  716. ISplineInterpolator::ValueType value;
  717. ISplineInterpolator::ZeroValue(value);
  718. pSpline->Interpolate(time, value);
  719. if (pDetailSpline)
  720. {
  721. ISplineInterpolator::ValueType value2;
  722. ISplineInterpolator::ZeroValue(value2);
  723. pDetailSpline->Interpolate(time, value2);
  724. value[nCurrentDimension] = value[nCurrentDimension] + value2[nCurrentDimension];
  725. }
  726. QPoint pt = WorldToClient(Vec2(time, value[nCurrentDimension]));
  727. if ((x == right && pointsInLine >= 0) || (pointsInLine > 0 && fabs(lineStart.y() + gradient * (pt.x() - lineStart.x()) - pt.y()) > 1.0f))
  728. {
  729. lineStart = QPoint(pt.x() - 1, static_cast<int>(lineStart.y() + gradient * (pt.x() - 1 - lineStart.x())));
  730. path.lineTo(lineStart);
  731. gradient = float(pt.y() - lineStart.y()) / (pt.x() - lineStart.x());
  732. pointsInLine = 1;
  733. }
  734. else if ((x == right && pointsInLine >= 0) || (pointsInLine > 0 && fabs(lineStart.y() + gradient * (pt.x() - lineStart.x()) - pt.y()) == 1.0f))
  735. {
  736. lineStart = pt;
  737. path.lineTo(lineStart);
  738. gradient = 0.0f;
  739. pointsInLine = 0;
  740. }
  741. else if (pointsInLine > 0)
  742. {
  743. ++pointsInLine;
  744. }
  745. else if (pointsInLine == 0)
  746. {
  747. gradient = float(pt.y() - lineStart.y()) / (pt.x() - lineStart.x());
  748. ++pointsInLine;
  749. }
  750. else
  751. {
  752. path.moveTo(pt);
  753. lineStart = pt;
  754. ++pointsInLine;
  755. gradient = 0.0f;
  756. }
  757. }
  758. painter->drawPath(path);
  759. // Put back the old objects
  760. painter->setPen(pOldPen);
  761. }
  762. }
  763. //////////////////////////////////////////////////////////////////////////
  764. void SplineWidget::DrawKeys(QPainter* painter, int splineIndex, [[maybe_unused]] float startTime, float endTime)
  765. {
  766. SSplineInfo& splineInfo = m_splines[splineIndex];
  767. ISplineInterpolator* pSpline = splineInfo.pSpline;
  768. ISplineInterpolator* pDetailSpline = splineInfo.pDetailSpline;
  769. if (!pSpline)
  770. {
  771. return;
  772. }
  773. // create and select a white pen - nope, black
  774. const QPen pOldPen = painter->pen();
  775. painter->setPen(Qt::black);
  776. int i;
  777. int nTotalNumberOfDimensions(0);
  778. int nCurrentDimension(0);
  779. nTotalNumberOfDimensions = pSpline->GetNumDimensions();
  780. for (nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
  781. {
  782. // Why is this here? Not even god knows...
  783. //for (i = 0; i < pSpline->GetKeyCount() && pSpline->GetKeyTime(i) < startTime; ++i);
  784. QPoint lastKeyPt;
  785. int numKeys = pSpline->GetKeyCount();
  786. for (i = 0; i < numKeys; i++)
  787. {
  788. float time = pSpline->GetKeyTime(i);
  789. if (time >= endTime)
  790. {
  791. break;
  792. }
  793. ISplineInterpolator::ValueType value;
  794. ISplineInterpolator::ZeroValue(value);
  795. pSpline->Interpolate(time, value);
  796. if (pDetailSpline)
  797. {
  798. ISplineInterpolator::ValueType value2;
  799. ISplineInterpolator::ZeroValue(value2);
  800. pDetailSpline->Interpolate(time, value2);
  801. value[nCurrentDimension] = value[nCurrentDimension] + value2[nCurrentDimension];
  802. }
  803. QPoint pt = WorldToClient(Vec2(time, value[nCurrentDimension]));
  804. ;
  805. if (pt.x() < m_rcSpline.left())
  806. {
  807. continue;
  808. }
  809. if (i > 0 && (pt - lastKeyPt).manhattanLength() < 4)
  810. {
  811. continue;
  812. }
  813. QColor clr(220, 220, 0);
  814. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  815. {
  816. clr = Qt::red;
  817. DrawTangentHandle(painter, splineIndex, i, nCurrentDimension);
  818. }
  819. const QBrush brush(clr);
  820. const QBrush pOldBrush = painter->brush();
  821. painter->setBrush(brush);
  822. // Draw this key.
  823. painter->drawRect(QRect(QPoint(pt.x() - m_nKeyDrawRadius, pt.y() - m_nKeyDrawRadius), QPoint(pt.x() + m_nKeyDrawRadius - 1, pt.y() + m_nKeyDrawRadius - 1)));
  824. lastKeyPt = pt;
  825. painter->setBrush(pOldBrush);
  826. }
  827. }
  828. painter->setPen(pOldPen);
  829. }
  830. bool AbstractSplineWidget::GetTangentHandlePts(QPoint&, QPoint&, QPoint&, int, int, int)
  831. {
  832. return false;
  833. }
  834. void SplineWidget::DrawTangentHandle(QPainter* painter, int nSpline, int nKey, int nDimension)
  835. {
  836. // create and select a white pen
  837. const QPen pOldPen = painter->pen();
  838. painter->setPen(QColor(96, 96, 96));
  839. // Draw in-tangent & out-tangent lines.
  840. QPoint a, b, pt;
  841. if (GetTangentHandlePts(a, pt, b, nSpline, nKey, nDimension))
  842. {
  843. painter->drawLine(a, pt);
  844. painter->drawLine(pt, b);
  845. // Draw end-effectors.
  846. const QBrush pOldBrush = painter->brush();
  847. painter->setBrush(QColor(0, 220, 0));
  848. painter->drawRect(QRect(QPoint(a.x() - m_nKeyDrawRadius, a.y() - m_nKeyDrawRadius), QPoint(a.x() + m_nKeyDrawRadius - 1, a.y() + m_nKeyDrawRadius - 1)));
  849. painter->drawRect(QRect(QPoint(b.x() - m_nKeyDrawRadius, b.y() - m_nKeyDrawRadius), QPoint(b.x() + m_nKeyDrawRadius - 1, b.y() + m_nKeyDrawRadius - 1)));
  850. painter->setBrush(pOldBrush);
  851. }
  852. painter->setPen(pOldPen);
  853. }
  854. //////////////////////////////////////////////////////////////////////////
  855. void SplineWidget::DrawTimeMarker(QPainter* painter)
  856. {
  857. const QPen pOldPen = painter->pen();
  858. painter->setPen(QColor(255, 0, 255));
  859. float x = TimeToXOfs(m_fTimeMarker);
  860. if (x >= m_rcSpline.left() && x <= m_rcSpline.right() + 1)
  861. {
  862. painter->drawLine(static_cast<int>(x), m_rcSpline.top(), static_cast<int>(x), m_rcSpline.bottom() + 1);
  863. }
  864. painter->setPen(pOldPen);
  865. }
  866. /////////////////////////////////////////////////////////////////////////////
  867. //Mouse Message Handlers
  868. //////////////////////////////////////////////////////////////////////////
  869. void SplineWidget::mousePressEvent(QMouseEvent* event)
  870. {
  871. switch (event->button())
  872. {
  873. case Qt::LeftButton:
  874. OnLButtonDown(event->pos(), event->modifiers());
  875. break;
  876. case Qt::MiddleButton:
  877. OnMButtonDown(event->pos(), event->modifiers());
  878. break;
  879. case Qt::RightButton:
  880. OnRButtonDown(event->pos(), event->modifiers());
  881. break;
  882. }
  883. }
  884. void SplineWidget::mouseReleaseEvent(QMouseEvent* event)
  885. {
  886. switch (event->button())
  887. {
  888. case Qt::LeftButton:
  889. OnLButtonUp(event->pos(), event->modifiers());
  890. break;
  891. case Qt::MiddleButton:
  892. OnMButtonUp(event->pos(), event->modifiers());
  893. break;
  894. }
  895. }
  896. void SplineWidget::OnLButtonDown(const QPoint& point, Qt::KeyboardModifiers modifiers)
  897. {
  898. m_pCurrentUndo = nullptr;
  899. if (m_bEditLock)
  900. {
  901. return;
  902. }
  903. m_boLeftMouseButtonDown = true;
  904. if (m_editMode == TrackingMode)
  905. {
  906. return;
  907. }
  908. SendNotifyEvent(NM_CLICK);
  909. m_cMouseDownPos = point;
  910. ISplineInterpolator* pSpline = HitSpline(m_cMouseDownPos);
  911. // Get control key status.
  912. bool bCtrlClick = modifiers & Qt::ControlModifier;
  913. switch (m_hitCode)
  914. {
  915. case HIT_KEY:
  916. {
  917. {
  918. CUndo undo("Select Spline Key");
  919. StoreUndo();
  920. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  921. bool bHitSelection = IsKeySelected(m_pHitSpline, m_nHitKeyIndex, m_nHitDimension);
  922. bool bAddSelect = bCtrlClick;
  923. if (!bAddSelect && !bHitSelection)
  924. {
  925. ClearSelection();
  926. }
  927. SelectKey(pSpline, m_nHitKeyIndex, m_nHitDimension, true);
  928. SendNotifyEvent(SPLN_CHANGE);
  929. if (m_pCurrentUndo && !m_pCurrentUndo->IsSelectionChanged())
  930. {
  931. undo.Cancel();
  932. }
  933. m_pCurrentUndo = nullptr;
  934. }
  935. GetIEditor()->BeginUndo();
  936. StartTracking(bCtrlClick);
  937. }
  938. break;
  939. case HIT_TANGENT_HANDLE:
  940. {
  941. {
  942. CUndo undo("Select Tangent Handle");
  943. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  944. ClearSelection();
  945. SelectKey(pSpline, m_nHitKeyIndex, m_nHitDimension, true);
  946. SendNotifyEvent(SPLN_CHANGE);
  947. }
  948. StartTracking(false);
  949. }
  950. break;
  951. case HIT_SPLINE:
  952. {
  953. if (GetNumSelected() > 0)
  954. {
  955. StartTracking(bCtrlClick);
  956. }
  957. }
  958. break;
  959. case HIT_TIMEMARKER:
  960. {
  961. SendNotifyEvent(SPLN_TIME_START_CHANGE);
  962. m_editMode = TimeMarkerMode;
  963. grabMouse();
  964. }
  965. break;
  966. case HIT_NOTHING:
  967. {
  968. if (m_rcSpline.contains(point))
  969. {
  970. GetIEditor()->BeginUndo();
  971. StoreUndo();
  972. m_rcSelect = QRect();
  973. m_rubberBand->setVisible(false);
  974. m_editMode = SelectMode;
  975. grabMouse();
  976. }
  977. }
  978. break;
  979. }
  980. update();
  981. }
  982. //////////////////////////////////////////////////////////////////////////
  983. void SplineWidget::OnRButtonDown(const QPoint&, Qt::KeyboardModifiers)
  984. {
  985. m_pCurrentUndo = nullptr;
  986. SendNotifyEvent(NM_RCLICK);
  987. }
  988. //////////////////////////////////////////////////////////////////////////
  989. void SplineWidget::OnMButtonDown(const QPoint& point, Qt::KeyboardModifiers modifiers)
  990. {
  991. m_pCurrentUndo = nullptr;
  992. bool bShiftClick = modifiers & Qt::ShiftModifier;
  993. if (m_editMode == NothingMode)
  994. {
  995. if (bShiftClick)
  996. {
  997. m_editMode = ZoomMode;
  998. setCursor(Qt::SizeAllCursor);
  999. }
  1000. else
  1001. {
  1002. setCursor(Qt::SizeAllCursor);
  1003. m_editMode = ScrollMode;
  1004. }
  1005. m_cMouseDownPos = point;
  1006. }
  1007. }
  1008. //////////////////////////////////////////////////////////////////////////
  1009. void SplineWidget::OnMButtonUp(const QPoint&, Qt::KeyboardModifiers)
  1010. {
  1011. if (m_editMode == ScrollMode || m_editMode == ZoomMode)
  1012. {
  1013. m_editMode = NothingMode;
  1014. return;
  1015. }
  1016. }
  1017. //////////////////////////////////////////////////////////////////////////
  1018. void SplineWidget::mouseDoubleClickEvent(QMouseEvent* event)
  1019. {
  1020. if (event->button() != Qt::LeftButton)
  1021. {
  1022. return;
  1023. }
  1024. const QPoint point = event->pos();
  1025. m_pCurrentUndo = nullptr;
  1026. if (m_bEditLock)
  1027. {
  1028. return;
  1029. }
  1030. switch (m_hitCode)
  1031. {
  1032. case HIT_SPLINE:
  1033. {
  1034. if (m_pHitSpline)
  1035. {
  1036. InsertKey(m_pHitSpline, m_pHitDetailSpline, point);
  1037. }
  1038. update();
  1039. if (m_pTimelineCtrl)
  1040. {
  1041. m_pTimelineCtrl->update();
  1042. }
  1043. }
  1044. break;
  1045. case HIT_KEY:
  1046. {
  1047. RemoveKey(m_pHitSpline, m_nHitKeyIndex);
  1048. }
  1049. break;
  1050. case HIT_TANGENT_HANDLE:
  1051. {
  1052. if (m_bHitIncomingHandle)
  1053. {
  1054. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, 0);
  1055. }
  1056. else
  1057. {
  1058. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, 0);
  1059. }
  1060. }
  1061. break;
  1062. }
  1063. }
  1064. //////////////////////////////////////////////////////////////////////////
  1065. void SplineWidget::mouseMoveEvent(QMouseEvent* event)
  1066. {
  1067. const QPoint point = event->pos();
  1068. switch (HitTest(point))
  1069. {
  1070. case HIT_SPLINE:
  1071. setCursor(CMFCUtils::LoadCursor(IDC_ARRWHITE));
  1072. break;
  1073. case HIT_KEY:
  1074. case HIT_TANGENT_HANDLE:
  1075. setCursor(CMFCUtils::LoadCursor(IDC_ARRBLCK));
  1076. break;
  1077. default:
  1078. setCursor(Qt::ArrowCursor);
  1079. break;
  1080. }
  1081. if (m_pHitSpline && m_nHitKeyIndex >= 0)
  1082. {
  1083. float time = m_pHitSpline->GetKeyTime(m_nHitKeyIndex);
  1084. ISplineInterpolator::ValueType afValue;
  1085. m_pHitSpline->GetKeyValue(m_nHitKeyIndex, afValue);
  1086. const QString tipText = QString::fromLatin1("t=%1 v=%2").arg(time * m_fTooltipScaleX, 0, 'f', 3).arg(afValue[m_nHitDimension] * m_fTooltipScaleY, 2, 'f', 3);
  1087. m_tooltipText = tipText;
  1088. if (m_lastToolTipPos != point)
  1089. {
  1090. m_lastToolTipPos = point;
  1091. QToolTip::showText(event->globalPos(), tipText);
  1092. }
  1093. }
  1094. else if (m_editMode != TrackingMode)
  1095. {
  1096. if (!m_tooltipText.isEmpty())
  1097. {
  1098. QToolTip::hideText();
  1099. }
  1100. }
  1101. if (m_bEditLock)
  1102. {
  1103. return;
  1104. }
  1105. m_cMousePos = event->pos();
  1106. if (m_editMode == SelectMode)
  1107. {
  1108. setCursor(Qt::BlankCursor);
  1109. QRect rc(QPoint(m_cMouseDownPos.x(), m_cMouseDownPos.y()), point - QPoint(1, 1));
  1110. rc = rc.normalized().intersected(m_rcSpline);
  1111. m_rcSelect = rc;
  1112. m_rubberBand->setGeometry(m_rcSelect);
  1113. m_rubberBand->setVisible(true);
  1114. // Trigger a repaint so that the spline lines will be drawn correctly,
  1115. // or else they end up being chopped up if you drag the selection area
  1116. // over them
  1117. update();
  1118. }
  1119. if (m_editMode == TimeMarkerMode)
  1120. {
  1121. setCursor(Qt::BlankCursor);
  1122. SetTimeMarker(XOfsToTime(event->x()));
  1123. SendNotifyEvent(SPLN_TIME_CHANGE);
  1124. }
  1125. if (m_boLeftMouseButtonDown)
  1126. {
  1127. if (m_editMode == TrackingMode && event->pos() != m_cMouseDownPos)
  1128. {
  1129. m_startedDragging = true;
  1130. GetIEditor()->RestoreUndo();
  1131. m_pCurrentUndo = nullptr;
  1132. StoreUndo();
  1133. bool bAltClick = event->modifiers() & Qt::AltModifier;
  1134. Vec2 v0 = ClientToWorld(m_cMouseDownPos);
  1135. Vec2 v1 = ClientToWorld(event->pos());
  1136. if (bAltClick)
  1137. {
  1138. TimeScaleKeys(m_fTimeMarker, v0.x, v1.x);
  1139. }
  1140. else if (m_controlAmplitude)
  1141. {
  1142. ScaleAmplitudeKeys(v0.x, v0.y, v1.y - v0.y);
  1143. }
  1144. else
  1145. {
  1146. MoveSelectedKeys(v1 - v0, m_copyKeys);
  1147. }
  1148. }
  1149. }
  1150. switch (m_editMode)
  1151. {
  1152. case ScrollMode:
  1153. {
  1154. // Set the new scrolled coordinates
  1155. float ofsx = m_grid.origin.GetX() - (event->x() - m_cMouseDownPos.x()) / m_grid.zoom.GetX();
  1156. float ofsy = m_grid.origin.GetY() + (event->y() - m_cMouseDownPos.y()) / m_grid.zoom.GetY();
  1157. SetScrollOffset(Vec2(ofsx, ofsy));
  1158. m_cMouseDownPos = event->pos();
  1159. }
  1160. break;
  1161. case ZoomMode:
  1162. {
  1163. float ofsx = (event->x() - m_cMouseDownPos.x()) * 0.01f;
  1164. float ofsy = (event->y() - m_cMouseDownPos.y()) * 0.01f;
  1165. AZ::Vector2 z = m_grid.zoom;
  1166. if (ofsx != 0)
  1167. {
  1168. z.SetX(max(z.GetX() * (1.0f + ofsx), 0.001f));
  1169. }
  1170. if (ofsy != 0)
  1171. {
  1172. z.SetY(max(z.GetY() * (1.0f + ofsy), 0.001f));
  1173. }
  1174. SetZoom(Vec2(z.GetX(), z.GetY()), m_cMouseDownPos);
  1175. m_cMouseDownPos = event->pos();
  1176. }
  1177. break;
  1178. }
  1179. }
  1180. //////////////////////////////////////////////////////////////////////////
  1181. void AbstractSplineWidget::UpdateKeyTimes() const
  1182. {
  1183. if (!m_bKeyTimesDirty)
  1184. {
  1185. return;
  1186. }
  1187. std::vector<float> selectedKeyTimes;
  1188. selectedKeyTimes.reserve(m_keyTimes.size());
  1189. for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
  1190. {
  1191. if ((*it).selected)
  1192. {
  1193. selectedKeyTimes.push_back((*it).time);
  1194. }
  1195. }
  1196. std::sort(selectedKeyTimes.begin(), selectedKeyTimes.end());
  1197. m_keyTimes.clear();
  1198. for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
  1199. {
  1200. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1201. for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
  1202. {
  1203. float value = pSpline->GetKeyTime(keyIndex);
  1204. int lower = 0;
  1205. int upper = int(m_keyTimes.size());
  1206. while (lower < upper - 1)
  1207. {
  1208. int mid = ((lower + upper) >> 1);
  1209. ((m_keyTimes[mid].time >= value) ? upper : lower) = mid;
  1210. }
  1211. if ((lower >= int(m_keyTimes.size()) || fabsf(m_keyTimes[lower].time - value) > threshold) &&
  1212. (upper >= int(m_keyTimes.size()) || fabsf(m_keyTimes[upper].time - value) > threshold))
  1213. {
  1214. m_keyTimes.insert(m_keyTimes.begin() + upper, KeyTime(value, 0));
  1215. }
  1216. }
  1217. }
  1218. for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
  1219. {
  1220. (*it).count = (m_pSplineSet ? m_pSplineSet->GetKeyCountAtTime((*it).time, threshold) : 0);
  1221. }
  1222. std::vector<float>::iterator itSelected = selectedKeyTimes.begin(), endSelected = selectedKeyTimes.end();
  1223. for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
  1224. {
  1225. const float thisThreshold = 0.01f;
  1226. for (; itSelected != endSelected && (*itSelected) < (*it).time - thisThreshold; ++itSelected)
  1227. {
  1228. ;
  1229. }
  1230. if (itSelected != endSelected && fabsf((*itSelected) - (*it).time) < thisThreshold)
  1231. {
  1232. (*it).selected = true;
  1233. }
  1234. }
  1235. m_totalSplineCount = (m_pSplineSet ? m_pSplineSet->GetSplineCount() : 0);
  1236. m_bKeyTimesDirty = false;
  1237. }
  1238. //////////////////////////////////////////////////////////////////////////
  1239. void SplineWidget::OnLButtonUp([[maybe_unused]] const QPoint& point, Qt::KeyboardModifiers modifiers)
  1240. {
  1241. if (m_bEditLock)
  1242. {
  1243. return;
  1244. }
  1245. m_boLeftMouseButtonDown = false;
  1246. if (m_editMode == TrackingMode)
  1247. {
  1248. StopTracking();
  1249. if (!m_startedDragging)
  1250. {
  1251. HitSpline(m_cMouseDownPos);
  1252. }
  1253. }
  1254. if (m_editMode == SelectMode)
  1255. {
  1256. // Get control key status.
  1257. bool bAltClick = modifiers & Qt::AltModifier;
  1258. bool bCtrlClick = modifiers & Qt::ControlModifier;
  1259. bool bAddSelect = bCtrlClick;
  1260. bool bUnselect = bAltClick;
  1261. // Stop tracking first or else the clear selection will end up being
  1262. // undone
  1263. StopTracking();
  1264. if (!bAddSelect && !bUnselect)
  1265. {
  1266. ClearSelection();
  1267. }
  1268. SelectRectangle(m_rcSelect, !bUnselect);
  1269. m_rcSelect = QRect();
  1270. m_rubberBand->setVisible(false);
  1271. setCursor(Qt::ArrowCursor);
  1272. }
  1273. if (m_editMode == TimeMarkerMode)
  1274. {
  1275. m_editMode = NothingMode;
  1276. releaseMouse();
  1277. SendNotifyEvent(SPLN_TIME_END_CHANGE);
  1278. }
  1279. if (m_pTimelineCtrl)
  1280. {
  1281. m_pTimelineCtrl->update();
  1282. }
  1283. m_tooltipText = "";
  1284. update();
  1285. m_editMode = NothingMode;
  1286. }
  1287. /////////////////////////////////////////////////////////////////////////////
  1288. void AbstractSplineWidget::SelectKey(ISplineInterpolator* pSpline, int nKey, int nDimension, bool bSelect)
  1289. {
  1290. if (nKey >= 0)
  1291. {
  1292. pSpline->SelectKeyAtDimension(nKey, nDimension, bSelect);
  1293. SendNotifyEvent(SPLN_KEY_SELECTION_CHANGE);
  1294. }
  1295. }
  1296. //////////////////////////////////////////////////////////////////////////
  1297. bool AbstractSplineWidget::IsKeySelected(ISplineInterpolator* pSpline, int nKey, int nHitDimension) const
  1298. {
  1299. if (pSpline && nKey >= 0)
  1300. {
  1301. return (pSpline->IsKeySelectedAtDimension(nKey, nHitDimension));
  1302. }
  1303. return false;
  1304. }
  1305. //////////////////////////////////////////////////////////////////////////
  1306. int AbstractSplineWidget::GetNumSelected()
  1307. {
  1308. int nSelected = 0;
  1309. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1310. {
  1311. if (ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline)
  1312. {
  1313. for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
  1314. {
  1315. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  1316. {
  1317. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  1318. {
  1319. nSelected++;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. return nSelected;
  1326. }
  1327. /////////////////////////////////////////////////////////////////////////////
  1328. void AbstractSplineWidget::SetSplineSet(ISplineSet* pSplineSet)
  1329. {
  1330. m_pSplineSet = pSplineSet;
  1331. }
  1332. //////////////////////////////////////////////////////////////////////////
  1333. void SplineWidget::wheelEvent(QWheelEvent* event)
  1334. {
  1335. int zDelta = event->angleDelta().y();
  1336. if (zDelta == 0)
  1337. {
  1338. return;
  1339. }
  1340. AZ::Vector2 z = m_grid.zoom;
  1341. float scale = 1.2f * fabs(zDelta / 120.0f);
  1342. if (zDelta > 0)
  1343. {
  1344. z *= scale;
  1345. }
  1346. else
  1347. {
  1348. z /= scale;
  1349. }
  1350. SetZoom(Vec2(z.GetX(), z.GetY()), m_cMousePos);
  1351. event->accept();
  1352. }
  1353. void SplineWidget::keyPressEvent(QKeyEvent* e)
  1354. {
  1355. bool bProcessed = false;
  1356. switch (e->key())
  1357. {
  1358. case Qt::Key_Delete:
  1359. {
  1360. RemoveSelectedKeys();
  1361. RemoveSelectedKeyTimes();
  1362. bProcessed = true;
  1363. }
  1364. break;
  1365. case Qt::Key_Up:
  1366. {
  1367. CUndo undo("Move Spline Key(s)");
  1368. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1369. MoveSelectedKeys(ClientToWorld(QPoint(0, -1)), false);
  1370. bProcessed = true;
  1371. }
  1372. break;
  1373. case Qt::Key_Down:
  1374. {
  1375. CUndo undo("Move Spline Key(s)");
  1376. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1377. MoveSelectedKeys(ClientToWorld(QPoint(0, 1)), false);
  1378. bProcessed = true;
  1379. }
  1380. break;
  1381. case Qt::Key_Left:
  1382. {
  1383. CUndo undo("Move Spline Key(s)");
  1384. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1385. MoveSelectedKeys(ClientToWorld(QPoint(-1, 0)), false);
  1386. bProcessed = true;
  1387. }
  1388. break;
  1389. case Qt::Key_Right:
  1390. {
  1391. CUndo undo("Move Spline Key(s)");
  1392. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1393. MoveSelectedKeys(ClientToWorld(QPoint(1, 0)), false);
  1394. bProcessed = true;
  1395. }
  1396. break;
  1397. default:
  1398. break; //do nothing
  1399. }
  1400. bool bCtrl = e->modifiers() & Qt::ControlModifier;
  1401. if (e->key() == Qt::Key_C && bCtrl)
  1402. {
  1403. CopyKeys();
  1404. return;
  1405. }
  1406. if (e->key() == Qt::Key_V && bCtrl)
  1407. {
  1408. PasteKeys();
  1409. return;
  1410. }
  1411. if (e->key() == Qt::Key_Z && bCtrl)
  1412. {
  1413. GetIEditor()->Undo();
  1414. return;
  1415. }
  1416. if (e->key() == Qt::Key_Y && bCtrl)
  1417. {
  1418. GetIEditor()->Redo();
  1419. return;
  1420. }
  1421. if (!bProcessed)
  1422. {
  1423. QWidget::keyPressEvent(e);
  1424. }
  1425. }
  1426. bool SplineWidget::event(QEvent* e)
  1427. {
  1428. // Shortcut override events are sent to allow a widget to say that it wants to get the matching
  1429. // keypress event, and for it not to be treated as a shortcut by the shortcut/QAction system.
  1430. // So we need to handle it here, return true and mark the event as accepted, if it's a key combination we care
  1431. // about
  1432. if (e->type() == QEvent::ShortcutOverride)
  1433. {
  1434. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
  1435. bool filterOutEvent = false;
  1436. switch (keyEvent->key())
  1437. {
  1438. case Qt::Key_Delete:
  1439. case Qt::Key_Up:
  1440. case Qt::Key_Down:
  1441. case Qt::Key_Left:
  1442. case Qt::Key_Right:
  1443. filterOutEvent = true;
  1444. break;
  1445. case Qt::Key_C:
  1446. case Qt::Key_V:
  1447. case Qt::Key_Z:
  1448. case Qt::Key_Y:
  1449. filterOutEvent = (keyEvent->modifiers() & Qt::ControlModifier) != 0;
  1450. break;
  1451. default:
  1452. break; //do nothing
  1453. }
  1454. if (filterOutEvent)
  1455. {
  1456. e->accept();
  1457. return true;
  1458. }
  1459. }
  1460. return QWidget::event(e);
  1461. }
  1462. //////////////////////////////////////////////////////////////////////////
  1463. void AbstractSplineWidget::SetHorizontalExtent([[maybe_unused]] int min, [[maybe_unused]] int max)
  1464. {
  1465. /*
  1466. m_scrollMin.x = min;
  1467. m_scrollMax.x = max;
  1468. int width = max - min;
  1469. int nPage = m_rcClient.Width()/2;
  1470. int sx = width - nPage + m_rcSpline.left;
  1471. SCROLLINFO si;
  1472. ZeroStruct(si);
  1473. si.cbSize = sizeof(si);
  1474. si.fMask = SIF_ALL;
  1475. si.nMin = m_scrollMin.x;
  1476. si.nMax = m_scrollMax.x - nPage + m_rcSpline.left;
  1477. si.nPage = m_rcClient.Width()/2;
  1478. si.nPos = m_scrollOffset.x;
  1479. //si.nPage = max(0,m_rcClient.Width() - m_leftOffset*2);
  1480. //si.nPage = 1;
  1481. //si.nPage = 1;
  1482. SetScrollInfo( SB_HORZ,&si,true );
  1483. */
  1484. }
  1485. //////////////////////////////////////////////////////////////////////////
  1486. ISplineInterpolator* AbstractSplineWidget::HitSpline(const QPoint& point)
  1487. {
  1488. if (HitTest(point) != HIT_NOTHING)
  1489. {
  1490. return m_pHitSpline;
  1491. }
  1492. return nullptr;
  1493. }
  1494. //////////////////////////////////////////////////////////////////////////////
  1495. AbstractSplineWidget::EHitCode AbstractSplineWidget::HitTest(const QPoint& point)
  1496. {
  1497. float time, val;
  1498. int nTotalNumberOfDimensions(0);
  1499. int nCurrentDimension(0);
  1500. PointToTimeValue(point, time, val);
  1501. m_hitCode = HIT_NOTHING;
  1502. m_pHitSpline = nullptr;
  1503. m_pHitDetailSpline = nullptr;
  1504. m_nHitKeyIndex = -1;
  1505. m_nHitDimension = -1;
  1506. m_bHitIncomingHandle = true;
  1507. if (abs(point.x() - TimeToXOfs(m_fTimeMarker)) < 4)
  1508. {
  1509. m_hitCode = HIT_TIMEMARKER;
  1510. }
  1511. // For each Spline...
  1512. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1513. {
  1514. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1515. ISplineInterpolator* pDetailSpline = m_splines[splineIndex].pDetailSpline;
  1516. // If there is no spline, you can't hit nor a spline nor a key... isn't that logical?
  1517. if (!pSpline)
  1518. {
  1519. return m_hitCode;
  1520. }
  1521. ISplineInterpolator::ValueType stSplineValue;
  1522. ISplineInterpolator::ValueType stDetailSplineValue;
  1523. ISplineInterpolator::ZeroValue(stSplineValue);
  1524. ISplineInterpolator::ZeroValue(stDetailSplineValue);
  1525. pSpline->Interpolate(time, stSplineValue);
  1526. if (pDetailSpline)
  1527. {
  1528. pDetailSpline->Interpolate(time, stDetailSplineValue);
  1529. }
  1530. // For each dimension...
  1531. nTotalNumberOfDimensions = pSpline->GetNumDimensions();
  1532. for (nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
  1533. {
  1534. if (pDetailSpline)
  1535. {
  1536. stSplineValue[nCurrentDimension] = stSplineValue[nCurrentDimension] + stDetailSplineValue[nCurrentDimension];
  1537. }
  1538. for (int i = 0; i < pSpline->GetKeyCount(); i++)
  1539. {
  1540. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  1541. // Check tangent handles first.
  1542. {
  1543. QPoint incomingHandlePt, outgoingHandlePt, pt;
  1544. if (GetTangentHandlePts(incomingHandlePt, pt, outgoingHandlePt, static_cast<int>(splineIndex), static_cast<int>(i), nCurrentDimension))
  1545. {
  1546. // For the incoming handle
  1547. if (abs(incomingHandlePt.x() - point.x()) < 4 && abs(incomingHandlePt.y() - point.y()) < 4)
  1548. {
  1549. m_hitCode = HIT_TANGENT_HANDLE;
  1550. m_pHitSpline = pSpline;
  1551. m_pHitDetailSpline = m_splines[splineIndex].pDetailSpline;
  1552. m_nHitKeyIndex = i;
  1553. m_nHitDimension = nCurrentDimension;
  1554. m_bHitIncomingHandle = true;
  1555. return m_hitCode;
  1556. }
  1557. // For the outgoing handle
  1558. else if (abs(outgoingHandlePt.x() - point.x()) < 4 && abs(outgoingHandlePt.y() - point.y()) < 4)
  1559. {
  1560. m_hitCode = HIT_TANGENT_HANDLE;
  1561. m_pHitSpline = pSpline;
  1562. m_pHitDetailSpline = m_splines[splineIndex].pDetailSpline;
  1563. m_nHitKeyIndex = i;
  1564. m_nHitDimension = nCurrentDimension;
  1565. m_bHitIncomingHandle = false;
  1566. return m_hitCode;
  1567. }
  1568. }
  1569. }
  1570. }
  1571. QPoint splinePt = WorldToClient(Vec2(time, stSplineValue[nCurrentDimension]));
  1572. bool bSplineHit = abs(splinePt.x() - point.x()) < 4 && abs(splinePt.y() - point.y()) < 4;
  1573. if (bSplineHit)
  1574. {
  1575. m_hitCode = HIT_SPLINE;
  1576. m_pHitSpline = pSpline;
  1577. m_pHitDetailSpline = m_splines[splineIndex].pDetailSpline;
  1578. for (int i = 0; i < pSpline->GetKeyCount(); i++)
  1579. {
  1580. QPoint splinePt2 = TimeToPoint(pSpline->GetKeyTime(i), pSpline);
  1581. if (abs(splinePt2.x() - point.x()) < 4 /* && abs(splinePt.y()-point.y()) < 4*/)
  1582. {
  1583. m_nHitKeyIndex = i;
  1584. m_nHitDimension = nCurrentDimension;
  1585. m_hitCode = HIT_KEY;
  1586. return m_hitCode;
  1587. }
  1588. }
  1589. }
  1590. }
  1591. }
  1592. return m_hitCode;
  1593. }
  1594. ///////////////////////////////////////////////////////////////////////////////
  1595. void AbstractSplineWidget::StartTracking(bool copyKeys)
  1596. {
  1597. m_copyKeys = copyKeys;
  1598. m_startedDragging = false;
  1599. m_editMode = TrackingMode;
  1600. captureMouseImpl();
  1601. GetIEditor()->BeginUndo();
  1602. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1603. setCursorImpl(IDC_ARRBLCKCROSS);
  1604. }
  1605. //////////////////////////////////////////////////////////////////////////
  1606. void AbstractSplineWidget::StopTracking()
  1607. {
  1608. if ((m_editMode == TrackingMode) && (m_cMousePos != m_cMouseDownPos))
  1609. {
  1610. GetIEditor()->AcceptUndo("Spline Move");
  1611. }
  1612. else if (m_pCurrentUndo && (m_cMousePos == m_cMouseDownPos))
  1613. {
  1614. if (m_editMode == SelectMode)
  1615. {
  1616. if (m_pCurrentUndo->IsSelectionChanged())
  1617. {
  1618. GetIEditor()->AcceptUndo("Key Selection");
  1619. }
  1620. else
  1621. {
  1622. GetIEditor()->CancelUndo();
  1623. }
  1624. }
  1625. else if ((m_editMode == TrackingMode))
  1626. {
  1627. GetIEditor()->CancelUndo();
  1628. }
  1629. }
  1630. else
  1631. {
  1632. GetIEditor()->CancelUndo();
  1633. }
  1634. // Undo has been accepted or cancelled, so clear the m_pCurrentUndo pointer.
  1635. m_pCurrentUndo = nullptr;
  1636. m_editMode = NothingMode;
  1637. releaseMouseImpl();
  1638. update();
  1639. }
  1640. //////////////////////////////////////////////////////////////////////////
  1641. void AbstractSplineWidget::ScaleAmplitudeKeys(float time, float startValue, float offset)
  1642. {
  1643. //TODO: Test it in the facial animation pane and fix it...
  1644. m_pHitSpline = nullptr;
  1645. m_pHitDetailSpline = nullptr;
  1646. m_nHitKeyIndex = -1;
  1647. m_nHitDimension = -1;
  1648. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1649. {
  1650. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1651. // Find the range of keys to process.
  1652. int keyCount = (pSpline ? pSpline->GetKeyCount() : 0);
  1653. int firstKeyIndex = keyCount;
  1654. int lastKeyIndex = -1;
  1655. for (int i = 0; i < keyCount; ++i)
  1656. {
  1657. if (pSpline->IsKeySelectedAtAnyDimension(i))
  1658. {
  1659. firstKeyIndex = min(firstKeyIndex, i);
  1660. lastKeyIndex = max(lastKeyIndex, i);
  1661. }
  1662. }
  1663. // Find the parameters of a line between the start and end points. This will form the centre line
  1664. // around which the amplitude of the keys will be scaled.
  1665. float rangeStartTime = (firstKeyIndex >= 0 && pSpline ? pSpline->GetKeyTime(firstKeyIndex) : 0.0f);
  1666. float rangeEndTime = (lastKeyIndex >= 0 && pSpline ? pSpline->GetKeyTime(lastKeyIndex) : 0.0f);
  1667. float rangeLength = max(0.01f, rangeEndTime - rangeStartTime);
  1668. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  1669. {
  1670. ISplineInterpolator::ValueType afRangeStartValue;
  1671. if (firstKeyIndex >= 0 && pSpline)
  1672. {
  1673. pSpline->GetKeyValue(firstKeyIndex, afRangeStartValue);
  1674. }
  1675. else
  1676. {
  1677. memset(afRangeStartValue, 0, sizeof(ISplineInterpolator::ValueType));
  1678. }
  1679. ISplineInterpolator::ValueType afRangeEndValue;
  1680. if (lastKeyIndex >= 0 && pSpline)
  1681. {
  1682. pSpline->GetKeyValue(lastKeyIndex, afRangeEndValue);
  1683. }
  1684. else
  1685. {
  1686. memset(afRangeEndValue, 0, sizeof(ISplineInterpolator::ValueType));
  1687. }
  1688. float centreM = (afRangeEndValue[nCurrentDimension] - afRangeStartValue[nCurrentDimension]) / rangeLength;
  1689. float centreC = afRangeStartValue[nCurrentDimension] - centreM * rangeStartTime;
  1690. // Calculate the scale factor, based on how the mouse was dragged.
  1691. float dragCentreValue = centreM * time + centreC;
  1692. float dragCentreOffset = startValue - dragCentreValue;
  1693. float offsetScale = (fabs(dragCentreOffset) > 0.001 ? (offset + dragCentreOffset) / dragCentreOffset : 1.0f);
  1694. // Scale all the selected keys around this central line.
  1695. for (int i = 0; i < keyCount; ++i)
  1696. {
  1697. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  1698. {
  1699. float keyTime = (pSpline ? pSpline->GetKeyTime(i) : 0.0f);
  1700. float centreValue = keyTime * centreM + centreC;
  1701. ISplineInterpolator::ValueType afKeyValue;
  1702. if (pSpline)
  1703. {
  1704. pSpline->GetKeyValue(i, afKeyValue);
  1705. }
  1706. else
  1707. {
  1708. memset(afKeyValue, 0, sizeof(ISplineInterpolator::ValueType));
  1709. }
  1710. float keyOffset = afKeyValue[nCurrentDimension] - centreValue;
  1711. float newKeyOffset = keyOffset * offsetScale;
  1712. if (pSpline)
  1713. {
  1714. afKeyValue[nCurrentDimension] = centreValue + newKeyOffset;
  1715. pSpline->SetKeyValue(i, afKeyValue);
  1716. }
  1717. }
  1718. }
  1719. }
  1720. }
  1721. update();
  1722. if (m_pTimelineCtrl)
  1723. {
  1724. m_pTimelineCtrl->update();
  1725. }
  1726. SendNotifyEvent(SPLN_CHANGE);
  1727. }
  1728. //////////////////////////////////////////////////////////////////////////
  1729. void AbstractSplineWidget::TimeScaleKeys(float time, float startTime, float endTime)
  1730. {
  1731. // Calculate the scaling parameters (ie t1 = t0 * M + C).
  1732. float timeScaleM = 1.0f;
  1733. if (fabsf(startTime - time) > MIN_TIME_EPSILON_FOR_SCALING)
  1734. {
  1735. timeScaleM = (endTime - time) / (startTime - time);
  1736. }
  1737. float timeScaleC = endTime - startTime * timeScaleM;
  1738. // Loop through all keys that are selected.
  1739. m_pHitSpline = nullptr;
  1740. m_pHitDetailSpline = nullptr;
  1741. m_nHitKeyIndex = -1;
  1742. float affectedRangeMin = FLT_MAX;
  1743. float affectedRangeMax = -FLT_MAX;
  1744. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1745. {
  1746. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1747. int keyCount = pSpline->GetKeyCount();
  1748. float keyRangeMin = FLT_MAX;
  1749. float keyRangeMax = -FLT_MAX;
  1750. for (int i = 0; i < keyCount; i++)
  1751. {
  1752. if (pSpline->IsKeySelectedAtAnyDimension(i))
  1753. {
  1754. float oldTime = pSpline->GetKeyTime(i);
  1755. float t = SnapTime(oldTime * timeScaleM + timeScaleC);
  1756. pSpline->SetKeyTime(i, SnapTimeToGridVertical(t));
  1757. keyRangeMin = min(keyRangeMin, oldTime);
  1758. keyRangeMin = min(keyRangeMin, t);
  1759. keyRangeMax = max(keyRangeMax, oldTime);
  1760. keyRangeMax = max(keyRangeMax, t);
  1761. }
  1762. }
  1763. if (keyRangeMin <= keyRangeMax)
  1764. {
  1765. // Changes to a key's value affect spline up to two keys away.
  1766. int lastMovedKey = 0;
  1767. for (int keyIndex = 0; keyIndex < keyCount; ++keyIndex)
  1768. {
  1769. if (pSpline->GetKeyTime(keyIndex) <= keyRangeMax)
  1770. {
  1771. lastMovedKey = keyIndex + 1;
  1772. }
  1773. }
  1774. int firstMovedKey = pSpline->GetKeyCount();
  1775. for (int keyIndex = pSpline->GetKeyCount() - 1; keyIndex >= 0; --keyIndex)
  1776. {
  1777. if (pSpline->GetKeyTime(keyIndex) >= keyRangeMin)
  1778. {
  1779. firstMovedKey = keyIndex;
  1780. }
  1781. }
  1782. int firstAffectedKey = max(0, firstMovedKey - 2);
  1783. int lastAffectedKey = min(keyCount - 1, lastMovedKey + 2);
  1784. affectedRangeMin = min(affectedRangeMin, (firstAffectedKey <= 0 ? m_timeRange.start : pSpline->GetKeyTime(firstAffectedKey)));
  1785. affectedRangeMax = max(affectedRangeMax, (lastAffectedKey >= keyCount - 1 ? m_timeRange.end : pSpline->GetKeyTime(lastAffectedKey)));
  1786. // Loop through all moved keys, checking whether there are multiple keys on the same frame.
  1787. float lastKeyTime = -FLT_MAX;
  1788. pSpline->Update();
  1789. for (int keyIndex = 0, keys = pSpline->GetKeyCount(); keyIndex <= keys; )
  1790. {
  1791. float keyTime = pSpline->GetKeyTime(keyIndex);
  1792. if (fabsf(keyTime - lastKeyTime) < m_fMinTimeEpsilon)
  1793. {
  1794. --keys;
  1795. pSpline->RemoveKey(keyIndex);
  1796. }
  1797. else
  1798. {
  1799. ++keyIndex;
  1800. lastKeyTime = keyTime;
  1801. }
  1802. }
  1803. }
  1804. }
  1805. int rangeMin = static_cast<int>(TimeToXOfs(affectedRangeMin));
  1806. int rangeMax = static_cast<int>(TimeToXOfs(affectedRangeMax));
  1807. if (m_timeRange.start == affectedRangeMin)
  1808. {
  1809. rangeMin = m_rcSpline.left();
  1810. }
  1811. if (m_timeRange.end == affectedRangeMax)
  1812. {
  1813. rangeMax = m_rcSpline.right();
  1814. }
  1815. QRect invalidRect(QPoint(rangeMin - 3, m_rcSpline.top()), QPoint(rangeMax + 3, m_rcSpline.bottom() + 1) - QPoint(1, 1));
  1816. update(invalidRect);
  1817. if (m_pTimelineCtrl)
  1818. {
  1819. m_pTimelineCtrl->update();
  1820. }
  1821. m_bKeyTimesDirty = true;
  1822. SendNotifyEvent(SPLN_CHANGE);
  1823. }
  1824. //////////////////////////////////////////////////////////////////////////
  1825. void AbstractSplineWidget::ValueScaleKeys(float startValue, float endValue)
  1826. {
  1827. // Calculate the scaling parameters.
  1828. float valueScale = 1.0f;
  1829. if (fabsf(startValue) > MIN_TIME_EPSILON_FOR_SCALING)
  1830. {
  1831. valueScale = endValue / startValue;
  1832. }
  1833. // Loop through all keys that are selected.
  1834. m_pHitSpline = nullptr;
  1835. m_pHitDetailSpline = nullptr;
  1836. m_nHitKeyIndex = -1;
  1837. m_nHitDimension = -1;
  1838. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1839. {
  1840. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1841. int keyCount = pSpline->GetKeyCount();
  1842. for (int i = 0; i < keyCount; i++)
  1843. {
  1844. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  1845. {
  1846. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  1847. {
  1848. ISplineInterpolator::ValueType afValue;
  1849. pSpline->GetKeyValue(i, afValue);
  1850. afValue[nCurrentDimension] = SnapValue(afValue[nCurrentDimension] * valueScale);
  1851. pSpline->SetKeyValue(i, afValue);
  1852. }
  1853. }
  1854. }
  1855. }
  1856. update();
  1857. SendNotifyEvent(SPLN_CHANGE);
  1858. }
  1859. //////////////////////////////////////////////////////////////////////////
  1860. void AbstractSplineWidget::MoveSelectedKeys(Vec2 offset, bool copyKeys)
  1861. {
  1862. m_pHitSpline = nullptr;
  1863. m_pHitDetailSpline = nullptr;
  1864. m_nHitKeyIndex = -1;
  1865. m_nHitDimension = -1;
  1866. if (copyKeys)
  1867. {
  1868. DuplicateSelectedKeys();
  1869. }
  1870. // For each spline...
  1871. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1872. {
  1873. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1874. int keyCount = pSpline->GetKeyCount();
  1875. for (int i = 0; i < keyCount; i++)
  1876. {
  1877. float oldTime = pSpline->GetKeyTime(i);
  1878. float t = SnapTime(oldTime + offset.x);
  1879. if (pSpline->IsKeySelectedAtAnyDimension(i))
  1880. {
  1881. if (pSpline->FindKey(t, m_fMinTimeEpsilon) < 0)
  1882. {
  1883. pSpline->SetKeyTime(i, SnapTimeToGridVertical(t));
  1884. }
  1885. }
  1886. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  1887. {
  1888. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  1889. {
  1890. ISplineInterpolator::ValueType afValue;
  1891. pSpline->GetKeyValue(i, afValue);
  1892. afValue[nCurrentDimension] = SnapValue(afValue[nCurrentDimension] + offset.y);
  1893. pSpline->SetKeyValue(i, afValue);
  1894. }
  1895. }
  1896. }
  1897. }
  1898. update();
  1899. if (m_pTimelineCtrl)
  1900. {
  1901. m_pTimelineCtrl->update();
  1902. }
  1903. m_bKeyTimesDirty = true;
  1904. SendNotifyEvent(SPLN_CHANGE);
  1905. }
  1906. //////////////////////////////////////////////////////////////////////////
  1907. void AbstractSplineWidget::RemoveKey(ISplineInterpolator* pSpline, int nKey)
  1908. {
  1909. CUndo undo("Remove Spline Key");
  1910. ConditionalStoreUndo();
  1911. m_bKeyTimesDirty = true;
  1912. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1913. m_pHitSpline = nullptr;
  1914. m_pHitDetailSpline = nullptr;
  1915. m_nHitKeyIndex = -1;
  1916. if (nKey != -1)
  1917. {
  1918. pSpline->RemoveKey(nKey);
  1919. }
  1920. SendNotifyEvent(SPLN_CHANGE);
  1921. update();
  1922. }
  1923. //////////////////////////////////////////////////////////////////////////
  1924. void AbstractSplineWidget::RemoveSelectedKeys()
  1925. {
  1926. CUndo undo("Remove Spline Key");
  1927. StoreUndo();
  1928. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1929. m_pHitSpline = nullptr;
  1930. m_pHitDetailSpline = nullptr;
  1931. m_nHitKeyIndex = -1;
  1932. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  1933. {
  1934. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  1935. for (int i = 0; i < (int)pSpline->GetKeyCount(); )
  1936. {
  1937. if (pSpline->IsKeySelectedAtAnyDimension(i))
  1938. {
  1939. pSpline->RemoveKey(i);
  1940. }
  1941. else
  1942. {
  1943. i++;
  1944. }
  1945. }
  1946. }
  1947. m_bKeyTimesDirty = true;
  1948. SendNotifyEvent(SPLN_CHANGE);
  1949. update();
  1950. }
  1951. //////////////////////////////////////////////////////////////////////////
  1952. void AbstractSplineWidget::RemoveSelectedKeyTimesImpl()
  1953. {
  1954. int numSelectedKeyTimes = 0;
  1955. for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
  1956. {
  1957. if ((*it).selected)
  1958. {
  1959. ++numSelectedKeyTimes;
  1960. }
  1961. }
  1962. if (numSelectedKeyTimes)
  1963. {
  1964. CUndo undo("Remove Spline Key");
  1965. StoreUndo();
  1966. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  1967. for (size_t splineIndex = 0, end = m_splines.size(); splineIndex < end; ++splineIndex)
  1968. {
  1969. std::vector<KeyTime>::iterator itTime = m_keyTimes.begin(), endTime = m_keyTimes.end();
  1970. for (int keyIndex = 0, endIndex = m_splines[splineIndex].pSpline->GetKeyCount(); keyIndex < endIndex; )
  1971. {
  1972. const float thisThreshold = 0.01f;
  1973. for (; itTime != endTime && (*itTime).time < m_splines[splineIndex].pSpline->GetKeyTime(keyIndex) - thisThreshold; ++itTime)
  1974. {
  1975. ;
  1976. }
  1977. if (itTime != endTime && fabsf((*itTime).time - m_splines[splineIndex].pSpline->GetKeyTime(keyIndex)) < thisThreshold && (*itTime).selected)
  1978. {
  1979. m_splines[splineIndex].pSpline->RemoveKey(keyIndex);
  1980. }
  1981. else
  1982. {
  1983. ++keyIndex;
  1984. }
  1985. }
  1986. }
  1987. }
  1988. }
  1989. //////////////////////////////////////////////////////////////////////////
  1990. void AbstractSplineWidget::RemoveSelectedKeyTimes()
  1991. {
  1992. RemoveSelectedKeyTimesImpl();
  1993. m_bKeyTimesDirty = true;
  1994. SendNotifyEvent(SPLN_CHANGE);
  1995. update();
  1996. }
  1997. //////////////////////////////////////////////////////////////////////////
  1998. void AbstractSplineWidget::RedrawWindowAroundMarker()
  1999. {
  2000. UpdateKeyTimes();
  2001. std::vector<KeyTime>::iterator itKeyTime = std::lower_bound(m_keyTimes.begin(), m_keyTimes.end(), KeyTime(m_fTimeMarker, 0));
  2002. size_t keyTimeIndex = (itKeyTime != m_keyTimes.end() ? itKeyTime - m_keyTimes.begin() : m_keyTimes.size());
  2003. int redrawRangeStart = (keyTimeIndex >= 2 ? static_cast<int>(TimeToXOfs(m_keyTimes[keyTimeIndex - 2].time)) : m_rcSpline.left());
  2004. int redrawRangeEnd = (keyTimeIndex < m_keyTimes.size() - 2 ? static_cast<int>(TimeToXOfs(m_keyTimes[keyTimeIndex + 2].time)) : m_rcSpline.right() + 1);
  2005. QRect rc(QPoint(redrawRangeStart, m_rcSpline.top()), QPoint(redrawRangeEnd, m_rcSpline.bottom() + 1) - QPoint(1, 1));
  2006. rc = rc.normalized().intersected(m_rcSpline);
  2007. m_TimeUpdateRect = QRect(QPoint(1, 2), QSize(2, 2));
  2008. update(rc);
  2009. }
  2010. //////////////////////////////////////////////////////////////////////////
  2011. void AbstractSplineWidget::SplinesChanged()
  2012. {
  2013. m_bKeyTimesDirty = true;
  2014. UpdateKeyTimes();
  2015. update();
  2016. }
  2017. //////////////////////////////////////////////////////////////////////////
  2018. void AbstractSplineWidget::SetControlAmplitude(bool controlAmplitude)
  2019. {
  2020. m_controlAmplitude = controlAmplitude;
  2021. }
  2022. //////////////////////////////////////////////////////////////////////////
  2023. bool AbstractSplineWidget::GetControlAmplitude() const
  2024. {
  2025. return m_controlAmplitude;
  2026. }
  2027. float AbstractSplineWidget::SnapTimeToGridVertical(float time)
  2028. {
  2029. //float fSnapTime = int((time * m_fGridTimeScale) + 0.5f) * (1.0f / m_fGridTimeScale);
  2030. float fSnapTime = time;
  2031. return fSnapTime;
  2032. }
  2033. //////////////////////////////////////////////////////////////////////////
  2034. int AbstractSplineWidget::InsertKey(ISplineInterpolator* pSpline, ISplineInterpolator* pDetailSpline, const QPoint& point)
  2035. {
  2036. CUndo undo("Spline Insert Key");
  2037. StoreUndo();
  2038. float time, val;
  2039. PointToTimeValue(point, time, val);
  2040. time = SnapTimeToGridVertical(time);
  2041. int i;
  2042. for (i = 0; i < pSpline->GetKeyCount(); i++)
  2043. {
  2044. // Skip if any key already have time that is very close.
  2045. if (fabs(pSpline->GetKeyTime(i) - time) < m_fMinTimeEpsilon)
  2046. {
  2047. return i;
  2048. }
  2049. }
  2050. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  2051. // The proper key value for a spline that has a detail spline is not what is shown in the control - we have
  2052. // to remove the detail value to get back to the underlying spline value.
  2053. if (pDetailSpline)
  2054. {
  2055. float offset = 0.0f;
  2056. pDetailSpline->InterpolateFloat(time, offset);
  2057. val -= offset;
  2058. }
  2059. ClearSelection();
  2060. ISplineInterpolator::ValueType currValue;
  2061. ISplineInterpolator::ZeroValue(currValue);
  2062. pSpline->Interpolate(time, currValue);
  2063. if (pSpline->GetNumDimensions() > 1)
  2064. {
  2065. }
  2066. int nKey = pSpline->InsertKey(time, currValue); // TODO: Don't use FE specific snapping!
  2067. if (m_defaultKeyTangentType != SPLINE_KEY_TANGENT_NONE)
  2068. {
  2069. SetKeyTangentType(pSpline, nKey, m_defaultKeyTangentType);
  2070. }
  2071. //int nKey = pSpline->InsertKeyFloat( time,val ); // TODO: Don't use FE specific snapping!
  2072. SelectKey(pSpline, nKey, 0, true);
  2073. update();
  2074. m_bKeyTimesDirty = true;
  2075. SendNotifyEvent(SPLN_CHANGE);
  2076. return nKey;
  2077. }
  2078. //////////////////////////////////////////////////////////////////////////
  2079. void AbstractSplineWidget::ClearSelection()
  2080. {
  2081. ConditionalStoreUndo();
  2082. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  2083. {
  2084. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2085. for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
  2086. {
  2087. pSpline->SelectKeyAllDimensions(i, false);
  2088. }
  2089. }
  2090. ClearSelectedKeys();
  2091. }
  2092. //////////////////////////////////////////////////////////////////////////
  2093. void AbstractSplineWidget::SetTimeMarker(float fTime)
  2094. {
  2095. if (m_pTimelineCtrl)
  2096. {
  2097. m_pTimelineCtrl->SetTimeMarker(fTime);
  2098. }
  2099. if (fTime == m_fTimeMarker)
  2100. {
  2101. return;
  2102. }
  2103. //// Erase old first.
  2104. //float x1 = TimeToXOfs(m_fTimeMarker);
  2105. //float x2 = TimeToXOfs(fTime);
  2106. //QRect rc(QPoint(x1, m_rcSpline.top()), QPoint(x2, m_rcSpline.bottom()));
  2107. //rc = rc.normalized().adjusted(-3, 0, 3, 0);// .intersected(m_rcSpline);
  2108. m_TimeUpdateRect = {};
  2109. m_fTimeMarker = fTime;
  2110. update();
  2111. }
  2112. //////////////////////////////////////////////////////////////////////////
  2113. void AbstractSplineWidget::StoreUndo()
  2114. {
  2115. if (CUndo::IsRecording() && !m_pCurrentUndo)
  2116. {
  2117. std::vector<ISplineInterpolator*> splines(m_splines.size());
  2118. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  2119. {
  2120. splines[splineIndex] = m_splines[splineIndex].pSpline;
  2121. }
  2122. CUndo::Record(m_pCurrentUndo = CreateSplineCtrlUndoObject(splines));
  2123. }
  2124. }
  2125. //////////////////////////////////////////////////////////////////////////
  2126. void AbstractSplineWidget::ConditionalStoreUndo()
  2127. {
  2128. if (m_editMode == TrackingMode || m_editMode == SelectMode)
  2129. {
  2130. StoreUndo();
  2131. }
  2132. }
  2133. //////////////////////////////////////////////////////////////////////////
  2134. void AbstractSplineWidget::ClearSelectedKeys()
  2135. {
  2136. for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
  2137. {
  2138. (*it).selected = false;
  2139. }
  2140. }
  2141. //////////////////////////////////////////////////////////////////////////
  2142. class CKeyCopyInfo
  2143. {
  2144. public:
  2145. ISplineInterpolator::ValueType value;
  2146. float time;
  2147. int flags;
  2148. ISplineInterpolator::ValueType tin, tout;
  2149. };
  2150. void AbstractSplineWidget::DuplicateSelectedKeys()
  2151. {
  2152. m_pHitSpline = nullptr;
  2153. m_pHitDetailSpline = nullptr;
  2154. m_nHitKeyIndex = -1;
  2155. using KeysToAddContainer = std::vector<CKeyCopyInfo>;
  2156. KeysToAddContainer keysToInsert;
  2157. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  2158. {
  2159. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2160. keysToInsert.resize(0);
  2161. for (int i = 0; i < pSpline->GetKeyCount(); i++)
  2162. {
  2163. // In this particular case, the dimension doesn't matter.
  2164. if (pSpline->IsKeySelectedAtAnyDimension(i))
  2165. {
  2166. keysToInsert.resize(keysToInsert.size() + 1);
  2167. CKeyCopyInfo& copyInfo = keysToInsert.back();
  2168. copyInfo.time = pSpline->GetKeyTime(i);
  2169. pSpline->GetKeyValue(i, copyInfo.value);
  2170. pSpline->GetKeyTangents(i, copyInfo.tin, copyInfo.tout);
  2171. copyInfo.flags = pSpline->GetKeyFlags(i);
  2172. }
  2173. }
  2174. for (KeysToAddContainer::iterator keyToAdd = keysToInsert.begin(), endKeysToAdd = keysToInsert.end(); keyToAdd != endKeysToAdd; ++keyToAdd)
  2175. {
  2176. int keyIndex = pSpline->InsertKey(SnapTimeToGridVertical((*keyToAdd).time), (*keyToAdd).value);
  2177. pSpline->SetKeyTangents(keyIndex, (*keyToAdd).tin, (*keyToAdd).tout);
  2178. pSpline->SetKeyFlags(keyIndex, (*keyToAdd).flags & (~ESPLINE_KEY_UI_SELECTED_MASK));
  2179. }
  2180. }
  2181. m_bKeyTimesDirty = true;
  2182. }
  2183. //////////////////////////////////////////////////////////////////////////
  2184. void AbstractSplineWidget::ZeroAll()
  2185. {
  2186. GetIEditor()->BeginUndo();
  2187. using SplineContainer = std::vector<ISplineInterpolator *>;
  2188. SplineContainer splines;
  2189. for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
  2190. {
  2191. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2192. int keyIndex = (pSpline ? pSpline->FindKey(m_fTimeMarker, 0.015f) : -1);
  2193. if (pSpline && keyIndex >= 0)
  2194. {
  2195. splines.push_back(pSpline);
  2196. }
  2197. }
  2198. CUndo::Record(CreateSplineCtrlUndoObject(splines));
  2199. for (SplineContainer::iterator itSpline = splines.begin(); itSpline != splines.end(); ++itSpline)
  2200. {
  2201. int keyIndex = ((*itSpline) ? (*itSpline)->FindKey(m_fTimeMarker, 0.015f) : -1);
  2202. if ((*itSpline) && keyIndex >= 0)
  2203. {
  2204. (*itSpline)->SetKeyValueFloat(keyIndex, 0.0f);
  2205. }
  2206. }
  2207. GetIEditor()->AcceptUndo("Zero All");
  2208. m_pCurrentUndo = nullptr;
  2209. }
  2210. //////////////////////////////////////////////////////////////////////////
  2211. void AbstractSplineWidget::KeyAll()
  2212. {
  2213. GetIEditor()->BeginUndo();
  2214. using SplineContainer = std::vector<ISplineInterpolator *>;
  2215. SplineContainer splines;
  2216. for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
  2217. {
  2218. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2219. int keyIndex = (pSpline ? pSpline->FindKey(m_fTimeMarker, 0.015f) : -1);
  2220. if (pSpline && keyIndex == -1)
  2221. {
  2222. splines.push_back(pSpline);
  2223. }
  2224. }
  2225. CUndo::Record(CreateSplineCtrlUndoObject(splines));
  2226. for (SplineContainer::iterator itSpline = splines.begin(); itSpline != splines.end(); ++itSpline)
  2227. {
  2228. float value = 0.0f;
  2229. (*itSpline)->InterpolateFloat(m_fTimeMarker, value);
  2230. int keyIndex = (*itSpline)->InsertKeyFloat(SnapTimeToGridVertical(m_fTimeMarker), value);
  2231. if (m_defaultKeyTangentType != SPLINE_KEY_TANGENT_NONE)
  2232. {
  2233. SetKeyTangentType(*itSpline, keyIndex, m_defaultKeyTangentType);
  2234. }
  2235. }
  2236. GetIEditor()->AcceptUndo("Key All");
  2237. m_pCurrentUndo = nullptr;
  2238. }
  2239. //////////////////////////////////////////////////////////////////////////
  2240. void AbstractSplineWidget::SelectAll()
  2241. {
  2242. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  2243. {
  2244. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2245. for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
  2246. {
  2247. pSpline->SelectKeyAllDimensions(i, true);
  2248. }
  2249. }
  2250. update();
  2251. }
  2252. //////////////////////////////////////////////////////////////////////////
  2253. void SplineWidget::SendNotifyEvent(const uint32_t nEvent)
  2254. {
  2255. if (nEvent == SPLN_BEFORE_CHANGE)
  2256. {
  2257. ConditionalStoreUndo();
  2258. }
  2259. switch (nEvent)
  2260. {
  2261. case SPLN_BEFORE_CHANGE:
  2262. Q_EMIT beforeChange();
  2263. break;
  2264. case SPLN_CHANGE:
  2265. Q_EMIT change();
  2266. break;
  2267. case SPLN_TIME_CHANGE:
  2268. Q_EMIT timeChange();
  2269. break;
  2270. case SPLN_SCROLL_ZOOM:
  2271. Q_EMIT scrollZoomRequested();
  2272. break;
  2273. case SPLN_KEY_SELECTION_CHANGE:
  2274. Q_EMIT keySelectionChange();
  2275. break;
  2276. case NM_CLICK:
  2277. Q_EMIT clicked();
  2278. break;
  2279. case NM_RCLICK:
  2280. Q_EMIT rightClicked();
  2281. break;
  2282. }
  2283. }
  2284. //////////////////////////////////////////////////////////////////////////
  2285. void SplineWidget::SetTimelineCtrl(TimelineWidget* pTimelineCtrl)
  2286. {
  2287. m_pTimelineCtrl = pTimelineCtrl;
  2288. if (m_pTimelineCtrl)
  2289. {
  2290. pTimelineCtrl->setParent(this);
  2291. pTimelineCtrl->SetZoom(m_grid.zoom.GetX());
  2292. pTimelineCtrl->SetOrigin(m_grid.origin.GetX());
  2293. pTimelineCtrl->SetKeyTimeSet(this);
  2294. pTimelineCtrl->update();
  2295. }
  2296. }
  2297. //////////////////////////////////////////////////////////////////////////
  2298. void AbstractSplineWidget::AddSpline(ISplineInterpolator* pSpline, ISplineInterpolator* pDetailSpline, const QColor& color)
  2299. {
  2300. for (int i = 0; i < (int)m_splines.size(); i++)
  2301. {
  2302. if (m_splines[i].pSpline == pSpline)
  2303. {
  2304. return;
  2305. }
  2306. }
  2307. SSplineInfo si;
  2308. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  2309. {
  2310. si.anColorArray[nCurrentDimension] = color;
  2311. }
  2312. si.pSpline = pSpline;
  2313. si.pDetailSpline = pDetailSpline;
  2314. m_splines.push_back(si);
  2315. m_bKeyTimesDirty = true;
  2316. update();
  2317. }
  2318. void AbstractSplineWidget::AddSpline(ISplineInterpolator* pSpline, ISplineInterpolator* pDetailSpline, QColor anColorArray[4])
  2319. {
  2320. for (int i = 0; i < (int)m_splines.size(); i++)
  2321. {
  2322. if (m_splines[i].pSpline == pSpline)
  2323. {
  2324. return;
  2325. }
  2326. }
  2327. SSplineInfo si;
  2328. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  2329. {
  2330. si.anColorArray[nCurrentDimension] = anColorArray[nCurrentDimension];
  2331. }
  2332. si.pSpline = pSpline;
  2333. si.pDetailSpline = pDetailSpline;
  2334. m_splines.push_back(si);
  2335. m_bKeyTimesDirty = true;
  2336. update();
  2337. }
  2338. //////////////////////////////////////////////////////////////////////////
  2339. void AbstractSplineWidget::RemoveSpline(ISplineInterpolator* pSpline)
  2340. {
  2341. for (int i = 0; i < (int)m_splines.size(); i++)
  2342. {
  2343. if (m_splines[i].pSpline == pSpline)
  2344. {
  2345. m_splines.erase(m_splines.begin() + i);
  2346. return;
  2347. }
  2348. }
  2349. m_bKeyTimesDirty = true;
  2350. update();
  2351. }
  2352. //////////////////////////////////////////////////////////////////////////
  2353. void AbstractSplineWidget::RemoveAllSplines()
  2354. {
  2355. m_splines.clear();
  2356. m_bKeyTimesDirty = true;
  2357. update();
  2358. }
  2359. //////////////////////////////////////////////////////////////////////////
  2360. void AbstractSplineWidget::SelectRectangle(const QRect& rc, bool bSelect)
  2361. {
  2362. ConditionalStoreUndo();
  2363. ClearSelectedKeys();
  2364. Vec2 vec0 = ClientToWorld(rc.topLeft());
  2365. Vec2 vec1 = ClientToWorld(rc.bottomRight());
  2366. float t0 = vec0.x;
  2367. float t1 = vec1.x;
  2368. float v0 = vec0.y;
  2369. float v1 = vec1.y;
  2370. if (v0 > v1)
  2371. {
  2372. std::swap(v0, v1);
  2373. }
  2374. if (t0 > t1)
  2375. {
  2376. std::swap(t0, t1);
  2377. }
  2378. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  2379. {
  2380. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2381. ISplineInterpolator* pDetailSpline = m_splines[splineIndex].pDetailSpline;
  2382. for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
  2383. {
  2384. float t = pSpline->GetKeyTime(i);
  2385. ISplineInterpolator::ValueType afValue;
  2386. pSpline->GetKeyValue(i, afValue);
  2387. int nTotalNumberOfDimensions(pSpline->GetNumDimensions());
  2388. ISplineInterpolator::ValueType afDetailValue;
  2389. if (pDetailSpline)
  2390. {
  2391. ISplineInterpolator::ZeroValue(afDetailValue);
  2392. pDetailSpline->Interpolate(t, afDetailValue);
  2393. }
  2394. for (int nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
  2395. {
  2396. if (pDetailSpline)
  2397. {
  2398. afValue[nCurrentDimension] = afValue[nCurrentDimension] + afDetailValue[nCurrentDimension];
  2399. }
  2400. if (t >= t0 && t <= t1 && afValue[nCurrentDimension] >= v0 && afValue[nCurrentDimension] <= v1)
  2401. {
  2402. pSpline->SelectKeyAtDimension(i, nCurrentDimension, bSelect);
  2403. }
  2404. }
  2405. }
  2406. }
  2407. SendNotifyEvent(SPLN_CHANGE);
  2408. }
  2409. //////////////////////////////////////////////////////////////////////////
  2410. void AbstractSplineWidget::CopyKeys()
  2411. {
  2412. // Copy selected keys.
  2413. if (m_splines.empty() || GetNumSelected() == 0)
  2414. {
  2415. return;
  2416. }
  2417. XmlNodeRef rootNode = XmlHelpers::CreateXmlNode("SplineKeys");
  2418. int i;
  2419. float minTime = FLT_MAX;
  2420. float maxTime = -FLT_MAX;
  2421. ISplineInterpolator* pSpline = m_splines[0].pSpline;
  2422. for (i = 0; i < (int)pSpline->GetKeyCount(); i++)
  2423. {
  2424. if (!pSpline->IsKeySelectedAtAnyDimension(i))
  2425. {
  2426. continue;
  2427. }
  2428. float t = pSpline->GetKeyTime(i);
  2429. if (t < minTime)
  2430. {
  2431. minTime = t;
  2432. }
  2433. if (t > maxTime)
  2434. {
  2435. maxTime = t;
  2436. }
  2437. }
  2438. rootNode->setAttr("start", minTime);
  2439. rootNode->setAttr("end", maxTime);
  2440. for (i = 0; i < (int)pSpline->GetKeyCount(); i++)
  2441. {
  2442. if (!pSpline->IsKeySelectedAtAnyDimension(i))
  2443. {
  2444. continue;
  2445. }
  2446. float t = pSpline->GetKeyTime(i); // Store offset time from copy/paste range.
  2447. ISplineInterpolator::ValueType afValue;
  2448. pSpline->GetKeyValue(i, afValue);
  2449. float tin, tout;
  2450. ISplineInterpolator::ValueType vtin, vtout;
  2451. pSpline->GetKeyTangents(i, vtin, vtout);
  2452. tin = vtin[0];
  2453. tout = vtout[0];
  2454. XmlNodeRef keyNode = rootNode->newChild("Key");
  2455. keyNode->setAttr("time", t);
  2456. keyNode->setAttr("flags", (int)pSpline->GetKeyFlags(i));
  2457. keyNode->setAttr("in", tin);
  2458. keyNode->setAttr("out", tout);
  2459. for (int ii = 0; ii < pSpline->GetNumDimensions(); ++ii)
  2460. {
  2461. XmlNodeRef dimensionNode = keyNode->newChild("values");
  2462. dimensionNode->setAttr("value", afValue[ii]);
  2463. }
  2464. }
  2465. CClipboard clipboard(WidgetCast());
  2466. clipboard.Put(rootNode);
  2467. }
  2468. //////////////////////////////////////////////////////////////////////////
  2469. void AbstractSplineWidget::PasteKeys()
  2470. {
  2471. if (m_splines.empty() || GetNumSelected() == 0)
  2472. {
  2473. return;
  2474. }
  2475. ISplineInterpolator* pSpline = m_splines[0].pSpline;
  2476. CClipboard clipboard(WidgetCast());
  2477. if (clipboard.IsEmpty())
  2478. {
  2479. return;
  2480. }
  2481. XmlNodeRef rootNode = clipboard.Get();
  2482. if (!rootNode)
  2483. {
  2484. return;
  2485. }
  2486. if (!rootNode->isTag("SplineKeys"))
  2487. {
  2488. return;
  2489. }
  2490. float minTime = 0;
  2491. float maxTime = 0;
  2492. rootNode->getAttr("start", minTime);
  2493. rootNode->getAttr("end", maxTime);
  2494. const QPoint point = mapFromGlobal(QCursor::pos());
  2495. float fTime = XOfsToTime(point.x());
  2496. float fTimeRange = (maxTime - minTime);
  2497. CUndo undo("Paste Spline Keys");
  2498. ConditionalStoreUndo();
  2499. ClearSelection();
  2500. int i;
  2501. // Delete keys in range min to max time.
  2502. for (i = 0; i < pSpline->GetKeyCount(); )
  2503. {
  2504. float t = pSpline->GetKeyTime(i);
  2505. if (t >= fTime && t <= fTime + fTimeRange)
  2506. {
  2507. pSpline->RemoveKey(i);
  2508. }
  2509. else
  2510. {
  2511. i++;
  2512. }
  2513. }
  2514. for (i = 0; i < rootNode->getChildCount(); i++)
  2515. {
  2516. XmlNodeRef keyNode = rootNode->getChild(i);
  2517. float t = 0;
  2518. float tin = 0;
  2519. float tout = 0;
  2520. int flags = 0;
  2521. keyNode->getAttr("time", t);
  2522. keyNode->getAttr("flags", flags);
  2523. keyNode->getAttr("in", tin);
  2524. keyNode->getAttr("out", tout);
  2525. int nNumberOfChildXMLNodes(0);
  2526. int nCurrentChildXMLNode(0);
  2527. int nCurrentValue(0);
  2528. ISplineInterpolator::ValueType afValue;
  2529. nNumberOfChildXMLNodes = keyNode->getChildCount();
  2530. for (nCurrentChildXMLNode = 0; nCurrentChildXMLNode < nNumberOfChildXMLNodes; ++nCurrentChildXMLNode)
  2531. {
  2532. XmlNodeRef rSubKeyNode = keyNode->getChild(nCurrentChildXMLNode);
  2533. if (rSubKeyNode->isTag("values"))
  2534. {
  2535. rSubKeyNode->getAttr("value", afValue[nCurrentValue]);
  2536. nCurrentValue++;
  2537. }
  2538. }
  2539. int key = pSpline->InsertKey(SnapTimeToGridVertical(t - minTime + fTime), afValue);
  2540. if (key >= 0)
  2541. {
  2542. pSpline->SelectKeyAllDimensions(key, true);
  2543. ISplineInterpolator::ValueType vtin, vtout;
  2544. vtin[0] = tin;
  2545. vtout[0] = tout;
  2546. pSpline->SetKeyTangents(key, vtin, vtout);
  2547. }
  2548. }
  2549. m_bKeyTimesDirty = true;
  2550. update();
  2551. }
  2552. //////////////////////////////////////////////////////////////////////////
  2553. void AbstractSplineWidget::ModifySelectedKeysFlags(int nRemoveFlags, int nAddFlags)
  2554. {
  2555. CUndo undo("Modify Spline Keys");
  2556. StoreUndo();
  2557. SendNotifyEvent(SPLN_BEFORE_CHANGE);
  2558. for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
  2559. {
  2560. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2561. for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
  2562. {
  2563. // If the key is selected in any dimension...
  2564. for (
  2565. int nCurrentDimension = 0;
  2566. nCurrentDimension < pSpline->GetNumDimensions();
  2567. nCurrentDimension++
  2568. )
  2569. {
  2570. if (IsKeySelected(pSpline, i, nCurrentDimension))
  2571. {
  2572. int flags = pSpline->GetKeyFlags(i);
  2573. flags &= ~nRemoveFlags;
  2574. flags |= nAddFlags;
  2575. pSpline->SetKeyFlags(i, flags);
  2576. break;
  2577. }
  2578. }
  2579. }
  2580. }
  2581. SendNotifyEvent(SPLN_CHANGE);
  2582. update();
  2583. }
  2584. //////////////////////////////////////////////////////////////////////////
  2585. void AbstractSplineWidget::FitSplineToViewWidth()
  2586. {
  2587. // Calculate time zoom so that whole time range fits.
  2588. float t0 = FLT_MAX;
  2589. float t1 = -FLT_MAX;
  2590. bool bAnyKey = false;
  2591. for (int i = 0; i < int(m_splines.size()); ++i)
  2592. {
  2593. //////////////////////////////////////////////////////////////////////////
  2594. ISplineInterpolator* pSpline = m_splines[i].pSpline;
  2595. if (!pSpline)
  2596. {
  2597. continue;
  2598. }
  2599. for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
  2600. {
  2601. float keyTime = pSpline->GetKeyTime(keyIndex);
  2602. t0 = std::min(t0, keyTime);
  2603. t1 = std::max(t1, keyTime);
  2604. bAnyKey = true;
  2605. }
  2606. }
  2607. if (!bAnyKey)
  2608. {
  2609. t0 = m_timeRange.start;
  2610. t1 = m_timeRange.end;
  2611. }
  2612. float zoom = abs(m_rcSpline.width() - 20) / max(1.0f, fabs(t1 - t0));
  2613. SetZoom(Vec2(zoom, m_grid.zoom.GetY()));
  2614. SetScrollOffset(Vec2(t0, m_grid.origin.GetY()));
  2615. }
  2616. //////////////////////////////////////////////////////////////////////////
  2617. void AbstractSplineWidget::FitSplineToViewHeight()
  2618. {
  2619. // Calculate time zoom so that whole value range fits.
  2620. Range splineRange = GetSplinesRange();
  2621. if (splineRange.start == FLT_MAX)
  2622. {
  2623. splineRange = m_valueRange;
  2624. }
  2625. float zoom = abs(m_rcSpline.height() - 40) / max(minViewRange, splineRange.Length());
  2626. SetZoom(Vec2(m_grid.zoom.GetX(), zoom));
  2627. // Center the range if it's less than the minRange by adjusting it's offset.
  2628. float scrollOffset = max(0.0f, minViewRange - splineRange.Length()) / 2.0f;
  2629. SetScrollOffset(Vec2(m_grid.origin.GetX(), splineRange.start - scrollOffset));
  2630. }
  2631. //////////////////////////////////////////////////////////////////////////
  2632. void AbstractSplineWidget::FitSplineHeightToValueRange()
  2633. {
  2634. Range splineRange = GetSplinesRange();
  2635. splineRange.start = min(splineRange.start, m_valueRange.start);
  2636. splineRange.end = max(splineRange.end, m_valueRange.end);
  2637. float zoom = abs(m_rcSpline.height() - 40) / max(minViewRange, splineRange.Length());
  2638. SetZoom(Vec2(m_grid.zoom.GetX(), zoom));
  2639. SetScrollOffset(Vec2(m_grid.origin.GetX(), splineRange.start));
  2640. }
  2641. //////////////////////////////////////////////////////////////////////////
  2642. void AbstractSplineWidget::OnUserCommand(UINT cmd)
  2643. {
  2644. switch (cmd)
  2645. {
  2646. case ID_TANGENT_IN_ZERO:
  2647. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, SPLINE_KEY_TANGENT_ZERO << SPLINE_KEY_TANGENT_IN_SHIFT);
  2648. break;
  2649. case ID_TANGENT_IN_STEP:
  2650. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, SPLINE_KEY_TANGENT_STEP << SPLINE_KEY_TANGENT_IN_SHIFT);
  2651. break;
  2652. case ID_TANGENT_IN_LINEAR:
  2653. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, SPLINE_KEY_TANGENT_LINEAR << SPLINE_KEY_TANGENT_IN_SHIFT);
  2654. break;
  2655. case ID_TANGENT_OUT_ZERO:
  2656. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, SPLINE_KEY_TANGENT_ZERO << SPLINE_KEY_TANGENT_OUT_SHIFT);
  2657. break;
  2658. case ID_TANGENT_OUT_STEP:
  2659. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, SPLINE_KEY_TANGENT_STEP << SPLINE_KEY_TANGENT_OUT_SHIFT);
  2660. break;
  2661. case ID_TANGENT_OUT_LINEAR:
  2662. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, SPLINE_KEY_TANGENT_LINEAR << SPLINE_KEY_TANGENT_OUT_SHIFT);
  2663. break;
  2664. case ID_TANGENT_AUTO:
  2665. ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK | SPLINE_KEY_TANGENT_OUT_MASK, 0);
  2666. break;
  2667. case ID_SPLINE_FIT_X:
  2668. FitSplineToViewWidth();
  2669. break;
  2670. case ID_SPLINE_FIT_Y:
  2671. FitSplineToViewHeight();
  2672. break;
  2673. case ID_SPLINE_SNAP_GRID_X:
  2674. SetSnapTime(!m_bSnapTime);
  2675. break;
  2676. case ID_SPLINE_SNAP_GRID_Y:
  2677. SetSnapValue(!m_bSnapValue);
  2678. break;
  2679. case ID_SPLINE_PREVIOUS_KEY:
  2680. GotoNextKey(true);
  2681. break;
  2682. case ID_SPLINE_NEXT_KEY:
  2683. GotoNextKey(false);
  2684. break;
  2685. case ID_SPLINE_FLATTEN_ALL:
  2686. RemoveAllKeysButThis();
  2687. break;
  2688. }
  2689. }
  2690. //////////////////////////////////////////////////////////////////////////
  2691. void AbstractSplineWidget::GotoNextKey(bool previousKey)
  2692. {
  2693. if (GetNumSelected() == 1)
  2694. {
  2695. bool boFoundTheSelectedKey(false);
  2696. for (size_t splineIndex = 0, endSpline = m_splines.size(); splineIndex < endSpline; ++splineIndex)
  2697. {
  2698. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2699. for (int i = 0; i < pSpline->GetKeyCount(); i++)
  2700. {
  2701. for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
  2702. {
  2703. if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
  2704. {
  2705. boFoundTheSelectedKey = true;
  2706. if ((previousKey && i > 0) || (!previousKey && i + 1 < pSpline->GetKeyCount()))
  2707. {
  2708. int nextKey = previousKey ? i - 1 : i + 1;
  2709. float keyTime = pSpline->GetKeyTime(nextKey);
  2710. SetTimeMarker(keyTime);
  2711. ISplineInterpolator::ValueType afValue;
  2712. pSpline->GetKeyValue(nextKey, afValue);
  2713. pSpline->SelectKeyAtDimension(i, nCurrentDimension, false);
  2714. pSpline->SelectKeyAtDimension(nextKey, nCurrentDimension, true);
  2715. // Set the new scrolled coordinates
  2716. float ofsx = keyTime - ((m_grid.rect.right() + 1) / 2) / m_grid.zoom.GetX();
  2717. float ofsy = afValue[nCurrentDimension] - ((m_grid.rect.bottom() + 1) / 2) / m_grid.zoom.GetY();
  2718. SetScrollOffset(Vec2(ofsx, ofsy));
  2719. }
  2720. break;
  2721. }
  2722. }
  2723. if (boFoundTheSelectedKey)
  2724. {
  2725. break;
  2726. }
  2727. }
  2728. }
  2729. }
  2730. else
  2731. {
  2732. for (size_t splineIndex = 0, endSpline = m_splines.size(); splineIndex < endSpline; ++splineIndex)
  2733. {
  2734. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2735. float fClosestKeyTime = -1.0f;
  2736. float fClosestDist = 1E8;
  2737. for (int i = 0; i < pSpline->GetKeyCount(); i++)
  2738. {
  2739. float fKeyTime = pSpline->GetKeyTime(i);
  2740. float fKeyDist = previousKey ? m_fTimeMarker - fKeyTime : fKeyTime - m_fTimeMarker;
  2741. if ((fKeyDist > 0.0f) && (fKeyDist < fClosestDist))
  2742. {
  2743. fClosestDist = fKeyDist;
  2744. fClosestKeyTime = pSpline->GetKeyTime(i);
  2745. }
  2746. }
  2747. if (fClosestKeyTime >= 0.0f)
  2748. {
  2749. SetTimeMarker(fClosestKeyTime);
  2750. float averageValue = 0.f;
  2751. int dimensions = pSpline->GetNumDimensions();
  2752. for (int i = 0; i < dimensions; i++)
  2753. {
  2754. float keyValue;
  2755. int keyNum = pSpline->FindKey(fClosestKeyTime);
  2756. pSpline->GetKeyValueFloat(keyNum, keyValue);
  2757. averageValue += keyValue;
  2758. }
  2759. // Set the new scrolled coordinates
  2760. const float ofsx = fClosestKeyTime - ((m_grid.rect.right() + 1) / 2) / m_grid.zoom.GetX();
  2761. const float ofsy = averageValue / dimensions - ((m_grid.rect.bottom() + 1) / 2) / m_grid.zoom.GetY();
  2762. SetScrollOffset(Vec2(ofsx, ofsy));
  2763. }
  2764. }
  2765. }
  2766. SendNotifyEvent(SPLN_TIME_CHANGE);
  2767. }
  2768. //////////////////////////////////////////////////////////////////////////
  2769. void AbstractSplineWidget::RemoveAllKeysButThis()
  2770. {
  2771. std::vector<int> keys;
  2772. for (size_t splineIndex = 0, endSpline = m_splines.size(); splineIndex < endSpline; ++splineIndex)
  2773. {
  2774. ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
  2775. for (int i = 0; i < pSpline->GetKeyCount(); i++)
  2776. {
  2777. if (pSpline->IsKeySelectedAtAnyDimension(i))
  2778. {
  2779. keys.push_back(i);
  2780. }
  2781. }
  2782. for (int i = pSpline->GetKeyCount(); i >= 0; i--)
  2783. {
  2784. bool saveKey = false;
  2785. for (int nIndex = 0; nIndex < keys.size(); nIndex++)
  2786. {
  2787. if (keys[nIndex] == i)
  2788. {
  2789. saveKey = true;
  2790. }
  2791. }
  2792. if (!saveKey)
  2793. {
  2794. RemoveKey(pSpline, i);
  2795. }
  2796. }
  2797. }
  2798. }
  2799. //////////////////////////////////////////////////////////////////////////
  2800. ISplineCtrlUndo* AbstractSplineWidget::CreateSplineCtrlUndoObject(std::vector<ISplineInterpolator*>& splineContainer)
  2801. {
  2802. return new CUndoSplineCtrlEx(this, splineContainer);
  2803. }
  2804. Range AbstractSplineWidget::GetSplinesRange()
  2805. {
  2806. Range range(FLT_MAX, -FLT_MAX);
  2807. for (int i = 0; i < int(m_splines.size()); ++i)
  2808. {
  2809. //////////////////////////////////////////////////////////////////////////
  2810. ISplineInterpolator* pSpline = m_splines[i].pSpline;
  2811. if (!pSpline)
  2812. {
  2813. continue;
  2814. }
  2815. ISplineInterpolator::ValueType value;
  2816. for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
  2817. {
  2818. pSpline->GetKeyValue(keyIndex, value);
  2819. for (int d = 0, numDim = pSpline->GetNumDimensions(); d < numDim; d++)
  2820. {
  2821. range.start = std::min(range.start, value[d]);
  2822. range.end = std::max(range.end, value[d]);
  2823. }
  2824. }
  2825. }
  2826. return range;
  2827. }
  2828. #include <Controls/moc_SplineCtrlEx.cpp>