12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include "EditorDefs.h"
- #include "SplineCtrlEx.h"
- // Qt
- #include <QPainter>
- #include <QPainterPath>
- #include <QToolTip>
- #include <QRubberBand>
- // Editor
- #include "TimelineCtrl.h"
- #include "Clipboard.h"
- #include "GridUtils.h"
- #define DEFAULT_MIN_TIME_EPSILON 0.001f
- #define MIN_TIME_EPSILON_FOR_SCALING 0.1f
- #define ACTIVE_BKG_COLOR QColor(190, 190, 190)
- #define GRID_COLOR QColor(110, 110, 110)
- #define EDIT_SPLINE_COLOR QColor(128, 255, 128)
- #define MIN_PIXEL_PER_GRID_X 50
- #define MIN_PIXEL_PER_GRID_Y 10
- #define LEFT_BORDER_OFFSET 40
- const float AbstractSplineWidget::threshold = 0.015f;
- static const float minViewRange = 0.1f;
- //////////////////////////////////////////////////////////////////////////
- static void SetKeyTangentType(ISplineInterpolator* pSpline, int key, ESplineKeyTangentType type)
- {
- int flags = (pSpline->GetKeyFlags(key) & ~SPLINE_KEY_TANGENT_IN_MASK) & ~SPLINE_KEY_TANGENT_OUT_MASK;
- pSpline->SetKeyFlags(key, flags | (type << SPLINE_KEY_TANGENT_IN_SHIFT) | (type << SPLINE_KEY_TANGENT_OUT_SHIFT));
- }
- //////////////////////////////////////////////////////////////////////////
- class CUndoSplineCtrlEx
- : public ISplineCtrlUndo
- {
- public:
- CUndoSplineCtrlEx(AbstractSplineWidget* pCtrl, std::vector<ISplineInterpolator*>& splineContainer)
- {
- m_pCtrl = FindControl(pCtrl);
- for (size_t splineIndex = 0; splineIndex < splineContainer.size(); ++splineIndex)
- {
- AddSpline(splineContainer[splineIndex]);
- }
- SerializeSplines(&SplineEntry::undo, false);
- }
- protected:
- void AddSpline(ISplineInterpolator* pSpline)
- {
- AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
- m_splineEntries.resize(m_splineEntries.size() + 1);
- SplineEntry& entry = m_splineEntries.back();
- ISplineSet* pSplineSet = (pCtrl ? pCtrl->m_pSplineSet : nullptr);
- entry.id = (pSplineSet ? pSplineSet->GetIDFromSpline(pSpline) : AZStd::string{});
- entry.pSpline = pSpline;
- const int numKeys = pSpline->GetKeyCount();
- entry.keySelectionFlags.reserve(numKeys);
- for (int i = 0; i < numKeys; ++i)
- {
- entry.keySelectionFlags.push_back(pSpline->GetKeyFlags(i) & ESPLINE_KEY_UI_SELECTED_MASK);
- }
- }
- int GetSize() override { return sizeof(*this); }
- void Undo(bool bUndo) override
- {
- AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
- if (pCtrl)
- {
- pCtrl->SendNotifyEvent(SPLN_BEFORE_CHANGE);
- }
- if (bUndo)
- {
- SerializeSplines(&SplineEntry::redo, false);
- }
- SerializeSplines(&SplineEntry::undo, true);
- if (pCtrl && bUndo)
- {
- pCtrl->m_bKeyTimesDirty = true;
- pCtrl->SendNotifyEvent(SPLN_CHANGE);
- pCtrl->update();
- }
- }
- void Redo() override
- {
- AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
- if (pCtrl)
- {
- pCtrl->SendNotifyEvent(SPLN_BEFORE_CHANGE);
- }
- SerializeSplines(&SplineEntry::redo, true);
- if (pCtrl)
- {
- pCtrl->m_bKeyTimesDirty = true;
- pCtrl->SendNotifyEvent(SPLN_CHANGE);
- pCtrl->update();
- }
- }
- private:
- class SplineEntry
- {
- public:
- std::vector<int> keySelectionFlags;
- _smart_ptr<ISplineBackup> undo;
- _smart_ptr<ISplineBackup> redo;
- AZStd::string id;
- ISplineInterpolator* pSpline;
- };
- void SerializeSplines(_smart_ptr<ISplineBackup> SplineEntry::* backup, bool bLoading)
- {
- AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
- ISplineSet* pSplineSet = (pCtrl ? pCtrl->m_pSplineSet : nullptr);
- for (auto it = m_splineEntries.begin(); it != m_splineEntries.end(); ++it)
- {
- SplineEntry& entry = *it;
- ISplineInterpolator* pSpline = (pSplineSet ? pSplineSet->GetSplineFromID(entry.id) : entry.pSpline);
- if (!pSpline && pCtrl && pCtrl->GetSplineCount() > 0)
- {
- pSpline = pCtrl->GetSpline(0);
- }
- if (pSpline && bLoading)
- {
- pSpline->Restore(entry.*backup);
- }
- else if (pSpline)
- {
- (entry.*backup) = pSpline->Backup();
- }
- }
- }
- public:
- using CSplineCtrls = std::list<AbstractSplineWidget *>;
- static AbstractSplineWidget* FindControl(AbstractSplineWidget* pCtrl)
- {
- if (!pCtrl)
- {
- return nullptr;
- }
- auto iter = std::find(s_activeCtrls.begin(), s_activeCtrls.end(), pCtrl);
- if (iter == s_activeCtrls.end())
- {
- return nullptr;
- }
- return *iter;
- }
- static void RegisterControl(AbstractSplineWidget* pCtrl)
- {
- if (!FindControl(pCtrl))
- {
- s_activeCtrls.push_back(pCtrl);
- }
- }
- static void UnregisterControl(AbstractSplineWidget* pCtrl)
- {
- if (FindControl(pCtrl))
- {
- s_activeCtrls.remove(pCtrl);
- }
- }
- static CSplineCtrls s_activeCtrls;
- bool IsSelectionChanged() const override
- {
- AbstractSplineWidget* pCtrl = FindControl(m_pCtrl);
- ISplineSet* pSplineSet = (pCtrl ? pCtrl->m_pSplineSet : nullptr);
- for (auto it = m_splineEntries.begin(); it != m_splineEntries.end(); ++it)
- {
- const SplineEntry& entry = *it;
- ISplineInterpolator* pSpline = (pSplineSet ? pSplineSet->GetSplineFromID(entry.id) : entry.pSpline);
- if (!pSpline && pCtrl && pCtrl->GetSplineCount() > 0)
- {
- pSpline = pCtrl->GetSpline(0);
- }
- if (!pSpline)
- {
- return false;
- }
- if (pSpline->GetKeyCount() != entry.keySelectionFlags.size())
- {
- return true;
- }
- for (int i = 0; i < pSpline->GetKeyCount(); ++i)
- {
- if (entry.keySelectionFlags[i] != (pSpline->GetKeyFlags(i) & ESPLINE_KEY_UI_SELECTED_MASK))
- {
- return true;
- }
- }
- }
- return false;
- }
- private:
- AbstractSplineWidget* m_pCtrl;
- std::vector<SplineEntry> m_splineEntries;
- std::vector<float> m_keyTimes;
- };
- CUndoSplineCtrlEx::CSplineCtrls CUndoSplineCtrlEx::s_activeCtrls;
- SplineWidget::SplineWidget(QWidget* parent)
- : QWidget(parent)
- , m_rubberBand(new QRubberBand(QRubberBand::Rectangle, this))
- {
- m_rubberBand->setVisible(false);
- setMouseTracking(true);
- setFocusPolicy(Qt::StrongFocus);
- }
- SplineWidget::~SplineWidget()
- {
- }
- //////////////////////////////////////////////////////////////////////////
- AbstractSplineWidget::AbstractSplineWidget()
- : m_defaultKeyTangentType(SPLINE_KEY_TANGENT_NONE)
- {
- m_pTimelineCtrl = nullptr;
- m_totalSplineCount = 0;
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- m_nHitDimension = -1;
- m_bHitIncomingHandle = true;
- m_nKeyDrawRadius = 3;
- m_gridX = 10;
- m_gridY = 10;
- m_timeRange.start = 0;
- m_timeRange.end = 1;
- m_fMinValue = -1;
- m_fMaxValue = 1;
- m_valueRange.Set(-1, 1);
- m_fTooltipScaleX = 1;
- m_fTooltipScaleY = 1;
- m_cMousePos = QPoint(0, 0);
- m_fTimeScale = 1;
- m_fValueScale = 1;
- m_fGridTimeScale = 30.0f;
- m_ticksStep = 10;
- m_fTimeMarker = -10;
- m_editMode = NothingMode;
- m_bSnapTime = false;
- m_bSnapValue = false;
- m_bBitmapValid = false;
- m_nLeftOffset = LEFT_BORDER_OFFSET;
- m_grid.zoom = AZ::Vector2(200,100);
- m_bKeyTimesDirty = false;
- m_rcSelect = QRect();
- m_rcSpline = QRect();
- m_boLeftMouseButtonDown = false;
- m_pSplineSet = nullptr;
- m_controlAmplitude = false;
- m_fMinTimeEpsilon = DEFAULT_MIN_TIME_EPSILON;
- m_defaultValueRange.Set(-1.1f, 1.1f);
- m_bEditLock = false;
- m_pCurrentUndo = nullptr;
- CUndoSplineCtrlEx::RegisterControl(this);
- }
- //////////////////////////////////////////////////////////////////////////
- AbstractSplineWidget::~AbstractSplineWidget()
- {
- CUndoSplineCtrlEx::UnregisterControl(this);
- }
- //////////////////////////////////////////////////////////////////////////
- Vec2 AbstractSplineWidget::GetZoom()
- {
- return Vec2(m_grid.zoom.GetX(), m_grid.zoom.GetY());
- }
- Vec2 AbstractSplineWidget::GetScrollOffset()
- {
- return Vec2(m_grid.origin.GetX(), m_grid.origin.GetY());
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetZoom(Vec2 zoom, const QPoint& center)
- {
- m_grid.SetZoom(zoom, QPoint(center.x(), m_rcSpline.bottom() + 1 - center.y()));
- SetScrollOffset(Vec2(m_grid.origin.GetX(), m_grid.origin.GetY()));
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->setZoom(zoom.x, m_grid.origin.GetX());
- }
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetZoom(Vec2 zoom)
- {
- m_grid.zoom = AZ::Vector2(zoom.x,zoom.y);
- SetScrollOffset(Vec2(m_grid.origin.GetX(), m_grid.origin.GetY()));
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->setZoom(zoom.x, m_grid.origin.GetX());
- }
- SendNotifyEvent(SPLN_SCROLL_ZOOM);
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetScrollOffset(Vec2 ofs)
- {
- m_grid.origin = AZ::Vector2(ofs.x, ofs.y);
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->setZoom(m_grid.zoom.GetX(), m_grid.origin.GetX());
- }
- SendNotifyEvent(SPLN_SCROLL_ZOOM);
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- float AbstractSplineWidget::SnapTime(float time)
- {
- if (m_bSnapTime)
- {
- const float step = m_grid.step.GetX() / 10.0f;
- return floor((time / step) + 0.5f) * step;
- }
- return time;
- }
- //////////////////////////////////////////////////////////////////////////
- float AbstractSplineWidget::SnapValue(float val)
- {
- if (m_bSnapValue)
- {
- const float step = m_grid.step.GetY();
- return floor((val / step) + 0.5f) * step;
- }
- return val;
- }
- //////////////////////////////////////////////////////////////////////////
- int AbstractSplineWidget::GetKeyTimeCount() const
- {
- UpdateKeyTimes();
- return int(m_keyTimes.size());
- }
- //////////////////////////////////////////////////////////////////////////
- float AbstractSplineWidget::GetKeyTime(int index) const
- {
- UpdateKeyTimes();
- return m_keyTimes[index].time;
- }
- //////////////////////////////////////////////////////////////////////////
- bool AbstractSplineWidget::GetKeyTimeSelected(int index) const
- {
- UpdateKeyTimes();
- return m_keyTimes[index].selected;
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetKeyTimeSelected(int index, bool selected)
- {
- m_keyTimes[index].selected = selected;
- }
- //////////////////////////////////////////////////////////////////////////
- int AbstractSplineWidget::GetKeyCount(int index) const
- {
- UpdateKeyTimes();
- return m_keyTimes[index].count;
- }
- //////////////////////////////////////////////////////////////////////////
- int AbstractSplineWidget::GetKeyCountBound() const
- {
- UpdateKeyTimes();
- return m_totalSplineCount;
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::BeginEdittingKeyTimes()
- {
- if (CUndo::IsRecording())
- {
- GetIEditor()->CancelUndo();
- m_pCurrentUndo = nullptr;
- }
- GetIEditor()->BeginUndo();
- for (int keyTimeIndex = 0; keyTimeIndex < int(m_keyTimes.size()); ++keyTimeIndex)
- {
- m_keyTimes[keyTimeIndex].oldTime = m_keyTimes[keyTimeIndex].time;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::EndEdittingKeyTimes()
- {
- if (CUndo::IsRecording())
- {
- GetIEditor()->AcceptUndo("Batch key move");
- m_pCurrentUndo = nullptr;
- }
- m_bKeyTimesDirty = true;
- update();
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::MoveKeyTimes(int numChanges, int* indices, float scale, float offset, bool copyKeys)
- {
- if (CUndo::IsRecording())
- {
- GetIEditor()->RestoreUndo();
- std::vector<ISplineInterpolator*> splines;
- for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- if (pSpline)
- {
- splines.push_back(pSpline);
- }
- }
- CUndo::Record(m_pCurrentUndo = CreateSplineCtrlUndoObject(splines));
- for (int keyTimeIndex = 0; keyTimeIndex < int(m_keyTimes.size()); ++keyTimeIndex)
- {
- m_keyTimes[keyTimeIndex].time = m_keyTimes[keyTimeIndex].oldTime;
- }
- }
- class KeyChange
- {
- public:
- KeyChange(ISplineInterpolator* pSpline, int keyIndex, float oldTime, float newTime, int flags)
- : pSpline(pSpline)
- , keyIndex(keyIndex)
- , oldTime(oldTime)
- , newTime(newTime)
- , flags(flags) {}
- ISplineInterpolator* pSpline;
- int keyIndex;
- float oldTime;
- float newTime;
- ISplineInterpolator::ValueType value;
- int flags;
- ISplineInterpolator::ValueType tin, tout;
- };
- std::vector<KeyChange> individualKeyChanges;
- for (int changeIndex = 0; indices && changeIndex < numChanges; ++changeIndex)
- {
- int index = (indices ? indices[changeIndex] : 0);
- float oldTime = m_keyTimes[index].time;
- float time = __max(m_timeRange.start, __min(m_timeRange.end, scale * oldTime + offset));
- for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
- {
- float keyTime = pSpline->GetKeyTime(keyIndex);
- KeyChange change(pSpline, keyIndex, keyTime, SnapTimeToGridVertical(time), pSpline->GetKeyFlags(keyIndex));
- pSpline->GetKeyValue(keyIndex, change.value);
- pSpline->GetKeyTangents(keyIndex, change.tin, change.tout);
- if (fabsf(keyTime - oldTime) < threshold)
- {
- individualKeyChanges.push_back(change);
- }
- }
- }
- m_keyTimes[index].time = SnapTimeToGridVertical(time);
- }
- for (std::vector<KeyChange>::iterator itChange = individualKeyChanges.begin(); itChange != individualKeyChanges.end(); ++itChange)
- {
- (*itChange).pSpline->SetKeyTime((*itChange).keyIndex, (*itChange).newTime);
- }
- if (copyKeys)
- {
- for (std::vector<KeyChange>::iterator keyToAdd = individualKeyChanges.begin(), endKeysToAdd = individualKeyChanges.end(); keyToAdd != endKeysToAdd; ++keyToAdd)
- {
- int keyIndex = (*keyToAdd).pSpline->InsertKey((*keyToAdd).oldTime, (*keyToAdd).value);
- (*keyToAdd).pSpline->SetKeyTangents(keyIndex, (*keyToAdd).tin, (*keyToAdd).tout);
- (*keyToAdd).pSpline->SetKeyFlags(keyIndex, (*keyToAdd).flags & (~ESPLINE_KEY_UI_SELECTED_MASK));
- }
- }
- // Loop through all moved keys, checking whether there are multiple keys on the same frame.
- for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- float lastKeyTime = -FLT_MAX;
- pSpline->Update();
- for (int keyIndex = 0, keys = pSpline->GetKeyCount(); keyIndex <= keys; )
- {
- float keyTime = pSpline->GetKeyTime(keyIndex);
- if (fabsf(keyTime - lastKeyTime) < m_fMinTimeEpsilon)
- {
- --keys;
- pSpline->RemoveKey(keyIndex);
- }
- else
- {
- ++keyIndex;
- lastKeyTime = keyTime;
- }
- }
- }
- SendNotifyEvent(SPLN_CHANGE);
- update();
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::resizeEvent(QResizeEvent* event)
- {
- QRect oldRect = m_rcSpline;
- QWidget::resizeEvent(event);
- m_rcClient = rect();
- m_rcSpline = m_rcClient;
- if (m_pTimelineCtrl)
- {
- QRect rct = m_rcSpline;
- const int timelineControlHeight = 16;
- rct.setHeight(timelineControlHeight);
- m_rcSpline.setTop(rct.bottom() + 1);
- rct.setLeft(rct.left() + m_nLeftOffset);
- m_pTimelineCtrl->setGeometry(rct);
- }
- m_rcSpline.setLeft(m_rcSpline.left() + m_nLeftOffset);
- m_grid.rect = m_rcSpline;
- int oldW = oldRect.width();
- int oldH = oldRect.height();
- if (width() > 1 && height() > 1 && oldW > 1 && oldH > 1 && m_rcSpline.width() > 0 && m_rcSpline.height())
- {
- SetZoom(Vec2(float(m_rcSpline.width()) / oldW * GetZoom().x, float(m_rcSpline.height()) / oldH * GetZoom().y));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- QPoint AbstractSplineWidget::TimeToPoint(float time, ISplineInterpolator* pSpline)
- {
- float val = 0;
- if (pSpline)
- {
- pSpline->InterpolateFloat(time, val);
- }
- return WorldToClient(Vec2(time, val));
- ;
- }
- //////////////////////////////////////////////////////////////////////////
- float AbstractSplineWidget::TimeToXOfs(float x)
- {
- return static_cast<float>(WorldToClient(Vec2(float(x), 0.0f)).x());
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::PointToTimeValue(QPoint point, float& time, float& value)
- {
- Vec2 v = ClientToWorld(point);
- value = v.y;
- time = XOfsToTime(point.x());
- }
- //////////////////////////////////////////////////////////////////////////
- float AbstractSplineWidget::XOfsToTime(int x)
- {
- Vec2 v = ClientToWorld(QPoint(x, 0));
- float time = v.x;
- return time;
- }
- //////////////////////////////////////////////////////////////////////////
- QPoint AbstractSplineWidget::XOfsToPoint(int x, ISplineInterpolator* pSpline)
- {
- return TimeToPoint(XOfsToTime(x), pSpline);
- }
- //////////////////////////////////////////////////////////////////////////
- QPoint AbstractSplineWidget::WorldToClient(Vec2 v)
- {
- QPoint p = m_grid.WorldToClient(v);
- p.setY(m_rcSpline.bottom() - p.y());
- return p;
- }
- //////////////////////////////////////////////////////////////////////////
- Vec2 AbstractSplineWidget::ClientToWorld(const QPoint& point)
- {
- Vec2 v = m_grid.ClientToWorld(QPoint(point.x(), m_rcSpline.bottom() + 1 - point.y()));
- return v;
- }
- void SplineWidget::paintEvent(QPaintEvent* event)
- {
- QPainter painter(this);
- if (m_TimeUpdateRect != event->rect())
- {
- painter.fillRect(event->rect(), QColor(160, 160, 160));
- m_grid.CalculateGridLines();
- //Draw Grid
- DrawGrid(&painter);
- const QRect drawSplineRect = event->rect().intersected(m_rcSpline);
- // Calculate the times corresponding to the left and right of the area to be painted -
- // we can use this to draw only the necessary parts of the splines.
- float startTime = XOfsToTime(drawSplineRect.left());
- float endTime = XOfsToTime(drawSplineRect.right());
- //Draw Keys and Curve
- for (int i = 0; i < int(m_splines.size()); ++i)
- {
- DrawSpline(&painter, m_splines[i], startTime, endTime);
- DrawKeys(&painter, i, startTime, endTime);
- }
- }
- m_TimeUpdateRect = QRect();
- DrawTimeMarker(&painter);
- }
- //////////////////////////////////////////////////////////////////////////
- class SplineControlVerticalLineDrawer
- {
- public:
- SplineControlVerticalLineDrawer(QPainter* painter, const QRect& rect)
- : rect(rect)
- , painter(painter)
- {
- }
- void operator()([[maybe_unused]] int frameIndex, int x)
- {
- if (painter)
- {
- painter->drawLine(x, rect.top(), x, rect.bottom());
- }
- }
- QPainter* painter;
- QRect rect;
- };
- void SplineWidget::DrawGrid(QPainter* painter)
- {
- QPoint ptTop = WorldToClient(Vec2(0.0f, m_valueRange.end));
- QPoint ptBottom = WorldToClient(Vec2(0.0f, m_valueRange.start));
- QPoint pt0 = WorldToClient(Vec2(m_timeRange.start, 0));
- QPoint pt1 = WorldToClient(Vec2(m_timeRange.end, 0));
- QRect timeRc = QRect(QPoint(pt0.x() - 2, ptTop.y()), QPoint(pt1.x() + 2, ptBottom.y()) - QPoint(1, 1));
- timeRc = timeRc.intersected(m_rcSpline);
- painter->fillRect(timeRc, ACTIVE_BKG_COLOR);
- //////////////////////////////////////////////////////////////////////////
- QPen pOldPen = painter->pen();
- painter->setPen(GRID_COLOR);
- /// Draw Left Separator.
- 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);
- painter->drawLine(m_rcClient.left() + m_nLeftOffset, m_rcClient.bottom(), m_rcClient.left() + m_nLeftOffset, m_rcClient.top());
- //////////////////////////////////////////////////////////////////////////
- int gy;
- QPen pen(QColor(GRID_COLOR), 1, Qt::DotLine);
- pen.setCosmetic(true);
- painter->setPen(pen);
- //if (m_grid.pixelsPerGrid.y >= MIN_PIXEL_PER_GRID_Y)
- {
- // Draw horizontal grid lines.
- for (gy = m_grid.firstGridLine.y(); gy < m_grid.firstGridLine.y() + m_grid.numGridLines.y() + 1; gy++)
- {
- int y = m_grid.GetGridLineY(gy);
- if (y < 0)
- {
- continue;
- }
- int py = m_rcSpline.height() - y;
- if (py < m_rcSpline.top() || py > m_rcSpline.bottom() + 1)
- {
- continue;
- }
- painter->setPen(pen);
- painter->drawLine(m_rcSpline.left(), py, m_rcSpline.right(), py);
- float v = m_grid.GetGridLineYValue(gy);
- v = floor(v * 1000.0f + 0.5f) / 1000.0f;
- if ((v >= m_valueRange.start && v <= m_valueRange.end) || fabs(v - m_valueRange.start) < 0.01f || fabs(v - m_valueRange.end) < 0.01f)
- {
- painter->setPen(Qt::black);
- painter->drawText(m_rcClient.left() + 2, py - 8, QString::number(v));
- }
- }
- }
- // Draw vertical grid lines.
- SplineControlVerticalLineDrawer verticalLineDrawer(painter, m_rcSpline);
- GridUtils::IterateGrid(verticalLineDrawer, 50.0f, m_grid.zoom.GetX(), m_grid.origin.GetX(), m_fGridTimeScale, m_grid.rect.left(), m_grid.rect.right() + 1);
- //////////////////////////////////////////////////////////////////////////
- {
- const QPen pen0(QColor(110, 100, 100), 2);
- const QPoint p = WorldToClient(Vec2(0, 0));
- painter->setPen(pen0);
- /// Draw X axis.
- painter->drawLine(m_rcSpline.left(), p.y(), m_rcSpline.right() + 1, p.y());
- // Draw Y Axis.
- if (p.x() > m_rcSpline.left() && p.y() < m_rcSpline.right() + 1)
- {
- painter->drawLine(p.x(), m_rcSpline.top(), p.x(), m_rcSpline.bottom() + 1);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- painter->setPen(pOldPen);
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::DrawSpline(QPainter* painter, SSplineInfo& splineInfo, float startTime, float endTime)
- {
- const QPen pOldPen = painter->pen();
- //////////////////////////////////////////////////////////////////////////
- ISplineInterpolator* pSpline = splineInfo.pSpline;
- ISplineInterpolator* pDetailSpline = splineInfo.pDetailSpline;
- if (!pSpline)
- {
- return;
- }
- int nTotalNumberOfDimensions(0);
- int nCurrentDimension(0);
- int left = static_cast<int>(TimeToXOfs(startTime));//rcClip.left;
- int right = static_cast<int>(TimeToXOfs(endTime));//rcClip.right;
- QPoint p0 = TimeToPoint(pSpline->GetKeyTime(0), pSpline);
- QPoint p1 = TimeToPoint(pSpline->GetKeyTime(pSpline->GetKeyCount() - 1), pSpline);
- nTotalNumberOfDimensions = pSpline->GetNumDimensions();
- for (nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
- {
- QColor splineColor = EDIT_SPLINE_COLOR;
- splineColor = splineInfo.anColorArray[nCurrentDimension];
- const QPen pen(splineColor, 2);
- if (p0.x() > left && !pDetailSpline)
- {
- QPen alternatePen(splineColor, 1, Qt::DotLine);
- alternatePen.setCosmetic(true);
- painter->setPen(alternatePen);
- painter->drawLine(m_rcSpline.left(), p0.y(), p0.x(), p0.y());
- left = p0.x();
- }
- if (p1.x() < right && !pDetailSpline)
- {
- QPen alternatePen(splineColor, 1, Qt::DotLine);
- alternatePen.setCosmetic(true);
- painter->setPen(alternatePen);
- painter->drawLine(p1.x(), p1.y(), m_rcSpline.right() + 1, p1.y());
- right = p1.x();
- }
- painter->setPen(pen);
- float gradient = 0.0f;
- int pointsInLine = -1;
- QPoint lineStart;
- QPainterPath path;
- for (int x = left; x <= right; x++)
- {
- float time = XOfsToTime(x);
- ISplineInterpolator::ValueType value;
- ISplineInterpolator::ZeroValue(value);
- pSpline->Interpolate(time, value);
- if (pDetailSpline)
- {
- ISplineInterpolator::ValueType value2;
- ISplineInterpolator::ZeroValue(value2);
- pDetailSpline->Interpolate(time, value2);
- value[nCurrentDimension] = value[nCurrentDimension] + value2[nCurrentDimension];
- }
- QPoint pt = WorldToClient(Vec2(time, value[nCurrentDimension]));
- if ((x == right && pointsInLine >= 0) || (pointsInLine > 0 && fabs(lineStart.y() + gradient * (pt.x() - lineStart.x()) - pt.y()) > 1.0f))
- {
- lineStart = QPoint(pt.x() - 1, static_cast<int>(lineStart.y() + gradient * (pt.x() - 1 - lineStart.x())));
- path.lineTo(lineStart);
- gradient = float(pt.y() - lineStart.y()) / (pt.x() - lineStart.x());
- pointsInLine = 1;
- }
- else if ((x == right && pointsInLine >= 0) || (pointsInLine > 0 && fabs(lineStart.y() + gradient * (pt.x() - lineStart.x()) - pt.y()) == 1.0f))
- {
- lineStart = pt;
- path.lineTo(lineStart);
- gradient = 0.0f;
- pointsInLine = 0;
- }
- else if (pointsInLine > 0)
- {
- ++pointsInLine;
- }
- else if (pointsInLine == 0)
- {
- gradient = float(pt.y() - lineStart.y()) / (pt.x() - lineStart.x());
- ++pointsInLine;
- }
- else
- {
- path.moveTo(pt);
- lineStart = pt;
- ++pointsInLine;
- gradient = 0.0f;
- }
- }
- painter->drawPath(path);
- // Put back the old objects
- painter->setPen(pOldPen);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::DrawKeys(QPainter* painter, int splineIndex, [[maybe_unused]] float startTime, float endTime)
- {
- SSplineInfo& splineInfo = m_splines[splineIndex];
- ISplineInterpolator* pSpline = splineInfo.pSpline;
- ISplineInterpolator* pDetailSpline = splineInfo.pDetailSpline;
- if (!pSpline)
- {
- return;
- }
- // create and select a white pen - nope, black
- const QPen pOldPen = painter->pen();
- painter->setPen(Qt::black);
- int i;
- int nTotalNumberOfDimensions(0);
- int nCurrentDimension(0);
- nTotalNumberOfDimensions = pSpline->GetNumDimensions();
- for (nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
- {
- // Why is this here? Not even god knows...
- //for (i = 0; i < pSpline->GetKeyCount() && pSpline->GetKeyTime(i) < startTime; ++i);
- QPoint lastKeyPt;
- int numKeys = pSpline->GetKeyCount();
- for (i = 0; i < numKeys; i++)
- {
- float time = pSpline->GetKeyTime(i);
- if (time >= endTime)
- {
- break;
- }
- ISplineInterpolator::ValueType value;
- ISplineInterpolator::ZeroValue(value);
- pSpline->Interpolate(time, value);
- if (pDetailSpline)
- {
- ISplineInterpolator::ValueType value2;
- ISplineInterpolator::ZeroValue(value2);
- pDetailSpline->Interpolate(time, value2);
- value[nCurrentDimension] = value[nCurrentDimension] + value2[nCurrentDimension];
- }
- QPoint pt = WorldToClient(Vec2(time, value[nCurrentDimension]));
- ;
- if (pt.x() < m_rcSpline.left())
- {
- continue;
- }
- if (i > 0 && (pt - lastKeyPt).manhattanLength() < 4)
- {
- continue;
- }
- QColor clr(220, 220, 0);
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- {
- clr = Qt::red;
- DrawTangentHandle(painter, splineIndex, i, nCurrentDimension);
- }
- const QBrush brush(clr);
- const QBrush pOldBrush = painter->brush();
- painter->setBrush(brush);
- // Draw this key.
- painter->drawRect(QRect(QPoint(pt.x() - m_nKeyDrawRadius, pt.y() - m_nKeyDrawRadius), QPoint(pt.x() + m_nKeyDrawRadius - 1, pt.y() + m_nKeyDrawRadius - 1)));
- lastKeyPt = pt;
- painter->setBrush(pOldBrush);
- }
- }
- painter->setPen(pOldPen);
- }
- bool AbstractSplineWidget::GetTangentHandlePts(QPoint&, QPoint&, QPoint&, int, int, int)
- {
- return false;
- }
- void SplineWidget::DrawTangentHandle(QPainter* painter, int nSpline, int nKey, int nDimension)
- {
- // create and select a white pen
- const QPen pOldPen = painter->pen();
- painter->setPen(QColor(96, 96, 96));
- // Draw in-tangent & out-tangent lines.
- QPoint a, b, pt;
- if (GetTangentHandlePts(a, pt, b, nSpline, nKey, nDimension))
- {
- painter->drawLine(a, pt);
- painter->drawLine(pt, b);
- // Draw end-effectors.
- const QBrush pOldBrush = painter->brush();
- painter->setBrush(QColor(0, 220, 0));
- painter->drawRect(QRect(QPoint(a.x() - m_nKeyDrawRadius, a.y() - m_nKeyDrawRadius), QPoint(a.x() + m_nKeyDrawRadius - 1, a.y() + m_nKeyDrawRadius - 1)));
- painter->drawRect(QRect(QPoint(b.x() - m_nKeyDrawRadius, b.y() - m_nKeyDrawRadius), QPoint(b.x() + m_nKeyDrawRadius - 1, b.y() + m_nKeyDrawRadius - 1)));
- painter->setBrush(pOldBrush);
- }
- painter->setPen(pOldPen);
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::DrawTimeMarker(QPainter* painter)
- {
- const QPen pOldPen = painter->pen();
- painter->setPen(QColor(255, 0, 255));
- float x = TimeToXOfs(m_fTimeMarker);
- if (x >= m_rcSpline.left() && x <= m_rcSpline.right() + 1)
- {
- painter->drawLine(static_cast<int>(x), m_rcSpline.top(), static_cast<int>(x), m_rcSpline.bottom() + 1);
- }
- painter->setPen(pOldPen);
- }
- /////////////////////////////////////////////////////////////////////////////
- //Mouse Message Handlers
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::mousePressEvent(QMouseEvent* event)
- {
- switch (event->button())
- {
- case Qt::LeftButton:
- OnLButtonDown(event->pos(), event->modifiers());
- break;
- case Qt::MiddleButton:
- OnMButtonDown(event->pos(), event->modifiers());
- break;
- case Qt::RightButton:
- OnRButtonDown(event->pos(), event->modifiers());
- break;
- }
- }
- void SplineWidget::mouseReleaseEvent(QMouseEvent* event)
- {
- switch (event->button())
- {
- case Qt::LeftButton:
- OnLButtonUp(event->pos(), event->modifiers());
- break;
- case Qt::MiddleButton:
- OnMButtonUp(event->pos(), event->modifiers());
- break;
- }
- }
- void SplineWidget::OnLButtonDown(const QPoint& point, Qt::KeyboardModifiers modifiers)
- {
- m_pCurrentUndo = nullptr;
- if (m_bEditLock)
- {
- return;
- }
- m_boLeftMouseButtonDown = true;
- if (m_editMode == TrackingMode)
- {
- return;
- }
- SendNotifyEvent(NM_CLICK);
- m_cMouseDownPos = point;
- ISplineInterpolator* pSpline = HitSpline(m_cMouseDownPos);
- // Get control key status.
- bool bCtrlClick = modifiers & Qt::ControlModifier;
- switch (m_hitCode)
- {
- case HIT_KEY:
- {
- {
- CUndo undo("Select Spline Key");
- StoreUndo();
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- bool bHitSelection = IsKeySelected(m_pHitSpline, m_nHitKeyIndex, m_nHitDimension);
- bool bAddSelect = bCtrlClick;
- if (!bAddSelect && !bHitSelection)
- {
- ClearSelection();
- }
- SelectKey(pSpline, m_nHitKeyIndex, m_nHitDimension, true);
- SendNotifyEvent(SPLN_CHANGE);
- if (m_pCurrentUndo && !m_pCurrentUndo->IsSelectionChanged())
- {
- undo.Cancel();
- }
- m_pCurrentUndo = nullptr;
- }
- GetIEditor()->BeginUndo();
- StartTracking(bCtrlClick);
- }
- break;
- case HIT_TANGENT_HANDLE:
- {
- {
- CUndo undo("Select Tangent Handle");
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- ClearSelection();
- SelectKey(pSpline, m_nHitKeyIndex, m_nHitDimension, true);
- SendNotifyEvent(SPLN_CHANGE);
- }
- StartTracking(false);
- }
- break;
- case HIT_SPLINE:
- {
- if (GetNumSelected() > 0)
- {
- StartTracking(bCtrlClick);
- }
- }
- break;
- case HIT_TIMEMARKER:
- {
- SendNotifyEvent(SPLN_TIME_START_CHANGE);
- m_editMode = TimeMarkerMode;
- grabMouse();
- }
- break;
- case HIT_NOTHING:
- {
- if (m_rcSpline.contains(point))
- {
- GetIEditor()->BeginUndo();
- StoreUndo();
- m_rcSelect = QRect();
- m_rubberBand->setVisible(false);
- m_editMode = SelectMode;
- grabMouse();
- }
- }
- break;
- }
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::OnRButtonDown(const QPoint&, Qt::KeyboardModifiers)
- {
- m_pCurrentUndo = nullptr;
- SendNotifyEvent(NM_RCLICK);
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::OnMButtonDown(const QPoint& point, Qt::KeyboardModifiers modifiers)
- {
- m_pCurrentUndo = nullptr;
- bool bShiftClick = modifiers & Qt::ShiftModifier;
- if (m_editMode == NothingMode)
- {
- if (bShiftClick)
- {
- m_editMode = ZoomMode;
- setCursor(Qt::SizeAllCursor);
- }
- else
- {
- setCursor(Qt::SizeAllCursor);
- m_editMode = ScrollMode;
- }
- m_cMouseDownPos = point;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::OnMButtonUp(const QPoint&, Qt::KeyboardModifiers)
- {
- if (m_editMode == ScrollMode || m_editMode == ZoomMode)
- {
- m_editMode = NothingMode;
- return;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::mouseDoubleClickEvent(QMouseEvent* event)
- {
- if (event->button() != Qt::LeftButton)
- {
- return;
- }
- const QPoint point = event->pos();
- m_pCurrentUndo = nullptr;
- if (m_bEditLock)
- {
- return;
- }
- switch (m_hitCode)
- {
- case HIT_SPLINE:
- {
- if (m_pHitSpline)
- {
- InsertKey(m_pHitSpline, m_pHitDetailSpline, point);
- }
- update();
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- }
- break;
- case HIT_KEY:
- {
- RemoveKey(m_pHitSpline, m_nHitKeyIndex);
- }
- break;
- case HIT_TANGENT_HANDLE:
- {
- if (m_bHitIncomingHandle)
- {
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, 0);
- }
- else
- {
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, 0);
- }
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::mouseMoveEvent(QMouseEvent* event)
- {
- const QPoint point = event->pos();
- switch (HitTest(point))
- {
- case HIT_SPLINE:
- setCursor(CMFCUtils::LoadCursor(IDC_ARRWHITE));
- break;
- case HIT_KEY:
- case HIT_TANGENT_HANDLE:
- setCursor(CMFCUtils::LoadCursor(IDC_ARRBLCK));
- break;
- default:
- setCursor(Qt::ArrowCursor);
- break;
- }
- if (m_pHitSpline && m_nHitKeyIndex >= 0)
- {
- float time = m_pHitSpline->GetKeyTime(m_nHitKeyIndex);
- ISplineInterpolator::ValueType afValue;
- m_pHitSpline->GetKeyValue(m_nHitKeyIndex, afValue);
- 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);
- m_tooltipText = tipText;
- if (m_lastToolTipPos != point)
- {
- m_lastToolTipPos = point;
- QToolTip::showText(event->globalPos(), tipText);
- }
- }
- else if (m_editMode != TrackingMode)
- {
- if (!m_tooltipText.isEmpty())
- {
- QToolTip::hideText();
- }
- }
- if (m_bEditLock)
- {
- return;
- }
- m_cMousePos = event->pos();
- if (m_editMode == SelectMode)
- {
- setCursor(Qt::BlankCursor);
- QRect rc(QPoint(m_cMouseDownPos.x(), m_cMouseDownPos.y()), point - QPoint(1, 1));
- rc = rc.normalized().intersected(m_rcSpline);
- m_rcSelect = rc;
- m_rubberBand->setGeometry(m_rcSelect);
- m_rubberBand->setVisible(true);
- // Trigger a repaint so that the spline lines will be drawn correctly,
- // or else they end up being chopped up if you drag the selection area
- // over them
- update();
- }
- if (m_editMode == TimeMarkerMode)
- {
- setCursor(Qt::BlankCursor);
- SetTimeMarker(XOfsToTime(event->x()));
- SendNotifyEvent(SPLN_TIME_CHANGE);
- }
- if (m_boLeftMouseButtonDown)
- {
- if (m_editMode == TrackingMode && event->pos() != m_cMouseDownPos)
- {
- m_startedDragging = true;
- GetIEditor()->RestoreUndo();
- m_pCurrentUndo = nullptr;
- StoreUndo();
- bool bAltClick = event->modifiers() & Qt::AltModifier;
- Vec2 v0 = ClientToWorld(m_cMouseDownPos);
- Vec2 v1 = ClientToWorld(event->pos());
- if (bAltClick)
- {
- TimeScaleKeys(m_fTimeMarker, v0.x, v1.x);
- }
- else if (m_controlAmplitude)
- {
- ScaleAmplitudeKeys(v0.x, v0.y, v1.y - v0.y);
- }
- else
- {
- MoveSelectedKeys(v1 - v0, m_copyKeys);
- }
- }
- }
- switch (m_editMode)
- {
- case ScrollMode:
- {
- // Set the new scrolled coordinates
- float ofsx = m_grid.origin.GetX() - (event->x() - m_cMouseDownPos.x()) / m_grid.zoom.GetX();
- float ofsy = m_grid.origin.GetY() + (event->y() - m_cMouseDownPos.y()) / m_grid.zoom.GetY();
- SetScrollOffset(Vec2(ofsx, ofsy));
- m_cMouseDownPos = event->pos();
- }
- break;
- case ZoomMode:
- {
- float ofsx = (event->x() - m_cMouseDownPos.x()) * 0.01f;
- float ofsy = (event->y() - m_cMouseDownPos.y()) * 0.01f;
- AZ::Vector2 z = m_grid.zoom;
- if (ofsx != 0)
- {
- z.SetX(max(z.GetX() * (1.0f + ofsx), 0.001f));
- }
- if (ofsy != 0)
- {
- z.SetY(max(z.GetY() * (1.0f + ofsy), 0.001f));
- }
- SetZoom(Vec2(z.GetX(), z.GetY()), m_cMouseDownPos);
- m_cMouseDownPos = event->pos();
- }
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::UpdateKeyTimes() const
- {
- if (!m_bKeyTimesDirty)
- {
- return;
- }
- std::vector<float> selectedKeyTimes;
- selectedKeyTimes.reserve(m_keyTimes.size());
- for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
- {
- if ((*it).selected)
- {
- selectedKeyTimes.push_back((*it).time);
- }
- }
- std::sort(selectedKeyTimes.begin(), selectedKeyTimes.end());
- m_keyTimes.clear();
- for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
- {
- float value = pSpline->GetKeyTime(keyIndex);
- int lower = 0;
- int upper = int(m_keyTimes.size());
- while (lower < upper - 1)
- {
- int mid = ((lower + upper) >> 1);
- ((m_keyTimes[mid].time >= value) ? upper : lower) = mid;
- }
- if ((lower >= int(m_keyTimes.size()) || fabsf(m_keyTimes[lower].time - value) > threshold) &&
- (upper >= int(m_keyTimes.size()) || fabsf(m_keyTimes[upper].time - value) > threshold))
- {
- m_keyTimes.insert(m_keyTimes.begin() + upper, KeyTime(value, 0));
- }
- }
- }
- for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
- {
- (*it).count = (m_pSplineSet ? m_pSplineSet->GetKeyCountAtTime((*it).time, threshold) : 0);
- }
- std::vector<float>::iterator itSelected = selectedKeyTimes.begin(), endSelected = selectedKeyTimes.end();
- for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
- {
- const float thisThreshold = 0.01f;
- for (; itSelected != endSelected && (*itSelected) < (*it).time - thisThreshold; ++itSelected)
- {
- ;
- }
- if (itSelected != endSelected && fabsf((*itSelected) - (*it).time) < thisThreshold)
- {
- (*it).selected = true;
- }
- }
- m_totalSplineCount = (m_pSplineSet ? m_pSplineSet->GetSplineCount() : 0);
- m_bKeyTimesDirty = false;
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::OnLButtonUp([[maybe_unused]] const QPoint& point, Qt::KeyboardModifiers modifiers)
- {
- if (m_bEditLock)
- {
- return;
- }
- m_boLeftMouseButtonDown = false;
- if (m_editMode == TrackingMode)
- {
- StopTracking();
- if (!m_startedDragging)
- {
- HitSpline(m_cMouseDownPos);
- }
- }
- if (m_editMode == SelectMode)
- {
- // Get control key status.
- bool bAltClick = modifiers & Qt::AltModifier;
- bool bCtrlClick = modifiers & Qt::ControlModifier;
- bool bAddSelect = bCtrlClick;
- bool bUnselect = bAltClick;
- // Stop tracking first or else the clear selection will end up being
- // undone
- StopTracking();
- if (!bAddSelect && !bUnselect)
- {
- ClearSelection();
- }
- SelectRectangle(m_rcSelect, !bUnselect);
- m_rcSelect = QRect();
- m_rubberBand->setVisible(false);
- setCursor(Qt::ArrowCursor);
- }
- if (m_editMode == TimeMarkerMode)
- {
- m_editMode = NothingMode;
- releaseMouse();
- SendNotifyEvent(SPLN_TIME_END_CHANGE);
- }
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- m_tooltipText = "";
- update();
- m_editMode = NothingMode;
- }
- /////////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SelectKey(ISplineInterpolator* pSpline, int nKey, int nDimension, bool bSelect)
- {
- if (nKey >= 0)
- {
- pSpline->SelectKeyAtDimension(nKey, nDimension, bSelect);
- SendNotifyEvent(SPLN_KEY_SELECTION_CHANGE);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- bool AbstractSplineWidget::IsKeySelected(ISplineInterpolator* pSpline, int nKey, int nHitDimension) const
- {
- if (pSpline && nKey >= 0)
- {
- return (pSpline->IsKeySelectedAtDimension(nKey, nHitDimension));
- }
- return false;
- }
- //////////////////////////////////////////////////////////////////////////
- int AbstractSplineWidget::GetNumSelected()
- {
- int nSelected = 0;
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- if (ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline)
- {
- for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- {
- nSelected++;
- }
- }
- }
- }
- }
- return nSelected;
- }
- /////////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetSplineSet(ISplineSet* pSplineSet)
- {
- m_pSplineSet = pSplineSet;
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::wheelEvent(QWheelEvent* event)
- {
- int zDelta = event->angleDelta().y();
- if (zDelta == 0)
- {
- return;
- }
- AZ::Vector2 z = m_grid.zoom;
- float scale = 1.2f * fabs(zDelta / 120.0f);
- if (zDelta > 0)
- {
- z *= scale;
- }
- else
- {
- z /= scale;
- }
- SetZoom(Vec2(z.GetX(), z.GetY()), m_cMousePos);
- event->accept();
- }
- void SplineWidget::keyPressEvent(QKeyEvent* e)
- {
- bool bProcessed = false;
- switch (e->key())
- {
- case Qt::Key_Delete:
- {
- RemoveSelectedKeys();
- RemoveSelectedKeyTimes();
- bProcessed = true;
- }
- break;
- case Qt::Key_Up:
- {
- CUndo undo("Move Spline Key(s)");
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- MoveSelectedKeys(ClientToWorld(QPoint(0, -1)), false);
- bProcessed = true;
- }
- break;
- case Qt::Key_Down:
- {
- CUndo undo("Move Spline Key(s)");
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- MoveSelectedKeys(ClientToWorld(QPoint(0, 1)), false);
- bProcessed = true;
- }
- break;
- case Qt::Key_Left:
- {
- CUndo undo("Move Spline Key(s)");
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- MoveSelectedKeys(ClientToWorld(QPoint(-1, 0)), false);
- bProcessed = true;
- }
- break;
- case Qt::Key_Right:
- {
- CUndo undo("Move Spline Key(s)");
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- MoveSelectedKeys(ClientToWorld(QPoint(1, 0)), false);
- bProcessed = true;
- }
- break;
- default:
- break; //do nothing
- }
- bool bCtrl = e->modifiers() & Qt::ControlModifier;
- if (e->key() == Qt::Key_C && bCtrl)
- {
- CopyKeys();
- return;
- }
- if (e->key() == Qt::Key_V && bCtrl)
- {
- PasteKeys();
- return;
- }
- if (e->key() == Qt::Key_Z && bCtrl)
- {
- GetIEditor()->Undo();
- return;
- }
- if (e->key() == Qt::Key_Y && bCtrl)
- {
- GetIEditor()->Redo();
- return;
- }
- if (!bProcessed)
- {
- QWidget::keyPressEvent(e);
- }
- }
- bool SplineWidget::event(QEvent* e)
- {
- // Shortcut override events are sent to allow a widget to say that it wants to get the matching
- // keypress event, and for it not to be treated as a shortcut by the shortcut/QAction system.
- // So we need to handle it here, return true and mark the event as accepted, if it's a key combination we care
- // about
- if (e->type() == QEvent::ShortcutOverride)
- {
- QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
- bool filterOutEvent = false;
- switch (keyEvent->key())
- {
- case Qt::Key_Delete:
- case Qt::Key_Up:
- case Qt::Key_Down:
- case Qt::Key_Left:
- case Qt::Key_Right:
- filterOutEvent = true;
- break;
- case Qt::Key_C:
- case Qt::Key_V:
- case Qt::Key_Z:
- case Qt::Key_Y:
- filterOutEvent = (keyEvent->modifiers() & Qt::ControlModifier) != 0;
- break;
- default:
- break; //do nothing
- }
- if (filterOutEvent)
- {
- e->accept();
- return true;
- }
- }
- return QWidget::event(e);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetHorizontalExtent([[maybe_unused]] int min, [[maybe_unused]] int max)
- {
- /*
- m_scrollMin.x = min;
- m_scrollMax.x = max;
- int width = max - min;
- int nPage = m_rcClient.Width()/2;
- int sx = width - nPage + m_rcSpline.left;
- SCROLLINFO si;
- ZeroStruct(si);
- si.cbSize = sizeof(si);
- si.fMask = SIF_ALL;
- si.nMin = m_scrollMin.x;
- si.nMax = m_scrollMax.x - nPage + m_rcSpline.left;
- si.nPage = m_rcClient.Width()/2;
- si.nPos = m_scrollOffset.x;
- //si.nPage = max(0,m_rcClient.Width() - m_leftOffset*2);
- //si.nPage = 1;
- //si.nPage = 1;
- SetScrollInfo( SB_HORZ,&si,true );
- */
- }
- //////////////////////////////////////////////////////////////////////////
- ISplineInterpolator* AbstractSplineWidget::HitSpline(const QPoint& point)
- {
- if (HitTest(point) != HIT_NOTHING)
- {
- return m_pHitSpline;
- }
- return nullptr;
- }
- //////////////////////////////////////////////////////////////////////////////
- AbstractSplineWidget::EHitCode AbstractSplineWidget::HitTest(const QPoint& point)
- {
- float time, val;
- int nTotalNumberOfDimensions(0);
- int nCurrentDimension(0);
- PointToTimeValue(point, time, val);
- m_hitCode = HIT_NOTHING;
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- m_nHitDimension = -1;
- m_bHitIncomingHandle = true;
- if (abs(point.x() - TimeToXOfs(m_fTimeMarker)) < 4)
- {
- m_hitCode = HIT_TIMEMARKER;
- }
- // For each Spline...
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- ISplineInterpolator* pDetailSpline = m_splines[splineIndex].pDetailSpline;
- // If there is no spline, you can't hit nor a spline nor a key... isn't that logical?
- if (!pSpline)
- {
- return m_hitCode;
- }
- ISplineInterpolator::ValueType stSplineValue;
- ISplineInterpolator::ValueType stDetailSplineValue;
- ISplineInterpolator::ZeroValue(stSplineValue);
- ISplineInterpolator::ZeroValue(stDetailSplineValue);
- pSpline->Interpolate(time, stSplineValue);
- if (pDetailSpline)
- {
- pDetailSpline->Interpolate(time, stDetailSplineValue);
- }
- // For each dimension...
- nTotalNumberOfDimensions = pSpline->GetNumDimensions();
- for (nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
- {
- if (pDetailSpline)
- {
- stSplineValue[nCurrentDimension] = stSplineValue[nCurrentDimension] + stDetailSplineValue[nCurrentDimension];
- }
- for (int i = 0; i < pSpline->GetKeyCount(); i++)
- {
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- // Check tangent handles first.
- {
- QPoint incomingHandlePt, outgoingHandlePt, pt;
- if (GetTangentHandlePts(incomingHandlePt, pt, outgoingHandlePt, static_cast<int>(splineIndex), static_cast<int>(i), nCurrentDimension))
- {
- // For the incoming handle
- if (abs(incomingHandlePt.x() - point.x()) < 4 && abs(incomingHandlePt.y() - point.y()) < 4)
- {
- m_hitCode = HIT_TANGENT_HANDLE;
- m_pHitSpline = pSpline;
- m_pHitDetailSpline = m_splines[splineIndex].pDetailSpline;
- m_nHitKeyIndex = i;
- m_nHitDimension = nCurrentDimension;
- m_bHitIncomingHandle = true;
- return m_hitCode;
- }
- // For the outgoing handle
- else if (abs(outgoingHandlePt.x() - point.x()) < 4 && abs(outgoingHandlePt.y() - point.y()) < 4)
- {
- m_hitCode = HIT_TANGENT_HANDLE;
- m_pHitSpline = pSpline;
- m_pHitDetailSpline = m_splines[splineIndex].pDetailSpline;
- m_nHitKeyIndex = i;
- m_nHitDimension = nCurrentDimension;
- m_bHitIncomingHandle = false;
- return m_hitCode;
- }
- }
- }
- }
- QPoint splinePt = WorldToClient(Vec2(time, stSplineValue[nCurrentDimension]));
- bool bSplineHit = abs(splinePt.x() - point.x()) < 4 && abs(splinePt.y() - point.y()) < 4;
- if (bSplineHit)
- {
- m_hitCode = HIT_SPLINE;
- m_pHitSpline = pSpline;
- m_pHitDetailSpline = m_splines[splineIndex].pDetailSpline;
- for (int i = 0; i < pSpline->GetKeyCount(); i++)
- {
- QPoint splinePt2 = TimeToPoint(pSpline->GetKeyTime(i), pSpline);
- if (abs(splinePt2.x() - point.x()) < 4 /* && abs(splinePt.y()-point.y()) < 4*/)
- {
- m_nHitKeyIndex = i;
- m_nHitDimension = nCurrentDimension;
- m_hitCode = HIT_KEY;
- return m_hitCode;
- }
- }
- }
- }
- }
- return m_hitCode;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::StartTracking(bool copyKeys)
- {
- m_copyKeys = copyKeys;
- m_startedDragging = false;
- m_editMode = TrackingMode;
- captureMouseImpl();
- GetIEditor()->BeginUndo();
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- setCursorImpl(IDC_ARRBLCKCROSS);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::StopTracking()
- {
- if ((m_editMode == TrackingMode) && (m_cMousePos != m_cMouseDownPos))
- {
- GetIEditor()->AcceptUndo("Spline Move");
- }
- else if (m_pCurrentUndo && (m_cMousePos == m_cMouseDownPos))
- {
- if (m_editMode == SelectMode)
- {
- if (m_pCurrentUndo->IsSelectionChanged())
- {
- GetIEditor()->AcceptUndo("Key Selection");
- }
- else
- {
- GetIEditor()->CancelUndo();
- }
- }
- else if ((m_editMode == TrackingMode))
- {
- GetIEditor()->CancelUndo();
- }
- }
- else
- {
- GetIEditor()->CancelUndo();
- }
- // Undo has been accepted or cancelled, so clear the m_pCurrentUndo pointer.
- m_pCurrentUndo = nullptr;
- m_editMode = NothingMode;
- releaseMouseImpl();
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ScaleAmplitudeKeys(float time, float startValue, float offset)
- {
- //TODO: Test it in the facial animation pane and fix it...
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- m_nHitDimension = -1;
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- // Find the range of keys to process.
- int keyCount = (pSpline ? pSpline->GetKeyCount() : 0);
- int firstKeyIndex = keyCount;
- int lastKeyIndex = -1;
- for (int i = 0; i < keyCount; ++i)
- {
- if (pSpline->IsKeySelectedAtAnyDimension(i))
- {
- firstKeyIndex = min(firstKeyIndex, i);
- lastKeyIndex = max(lastKeyIndex, i);
- }
- }
- // Find the parameters of a line between the start and end points. This will form the centre line
- // around which the amplitude of the keys will be scaled.
- float rangeStartTime = (firstKeyIndex >= 0 && pSpline ? pSpline->GetKeyTime(firstKeyIndex) : 0.0f);
- float rangeEndTime = (lastKeyIndex >= 0 && pSpline ? pSpline->GetKeyTime(lastKeyIndex) : 0.0f);
- float rangeLength = max(0.01f, rangeEndTime - rangeStartTime);
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- ISplineInterpolator::ValueType afRangeStartValue;
- if (firstKeyIndex >= 0 && pSpline)
- {
- pSpline->GetKeyValue(firstKeyIndex, afRangeStartValue);
- }
- else
- {
- memset(afRangeStartValue, 0, sizeof(ISplineInterpolator::ValueType));
- }
- ISplineInterpolator::ValueType afRangeEndValue;
- if (lastKeyIndex >= 0 && pSpline)
- {
- pSpline->GetKeyValue(lastKeyIndex, afRangeEndValue);
- }
- else
- {
- memset(afRangeEndValue, 0, sizeof(ISplineInterpolator::ValueType));
- }
- float centreM = (afRangeEndValue[nCurrentDimension] - afRangeStartValue[nCurrentDimension]) / rangeLength;
- float centreC = afRangeStartValue[nCurrentDimension] - centreM * rangeStartTime;
- // Calculate the scale factor, based on how the mouse was dragged.
- float dragCentreValue = centreM * time + centreC;
- float dragCentreOffset = startValue - dragCentreValue;
- float offsetScale = (fabs(dragCentreOffset) > 0.001 ? (offset + dragCentreOffset) / dragCentreOffset : 1.0f);
- // Scale all the selected keys around this central line.
- for (int i = 0; i < keyCount; ++i)
- {
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- {
- float keyTime = (pSpline ? pSpline->GetKeyTime(i) : 0.0f);
- float centreValue = keyTime * centreM + centreC;
- ISplineInterpolator::ValueType afKeyValue;
- if (pSpline)
- {
- pSpline->GetKeyValue(i, afKeyValue);
- }
- else
- {
- memset(afKeyValue, 0, sizeof(ISplineInterpolator::ValueType));
- }
- float keyOffset = afKeyValue[nCurrentDimension] - centreValue;
- float newKeyOffset = keyOffset * offsetScale;
- if (pSpline)
- {
- afKeyValue[nCurrentDimension] = centreValue + newKeyOffset;
- pSpline->SetKeyValue(i, afKeyValue);
- }
- }
- }
- }
- }
- update();
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- SendNotifyEvent(SPLN_CHANGE);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::TimeScaleKeys(float time, float startTime, float endTime)
- {
- // Calculate the scaling parameters (ie t1 = t0 * M + C).
- float timeScaleM = 1.0f;
- if (fabsf(startTime - time) > MIN_TIME_EPSILON_FOR_SCALING)
- {
- timeScaleM = (endTime - time) / (startTime - time);
- }
- float timeScaleC = endTime - startTime * timeScaleM;
- // Loop through all keys that are selected.
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- float affectedRangeMin = FLT_MAX;
- float affectedRangeMax = -FLT_MAX;
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- int keyCount = pSpline->GetKeyCount();
- float keyRangeMin = FLT_MAX;
- float keyRangeMax = -FLT_MAX;
- for (int i = 0; i < keyCount; i++)
- {
- if (pSpline->IsKeySelectedAtAnyDimension(i))
- {
- float oldTime = pSpline->GetKeyTime(i);
- float t = SnapTime(oldTime * timeScaleM + timeScaleC);
- pSpline->SetKeyTime(i, SnapTimeToGridVertical(t));
- keyRangeMin = min(keyRangeMin, oldTime);
- keyRangeMin = min(keyRangeMin, t);
- keyRangeMax = max(keyRangeMax, oldTime);
- keyRangeMax = max(keyRangeMax, t);
- }
- }
- if (keyRangeMin <= keyRangeMax)
- {
- // Changes to a key's value affect spline up to two keys away.
- int lastMovedKey = 0;
- for (int keyIndex = 0; keyIndex < keyCount; ++keyIndex)
- {
- if (pSpline->GetKeyTime(keyIndex) <= keyRangeMax)
- {
- lastMovedKey = keyIndex + 1;
- }
- }
- int firstMovedKey = pSpline->GetKeyCount();
- for (int keyIndex = pSpline->GetKeyCount() - 1; keyIndex >= 0; --keyIndex)
- {
- if (pSpline->GetKeyTime(keyIndex) >= keyRangeMin)
- {
- firstMovedKey = keyIndex;
- }
- }
- int firstAffectedKey = max(0, firstMovedKey - 2);
- int lastAffectedKey = min(keyCount - 1, lastMovedKey + 2);
- affectedRangeMin = min(affectedRangeMin, (firstAffectedKey <= 0 ? m_timeRange.start : pSpline->GetKeyTime(firstAffectedKey)));
- affectedRangeMax = max(affectedRangeMax, (lastAffectedKey >= keyCount - 1 ? m_timeRange.end : pSpline->GetKeyTime(lastAffectedKey)));
- // Loop through all moved keys, checking whether there are multiple keys on the same frame.
- float lastKeyTime = -FLT_MAX;
- pSpline->Update();
- for (int keyIndex = 0, keys = pSpline->GetKeyCount(); keyIndex <= keys; )
- {
- float keyTime = pSpline->GetKeyTime(keyIndex);
- if (fabsf(keyTime - lastKeyTime) < m_fMinTimeEpsilon)
- {
- --keys;
- pSpline->RemoveKey(keyIndex);
- }
- else
- {
- ++keyIndex;
- lastKeyTime = keyTime;
- }
- }
- }
- }
- int rangeMin = static_cast<int>(TimeToXOfs(affectedRangeMin));
- int rangeMax = static_cast<int>(TimeToXOfs(affectedRangeMax));
- if (m_timeRange.start == affectedRangeMin)
- {
- rangeMin = m_rcSpline.left();
- }
- if (m_timeRange.end == affectedRangeMax)
- {
- rangeMax = m_rcSpline.right();
- }
- QRect invalidRect(QPoint(rangeMin - 3, m_rcSpline.top()), QPoint(rangeMax + 3, m_rcSpline.bottom() + 1) - QPoint(1, 1));
- update(invalidRect);
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- m_bKeyTimesDirty = true;
- SendNotifyEvent(SPLN_CHANGE);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ValueScaleKeys(float startValue, float endValue)
- {
- // Calculate the scaling parameters.
- float valueScale = 1.0f;
- if (fabsf(startValue) > MIN_TIME_EPSILON_FOR_SCALING)
- {
- valueScale = endValue / startValue;
- }
- // Loop through all keys that are selected.
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- m_nHitDimension = -1;
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- int keyCount = pSpline->GetKeyCount();
- for (int i = 0; i < keyCount; i++)
- {
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- {
- ISplineInterpolator::ValueType afValue;
- pSpline->GetKeyValue(i, afValue);
- afValue[nCurrentDimension] = SnapValue(afValue[nCurrentDimension] * valueScale);
- pSpline->SetKeyValue(i, afValue);
- }
- }
- }
- }
- update();
- SendNotifyEvent(SPLN_CHANGE);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::MoveSelectedKeys(Vec2 offset, bool copyKeys)
- {
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- m_nHitDimension = -1;
- if (copyKeys)
- {
- DuplicateSelectedKeys();
- }
- // For each spline...
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- int keyCount = pSpline->GetKeyCount();
- for (int i = 0; i < keyCount; i++)
- {
- float oldTime = pSpline->GetKeyTime(i);
- float t = SnapTime(oldTime + offset.x);
- if (pSpline->IsKeySelectedAtAnyDimension(i))
- {
- if (pSpline->FindKey(t, m_fMinTimeEpsilon) < 0)
- {
- pSpline->SetKeyTime(i, SnapTimeToGridVertical(t));
- }
- }
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- {
- ISplineInterpolator::ValueType afValue;
- pSpline->GetKeyValue(i, afValue);
- afValue[nCurrentDimension] = SnapValue(afValue[nCurrentDimension] + offset.y);
- pSpline->SetKeyValue(i, afValue);
- }
- }
- }
- }
- update();
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->update();
- }
- m_bKeyTimesDirty = true;
- SendNotifyEvent(SPLN_CHANGE);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveKey(ISplineInterpolator* pSpline, int nKey)
- {
- CUndo undo("Remove Spline Key");
- ConditionalStoreUndo();
- m_bKeyTimesDirty = true;
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- if (nKey != -1)
- {
- pSpline->RemoveKey(nKey);
- }
- SendNotifyEvent(SPLN_CHANGE);
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveSelectedKeys()
- {
- CUndo undo("Remove Spline Key");
- StoreUndo();
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int i = 0; i < (int)pSpline->GetKeyCount(); )
- {
- if (pSpline->IsKeySelectedAtAnyDimension(i))
- {
- pSpline->RemoveKey(i);
- }
- else
- {
- i++;
- }
- }
- }
- m_bKeyTimesDirty = true;
- SendNotifyEvent(SPLN_CHANGE);
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveSelectedKeyTimesImpl()
- {
- int numSelectedKeyTimes = 0;
- for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
- {
- if ((*it).selected)
- {
- ++numSelectedKeyTimes;
- }
- }
- if (numSelectedKeyTimes)
- {
- CUndo undo("Remove Spline Key");
- StoreUndo();
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- for (size_t splineIndex = 0, end = m_splines.size(); splineIndex < end; ++splineIndex)
- {
- std::vector<KeyTime>::iterator itTime = m_keyTimes.begin(), endTime = m_keyTimes.end();
- for (int keyIndex = 0, endIndex = m_splines[splineIndex].pSpline->GetKeyCount(); keyIndex < endIndex; )
- {
- const float thisThreshold = 0.01f;
- for (; itTime != endTime && (*itTime).time < m_splines[splineIndex].pSpline->GetKeyTime(keyIndex) - thisThreshold; ++itTime)
- {
- ;
- }
- if (itTime != endTime && fabsf((*itTime).time - m_splines[splineIndex].pSpline->GetKeyTime(keyIndex)) < thisThreshold && (*itTime).selected)
- {
- m_splines[splineIndex].pSpline->RemoveKey(keyIndex);
- }
- else
- {
- ++keyIndex;
- }
- }
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveSelectedKeyTimes()
- {
- RemoveSelectedKeyTimesImpl();
- m_bKeyTimesDirty = true;
- SendNotifyEvent(SPLN_CHANGE);
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RedrawWindowAroundMarker()
- {
- UpdateKeyTimes();
- std::vector<KeyTime>::iterator itKeyTime = std::lower_bound(m_keyTimes.begin(), m_keyTimes.end(), KeyTime(m_fTimeMarker, 0));
- size_t keyTimeIndex = (itKeyTime != m_keyTimes.end() ? itKeyTime - m_keyTimes.begin() : m_keyTimes.size());
- int redrawRangeStart = (keyTimeIndex >= 2 ? static_cast<int>(TimeToXOfs(m_keyTimes[keyTimeIndex - 2].time)) : m_rcSpline.left());
- int redrawRangeEnd = (keyTimeIndex < m_keyTimes.size() - 2 ? static_cast<int>(TimeToXOfs(m_keyTimes[keyTimeIndex + 2].time)) : m_rcSpline.right() + 1);
- QRect rc(QPoint(redrawRangeStart, m_rcSpline.top()), QPoint(redrawRangeEnd, m_rcSpline.bottom() + 1) - QPoint(1, 1));
- rc = rc.normalized().intersected(m_rcSpline);
- m_TimeUpdateRect = QRect(QPoint(1, 2), QSize(2, 2));
- update(rc);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SplinesChanged()
- {
- m_bKeyTimesDirty = true;
- UpdateKeyTimes();
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetControlAmplitude(bool controlAmplitude)
- {
- m_controlAmplitude = controlAmplitude;
- }
- //////////////////////////////////////////////////////////////////////////
- bool AbstractSplineWidget::GetControlAmplitude() const
- {
- return m_controlAmplitude;
- }
- float AbstractSplineWidget::SnapTimeToGridVertical(float time)
- {
- //float fSnapTime = int((time * m_fGridTimeScale) + 0.5f) * (1.0f / m_fGridTimeScale);
- float fSnapTime = time;
- return fSnapTime;
- }
- //////////////////////////////////////////////////////////////////////////
- int AbstractSplineWidget::InsertKey(ISplineInterpolator* pSpline, ISplineInterpolator* pDetailSpline, const QPoint& point)
- {
- CUndo undo("Spline Insert Key");
- StoreUndo();
- float time, val;
- PointToTimeValue(point, time, val);
- time = SnapTimeToGridVertical(time);
- int i;
- for (i = 0; i < pSpline->GetKeyCount(); i++)
- {
- // Skip if any key already have time that is very close.
- if (fabs(pSpline->GetKeyTime(i) - time) < m_fMinTimeEpsilon)
- {
- return i;
- }
- }
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- // The proper key value for a spline that has a detail spline is not what is shown in the control - we have
- // to remove the detail value to get back to the underlying spline value.
- if (pDetailSpline)
- {
- float offset = 0.0f;
- pDetailSpline->InterpolateFloat(time, offset);
- val -= offset;
- }
- ClearSelection();
- ISplineInterpolator::ValueType currValue;
- ISplineInterpolator::ZeroValue(currValue);
- pSpline->Interpolate(time, currValue);
- if (pSpline->GetNumDimensions() > 1)
- {
- }
- int nKey = pSpline->InsertKey(time, currValue); // TODO: Don't use FE specific snapping!
- if (m_defaultKeyTangentType != SPLINE_KEY_TANGENT_NONE)
- {
- SetKeyTangentType(pSpline, nKey, m_defaultKeyTangentType);
- }
- //int nKey = pSpline->InsertKeyFloat( time,val ); // TODO: Don't use FE specific snapping!
- SelectKey(pSpline, nKey, 0, true);
- update();
- m_bKeyTimesDirty = true;
- SendNotifyEvent(SPLN_CHANGE);
- return nKey;
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ClearSelection()
- {
- ConditionalStoreUndo();
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- pSpline->SelectKeyAllDimensions(i, false);
- }
- }
- ClearSelectedKeys();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SetTimeMarker(float fTime)
- {
- if (m_pTimelineCtrl)
- {
- m_pTimelineCtrl->SetTimeMarker(fTime);
- }
- if (fTime == m_fTimeMarker)
- {
- return;
- }
- //// Erase old first.
- //float x1 = TimeToXOfs(m_fTimeMarker);
- //float x2 = TimeToXOfs(fTime);
- //QRect rc(QPoint(x1, m_rcSpline.top()), QPoint(x2, m_rcSpline.bottom()));
- //rc = rc.normalized().adjusted(-3, 0, 3, 0);// .intersected(m_rcSpline);
- m_TimeUpdateRect = {};
- m_fTimeMarker = fTime;
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::StoreUndo()
- {
- if (CUndo::IsRecording() && !m_pCurrentUndo)
- {
- std::vector<ISplineInterpolator*> splines(m_splines.size());
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- splines[splineIndex] = m_splines[splineIndex].pSpline;
- }
- CUndo::Record(m_pCurrentUndo = CreateSplineCtrlUndoObject(splines));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ConditionalStoreUndo()
- {
- if (m_editMode == TrackingMode || m_editMode == SelectMode)
- {
- StoreUndo();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ClearSelectedKeys()
- {
- for (std::vector<KeyTime>::iterator it = m_keyTimes.begin(), end = m_keyTimes.end(); it != end; ++it)
- {
- (*it).selected = false;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- class CKeyCopyInfo
- {
- public:
- ISplineInterpolator::ValueType value;
- float time;
- int flags;
- ISplineInterpolator::ValueType tin, tout;
- };
- void AbstractSplineWidget::DuplicateSelectedKeys()
- {
- m_pHitSpline = nullptr;
- m_pHitDetailSpline = nullptr;
- m_nHitKeyIndex = -1;
- using KeysToAddContainer = std::vector<CKeyCopyInfo>;
- KeysToAddContainer keysToInsert;
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- keysToInsert.resize(0);
- for (int i = 0; i < pSpline->GetKeyCount(); i++)
- {
- // In this particular case, the dimension doesn't matter.
- if (pSpline->IsKeySelectedAtAnyDimension(i))
- {
- keysToInsert.resize(keysToInsert.size() + 1);
- CKeyCopyInfo& copyInfo = keysToInsert.back();
- copyInfo.time = pSpline->GetKeyTime(i);
- pSpline->GetKeyValue(i, copyInfo.value);
- pSpline->GetKeyTangents(i, copyInfo.tin, copyInfo.tout);
- copyInfo.flags = pSpline->GetKeyFlags(i);
- }
- }
- for (KeysToAddContainer::iterator keyToAdd = keysToInsert.begin(), endKeysToAdd = keysToInsert.end(); keyToAdd != endKeysToAdd; ++keyToAdd)
- {
- int keyIndex = pSpline->InsertKey(SnapTimeToGridVertical((*keyToAdd).time), (*keyToAdd).value);
- pSpline->SetKeyTangents(keyIndex, (*keyToAdd).tin, (*keyToAdd).tout);
- pSpline->SetKeyFlags(keyIndex, (*keyToAdd).flags & (~ESPLINE_KEY_UI_SELECTED_MASK));
- }
- }
- m_bKeyTimesDirty = true;
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ZeroAll()
- {
- GetIEditor()->BeginUndo();
- using SplineContainer = std::vector<ISplineInterpolator *>;
- SplineContainer splines;
- for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- int keyIndex = (pSpline ? pSpline->FindKey(m_fTimeMarker, 0.015f) : -1);
- if (pSpline && keyIndex >= 0)
- {
- splines.push_back(pSpline);
- }
- }
- CUndo::Record(CreateSplineCtrlUndoObject(splines));
- for (SplineContainer::iterator itSpline = splines.begin(); itSpline != splines.end(); ++itSpline)
- {
- int keyIndex = ((*itSpline) ? (*itSpline)->FindKey(m_fTimeMarker, 0.015f) : -1);
- if ((*itSpline) && keyIndex >= 0)
- {
- (*itSpline)->SetKeyValueFloat(keyIndex, 0.0f);
- }
- }
- GetIEditor()->AcceptUndo("Zero All");
- m_pCurrentUndo = nullptr;
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::KeyAll()
- {
- GetIEditor()->BeginUndo();
- using SplineContainer = std::vector<ISplineInterpolator *>;
- SplineContainer splines;
- for (int splineIndex = 0; splineIndex < int(m_splines.size()); ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- int keyIndex = (pSpline ? pSpline->FindKey(m_fTimeMarker, 0.015f) : -1);
- if (pSpline && keyIndex == -1)
- {
- splines.push_back(pSpline);
- }
- }
- CUndo::Record(CreateSplineCtrlUndoObject(splines));
- for (SplineContainer::iterator itSpline = splines.begin(); itSpline != splines.end(); ++itSpline)
- {
- float value = 0.0f;
- (*itSpline)->InterpolateFloat(m_fTimeMarker, value);
- int keyIndex = (*itSpline)->InsertKeyFloat(SnapTimeToGridVertical(m_fTimeMarker), value);
- if (m_defaultKeyTangentType != SPLINE_KEY_TANGENT_NONE)
- {
- SetKeyTangentType(*itSpline, keyIndex, m_defaultKeyTangentType);
- }
- }
- GetIEditor()->AcceptUndo("Key All");
- m_pCurrentUndo = nullptr;
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SelectAll()
- {
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- pSpline->SelectKeyAllDimensions(i, true);
- }
- }
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::SendNotifyEvent(const uint32_t nEvent)
- {
- if (nEvent == SPLN_BEFORE_CHANGE)
- {
- ConditionalStoreUndo();
- }
- switch (nEvent)
- {
- case SPLN_BEFORE_CHANGE:
- Q_EMIT beforeChange();
- break;
- case SPLN_CHANGE:
- Q_EMIT change();
- break;
- case SPLN_TIME_CHANGE:
- Q_EMIT timeChange();
- break;
- case SPLN_SCROLL_ZOOM:
- Q_EMIT scrollZoomRequested();
- break;
- case SPLN_KEY_SELECTION_CHANGE:
- Q_EMIT keySelectionChange();
- break;
- case NM_CLICK:
- Q_EMIT clicked();
- break;
- case NM_RCLICK:
- Q_EMIT rightClicked();
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void SplineWidget::SetTimelineCtrl(TimelineWidget* pTimelineCtrl)
- {
- m_pTimelineCtrl = pTimelineCtrl;
- if (m_pTimelineCtrl)
- {
- pTimelineCtrl->setParent(this);
- pTimelineCtrl->SetZoom(m_grid.zoom.GetX());
- pTimelineCtrl->SetOrigin(m_grid.origin.GetX());
- pTimelineCtrl->SetKeyTimeSet(this);
- pTimelineCtrl->update();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::AddSpline(ISplineInterpolator* pSpline, ISplineInterpolator* pDetailSpline, const QColor& color)
- {
- for (int i = 0; i < (int)m_splines.size(); i++)
- {
- if (m_splines[i].pSpline == pSpline)
- {
- return;
- }
- }
- SSplineInfo si;
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- si.anColorArray[nCurrentDimension] = color;
- }
- si.pSpline = pSpline;
- si.pDetailSpline = pDetailSpline;
- m_splines.push_back(si);
- m_bKeyTimesDirty = true;
- update();
- }
- void AbstractSplineWidget::AddSpline(ISplineInterpolator* pSpline, ISplineInterpolator* pDetailSpline, QColor anColorArray[4])
- {
- for (int i = 0; i < (int)m_splines.size(); i++)
- {
- if (m_splines[i].pSpline == pSpline)
- {
- return;
- }
- }
- SSplineInfo si;
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- si.anColorArray[nCurrentDimension] = anColorArray[nCurrentDimension];
- }
- si.pSpline = pSpline;
- si.pDetailSpline = pDetailSpline;
- m_splines.push_back(si);
- m_bKeyTimesDirty = true;
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveSpline(ISplineInterpolator* pSpline)
- {
- for (int i = 0; i < (int)m_splines.size(); i++)
- {
- if (m_splines[i].pSpline == pSpline)
- {
- m_splines.erase(m_splines.begin() + i);
- return;
- }
- }
- m_bKeyTimesDirty = true;
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveAllSplines()
- {
- m_splines.clear();
- m_bKeyTimesDirty = true;
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::SelectRectangle(const QRect& rc, bool bSelect)
- {
- ConditionalStoreUndo();
- ClearSelectedKeys();
- Vec2 vec0 = ClientToWorld(rc.topLeft());
- Vec2 vec1 = ClientToWorld(rc.bottomRight());
- float t0 = vec0.x;
- float t1 = vec1.x;
- float v0 = vec0.y;
- float v1 = vec1.y;
- if (v0 > v1)
- {
- std::swap(v0, v1);
- }
- if (t0 > t1)
- {
- std::swap(t0, t1);
- }
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- ISplineInterpolator* pDetailSpline = m_splines[splineIndex].pDetailSpline;
- for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- float t = pSpline->GetKeyTime(i);
- ISplineInterpolator::ValueType afValue;
- pSpline->GetKeyValue(i, afValue);
- int nTotalNumberOfDimensions(pSpline->GetNumDimensions());
- ISplineInterpolator::ValueType afDetailValue;
- if (pDetailSpline)
- {
- ISplineInterpolator::ZeroValue(afDetailValue);
- pDetailSpline->Interpolate(t, afDetailValue);
- }
- for (int nCurrentDimension = 0; nCurrentDimension < nTotalNumberOfDimensions; nCurrentDimension++)
- {
- if (pDetailSpline)
- {
- afValue[nCurrentDimension] = afValue[nCurrentDimension] + afDetailValue[nCurrentDimension];
- }
- if (t >= t0 && t <= t1 && afValue[nCurrentDimension] >= v0 && afValue[nCurrentDimension] <= v1)
- {
- pSpline->SelectKeyAtDimension(i, nCurrentDimension, bSelect);
- }
- }
- }
- }
- SendNotifyEvent(SPLN_CHANGE);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::CopyKeys()
- {
- // Copy selected keys.
- if (m_splines.empty() || GetNumSelected() == 0)
- {
- return;
- }
- XmlNodeRef rootNode = XmlHelpers::CreateXmlNode("SplineKeys");
- int i;
- float minTime = FLT_MAX;
- float maxTime = -FLT_MAX;
- ISplineInterpolator* pSpline = m_splines[0].pSpline;
- for (i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- if (!pSpline->IsKeySelectedAtAnyDimension(i))
- {
- continue;
- }
- float t = pSpline->GetKeyTime(i);
- if (t < minTime)
- {
- minTime = t;
- }
- if (t > maxTime)
- {
- maxTime = t;
- }
- }
- rootNode->setAttr("start", minTime);
- rootNode->setAttr("end", maxTime);
- for (i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- if (!pSpline->IsKeySelectedAtAnyDimension(i))
- {
- continue;
- }
- float t = pSpline->GetKeyTime(i); // Store offset time from copy/paste range.
- ISplineInterpolator::ValueType afValue;
- pSpline->GetKeyValue(i, afValue);
- float tin, tout;
- ISplineInterpolator::ValueType vtin, vtout;
- pSpline->GetKeyTangents(i, vtin, vtout);
- tin = vtin[0];
- tout = vtout[0];
- XmlNodeRef keyNode = rootNode->newChild("Key");
- keyNode->setAttr("time", t);
- keyNode->setAttr("flags", (int)pSpline->GetKeyFlags(i));
- keyNode->setAttr("in", tin);
- keyNode->setAttr("out", tout);
- for (int ii = 0; ii < pSpline->GetNumDimensions(); ++ii)
- {
- XmlNodeRef dimensionNode = keyNode->newChild("values");
- dimensionNode->setAttr("value", afValue[ii]);
- }
- }
- CClipboard clipboard(WidgetCast());
- clipboard.Put(rootNode);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::PasteKeys()
- {
- if (m_splines.empty() || GetNumSelected() == 0)
- {
- return;
- }
- ISplineInterpolator* pSpline = m_splines[0].pSpline;
- CClipboard clipboard(WidgetCast());
- if (clipboard.IsEmpty())
- {
- return;
- }
- XmlNodeRef rootNode = clipboard.Get();
- if (!rootNode)
- {
- return;
- }
- if (!rootNode->isTag("SplineKeys"))
- {
- return;
- }
- float minTime = 0;
- float maxTime = 0;
- rootNode->getAttr("start", minTime);
- rootNode->getAttr("end", maxTime);
- const QPoint point = mapFromGlobal(QCursor::pos());
- float fTime = XOfsToTime(point.x());
- float fTimeRange = (maxTime - minTime);
- CUndo undo("Paste Spline Keys");
- ConditionalStoreUndo();
- ClearSelection();
- int i;
- // Delete keys in range min to max time.
- for (i = 0; i < pSpline->GetKeyCount(); )
- {
- float t = pSpline->GetKeyTime(i);
- if (t >= fTime && t <= fTime + fTimeRange)
- {
- pSpline->RemoveKey(i);
- }
- else
- {
- i++;
- }
- }
- for (i = 0; i < rootNode->getChildCount(); i++)
- {
- XmlNodeRef keyNode = rootNode->getChild(i);
- float t = 0;
- float tin = 0;
- float tout = 0;
- int flags = 0;
- keyNode->getAttr("time", t);
- keyNode->getAttr("flags", flags);
- keyNode->getAttr("in", tin);
- keyNode->getAttr("out", tout);
- int nNumberOfChildXMLNodes(0);
- int nCurrentChildXMLNode(0);
- int nCurrentValue(0);
- ISplineInterpolator::ValueType afValue;
- nNumberOfChildXMLNodes = keyNode->getChildCount();
- for (nCurrentChildXMLNode = 0; nCurrentChildXMLNode < nNumberOfChildXMLNodes; ++nCurrentChildXMLNode)
- {
- XmlNodeRef rSubKeyNode = keyNode->getChild(nCurrentChildXMLNode);
- if (rSubKeyNode->isTag("values"))
- {
- rSubKeyNode->getAttr("value", afValue[nCurrentValue]);
- nCurrentValue++;
- }
- }
- int key = pSpline->InsertKey(SnapTimeToGridVertical(t - minTime + fTime), afValue);
- if (key >= 0)
- {
- pSpline->SelectKeyAllDimensions(key, true);
- ISplineInterpolator::ValueType vtin, vtout;
- vtin[0] = tin;
- vtout[0] = tout;
- pSpline->SetKeyTangents(key, vtin, vtout);
- }
- }
- m_bKeyTimesDirty = true;
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::ModifySelectedKeysFlags(int nRemoveFlags, int nAddFlags)
- {
- CUndo undo("Modify Spline Keys");
- StoreUndo();
- SendNotifyEvent(SPLN_BEFORE_CHANGE);
- for (size_t splineIndex = 0, splineCount = m_splines.size(); splineIndex < splineCount; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int i = 0; i < (int)pSpline->GetKeyCount(); i++)
- {
- // If the key is selected in any dimension...
- for (
- int nCurrentDimension = 0;
- nCurrentDimension < pSpline->GetNumDimensions();
- nCurrentDimension++
- )
- {
- if (IsKeySelected(pSpline, i, nCurrentDimension))
- {
- int flags = pSpline->GetKeyFlags(i);
- flags &= ~nRemoveFlags;
- flags |= nAddFlags;
- pSpline->SetKeyFlags(i, flags);
- break;
- }
- }
- }
- }
- SendNotifyEvent(SPLN_CHANGE);
- update();
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::FitSplineToViewWidth()
- {
- // Calculate time zoom so that whole time range fits.
- float t0 = FLT_MAX;
- float t1 = -FLT_MAX;
- bool bAnyKey = false;
- for (int i = 0; i < int(m_splines.size()); ++i)
- {
- //////////////////////////////////////////////////////////////////////////
- ISplineInterpolator* pSpline = m_splines[i].pSpline;
- if (!pSpline)
- {
- continue;
- }
- for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
- {
- float keyTime = pSpline->GetKeyTime(keyIndex);
- t0 = std::min(t0, keyTime);
- t1 = std::max(t1, keyTime);
- bAnyKey = true;
- }
- }
- if (!bAnyKey)
- {
- t0 = m_timeRange.start;
- t1 = m_timeRange.end;
- }
- float zoom = abs(m_rcSpline.width() - 20) / max(1.0f, fabs(t1 - t0));
- SetZoom(Vec2(zoom, m_grid.zoom.GetY()));
- SetScrollOffset(Vec2(t0, m_grid.origin.GetY()));
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::FitSplineToViewHeight()
- {
- // Calculate time zoom so that whole value range fits.
- Range splineRange = GetSplinesRange();
- if (splineRange.start == FLT_MAX)
- {
- splineRange = m_valueRange;
- }
- float zoom = abs(m_rcSpline.height() - 40) / max(minViewRange, splineRange.Length());
- SetZoom(Vec2(m_grid.zoom.GetX(), zoom));
- // Center the range if it's less than the minRange by adjusting it's offset.
- float scrollOffset = max(0.0f, minViewRange - splineRange.Length()) / 2.0f;
- SetScrollOffset(Vec2(m_grid.origin.GetX(), splineRange.start - scrollOffset));
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::FitSplineHeightToValueRange()
- {
- Range splineRange = GetSplinesRange();
- splineRange.start = min(splineRange.start, m_valueRange.start);
- splineRange.end = max(splineRange.end, m_valueRange.end);
- float zoom = abs(m_rcSpline.height() - 40) / max(minViewRange, splineRange.Length());
- SetZoom(Vec2(m_grid.zoom.GetX(), zoom));
- SetScrollOffset(Vec2(m_grid.origin.GetX(), splineRange.start));
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::OnUserCommand(UINT cmd)
- {
- switch (cmd)
- {
- case ID_TANGENT_IN_ZERO:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, SPLINE_KEY_TANGENT_ZERO << SPLINE_KEY_TANGENT_IN_SHIFT);
- break;
- case ID_TANGENT_IN_STEP:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, SPLINE_KEY_TANGENT_STEP << SPLINE_KEY_TANGENT_IN_SHIFT);
- break;
- case ID_TANGENT_IN_LINEAR:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK, SPLINE_KEY_TANGENT_LINEAR << SPLINE_KEY_TANGENT_IN_SHIFT);
- break;
- case ID_TANGENT_OUT_ZERO:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, SPLINE_KEY_TANGENT_ZERO << SPLINE_KEY_TANGENT_OUT_SHIFT);
- break;
- case ID_TANGENT_OUT_STEP:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, SPLINE_KEY_TANGENT_STEP << SPLINE_KEY_TANGENT_OUT_SHIFT);
- break;
- case ID_TANGENT_OUT_LINEAR:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_OUT_MASK, SPLINE_KEY_TANGENT_LINEAR << SPLINE_KEY_TANGENT_OUT_SHIFT);
- break;
- case ID_TANGENT_AUTO:
- ModifySelectedKeysFlags(SPLINE_KEY_TANGENT_IN_MASK | SPLINE_KEY_TANGENT_OUT_MASK, 0);
- break;
- case ID_SPLINE_FIT_X:
- FitSplineToViewWidth();
- break;
- case ID_SPLINE_FIT_Y:
- FitSplineToViewHeight();
- break;
- case ID_SPLINE_SNAP_GRID_X:
- SetSnapTime(!m_bSnapTime);
- break;
- case ID_SPLINE_SNAP_GRID_Y:
- SetSnapValue(!m_bSnapValue);
- break;
- case ID_SPLINE_PREVIOUS_KEY:
- GotoNextKey(true);
- break;
- case ID_SPLINE_NEXT_KEY:
- GotoNextKey(false);
- break;
- case ID_SPLINE_FLATTEN_ALL:
- RemoveAllKeysButThis();
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::GotoNextKey(bool previousKey)
- {
- if (GetNumSelected() == 1)
- {
- bool boFoundTheSelectedKey(false);
- for (size_t splineIndex = 0, endSpline = m_splines.size(); splineIndex < endSpline; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int i = 0; i < pSpline->GetKeyCount(); i++)
- {
- for (int nCurrentDimension = 0; nCurrentDimension < pSpline->GetNumDimensions(); nCurrentDimension++)
- {
- if (pSpline->IsKeySelectedAtDimension(i, nCurrentDimension))
- {
- boFoundTheSelectedKey = true;
- if ((previousKey && i > 0) || (!previousKey && i + 1 < pSpline->GetKeyCount()))
- {
- int nextKey = previousKey ? i - 1 : i + 1;
- float keyTime = pSpline->GetKeyTime(nextKey);
- SetTimeMarker(keyTime);
- ISplineInterpolator::ValueType afValue;
- pSpline->GetKeyValue(nextKey, afValue);
- pSpline->SelectKeyAtDimension(i, nCurrentDimension, false);
- pSpline->SelectKeyAtDimension(nextKey, nCurrentDimension, true);
- // Set the new scrolled coordinates
- float ofsx = keyTime - ((m_grid.rect.right() + 1) / 2) / m_grid.zoom.GetX();
- float ofsy = afValue[nCurrentDimension] - ((m_grid.rect.bottom() + 1) / 2) / m_grid.zoom.GetY();
- SetScrollOffset(Vec2(ofsx, ofsy));
- }
- break;
- }
- }
- if (boFoundTheSelectedKey)
- {
- break;
- }
- }
- }
- }
- else
- {
- for (size_t splineIndex = 0, endSpline = m_splines.size(); splineIndex < endSpline; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- float fClosestKeyTime = -1.0f;
- float fClosestDist = 1E8;
- for (int i = 0; i < pSpline->GetKeyCount(); i++)
- {
- float fKeyTime = pSpline->GetKeyTime(i);
- float fKeyDist = previousKey ? m_fTimeMarker - fKeyTime : fKeyTime - m_fTimeMarker;
- if ((fKeyDist > 0.0f) && (fKeyDist < fClosestDist))
- {
- fClosestDist = fKeyDist;
- fClosestKeyTime = pSpline->GetKeyTime(i);
- }
- }
- if (fClosestKeyTime >= 0.0f)
- {
- SetTimeMarker(fClosestKeyTime);
- float averageValue = 0.f;
- int dimensions = pSpline->GetNumDimensions();
- for (int i = 0; i < dimensions; i++)
- {
- float keyValue;
- int keyNum = pSpline->FindKey(fClosestKeyTime);
- pSpline->GetKeyValueFloat(keyNum, keyValue);
- averageValue += keyValue;
- }
- // Set the new scrolled coordinates
- const float ofsx = fClosestKeyTime - ((m_grid.rect.right() + 1) / 2) / m_grid.zoom.GetX();
- const float ofsy = averageValue / dimensions - ((m_grid.rect.bottom() + 1) / 2) / m_grid.zoom.GetY();
- SetScrollOffset(Vec2(ofsx, ofsy));
- }
- }
- }
- SendNotifyEvent(SPLN_TIME_CHANGE);
- }
- //////////////////////////////////////////////////////////////////////////
- void AbstractSplineWidget::RemoveAllKeysButThis()
- {
- std::vector<int> keys;
- for (size_t splineIndex = 0, endSpline = m_splines.size(); splineIndex < endSpline; ++splineIndex)
- {
- ISplineInterpolator* pSpline = m_splines[splineIndex].pSpline;
- for (int i = 0; i < pSpline->GetKeyCount(); i++)
- {
- if (pSpline->IsKeySelectedAtAnyDimension(i))
- {
- keys.push_back(i);
- }
- }
- for (int i = pSpline->GetKeyCount(); i >= 0; i--)
- {
- bool saveKey = false;
- for (int nIndex = 0; nIndex < keys.size(); nIndex++)
- {
- if (keys[nIndex] == i)
- {
- saveKey = true;
- }
- }
- if (!saveKey)
- {
- RemoveKey(pSpline, i);
- }
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- ISplineCtrlUndo* AbstractSplineWidget::CreateSplineCtrlUndoObject(std::vector<ISplineInterpolator*>& splineContainer)
- {
- return new CUndoSplineCtrlEx(this, splineContainer);
- }
- Range AbstractSplineWidget::GetSplinesRange()
- {
- Range range(FLT_MAX, -FLT_MAX);
- for (int i = 0; i < int(m_splines.size()); ++i)
- {
- //////////////////////////////////////////////////////////////////////////
- ISplineInterpolator* pSpline = m_splines[i].pSpline;
- if (!pSpline)
- {
- continue;
- }
- ISplineInterpolator::ValueType value;
- for (int keyIndex = 0; pSpline && keyIndex < pSpline->GetKeyCount(); ++keyIndex)
- {
- pSpline->GetKeyValue(keyIndex, value);
- for (int d = 0, numDim = pSpline->GetNumDimensions(); d < numDim; d++)
- {
- range.start = std::min(range.start, value[d]);
- range.end = std::max(range.end, value[d]);
- }
- }
- }
- return range;
- }
- #include <Controls/moc_SplineCtrlEx.cpp>
|