animation_editor.cpp 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070
  1. /*************************************************************************/
  2. /* animation_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_editor.h"
  31. #include "editor/plugins/animation_player_editor_plugin.h"
  32. #include "editor_node.h"
  33. #include "editor_settings.h"
  34. #include "io/resource_saver.h"
  35. #include "os/keyboard.h"
  36. #include "os/os.h"
  37. #include "pair.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/main/viewport.h"
  40. /* Missing to fix:
  41. *Set
  42. *Find better source for hint for edited value keys
  43. * + button on track to add a key
  44. * when clicked for first time, erase selection of not selected at first
  45. * automatically create discrete/continuous tracks!!
  46. *when create track do undo/redo
  47. */
  48. class AnimationCurveEdit : public Control {
  49. GDCLASS(AnimationCurveEdit, Control);
  50. public:
  51. enum Mode {
  52. MODE_DISABLED,
  53. MODE_SINGLE,
  54. MODE_MULTIPLE
  55. };
  56. private:
  57. Set<float> multiples;
  58. float transition;
  59. Mode mode;
  60. LineEdit *value_edit;
  61. void _notification(int p_what) {
  62. if (p_what == NOTIFICATION_DRAW) {
  63. RID ci = get_canvas_item();
  64. Size2 s = get_size();
  65. Rect2 r(Point2(), s);
  66. //r=r.grow(3);
  67. Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
  68. sb->draw(ci, r);
  69. r.size -= sb->get_minimum_size();
  70. r.position += sb->get_offset();
  71. //VisualServer::get_singleton()->canvas_item_add
  72. Ref<Font> f = get_font("font", "Label");
  73. r = r.grow(-2);
  74. Color color = get_color("font_color", "Label");
  75. int points = 48;
  76. if (mode == MODE_MULTIPLE) {
  77. Color mcolor = color;
  78. mcolor.a *= 0.3;
  79. Set<float>::Element *E = multiples.front();
  80. for (int j = 0; j < 16; j++) {
  81. if (!E)
  82. break;
  83. float prev = 1.0;
  84. float exp = E->get();
  85. bool flip = false; //hint_text=="attenuation";
  86. for (int i = 1; i <= points; i++) {
  87. float ifl = i / float(points);
  88. float iflp = (i - 1) / float(points);
  89. float h = 1.0 - Math::ease(ifl, exp);
  90. if (flip) {
  91. ifl = 1.0 - ifl;
  92. iflp = 1.0 - iflp;
  93. }
  94. VisualServer::get_singleton()->canvas_item_add_line(ci, r.position + Point2(iflp * r.size.width, prev * r.size.height), r.position + Point2(ifl * r.size.width, h * r.size.height), mcolor);
  95. prev = h;
  96. }
  97. E = E->next();
  98. }
  99. }
  100. float exp = transition;
  101. if (mode != MODE_DISABLED) {
  102. float prev = 1.0;
  103. bool flip = false; //hint_text=="attenuation";
  104. for (int i = 1; i <= points; i++) {
  105. float ifl = i / float(points);
  106. float iflp = (i - 1) / float(points);
  107. float h = 1.0 - Math::ease(ifl, exp);
  108. if (flip) {
  109. ifl = 1.0 - ifl;
  110. iflp = 1.0 - iflp;
  111. }
  112. VisualServer::get_singleton()->canvas_item_add_line(ci, r.position + Point2(iflp * r.size.width, prev * r.size.height), r.position + Point2(ifl * r.size.width, h * r.size.height), color);
  113. prev = h;
  114. }
  115. }
  116. if (mode == MODE_DISABLED) {
  117. f->draw(ci, Point2(5, 5 + f->get_ascent()), TTR("Disabled"), color);
  118. } else if (mode == MODE_MULTIPLE) {
  119. f->draw(ci, Point2(5, 5 + f->get_ascent() + value_edit->get_size().height), TTR("All Selection"), color);
  120. }
  121. }
  122. }
  123. void _gui_input(const Ref<InputEvent> &p_ev) {
  124. Ref<InputEventMouseMotion> mm = p_ev;
  125. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  126. if (mode == MODE_DISABLED)
  127. return;
  128. value_edit->release_focus();
  129. float rel = mm->get_relative().x;
  130. if (rel == 0)
  131. return;
  132. bool flip = false;
  133. if (flip)
  134. rel = -rel;
  135. float val = transition;
  136. if (val == 0)
  137. return;
  138. bool sg = val < 0;
  139. val = Math::absf(val);
  140. val = Math::log(val) / Math::log((float)2.0);
  141. //logspace
  142. val += rel * 0.05;
  143. //
  144. val = Math::pow((float)2.0, val);
  145. if (sg)
  146. val = -val;
  147. force_transition(val);
  148. }
  149. }
  150. void _edit_value_changed(const String &p_value_str) {
  151. force_transition(p_value_str.to_float());
  152. }
  153. public:
  154. static void _bind_methods() {
  155. //ClassDB::bind_method("_update_obj",&AnimationKeyEdit::_update_obj);
  156. ClassDB::bind_method("_gui_input", &AnimationCurveEdit::_gui_input);
  157. ClassDB::bind_method("_edit_value_changed", &AnimationCurveEdit::_edit_value_changed);
  158. ADD_SIGNAL(MethodInfo("transition_changed"));
  159. }
  160. void set_mode(Mode p_mode) {
  161. mode = p_mode;
  162. value_edit->set_visible(mode != MODE_DISABLED);
  163. update();
  164. }
  165. void clear_multiples() {
  166. multiples.clear();
  167. update();
  168. }
  169. void set_multiple(float p_transition) {
  170. multiples.insert(p_transition);
  171. }
  172. void set_transition(float p_transition) {
  173. transition = Math::stepify(p_transition, 0.01);
  174. value_edit->set_text(String::num(transition));
  175. update();
  176. }
  177. float get_transition() const {
  178. return transition;
  179. }
  180. void force_transition(float p_value) {
  181. if (mode == MODE_DISABLED)
  182. return;
  183. set_transition(p_value);
  184. emit_signal("transition_changed", p_value);
  185. }
  186. AnimationCurveEdit() {
  187. transition = 1.0;
  188. set_default_cursor_shape(CURSOR_HSPLIT);
  189. mode = MODE_DISABLED;
  190. value_edit = memnew(LineEdit);
  191. value_edit->hide();
  192. value_edit->connect("text_entered", this, "_edit_value_changed");
  193. add_child(value_edit);
  194. }
  195. };
  196. class AnimationKeyEdit : public Object {
  197. GDCLASS(AnimationKeyEdit, Object);
  198. public:
  199. bool setting;
  200. bool hidden;
  201. static void _bind_methods() {
  202. ClassDB::bind_method("_update_obj", &AnimationKeyEdit::_update_obj);
  203. ClassDB::bind_method("_key_ofs_changed", &AnimationKeyEdit::_key_ofs_changed);
  204. }
  205. //PopupDialog *ke_dialog;
  206. void _fix_node_path(Variant &value) {
  207. NodePath np = value;
  208. if (np == NodePath())
  209. return;
  210. Node *root = EditorNode::get_singleton()->get_tree()->get_root();
  211. Node *np_node = root->get_node(np);
  212. ERR_FAIL_COND(!np_node);
  213. Node *edited_node = root->get_node(base);
  214. ERR_FAIL_COND(!edited_node);
  215. value = edited_node->get_path_to(np_node);
  216. }
  217. void _update_obj(const Ref<Animation> &p_anim) {
  218. if (setting)
  219. return;
  220. if (hidden)
  221. return;
  222. if (!(animation == p_anim))
  223. return;
  224. notify_change();
  225. }
  226. void _key_ofs_changed(const Ref<Animation> &p_anim, float from, float to) {
  227. if (hidden)
  228. return;
  229. if (!(animation == p_anim))
  230. return;
  231. if (from != key_ofs)
  232. return;
  233. key_ofs = to;
  234. if (setting)
  235. return;
  236. notify_change();
  237. }
  238. bool _set(const StringName &p_name, const Variant &p_value) {
  239. int key = animation->track_find_key(track, key_ofs, true);
  240. ERR_FAIL_COND_V(key == -1, false);
  241. String name = p_name;
  242. if (name == "time") {
  243. float new_time = p_value;
  244. if (new_time == key_ofs)
  245. return true;
  246. int existing = animation->track_find_key(track, new_time, true);
  247. setting = true;
  248. undo_redo->create_action(TTR("Move Add Key"), UndoRedo::MERGE_ENDS);
  249. Variant val = animation->track_get_key_value(track, key);
  250. float trans = animation->track_get_key_transition(track, key);
  251. undo_redo->add_do_method(animation.ptr(), "track_remove_key", track, key);
  252. undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, new_time, val, trans);
  253. undo_redo->add_do_method(this, "_key_ofs_changed", animation, key_ofs, new_time);
  254. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", track, new_time);
  255. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, key_ofs, val, trans);
  256. undo_redo->add_undo_method(this, "_key_ofs_changed", animation, new_time, key_ofs);
  257. if (existing != -1) {
  258. Variant v = animation->track_get_key_value(track, existing);
  259. float trans = animation->track_get_key_transition(track, existing);
  260. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, new_time, v, trans);
  261. }
  262. undo_redo->commit_action();
  263. setting = false;
  264. return true;
  265. } else if (name == "easing") {
  266. float val = p_value;
  267. float prev_val = animation->track_get_key_transition(track, key);
  268. setting = true;
  269. undo_redo->create_action(TTR("Anim Change Transition"), UndoRedo::MERGE_ENDS);
  270. undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", track, key, val);
  271. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", track, key, prev_val);
  272. undo_redo->add_do_method(this, "_update_obj", animation);
  273. undo_redo->add_undo_method(this, "_update_obj", animation);
  274. undo_redo->commit_action();
  275. setting = false;
  276. return true;
  277. }
  278. switch (animation->track_get_type(track)) {
  279. case Animation::TYPE_TRANSFORM: {
  280. Dictionary d_old = animation->track_get_key_value(track, key);
  281. Dictionary d_new = d_old;
  282. d_new[p_name] = p_value;
  283. setting = true;
  284. undo_redo->create_action(TTR("Anim Change Transform"));
  285. undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, d_new);
  286. undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, d_old);
  287. undo_redo->add_do_method(this, "_update_obj", animation);
  288. undo_redo->add_undo_method(this, "_update_obj", animation);
  289. undo_redo->commit_action();
  290. setting = false;
  291. return true;
  292. } break;
  293. case Animation::TYPE_VALUE: {
  294. if (name == "value") {
  295. Variant value = p_value;
  296. if (value.get_type() == Variant::NODE_PATH) {
  297. _fix_node_path(value);
  298. }
  299. setting = true;
  300. undo_redo->create_action(TTR("Anim Change Value"), UndoRedo::MERGE_ENDS);
  301. Variant prev = animation->track_get_key_value(track, key);
  302. undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, value);
  303. undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, prev);
  304. undo_redo->add_do_method(this, "_update_obj", animation);
  305. undo_redo->add_undo_method(this, "_update_obj", animation);
  306. undo_redo->commit_action();
  307. setting = false;
  308. return true;
  309. }
  310. } break;
  311. case Animation::TYPE_METHOD: {
  312. Dictionary d_old = animation->track_get_key_value(track, key);
  313. Dictionary d_new = d_old;
  314. bool change_notify_deserved = false;
  315. bool mergeable = false;
  316. if (name == "name") {
  317. d_new["method"] = p_value;
  318. }
  319. if (name == "arg_count") {
  320. Vector<Variant> args = d_old["args"];
  321. args.resize(p_value);
  322. d_new["args"] = args;
  323. change_notify_deserved = true;
  324. }
  325. if (name.begins_with("args/")) {
  326. Vector<Variant> args = d_old["args"];
  327. int idx = name.get_slice("/", 1).to_int();
  328. ERR_FAIL_INDEX_V(idx, args.size(), false);
  329. String what = name.get_slice("/", 2);
  330. if (what == "type") {
  331. Variant::Type t = Variant::Type(int(p_value));
  332. if (t != args[idx].get_type()) {
  333. Variant::CallError err;
  334. if (Variant::can_convert(args[idx].get_type(), t)) {
  335. Variant old = args[idx];
  336. Variant *ptrs[1] = { &old };
  337. args[idx] = Variant::construct(t, (const Variant **)ptrs, 1, err);
  338. } else {
  339. args[idx] = Variant::construct(t, NULL, 0, err);
  340. }
  341. change_notify_deserved = true;
  342. d_new["args"] = args;
  343. }
  344. }
  345. if (what == "value") {
  346. Variant value = p_value;
  347. if (value.get_type() == Variant::NODE_PATH) {
  348. _fix_node_path(value);
  349. }
  350. args[idx] = value;
  351. d_new["args"] = args;
  352. mergeable = true;
  353. }
  354. }
  355. if (mergeable)
  356. undo_redo->create_action(TTR("Anim Change Call"), UndoRedo::MERGE_ENDS);
  357. else
  358. undo_redo->create_action(TTR("Anim Change Call"));
  359. Variant prev = animation->track_get_key_value(track, key);
  360. setting = true;
  361. undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, d_new);
  362. undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, d_old);
  363. undo_redo->add_do_method(this, "_update_obj", animation);
  364. undo_redo->add_undo_method(this, "_update_obj", animation);
  365. undo_redo->commit_action();
  366. setting = false;
  367. if (change_notify_deserved)
  368. notify_change();
  369. return true;
  370. } break;
  371. }
  372. return false;
  373. }
  374. bool _get(const StringName &p_name, Variant &r_ret) const {
  375. int key = animation->track_find_key(track, key_ofs, true);
  376. ERR_FAIL_COND_V(key == -1, false);
  377. String name = p_name;
  378. if (name == "time") {
  379. r_ret = key_ofs;
  380. return true;
  381. } else if (name == "easing") {
  382. r_ret = animation->track_get_key_transition(track, key);
  383. return true;
  384. }
  385. switch (animation->track_get_type(track)) {
  386. case Animation::TYPE_TRANSFORM: {
  387. Dictionary d = animation->track_get_key_value(track, key);
  388. ERR_FAIL_COND_V(!d.has(name), false);
  389. r_ret = d[p_name];
  390. return true;
  391. } break;
  392. case Animation::TYPE_VALUE: {
  393. if (name == "value") {
  394. r_ret = animation->track_get_key_value(track, key);
  395. return true;
  396. }
  397. } break;
  398. case Animation::TYPE_METHOD: {
  399. Dictionary d = animation->track_get_key_value(track, key);
  400. if (name == "name") {
  401. ERR_FAIL_COND_V(!d.has("method"), false);
  402. r_ret = d["method"];
  403. return true;
  404. }
  405. ERR_FAIL_COND_V(!d.has("args"), false);
  406. Vector<Variant> args = d["args"];
  407. if (name == "arg_count") {
  408. r_ret = args.size();
  409. return true;
  410. }
  411. if (name.begins_with("args/")) {
  412. int idx = name.get_slice("/", 1).to_int();
  413. ERR_FAIL_INDEX_V(idx, args.size(), false);
  414. String what = name.get_slice("/", 2);
  415. if (what == "type") {
  416. r_ret = args[idx].get_type();
  417. return true;
  418. }
  419. if (what == "value") {
  420. r_ret = args[idx];
  421. return true;
  422. }
  423. }
  424. } break;
  425. }
  426. return false;
  427. }
  428. void _get_property_list(List<PropertyInfo> *p_list) const {
  429. if (animation.is_null())
  430. return;
  431. ERR_FAIL_INDEX(track, animation->get_track_count());
  432. int key = animation->track_find_key(track, key_ofs, true);
  433. ERR_FAIL_COND(key == -1);
  434. p_list->push_back(PropertyInfo(Variant::REAL, "time", PROPERTY_HINT_RANGE, "0," + rtos(animation->get_length()) + ",0.01"));
  435. switch (animation->track_get_type(track)) {
  436. case Animation::TYPE_TRANSFORM: {
  437. p_list->push_back(PropertyInfo(Variant::VECTOR3, "location"));
  438. p_list->push_back(PropertyInfo(Variant::QUAT, "rotation"));
  439. p_list->push_back(PropertyInfo(Variant::VECTOR3, "scale"));
  440. } break;
  441. case Animation::TYPE_VALUE: {
  442. Variant v = animation->track_get_key_value(track, key);
  443. if (hint.type != Variant::NIL) {
  444. PropertyInfo pi = hint;
  445. pi.name = "value";
  446. p_list->push_back(pi);
  447. } else {
  448. PropertyHint hint = PROPERTY_HINT_NONE;
  449. String hint_string;
  450. if (v.get_type() == Variant::OBJECT) {
  451. //could actually check the object property if exists..? yes i will!
  452. Ref<Resource> res = v;
  453. if (res.is_valid()) {
  454. hint = PROPERTY_HINT_RESOURCE_TYPE;
  455. hint_string = res->get_class();
  456. }
  457. }
  458. if (v.get_type() != Variant::NIL)
  459. p_list->push_back(PropertyInfo(v.get_type(), "value", hint, hint_string));
  460. }
  461. } break;
  462. case Animation::TYPE_METHOD: {
  463. p_list->push_back(PropertyInfo(Variant::STRING, "name"));
  464. p_list->push_back(PropertyInfo(Variant::INT, "arg_count", PROPERTY_HINT_RANGE, "0,5,1"));
  465. Dictionary d = animation->track_get_key_value(track, key);
  466. ERR_FAIL_COND(!d.has("args"));
  467. Vector<Variant> args = d["args"];
  468. String vtypes;
  469. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  470. if (i > 0)
  471. vtypes += ",";
  472. vtypes += Variant::get_type_name(Variant::Type(i));
  473. }
  474. for (int i = 0; i < args.size(); i++) {
  475. p_list->push_back(PropertyInfo(Variant::INT, "args/" + itos(i) + "/type", PROPERTY_HINT_ENUM, vtypes));
  476. if (args[i].get_type() != Variant::NIL)
  477. p_list->push_back(PropertyInfo(args[i].get_type(), "args/" + itos(i) + "/value"));
  478. }
  479. } break;
  480. }
  481. /*
  482. if (animation->track_get_type(track)!=Animation::TYPE_METHOD)
  483. p_list->push_back( PropertyInfo( Variant::REAL, "easing", PROPERTY_HINT_EXP_EASING));
  484. */
  485. }
  486. UndoRedo *undo_redo;
  487. Ref<Animation> animation;
  488. int track;
  489. float key_ofs;
  490. PropertyInfo hint;
  491. NodePath base;
  492. void notify_change() {
  493. _change_notify();
  494. }
  495. AnimationKeyEdit() {
  496. hidden = true;
  497. key_ofs = 0;
  498. track = -1;
  499. setting = false;
  500. }
  501. };
  502. void AnimationKeyEditor::_menu_add_track(int p_type) {
  503. ERR_FAIL_COND(!animation.is_valid());
  504. switch (p_type) {
  505. case ADD_TRACK_MENU_ADD_CALL_TRACK: {
  506. if (root) {
  507. call_select->popup_centered_ratio();
  508. break;
  509. }
  510. } break;
  511. case ADD_TRACK_MENU_ADD_VALUE_TRACK:
  512. case ADD_TRACK_MENU_ADD_TRANSFORM_TRACK: {
  513. undo_redo->create_action(TTR("Anim Add Track"));
  514. undo_redo->add_do_method(animation.ptr(), "add_track", p_type);
  515. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), ".");
  516. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  517. undo_redo->commit_action();
  518. } break;
  519. }
  520. }
  521. void AnimationKeyEditor::_anim_duplicate_keys(bool transpose) {
  522. //duplicait!
  523. if (selection.size() && animation.is_valid() && selected_track >= 0 && selected_track < animation->get_track_count()) {
  524. int top_track = 0x7FFFFFFF;
  525. float top_time = 1e10;
  526. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  527. const SelectedKey &sk = E->key();
  528. float t = animation->track_get_key_time(sk.track, sk.key);
  529. if (t < top_time)
  530. top_time = t;
  531. if (sk.track < top_track)
  532. top_track = sk.track;
  533. }
  534. ERR_FAIL_COND(top_track == 0x7FFFFFFF || top_time == 1e10);
  535. //
  536. int start_track = transpose ? selected_track : top_track;
  537. undo_redo->create_action(TTR("Anim Duplicate Keys"));
  538. List<Pair<int, float> > new_selection_values;
  539. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  540. const SelectedKey &sk = E->key();
  541. float t = animation->track_get_key_time(sk.track, sk.key);
  542. float dst_time = t + (timeline_pos - top_time);
  543. int dst_track = sk.track + (start_track - top_track);
  544. if (dst_track < 0 || dst_track >= animation->get_track_count())
  545. continue;
  546. if (animation->track_get_type(dst_track) != animation->track_get_type(sk.track))
  547. continue;
  548. int existing_idx = animation->track_find_key(dst_track, dst_time, true);
  549. undo_redo->add_do_method(animation.ptr(), "track_insert_key", dst_track, dst_time, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  550. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", dst_track, dst_time);
  551. Pair<int, float> p;
  552. p.first = dst_track;
  553. p.second = dst_time;
  554. new_selection_values.push_back(p);
  555. if (existing_idx != -1) {
  556. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", dst_track, dst_time, animation->track_get_key_value(dst_track, existing_idx), animation->track_get_key_transition(dst_track, existing_idx));
  557. }
  558. }
  559. undo_redo->commit_action();
  560. //reselect duplicated
  561. Map<SelectedKey, KeyInfo> new_selection;
  562. for (List<Pair<int, float> >::Element *E = new_selection_values.front(); E; E = E->next()) {
  563. int track = E->get().first;
  564. float time = E->get().second;
  565. int existing_idx = animation->track_find_key(track, time, true);
  566. if (existing_idx == -1)
  567. continue;
  568. SelectedKey sk2;
  569. sk2.track = track;
  570. sk2.key = existing_idx;
  571. KeyInfo ki;
  572. ki.pos = time;
  573. new_selection[sk2] = ki;
  574. }
  575. selection = new_selection;
  576. track_editor->update();
  577. _edit_if_single_selection();
  578. }
  579. }
  580. void AnimationKeyEditor::_menu_track(int p_type) {
  581. ERR_FAIL_COND(!animation.is_valid());
  582. last_menu_track_opt = p_type;
  583. switch (p_type) {
  584. case TRACK_MENU_SCALE:
  585. case TRACK_MENU_SCALE_PIVOT: {
  586. scale_dialog->popup_centered(Size2(200, 100));
  587. } break;
  588. case TRACK_MENU_MOVE_UP: {
  589. int idx = selected_track;
  590. if (idx > 0 && idx < animation->get_track_count()) {
  591. undo_redo->create_action(TTR("Move Anim Track Up"));
  592. undo_redo->add_do_method(animation.ptr(), "track_move_down", idx);
  593. undo_redo->add_undo_method(animation.ptr(), "track_move_up", idx - 1);
  594. undo_redo->commit_action();
  595. selected_track = idx - 1;
  596. }
  597. } break;
  598. case TRACK_MENU_MOVE_DOWN: {
  599. int idx = selected_track;
  600. if (idx >= 0 && idx < animation->get_track_count() - 1) {
  601. undo_redo->create_action(TTR("Move Anim Track Down"));
  602. undo_redo->add_do_method(animation.ptr(), "track_move_up", idx);
  603. undo_redo->add_undo_method(animation.ptr(), "track_move_down", idx + 1);
  604. undo_redo->commit_action();
  605. selected_track = idx + 1;
  606. }
  607. } break;
  608. case TRACK_MENU_REMOVE: {
  609. int idx = selected_track;
  610. if (idx >= 0 && idx < animation->get_track_count()) {
  611. undo_redo->create_action(TTR("Remove Anim Track"));
  612. undo_redo->add_do_method(animation.ptr(), "remove_track", idx);
  613. undo_redo->add_undo_method(animation.ptr(), "add_track", animation->track_get_type(idx), idx);
  614. undo_redo->add_undo_method(animation.ptr(), "track_set_path", idx, animation->track_get_path(idx));
  615. //todo interpolation
  616. for (int i = 0; i < animation->track_get_key_count(idx); i++) {
  617. Variant v = animation->track_get_key_value(idx, i);
  618. float time = animation->track_get_key_time(idx, i);
  619. float trans = animation->track_get_key_transition(idx, i);
  620. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", idx, time, v);
  621. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", idx, i, trans);
  622. }
  623. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", idx, animation->track_get_interpolation_type(idx));
  624. if (animation->track_get_type(idx) == Animation::TYPE_VALUE) {
  625. undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", idx, animation->value_track_get_update_mode(idx));
  626. }
  627. undo_redo->commit_action();
  628. }
  629. } break;
  630. case TRACK_MENU_DUPLICATE:
  631. case TRACK_MENU_DUPLICATE_TRANSPOSE: {
  632. _anim_duplicate_keys(p_type == TRACK_MENU_DUPLICATE_TRANSPOSE);
  633. } break;
  634. case TRACK_MENU_SET_ALL_TRANS_LINEAR:
  635. case TRACK_MENU_SET_ALL_TRANS_CONSTANT:
  636. case TRACK_MENU_SET_ALL_TRANS_OUT:
  637. case TRACK_MENU_SET_ALL_TRANS_IN:
  638. case TRACK_MENU_SET_ALL_TRANS_INOUT:
  639. case TRACK_MENU_SET_ALL_TRANS_OUTIN: {
  640. if (!selection.size() || !animation.is_valid())
  641. break;
  642. float t = 0;
  643. switch (p_type) {
  644. case TRACK_MENU_SET_ALL_TRANS_LINEAR: t = 1.0; break;
  645. case TRACK_MENU_SET_ALL_TRANS_CONSTANT: t = 0.0; break;
  646. case TRACK_MENU_SET_ALL_TRANS_OUT: t = 0.5; break;
  647. case TRACK_MENU_SET_ALL_TRANS_IN: t = 2.0; break;
  648. case TRACK_MENU_SET_ALL_TRANS_INOUT: t = -0.5; break;
  649. case TRACK_MENU_SET_ALL_TRANS_OUTIN: t = -2.0; break;
  650. }
  651. undo_redo->create_action(TTR("Set Transitions to:") + " " + rtos(t));
  652. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  653. const SelectedKey &sk = E->key();
  654. undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", sk.track, sk.key, t);
  655. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", sk.track, sk.key, animation->track_get_key_transition(sk.track, sk.key));
  656. }
  657. undo_redo->commit_action();
  658. } break;
  659. case TRACK_MENU_NEXT_STEP: {
  660. if (animation.is_null())
  661. break;
  662. float step = animation->get_step();
  663. if (step == 0)
  664. step = 1;
  665. float pos = timeline_pos;
  666. pos = Math::stepify(pos + step, step);
  667. if (pos > animation->get_length())
  668. pos = animation->get_length();
  669. timeline_pos = pos;
  670. track_pos->update();
  671. emit_signal("timeline_changed", pos, true);
  672. } break;
  673. case TRACK_MENU_PREV_STEP: {
  674. if (animation.is_null())
  675. break;
  676. float step = animation->get_step();
  677. if (step == 0)
  678. step = 1;
  679. float pos = timeline_pos;
  680. pos = Math::stepify(pos - step, step);
  681. if (pos < 0)
  682. pos = 0;
  683. timeline_pos = pos;
  684. track_pos->update();
  685. emit_signal("timeline_changed", pos, true);
  686. } break;
  687. case TRACK_MENU_OPTIMIZE: {
  688. optimize_dialog->popup_centered(Size2(250, 180));
  689. } break;
  690. case TRACK_MENU_CLEAN_UP: {
  691. cleanup_dialog->popup_centered_minsize(Size2(300, 0));
  692. } break;
  693. case TRACK_MENU_CLEAN_UP_CONFIRM: {
  694. if (cleanup_all->is_pressed()) {
  695. List<StringName> names;
  696. AnimationPlayerEditor::singleton->get_player()->get_animation_list(&names);
  697. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  698. _cleanup_animation(AnimationPlayerEditor::singleton->get_player()->get_animation(E->get()));
  699. }
  700. } else {
  701. _cleanup_animation(animation);
  702. }
  703. } break;
  704. case CURVE_SET_LINEAR: {
  705. curve_edit->force_transition(1.0);
  706. } break;
  707. case CURVE_SET_IN: {
  708. curve_edit->force_transition(4.0);
  709. } break;
  710. case CURVE_SET_OUT: {
  711. curve_edit->force_transition(0.25);
  712. } break;
  713. case CURVE_SET_INOUT: {
  714. curve_edit->force_transition(-4);
  715. } break;
  716. case CURVE_SET_OUTIN: {
  717. curve_edit->force_transition(-0.25);
  718. } break;
  719. case CURVE_SET_CONSTANT: {
  720. curve_edit->force_transition(0);
  721. } break;
  722. }
  723. }
  724. void AnimationKeyEditor::_cleanup_animation(Ref<Animation> p_animation) {
  725. for (int i = 0; i < p_animation->get_track_count(); i++) {
  726. bool prop_exists = false;
  727. Variant::Type valid_type = Variant::NIL;
  728. Object *obj = NULL;
  729. RES res;
  730. Node *node = root->get_node_and_resource(p_animation->track_get_path(i), res);
  731. if (res.is_valid()) {
  732. obj = res.ptr();
  733. } else if (node) {
  734. obj = node;
  735. }
  736. if (obj && p_animation->track_get_type(i) == Animation::TYPE_VALUE) {
  737. valid_type = obj->get_static_property_type(p_animation->track_get_path(i).get_property(), &prop_exists);
  738. }
  739. if (!obj && cleanup_tracks->is_pressed()) {
  740. p_animation->remove_track(i);
  741. i--;
  742. continue;
  743. }
  744. if (!prop_exists || p_animation->track_get_type(i) != Animation::TYPE_VALUE || cleanup_keys->is_pressed() == false)
  745. continue;
  746. for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
  747. Variant v = p_animation->track_get_key_value(i, j);
  748. if (!Variant::can_convert(v.get_type(), valid_type)) {
  749. p_animation->track_remove_key(i, j);
  750. j--;
  751. }
  752. }
  753. if (p_animation->track_get_key_count(i) == 0 && cleanup_tracks->is_pressed()) {
  754. p_animation->remove_track(i);
  755. i--;
  756. }
  757. }
  758. undo_redo->clear_history();
  759. _update_paths();
  760. }
  761. void AnimationKeyEditor::_animation_optimize() {
  762. animation->optimize(optimize_linear_error->get_value(), optimize_angular_error->get_value(), optimize_max_angle->get_value());
  763. track_editor->update();
  764. undo_redo->clear_history();
  765. }
  766. float AnimationKeyEditor::_get_zoom_scale() const {
  767. float zv = zoom->get_value();
  768. if (zv < 1) {
  769. zv = 1.0 - zv;
  770. return Math::pow(1.0f + zv, 8.0f) * 100;
  771. } else {
  772. return 1.0 / Math::pow(zv, 8.0f) * 100;
  773. }
  774. }
  775. void AnimationKeyEditor::_track_position_draw() {
  776. if (!animation.is_valid()) {
  777. return;
  778. }
  779. Ref<StyleBox> style = get_stylebox("normal", "TextEdit");
  780. Size2 size = track_editor->get_size() - style->get_minimum_size();
  781. Size2 ofs = style->get_offset();
  782. int settings_limit = size.width - right_data_size_cache;
  783. int name_limit = settings_limit * name_column_ratio;
  784. float keys_from = h_scroll->get_value();
  785. float zoom_scale = _get_zoom_scale();
  786. float keys_to = keys_from + (settings_limit - name_limit) / zoom_scale;
  787. //will move to separate control! (for speedup)
  788. if (timeline_pos >= keys_from && timeline_pos < keys_to) {
  789. //draw position
  790. int pixel = (timeline_pos - h_scroll->get_value()) * zoom_scale;
  791. pixel += name_limit;
  792. track_pos->draw_line(ofs + Point2(pixel, 0), ofs + Point2(pixel, size.height), get_color("accent_color", "Editor"));
  793. }
  794. }
  795. void AnimationKeyEditor::_track_editor_draw() {
  796. if (animation.is_valid() && animation->get_track_count()) {
  797. if (selected_track < 0)
  798. selected_track = 0;
  799. else if (selected_track >= animation->get_track_count())
  800. selected_track = animation->get_track_count() - 1;
  801. }
  802. track_pos->update();
  803. Control *te = track_editor;
  804. Ref<StyleBox> style = get_stylebox("normal", "TextEdit");
  805. te->draw_style_box(style, Rect2(Point2(), track_editor->get_size()));
  806. if (te->has_focus()) {
  807. te->draw_style_box(get_stylebox("bg_focus", "Tree"), Rect2(Point2(), track_editor->get_size()));
  808. }
  809. if (!animation.is_valid()) {
  810. v_scroll->hide();
  811. h_scroll->hide();
  812. menu_add_track->set_disabled(true);
  813. menu_track->set_disabled(true);
  814. edit_button->set_disabled(true);
  815. key_editor_tab->hide();
  816. move_up_button->set_disabled(true);
  817. move_down_button->set_disabled(true);
  818. remove_button->set_disabled(true);
  819. return;
  820. }
  821. menu_add_track->set_disabled(false);
  822. menu_track->set_disabled(false);
  823. edit_button->set_disabled(false);
  824. move_up_button->set_disabled(false);
  825. move_down_button->set_disabled(false);
  826. remove_button->set_disabled(false);
  827. if (edit_button->is_pressed())
  828. key_editor_tab->show();
  829. te_drawing = true;
  830. Size2 size = te->get_size() - style->get_minimum_size();
  831. Size2 ofs = style->get_offset();
  832. Ref<Font> font = te->get_font("font", "Tree");
  833. int sep = get_constant("vseparation", "Tree");
  834. int hsep = get_constant("hseparation", "Tree");
  835. Color color = get_color("font_color", "Tree");
  836. Color sepcolor = color;
  837. sepcolor.a = 0.2;
  838. Color timecolor = color;
  839. timecolor.a = 0.2;
  840. Color hover_color = color;
  841. hover_color.a = 0.05;
  842. Color select_color = color;
  843. select_color.a = 0.1;
  844. Color invalid_path_color = get_color("error_color", "Editor");
  845. Color track_select_color = get_color("highlighted_font_color", "Editor");
  846. Ref<Texture> remove_icon = get_icon("Remove", "EditorIcons");
  847. Ref<Texture> move_up_icon = get_icon("MoveUp", "EditorIcons");
  848. Ref<Texture> move_down_icon = get_icon("MoveDown", "EditorIcons");
  849. Ref<Texture> remove_icon_hl = get_icon("RemoveHl", "EditorIcons");
  850. Ref<Texture> move_up_icon_hl = get_icon("MoveUpHl", "EditorIcons");
  851. Ref<Texture> move_down_icon_hl = get_icon("MoveDownHl", "EditorIcons");
  852. Ref<Texture> add_key_icon = get_icon("TrackAddKey", "EditorIcons");
  853. Ref<Texture> add_key_icon_hl = get_icon("TrackAddKeyHl", "EditorIcons");
  854. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  855. Ref<Texture> wrap_icon[2] = {
  856. get_icon("InterpWrapClamp", "EditorIcons"),
  857. get_icon("InterpWrapLoop", "EditorIcons"),
  858. };
  859. Ref<Texture> interp_icon[3] = {
  860. get_icon("InterpRaw", "EditorIcons"),
  861. get_icon("InterpLinear", "EditorIcons"),
  862. get_icon("InterpCubic", "EditorIcons")
  863. };
  864. Ref<Texture> cont_icon[3] = {
  865. get_icon("TrackContinuous", "EditorIcons"),
  866. get_icon("TrackDiscrete", "EditorIcons"),
  867. get_icon("TrackTrigger", "EditorIcons")
  868. };
  869. Ref<Texture> type_icon[3] = {
  870. get_icon("KeyValue", "EditorIcons"),
  871. get_icon("KeyXform", "EditorIcons"),
  872. get_icon("KeyCall", "EditorIcons")
  873. };
  874. Ref<Texture> invalid_icon = get_icon("KeyInvalid", "EditorIcons");
  875. Ref<Texture> invalid_icon_hover = get_icon("KeyInvalidHover", "EditorIcons");
  876. Ref<Texture> hsize_icon = get_icon("Hsize", "EditorIcons");
  877. Ref<Texture> type_hover = get_icon("KeyHover", "EditorIcons");
  878. Ref<Texture> type_selected = get_icon("KeySelected", "EditorIcons");
  879. int right_separator_ofs = down_icon->get_width() * 3 + add_key_icon->get_width() + interp_icon[0]->get_width() + wrap_icon[0]->get_width() + cont_icon[0]->get_width() + hsep * 9;
  880. int h = font->get_height() + sep;
  881. int fit = (size.height / h) - 1;
  882. int total = animation->get_track_count();
  883. if (total < fit) {
  884. v_scroll->hide();
  885. v_scroll->set_max(total);
  886. v_scroll->set_page(fit);
  887. } else {
  888. v_scroll->show();
  889. v_scroll->set_max(total);
  890. v_scroll->set_page(fit);
  891. }
  892. int settings_limit = size.width - right_separator_ofs;
  893. int name_limit = settings_limit * name_column_ratio;
  894. Color linecolor = color;
  895. linecolor.a = 0.2;
  896. te->draw_line(ofs + Point2(name_limit, 0), ofs + Point2(name_limit, size.height), linecolor);
  897. te->draw_line(ofs + Point2(settings_limit, 0), ofs + Point2(settings_limit, size.height), linecolor);
  898. te->draw_texture(hsize_icon, ofs + Point2(name_limit - hsize_icon->get_width() - hsep, (h - hsize_icon->get_height()) / 2));
  899. te->draw_line(ofs + Point2(0, h), ofs + Point2(size.width, h), linecolor);
  900. // draw time
  901. float keys_from;
  902. float keys_to;
  903. float zoom_scale;
  904. {
  905. int zoomw = settings_limit - name_limit;
  906. float scale = _get_zoom_scale();
  907. zoom_scale = scale;
  908. float l = animation->get_length();
  909. if (l <= 0)
  910. l = 0.001; //avoid crashor
  911. int end_px = (l - h_scroll->get_value()) * scale;
  912. int begin_px = -h_scroll->get_value() * scale;
  913. Color notimecol = get_color("dark_color_2", "Editor");
  914. {
  915. te->draw_rect(Rect2(ofs + Point2(name_limit, 0), Point2(zoomw - 1, h)), notimecol);
  916. if (begin_px < zoomw && end_px > 0) {
  917. if (begin_px < 0)
  918. begin_px = 0;
  919. if (end_px > zoomw)
  920. end_px = zoomw;
  921. te->draw_rect(Rect2(ofs + Point2(name_limit + begin_px, 0), Point2(end_px - begin_px - 1, h)), timecolor);
  922. }
  923. }
  924. keys_from = h_scroll->get_value();
  925. keys_to = keys_from + zoomw / scale;
  926. {
  927. float time_min = 0;
  928. float time_max = animation->get_length();
  929. for (int i = 0; i < animation->get_track_count(); i++) {
  930. if (animation->track_get_key_count(i) > 0) {
  931. float beg = animation->track_get_key_time(i, 0);
  932. if (beg < time_min)
  933. time_min = beg;
  934. float end = animation->track_get_key_time(i, animation->track_get_key_count(i) - 1);
  935. if (end > time_max)
  936. time_max = end;
  937. }
  938. }
  939. float extra = (zoomw / scale) * 0.5;
  940. if (time_min < -0.001)
  941. time_min -= extra;
  942. time_max += extra;
  943. h_scroll->set_min(time_min);
  944. h_scroll->set_max(time_max);
  945. if (zoomw / scale < (time_max - time_min)) {
  946. h_scroll->show();
  947. } else {
  948. h_scroll->hide();
  949. }
  950. }
  951. h_scroll->set_page(zoomw / scale);
  952. Color color_time_sec = color;
  953. Color color_time_dec = color;
  954. color_time_dec.a *= 0.5;
  955. #define SC_ADJ 100
  956. int min = 30;
  957. int dec = 1;
  958. int step = 1;
  959. int decimals = 2;
  960. bool step_found = false;
  961. while (!step_found) {
  962. static const int _multp[3] = { 1, 2, 5 };
  963. for (int i = 0; i < 3; i++) {
  964. step = (_multp[i] * dec);
  965. if (step * scale / SC_ADJ > min) {
  966. step_found = true;
  967. break;
  968. }
  969. }
  970. if (step_found)
  971. break;
  972. dec *= 10;
  973. decimals--;
  974. if (decimals < 0)
  975. decimals = 0;
  976. }
  977. for (int i = 0; i < zoomw; i++) {
  978. float pos = h_scroll->get_value() + double(i) / scale;
  979. float prev = h_scroll->get_value() + (double(i) - 1.0) / scale;
  980. int sc = int(Math::floor(pos * SC_ADJ));
  981. int prev_sc = int(Math::floor(prev * SC_ADJ));
  982. bool sub = (sc % SC_ADJ);
  983. if ((sc / step) != (prev_sc / step) || (prev_sc < 0 && sc >= 0)) {
  984. int scd = sc < 0 ? prev_sc : sc;
  985. te->draw_line(ofs + Point2(name_limit + i, 0), ofs + Point2(name_limit + i, h), linecolor);
  986. te->draw_string(font, ofs + Point2(name_limit + i + 3, (h - font->get_height()) / 2 + font->get_ascent()).floor(), String::num((scd - (scd % step)) / double(SC_ADJ), decimals), sub ? color_time_dec : color_time_sec, zoomw - i);
  987. }
  988. }
  989. }
  990. color.a *= 0.5;
  991. for (int i = 0; i < fit; i++) {
  992. //this code sucks, i always forget how it works
  993. int idx = v_scroll->get_value() + i;
  994. if (idx >= animation->get_track_count())
  995. break;
  996. int y = h + i * h + sep;
  997. bool prop_exists = false;
  998. Variant::Type valid_type = Variant::NIL;
  999. Object *obj = NULL;
  1000. RES res;
  1001. Node *node = root ? root->get_node_and_resource(animation->track_get_path(idx), res) : (Node *)NULL;
  1002. if (res.is_valid()) {
  1003. obj = res.ptr();
  1004. } else if (node) {
  1005. obj = node;
  1006. }
  1007. if (obj && animation->track_get_type(idx) == Animation::TYPE_VALUE) {
  1008. valid_type = obj->get_static_property_type(animation->track_get_path(idx).get_property(), &prop_exists);
  1009. }
  1010. if (/*mouse_over.over!=MouseOver::OVER_NONE &&*/ idx == mouse_over.track) {
  1011. Color sepc = hover_color;
  1012. te->draw_rect(Rect2(ofs + Point2(0, y), Size2(size.width, h - 1)), sepc);
  1013. }
  1014. if (selected_track == idx) {
  1015. Color tc = select_color;
  1016. //tc.a*=0.7;
  1017. te->draw_rect(Rect2(ofs + Point2(0, y), Size2(size.width - 1, h - 1)), tc);
  1018. }
  1019. te->draw_texture(type_icon[animation->track_get_type(idx)], ofs + Point2(0, y + (h - type_icon[0]->get_height()) / 2).floor());
  1020. NodePath np = animation->track_get_path(idx);
  1021. Node *n = root ? root->get_node(np) : (Node *)NULL;
  1022. Color ncol = color;
  1023. if (n && editor_selection->is_selected(n))
  1024. ncol = track_select_color;
  1025. te->draw_string(font, Point2(ofs + Point2(type_icon[0]->get_width() + sep, y + font->get_ascent() + (sep / 2))).floor(), np, ncol, name_limit - (type_icon[0]->get_width() + sep) - 5);
  1026. if (!obj)
  1027. te->draw_line(ofs + Point2(0, y + h / 2), ofs + Point2(name_limit, y + h / 2), invalid_path_color);
  1028. te->draw_line(ofs + Point2(0, y + h), ofs + Point2(size.width, y + h), sepcolor);
  1029. Point2 icon_ofs = ofs + Point2(size.width, y + (h - remove_icon->get_height()) / 2).floor();
  1030. icon_ofs.y += 4 * EDSCALE;
  1031. /* icon_ofs.x-=remove_icon->get_width();
  1032. te->draw_texture((mouse_over.over==MouseOver::OVER_REMOVE && mouse_over.track==idx)?remove_icon_hl:remove_icon,icon_ofs);
  1033. icon_ofs.x-=hsep;
  1034. icon_ofs.x-=move_down_icon->get_width();
  1035. te->draw_texture((mouse_over.over==MouseOver::OVER_DOWN && mouse_over.track==idx)?move_down_icon_hl:move_down_icon,icon_ofs);
  1036. icon_ofs.x-=hsep;
  1037. icon_ofs.x-=move_up_icon->get_width();
  1038. te->draw_texture((mouse_over.over==MouseOver::OVER_UP && mouse_over.track==idx)?move_up_icon_hl:move_up_icon,icon_ofs);
  1039. icon_ofs.x-=hsep;
  1040. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  1041. icon_ofs.x-=hsep;
  1042. */
  1043. track_ofs[0] = size.width - icon_ofs.x;
  1044. icon_ofs.x -= down_icon->get_width();
  1045. te->draw_texture(down_icon, icon_ofs - Size2(0, 4 * EDSCALE));
  1046. int wrap_type = animation->track_get_interpolation_loop_wrap(idx) ? 1 : 0;
  1047. icon_ofs.x -= hsep;
  1048. icon_ofs.x -= wrap_icon[wrap_type]->get_width();
  1049. te->draw_texture(wrap_icon[wrap_type], icon_ofs);
  1050. icon_ofs.x -= hsep;
  1051. te->draw_line(Point2(icon_ofs.x, ofs.y + y), Point2(icon_ofs.x, ofs.y + y + h), sepcolor);
  1052. track_ofs[1] = size.width - icon_ofs.x;
  1053. icon_ofs.x -= down_icon->get_width();
  1054. te->draw_texture(down_icon, icon_ofs - Size2(0, 4 * EDSCALE));
  1055. int interp_type = animation->track_get_interpolation_type(idx);
  1056. ERR_CONTINUE(interp_type < 0 || interp_type >= 3);
  1057. icon_ofs.x -= hsep;
  1058. icon_ofs.x -= interp_icon[interp_type]->get_width();
  1059. te->draw_texture(interp_icon[interp_type], icon_ofs);
  1060. icon_ofs.x -= hsep;
  1061. te->draw_line(Point2(icon_ofs.x, ofs.y + y), Point2(icon_ofs.x, ofs.y + y + h), sepcolor);
  1062. track_ofs[2] = size.width - icon_ofs.x;
  1063. if (animation->track_get_type(idx) == Animation::TYPE_VALUE) {
  1064. int umode = animation->value_track_get_update_mode(idx);
  1065. icon_ofs.x -= hsep;
  1066. icon_ofs.x -= down_icon->get_width();
  1067. te->draw_texture(down_icon, icon_ofs - Size2(0, 4 * EDSCALE));
  1068. icon_ofs.x -= hsep;
  1069. icon_ofs.x -= cont_icon[umode]->get_width();
  1070. te->draw_texture(cont_icon[umode], icon_ofs);
  1071. } else {
  1072. icon_ofs.x -= hsep * 2 + cont_icon[0]->get_width() + down_icon->get_width();
  1073. }
  1074. icon_ofs.x -= hsep;
  1075. te->draw_line(Point2(icon_ofs.x, ofs.y + y), Point2(icon_ofs.x, ofs.y + y + h), sepcolor);
  1076. track_ofs[3] = size.width - icon_ofs.x;
  1077. icon_ofs.x -= hsep;
  1078. icon_ofs.x -= add_key_icon->get_width();
  1079. te->draw_texture((mouse_over.over == MouseOver::OVER_ADD_KEY && mouse_over.track == idx) ? add_key_icon_hl : add_key_icon, icon_ofs);
  1080. track_ofs[4] = size.width - icon_ofs.x;
  1081. //draw the keys;
  1082. int tt = animation->track_get_type(idx);
  1083. float key_vofs = Math::floor((float)(h - type_icon[tt]->get_height()) / 2);
  1084. float key_hofs = -Math::floor((float)type_icon[tt]->get_height() / 2);
  1085. int kc = animation->track_get_key_count(idx);
  1086. bool first = true;
  1087. for (int i = 0; i < kc; i++) {
  1088. float time = animation->track_get_key_time(idx, i);
  1089. if (time < keys_from)
  1090. continue;
  1091. if (time > keys_to) {
  1092. if (first && i > 0 && animation->track_get_key_value(idx, i) == animation->track_get_key_value(idx, i - 1)) {
  1093. //draw whole line
  1094. te->draw_line(ofs + Vector2(name_limit, y + h / 2), ofs + Point2(settings_limit, y + h / 2), color);
  1095. }
  1096. break;
  1097. }
  1098. float x = key_hofs + name_limit + (time - keys_from) * zoom_scale;
  1099. Ref<Texture> tex = type_icon[tt];
  1100. SelectedKey sk;
  1101. sk.key = i;
  1102. sk.track = idx;
  1103. if (selection.has(sk)) {
  1104. if (click.click == ClickOver::CLICK_MOVE_KEYS)
  1105. continue;
  1106. tex = type_selected;
  1107. }
  1108. if (mouse_over.over == MouseOver::OVER_KEY && mouse_over.track == idx && mouse_over.over_key == i)
  1109. tex = type_hover;
  1110. Variant value = animation->track_get_key_value(idx, i);
  1111. if (first && i > 0 && value == animation->track_get_key_value(idx, i - 1)) {
  1112. te->draw_line(ofs + Vector2(name_limit, y + h / 2), ofs + Point2(x, y + h / 2), color);
  1113. }
  1114. if (i < kc - 1 && value == animation->track_get_key_value(idx, i + 1)) {
  1115. float x_n = key_hofs + name_limit + (animation->track_get_key_time(idx, i + 1) - keys_from) * zoom_scale;
  1116. x_n = MIN(x_n, settings_limit);
  1117. te->draw_line(ofs + Point2(x_n, y + h / 2), ofs + Point2(x, y + h / 2), color);
  1118. }
  1119. if (prop_exists && !Variant::can_convert(value.get_type(), valid_type)) {
  1120. te->draw_texture(invalid_icon, ofs + Point2(x, y + key_vofs).floor());
  1121. }
  1122. if (prop_exists && !Variant::can_convert(value.get_type(), valid_type)) {
  1123. if (tex == type_hover)
  1124. te->draw_texture(invalid_icon_hover, ofs + Point2(x, y + key_vofs).floor());
  1125. else
  1126. te->draw_texture(invalid_icon, ofs + Point2(x, y + key_vofs).floor());
  1127. } else {
  1128. te->draw_texture(tex, ofs + Point2(x, y + key_vofs).floor());
  1129. }
  1130. first = false;
  1131. }
  1132. }
  1133. switch (click.click) {
  1134. case ClickOver::CLICK_SELECT_KEYS: {
  1135. Color box_color = get_color("accent_color", "Editor");
  1136. box_color.a = 0.35;
  1137. te->draw_rect(Rect2(click.at, click.to - click.at), box_color);
  1138. } break;
  1139. case ClickOver::CLICK_MOVE_KEYS: {
  1140. float from_t = 1e20;
  1141. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1142. float t = animation->track_get_key_time(E->key().track, E->key().key);
  1143. if (t < from_t)
  1144. from_t = t;
  1145. }
  1146. float motion = from_t + (click.to.x - click.at.x) / zoom_scale;
  1147. if (step->get_value())
  1148. motion = Math::stepify(motion, step->get_value());
  1149. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1150. int idx = E->key().track;
  1151. int i = idx - v_scroll->get_value();
  1152. if (i < 0 || i >= fit)
  1153. continue;
  1154. int y = h + i * h + sep;
  1155. float key_vofs = Math::floor((float)(h - type_selected->get_height()) / 2);
  1156. float key_hofs = -Math::floor((float)type_selected->get_height() / 2);
  1157. float time = animation->track_get_key_time(idx, E->key().key);
  1158. float diff = time - from_t;
  1159. float t = motion + diff;
  1160. float x = (t - keys_from) * zoom_scale;
  1161. //x+=click.to.x - click.at.x;
  1162. if (x < 0 || x >= (settings_limit - name_limit))
  1163. continue;
  1164. x += name_limit;
  1165. te->draw_texture(type_selected, ofs + Point2(x + key_hofs, y + key_vofs).floor());
  1166. }
  1167. } break;
  1168. default: {};
  1169. }
  1170. te_drawing = false;
  1171. }
  1172. void AnimationKeyEditor::_track_name_changed(const String &p_name) {
  1173. ERR_FAIL_COND(!animation.is_valid());
  1174. undo_redo->create_action(TTR("Anim Track Rename"));
  1175. undo_redo->add_do_method(animation.ptr(), "track_set_path", track_name_editing, p_name);
  1176. undo_redo->add_undo_method(animation.ptr(), "track_set_path", track_name_editing, animation->track_get_path(track_name_editing));
  1177. undo_redo->commit_action();
  1178. track_name->hide();
  1179. }
  1180. void AnimationKeyEditor::_track_menu_selected(int p_idx) {
  1181. ERR_FAIL_COND(!animation.is_valid());
  1182. if (interp_editing != -1) {
  1183. ERR_FAIL_INDEX(interp_editing, animation->get_track_count());
  1184. undo_redo->create_action(TTR("Anim Track Change Interpolation"));
  1185. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", interp_editing, p_idx);
  1186. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", interp_editing, animation->track_get_interpolation_type(interp_editing));
  1187. undo_redo->commit_action();
  1188. } else if (cont_editing != -1) {
  1189. ERR_FAIL_INDEX(cont_editing, animation->get_track_count());
  1190. undo_redo->create_action(TTR("Anim Track Change Value Mode"));
  1191. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", cont_editing, p_idx);
  1192. undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", cont_editing, animation->value_track_get_update_mode(cont_editing));
  1193. undo_redo->commit_action();
  1194. } else if (wrap_editing != -1) {
  1195. ERR_FAIL_INDEX(wrap_editing, animation->get_track_count());
  1196. undo_redo->create_action(TTR("Anim Track Change Wrap Mode"));
  1197. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", wrap_editing, p_idx ? true : false);
  1198. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_loop_wrap", wrap_editing, animation->track_get_interpolation_loop_wrap(wrap_editing));
  1199. undo_redo->commit_action();
  1200. } else {
  1201. switch (p_idx) {
  1202. case RIGHT_MENU_DUPLICATE:
  1203. _anim_duplicate_keys();
  1204. break;
  1205. case RIGHT_MENU_DUPLICATE_TRANSPOSE:
  1206. _anim_duplicate_keys(true);
  1207. break;
  1208. case RIGHT_MENU_REMOVE:
  1209. _anim_delete_keys();
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. struct _AnimMoveRestore {
  1215. int track;
  1216. float time;
  1217. Variant key;
  1218. float transition;
  1219. };
  1220. void AnimationKeyEditor::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
  1221. if (!(animation == p_anim))
  1222. return;
  1223. //selection.clear();
  1224. _clear_selection();
  1225. }
  1226. void AnimationKeyEditor::_select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos) {
  1227. if (!(animation == p_anim))
  1228. return;
  1229. int idx = animation->track_find_key(p_track, p_pos, true);
  1230. ERR_FAIL_COND(idx < 0);
  1231. SelectedKey sk;
  1232. sk.track = p_track;
  1233. sk.key = idx;
  1234. KeyInfo ki;
  1235. ki.pos = p_pos;
  1236. selection.insert(sk, ki);
  1237. }
  1238. PropertyInfo AnimationKeyEditor::_find_hint_for_track(int p_idx, NodePath &r_base_path) {
  1239. r_base_path = NodePath();
  1240. ERR_FAIL_COND_V(!animation.is_valid(), PropertyInfo());
  1241. ERR_FAIL_INDEX_V(p_idx, animation->get_track_count(), PropertyInfo());
  1242. if (!root)
  1243. return PropertyInfo();
  1244. NodePath path = animation->track_get_path(p_idx);
  1245. if (!root->has_node_and_resource(path))
  1246. return PropertyInfo();
  1247. RES res;
  1248. Node *node = root->get_node_and_resource(path, res);
  1249. if (node) {
  1250. r_base_path = node->get_path();
  1251. }
  1252. String property = path.get_property();
  1253. if (property == "")
  1254. return PropertyInfo();
  1255. List<PropertyInfo> pinfo;
  1256. if (res.is_valid())
  1257. res->get_property_list(&pinfo);
  1258. else if (node)
  1259. node->get_property_list(&pinfo);
  1260. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1261. if (E->get().name == property)
  1262. return E->get();
  1263. }
  1264. return PropertyInfo();
  1265. }
  1266. void AnimationKeyEditor::_curve_transition_changed(float p_what) {
  1267. if (selection.size() == 0)
  1268. return;
  1269. if (selection.size() == 1)
  1270. undo_redo->create_action(TTR("Edit Node Curve"), UndoRedo::MERGE_ENDS);
  1271. else
  1272. undo_redo->create_action(TTR("Edit Selection Curve"), UndoRedo::MERGE_ENDS);
  1273. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1274. int track = E->key().track;
  1275. int key = E->key().key;
  1276. float prev_val = animation->track_get_key_transition(track, key);
  1277. undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", track, key, p_what);
  1278. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", track, key, prev_val);
  1279. }
  1280. undo_redo->commit_action();
  1281. }
  1282. void AnimationKeyEditor::_toggle_edit_curves() {
  1283. if (edit_button->is_pressed())
  1284. key_editor_tab->show();
  1285. else
  1286. key_editor_tab->hide();
  1287. }
  1288. bool AnimationKeyEditor::_edit_if_single_selection() {
  1289. if (selection.size() != 1) {
  1290. if (selection.size() == 0) {
  1291. curve_edit->set_mode(AnimationCurveEdit::MODE_DISABLED);
  1292. //print_line("disable");
  1293. } else {
  1294. curve_edit->set_mode(AnimationCurveEdit::MODE_MULTIPLE);
  1295. curve_edit->set_transition(1.0);
  1296. curve_edit->clear_multiples();
  1297. //add all
  1298. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1299. curve_edit->set_multiple(animation->track_get_key_transition(E->key().track, E->key().key));
  1300. }
  1301. //print_line("multiple");
  1302. }
  1303. return false;
  1304. }
  1305. curve_edit->set_mode(AnimationCurveEdit::MODE_SINGLE);
  1306. //print_line("regular");
  1307. int idx = selection.front()->key().track;
  1308. int key = selection.front()->key().key;
  1309. {
  1310. key_edit->animation = animation;
  1311. key_edit->track = idx;
  1312. key_edit->key_ofs = animation->track_get_key_time(idx, key);
  1313. key_edit->hint = _find_hint_for_track(idx, key_edit->base);
  1314. key_edit->notify_change();
  1315. curve_edit->set_transition(animation->track_get_key_transition(idx, key));
  1316. /*key_edit_dialog->set_size( Size2( 200,200) );
  1317. key_edit_dialog->set_position( track_editor->get_global_position() + ofs + mpos +Point2(-100,20));
  1318. key_edit_dialog->popup();*/
  1319. }
  1320. return true;
  1321. }
  1322. void AnimationKeyEditor::_anim_delete_keys() {
  1323. if (selection.size()) {
  1324. undo_redo->create_action(TTR("Anim Delete Keys"));
  1325. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1326. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  1327. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  1328. }
  1329. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1330. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1331. undo_redo->commit_action();
  1332. //selection.clear();
  1333. accept_event();
  1334. _edit_if_single_selection();
  1335. }
  1336. }
  1337. void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input) {
  1338. Control *te = track_editor;
  1339. Ref<StyleBox> style = get_stylebox("normal", "TextEdit");
  1340. if (!animation.is_valid()) {
  1341. return;
  1342. }
  1343. Size2 size = te->get_size() - style->get_minimum_size();
  1344. Size2 ofs = style->get_offset();
  1345. Ref<Font> font = te->get_font("font", "Tree");
  1346. int sep = get_constant("vseparation", "Tree");
  1347. int hsep = get_constant("hseparation", "Tree");
  1348. Ref<Texture> remove_icon = get_icon("Remove", "EditorIcons");
  1349. Ref<Texture> move_up_icon = get_icon("MoveUp", "EditorIcons");
  1350. Ref<Texture> move_down_icon = get_icon("MoveDown", "EditorIcons");
  1351. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  1352. Ref<Texture> hsize_icon = get_icon("Hsize", "EditorIcons");
  1353. Ref<Texture> add_key_icon = get_icon("TrackAddKey", "EditorIcons");
  1354. Ref<Texture> wrap_icon[2] = {
  1355. get_icon("InterpWrapClamp", "EditorIcons"),
  1356. get_icon("InterpWrapLoop", "EditorIcons"),
  1357. };
  1358. Ref<Texture> interp_icon[3] = {
  1359. get_icon("InterpRaw", "EditorIcons"),
  1360. get_icon("InterpLinear", "EditorIcons"),
  1361. get_icon("InterpCubic", "EditorIcons")
  1362. };
  1363. Ref<Texture> cont_icon[3] = {
  1364. get_icon("TrackContinuous", "EditorIcons"),
  1365. get_icon("TrackDiscrete", "EditorIcons"),
  1366. get_icon("TrackTrigger", "EditorIcons")
  1367. };
  1368. Ref<Texture> type_icon[3] = {
  1369. get_icon("KeyValue", "EditorIcons"),
  1370. get_icon("KeyXform", "EditorIcons"),
  1371. get_icon("KeyCall", "EditorIcons")
  1372. };
  1373. int right_separator_ofs = down_icon->get_width() * 3 + add_key_icon->get_width() + interp_icon[0]->get_width() + wrap_icon[0]->get_width() + cont_icon[0]->get_width() + hsep * 9;
  1374. int h = font->get_height() + sep;
  1375. int fit = (size.height / h) - 1;
  1376. int total = animation->get_track_count();
  1377. if (total < fit) {
  1378. v_scroll->hide();
  1379. } else {
  1380. v_scroll->show();
  1381. v_scroll->set_max(total);
  1382. v_scroll->set_page(fit);
  1383. }
  1384. int settings_limit = size.width - right_separator_ofs;
  1385. int name_limit = settings_limit * name_column_ratio;
  1386. Ref<InputEventKey> key = p_input;
  1387. if (key.is_valid()) {
  1388. if (key->get_scancode() == KEY_D && key->is_pressed() && key->get_command()) {
  1389. if (key->get_shift())
  1390. _menu_track(TRACK_MENU_DUPLICATE_TRANSPOSE);
  1391. else
  1392. _menu_track(TRACK_MENU_DUPLICATE);
  1393. accept_event();
  1394. } else if (key->get_scancode() == KEY_DELETE && key->is_pressed() && click.click == ClickOver::CLICK_NONE) {
  1395. _anim_delete_keys();
  1396. } else if (animation.is_valid() && animation->get_track_count() > 0) {
  1397. if (key->is_pressed() && (key->is_action("ui_up") || key->is_action("ui_page_up"))) {
  1398. if (key->is_action("ui_up"))
  1399. selected_track--;
  1400. if (v_scroll->is_visible_in_tree() && key->is_action("ui_page_up"))
  1401. selected_track--;
  1402. if (selected_track < 0)
  1403. selected_track = 0;
  1404. if (v_scroll->is_visible_in_tree()) {
  1405. if (v_scroll->get_value() > selected_track)
  1406. v_scroll->set_value(selected_track);
  1407. }
  1408. track_editor->update();
  1409. accept_event();
  1410. }
  1411. if (key->is_pressed() && (key->is_action("ui_down") || key->is_action("ui_page_down"))) {
  1412. if (key->is_action("ui_down"))
  1413. selected_track++;
  1414. else if (v_scroll->is_visible_in_tree() && key->is_action("ui_page_down"))
  1415. selected_track += v_scroll->get_page();
  1416. if (selected_track >= animation->get_track_count())
  1417. selected_track = animation->get_track_count() - 1;
  1418. if (v_scroll->is_visible_in_tree() && v_scroll->get_page() + v_scroll->get_value() < selected_track + 1) {
  1419. v_scroll->set_value(selected_track - v_scroll->get_page() + 1);
  1420. }
  1421. track_editor->update();
  1422. accept_event();
  1423. }
  1424. }
  1425. }
  1426. Ref<InputEventMouseButton> mb = p_input;
  1427. if (mb.is_valid()) {
  1428. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  1429. if (mb->get_command()) {
  1430. zoom->set_value(zoom->get_value() + zoom->get_step());
  1431. } else {
  1432. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * mb->get_factor() / 8);
  1433. }
  1434. }
  1435. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  1436. if (mb->get_command()) {
  1437. zoom->set_value(zoom->get_value() - zoom->get_step());
  1438. } else {
  1439. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * mb->get_factor() / 8);
  1440. }
  1441. }
  1442. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT && mb->is_pressed()) {
  1443. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb->get_factor() / 8);
  1444. }
  1445. if (mb->get_button_index() == BUTTON_WHEEL_LEFT && mb->is_pressed()) {
  1446. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * mb->get_factor() / 8);
  1447. }
  1448. if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  1449. Point2 mpos = mb->get_position() - ofs;
  1450. if (selection.size() == 0) {
  1451. // Auto-select on right-click if nothing is selected
  1452. // Note: This code is pretty much duplicated from the left click code,
  1453. // both codes could be moved into a function to avoid the duplicated code.
  1454. Point2 mpos = mb->get_position() - ofs;
  1455. if (mpos.y < h) {
  1456. return;
  1457. }
  1458. mpos.y -= h;
  1459. int idx = mpos.y / h;
  1460. idx += v_scroll->get_value();
  1461. if (idx < 0 || idx >= animation->get_track_count())
  1462. return;
  1463. if (mpos.x < name_limit) {
  1464. } else if (mpos.x < settings_limit) {
  1465. float pos = mpos.x - name_limit;
  1466. pos /= _get_zoom_scale();
  1467. pos += h_scroll->get_value();
  1468. float w_time = (type_icon[0]->get_width() / _get_zoom_scale()) / 2.0;
  1469. int kidx = animation->track_find_key(idx, pos);
  1470. int kidx_n = kidx + 1;
  1471. int key = -1;
  1472. if (kidx >= 0 && kidx < animation->track_get_key_count(idx)) {
  1473. float kpos = animation->track_get_key_time(idx, kidx);
  1474. if (ABS(pos - kpos) <= w_time) {
  1475. key = kidx;
  1476. }
  1477. }
  1478. if (key == -1 && kidx_n >= 0 && kidx_n < animation->track_get_key_count(idx)) {
  1479. float kpos = animation->track_get_key_time(idx, kidx_n);
  1480. if (ABS(pos - kpos) <= w_time) {
  1481. key = kidx_n;
  1482. }
  1483. }
  1484. if (key == -1) {
  1485. click.click = ClickOver::CLICK_SELECT_KEYS;
  1486. click.at = mb->get_position();
  1487. click.to = click.at;
  1488. click.shift = mb->get_shift();
  1489. selected_track = idx;
  1490. track_editor->update();
  1491. //drag select region
  1492. return;
  1493. }
  1494. SelectedKey sk;
  1495. sk.track = idx;
  1496. sk.key = key;
  1497. KeyInfo ki;
  1498. ki.pos = animation->track_get_key_time(idx, key);
  1499. click.shift = mb->get_shift();
  1500. click.selk = sk;
  1501. if (!mb->get_shift() && !selection.has(sk))
  1502. _clear_selection();
  1503. selection.insert(sk, ki);
  1504. click.click = ClickOver::CLICK_MOVE_KEYS;
  1505. click.at = mb->get_position();
  1506. click.to = click.at;
  1507. update();
  1508. selected_track = idx;
  1509. track_editor->update();
  1510. if (_edit_if_single_selection() && mb->get_command()) {
  1511. edit_button->set_pressed(true);
  1512. key_editor_tab->show();
  1513. }
  1514. }
  1515. }
  1516. if (selection.size()) {
  1517. // User has right clicked and we have a selection, show a popup menu with options
  1518. track_menu->clear();
  1519. track_menu->set_size(Point2(1, 1));
  1520. track_menu->add_item(TTR("Duplicate Selection"), RIGHT_MENU_DUPLICATE);
  1521. track_menu->add_item(TTR("Duplicate Transposed"), RIGHT_MENU_DUPLICATE_TRANSPOSE);
  1522. track_menu->add_item(TTR("Remove Selection"), RIGHT_MENU_REMOVE);
  1523. track_menu->set_position(te->get_global_position() + mpos);
  1524. interp_editing = -1;
  1525. cont_editing = -1;
  1526. wrap_editing = -1;
  1527. track_menu->popup();
  1528. }
  1529. }
  1530. if (mb->get_button_index() == BUTTON_LEFT && !(mb->get_button_mask() & ~BUTTON_MASK_LEFT)) {
  1531. if (mb->is_pressed()) {
  1532. Point2 mpos = mb->get_position() - ofs;
  1533. if (mpos.y < h) {
  1534. if (mpos.x < name_limit && mpos.x > (name_limit - hsep - hsize_icon->get_width())) {
  1535. click.click = ClickOver::CLICK_RESIZE_NAMES;
  1536. click.at = mb->get_position();
  1537. click.to = click.at;
  1538. click.at.y = name_limit;
  1539. }
  1540. if (mpos.x >= name_limit && mpos.x < settings_limit) {
  1541. //seek
  1542. //int zoomw = settings_limit-name_limit;
  1543. float scale = _get_zoom_scale();
  1544. float pos = h_scroll->get_value() + (mpos.x - name_limit) / scale;
  1545. if (animation->get_step())
  1546. pos = Math::stepify(pos, animation->get_step());
  1547. if (pos < 0)
  1548. pos = 0;
  1549. if (pos >= animation->get_length())
  1550. pos = animation->get_length();
  1551. timeline_pos = pos;
  1552. click.click = ClickOver::CLICK_DRAG_TIMELINE;
  1553. click.at = mb->get_position();
  1554. click.to = click.at;
  1555. emit_signal("timeline_changed", pos, false);
  1556. }
  1557. return;
  1558. }
  1559. mpos.y -= h;
  1560. int idx = mpos.y / h;
  1561. idx += v_scroll->get_value();
  1562. if (idx < 0)
  1563. return;
  1564. if (idx >= animation->get_track_count()) {
  1565. if (mpos.x >= name_limit && mpos.x < settings_limit) {
  1566. click.click = ClickOver::CLICK_SELECT_KEYS;
  1567. click.at = mb->get_position();
  1568. click.to = click.at;
  1569. //drag select region
  1570. }
  1571. return;
  1572. }
  1573. if (mpos.x < name_limit) {
  1574. //name column
  1575. // area
  1576. if (idx != selected_track) {
  1577. selected_track = idx;
  1578. track_editor->update();
  1579. return;
  1580. }
  1581. Rect2 area(ofs.x, ofs.y + ((int(mpos.y) / h) + 1) * h, name_limit, h);
  1582. track_name->set_text(animation->track_get_path(idx));
  1583. track_name->set_position(te->get_global_position() + area.position);
  1584. track_name->set_size(area.size);
  1585. track_name->show_modal();
  1586. track_name->grab_focus();
  1587. track_name->select_all();
  1588. track_name_editing = idx;
  1589. } else if (mpos.x < settings_limit) {
  1590. float pos = mpos.x - name_limit;
  1591. pos /= _get_zoom_scale();
  1592. pos += h_scroll->get_value();
  1593. float w_time = (type_icon[0]->get_width() / _get_zoom_scale()) / 2.0;
  1594. int kidx = animation->track_find_key(idx, pos);
  1595. int kidx_n = kidx + 1;
  1596. int key = -1;
  1597. if (kidx >= 0 && kidx < animation->track_get_key_count(idx)) {
  1598. float kpos = animation->track_get_key_time(idx, kidx);
  1599. if (ABS(pos - kpos) <= w_time) {
  1600. key = kidx;
  1601. }
  1602. }
  1603. if (key == -1 && kidx_n >= 0 && kidx_n < animation->track_get_key_count(idx)) {
  1604. float kpos = animation->track_get_key_time(idx, kidx_n);
  1605. if (ABS(pos - kpos) <= w_time) {
  1606. key = kidx_n;
  1607. }
  1608. }
  1609. if (key == -1) {
  1610. click.click = ClickOver::CLICK_SELECT_KEYS;
  1611. click.at = mb->get_position();
  1612. click.to = click.at;
  1613. click.shift = mb->get_shift();
  1614. selected_track = idx;
  1615. track_editor->update();
  1616. //drag select region
  1617. return;
  1618. }
  1619. SelectedKey sk;
  1620. sk.track = idx;
  1621. sk.key = key;
  1622. KeyInfo ki;
  1623. ki.pos = animation->track_get_key_time(idx, key);
  1624. click.shift = mb->get_shift();
  1625. click.selk = sk;
  1626. if (!mb->get_shift() && !selection.has(sk))
  1627. _clear_selection();
  1628. selection.insert(sk, ki);
  1629. click.click = ClickOver::CLICK_MOVE_KEYS;
  1630. click.at = mb->get_position();
  1631. click.to = click.at;
  1632. update();
  1633. selected_track = idx;
  1634. track_editor->update();
  1635. if (_edit_if_single_selection() && mb->get_command()) {
  1636. edit_button->set_pressed(true);
  1637. key_editor_tab->show();
  1638. }
  1639. } else {
  1640. //button column
  1641. int ofsx = size.width - mpos.x;
  1642. if (ofsx < 0)
  1643. return;
  1644. /*
  1645. if (ofsx < remove_icon->get_width()) {
  1646. undo_redo->create_action("Remove Anim Track");
  1647. undo_redo->add_do_method(animation.ptr(),"remove_track",idx);
  1648. undo_redo->add_undo_method(animation.ptr(),"add_track",animation->track_get_type(idx),idx);
  1649. undo_redo->add_undo_method(animation.ptr(),"track_set_path",idx,animation->track_get_path(idx));
  1650. //todo interpolation
  1651. for(int i=0;i<animation->track_get_key_count(idx);i++) {
  1652. Variant v = animation->track_get_key_value(idx,i);
  1653. float time = animation->track_get_key_time(idx,i);
  1654. float trans = animation->track_get_key_transition(idx,i);
  1655. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,time,v);
  1656. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",idx,i,trans);
  1657. }
  1658. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",idx,animation->track_get_interpolation_type(idx));
  1659. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  1660. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",idx,animation->value_track_is_continuous(idx));
  1661. }
  1662. undo_redo->commit_action();
  1663. return;
  1664. }
  1665. ofsx-=hsep+remove_icon->get_width();
  1666. if (ofsx < move_down_icon->get_width()) {
  1667. if (idx < animation->get_track_count() -1) {
  1668. undo_redo->create_action("Move Anim Track Down");
  1669. undo_redo->add_do_method(animation.ptr(),"track_move_up",idx);
  1670. undo_redo->add_undo_method(animation.ptr(),"track_move_down",idx+1);
  1671. undo_redo->commit_action();
  1672. }
  1673. return;
  1674. }
  1675. ofsx-=hsep+move_down_icon->get_width();
  1676. if (ofsx < move_up_icon->get_width()) {
  1677. if (idx >0) {
  1678. undo_redo->create_action("Move Anim Track Up");
  1679. undo_redo->add_do_method(animation.ptr(),"track_move_down",idx);
  1680. undo_redo->add_undo_method(animation.ptr(),"track_move_up",idx-1);
  1681. undo_redo->commit_action();
  1682. }
  1683. return;
  1684. }
  1685. ofsx-=hsep*3+move_up_icon->get_width();
  1686. */
  1687. if (ofsx < track_ofs[1]) {
  1688. track_menu->clear();
  1689. track_menu->set_size(Point2(1, 1));
  1690. static const char *interp_name[2] = { "Clamp Loop Interp", "Wrap Loop Interp" };
  1691. for (int i = 0; i < 2; i++) {
  1692. track_menu->add_icon_item(wrap_icon[i], interp_name[i]);
  1693. }
  1694. int popup_y = ofs.y + ((int(mpos.y) / h) + 2) * h;
  1695. int popup_x = size.width - track_ofs[1];
  1696. track_menu->set_position(te->get_global_position() + Point2(popup_x, popup_y));
  1697. wrap_editing = idx;
  1698. interp_editing = -1;
  1699. cont_editing = -1;
  1700. track_menu->popup();
  1701. return;
  1702. }
  1703. if (ofsx < track_ofs[2]) {
  1704. track_menu->clear();
  1705. track_menu->set_size(Point2(1, 1));
  1706. static const char *interp_name[3] = { "Nearest", "Linear", "Cubic" };
  1707. for (int i = 0; i < 3; i++) {
  1708. track_menu->add_icon_item(interp_icon[i], interp_name[i]);
  1709. }
  1710. int popup_y = ofs.y + ((int(mpos.y) / h) + 2) * h;
  1711. int popup_x = size.width - track_ofs[2];
  1712. track_menu->set_position(te->get_global_position() + Point2(popup_x, popup_y));
  1713. interp_editing = idx;
  1714. cont_editing = -1;
  1715. wrap_editing = -1;
  1716. track_menu->popup();
  1717. return;
  1718. }
  1719. if (ofsx < track_ofs[3]) {
  1720. track_menu->clear();
  1721. track_menu->set_size(Point2(1, 1));
  1722. String cont_name[3] = { TTR("Continuous"), TTR("Discrete"), TTR("Trigger") };
  1723. for (int i = 0; i < 3; i++) {
  1724. track_menu->add_icon_item(cont_icon[i], cont_name[i]);
  1725. }
  1726. int popup_y = ofs.y + ((int(mpos.y) / h) + 2) * h;
  1727. int popup_x = size.width - track_ofs[3];
  1728. track_menu->set_position(te->get_global_position() + Point2(popup_x, popup_y));
  1729. interp_editing = -1;
  1730. wrap_editing = -1;
  1731. cont_editing = idx;
  1732. track_menu->popup();
  1733. return;
  1734. }
  1735. if (ofsx < track_ofs[4]) {
  1736. Animation::TrackType tt = animation->track_get_type(idx);
  1737. float pos = timeline_pos;
  1738. int existing = animation->track_find_key(idx, pos, true);
  1739. Variant newval;
  1740. if (tt == Animation::TYPE_TRANSFORM) {
  1741. Dictionary d;
  1742. d["location"] = Vector3();
  1743. d["rotation"] = Quat();
  1744. d["scale"] = Vector3();
  1745. newval = d;
  1746. } else if (tt == Animation::TYPE_METHOD) {
  1747. Dictionary d;
  1748. d["method"] = "";
  1749. d["args"] = Vector<Variant>();
  1750. newval = d;
  1751. } else if (tt == Animation::TYPE_VALUE) {
  1752. NodePath np;
  1753. PropertyInfo inf = _find_hint_for_track(idx, np);
  1754. if (inf.type != Variant::NIL) {
  1755. Variant::CallError err;
  1756. newval = Variant::construct(inf.type, NULL, 0, err);
  1757. }
  1758. if (newval.get_type() == Variant::NIL) {
  1759. //popup a new type
  1760. cvi_track = idx;
  1761. cvi_pos = pos;
  1762. type_menu->set_position(get_global_position() + mpos + ofs);
  1763. type_menu->popup();
  1764. return;
  1765. }
  1766. }
  1767. undo_redo->create_action(TTR("Anim Add Key"));
  1768. undo_redo->add_do_method(animation.ptr(), "track_insert_key", idx, pos, newval, 1);
  1769. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", idx, pos);
  1770. if (existing != -1) {
  1771. Variant v = animation->track_get_key_value(idx, existing);
  1772. float trans = animation->track_get_key_transition(idx, existing);
  1773. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", idx, pos, v, trans);
  1774. }
  1775. undo_redo->commit_action();
  1776. return;
  1777. }
  1778. }
  1779. } else {
  1780. switch (click.click) {
  1781. case ClickOver::CLICK_SELECT_KEYS: {
  1782. float zoom_scale = _get_zoom_scale();
  1783. float keys_from = h_scroll->get_value();
  1784. float keys_to = keys_from + (settings_limit - name_limit) / zoom_scale;
  1785. float from_time = keys_from + (click.at.x - (name_limit + ofs.x)) / zoom_scale;
  1786. float to_time = keys_from + (click.to.x - (name_limit + ofs.x)) / zoom_scale;
  1787. if (to_time < from_time)
  1788. SWAP(from_time, to_time);
  1789. if (from_time > keys_to || to_time < keys_from)
  1790. break;
  1791. if (from_time < keys_from)
  1792. from_time = keys_from;
  1793. if (to_time >= keys_to)
  1794. to_time = keys_to;
  1795. int from_track = int(click.at.y - ofs.y - h - sep) / h + v_scroll->get_value();
  1796. int to_track = int(click.to.y - ofs.y - h - sep) / h + v_scroll->get_value();
  1797. int from_mod = int(click.at.y - ofs.y - sep) % h;
  1798. int to_mod = int(click.to.y - ofs.y - sep) % h;
  1799. if (to_track < from_track) {
  1800. SWAP(from_track, to_track);
  1801. SWAP(from_mod, to_mod);
  1802. }
  1803. if ((from_mod > (h / 2)) && ((click.at.y - ofs.y) >= (h + sep))) {
  1804. from_track++;
  1805. }
  1806. if (to_mod < h / 2) {
  1807. to_track--;
  1808. }
  1809. if (from_track > to_track) {
  1810. if (!click.shift)
  1811. _clear_selection();
  1812. _edit_if_single_selection();
  1813. break;
  1814. }
  1815. int tracks_from = v_scroll->get_value();
  1816. int tracks_to = v_scroll->get_value() + fit - 1;
  1817. if (tracks_to >= animation->get_track_count())
  1818. tracks_to = animation->get_track_count() - 1;
  1819. tracks_from = 0;
  1820. tracks_to = animation->get_track_count() - 1;
  1821. if (to_track > tracks_to)
  1822. to_track = tracks_to;
  1823. if (from_track < tracks_from)
  1824. from_track = tracks_from;
  1825. if (from_track > tracks_to || to_track < tracks_from) {
  1826. if (!click.shift)
  1827. _clear_selection();
  1828. _edit_if_single_selection();
  1829. break;
  1830. }
  1831. if (!click.shift)
  1832. _clear_selection();
  1833. int higher_track = 0x7FFFFFFF;
  1834. for (int i = from_track; i <= to_track; i++) {
  1835. int kc = animation->track_get_key_count(i);
  1836. for (int j = 0; j < kc; j++) {
  1837. float t = animation->track_get_key_time(i, j);
  1838. if (t < from_time)
  1839. continue;
  1840. if (t > to_time)
  1841. break;
  1842. if (i < higher_track)
  1843. higher_track = i;
  1844. SelectedKey sk;
  1845. sk.track = i;
  1846. sk.key = j;
  1847. KeyInfo ki;
  1848. ki.pos = t;
  1849. selection[sk] = ki;
  1850. }
  1851. }
  1852. if (higher_track != 0x7FFFFFFF) {
  1853. selected_track = higher_track;
  1854. track_editor->update();
  1855. }
  1856. _edit_if_single_selection();
  1857. } break;
  1858. case ClickOver::CLICK_MOVE_KEYS: {
  1859. if (selection.empty())
  1860. break;
  1861. if (click.at == click.to) {
  1862. if (!click.shift) {
  1863. KeyInfo ki = selection[click.selk];
  1864. _clear_selection();
  1865. selection[click.selk] = ki;
  1866. _edit_if_single_selection();
  1867. }
  1868. break;
  1869. }
  1870. float from_t = 1e20;
  1871. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1872. float t = animation->track_get_key_time(E->key().track, E->key().key);
  1873. if (t < from_t)
  1874. from_t = t;
  1875. }
  1876. float motion = from_t + (click.to.x - click.at.x) / _get_zoom_scale();
  1877. if (step->get_value())
  1878. motion = Math::stepify(motion, step->get_value());
  1879. undo_redo->create_action(TTR("Anim Move Keys"));
  1880. List<_AnimMoveRestore> to_restore;
  1881. // 1-remove the keys
  1882. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1883. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  1884. }
  1885. // 2- remove overlapped keys
  1886. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1887. float newtime = E->get().pos - from_t + motion;
  1888. int idx = animation->track_find_key(E->key().track, newtime, true);
  1889. if (idx == -1)
  1890. continue;
  1891. SelectedKey sk;
  1892. sk.key = idx;
  1893. sk.track = E->key().track;
  1894. if (selection.has(sk))
  1895. continue; //already in selection, don't save
  1896. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newtime);
  1897. _AnimMoveRestore amr;
  1898. amr.key = animation->track_get_key_value(E->key().track, idx);
  1899. amr.track = E->key().track;
  1900. amr.time = newtime;
  1901. amr.transition = animation->track_get_key_transition(E->key().track, idx);
  1902. to_restore.push_back(amr);
  1903. }
  1904. // 3-move the keys (re insert them)
  1905. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1906. float newpos = E->get().pos - from_t + motion;
  1907. /*
  1908. if (newpos<0)
  1909. continue; //no add at the beginning
  1910. */
  1911. undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->key().track, newpos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  1912. }
  1913. // 4-(undo) remove inserted keys
  1914. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1915. float newpos = E->get().pos + -from_t + motion;
  1916. /*
  1917. if (newpos<0)
  1918. continue; //no remove what no inserted
  1919. */
  1920. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newpos);
  1921. }
  1922. // 5-(undo) reinsert keys
  1923. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1924. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  1925. }
  1926. // 6-(undo) reinsert overlapped keys
  1927. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  1928. _AnimMoveRestore &amr = E->get();
  1929. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  1930. }
  1931. // 6-(undo) reinsert overlapped keys
  1932. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  1933. _AnimMoveRestore &amr = E->get();
  1934. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  1935. }
  1936. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1937. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1938. // 7-reselect
  1939. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1940. float oldpos = E->get().pos;
  1941. float newpos = oldpos - from_t + motion;
  1942. //if (newpos>=0)
  1943. undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
  1944. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
  1945. }
  1946. undo_redo->commit_action();
  1947. _edit_if_single_selection();
  1948. } break;
  1949. default: {}
  1950. }
  1951. //button released
  1952. click.click = ClickOver::CLICK_NONE;
  1953. track_editor->update();
  1954. }
  1955. }
  1956. }
  1957. Ref<InputEventMouseMotion> mm = p_input;
  1958. if (mm.is_valid()) {
  1959. mouse_over.over = MouseOver::OVER_NONE;
  1960. mouse_over.track = -1;
  1961. te->update();
  1962. track_editor->set_tooltip("");
  1963. if (!track_editor->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  1964. track_editor->call_deferred("grab_focus");
  1965. if (click.click != ClickOver::CLICK_NONE) {
  1966. switch (click.click) {
  1967. case ClickOver::CLICK_RESIZE_NAMES: {
  1968. float base = click.at.y;
  1969. float clickp = click.at.x - ofs.x;
  1970. float dif = base - clickp;
  1971. float target = mm->get_position().x + dif - ofs.x;
  1972. float ratio = target / settings_limit;
  1973. if (ratio > 0.9)
  1974. ratio = 0.9;
  1975. else if (ratio < 0.2)
  1976. ratio = 0.2;
  1977. name_column_ratio = ratio;
  1978. } break;
  1979. case ClickOver::CLICK_DRAG_TIMELINE: {
  1980. Point2 mpos = mm->get_position() - ofs;
  1981. /*
  1982. if (mpos.x<name_limit)
  1983. mpos.x=name_limit;
  1984. if (mpos.x>settings_limit)
  1985. mpos.x=settings_limit;
  1986. */
  1987. //int zoomw = settings_limit-name_limit;
  1988. float scale = _get_zoom_scale();
  1989. float pos = h_scroll->get_value() + (mpos.x - name_limit) / scale;
  1990. if (animation->get_step()) {
  1991. pos = Math::stepify(pos, animation->get_step());
  1992. }
  1993. if (pos < 0)
  1994. pos = 0;
  1995. if (pos >= animation->get_length())
  1996. pos = animation->get_length();
  1997. if (pos < h_scroll->get_value()) {
  1998. h_scroll->set_value(pos);
  1999. } else if (pos > h_scroll->get_value() + (settings_limit - name_limit) / scale) {
  2000. h_scroll->set_value(pos - (settings_limit - name_limit) / scale);
  2001. }
  2002. timeline_pos = pos;
  2003. emit_signal("timeline_changed", pos, true);
  2004. } break;
  2005. case ClickOver::CLICK_SELECT_KEYS: {
  2006. click.to = mm->get_position();
  2007. if (click.to.y < h && click.at.y > h && mm->get_relative().y < 0) {
  2008. float prev = v_scroll->get_value();
  2009. v_scroll->set_value(v_scroll->get_value() - 1);
  2010. if (prev != v_scroll->get_value())
  2011. click.at.y += h;
  2012. }
  2013. if (click.to.y > size.height && click.at.y < size.height && mm->get_relative().y > 0) {
  2014. float prev = v_scroll->get_value();
  2015. v_scroll->set_value(v_scroll->get_value() + 1);
  2016. if (prev != v_scroll->get_value())
  2017. click.at.y -= h;
  2018. }
  2019. } break;
  2020. case ClickOver::CLICK_MOVE_KEYS: {
  2021. click.to = mm->get_position();
  2022. } break;
  2023. default: {}
  2024. }
  2025. return;
  2026. } else if (mm->get_button_mask() & BUTTON_MASK_MIDDLE) {
  2027. int rel = mm->get_relative().x;
  2028. float relf = rel / _get_zoom_scale();
  2029. h_scroll->set_value(h_scroll->get_value() - relf);
  2030. }
  2031. if (mm->get_button_mask() == 0) {
  2032. Point2 mpos = mm->get_position() - ofs;
  2033. if (mpos.y < h) {
  2034. return;
  2035. }
  2036. mpos.y -= h;
  2037. int idx = mpos.y / h;
  2038. idx += v_scroll->get_value();
  2039. if (idx < 0 || idx >= animation->get_track_count())
  2040. return;
  2041. mouse_over.track = idx;
  2042. if (mpos.x < name_limit) {
  2043. //name column
  2044. mouse_over.over = MouseOver::OVER_NAME;
  2045. } else if (mpos.x < settings_limit) {
  2046. float pos = mpos.x - name_limit;
  2047. pos /= _get_zoom_scale();
  2048. pos += h_scroll->get_value();
  2049. float w_time = (type_icon[0]->get_width() / _get_zoom_scale()) / 2.0;
  2050. int kidx = animation->track_find_key(idx, pos);
  2051. int kidx_n = kidx + 1;
  2052. bool found = false;
  2053. if (kidx >= 0 && kidx < animation->track_get_key_count(idx)) {
  2054. float kpos = animation->track_get_key_time(idx, kidx);
  2055. if (ABS(pos - kpos) <= w_time) {
  2056. mouse_over.over = MouseOver::OVER_KEY;
  2057. mouse_over.track = idx;
  2058. mouse_over.over_key = kidx;
  2059. found = true;
  2060. }
  2061. }
  2062. if (!found && kidx_n >= 0 && kidx_n < animation->track_get_key_count(idx)) {
  2063. float kpos = animation->track_get_key_time(idx, kidx_n);
  2064. if (ABS(pos - kpos) <= w_time) {
  2065. mouse_over.over = MouseOver::OVER_KEY;
  2066. mouse_over.track = idx;
  2067. mouse_over.over_key = kidx_n;
  2068. found = true;
  2069. }
  2070. }
  2071. if (found) {
  2072. String text;
  2073. text = "time: " + rtos(animation->track_get_key_time(idx, mouse_over.over_key)) + "\n";
  2074. switch (animation->track_get_type(idx)) {
  2075. case Animation::TYPE_TRANSFORM: {
  2076. Dictionary d = animation->track_get_key_value(idx, mouse_over.over_key);
  2077. if (d.has("location"))
  2078. text += "location: " + String(d["location"]) + "\n";
  2079. if (d.has("rotation"))
  2080. text += "rot: " + String(d["rotation"]) + "\n";
  2081. if (d.has("scale"))
  2082. text += "scale: " + String(d["scale"]) + "\n";
  2083. } break;
  2084. case Animation::TYPE_VALUE: {
  2085. Variant v = animation->track_get_key_value(idx, mouse_over.over_key);
  2086. //text+="value: "+String(v)+"\n";
  2087. bool prop_exists = false;
  2088. Variant::Type valid_type = Variant::NIL;
  2089. Object *obj = NULL;
  2090. RES res;
  2091. Node *node = root->get_node_and_resource(animation->track_get_path(idx), res);
  2092. if (res.is_valid()) {
  2093. obj = res.ptr();
  2094. } else if (node) {
  2095. obj = node;
  2096. }
  2097. if (obj) {
  2098. valid_type = obj->get_static_property_type(animation->track_get_path(idx).get_property(), &prop_exists);
  2099. }
  2100. text += "type: " + Variant::get_type_name(v.get_type()) + "\n";
  2101. if (prop_exists && !Variant::can_convert(v.get_type(), valid_type)) {
  2102. text += "value: " + String(v) + " (Invalid, expected type: " + Variant::get_type_name(valid_type) + ")\n";
  2103. } else {
  2104. text += "value: " + String(v) + "\n";
  2105. }
  2106. } break;
  2107. case Animation::TYPE_METHOD: {
  2108. Dictionary d = animation->track_get_key_value(idx, mouse_over.over_key);
  2109. if (d.has("method"))
  2110. text += String(d["method"]);
  2111. text += "(";
  2112. Vector<Variant> args;
  2113. if (d.has("args"))
  2114. args = d["args"];
  2115. for (int i = 0; i < args.size(); i++) {
  2116. if (i > 0)
  2117. text += ", ";
  2118. text += String(args[i]);
  2119. }
  2120. text += ")\n";
  2121. } break;
  2122. }
  2123. text += "easing: " + rtos(animation->track_get_key_transition(idx, mouse_over.over_key));
  2124. track_editor->set_tooltip(text);
  2125. return;
  2126. }
  2127. } else {
  2128. //button column
  2129. int ofsx = size.width - mpos.x;
  2130. if (ofsx < 0)
  2131. return;
  2132. /*
  2133. if (ofsx < remove_icon->get_width()) {
  2134. mouse_over.over=MouseOver::OVER_REMOVE;
  2135. return;
  2136. }
  2137. ofsx-=hsep+remove_icon->get_width();
  2138. if (ofsx < move_down_icon->get_width()) {
  2139. mouse_over.over=MouseOver::OVER_DOWN;
  2140. return;
  2141. }
  2142. ofsx-=hsep+move_down_icon->get_width();
  2143. if (ofsx < move_up_icon->get_width()) {
  2144. mouse_over.over=MouseOver::OVER_UP;
  2145. return;
  2146. }
  2147. ofsx-=hsep*3+move_up_icon->get_width();
  2148. */
  2149. if (ofsx < down_icon->get_width() + wrap_icon[0]->get_width() + hsep * 3) {
  2150. mouse_over.over = MouseOver::OVER_WRAP;
  2151. return;
  2152. }
  2153. ofsx -= hsep * 3 + wrap_icon[0]->get_width() + down_icon->get_width();
  2154. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep * 3) {
  2155. mouse_over.over = MouseOver::OVER_INTERP;
  2156. return;
  2157. }
  2158. ofsx -= hsep * 2 + interp_icon[0]->get_width() + down_icon->get_width();
  2159. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width() + hsep * 3) {
  2160. mouse_over.over = MouseOver::OVER_VALUE;
  2161. return;
  2162. }
  2163. ofsx -= hsep * 3 + cont_icon[0]->get_width() + down_icon->get_width();
  2164. if (ofsx < add_key_icon->get_width()) {
  2165. mouse_over.over = MouseOver::OVER_ADD_KEY;
  2166. return;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. void AnimationKeyEditor::_notification(int p_what) {
  2173. switch (p_what) {
  2174. case NOTIFICATION_VISIBILITY_CHANGED: {
  2175. EditorNode::get_singleton()->update_keying();
  2176. emit_signal("keying_changed");
  2177. } break;
  2178. case NOTIFICATION_ENTER_TREE: {
  2179. key_editor->edit(key_edit);
  2180. zoomicon->set_custom_minimum_size(Size2(24 * EDSCALE, 0));
  2181. zoomicon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  2182. menu_track->set_icon(get_icon("Tools", "EditorIcons"));
  2183. menu_track->get_popup()->add_item(TTR("Scale Selection"), TRACK_MENU_SCALE);
  2184. menu_track->get_popup()->add_item(TTR("Scale From Cursor"), TRACK_MENU_SCALE_PIVOT);
  2185. menu_track->get_popup()->add_separator();
  2186. menu_track->get_popup()->add_item(TTR("Duplicate Selection"), TRACK_MENU_DUPLICATE);
  2187. menu_track->get_popup()->add_item(TTR("Duplicate Transposed"), TRACK_MENU_DUPLICATE_TRANSPOSE);
  2188. menu_track->get_popup()->add_separator();
  2189. menu_track->get_popup()->add_item(TTR("Goto Next Step"), TRACK_MENU_NEXT_STEP, KEY_MASK_CMD | KEY_RIGHT);
  2190. menu_track->get_popup()->add_item(TTR("Goto Prev Step"), TRACK_MENU_PREV_STEP, KEY_MASK_CMD | KEY_LEFT);
  2191. menu_track->get_popup()->add_separator();
  2192. PopupMenu *tpp = memnew(PopupMenu);
  2193. tpp->add_item(TTR("Linear"), TRACK_MENU_SET_ALL_TRANS_LINEAR);
  2194. tpp->add_item(TTR("Constant"), TRACK_MENU_SET_ALL_TRANS_CONSTANT);
  2195. tpp->add_item(TTR("In"), TRACK_MENU_SET_ALL_TRANS_IN);
  2196. tpp->add_item(TTR("Out"), TRACK_MENU_SET_ALL_TRANS_OUT);
  2197. tpp->add_item(TTR("In-Out"), TRACK_MENU_SET_ALL_TRANS_INOUT);
  2198. tpp->add_item(TTR("Out-In"), TRACK_MENU_SET_ALL_TRANS_OUTIN);
  2199. tpp->set_name(TTR("Transitions"));
  2200. tpp->connect("id_pressed", this, "_menu_track");
  2201. optimize_dialog->connect("confirmed", this, "_animation_optimize");
  2202. menu_track->get_popup()->add_child(tpp);
  2203. menu_track->get_popup()->add_item(TTR("Optimize Animation"), TRACK_MENU_OPTIMIZE);
  2204. menu_track->get_popup()->add_item(TTR("Clean-Up Animation"), TRACK_MENU_CLEAN_UP);
  2205. curve_linear->connect("pressed", this, "_menu_track", varray(CURVE_SET_LINEAR));
  2206. curve_in->connect("pressed", this, "_menu_track", varray(CURVE_SET_IN));
  2207. curve_out->connect("pressed", this, "_menu_track", varray(CURVE_SET_OUT));
  2208. curve_inout->connect("pressed", this, "_menu_track", varray(CURVE_SET_INOUT));
  2209. curve_outin->connect("pressed", this, "_menu_track", varray(CURVE_SET_OUTIN));
  2210. curve_constant->connect("pressed", this, "_menu_track", varray(CURVE_SET_CONSTANT));
  2211. edit_button->connect("pressed", this, "_toggle_edit_curves");
  2212. curve_edit->connect("transition_changed", this, "_curve_transition_changed");
  2213. call_select->connect("selected", this, "_add_call_track");
  2214. _update_menu();
  2215. } break;
  2216. case NOTIFICATION_THEME_CHANGED: {
  2217. zoomicon->set_texture(get_icon("Zoom", "EditorIcons"));
  2218. menu_add_track->set_icon(get_icon("Add", "EditorIcons"));
  2219. menu_track->set_icon(get_icon("Tools", "EditorIcons"));
  2220. menu_add_track->get_popup()->set_item_icon(ADD_TRACK_MENU_ADD_VALUE_TRACK, get_icon("KeyValue", "EditorIcons"));
  2221. menu_add_track->get_popup()->set_item_icon(ADD_TRACK_MENU_ADD_TRANSFORM_TRACK, get_icon("KeyXform", "EditorIcons"));
  2222. menu_add_track->get_popup()->set_item_icon(ADD_TRACK_MENU_ADD_CALL_TRACK, get_icon("KeyCall", "EditorIcons"));
  2223. curve_linear->set_icon(get_icon("CurveLinear", "EditorIcons"));
  2224. curve_in->set_icon(get_icon("CurveIn", "EditorIcons"));
  2225. curve_out->set_icon(get_icon("CurveOut", "EditorIcons"));
  2226. curve_inout->set_icon(get_icon("CurveInOut", "EditorIcons"));
  2227. curve_outin->set_icon(get_icon("CurveOutIn", "EditorIcons"));
  2228. curve_constant->set_icon(get_icon("CurveConstant", "EditorIcons"));
  2229. move_up_button->set_icon(get_icon("MoveUp", "EditorIcons"));
  2230. move_down_button->set_icon(get_icon("MoveDown", "EditorIcons"));
  2231. remove_button->set_icon(get_icon("Remove", "EditorIcons"));
  2232. edit_button->set_icon(get_icon("EditKey", "EditorIcons"));
  2233. loop->set_icon(get_icon("Loop", "EditorIcons"));
  2234. {
  2235. right_data_size_cache = 0;
  2236. int hsep = get_constant("hseparation", "Tree");
  2237. Ref<Texture> remove_icon = get_icon("Remove", "EditorIcons");
  2238. Ref<Texture> move_up_icon = get_icon("MoveUp", "EditorIcons");
  2239. Ref<Texture> move_down_icon = get_icon("MoveDown", "EditorIcons");
  2240. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  2241. Ref<Texture> add_key_icon = get_icon("TrackAddKey", "EditorIcons");
  2242. Ref<Texture> interp_icon[3] = {
  2243. get_icon("InterpRaw", "EditorIcons"),
  2244. get_icon("InterpLinear", "EditorIcons"),
  2245. get_icon("InterpCubic", "EditorIcons")
  2246. };
  2247. Ref<Texture> cont_icon[3] = {
  2248. get_icon("TrackContinuous", "EditorIcons"),
  2249. get_icon("TrackDiscrete", "EditorIcons"),
  2250. get_icon("TrackTrigger", "EditorIcons")
  2251. };
  2252. Ref<Texture> wrap_icon[2] = {
  2253. get_icon("InterpWrapClamp", "EditorIcons"),
  2254. get_icon("InterpWrapLoop", "EditorIcons"),
  2255. };
  2256. right_data_size_cache = down_icon->get_width() * 3 + add_key_icon->get_width() + interp_icon[0]->get_width() + cont_icon[0]->get_width() + wrap_icon[0]->get_width() + hsep * 8;
  2257. }
  2258. } break;
  2259. }
  2260. }
  2261. void AnimationKeyEditor::_scroll_changed(double) {
  2262. if (te_drawing)
  2263. return;
  2264. track_editor->update();
  2265. }
  2266. void AnimationKeyEditor::_update_paths() {
  2267. if (animation.is_valid()) {
  2268. //timeline->set_max(animation->get_length());
  2269. //timeline->set_step(0.01);
  2270. track_editor->update();
  2271. length->set_value(animation->get_length());
  2272. step->set_value(animation->get_step());
  2273. }
  2274. }
  2275. void AnimationKeyEditor::_root_removed() {
  2276. root = NULL;
  2277. }
  2278. void AnimationKeyEditor::_update_menu() {
  2279. updating = true;
  2280. if (animation.is_valid()) {
  2281. length->set_value(animation->get_length());
  2282. loop->set_pressed(animation->has_loop());
  2283. step->set_value(animation->get_step());
  2284. }
  2285. track_editor->update();
  2286. updating = false;
  2287. }
  2288. void AnimationKeyEditor::_clear_selection() {
  2289. selection.clear();
  2290. key_edit->animation = Ref<Animation>();
  2291. key_edit->track = 0;
  2292. key_edit->key_ofs = 0;
  2293. key_edit->hint = PropertyInfo();
  2294. key_edit->base = NodePath();
  2295. key_edit->notify_change();
  2296. }
  2297. void AnimationKeyEditor::set_animation(const Ref<Animation> &p_anim) {
  2298. if (animation.is_valid())
  2299. animation->disconnect("changed", this, "_update_paths");
  2300. animation = p_anim;
  2301. if (animation.is_valid())
  2302. animation->connect("changed", this, "_update_paths");
  2303. timeline_pos = 0;
  2304. _clear_selection();
  2305. _update_paths();
  2306. _update_menu();
  2307. selected_track = -1;
  2308. _edit_if_single_selection();
  2309. EditorNode::get_singleton()->update_keying();
  2310. }
  2311. void AnimationKeyEditor::set_root(Node *p_root) {
  2312. if (root)
  2313. root->disconnect("tree_exited", this, "_root_removed");
  2314. root = p_root;
  2315. if (root)
  2316. root->connect("tree_exited", this, "_root_removed", make_binds(), CONNECT_ONESHOT);
  2317. }
  2318. Node *AnimationKeyEditor::get_root() const {
  2319. return root;
  2320. }
  2321. void AnimationKeyEditor::update_keying() {
  2322. bool keying_enabled = is_visible_in_tree() && animation.is_valid();
  2323. if (keying_enabled == keying)
  2324. return;
  2325. keying = keying_enabled;
  2326. _update_menu();
  2327. emit_signal("keying_changed");
  2328. }
  2329. bool AnimationKeyEditor::has_keying() const {
  2330. return keying;
  2331. }
  2332. void AnimationKeyEditor::_query_insert(const InsertData &p_id) {
  2333. if (insert_frame != Engine::get_singleton()->get_frames_drawn()) {
  2334. //clear insert list for the frame if frame changed
  2335. if (insert_confirm->is_visible_in_tree())
  2336. return; //do nothing
  2337. insert_data.clear();
  2338. insert_query = false;
  2339. }
  2340. insert_frame = Engine::get_singleton()->get_frames_drawn();
  2341. for (List<InsertData>::Element *E = insert_data.front(); E; E = E->next()) {
  2342. //prevent insertion of multiple tracks
  2343. if (E->get().path == p_id.path)
  2344. return; //already inserted a track for this on this frame
  2345. }
  2346. insert_data.push_back(p_id);
  2347. if (p_id.track_idx == -1) {
  2348. if (bool(EDITOR_DEF("editors/animation/confirm_insert_track", true))) {
  2349. //potential new key, does not exist
  2350. if (insert_data.size() == 1)
  2351. insert_confirm->set_text(vformat(TTR("Create NEW track for %s and insert key?"), p_id.query));
  2352. else
  2353. insert_confirm->set_text(vformat(TTR("Create %d NEW tracks and insert keys?"), insert_data.size()));
  2354. insert_confirm->get_ok()->set_text(TTR("Create"));
  2355. insert_confirm->popup_centered_minsize();
  2356. insert_query = true;
  2357. } else {
  2358. call_deferred("_insert_delay");
  2359. insert_queue = true;
  2360. }
  2361. } else {
  2362. if (!insert_query && !insert_queue) {
  2363. call_deferred("_insert_delay");
  2364. insert_queue = true;
  2365. }
  2366. }
  2367. }
  2368. void AnimationKeyEditor::insert_transform_key(Spatial *p_node, const String &p_sub, const Transform &p_xform) {
  2369. if (!keying)
  2370. return;
  2371. if (!animation.is_valid())
  2372. return;
  2373. ERR_FAIL_COND(!root);
  2374. //let's build a node path
  2375. String path = root->get_path_to(p_node);
  2376. if (p_sub != "")
  2377. path += ":" + p_sub;
  2378. NodePath np = path;
  2379. int track_idx = -1;
  2380. for (int i = 0; i < animation->get_track_count(); i++) {
  2381. if (animation->track_get_type(i) != Animation::TYPE_TRANSFORM)
  2382. continue;
  2383. if (animation->track_get_path(i) != np)
  2384. continue;
  2385. track_idx = i;
  2386. break;
  2387. }
  2388. InsertData id;
  2389. Dictionary val;
  2390. id.path = np;
  2391. id.track_idx = track_idx;
  2392. id.value = p_xform;
  2393. id.type = Animation::TYPE_TRANSFORM;
  2394. id.query = "node '" + p_node->get_name() + "'";
  2395. id.advance = false;
  2396. //dialog insert
  2397. _query_insert(id);
  2398. }
  2399. void AnimationKeyEditor::insert_node_value_key(Node *p_node, const String &p_property, const Variant &p_value, bool p_only_if_exists) {
  2400. ERR_FAIL_COND(!root);
  2401. //let's build a node path
  2402. Node *node = p_node;
  2403. String path = root->get_path_to(node);
  2404. for (int i = 1; i < history->get_path_size(); i++) {
  2405. String prop = history->get_path_property(i);
  2406. ERR_FAIL_COND(prop == "");
  2407. path += ":" + prop;
  2408. }
  2409. path += ":" + p_property;
  2410. NodePath np = path;
  2411. //locate track
  2412. int track_idx = -1;
  2413. for (int i = 0; i < animation->get_track_count(); i++) {
  2414. if (animation->track_get_type(i) != Animation::TYPE_VALUE)
  2415. continue;
  2416. if (animation->track_get_path(i) != np)
  2417. continue;
  2418. track_idx = i;
  2419. break;
  2420. }
  2421. if (p_only_if_exists && track_idx == -1)
  2422. return;
  2423. InsertData id;
  2424. id.path = np;
  2425. id.track_idx = track_idx;
  2426. id.value = p_value;
  2427. id.type = Animation::TYPE_VALUE;
  2428. id.query = "property '" + p_property + "'";
  2429. id.advance = false;
  2430. //dialog insert
  2431. _query_insert(id);
  2432. }
  2433. void AnimationKeyEditor::insert_value_key(const String &p_property, const Variant &p_value, bool p_advance) {
  2434. ERR_FAIL_COND(!root);
  2435. //let's build a node path
  2436. ERR_FAIL_COND(history->get_path_size() == 0);
  2437. Object *obj = ObjectDB::get_instance(history->get_path_object(0));
  2438. ERR_FAIL_COND(!Object::cast_to<Node>(obj));
  2439. Node *node = Object::cast_to<Node>(obj);
  2440. String path = root->get_path_to(node);
  2441. for (int i = 1; i < history->get_path_size(); i++) {
  2442. String prop = history->get_path_property(i);
  2443. ERR_FAIL_COND(prop == "");
  2444. path += ":" + prop;
  2445. }
  2446. path += ":" + p_property;
  2447. NodePath np = path;
  2448. //locate track
  2449. int track_idx = -1;
  2450. for (int i = 0; i < animation->get_track_count(); i++) {
  2451. if (animation->track_get_type(i) != Animation::TYPE_VALUE)
  2452. continue;
  2453. if (animation->track_get_path(i) != np)
  2454. continue;
  2455. track_idx = i;
  2456. break;
  2457. }
  2458. InsertData id;
  2459. id.path = np;
  2460. id.track_idx = track_idx;
  2461. id.value = p_value;
  2462. id.type = Animation::TYPE_VALUE;
  2463. id.query = "property '" + p_property + "'";
  2464. id.advance = p_advance;
  2465. //dialog insert
  2466. _query_insert(id);
  2467. }
  2468. void AnimationKeyEditor::_confirm_insert_list() {
  2469. undo_redo->create_action(TTR("Anim Create & Insert"));
  2470. int last_track = animation->get_track_count();
  2471. while (insert_data.size()) {
  2472. last_track = _confirm_insert(insert_data.front()->get(), last_track);
  2473. insert_data.pop_front();
  2474. }
  2475. undo_redo->commit_action();
  2476. }
  2477. int AnimationKeyEditor::_confirm_insert(InsertData p_id, int p_last_track) {
  2478. if (p_last_track == -1)
  2479. p_last_track = animation->get_track_count();
  2480. bool created = false;
  2481. if (p_id.track_idx < 0) {
  2482. created = true;
  2483. undo_redo->create_action(TTR("Anim Insert Track & Key"));
  2484. Animation::UpdateMode update_mode = Animation::UPDATE_DISCRETE;
  2485. if (p_id.type == Animation::TYPE_VALUE) {
  2486. //wants a new tack
  2487. {
  2488. //shitty hack
  2489. NodePath np;
  2490. animation->add_track(p_id.type);
  2491. animation->track_set_path(animation->get_track_count() - 1, p_id.path);
  2492. PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
  2493. animation->remove_track(animation->get_track_count() - 1); //hack
  2494. if (h.type == Variant::REAL ||
  2495. h.type == Variant::VECTOR2 ||
  2496. h.type == Variant::RECT2 ||
  2497. h.type == Variant::VECTOR3 ||
  2498. h.type == Variant::RECT3 ||
  2499. h.type == Variant::QUAT ||
  2500. h.type == Variant::COLOR ||
  2501. h.type == Variant::TRANSFORM) {
  2502. update_mode = Animation::UPDATE_CONTINUOUS;
  2503. }
  2504. if (h.usage & PROPERTY_USAGE_ANIMATE_AS_TRIGGER) {
  2505. update_mode = Animation::UPDATE_TRIGGER;
  2506. }
  2507. }
  2508. }
  2509. p_id.track_idx = p_last_track;
  2510. undo_redo->add_do_method(animation.ptr(), "add_track", p_id.type);
  2511. undo_redo->add_do_method(animation.ptr(), "track_set_path", p_id.track_idx, p_id.path);
  2512. if (p_id.type == Animation::TYPE_VALUE)
  2513. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", p_id.track_idx, update_mode);
  2514. } else {
  2515. undo_redo->create_action(TTR("Anim Insert Key"));
  2516. }
  2517. float time = timeline_pos;
  2518. Variant value;
  2519. switch (p_id.type) {
  2520. case Animation::TYPE_VALUE: {
  2521. value = p_id.value;
  2522. } break;
  2523. case Animation::TYPE_TRANSFORM: {
  2524. Transform tr = p_id.value;
  2525. Dictionary d;
  2526. d["location"] = tr.origin;
  2527. d["scale"] = tr.basis.get_scale();
  2528. d["rotation"] = Quat(tr.basis); //.orthonormalized();
  2529. value = d;
  2530. } break;
  2531. default: {}
  2532. }
  2533. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, value);
  2534. if (created) {
  2535. //just remove the track
  2536. undo_redo->add_undo_method(animation.ptr(), "remove_track", p_last_track);
  2537. p_last_track++;
  2538. } else {
  2539. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_id.track_idx, time);
  2540. int existing = animation->track_find_key(p_id.track_idx, time, true);
  2541. if (existing != -1) {
  2542. Variant v = animation->track_get_key_value(p_id.track_idx, existing);
  2543. float trans = animation->track_get_key_transition(p_id.track_idx, existing);
  2544. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, v, trans);
  2545. }
  2546. }
  2547. undo_redo->add_do_method(this, "update");
  2548. undo_redo->add_undo_method(this, "update");
  2549. undo_redo->add_do_method(track_editor, "update");
  2550. undo_redo->add_undo_method(track_editor, "update");
  2551. undo_redo->add_do_method(track_pos, "update");
  2552. undo_redo->add_undo_method(track_pos, "update");
  2553. undo_redo->commit_action();
  2554. return p_last_track;
  2555. }
  2556. Ref<Animation> AnimationKeyEditor::get_current_animation() const {
  2557. return animation;
  2558. }
  2559. void AnimationKeyEditor::_animation_len_changed(float p_len) {
  2560. if (updating)
  2561. return;
  2562. if (!animation.is_null()) {
  2563. undo_redo->create_action(TTR("Change Anim Len"));
  2564. undo_redo->add_do_method(animation.ptr(), "set_length", p_len);
  2565. undo_redo->add_undo_method(animation.ptr(), "set_length", animation->get_length());
  2566. undo_redo->add_do_method(this, "_animation_len_update");
  2567. undo_redo->add_undo_method(this, "_animation_len_update");
  2568. undo_redo->commit_action();
  2569. }
  2570. }
  2571. void AnimationKeyEditor::_animation_len_update() {
  2572. if (!animation.is_null())
  2573. emit_signal(alc, animation->get_length());
  2574. }
  2575. void AnimationKeyEditor::_animation_changed() {
  2576. if (updating)
  2577. return;
  2578. _update_menu();
  2579. }
  2580. void AnimationKeyEditor::_animation_loop_changed() {
  2581. if (updating)
  2582. return;
  2583. if (!animation.is_null()) {
  2584. undo_redo->create_action(TTR("Change Anim Loop"));
  2585. undo_redo->add_do_method(animation.ptr(), "set_loop", loop->is_pressed());
  2586. undo_redo->add_undo_method(animation.ptr(), "set_loop", !loop->is_pressed());
  2587. undo_redo->commit_action();
  2588. }
  2589. }
  2590. void AnimationKeyEditor::_create_value_item(int p_type) {
  2591. undo_redo->create_action(TTR("Anim Create Typed Value Key"));
  2592. Variant::CallError ce;
  2593. Variant v = Variant::construct(Variant::Type(p_type), NULL, 0, ce);
  2594. undo_redo->add_do_method(animation.ptr(), "track_insert_key", cvi_track, cvi_pos, v);
  2595. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", cvi_track, cvi_pos);
  2596. int existing = animation->track_find_key(cvi_track, cvi_pos, true);
  2597. if (existing != -1) {
  2598. Variant v = animation->track_get_key_value(cvi_track, existing);
  2599. float trans = animation->track_get_key_transition(cvi_track, existing);
  2600. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", cvi_track, cvi_pos, v, trans);
  2601. }
  2602. undo_redo->commit_action();
  2603. }
  2604. void AnimationKeyEditor::set_anim_pos(float p_pos) {
  2605. if (animation.is_null())
  2606. return;
  2607. timeline_pos = p_pos;
  2608. update();
  2609. track_pos->update();
  2610. track_editor->update();
  2611. }
  2612. void AnimationKeyEditor::_pane_drag(const Point2 &p_delta) {
  2613. Size2 ecs = ec->get_custom_minimum_size();
  2614. ecs.y -= p_delta.y;
  2615. if (ecs.y < 100)
  2616. ecs.y = 100;
  2617. ec->set_custom_minimum_size(ecs);
  2618. }
  2619. void AnimationKeyEditor::_insert_delay() {
  2620. if (insert_query) {
  2621. //discard since it's entered into query mode
  2622. insert_queue = false;
  2623. return;
  2624. }
  2625. undo_redo->create_action(TTR("Anim Insert"));
  2626. int last_track = animation->get_track_count();
  2627. bool advance = false;
  2628. while (insert_data.size()) {
  2629. if (insert_data.front()->get().advance)
  2630. advance = true;
  2631. last_track = _confirm_insert(insert_data.front()->get(), last_track);
  2632. insert_data.pop_front();
  2633. }
  2634. undo_redo->commit_action();
  2635. if (advance) {
  2636. float step = animation->get_step();
  2637. if (step == 0)
  2638. step = 1;
  2639. float pos = timeline_pos;
  2640. pos = Math::stepify(pos + step, step);
  2641. if (pos > animation->get_length())
  2642. pos = animation->get_length();
  2643. timeline_pos = pos;
  2644. track_pos->update();
  2645. emit_signal("timeline_changed", pos, true);
  2646. }
  2647. insert_queue = false;
  2648. }
  2649. void AnimationKeyEditor::_step_changed(float p_len) {
  2650. updating = true;
  2651. if (!animation.is_null()) {
  2652. animation->set_step(p_len);
  2653. emit_signal("animation_step_changed", animation->get_step());
  2654. }
  2655. updating = false;
  2656. }
  2657. void AnimationKeyEditor::_scale() {
  2658. if (selection.empty())
  2659. return;
  2660. float from_t = 1e20;
  2661. float to_t = -1e20;
  2662. float len = -1e20;
  2663. float pivot = 0;
  2664. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  2665. float t = animation->track_get_key_time(E->key().track, E->key().key);
  2666. if (t < from_t)
  2667. from_t = t;
  2668. if (t > to_t)
  2669. to_t = t;
  2670. }
  2671. len = to_t - from_t;
  2672. if (last_menu_track_opt == TRACK_MENU_SCALE_PIVOT) {
  2673. pivot = timeline_pos;
  2674. } else {
  2675. pivot = from_t;
  2676. }
  2677. float s = scale->get_value();
  2678. if (s == 0) {
  2679. ERR_PRINT("Can't scale to 0");
  2680. }
  2681. undo_redo->create_action(TTR("Anim Scale Keys"));
  2682. List<_AnimMoveRestore> to_restore;
  2683. // 1-remove the keys
  2684. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2685. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  2686. }
  2687. // 2- remove overlapped keys
  2688. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2689. float newtime = (E->get().pos - from_t) * s + from_t;
  2690. int idx = animation->track_find_key(E->key().track, newtime, true);
  2691. if (idx == -1)
  2692. continue;
  2693. SelectedKey sk;
  2694. sk.key = idx;
  2695. sk.track = E->key().track;
  2696. if (selection.has(sk))
  2697. continue; //already in selection, don't save
  2698. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newtime);
  2699. _AnimMoveRestore amr;
  2700. amr.key = animation->track_get_key_value(E->key().track, idx);
  2701. amr.track = E->key().track;
  2702. amr.time = newtime;
  2703. amr.transition = animation->track_get_key_transition(E->key().track, idx);
  2704. to_restore.push_back(amr);
  2705. }
  2706. #define _NEW_POS(m_ofs) (((s > 0) ? m_ofs : from_t + (len - (m_ofs - from_t))) - pivot) * ABS(s) + from_t
  2707. // 3-move the keys (re insert them)
  2708. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2709. float newpos = _NEW_POS(E->get().pos);
  2710. undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->key().track, newpos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  2711. }
  2712. // 4-(undo) remove inserted keys
  2713. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2714. float newpos = _NEW_POS(E->get().pos);
  2715. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newpos);
  2716. }
  2717. // 5-(undo) reinsert keys
  2718. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2719. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  2720. }
  2721. // 6-(undo) reinsert overlapped keys
  2722. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  2723. _AnimMoveRestore &amr = E->get();
  2724. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  2725. }
  2726. // 6-(undo) reinsert overlapped keys
  2727. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  2728. _AnimMoveRestore &amr = E->get();
  2729. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  2730. }
  2731. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  2732. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  2733. // 7-reselect
  2734. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2735. float oldpos = E->get().pos;
  2736. float newpos = _NEW_POS(oldpos);
  2737. if (newpos >= 0)
  2738. undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
  2739. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
  2740. }
  2741. #undef _NEW_POS
  2742. undo_redo->commit_action();
  2743. }
  2744. void AnimationKeyEditor::_add_call_track(const NodePath &p_base) {
  2745. Node *base = EditorNode::get_singleton()->get_edited_scene();
  2746. if (!base)
  2747. return;
  2748. Node *from = base->get_node(p_base);
  2749. if (!from || !root)
  2750. return;
  2751. NodePath path = root->get_path_to(from);
  2752. //print_line("root: "+String(root->get_path()));
  2753. //print_line("path: "+String(path));
  2754. undo_redo->create_action(TTR("Anim Add Call Track"));
  2755. undo_redo->add_do_method(animation.ptr(), "add_track", Animation::TYPE_METHOD);
  2756. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path);
  2757. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  2758. undo_redo->commit_action();
  2759. }
  2760. void AnimationKeyEditor::cleanup() {
  2761. set_animation(Ref<Animation>());
  2762. }
  2763. void AnimationKeyEditor::_bind_methods() {
  2764. ClassDB::bind_method(D_METHOD("_root_removed"), &AnimationKeyEditor::_root_removed);
  2765. ClassDB::bind_method(D_METHOD("_scale"), &AnimationKeyEditor::_scale);
  2766. ClassDB::bind_method(D_METHOD("set_root"), &AnimationKeyEditor::set_root);
  2767. //ClassDB::bind_method(D_METHOD("_confirm_insert"),&AnimationKeyEditor::_confirm_insert);
  2768. ClassDB::bind_method(D_METHOD("_confirm_insert_list"), &AnimationKeyEditor::_confirm_insert_list);
  2769. ClassDB::bind_method(D_METHOD("_update_paths"), &AnimationKeyEditor::_update_paths);
  2770. ClassDB::bind_method(D_METHOD("_track_editor_draw"), &AnimationKeyEditor::_track_editor_draw);
  2771. ClassDB::bind_method(D_METHOD("_animation_changed"), &AnimationKeyEditor::_animation_changed);
  2772. ClassDB::bind_method(D_METHOD("_scroll_changed"), &AnimationKeyEditor::_scroll_changed);
  2773. ClassDB::bind_method(D_METHOD("_track_editor_gui_input"), &AnimationKeyEditor::_track_editor_gui_input);
  2774. ClassDB::bind_method(D_METHOD("_track_name_changed"), &AnimationKeyEditor::_track_name_changed);
  2775. ClassDB::bind_method(D_METHOD("_track_menu_selected"), &AnimationKeyEditor::_track_menu_selected);
  2776. ClassDB::bind_method(D_METHOD("_menu_add_track"), &AnimationKeyEditor::_menu_add_track);
  2777. ClassDB::bind_method(D_METHOD("_menu_track"), &AnimationKeyEditor::_menu_track);
  2778. ClassDB::bind_method(D_METHOD("_clear_selection_for_anim"), &AnimationKeyEditor::_clear_selection_for_anim);
  2779. ClassDB::bind_method(D_METHOD("_select_at_anim"), &AnimationKeyEditor::_select_at_anim);
  2780. ClassDB::bind_method(D_METHOD("_track_position_draw"), &AnimationKeyEditor::_track_position_draw);
  2781. ClassDB::bind_method(D_METHOD("_insert_delay"), &AnimationKeyEditor::_insert_delay);
  2782. ClassDB::bind_method(D_METHOD("_step_changed"), &AnimationKeyEditor::_step_changed);
  2783. ClassDB::bind_method(D_METHOD("_animation_loop_changed"), &AnimationKeyEditor::_animation_loop_changed);
  2784. ClassDB::bind_method(D_METHOD("_animation_len_changed"), &AnimationKeyEditor::_animation_len_changed);
  2785. ClassDB::bind_method(D_METHOD("_create_value_item"), &AnimationKeyEditor::_create_value_item);
  2786. ClassDB::bind_method(D_METHOD("_pane_drag"), &AnimationKeyEditor::_pane_drag);
  2787. ClassDB::bind_method(D_METHOD("_animation_len_update"), &AnimationKeyEditor::_animation_len_update);
  2788. ClassDB::bind_method(D_METHOD("set_animation"), &AnimationKeyEditor::set_animation);
  2789. ClassDB::bind_method(D_METHOD("_animation_optimize"), &AnimationKeyEditor::_animation_optimize);
  2790. ClassDB::bind_method(D_METHOD("_curve_transition_changed"), &AnimationKeyEditor::_curve_transition_changed);
  2791. ClassDB::bind_method(D_METHOD("_toggle_edit_curves"), &AnimationKeyEditor::_toggle_edit_curves);
  2792. ClassDB::bind_method(D_METHOD("_add_call_track"), &AnimationKeyEditor::_add_call_track);
  2793. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "res"), PropertyInfo(Variant::STRING, "prop")));
  2794. ADD_SIGNAL(MethodInfo("keying_changed"));
  2795. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::REAL, "position"), PropertyInfo(Variant::BOOL, "drag")));
  2796. ADD_SIGNAL(MethodInfo("animation_len_changed", PropertyInfo(Variant::REAL, "len")));
  2797. ADD_SIGNAL(MethodInfo("animation_step_changed", PropertyInfo(Variant::REAL, "step")));
  2798. ADD_SIGNAL(MethodInfo("key_edited", PropertyInfo(Variant::INT, "track"), PropertyInfo(Variant::INT, "key")));
  2799. }
  2800. AnimationKeyEditor::AnimationKeyEditor() {
  2801. alc = "animation_len_changed";
  2802. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  2803. selected_track = -1;
  2804. updating = false;
  2805. te_drawing = false;
  2806. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2807. history = EditorNode::get_singleton()->get_editor_history();
  2808. ec = memnew(Control);
  2809. ec->set_custom_minimum_size(Size2(0, 150) * EDSCALE);
  2810. add_child(ec);
  2811. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  2812. h_scroll = memnew(HScrollBar);
  2813. h_scroll->connect("value_changed", this, "_scroll_changed");
  2814. add_child(h_scroll);
  2815. h_scroll->set_value(0);
  2816. HBoxContainer *hb = memnew(HBoxContainer);
  2817. add_child(hb);
  2818. root = NULL;
  2819. //menu = memnew( MenuButton );
  2820. //menu->set_flat(true);
  2821. //menu->set_position(Point2());
  2822. //add_child(menu);
  2823. zoomicon = memnew(TextureRect);
  2824. hb->add_child(zoomicon);
  2825. zoomicon->set_tooltip(TTR("Animation zoom."));
  2826. zoom = memnew(HSlider);
  2827. //hb->add_child(zoom);
  2828. zoom->set_step(0.01);
  2829. zoom->set_min(0.0);
  2830. zoom->set_max(2.0);
  2831. zoom->set_value(1.0);
  2832. zoom->set_h_size_flags(SIZE_EXPAND_FILL);
  2833. zoom->set_v_size_flags(SIZE_EXPAND_FILL);
  2834. zoom->set_stretch_ratio(2);
  2835. hb->add_child(zoom);
  2836. zoom->connect("value_changed", this, "_scroll_changed");
  2837. zoom->set_tooltip(TTR("Animation zoom."));
  2838. hb->add_child(memnew(VSeparator));
  2839. Label *l = memnew(Label);
  2840. l->set_text(TTR("Length (s):"));
  2841. hb->add_child(l);
  2842. length = memnew(SpinBox);
  2843. length->set_min(0.01);
  2844. length->set_max(10000);
  2845. length->set_step(0.01);
  2846. length->set_h_size_flags(SIZE_EXPAND_FILL);
  2847. length->set_stretch_ratio(1);
  2848. length->set_tooltip(TTR("Animation length (in seconds)."));
  2849. hb->add_child(length);
  2850. length->connect("value_changed", this, "_animation_len_changed");
  2851. l = memnew(Label);
  2852. l->set_text(TTR("Step (s):"));
  2853. hb->add_child(l);
  2854. step = memnew(SpinBox);
  2855. step->set_min(0.00);
  2856. step->set_max(128);
  2857. step->set_step(0.01);
  2858. step->set_value(0.0);
  2859. step->set_h_size_flags(SIZE_EXPAND_FILL);
  2860. step->set_stretch_ratio(1);
  2861. step->set_tooltip(TTR("Cursor step snap (in seconds)."));
  2862. hb->add_child(step);
  2863. step->connect("value_changed", this, "_step_changed");
  2864. loop = memnew(ToolButton);
  2865. loop->set_toggle_mode(true);
  2866. loop->connect("pressed", this, "_animation_loop_changed");
  2867. hb->add_child(loop);
  2868. loop->set_tooltip(TTR("Enable/Disable looping in animation."));
  2869. hb->add_child(memnew(VSeparator));
  2870. menu_add_track = memnew(MenuButton);
  2871. hb->add_child(menu_add_track);
  2872. menu_add_track->get_popup()->connect("id_pressed", this, "_menu_add_track");
  2873. menu_add_track->set_tooltip(TTR("Add new tracks."));
  2874. menu_add_track->get_popup()->add_icon_item(get_icon("KeyValue", "EditorIcons"), "Add Normal Track", ADD_TRACK_MENU_ADD_VALUE_TRACK);
  2875. menu_add_track->get_popup()->add_icon_item(get_icon("KeyXform", "EditorIcons"), "Add Transform Track", ADD_TRACK_MENU_ADD_TRANSFORM_TRACK);
  2876. menu_add_track->get_popup()->add_icon_item(get_icon("KeyCall", "EditorIcons"), "Add Call Func Track", ADD_TRACK_MENU_ADD_CALL_TRACK);
  2877. move_up_button = memnew(ToolButton);
  2878. hb->add_child(move_up_button);
  2879. move_up_button->connect("pressed", this, "_menu_track", make_binds(TRACK_MENU_MOVE_UP));
  2880. move_up_button->set_focus_mode(FOCUS_NONE);
  2881. move_up_button->set_disabled(true);
  2882. move_up_button->set_tooltip(TTR("Move current track up."));
  2883. move_down_button = memnew(ToolButton);
  2884. hb->add_child(move_down_button);
  2885. move_down_button->connect("pressed", this, "_menu_track", make_binds(TRACK_MENU_MOVE_DOWN));
  2886. move_down_button->set_focus_mode(FOCUS_NONE);
  2887. move_down_button->set_disabled(true);
  2888. move_down_button->set_tooltip(TTR("Move current track down."));
  2889. remove_button = memnew(ToolButton);
  2890. hb->add_child(remove_button);
  2891. remove_button->connect("pressed", this, "_menu_track", make_binds(TRACK_MENU_REMOVE));
  2892. remove_button->set_focus_mode(FOCUS_NONE);
  2893. remove_button->set_disabled(true);
  2894. remove_button->set_tooltip(TTR("Remove selected track."));
  2895. hb->add_child(memnew(VSeparator));
  2896. menu_track = memnew(MenuButton);
  2897. hb->add_child(menu_track);
  2898. menu_track->get_popup()->connect("id_pressed", this, "_menu_track");
  2899. menu_track->set_tooltip(TTR("Track tools"));
  2900. edit_button = memnew(ToolButton);
  2901. edit_button->set_toggle_mode(true);
  2902. edit_button->set_focus_mode(FOCUS_NONE);
  2903. edit_button->set_disabled(true);
  2904. hb->add_child(edit_button);
  2905. edit_button->set_tooltip(TTR("Enable editing of individual keys by clicking them."));
  2906. optimize_dialog = memnew(ConfirmationDialog);
  2907. add_child(optimize_dialog);
  2908. optimize_dialog->set_title(TTR("Anim. Optimizer"));
  2909. VBoxContainer *optimize_vb = memnew(VBoxContainer);
  2910. optimize_dialog->add_child(optimize_vb);
  2911. optimize_linear_error = memnew(SpinBox);
  2912. optimize_linear_error->set_max(1.0);
  2913. optimize_linear_error->set_min(0.001);
  2914. optimize_linear_error->set_step(0.001);
  2915. optimize_linear_error->set_value(0.05);
  2916. optimize_vb->add_margin_child(TTR("Max. Linear Error:"), optimize_linear_error);
  2917. optimize_angular_error = memnew(SpinBox);
  2918. optimize_angular_error->set_max(1.0);
  2919. optimize_angular_error->set_min(0.001);
  2920. optimize_angular_error->set_step(0.001);
  2921. optimize_angular_error->set_value(0.01);
  2922. optimize_vb->add_margin_child(TTR("Max. Angular Error:"), optimize_angular_error);
  2923. optimize_max_angle = memnew(SpinBox);
  2924. optimize_vb->add_margin_child(TTR("Max Optimizable Angle:"), optimize_max_angle);
  2925. optimize_max_angle->set_max(360.0);
  2926. optimize_max_angle->set_min(0.0);
  2927. optimize_max_angle->set_step(0.1);
  2928. optimize_max_angle->set_value(22);
  2929. optimize_dialog->get_ok()->set_text(TTR("Optimize"));
  2930. /*keying = memnew( Button );
  2931. keying->set_toggle_mode(true);
  2932. //keying->set_text("Keys");
  2933. keying->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,60);
  2934. keying->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
  2935. keying->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_BEGIN,55);
  2936. keying->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  2937. //add_child(keying);
  2938. keying->connect("pressed",this,"_keying_toggled");
  2939. */
  2940. /* l = memnew( Label );
  2941. l->set_text("Base: ");
  2942. l->set_position(Point2(0,3));
  2943. //dr_panel->add_child(l);*/
  2944. //menu->get_popup()->connect("id_pressed",this,"_menu_callback");
  2945. hb = memnew(HBoxContainer);
  2946. hb->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2947. ec->add_child(hb);
  2948. hb->set_v_size_flags(SIZE_EXPAND_FILL);
  2949. track_editor = memnew(Control);
  2950. track_editor->connect("draw", this, "_track_editor_draw");
  2951. hb->add_child(track_editor);
  2952. track_editor->connect("gui_input", this, "_track_editor_gui_input");
  2953. track_editor->set_focus_mode(Control::FOCUS_ALL);
  2954. track_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2955. track_pos = memnew(Control);
  2956. track_pos->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2957. track_pos->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2958. track_editor->add_child(track_pos);
  2959. track_pos->connect("draw", this, "_track_position_draw");
  2960. select_anim_warning = memnew(Label);
  2961. track_editor->add_child(select_anim_warning);
  2962. select_anim_warning->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2963. select_anim_warning->set_text(TTR("Select an AnimationPlayer from the Scene Tree to edit animations."));
  2964. select_anim_warning->set_autowrap(true);
  2965. select_anim_warning->set_align(Label::ALIGN_CENTER);
  2966. select_anim_warning->set_valign(Label::VALIGN_CENTER);
  2967. v_scroll = memnew(VScrollBar);
  2968. hb->add_child(v_scroll);
  2969. v_scroll->connect("value_changed", this, "_scroll_changed");
  2970. v_scroll->set_value(0);
  2971. key_editor_tab = memnew(TabContainer);
  2972. key_editor_tab->set_tab_align(TabContainer::ALIGN_LEFT);
  2973. hb->add_child(key_editor_tab);
  2974. key_editor_tab->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  2975. key_editor = memnew(PropertyEditor);
  2976. key_editor->hide_top_label();
  2977. key_editor->set_name(TTR("Key"));
  2978. key_editor_tab->add_child(key_editor);
  2979. key_edit = memnew(AnimationKeyEdit);
  2980. key_edit->undo_redo = undo_redo;
  2981. //key_edit->ke_dialog=key_edit_dialog;
  2982. type_menu = memnew(PopupMenu);
  2983. add_child(type_menu);
  2984. for (int i = 0; i < Variant::VARIANT_MAX; i++)
  2985. type_menu->add_item(Variant::get_type_name(Variant::Type(i)), i);
  2986. type_menu->connect("id_pressed", this, "_create_value_item");
  2987. VBoxContainer *curve_vb = memnew(VBoxContainer);
  2988. curve_vb->set_name(TTR("Transition"));
  2989. HBoxContainer *curve_hb = memnew(HBoxContainer);
  2990. curve_vb->add_child(curve_hb);
  2991. curve_linear = memnew(ToolButton);
  2992. curve_linear->set_focus_mode(FOCUS_NONE);
  2993. curve_hb->add_child(curve_linear);
  2994. curve_in = memnew(ToolButton);
  2995. curve_in->set_focus_mode(FOCUS_NONE);
  2996. curve_hb->add_child(curve_in);
  2997. curve_out = memnew(ToolButton);
  2998. curve_out->set_focus_mode(FOCUS_NONE);
  2999. curve_hb->add_child(curve_out);
  3000. curve_inout = memnew(ToolButton);
  3001. curve_inout->set_focus_mode(FOCUS_NONE);
  3002. curve_hb->add_child(curve_inout);
  3003. curve_outin = memnew(ToolButton);
  3004. curve_outin->set_focus_mode(FOCUS_NONE);
  3005. curve_hb->add_child(curve_outin);
  3006. curve_constant = memnew(ToolButton);
  3007. curve_constant->set_focus_mode(FOCUS_NONE);
  3008. curve_hb->add_child(curve_constant);
  3009. curve_edit = memnew(AnimationCurveEdit);
  3010. curve_vb->add_child(curve_edit);
  3011. curve_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  3012. key_editor_tab->add_child(curve_vb);
  3013. track_name = memnew(LineEdit);
  3014. track_name->set_as_toplevel(true);
  3015. track_name->hide();
  3016. add_child(track_name);
  3017. track_name->connect("text_entered", this, "_track_name_changed");
  3018. track_menu = memnew(PopupMenu);
  3019. add_child(track_menu);
  3020. track_menu->connect("id_pressed", this, "_track_menu_selected");
  3021. key_editor_tab->hide();
  3022. last_idx = 1;
  3023. _update_menu();
  3024. insert_confirm = memnew(ConfirmationDialog);
  3025. add_child(insert_confirm);
  3026. insert_confirm->connect("confirmed", this, "_confirm_insert_list");
  3027. click.click = ClickOver::CLICK_NONE;
  3028. name_column_ratio = 0.3;
  3029. timeline_pos = 0;
  3030. keying = false;
  3031. insert_frame = 0;
  3032. insert_query = false;
  3033. insert_queue = false;
  3034. editor_selection->connect("selection_changed", track_editor, "update");
  3035. scale_dialog = memnew(ConfirmationDialog);
  3036. VBoxContainer *vbc = memnew(VBoxContainer);
  3037. scale_dialog->add_child(vbc);
  3038. scale = memnew(SpinBox);
  3039. scale->set_min(-99999);
  3040. scale->set_max(99999);
  3041. scale->set_step(0.001);
  3042. vbc->add_margin_child(TTR("Scale Ratio:"), scale);
  3043. scale_dialog->connect("confirmed", this, "_scale");
  3044. add_child(scale_dialog);
  3045. call_select = memnew(SceneTreeDialog);
  3046. add_child(call_select);
  3047. call_select->set_title(TTR("Call Functions in Which Node?"));
  3048. cleanup_dialog = memnew(ConfirmationDialog);
  3049. add_child(cleanup_dialog);
  3050. VBoxContainer *cleanup_vb = memnew(VBoxContainer);
  3051. cleanup_dialog->add_child(cleanup_vb);
  3052. cleanup_keys = memnew(CheckButton);
  3053. cleanup_keys->set_text(TTR("Remove invalid keys"));
  3054. cleanup_keys->set_pressed(true);
  3055. cleanup_vb->add_child(cleanup_keys);
  3056. cleanup_tracks = memnew(CheckButton);
  3057. cleanup_tracks->set_text(TTR("Remove unresolved and empty tracks"));
  3058. cleanup_tracks->set_pressed(true);
  3059. cleanup_vb->add_child(cleanup_tracks);
  3060. cleanup_all = memnew(CheckButton);
  3061. cleanup_all->set_text(TTR("Clean-up all animations"));
  3062. cleanup_vb->add_child(cleanup_all);
  3063. cleanup_dialog->set_title(TTR("Clean-Up Animation(s) (NO UNDO!)"));
  3064. cleanup_dialog->get_ok()->set_text(TTR("Clean-Up"));
  3065. cleanup_dialog->connect("confirmed", this, "_menu_track", varray(TRACK_MENU_CLEAN_UP_CONFIRM));
  3066. track_editor->set_clip_contents(true);
  3067. }
  3068. AnimationKeyEditor::~AnimationKeyEditor() {
  3069. memdelete(key_edit);
  3070. }