animation_editor.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410
  1. /*************************************************************************/
  2. /* animation_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "animation_editor.h"
  30. #include "editor_settings.h"
  31. #include "os/keyboard.h"
  32. #include "os/os.h"
  33. #include "io/resource_saver.h"
  34. #include "pair.h"
  35. #include "scene/gui/separator.h"
  36. /* Missing to fix:
  37. *Set
  38. *Find better source for hint for edited value keys
  39. * + button on track to add a key
  40. * when clicked for first time, erase selection of not selected at first
  41. * automatically create discrete/continuous tracks!!
  42. *when create track do undo/redo
  43. */
  44. class AnimationKeyEdit : public Object {
  45. OBJ_TYPE(AnimationKeyEdit,Object);
  46. public:
  47. bool setting;
  48. static void _bind_methods() {
  49. ObjectTypeDB::bind_method("_update_obj",&AnimationKeyEdit::_update_obj);
  50. ObjectTypeDB::bind_method("_key_ofs_changed",&AnimationKeyEdit::_key_ofs_changed);
  51. }
  52. PopupDialog *ke_dialog;
  53. void _update_obj(const Ref<Animation> &p_anim) {
  54. if (setting)
  55. return;
  56. if (!ke_dialog->is_visible())
  57. return;
  58. if (!(animation==p_anim))
  59. return;
  60. notify_change();
  61. }
  62. void _key_ofs_changed(const Ref<Animation> &p_anim,float from, float to) {
  63. if (!ke_dialog->is_visible())
  64. return;
  65. if (!(animation==p_anim))
  66. return;
  67. if (from!=key_ofs)
  68. return;
  69. key_ofs=to;
  70. if (setting)
  71. return;
  72. notify_change();
  73. }
  74. bool _set(const StringName& p_name, const Variant& p_value) {
  75. int key = animation->track_find_key(track,key_ofs,true);
  76. ERR_FAIL_COND_V(key==-1,false);
  77. String name=p_name;
  78. if (name=="time") {
  79. float new_time = p_value;
  80. if (new_time==key_ofs)
  81. return true;
  82. int existing = animation->track_find_key(track,new_time,true);
  83. setting=true;
  84. undo_redo->create_action("Move Add Key",false);
  85. Variant val = animation->track_get_key_value(track,key);
  86. float trans = animation->track_get_key_transition(track,key);
  87. undo_redo->add_do_method(animation.ptr(),"track_remove_key",track,key);
  88. undo_redo->add_do_method(animation.ptr(),"track_insert_key",track,new_time,val,trans);
  89. undo_redo->add_do_method(this,"_key_ofs_changed",animation,key_ofs,new_time);
  90. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",track,new_time);
  91. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",track,key_ofs,val,trans);
  92. undo_redo->add_undo_method(this,"_key_ofs_changed",animation,new_time,key_ofs);
  93. if (existing!=-1) {
  94. Variant v = animation->track_get_key_value(track,existing);
  95. float trans = animation->track_get_key_transition(track,existing);
  96. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",track,new_time,v,trans);
  97. }
  98. undo_redo->commit_action();
  99. setting=false;
  100. return true;
  101. } else if (name=="easing") {
  102. float val = p_value;
  103. float prev_val = animation->track_get_key_transition(track,key);
  104. setting=true;
  105. undo_redo->create_action("Anim Change Transition",true);
  106. undo_redo->add_do_method(animation.ptr(),"track_set_key_transition",track,key,val);
  107. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",track,key,prev_val);
  108. undo_redo->add_do_method(this,"_update_obj",animation);
  109. undo_redo->add_undo_method(this,"_update_obj",animation);
  110. undo_redo->commit_action();
  111. setting=false;
  112. return true;
  113. }
  114. switch(animation->track_get_type(track)) {
  115. case Animation::TYPE_TRANSFORM: {
  116. Dictionary d_old = animation->track_get_key_value(track,key);
  117. Dictionary d_new = d_old;
  118. d_new[p_name]=p_value;
  119. setting=true;
  120. undo_redo->create_action("Anim Change Transform");
  121. undo_redo->add_do_method(animation.ptr(),"track_set_key_value",track,key,d_new);
  122. undo_redo->add_undo_method(animation.ptr(),"track_set_key_value",track,key,d_old);
  123. undo_redo->add_do_method(this,"_update_obj",animation);
  124. undo_redo->add_undo_method(this,"_update_obj",animation);
  125. undo_redo->commit_action();
  126. setting=false;
  127. return true;
  128. } break;
  129. case Animation::TYPE_VALUE: {
  130. if (name=="value") {
  131. setting=true;
  132. undo_redo->create_action("Anim Change Value",true);
  133. Variant prev = animation->track_get_key_value(track,key);
  134. undo_redo->add_do_method(animation.ptr(),"track_set_key_value",track,key,p_value);
  135. undo_redo->add_undo_method(animation.ptr(),"track_set_key_value",track,key,prev);
  136. undo_redo->add_do_method(this,"_update_obj",animation);
  137. undo_redo->add_undo_method(this,"_update_obj",animation);
  138. undo_redo->commit_action();
  139. setting=false;
  140. return true;
  141. }
  142. } break;
  143. case Animation::TYPE_METHOD: {
  144. Dictionary d_old = animation->track_get_key_value(track,key);
  145. Dictionary d_new = d_old;
  146. bool change_notify_deserved=false;
  147. bool mergeable=false;
  148. if (name=="name") {
  149. d_new["method"]=p_value;
  150. }
  151. if (name=="arg_count") {
  152. Vector<Variant> args = d_old["args"];
  153. args.resize(p_value);
  154. d_new["args"]=args;
  155. change_notify_deserved=true;
  156. }
  157. if (name.begins_with("args/")) {
  158. Vector<Variant> args = d_old["args"];
  159. int idx = name.get_slice("/",1).to_int();
  160. ERR_FAIL_INDEX_V(idx,args.size(),false);
  161. String what = name.get_slice("/",2);
  162. if (what=="type") {
  163. Variant::Type t = Variant::Type(int(p_value));
  164. if (t!=args[idx].get_type()) {
  165. Variant::CallError err;
  166. if (Variant::can_convert(args[idx].get_type(),t)) {
  167. Variant old=args[idx];
  168. Variant *ptrs[1]={&old};
  169. args[idx]=Variant::construct(t,(const Variant**)ptrs,1,err);
  170. } else {
  171. args[idx]=Variant::construct(t,NULL,0,err);
  172. }
  173. change_notify_deserved=true;
  174. d_new["args"]=args;
  175. }
  176. }
  177. if (what=="value") {
  178. args[idx]=p_value;
  179. d_new["args"]=args;
  180. mergeable=true;
  181. }
  182. }
  183. undo_redo->create_action("Anim Change Call",mergeable);
  184. Variant prev = animation->track_get_key_value(track,key);
  185. setting=true;
  186. undo_redo->add_do_method(animation.ptr(),"track_set_key_value",track,key,d_new);
  187. undo_redo->add_undo_method(animation.ptr(),"track_set_key_value",track,key,d_old);
  188. undo_redo->add_do_method(this,"_update_obj",animation);
  189. undo_redo->add_undo_method(this,"_update_obj",animation);
  190. undo_redo->commit_action();
  191. setting=false;
  192. if (change_notify_deserved)
  193. notify_change();
  194. return true;
  195. } break;
  196. }
  197. return false;
  198. return false;
  199. }
  200. bool _get(const StringName& p_name,Variant &r_ret) const {
  201. int key = animation->track_find_key(track,key_ofs,true);
  202. ERR_FAIL_COND_V(key==-1,false);
  203. String name=p_name;
  204. if (name=="time") {
  205. r_ret = key_ofs;
  206. return true;
  207. } else if (name=="easing") {
  208. r_ret = animation->track_get_key_transition(track,key);
  209. return true;
  210. }
  211. switch(animation->track_get_type(track)) {
  212. case Animation::TYPE_TRANSFORM: {
  213. Dictionary d = animation->track_get_key_value(track,key);
  214. ERR_FAIL_COND_V(!d.has(name),false);
  215. r_ret = d[p_name];
  216. return true;
  217. } break;
  218. case Animation::TYPE_VALUE: {
  219. if (name=="value") {
  220. r_ret = animation->track_get_key_value(track,key);;
  221. return true;
  222. }
  223. } break;
  224. case Animation::TYPE_METHOD: {
  225. Dictionary d = animation->track_get_key_value(track,key);
  226. if (name=="name") {
  227. ERR_FAIL_COND_V(!d.has("method"),false);
  228. r_ret=d["method"];
  229. return true;
  230. }
  231. ERR_FAIL_COND_V(!d.has("args"),false);
  232. Vector<Variant> args = d["args"];
  233. if (name=="arg_count") {
  234. r_ret=args.size();
  235. return true;
  236. }
  237. if (name.begins_with("args/")) {
  238. int idx = name.get_slice("/",1).to_int();
  239. ERR_FAIL_INDEX_V(idx,args.size(),false);
  240. String what = name.get_slice("/",2);
  241. if (what=="type") {
  242. r_ret=args[idx].get_type();
  243. return true;
  244. }
  245. if (what=="value") {
  246. r_ret=args[idx];
  247. return true;
  248. }
  249. }
  250. } break;
  251. }
  252. return false;
  253. }
  254. void _get_property_list( List<PropertyInfo> *p_list) const {
  255. if (animation.is_null())
  256. return;
  257. ERR_FAIL_INDEX(track,animation->get_track_count());
  258. int key = animation->track_find_key(track,key_ofs,true);
  259. ERR_FAIL_COND(key==-1);
  260. p_list->push_back( PropertyInfo( Variant::REAL, "time", PROPERTY_HINT_RANGE,"0,"+rtos(animation->get_length())+",0.01") );
  261. switch(animation->track_get_type(track)) {
  262. case Animation::TYPE_TRANSFORM: {
  263. p_list->push_back( PropertyInfo( Variant::VECTOR3, "loc"));
  264. p_list->push_back( PropertyInfo( Variant::QUAT, "rot"));
  265. p_list->push_back( PropertyInfo( Variant::VECTOR3, "scale"));
  266. } break;
  267. case Animation::TYPE_VALUE: {
  268. Variant v = animation->track_get_key_value(track,key);
  269. if (hint.type!=Variant::NIL) {
  270. PropertyInfo pi=hint;
  271. pi.name="value";
  272. p_list->push_back( pi );
  273. } else {
  274. PropertyHint hint= PROPERTY_HINT_NONE;
  275. String hint_string;
  276. if (v.get_type()==Variant::OBJECT) {
  277. //could actually check the object property if exists..? yes i will!
  278. Ref<Resource> res = v;
  279. if (res.is_valid()) {
  280. hint=PROPERTY_HINT_RESOURCE_TYPE;
  281. hint_string=res->get_type();
  282. }
  283. }
  284. if (v.get_type()!=Variant::NIL)
  285. p_list->push_back( PropertyInfo( v.get_type(), "value", hint,hint_string));
  286. }
  287. } break;
  288. case Animation::TYPE_METHOD: {
  289. p_list->push_back( PropertyInfo( Variant::STRING, "name"));
  290. p_list->push_back( PropertyInfo( Variant::INT, "arg_count",PROPERTY_HINT_RANGE,"0,5,1"));
  291. Dictionary d = animation->track_get_key_value(track,key);
  292. ERR_FAIL_COND(!d.has("args"));
  293. Vector<Variant> args=d["args"];
  294. String vtypes;
  295. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  296. if (i>0)
  297. vtypes+=",";
  298. vtypes+=Variant::get_type_name( Variant::Type(i) );
  299. }
  300. for(int i=0;i<args.size();i++) {
  301. p_list->push_back( PropertyInfo( Variant::INT, "args/"+itos(i)+"/type", PROPERTY_HINT_ENUM,vtypes));
  302. if (args[i].get_type()!=Variant::NIL)
  303. p_list->push_back( PropertyInfo( args[i].get_type(), "args/"+itos(i)+"/value"));
  304. }
  305. } break;
  306. }
  307. if (animation->track_get_type(track)!=Animation::TYPE_METHOD)
  308. p_list->push_back( PropertyInfo( Variant::REAL, "easing", PROPERTY_HINT_EXP_EASING));
  309. }
  310. UndoRedo *undo_redo;
  311. Ref<Animation> animation;
  312. int track;
  313. float key_ofs;
  314. PropertyInfo hint;
  315. void notify_change() {
  316. _change_notify();
  317. }
  318. AnimationKeyEdit() { key_ofs=0; track=-1; setting=false; }
  319. };
  320. void AnimationKeyEditor::_menu_track(int p_type) {
  321. ERR_FAIL_COND(!animation.is_valid());
  322. last_menu_track_opt=p_type;
  323. switch(p_type) {
  324. case TRACK_MENU_ADD_VALUE_TRACK:
  325. case TRACK_MENU_ADD_TRANSFORM_TRACK:
  326. case TRACK_MENU_ADD_CALL_TRACK: {
  327. undo_redo->create_action("Anim Add Track");
  328. undo_redo->add_do_method(animation.ptr(),"add_track",p_type);
  329. undo_redo->add_do_method(animation.ptr(),"track_set_path",animation->get_track_count(),".");
  330. undo_redo->add_undo_method(animation.ptr(),"remove_track",animation->get_track_count());
  331. undo_redo->commit_action();
  332. } break;
  333. case TRACK_MENU_SCALE:
  334. case TRACK_MENU_SCALE_PIVOT: {
  335. scale_dialog->popup_centered(Size2(200,100));
  336. } break;
  337. case TRACK_MENU_MOVE_UP: {
  338. int idx=selected_track;
  339. if (idx>0 && idx<animation->get_track_count()) {
  340. undo_redo->create_action("Move Anim Track Up");
  341. undo_redo->add_do_method(animation.ptr(),"track_move_down",idx);
  342. undo_redo->add_undo_method(animation.ptr(),"track_move_up",idx-1);
  343. undo_redo->commit_action();
  344. selected_track=idx-1;
  345. }
  346. } break;
  347. case TRACK_MENU_MOVE_DOWN: {
  348. int idx=selected_track;
  349. if (idx>=0 && idx<animation->get_track_count()-1) {
  350. undo_redo->create_action("Move Anim Track Down");
  351. undo_redo->add_do_method(animation.ptr(),"track_move_up",idx);
  352. undo_redo->add_undo_method(animation.ptr(),"track_move_down",idx+1);
  353. undo_redo->commit_action();
  354. selected_track=idx+1;
  355. }
  356. } break;
  357. case TRACK_MENU_REMOVE: {
  358. int idx=selected_track;
  359. if (idx>=0 && idx<animation->get_track_count()) {
  360. undo_redo->create_action("Remove Anim Track");
  361. undo_redo->add_do_method(animation.ptr(),"remove_track",idx);
  362. undo_redo->add_undo_method(animation.ptr(),"add_track",animation->track_get_type(idx),idx);
  363. undo_redo->add_undo_method(animation.ptr(),"track_set_path",idx,animation->track_get_path(idx));
  364. //todo interpolation
  365. for(int i=0;i<animation->track_get_key_count(idx);i++) {
  366. Variant v = animation->track_get_key_value(idx,i);
  367. float time = animation->track_get_key_time(idx,i);
  368. float trans = animation->track_get_key_transition(idx,i);
  369. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,time,v);
  370. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",idx,i,trans);
  371. }
  372. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",idx,animation->track_get_interpolation_type(idx));
  373. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  374. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",idx,animation->value_track_is_continuous(idx));
  375. }
  376. undo_redo->commit_action();
  377. }
  378. } break;
  379. case TRACK_MENU_DUPLICATE:
  380. case TRACK_MENU_DUPLICATE_TRANSPOSE: {
  381. //duplicait!
  382. if (selection.size() && animation.is_valid() && selected_track>=0 && selected_track<animation->get_track_count()) {
  383. int top_track=0x7FFFFFFF;
  384. float top_time = 1e10;
  385. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  386. const SelectedKey &sk = E->key();
  387. float t = animation->track_get_key_time(sk.track,sk.key);
  388. if (t<top_time)
  389. top_time=t;
  390. if (sk.track<top_track)
  391. top_track=sk.track;
  392. }
  393. ERR_FAIL_COND( top_track == 0x7FFFFFFF || top_time==1e10 );
  394. //
  395. int start_track = p_type==TRACK_MENU_DUPLICATE_TRANSPOSE ? selected_track : top_track;
  396. undo_redo->create_action("Anim Duplicate Keys");
  397. List<Pair<int,float> > new_selection_values;
  398. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  399. const SelectedKey &sk = E->key();
  400. float t = animation->track_get_key_time(sk.track,sk.key);
  401. float dst_time = t+(timeline_pos - top_time);
  402. int dst_track = sk.track + (start_track - top_track);
  403. if (dst_track < 0 || dst_track>= animation->get_track_count())
  404. continue;
  405. if (animation->track_get_type(dst_track) != animation->track_get_type(sk.track))
  406. continue;
  407. int existing_idx = animation->track_find_key(dst_track,dst_time,true);
  408. 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));
  409. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",dst_track,dst_time);
  410. Pair<int,float> p;
  411. p.first=dst_track;
  412. p.second=dst_time;
  413. new_selection_values.push_back( p );
  414. if (existing_idx!=-1) {
  415. 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));
  416. }
  417. }
  418. undo_redo->commit_action();
  419. //reselect duplicated
  420. Map<SelectedKey,KeyInfo> new_selection;
  421. for (List<Pair<int,float> >::Element *E=new_selection_values.front();E;E=E->next()) {
  422. int track=E->get().first;
  423. float time = E->get().second;
  424. int existing_idx = animation->track_find_key(track,time,true);
  425. if (existing_idx==-1)
  426. continue;
  427. SelectedKey sk2;
  428. sk2.track=track;
  429. sk2.key=existing_idx;
  430. KeyInfo ki;
  431. ki.pos=time;
  432. new_selection[sk2]=ki;
  433. }
  434. selection=new_selection;
  435. track_editor->update();
  436. }
  437. } break;
  438. case TRACK_MENU_SET_ALL_TRANS_LINEAR:
  439. case TRACK_MENU_SET_ALL_TRANS_CONSTANT:
  440. case TRACK_MENU_SET_ALL_TRANS_OUT:
  441. case TRACK_MENU_SET_ALL_TRANS_IN:
  442. case TRACK_MENU_SET_ALL_TRANS_INOUT:
  443. case TRACK_MENU_SET_ALL_TRANS_OUTIN: {
  444. if (!selection.size() || !animation.is_valid())
  445. break;
  446. float t=0;
  447. switch(p_type) {
  448. case TRACK_MENU_SET_ALL_TRANS_LINEAR: t=1.0; break;
  449. case TRACK_MENU_SET_ALL_TRANS_CONSTANT: t=0.0; break;
  450. case TRACK_MENU_SET_ALL_TRANS_OUT: t=0.5; break;
  451. case TRACK_MENU_SET_ALL_TRANS_IN: t=2.0; break;
  452. case TRACK_MENU_SET_ALL_TRANS_INOUT: t=-0.5; break;
  453. case TRACK_MENU_SET_ALL_TRANS_OUTIN: t=-2.0; break;
  454. }
  455. undo_redo->create_action("Set Transitions to: "+rtos(t));
  456. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  457. const SelectedKey &sk = E->key();
  458. undo_redo->add_do_method(animation.ptr(),"track_set_key_transition",sk.track,sk.key,t);
  459. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",sk.track,sk.key,animation->track_get_key_transition(sk.track,sk.key));
  460. }
  461. undo_redo->commit_action();
  462. } break;
  463. case TRACK_MENU_NEXT_STEP: {
  464. if (animation.is_null())
  465. break;
  466. float step = animation->get_step();
  467. if (step==0)
  468. step=1;
  469. float pos=timeline_pos;
  470. pos=Math::stepify(pos+step,step);
  471. if (pos>animation->get_length())
  472. pos=animation->get_length();
  473. timeline_pos=pos;
  474. track_pos->update();
  475. emit_signal("timeline_changed",pos);
  476. } break;
  477. case TRACK_MENU_PREV_STEP: {
  478. if (animation.is_null())
  479. break;
  480. float step = animation->get_step();
  481. if (step==0)
  482. step=1;
  483. float pos=timeline_pos;
  484. pos=Math::stepify(pos-step,step);
  485. if (pos<0)
  486. pos=0;
  487. timeline_pos=pos;
  488. track_pos->update();
  489. emit_signal("timeline_changed",pos);
  490. } break;
  491. case TRACK_MENU_OPTIMIZE: {
  492. optimize_dialog->popup_centered(Size2(250,180));
  493. } break;
  494. }
  495. }
  496. void AnimationKeyEditor::_animation_optimize() {
  497. print_line("OPTIMIZE!");
  498. animation->optimize(optimize_linear_error->get_val(),optimize_angular_error->get_val(),optimize_max_angle->get_val());
  499. track_editor->update();
  500. undo_redo->clear_history();
  501. }
  502. float AnimationKeyEditor::_get_zoom_scale() const {
  503. float zv = zoom->get_val();
  504. if (zv<1) {
  505. zv = 1.0-zv;
  506. return Math::pow(1.0+zv,8.0)*100;
  507. } else {
  508. return 1.0/Math::pow(zv,8.0)*100;
  509. }
  510. }
  511. void AnimationKeyEditor::_track_pos_draw() {
  512. if (!animation.is_valid()) {
  513. return;
  514. }
  515. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  516. Size2 size= track_editor->get_size() - style->get_minimum_size();
  517. Size2 ofs = style->get_offset();
  518. int settings_limit = size.width - right_data_size_cache;
  519. int name_limit = settings_limit * name_column_ratio;
  520. float keys_from= h_scroll->get_val();
  521. float zoom_scale = _get_zoom_scale();
  522. float keys_to=keys_from+(settings_limit-name_limit) / zoom_scale;
  523. //will move to separate control! (for speedup)
  524. if (timeline_pos >= keys_from && timeline_pos<keys_to) {
  525. //draw position
  526. int pixel = (timeline_pos - h_scroll->get_val()) * zoom_scale;
  527. pixel+=name_limit;
  528. track_pos->draw_line(ofs+Point2(pixel,0),ofs+Point2(pixel,size.height),Color(1,0.3,0.3,0.8));
  529. }
  530. }
  531. void AnimationKeyEditor::_track_editor_draw() {
  532. VisualServer::get_singleton()->canvas_item_set_clip(track_editor->get_canvas_item(),true);
  533. if (animation.is_valid() && animation->get_track_count()) {
  534. if (selected_track < 0)
  535. selected_track=0;
  536. else if (selected_track>=animation->get_track_count())
  537. selected_track=animation->get_track_count()-1;
  538. }
  539. track_pos->update();
  540. Control *te=track_editor;
  541. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  542. te->draw_style_box(style,Rect2(Point2(),track_editor->get_size()));
  543. if (te->has_focus()) {
  544. te->draw_style_box(get_stylebox("bg_focus","Tree"),Rect2(Point2(),track_editor->get_size()));
  545. }
  546. if (!animation.is_valid()) {
  547. v_scroll->hide();
  548. h_scroll->hide();
  549. menu_track->set_disabled(true);
  550. edit_button->set_disabled(true);
  551. move_up_button->set_disabled(true);
  552. move_down_button->set_disabled(true);
  553. remove_button->set_disabled(true);
  554. return;
  555. }
  556. menu_track->set_disabled(false);
  557. edit_button->set_disabled(false);
  558. move_up_button->set_disabled(false);
  559. move_down_button->set_disabled(false);
  560. remove_button->set_disabled(false);
  561. te_drawing=true;
  562. Size2 size= te->get_size() - style->get_minimum_size();
  563. Size2 ofs = style->get_offset();
  564. Ref<Font> font = te->get_font("font","Tree");
  565. int sep = get_constant("vseparation","Tree");
  566. int hsep = get_constant("hseparation","Tree");
  567. Color color = get_color("font_color","Tree");
  568. Color sepcolor = get_color("guide_color","Tree");
  569. Color timecolor = get_color("prop_subsection","Editor");
  570. timecolor = Color::html("ff4a414f");
  571. Color hover_color = Color(1,1,1,0.05);
  572. Color select_color = Color(1,1,1,0.1);
  573. Color track_select_color =Color::html("ffbd8e8e");
  574. Ref<Texture> remove_icon = get_icon("Remove","EditorIcons");
  575. Ref<Texture> move_up_icon = get_icon("MoveUp","EditorIcons");
  576. Ref<Texture> move_down_icon = get_icon("MoveDown","EditorIcons");
  577. Ref<Texture> remove_icon_hl = get_icon("RemoveHl","EditorIcons");
  578. Ref<Texture> move_up_icon_hl = get_icon("MoveUpHl","EditorIcons");
  579. Ref<Texture> move_down_icon_hl = get_icon("MoveDownHl","EditorIcons");
  580. Ref<Texture> add_key_icon = get_icon("TrackAddKey","EditorIcons");
  581. Ref<Texture> add_key_icon_hl = get_icon("TrackAddKeyHl","EditorIcons");
  582. Ref<Texture> down_icon = get_icon("select_arrow","Tree");
  583. Ref<Texture> interp_icon[3]={
  584. get_icon("InterpRaw","EditorIcons"),
  585. get_icon("InterpLinear","EditorIcons"),
  586. get_icon("InterpCubic","EditorIcons")
  587. };
  588. Ref<Texture> cont_icon[3]={
  589. get_icon("TrackDiscrete","EditorIcons"),
  590. get_icon("TrackContinuous","EditorIcons")
  591. };
  592. Ref<Texture> type_icon[3]={
  593. get_icon("KeyValue","EditorIcons"),
  594. get_icon("KeyXform","EditorIcons"),
  595. get_icon("KeyCall","EditorIcons")
  596. };
  597. Ref<Texture> hsize_icon = get_icon("Hsize","EditorIcons");
  598. Ref<Texture> type_hover=get_icon("KeyHover","EditorIcons");
  599. Ref<Texture> type_selected=get_icon("KeySelected","EditorIcons");
  600. int right_separator_ofs = down_icon->get_width() *2 + add_key_icon->get_width() + interp_icon[0]->get_width() + cont_icon[0]->get_width() + hsep*7;
  601. int h = font->get_height()+sep;
  602. int fit = (size.height / h)-1;
  603. int total = animation->get_track_count();
  604. if (total < fit) {
  605. v_scroll->hide();
  606. v_scroll->set_max( total );
  607. v_scroll->set_page( fit );
  608. } else {
  609. v_scroll->show();
  610. v_scroll->set_max( total );
  611. v_scroll->set_page( fit );
  612. }
  613. int settings_limit = size.width - right_separator_ofs;
  614. int name_limit = settings_limit * name_column_ratio;
  615. te->draw_line(ofs+Point2(name_limit,0),ofs+Point2(name_limit,size.height),color);
  616. te->draw_line(ofs+Point2(settings_limit,0),ofs+Point2(settings_limit,size.height),color);
  617. te->draw_texture(hsize_icon,ofs+Point2(name_limit-hsize_icon->get_width()-hsep,(h-hsize_icon->get_height())/2));
  618. te->draw_line(ofs+Point2(0,h),ofs+Point2(size.width,h),color);
  619. // draw time
  620. float keys_from;
  621. float keys_to;
  622. float zoom_scale;
  623. {
  624. int zoomw = settings_limit-name_limit;
  625. float scale = _get_zoom_scale();
  626. zoom_scale=scale;
  627. float l = animation->get_length();
  628. if (l<=0)
  629. l=0.001; //avoid crashor
  630. int end_px = (l - h_scroll->get_val()) * scale;
  631. int begin_px = -h_scroll->get_val() * scale;
  632. Color notimecol;
  633. notimecol.r=timecolor.gray();
  634. notimecol.g=notimecol.r;
  635. notimecol.b=notimecol.r;
  636. notimecol.a=timecolor.a;
  637. {
  638. te->draw_rect(Rect2( ofs + Point2(name_limit,0), Point2(zoomw-1,h)), notimecol);
  639. if (begin_px < zoomw && end_px >0) {
  640. if (begin_px<0)
  641. begin_px=0;
  642. if (end_px>zoomw)
  643. end_px=zoomw;
  644. te->draw_rect(Rect2( ofs + Point2(name_limit+begin_px,0), Point2(end_px-begin_px-1,h)), timecolor);
  645. }
  646. }
  647. keys_from= h_scroll->get_val();
  648. keys_to=keys_from+zoomw / scale;
  649. {
  650. float time_min=0;
  651. float time_max=animation->get_length();
  652. for(int i=0;i<animation->get_track_count();i++) {
  653. if (animation->track_get_key_count(i)>0) {
  654. float beg = animation->track_get_key_time(i,0);
  655. if (beg<time_min)
  656. time_min=beg;
  657. float end = animation->track_get_key_time(i,animation->track_get_key_count(i)-1);
  658. if (end>time_max)
  659. time_max=end;
  660. }
  661. }
  662. float extra = (zoomw / scale)*0.5;
  663. if (time_min<-0.001)
  664. time_min-=extra;
  665. time_max+=extra;
  666. h_scroll->set_min(time_min);
  667. h_scroll->set_max(time_max);
  668. if (zoomw / scale < (time_max-time_min)) {
  669. h_scroll->show();
  670. } else {
  671. h_scroll->hide();
  672. }
  673. }
  674. h_scroll->set_page(zoomw / scale);
  675. Color color_time_sec = color;
  676. Color color_time_dec = color;
  677. color_time_dec.a*=0.5;
  678. #define SC_ADJ 100
  679. int min=30;
  680. int dec=1;
  681. int step=1;
  682. int decimals=2;
  683. bool step_found=false;
  684. while(!step_found) {
  685. static const int _multp[3]={1,2,5};
  686. for(int i=0;i<3;i++) {
  687. step = (_multp[i] * dec);
  688. if (step*scale/SC_ADJ> min) {
  689. step_found=true;
  690. break;
  691. }
  692. }
  693. if (step_found)
  694. break;
  695. dec*=10;
  696. decimals--;
  697. if (decimals<0)
  698. decimals=0;
  699. }
  700. for(int i=0;i<zoomw;i++) {
  701. float pos = h_scroll->get_val() + double(i)/scale;
  702. float prev = h_scroll->get_val() + (double(i)-1.0)/scale;
  703. int sc = int(Math::floor(pos*SC_ADJ));
  704. int prev_sc = int(Math::floor(prev*SC_ADJ));
  705. bool sub = (sc % SC_ADJ);
  706. if ((sc/step)!=(prev_sc/step) || (prev_sc<0 && sc>=0)) {
  707. int scd = sc < 0 ? prev_sc : sc;
  708. te->draw_line( ofs + Point2(name_limit+i,0), ofs+Point2(name_limit+i,h), color);
  709. 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);
  710. }
  711. }
  712. }
  713. for(int i=0;i<fit;i++) {
  714. int idx = v_scroll->get_val() + i;
  715. if (idx>=animation->get_track_count())
  716. break;
  717. int y = h+i*h+sep;
  718. if (/*mouse_over.over!=MouseOver::OVER_NONE &&*/ idx==mouse_over.track) {
  719. Color sepc=hover_color;
  720. te->draw_rect(Rect2(ofs+Point2(0,y),Size2(size.width,h-1)),sepc);
  721. }
  722. if (selected_track==idx) {
  723. Color tc = select_color;
  724. //tc.a*=0.7;
  725. te->draw_rect(Rect2(ofs+Point2(0,y),Size2(size.width-1,h-1)),tc);
  726. }
  727. te->draw_texture(type_icon[animation->track_get_type(idx)],ofs+Point2(0,y+(h-type_icon[0]->get_height())/2).floor());
  728. NodePath np = animation->track_get_path(idx);
  729. Node *n = root->get_node(np);
  730. Color ncol = color;
  731. if (n && editor_selection->is_selected(n))
  732. ncol=track_select_color;
  733. 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);
  734. te->draw_line(ofs+Point2(0,y+h),ofs+Point2(size.width,y+h),sepcolor);
  735. Point2 icon_ofs = ofs + Point2( size.width, y + (h - remove_icon->get_height() )/2).floor();
  736. icon_ofs.y+=4;
  737. /* icon_ofs.x-=remove_icon->get_width();
  738. te->draw_texture((mouse_over.over==MouseOver::OVER_REMOVE && mouse_over.track==idx)?remove_icon_hl:remove_icon,icon_ofs);
  739. icon_ofs.x-=hsep;
  740. icon_ofs.x-=move_down_icon->get_width();
  741. te->draw_texture((mouse_over.over==MouseOver::OVER_DOWN && mouse_over.track==idx)?move_down_icon_hl:move_down_icon,icon_ofs);
  742. icon_ofs.x-=hsep;
  743. icon_ofs.x-=move_up_icon->get_width();
  744. te->draw_texture((mouse_over.over==MouseOver::OVER_UP && mouse_over.track==idx)?move_up_icon_hl:move_up_icon,icon_ofs);
  745. icon_ofs.x-=hsep;
  746. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  747. icon_ofs.x-=hsep;
  748. */
  749. icon_ofs.x-=down_icon->get_width();
  750. te->draw_texture(down_icon,icon_ofs);
  751. int interp_type = animation->track_get_interpolation_type(idx);
  752. ERR_CONTINUE(interp_type<0 || interp_type>=3);
  753. icon_ofs.x-=hsep;
  754. icon_ofs.x-=interp_icon[interp_type]->get_width();
  755. te->draw_texture(interp_icon[interp_type],icon_ofs);
  756. icon_ofs.x-=hsep;
  757. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  758. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  759. int continuous = animation->value_track_is_continuous(idx)?1:0;
  760. icon_ofs.x-=hsep;
  761. icon_ofs.x-=down_icon->get_width();
  762. te->draw_texture(down_icon,icon_ofs);
  763. icon_ofs.x-=hsep;
  764. icon_ofs.x-=cont_icon[continuous]->get_width();
  765. te->draw_texture(cont_icon[continuous],icon_ofs);
  766. } else {
  767. icon_ofs.x -= hsep*2 + cont_icon[0]->get_width() + down_icon->get_width();
  768. }
  769. icon_ofs.x-=hsep;
  770. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  771. icon_ofs.x-=hsep;
  772. icon_ofs.x-=add_key_icon->get_width();
  773. te->draw_texture((mouse_over.over==MouseOver::OVER_ADD_KEY && mouse_over.track==idx)?add_key_icon_hl:add_key_icon,icon_ofs);
  774. //draw the keys;
  775. int tt = animation->track_get_type(idx);
  776. float key_vofs = Math::floor((h - type_icon[tt]->get_height())/2);
  777. float key_hofs = -Math::floor(type_icon[tt]->get_height()/2);
  778. int kc=animation->track_get_key_count(idx);
  779. for(int i=0;i<kc;i++) {
  780. float time = animation->track_get_key_time(idx,i);
  781. if (time<keys_from)
  782. continue;
  783. if (time>keys_to)
  784. break;
  785. float x = key_hofs + name_limit + (time-keys_from)*zoom_scale;
  786. Ref<Texture> tex = type_icon[tt];
  787. SelectedKey sk;
  788. sk.key=i;
  789. sk.track=idx;
  790. if (selection.has(sk)) {
  791. if (click.click==ClickOver::CLICK_MOVE_KEYS)
  792. continue;
  793. tex=type_selected;
  794. }
  795. if (mouse_over.over==MouseOver::OVER_KEY && mouse_over.track==idx && mouse_over.over_key==i)
  796. tex=type_hover;
  797. te->draw_texture(tex,ofs+Point2(x,y+key_vofs).floor());
  798. }
  799. }
  800. switch(click.click) {
  801. case ClickOver::CLICK_SELECT_KEYS: {
  802. te->draw_rect(Rect2(click.at,click.to-click.at),Color(0.7,0.7,1.0,0.5));
  803. } break;
  804. case ClickOver::CLICK_MOVE_KEYS: {
  805. float from_t = 1e20;
  806. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  807. float t = animation->track_get_key_time(E->key().track,E->key().key);
  808. if (t<from_t)
  809. from_t=t;
  810. }
  811. float motion = from_t+(click.to.x - click.at.x)/zoom_scale;
  812. if (step->get_val())
  813. motion = Math::stepify(motion,step->get_val());
  814. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  815. int idx = E->key().track;
  816. int i = idx-v_scroll->get_val();
  817. if (i<0 || i>=fit)
  818. continue;
  819. int y = h+i*h+sep;
  820. float key_vofs = Math::floor((h - type_selected->get_height())/2);
  821. float key_hofs = -Math::floor(type_selected->get_height()/2);
  822. float time = animation->track_get_key_time(idx,E->key().key);
  823. float diff = time-from_t;
  824. float t = motion + diff;
  825. float x = (t-keys_from)*zoom_scale;
  826. //x+=click.to.x - click.at.x;
  827. if (x<0 || x>=(settings_limit-name_limit))
  828. continue;
  829. x+=name_limit;
  830. te->draw_texture(type_selected,ofs+Point2(x+key_hofs,y+key_vofs).floor());
  831. }
  832. } break;
  833. default: {};
  834. }
  835. te_drawing=false;
  836. }
  837. void AnimationKeyEditor::_track_name_changed(const String& p_name) {
  838. ERR_FAIL_COND(!animation.is_valid());
  839. undo_redo->create_action("Anim Track Rename");
  840. undo_redo->add_do_method(animation.ptr(),"track_set_path",track_name_editing,p_name);
  841. undo_redo->add_undo_method(animation.ptr(),"track_set_path",track_name_editing,animation->track_get_path(track_name_editing));
  842. undo_redo->commit_action();
  843. track_name->hide();
  844. }
  845. void AnimationKeyEditor::_track_menu_selected(int p_idx) {
  846. ERR_FAIL_COND(!animation.is_valid());
  847. if (interp_editing!=-1) {
  848. ERR_FAIL_INDEX(interp_editing,animation->get_track_count());
  849. undo_redo->create_action("Anim Track Change Interpolation");
  850. undo_redo->add_do_method(animation.ptr(),"track_set_interpolation_type",interp_editing,p_idx);
  851. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",interp_editing,animation->track_get_interpolation_type(interp_editing));
  852. undo_redo->commit_action();
  853. }
  854. if (cont_editing!=-1) {
  855. ERR_FAIL_INDEX(cont_editing,animation->get_track_count());
  856. undo_redo->create_action("Anim Track Change Value Mode");
  857. undo_redo->add_do_method(animation.ptr(),"value_track_set_continuous",cont_editing,p_idx);
  858. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",cont_editing,animation->value_track_is_continuous(cont_editing));
  859. undo_redo->commit_action();
  860. }
  861. }
  862. struct _AnimMoveRestore {
  863. int track;
  864. float time;
  865. Variant key;
  866. float transition;
  867. };
  868. void AnimationKeyEditor::_clear_selection_for_anim(const Ref<Animation>& p_anim) {
  869. if (!(animation==p_anim))
  870. return;
  871. selection.clear();
  872. }
  873. void AnimationKeyEditor::_select_at_anim(const Ref<Animation>& p_anim,int p_track,float p_pos){
  874. if (!(animation==p_anim))
  875. return;
  876. int idx = animation->track_find_key(p_track,p_pos,true);
  877. ERR_FAIL_COND(idx<0);
  878. SelectedKey sk;
  879. sk.track=p_track;
  880. sk.key=idx;
  881. KeyInfo ki;
  882. ki.pos=p_pos;
  883. selection.insert(sk,ki);
  884. }
  885. PropertyInfo AnimationKeyEditor::_find_hint_for_track(int p_idx) {
  886. ERR_FAIL_COND_V(!animation.is_valid(),PropertyInfo());
  887. ERR_FAIL_INDEX_V(p_idx,animation->get_track_count(),PropertyInfo());
  888. if (!root)
  889. return PropertyInfo();
  890. NodePath path = animation->track_get_path(p_idx);
  891. String property = path.get_property();
  892. if (property=="")
  893. return PropertyInfo();
  894. if (!root->has_node_and_resource(path))
  895. return PropertyInfo();
  896. RES res;
  897. Node *node = root->get_node_and_resource(path,res);
  898. List<PropertyInfo> pinfo;
  899. if (res.is_valid())
  900. res->get_property_list(&pinfo);
  901. else
  902. node->get_property_list(&pinfo);
  903. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  904. if (E->get().name==property)
  905. return E->get();
  906. }
  907. return PropertyInfo();
  908. }
  909. void AnimationKeyEditor::_track_editor_input_event(const InputEvent& p_input) {
  910. Control *te=track_editor;
  911. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  912. if (!animation.is_valid()) {
  913. return;
  914. }
  915. Size2 size= te->get_size() - style->get_minimum_size();
  916. Size2 ofs = style->get_offset();
  917. Ref<Font> font = te->get_font("font","Tree");
  918. int sep = get_constant("vseparation","Tree");
  919. int hsep = get_constant("hseparation","Tree");
  920. Color color = get_color("font_color","Tree");
  921. Color sepcolor = get_color("guide_color","Tree");
  922. Ref<Texture> remove_icon = get_icon("Remove","EditorIcons");
  923. Ref<Texture> move_up_icon = get_icon("MoveUp","EditorIcons");
  924. Ref<Texture> move_down_icon = get_icon("MoveDown","EditorIcons");
  925. Ref<Texture> down_icon = get_icon("select_arrow","Tree");
  926. Ref<Texture> hsize_icon = get_icon("Hsize","EditorIcons");
  927. Ref<Texture> add_key_icon = get_icon("TrackAddKey","EditorIcons");
  928. Ref<Texture> interp_icon[3]={
  929. get_icon("InterpRaw","EditorIcons"),
  930. get_icon("InterpLinear","EditorIcons"),
  931. get_icon("InterpCubic","EditorIcons")
  932. };
  933. Ref<Texture> cont_icon[3]={
  934. get_icon("TrackDiscrete","EditorIcons"),
  935. get_icon("TrackContinuous","EditorIcons")
  936. };
  937. Ref<Texture> type_icon[3]={
  938. get_icon("KeyValue","EditorIcons"),
  939. get_icon("KeyXform","EditorIcons"),
  940. get_icon("KeyCall","EditorIcons")
  941. };
  942. int right_separator_ofs = down_icon->get_width() *2 + add_key_icon->get_width() + interp_icon[0]->get_width() + cont_icon[0]->get_width() + hsep*7;
  943. int h = font->get_height()+sep;
  944. int fit = (size.height / h)-1;
  945. int total = animation->get_track_count();
  946. if (total < fit) {
  947. v_scroll->hide();
  948. } else {
  949. v_scroll->show();
  950. v_scroll->set_max( total );
  951. v_scroll->set_page( fit );
  952. }
  953. int settings_limit = size.width - right_separator_ofs;
  954. int name_limit = settings_limit * name_column_ratio;
  955. switch(p_input.type) {
  956. case InputEvent::KEY: {
  957. if (p_input.key.scancode==KEY_D && p_input.key.pressed && p_input.key.mod.command) {
  958. if (p_input.key.mod.shift)
  959. _menu_track(TRACK_MENU_DUPLICATE_TRANSPOSE);
  960. else
  961. _menu_track(TRACK_MENU_DUPLICATE);
  962. accept_event();;
  963. } else if (p_input.key.scancode==KEY_DELETE && p_input.key.pressed && click.click==ClickOver::CLICK_NONE) {
  964. if (selection.size()) {
  965. undo_redo->create_action("Anim Delete Keys");
  966. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  967. undo_redo->add_do_method(animation.ptr(),"track_remove_key",E->key().track,E->key().key);
  968. 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));
  969. }
  970. undo_redo->commit_action();
  971. selection.clear();
  972. accept_event();
  973. }
  974. } else if (animation.is_valid() && animation->get_track_count()>0) {
  975. if (p_input.is_pressed() && (p_input.is_action("ui_up") || p_input.is_action("ui_page_up"))) {
  976. if (p_input.is_action("ui_up"))
  977. selected_track--;
  978. if (v_scroll->is_visible() && p_input.is_action("ui_page_up"))
  979. selected_track--;;
  980. if (selected_track<0)
  981. selected_track=0;
  982. if (v_scroll->is_visible()) {
  983. if (v_scroll->get_val() > selected_track)
  984. v_scroll->set_val(selected_track);
  985. }
  986. track_editor->update();
  987. accept_event();
  988. }
  989. if (p_input.is_pressed() && (p_input.is_action("ui_down") || p_input.is_action("ui_page_down"))) {
  990. if (p_input.is_action("ui_down"))
  991. selected_track++;
  992. else if (v_scroll->is_visible() && p_input.is_action("ui_page_down"))
  993. selected_track+=v_scroll->get_page();
  994. if (selected_track >= animation->get_track_count())
  995. selected_track=animation->get_track_count()-1;
  996. if (v_scroll->is_visible() && v_scroll->get_page()+v_scroll->get_val() < selected_track+1) {
  997. v_scroll->set_val(selected_track-v_scroll->get_page()+1);
  998. }
  999. track_editor->update();
  1000. accept_event();
  1001. }
  1002. }
  1003. } break;
  1004. case InputEvent::MOUSE_BUTTON: {
  1005. const InputEventMouseButton &mb = p_input.mouse_button;
  1006. if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) {
  1007. v_scroll->set_val( v_scroll->get_val() - v_scroll->get_page() / 8 );
  1008. }
  1009. if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) {
  1010. v_scroll->set_val( v_scroll->get_val() + v_scroll->get_page() / 8 );
  1011. }
  1012. if (mb.button_index==BUTTON_LEFT && !(mb.button_mask&~BUTTON_MASK_LEFT)) {
  1013. if (mb.pressed) {
  1014. Point2 mpos = Point2(mb.x,mb.y)-ofs;
  1015. if (mpos.y < h ) {
  1016. if (mpos.x<name_limit && mpos.x > (name_limit - hsep - hsize_icon->get_width())) {
  1017. click.click=ClickOver::CLICK_RESIZE_NAMES;
  1018. click.at=Point2(mb.x,mb.y);
  1019. click.to=click.at;
  1020. click.at.y=name_limit;
  1021. }
  1022. if (mpos.x>=name_limit && mpos.x<settings_limit) {
  1023. //seek
  1024. //int zoomw = settings_limit-name_limit;
  1025. float scale = _get_zoom_scale();
  1026. float pos = h_scroll->get_val() + (mpos.x-name_limit) / scale;
  1027. if (animation->get_step())
  1028. pos=Math::stepify(pos,animation->get_step());
  1029. if (pos<0 )
  1030. pos=0;
  1031. if (pos>=animation->get_length())
  1032. pos=animation->get_length();
  1033. timeline_pos=pos;
  1034. click.click=ClickOver::CLICK_DRAG_TIMELINE;
  1035. click.at=Point2(mb.x,mb.y);
  1036. click.to=click.at;
  1037. emit_signal("timeline_changed",pos);
  1038. }
  1039. return;
  1040. }
  1041. mpos.y -= h;
  1042. int idx = mpos.y / h;
  1043. idx+=v_scroll->get_val();
  1044. if (idx <0)
  1045. break;
  1046. if (idx>=animation->get_track_count()) {
  1047. if (mpos.x >= name_limit && mpos.x<settings_limit) {
  1048. click.click=ClickOver::CLICK_SELECT_KEYS;
  1049. click.at=Point2(mb.x,mb.y);
  1050. click.to=click.at;
  1051. //drag select region
  1052. }
  1053. break;
  1054. }
  1055. if (mpos.x < name_limit) {
  1056. //name column
  1057. // area
  1058. if (idx!=selected_track) {
  1059. selected_track=idx;
  1060. track_editor->update();
  1061. break;
  1062. }
  1063. Rect2 area(ofs.x,ofs.y+((int(mpos.y)/h)+1)*h,name_limit, h );
  1064. track_name->set_text(animation->track_get_path(idx));
  1065. track_name->set_pos( te->get_global_pos() + area.pos);
  1066. track_name->set_size(area.size);
  1067. track_name->show_modal();
  1068. track_name->grab_focus();
  1069. track_name->select_all();
  1070. track_name_editing=idx;
  1071. } else if (mpos.x < settings_limit) {
  1072. float pos = mpos.x - name_limit;
  1073. pos/=_get_zoom_scale();
  1074. pos+=h_scroll->get_val();
  1075. float w_time = (type_icon[0]->get_width() / _get_zoom_scale())/2.0;
  1076. int kidx = animation->track_find_key(idx,pos);
  1077. int kidx_n = kidx+1;
  1078. int key=-1;
  1079. if (kidx>=0 && kidx<animation->track_get_key_count(idx)) {
  1080. float kpos = animation->track_get_key_time(idx,kidx);
  1081. if (ABS(pos-kpos)<=w_time) {
  1082. key=kidx;
  1083. }
  1084. }
  1085. if (key==-1 && kidx_n>=0 && kidx_n<animation->track_get_key_count(idx)) {
  1086. float kpos = animation->track_get_key_time(idx,kidx_n);
  1087. if (ABS(pos-kpos)<=w_time) {
  1088. key=kidx_n;
  1089. }
  1090. }
  1091. if (key==-1) {
  1092. click.click=ClickOver::CLICK_SELECT_KEYS;
  1093. click.at=Point2(mb.x,mb.y);
  1094. click.to=click.at;
  1095. click.shift=mb.mod.shift;
  1096. selected_track=idx;
  1097. track_editor->update();
  1098. //drag select region
  1099. return;
  1100. }
  1101. if (mb.mod.command || edit_button->is_pressed()) {
  1102. key_edit->animation=animation;
  1103. key_edit->track=idx;
  1104. key_edit->key_ofs=animation->track_get_key_time(idx,key);
  1105. key_edit->hint=_find_hint_for_track(idx);
  1106. key_edit->notify_change();
  1107. key_edit_dialog->set_size( Size2( 200,200) );
  1108. key_edit_dialog->set_pos( track_editor->get_global_pos() + ofs + mpos +Point2(-100,20));
  1109. key_edit_dialog->popup();
  1110. }
  1111. SelectedKey sk;
  1112. sk.track=idx;
  1113. sk.key=key;
  1114. KeyInfo ki;
  1115. ki.pos= animation->track_get_key_time(idx,key);
  1116. click.shift=mb.mod.shift;
  1117. click.selk=sk;
  1118. if (!mb.mod.shift && !selection.has(sk))
  1119. selection.clear();
  1120. selection.insert(sk,ki);
  1121. click.click=ClickOver::CLICK_MOVE_KEYS;
  1122. click.at=Point2(mb.x,mb.y);
  1123. click.to=click.at;
  1124. update();
  1125. selected_track=idx;
  1126. track_editor->update();
  1127. } else {
  1128. //button column
  1129. int ofsx = size.width - mpos.x;
  1130. if (ofsx < 0)
  1131. break;
  1132. /*
  1133. if (ofsx < remove_icon->get_width()) {
  1134. undo_redo->create_action("Remove Anim Track");
  1135. undo_redo->add_do_method(animation.ptr(),"remove_track",idx);
  1136. undo_redo->add_undo_method(animation.ptr(),"add_track",animation->track_get_type(idx),idx);
  1137. undo_redo->add_undo_method(animation.ptr(),"track_set_path",idx,animation->track_get_path(idx));
  1138. //todo interpolation
  1139. for(int i=0;i<animation->track_get_key_count(idx);i++) {
  1140. Variant v = animation->track_get_key_value(idx,i);
  1141. float time = animation->track_get_key_time(idx,i);
  1142. float trans = animation->track_get_key_transition(idx,i);
  1143. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,time,v);
  1144. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",idx,i,trans);
  1145. }
  1146. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",idx,animation->track_get_interpolation_type(idx));
  1147. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  1148. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",idx,animation->value_track_is_continuous(idx));
  1149. }
  1150. undo_redo->commit_action();
  1151. return;
  1152. }
  1153. ofsx-=hsep+remove_icon->get_width();
  1154. if (ofsx < move_down_icon->get_width()) {
  1155. if (idx < animation->get_track_count() -1) {
  1156. undo_redo->create_action("Move Anim Track Down");
  1157. undo_redo->add_do_method(animation.ptr(),"track_move_up",idx);
  1158. undo_redo->add_undo_method(animation.ptr(),"track_move_down",idx+1);
  1159. undo_redo->commit_action();
  1160. }
  1161. return;
  1162. }
  1163. ofsx-=hsep+move_down_icon->get_width();
  1164. if (ofsx < move_up_icon->get_width()) {
  1165. if (idx >0) {
  1166. undo_redo->create_action("Move Anim Track Up");
  1167. undo_redo->add_do_method(animation.ptr(),"track_move_down",idx);
  1168. undo_redo->add_undo_method(animation.ptr(),"track_move_up",idx-1);
  1169. undo_redo->commit_action();
  1170. }
  1171. return;
  1172. }
  1173. ofsx-=hsep*3+move_up_icon->get_width();
  1174. */
  1175. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep*2) {
  1176. track_menu->clear();
  1177. track_menu->set_size(Point2(1,1));
  1178. static const char *interp_name[3]={"Nearest","Linear","Cubic"};
  1179. for(int i=0;i<3;i++) {
  1180. track_menu->add_icon_item(interp_icon[i],interp_name[i]);
  1181. }
  1182. int lofs = remove_icon->get_width() + move_up_icon->get_width() + move_down_icon->get_width() + down_icon->get_width() *2 + hsep*7;//interp_icon[0]->get_width() + cont_icon[0]->get_width() ;
  1183. int popup_y = ofs.y+((int(mpos.y)/h)+2)*h;
  1184. int popup_x = ofs.x+size.width-lofs;
  1185. track_menu->set_pos(te->get_global_pos()+Point2(popup_x,popup_y));
  1186. interp_editing=idx;
  1187. cont_editing=-1;
  1188. track_menu->popup();
  1189. return;
  1190. }
  1191. ofsx-=hsep*2+interp_icon[0]->get_width()+down_icon->get_width();
  1192. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width()) {
  1193. track_menu->clear();
  1194. track_menu->set_size(Point2(1,1));
  1195. static const char *cont_name[3]={"Discrete","Continuous"};
  1196. for(int i=0;i<2;i++) {
  1197. track_menu->add_icon_item(cont_icon[i],cont_name[i]);
  1198. }
  1199. int lofs = settings_limit;
  1200. int popup_y = ofs.y+((int(mpos.y)/h)+2)*h;
  1201. int popup_x = ofs.x+lofs;
  1202. track_menu->set_pos(te->get_global_pos()+Point2(popup_x,popup_y));
  1203. interp_editing=-1;
  1204. cont_editing=idx;
  1205. track_menu->popup();
  1206. return;
  1207. }
  1208. ofsx-=hsep*3+cont_icon[0]->get_width()+down_icon->get_width();
  1209. if (ofsx < add_key_icon->get_width()) {
  1210. Animation::TrackType tt = animation->track_get_type(idx);
  1211. float pos = timeline_pos;
  1212. int existing = animation->track_find_key(idx,pos,true);
  1213. Variant newval;
  1214. if (tt==Animation::TYPE_TRANSFORM) {
  1215. Dictionary d;
  1216. d["loc"]=Vector3();
  1217. d["rot"]=Quat();
  1218. d["scale"]=Vector3();
  1219. newval=d;
  1220. } else if (tt==Animation::TYPE_METHOD) {
  1221. Dictionary d;
  1222. d["method"]="";
  1223. d["args"]=Vector<Variant>();
  1224. newval=d;
  1225. } else if (tt==Animation::TYPE_VALUE) {
  1226. PropertyInfo inf = _find_hint_for_track(idx);
  1227. if (inf.type!=Variant::NIL) {
  1228. Variant::CallError err;
  1229. newval=Variant::construct(inf.type,NULL,0,err);
  1230. }
  1231. if (newval.get_type()==Variant::NIL) {
  1232. //popup a new type
  1233. cvi_track=idx;
  1234. cvi_pos=pos;
  1235. type_menu->set_pos( get_global_pos() + mpos +ofs );
  1236. type_menu->popup();
  1237. return;
  1238. }
  1239. }
  1240. undo_redo->create_action("Anim Add Key");
  1241. undo_redo->add_do_method(animation.ptr(),"track_insert_key",idx,pos,newval,1);
  1242. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",idx,pos);
  1243. if (existing!=-1) {
  1244. Variant v = animation->track_get_key_value(idx,existing);
  1245. float trans = animation->track_get_key_transition(idx,existing);
  1246. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,pos,v,trans);
  1247. }
  1248. undo_redo->commit_action();
  1249. return;
  1250. }
  1251. }
  1252. } else {
  1253. switch(click.click) {
  1254. case ClickOver::CLICK_SELECT_KEYS: {
  1255. float zoom_scale=_get_zoom_scale();
  1256. float keys_from = h_scroll->get_val();
  1257. float keys_to = keys_from + (settings_limit-name_limit) / zoom_scale;
  1258. float from_time = keys_from + ( click.at.x - (name_limit+ofs.x)) / zoom_scale;
  1259. float to_time = keys_from + (click.to.x - (name_limit+ofs.x)) / zoom_scale;
  1260. if (to_time < from_time)
  1261. SWAP(from_time,to_time);
  1262. if (from_time > keys_to || to_time < keys_from)
  1263. break;
  1264. if (from_time < keys_from)
  1265. from_time = keys_from;
  1266. if (to_time >= keys_to)
  1267. to_time = keys_to;
  1268. int from_track = int(click.at.y-ofs.y-h-sep) / h + v_scroll->get_val();
  1269. int to_track = int(click.to.y-ofs.y-h-sep) / h + v_scroll->get_val();
  1270. int from_mod = int(click.at.y-ofs.y-sep) % h;
  1271. int to_mod = int(click.to.y-ofs.y-sep) % h;
  1272. if (to_track < from_track) {
  1273. SWAP(from_track,to_track);
  1274. SWAP(from_mod,to_mod);
  1275. }
  1276. if ((from_mod > (h/2)) && ((click.at.y-ofs.y)>=(h+sep))) {
  1277. from_track++;
  1278. }
  1279. if (to_mod < h/2) {
  1280. to_track--;
  1281. }
  1282. if (from_track>to_track) {
  1283. if (!click.shift)
  1284. selection.clear();
  1285. break;
  1286. }
  1287. int tracks_from = v_scroll->get_val();
  1288. int tracks_to = v_scroll->get_val()+fit-1;
  1289. if (tracks_to>=animation->get_track_count())
  1290. tracks_to=animation->get_track_count()-1;
  1291. tracks_from=0;
  1292. tracks_to=animation->get_track_count()-1;
  1293. if (to_track >tracks_to)
  1294. to_track = tracks_to;
  1295. if (from_track<tracks_from)
  1296. from_track=tracks_from;
  1297. if (from_track > tracks_to || to_track < tracks_from) {
  1298. if (!click.shift)
  1299. selection.clear();
  1300. break;
  1301. }
  1302. if (!click.shift)
  1303. selection.clear();
  1304. int higher_track=0x7FFFFFFF;
  1305. for(int i=from_track;i<=to_track;i++) {
  1306. int kc=animation->track_get_key_count(i);
  1307. for(int j=0;j<kc;j++) {
  1308. float t = animation->track_get_key_time(i,j);
  1309. if (t<from_time)
  1310. continue;
  1311. if (t>to_time)
  1312. break;
  1313. if (i<higher_track)
  1314. higher_track=i;
  1315. SelectedKey sk;
  1316. sk.track=i;
  1317. sk.key=j;
  1318. KeyInfo ki;
  1319. ki.pos=t;
  1320. selection[sk]=ki;
  1321. }
  1322. }
  1323. if (higher_track!=0x7FFFFFFF) {
  1324. selected_track=higher_track;
  1325. track_editor->update();
  1326. }
  1327. } break;
  1328. case ClickOver::CLICK_MOVE_KEYS: {
  1329. if (selection.empty())
  1330. break;
  1331. if (click.at==click.to) {
  1332. if (!click.shift) {
  1333. KeyInfo ki=selection[click.selk];
  1334. selection.clear();
  1335. selection[click.selk]=ki;
  1336. }
  1337. break;
  1338. }
  1339. float from_t = 1e20;
  1340. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  1341. float t = animation->track_get_key_time(E->key().track,E->key().key);
  1342. if (t<from_t)
  1343. from_t=t;
  1344. }
  1345. float motion = from_t+(click.to.x - click.at.x)/_get_zoom_scale();
  1346. if (step->get_val())
  1347. motion = Math::stepify(motion,step->get_val());
  1348. undo_redo->create_action("Anim Move Keys");
  1349. List<_AnimMoveRestore> to_restore;
  1350. // 1-remove the keys
  1351. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1352. undo_redo->add_do_method(animation.ptr(),"track_remove_key",E->key().track,E->key().key);
  1353. }
  1354. // 2- remove overlapped keys
  1355. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1356. float newtime = E->get().pos-from_t+motion;
  1357. int idx = animation->track_find_key(E->key().track,newtime,true);
  1358. if (idx==-1)
  1359. continue;
  1360. SelectedKey sk;
  1361. sk.key=idx;
  1362. sk.track=E->key().track;
  1363. if (selection.has(sk))
  1364. continue; //already in selection, don't save
  1365. undo_redo->add_do_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newtime);
  1366. _AnimMoveRestore amr;
  1367. amr.key=animation->track_get_key_value(E->key().track,idx);
  1368. amr.track=E->key().track;
  1369. amr.time=newtime;
  1370. amr.transition=animation->track_get_key_transition(E->key().track,idx);
  1371. to_restore.push_back(amr);
  1372. }
  1373. // 3-move the keys (re insert them)
  1374. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1375. float newpos=E->get().pos-from_t+motion;
  1376. //if (newpos<0)
  1377. // continue; //no add at the begining
  1378. 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));
  1379. }
  1380. // 4-(undo) remove inserted keys
  1381. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1382. float newpos=E->get().pos+-from_t+motion;
  1383. //if (newpos<0)
  1384. // continue; //no remove what no inserted
  1385. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newpos);
  1386. }
  1387. // 5-(undo) reinsert keys
  1388. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1389. 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));
  1390. }
  1391. // 6-(undo) reinsert overlapped keys
  1392. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  1393. _AnimMoveRestore &amr = E->get();
  1394. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  1395. }
  1396. // 6-(undo) reinsert overlapped keys
  1397. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  1398. _AnimMoveRestore &amr = E->get();
  1399. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  1400. }
  1401. undo_redo->add_do_method(this,"_clear_selection_for_anim",animation);
  1402. undo_redo->add_undo_method(this,"_clear_selection_for_anim",animation);
  1403. // 7-reselect
  1404. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1405. float oldpos=E->get().pos;
  1406. float newpos=oldpos-from_t+motion;
  1407. //if (newpos>=0)
  1408. undo_redo->add_do_method(this,"_select_at_anim",animation,E->key().track,newpos);
  1409. undo_redo->add_undo_method(this,"_select_at_anim",animation,E->key().track,oldpos);
  1410. }
  1411. undo_redo->commit_action();
  1412. } break;
  1413. default: {}
  1414. }
  1415. //button released
  1416. click.click=ClickOver::CLICK_NONE;
  1417. track_editor->update();
  1418. }
  1419. }
  1420. } break;
  1421. case InputEvent::MOUSE_MOTION: {
  1422. const InputEventMouseMotion &mb = p_input.mouse_motion;
  1423. mouse_over.over=MouseOver::OVER_NONE;
  1424. mouse_over.track=-1;
  1425. te->update();
  1426. track_editor->set_tooltip("");
  1427. if (!track_editor->has_focus() && (!get_focus_owner() || !get_focus_owner()->cast_to<LineEdit>()))
  1428. track_editor->call_deferred("grab_focus");
  1429. if (click.click!=ClickOver::CLICK_NONE) {
  1430. switch(click.click) {
  1431. case ClickOver::CLICK_RESIZE_NAMES: {
  1432. float base = click.at.y;
  1433. float clickp = click.at.x-ofs.x;
  1434. float dif = base - clickp;
  1435. float target = mb.x+dif-ofs.x;
  1436. float ratio = target / settings_limit;
  1437. if (ratio>0.9)
  1438. ratio=0.9;
  1439. else if (ratio<0.2)
  1440. ratio=0.2;
  1441. name_column_ratio=ratio;
  1442. } break;
  1443. case ClickOver::CLICK_DRAG_TIMELINE: {
  1444. Point2 mpos = Point2(mb.x,mb.y)-ofs;
  1445. /*
  1446. if (mpos.x<name_limit)
  1447. mpos.x=name_limit;
  1448. if (mpos.x>settings_limit)
  1449. mpos.x=settings_limit;
  1450. */
  1451. //int zoomw = settings_limit-name_limit;
  1452. float scale = _get_zoom_scale();
  1453. float pos = h_scroll->get_val() + (mpos.x-name_limit) / scale;
  1454. if (animation->get_step()) {
  1455. pos=Math::stepify(pos,animation->get_step());
  1456. }
  1457. if (pos<0)
  1458. pos=0;
  1459. if (pos>=animation->get_length())
  1460. pos=animation->get_length();
  1461. if (pos < h_scroll->get_val()) {
  1462. h_scroll->set_val(pos);
  1463. } else if (pos > h_scroll->get_val() + (settings_limit - name_limit) / scale) {
  1464. h_scroll->set_val( pos - (settings_limit - name_limit) / scale );
  1465. }
  1466. timeline_pos=pos;
  1467. emit_signal("timeline_changed",pos);
  1468. } break;
  1469. case ClickOver::CLICK_SELECT_KEYS: {
  1470. click.to=Point2(mb.x,mb.y);
  1471. if (click.to.y<h && click.at.y>h && mb.relative_y<0) {
  1472. float prev = v_scroll->get_val();
  1473. v_scroll->set_val( v_scroll->get_val() -1 );
  1474. if (prev!=v_scroll->get_val())
  1475. click.at.y+=h;
  1476. }
  1477. if (click.to.y>size.height && click.at.y<size.height && mb.relative_y>0) {
  1478. float prev = v_scroll->get_val();
  1479. v_scroll->set_val( v_scroll->get_val() +1 );
  1480. if (prev!=v_scroll->get_val())
  1481. click.at.y-=h;
  1482. }
  1483. } break;
  1484. case ClickOver::CLICK_MOVE_KEYS: {
  1485. click.to=Point2(mb.x,mb.y);
  1486. } break;
  1487. default: {}
  1488. }
  1489. return;
  1490. } else if (mb.button_mask&BUTTON_MASK_MIDDLE) {
  1491. int rel = mb.relative_x;
  1492. float relf = rel / _get_zoom_scale();
  1493. h_scroll->set_val( h_scroll->get_val() - relf );
  1494. }
  1495. if (mb.button_mask==0) {
  1496. Point2 mpos = Point2(mb.x,mb.y)-ofs;
  1497. if (mpos.y < h ) {
  1498. #if 0
  1499. //seek
  1500. //int zoomw = settings_limit-name_limit;
  1501. float scale = _get_zoom_scale();
  1502. float pos = h_scroll->get_val() + (mpos.y-name_limit) / scale;
  1503. if (pos<0 )
  1504. pos=0;
  1505. if (pos>=animation->get_length())
  1506. pos=animation->get_length();
  1507. timeline->set_val(pos);
  1508. #endif
  1509. return;
  1510. }
  1511. mpos.y -= h;
  1512. int idx = mpos.y / h;
  1513. idx+=v_scroll->get_val();
  1514. if (idx <0 || idx>=animation->get_track_count())
  1515. break;
  1516. mouse_over.track=idx;
  1517. if (mpos.x < name_limit) {
  1518. //name column
  1519. mouse_over.over=MouseOver::OVER_NAME;
  1520. } else if (mpos.x < settings_limit) {
  1521. float pos = mpos.x - name_limit;
  1522. pos/=_get_zoom_scale();
  1523. pos+=h_scroll->get_val();
  1524. float w_time = (type_icon[0]->get_width() / _get_zoom_scale())/2.0;
  1525. int kidx = animation->track_find_key(idx,pos);
  1526. int kidx_n = kidx+1;
  1527. bool found = false;
  1528. if (kidx>=0 && kidx<animation->track_get_key_count(idx)) {
  1529. float kpos = animation->track_get_key_time(idx,kidx);
  1530. if (ABS(pos-kpos)<=w_time) {
  1531. mouse_over.over=MouseOver::OVER_KEY;
  1532. mouse_over.track=idx;
  1533. mouse_over.over_key=kidx;
  1534. found=true;
  1535. }
  1536. }
  1537. if (!found && kidx_n>=0 && kidx_n<animation->track_get_key_count(idx)) {
  1538. float kpos = animation->track_get_key_time(idx,kidx_n);
  1539. if (ABS(pos-kpos)<=w_time) {
  1540. mouse_over.over=MouseOver::OVER_KEY;
  1541. mouse_over.track=idx;
  1542. mouse_over.over_key=kidx_n;
  1543. found=true;
  1544. }
  1545. }
  1546. if (found) {
  1547. String text;
  1548. text="time: "+rtos(animation->track_get_key_time(idx,mouse_over.over_key))+"\n";
  1549. switch(animation->track_get_type(idx)) {
  1550. case Animation::TYPE_TRANSFORM: {
  1551. Dictionary d = animation->track_get_key_value(idx,mouse_over.over_key);
  1552. if (d.has("loc"))
  1553. text+="loc: "+String(d["loc"])+"\n";
  1554. if (d.has("rot"))
  1555. text+="rot: "+String(d["rot"])+"\n";
  1556. if (d.has("scale"))
  1557. text+="scale: "+String(d["scale"])+"\n";
  1558. } break;
  1559. case Animation::TYPE_VALUE: {
  1560. Variant v = animation->track_get_key_value(idx,mouse_over.over_key);
  1561. text+="value: "+String(v)+"\n";
  1562. } break;
  1563. case Animation::TYPE_METHOD: {
  1564. Dictionary d = animation->track_get_key_value(idx,mouse_over.over_key);
  1565. if (d.has("method"))
  1566. text+=String(d["method"]);
  1567. text+="(";
  1568. Vector<Variant> args;
  1569. if (d.has("args"))
  1570. args=d["args"];
  1571. for(int i=0;i<args.size();i++) {
  1572. if (i>0)
  1573. text+=", ";
  1574. text+=String(args[i]);
  1575. }
  1576. text+=")\n";
  1577. } break;
  1578. }
  1579. text+="easing: "+rtos(animation->track_get_key_transition(idx,mouse_over.over_key));
  1580. track_editor->set_tooltip(text);
  1581. return;
  1582. }
  1583. } else {
  1584. //button column
  1585. int ofsx = size.width - mpos.x;
  1586. if (ofsx < 0)
  1587. break;
  1588. /*
  1589. if (ofsx < remove_icon->get_width()) {
  1590. mouse_over.over=MouseOver::OVER_REMOVE;
  1591. return;
  1592. }
  1593. ofsx-=hsep+remove_icon->get_width();
  1594. if (ofsx < move_down_icon->get_width()) {
  1595. mouse_over.over=MouseOver::OVER_DOWN;
  1596. return;
  1597. }
  1598. ofsx-=hsep+move_down_icon->get_width();
  1599. if (ofsx < move_up_icon->get_width()) {
  1600. mouse_over.over=MouseOver::OVER_UP;
  1601. return;
  1602. }
  1603. ofsx-=hsep*3+move_up_icon->get_width();
  1604. */
  1605. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep*2) {
  1606. mouse_over.over=MouseOver::OVER_INTERP;
  1607. return;
  1608. }
  1609. ofsx-=hsep*2+interp_icon[0]->get_width() + down_icon->get_width();
  1610. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width() +hsep*3) {
  1611. mouse_over.over=MouseOver::OVER_VALUE;
  1612. return;
  1613. }
  1614. ofsx-=hsep*3+cont_icon[0]->get_width() + down_icon->get_width();
  1615. if (ofsx < add_key_icon->get_width()) {
  1616. mouse_over.over=MouseOver::OVER_ADD_KEY;
  1617. return;
  1618. }
  1619. }
  1620. }
  1621. } break;
  1622. }
  1623. }
  1624. void AnimationKeyEditor::_notification(int p_what) {
  1625. switch(p_what) {
  1626. case NOTIFICATION_ENTER_TREE: {
  1627. zoomicon->set_texture( get_icon("Zoom","EditorIcons") );
  1628. //menu_track->set_icon(get_icon("AddTrack","EditorIcons"));
  1629. menu_track->get_popup()->add_icon_item(get_icon("KeyValue","EditorIcons"),"Add Normal Track",TRACK_MENU_ADD_VALUE_TRACK);
  1630. menu_track->get_popup()->add_icon_item(get_icon("KeyXform","EditorIcons"),"Add Transform Track",TRACK_MENU_ADD_TRANSFORM_TRACK);
  1631. menu_track->get_popup()->add_icon_item(get_icon("KeyCall","EditorIcons"),"Add Call Func Track",TRACK_MENU_ADD_CALL_TRACK);
  1632. menu_track->get_popup()->add_separator();
  1633. menu_track->get_popup()->add_item("Scale Selection",TRACK_MENU_SCALE);
  1634. menu_track->get_popup()->add_item("Scale From Cursor",TRACK_MENU_SCALE_PIVOT);
  1635. menu_track->get_popup()->add_separator();
  1636. menu_track->get_popup()->add_item("Duplicate Selection",TRACK_MENU_DUPLICATE);
  1637. menu_track->get_popup()->add_item("Duplicate Transposed",TRACK_MENU_DUPLICATE_TRANSPOSE);
  1638. menu_track->get_popup()->add_separator();
  1639. menu_track->get_popup()->add_item("Goto Next Step",TRACK_MENU_NEXT_STEP,KEY_MASK_CMD|KEY_RIGHT);
  1640. menu_track->get_popup()->add_item("Goto Prev Step",TRACK_MENU_PREV_STEP,KEY_MASK_CMD|KEY_LEFT);
  1641. menu_track->get_popup()->add_separator();
  1642. PopupMenu *tpp = memnew( PopupMenu );
  1643. tpp->add_item("Linear",TRACK_MENU_SET_ALL_TRANS_LINEAR);
  1644. tpp->add_item("Constant",TRACK_MENU_SET_ALL_TRANS_CONSTANT);
  1645. tpp->add_item("In",TRACK_MENU_SET_ALL_TRANS_IN);
  1646. tpp->add_item("Out",TRACK_MENU_SET_ALL_TRANS_OUT);
  1647. tpp->add_item("In-Out",TRACK_MENU_SET_ALL_TRANS_INOUT);
  1648. tpp->add_item("Out-In",TRACK_MENU_SET_ALL_TRANS_OUTIN);
  1649. tpp->set_name("Transitions");
  1650. tpp->connect("item_pressed",this,"_menu_track");
  1651. optimize_dialog->connect("confirmed",this,"_animation_optimize");
  1652. menu_track->get_popup()->add_child(tpp);
  1653. menu_track->get_popup()->add_submenu_item("Set Transitions..","Transitions");
  1654. menu_track->get_popup()->add_separator();
  1655. menu_track->get_popup()->add_item("Optimize Animation",TRACK_MENU_OPTIMIZE);
  1656. move_up_button->set_icon(get_icon("MoveUp","EditorIcons"));
  1657. move_down_button->set_icon(get_icon("MoveDown","EditorIcons"));
  1658. remove_button->set_icon(get_icon("Remove","EditorIcons"));
  1659. edit_button->set_icon(get_icon("EditKey","EditorIcons"));
  1660. loop->set_icon(get_icon("Loop","EditorIcons"));
  1661. //edit_button->add_color_override("font_color",get_color("font_color","Tree"));
  1662. //edit_button->add_color_override("font_color_hover",get_color("font_color","Tree"));
  1663. {
  1664. right_data_size_cache=0;
  1665. int hsep = get_constant("hseparation","Tree");
  1666. Ref<Texture> remove_icon = get_icon("Remove","EditorIcons");
  1667. Ref<Texture> move_up_icon = get_icon("MoveUp","EditorIcons");
  1668. Ref<Texture> move_down_icon = get_icon("MoveDown","EditorIcons");
  1669. Ref<Texture> down_icon = get_icon("select_arrow","Tree");
  1670. Ref<Texture> add_key_icon = get_icon("TrackAddKey","EditorIcons");
  1671. Ref<Texture> interp_icon[3]={
  1672. get_icon("InterpRaw","EditorIcons"),
  1673. get_icon("InterpLinear","EditorIcons"),
  1674. get_icon("InterpCubic","EditorIcons")
  1675. };
  1676. Ref<Texture> cont_icon[3]={
  1677. get_icon("TrackDiscrete","EditorIcons"),
  1678. get_icon("TrackContinuous","EditorIcons")
  1679. };
  1680. //right_data_size_cache = remove_icon->get_width() + move_up_icon->get_width() + move_down_icon->get_width() + down_icon->get_width() *2 + interp_icon[0]->get_width() + cont_icon[0]->get_width() + add_key_icon->get_width() + hsep*11;
  1681. right_data_size_cache = down_icon->get_width() *2 + add_key_icon->get_width() + interp_icon[0]->get_width() + cont_icon[0]->get_width() + hsep*7;
  1682. }
  1683. // rename_anim->set_icon( get_icon("Rename","EditorIcons") );
  1684. /*
  1685. edit_anim->set_icon( get_icon("Edit","EditorIcons") );
  1686. blend_anim->set_icon( get_icon("Blend","EditorIcons") );
  1687. play->set_icon( get_icon("Play","EditorIcons") );
  1688. stop->set_icon( get_icon("Stop","EditorIcons") );
  1689. pause->set_icon( get_icon("Pause","EditorIcons") );
  1690. */
  1691. // menu->set_icon(get_icon("Animation","EditorIcons"));
  1692. // play->set_icon(get_icon("AnimationPlay","EditorIcons"));
  1693. //menu->set_icon(get_icon("Animation","EditorIcons"));
  1694. _update_menu();
  1695. } break;
  1696. }
  1697. }
  1698. void AnimationKeyEditor::_scroll_changed(double) {
  1699. if (te_drawing)
  1700. return;
  1701. track_editor->update();
  1702. }
  1703. void AnimationKeyEditor::_update_paths() {
  1704. if (animation.is_valid()) {
  1705. //timeline->set_max(animation->get_length());
  1706. //timeline->set_step(0.01);
  1707. track_editor->update();
  1708. length->set_val(animation->get_length());
  1709. step->set_val(animation->get_step());
  1710. }
  1711. }
  1712. void AnimationKeyEditor::_root_removed() {
  1713. root=NULL;
  1714. }
  1715. void AnimationKeyEditor::_update_menu() {
  1716. updating=true;
  1717. bool empty= !animation.is_valid();
  1718. if (animation.is_valid()) {
  1719. length->set_val(animation->get_length());
  1720. loop->set_pressed(animation->has_loop());
  1721. step->set_val(animation->get_step());
  1722. }
  1723. track_editor->update();
  1724. updating=false;
  1725. }
  1726. void AnimationKeyEditor::set_animation(const Ref<Animation>& p_anim) {
  1727. if (animation.is_valid())
  1728. animation->disconnect("changed",this,"_update_paths");
  1729. animation=p_anim;
  1730. if (animation.is_valid())
  1731. animation->connect("changed",this,"_update_paths");
  1732. timeline_pos=0;
  1733. selection.clear();
  1734. _update_paths();
  1735. _update_menu();
  1736. selected_track=-1;
  1737. }
  1738. void AnimationKeyEditor::set_root(Node *p_root) {
  1739. if (root)
  1740. root->disconnect("exit_tree",this,"_root_removed");
  1741. root=p_root;
  1742. if (root)
  1743. root->connect("exit_tree",this,"_root_removed",make_binds(),CONNECT_ONESHOT);
  1744. }
  1745. Node *AnimationKeyEditor::get_root() const {
  1746. return root;
  1747. }
  1748. void AnimationKeyEditor::set_keying(bool p_enabled) {
  1749. keying=p_enabled;
  1750. _update_menu();
  1751. emit_signal("keying_changed",p_enabled);
  1752. }
  1753. bool AnimationKeyEditor::has_keying() const {
  1754. return keying;
  1755. }
  1756. void AnimationKeyEditor::_query_insert(const InsertData& p_id) {
  1757. if (insert_frame!=OS::get_singleton()->get_frames_drawn()) {
  1758. //clear insert list for the frame if frame changed
  1759. if (insert_confirm->is_visible())
  1760. return; //do nothing
  1761. insert_data.clear();
  1762. insert_query=false;
  1763. }
  1764. insert_frame=OS::get_singleton()->get_frames_drawn();
  1765. for (List<InsertData>::Element *E=insert_data.front();E;E=E->next()) {
  1766. //prevent insertion of multiple tracks
  1767. if (E->get().path==p_id.path)
  1768. return; //already inserted a track for this on this frame
  1769. }
  1770. insert_data.push_back(p_id);
  1771. if (p_id.track_idx==-1) {
  1772. if (bool(EDITOR_DEF("animation/confirm_insert_track",true))) {
  1773. //potential new key, does not exist
  1774. if (insert_data.size()==1)
  1775. insert_confirm->set_text("Create NEW track for "+p_id.query+" and insert key?");
  1776. else
  1777. insert_confirm->set_text("Create "+itos(insert_data.size())+" NEW tracks and insert keys?");
  1778. insert_confirm->get_ok()->set_text("Create");
  1779. insert_confirm->popup_centered_minsize();
  1780. insert_query=true;
  1781. } else {
  1782. call_deferred("_insert_delay");
  1783. insert_queue=true;
  1784. }
  1785. } else {
  1786. if (!insert_query && !insert_queue) {
  1787. call_deferred("_insert_delay");
  1788. insert_queue=true;
  1789. }
  1790. }
  1791. }
  1792. void AnimationKeyEditor::insert_transform_key(Spatial *p_node,const String& p_sub,const Transform& p_xform) {
  1793. if (!keying)
  1794. return;
  1795. if (!animation.is_valid())
  1796. return;
  1797. ERR_FAIL_COND(!root);
  1798. //let's build a node path
  1799. String path = root->get_path_to(p_node);
  1800. if (p_sub!="")
  1801. path+=":"+p_sub;
  1802. NodePath np=path;
  1803. int track_idx=-1;
  1804. for(int i=0;i<animation->get_track_count();i++) {
  1805. if (animation->track_get_type(i)!=Animation::TYPE_TRANSFORM)
  1806. continue;
  1807. if (animation->track_get_path(i)!=np)
  1808. continue;
  1809. track_idx=i;
  1810. break;
  1811. }
  1812. InsertData id;
  1813. Dictionary val;
  1814. id.path=np;
  1815. id.track_idx=track_idx;
  1816. id.value=p_xform;
  1817. id.type=Animation::TYPE_TRANSFORM;
  1818. id.query="node '"+p_node->get_name()+"'";
  1819. //dialog insert
  1820. _query_insert(id);
  1821. }
  1822. void AnimationKeyEditor::insert_node_value_key(Node* p_node, const String& p_property,const Variant& p_value,bool p_only_if_exists) {
  1823. ERR_FAIL_COND(!root);
  1824. //let's build a node path
  1825. Node *node = p_node;
  1826. String path = root->get_path_to(node);
  1827. for(int i=1;i<history->get_path_size();i++) {
  1828. String prop = history->get_path_property(i);
  1829. ERR_FAIL_COND(prop=="");
  1830. path+=":"+prop;
  1831. }
  1832. path+=":"+p_property;
  1833. NodePath np = path;
  1834. //locate track
  1835. int track_idx=-1;
  1836. for(int i=0;i<animation->get_track_count();i++) {
  1837. if (animation->track_get_type(i)!=Animation::TYPE_VALUE)
  1838. continue;
  1839. if (animation->track_get_path(i)!=np)
  1840. continue;
  1841. track_idx=i;
  1842. break;
  1843. }
  1844. if (p_only_if_exists && track_idx==-1)
  1845. return;
  1846. InsertData id;
  1847. id.path=np;
  1848. id.track_idx=track_idx;
  1849. id.value=p_value;
  1850. id.type=Animation::TYPE_VALUE;
  1851. id.query="property '"+p_property+"'";
  1852. //dialog insert
  1853. _query_insert(id);
  1854. }
  1855. void AnimationKeyEditor::insert_value_key(const String& p_property,const Variant& p_value) {
  1856. ERR_FAIL_COND(!root);
  1857. //let's build a node path
  1858. ERR_FAIL_COND(history->get_path_size()==0);
  1859. Object *obj = ObjectDB::get_instance(history->get_path_object(0));
  1860. ERR_FAIL_COND(!obj || !obj->cast_to<Node>());
  1861. Node *node = obj->cast_to<Node>();
  1862. String path = root->get_path_to(node);
  1863. for(int i=1;i<history->get_path_size();i++) {
  1864. String prop = history->get_path_property(i);
  1865. ERR_FAIL_COND(prop=="");
  1866. path+=":"+prop;
  1867. }
  1868. path+=":"+p_property;
  1869. NodePath np = path;
  1870. //locate track
  1871. int track_idx=-1;
  1872. for(int i=0;i<animation->get_track_count();i++) {
  1873. if (animation->track_get_type(i)!=Animation::TYPE_VALUE)
  1874. continue;
  1875. if (animation->track_get_path(i)!=np)
  1876. continue;
  1877. track_idx=i;
  1878. break;
  1879. }
  1880. InsertData id;
  1881. id.path=np;
  1882. id.track_idx=track_idx;
  1883. id.value=p_value;
  1884. id.type=Animation::TYPE_VALUE;
  1885. id.query="property '"+p_property+"'";
  1886. //dialog insert
  1887. _query_insert(id);
  1888. }
  1889. void AnimationKeyEditor::_confirm_insert_list() {
  1890. undo_redo->create_action("Anim Create & Insert");
  1891. int last_track = animation->get_track_count();
  1892. while(insert_data.size()) {
  1893. last_track=_confirm_insert(insert_data.front()->get(),last_track);
  1894. insert_data.pop_front();
  1895. }
  1896. undo_redo->commit_action();
  1897. }
  1898. int AnimationKeyEditor::_confirm_insert(InsertData p_id,int p_last_track) {
  1899. if (p_last_track==-1)
  1900. p_last_track=animation->get_track_count();
  1901. bool created=false;
  1902. if (p_id.track_idx<0) {
  1903. created=true;
  1904. undo_redo->create_action("Anim Insert Track & Key");
  1905. bool continuous=false;
  1906. if (p_id.type==Animation::TYPE_VALUE) {
  1907. //wants a new tack
  1908. {
  1909. //shitty hack
  1910. animation->add_track(p_id.type);
  1911. animation->track_set_path(animation->get_track_count()-1,p_id.path);
  1912. PropertyInfo h = _find_hint_for_track(animation->get_track_count()-1);
  1913. animation->remove_track(animation->get_track_count()-1); //hack
  1914. continuous =
  1915. h.type==Variant::REAL ||
  1916. h.type==Variant::VECTOR2 ||
  1917. h.type==Variant::RECT2 ||
  1918. h.type==Variant::VECTOR3 ||
  1919. h.type==Variant::_AABB ||
  1920. h.type==Variant::QUAT ||
  1921. h.type==Variant::COLOR ||
  1922. h.type==Variant::TRANSFORM ;
  1923. }
  1924. }
  1925. p_id.track_idx=p_last_track;
  1926. undo_redo->add_do_method(animation.ptr(),"add_track",p_id.type);
  1927. undo_redo->add_do_method(animation.ptr(),"track_set_path",p_id.track_idx,p_id.path);
  1928. if (p_id.type==Animation::TYPE_VALUE)
  1929. undo_redo->add_do_method(animation.ptr(),"value_track_set_continuous",p_id.track_idx,continuous);
  1930. } else {
  1931. undo_redo->create_action("Anim Insert Key");
  1932. }
  1933. float time = timeline_pos;
  1934. Variant value;
  1935. switch(p_id.type) {
  1936. case Animation::TYPE_VALUE: {
  1937. value = p_id.value;
  1938. } break;
  1939. case Animation::TYPE_TRANSFORM: {
  1940. Transform tr = p_id.value;
  1941. Dictionary d;
  1942. d["loc"]=tr.origin;
  1943. d["scale"]=tr.basis.get_scale();
  1944. d["rot"]=Quat(tr.basis);//.orthonormalized();
  1945. value=d;
  1946. } break;
  1947. default:{}
  1948. }
  1949. undo_redo->add_do_method(animation.ptr(),"track_insert_key",p_id.track_idx,time,value);
  1950. if (created) {
  1951. //just remove the track
  1952. undo_redo->add_undo_method(animation.ptr(),"remove_track",p_last_track);
  1953. p_last_track++;
  1954. } else {
  1955. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",p_id.track_idx,time);
  1956. int existing = animation->track_find_key(p_id.track_idx,time,true);
  1957. if (existing!=-1) {
  1958. Variant v = animation->track_get_key_value(p_id.track_idx,existing);
  1959. float trans = animation->track_get_key_transition(p_id.track_idx,existing);
  1960. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",p_id.track_idx,time,v,trans);
  1961. }
  1962. }
  1963. undo_redo->add_do_method(this,"update");
  1964. undo_redo->add_undo_method(this,"update");
  1965. undo_redo->add_do_method(track_editor,"update");
  1966. undo_redo->add_undo_method(track_editor,"update");
  1967. undo_redo->add_do_method(track_pos,"update");
  1968. undo_redo->add_undo_method(track_pos,"update");
  1969. undo_redo->commit_action();
  1970. return p_last_track;
  1971. }
  1972. Ref<Animation> AnimationKeyEditor::get_current_animation() const {
  1973. return animation;
  1974. }
  1975. void AnimationKeyEditor::_animation_len_changed(float p_len) {
  1976. if (updating)
  1977. return;
  1978. if (!animation.is_null()) {
  1979. undo_redo->create_action("Change Anim Len");
  1980. undo_redo->add_do_method(animation.ptr(),"set_length",p_len);
  1981. undo_redo->add_undo_method(animation.ptr(),"set_length",animation->get_length());
  1982. undo_redo->add_do_method(this,"_animation_len_update");
  1983. undo_redo->add_undo_method(this,"_animation_len_update");
  1984. undo_redo->commit_action();
  1985. }
  1986. }
  1987. void AnimationKeyEditor::_animation_len_update() {
  1988. if (!animation.is_null())
  1989. emit_signal(alc,animation->get_length());
  1990. }
  1991. void AnimationKeyEditor::_animation_changed() {
  1992. if (updating)
  1993. return;
  1994. _update_menu();
  1995. }
  1996. void AnimationKeyEditor::_animation_loop_changed() {
  1997. if (updating)
  1998. return;
  1999. if (!animation.is_null()) {
  2000. undo_redo->create_action("Change Anim Loop");
  2001. undo_redo->add_do_method(animation.ptr(),"set_loop",loop->is_pressed());
  2002. undo_redo->add_undo_method(animation.ptr(),"set_loop",!loop->is_pressed());
  2003. undo_redo->commit_action();
  2004. }
  2005. }
  2006. void AnimationKeyEditor::_create_value_item(int p_type) {
  2007. undo_redo->create_action("Anim Create Typed Value Key");
  2008. Variant::CallError ce;
  2009. Variant v = Variant::construct(Variant::Type(p_type),NULL,0,ce);
  2010. undo_redo->add_do_method(animation.ptr(),"track_insert_key",cvi_track,cvi_pos,v);
  2011. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",cvi_track,cvi_pos);
  2012. int existing = animation->track_find_key(cvi_track,cvi_pos,true);
  2013. if (existing!=-1) {
  2014. Variant v = animation->track_get_key_value(cvi_track,existing);
  2015. float trans = animation->track_get_key_transition(cvi_track,existing);
  2016. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",cvi_track,cvi_pos,v,trans);
  2017. }
  2018. undo_redo->commit_action();
  2019. }
  2020. void AnimationKeyEditor::set_anim_pos(float p_pos) {
  2021. if (animation.is_null())
  2022. return;
  2023. timeline_pos=p_pos;
  2024. update();
  2025. track_pos->update();
  2026. track_editor->update();
  2027. }
  2028. void AnimationKeyEditor::_pane_drag(const Point2& p_delta) {
  2029. Size2 ecs = ec->get_custom_minimum_size();
  2030. ecs.y-=p_delta.y;
  2031. if (ecs.y<100)
  2032. ecs.y=100;
  2033. ec->set_custom_minimum_size(ecs);;
  2034. }
  2035. void AnimationKeyEditor::_insert_delay() {
  2036. if (insert_query) {
  2037. //discard since it's entered into query mode
  2038. insert_queue=false;
  2039. return;
  2040. }
  2041. undo_redo->create_action("Anim Insert");
  2042. int last_track = animation->get_track_count();
  2043. while(insert_data.size()) {
  2044. last_track=_confirm_insert(insert_data.front()->get(),last_track);
  2045. insert_data.pop_front();
  2046. }
  2047. undo_redo->commit_action();
  2048. insert_queue=false;
  2049. }
  2050. void AnimationKeyEditor::_step_changed(float p_len) {
  2051. updating=true;
  2052. animation->set_step(p_len);
  2053. updating=false;
  2054. }
  2055. void AnimationKeyEditor::_scale() {
  2056. if (selection.empty())
  2057. return;
  2058. float from_t = 1e20;
  2059. float to_t = -1e20;
  2060. float len = -1e20;
  2061. float pivot=0;
  2062. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  2063. float t = animation->track_get_key_time(E->key().track,E->key().key);
  2064. if (t<from_t)
  2065. from_t=t;
  2066. if (t>to_t)
  2067. to_t=t;
  2068. }
  2069. len=to_t-from_t;
  2070. if (last_menu_track_opt==TRACK_MENU_SCALE_PIVOT) {
  2071. pivot = timeline_pos;
  2072. } else {
  2073. pivot=from_t;
  2074. }
  2075. float s = scale->get_val();
  2076. if (s==0) {
  2077. ERR_PRINT("Can't scale to 0");
  2078. }
  2079. undo_redo->create_action("Anim Scale Keys");
  2080. List<_AnimMoveRestore> to_restore;
  2081. // 1-remove the keys
  2082. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2083. undo_redo->add_do_method(animation.ptr(),"track_remove_key",E->key().track,E->key().key);
  2084. }
  2085. // 2- remove overlapped keys
  2086. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2087. float newtime = (E->get().pos-from_t)*s+from_t;
  2088. int idx = animation->track_find_key(E->key().track,newtime,true);
  2089. if (idx==-1)
  2090. continue;
  2091. SelectedKey sk;
  2092. sk.key=idx;
  2093. sk.track=E->key().track;
  2094. if (selection.has(sk))
  2095. continue; //already in selection, don't save
  2096. undo_redo->add_do_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newtime);
  2097. _AnimMoveRestore amr;
  2098. amr.key=animation->track_get_key_value(E->key().track,idx);
  2099. amr.track=E->key().track;
  2100. amr.time=newtime;
  2101. amr.transition=animation->track_get_key_transition(E->key().track,idx);
  2102. to_restore.push_back(amr);
  2103. }
  2104. #define _NEW_POS(m_ofs) (((s>0)?m_ofs:from_t+(len-(m_ofs-from_t)))-pivot)*ABS(s)+from_t
  2105. // 3-move the keys (re insert them)
  2106. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2107. float newpos=_NEW_POS(E->get().pos);
  2108. 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));
  2109. }
  2110. // 4-(undo) remove inserted keys
  2111. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2112. float newpos=_NEW_POS(E->get().pos);
  2113. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newpos);
  2114. }
  2115. // 5-(undo) reinsert keys
  2116. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2117. 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));
  2118. }
  2119. // 6-(undo) reinsert overlapped keys
  2120. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  2121. _AnimMoveRestore &amr = E->get();
  2122. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  2123. }
  2124. // 6-(undo) reinsert overlapped keys
  2125. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  2126. _AnimMoveRestore &amr = E->get();
  2127. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  2128. }
  2129. undo_redo->add_do_method(this,"_clear_selection_for_anim",animation);
  2130. undo_redo->add_undo_method(this,"_clear_selection_for_anim",animation);
  2131. // 7-reselect
  2132. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2133. float oldpos=E->get().pos;
  2134. float newpos=_NEW_POS(oldpos);
  2135. if (newpos>=0)
  2136. undo_redo->add_do_method(this,"_select_at_anim",animation,E->key().track,newpos);
  2137. undo_redo->add_undo_method(this,"_select_at_anim",animation,E->key().track,oldpos);
  2138. }
  2139. #undef _NEW_POS
  2140. undo_redo->commit_action();
  2141. }
  2142. void AnimationKeyEditor::cleanup() {
  2143. set_animation(Ref<Animation>());
  2144. }
  2145. void AnimationKeyEditor::_bind_methods() {
  2146. ObjectTypeDB::bind_method(_MD("_root_removed"),&AnimationKeyEditor::_root_removed);
  2147. ObjectTypeDB::bind_method(_MD("_scale"),&AnimationKeyEditor::_scale);
  2148. ObjectTypeDB::bind_method(_MD("set_root"),&AnimationKeyEditor::set_root);
  2149. // ObjectTypeDB::bind_method(_MD("_confirm_insert"),&AnimationKeyEditor::_confirm_insert);
  2150. ObjectTypeDB::bind_method(_MD("_confirm_insert_list"),&AnimationKeyEditor::_confirm_insert_list);
  2151. ObjectTypeDB::bind_method(_MD("_update_paths"),&AnimationKeyEditor::_update_paths);
  2152. ObjectTypeDB::bind_method(_MD("_track_editor_draw"),&AnimationKeyEditor::_track_editor_draw);
  2153. ObjectTypeDB::bind_method(_MD("_animation_changed"),&AnimationKeyEditor::_animation_changed);
  2154. ObjectTypeDB::bind_method(_MD("_scroll_changed"),&AnimationKeyEditor::_scroll_changed);
  2155. ObjectTypeDB::bind_method(_MD("_track_editor_input_event"),&AnimationKeyEditor::_track_editor_input_event);
  2156. ObjectTypeDB::bind_method(_MD("_track_name_changed"),&AnimationKeyEditor::_track_name_changed);
  2157. ObjectTypeDB::bind_method(_MD("_track_menu_selected"),&AnimationKeyEditor::_track_menu_selected);
  2158. ObjectTypeDB::bind_method(_MD("_menu_track"),&AnimationKeyEditor::_menu_track);
  2159. ObjectTypeDB::bind_method(_MD("_clear_selection_for_anim"),&AnimationKeyEditor::_clear_selection_for_anim);
  2160. ObjectTypeDB::bind_method(_MD("_select_at_anim"),&AnimationKeyEditor::_select_at_anim);
  2161. ObjectTypeDB::bind_method(_MD("_track_pos_draw"),&AnimationKeyEditor::_track_pos_draw);
  2162. ObjectTypeDB::bind_method(_MD("_insert_delay"),&AnimationKeyEditor::_insert_delay);
  2163. ObjectTypeDB::bind_method(_MD("_step_changed"),&AnimationKeyEditor::_step_changed);
  2164. ObjectTypeDB::bind_method(_MD("_animation_loop_changed"),&AnimationKeyEditor::_animation_loop_changed);
  2165. ObjectTypeDB::bind_method(_MD("_animation_len_changed"),&AnimationKeyEditor::_animation_len_changed);
  2166. ObjectTypeDB::bind_method(_MD("_create_value_item"),&AnimationKeyEditor::_create_value_item);
  2167. ObjectTypeDB::bind_method(_MD("_pane_drag"),&AnimationKeyEditor::_pane_drag);
  2168. ObjectTypeDB::bind_method(_MD("_animation_len_update"),&AnimationKeyEditor::_animation_len_update);
  2169. ObjectTypeDB::bind_method(_MD("set_animation"),&AnimationKeyEditor::set_animation);
  2170. ObjectTypeDB::bind_method(_MD("_animation_optimize"),&AnimationKeyEditor::_animation_optimize);
  2171. ADD_SIGNAL( MethodInfo("resource_selected", PropertyInfo( Variant::OBJECT, "res"),PropertyInfo( Variant::STRING, "prop") ) );
  2172. ADD_SIGNAL( MethodInfo("keying_changed" ) );
  2173. ADD_SIGNAL( MethodInfo("timeline_changed", PropertyInfo(Variant::REAL,"pos") ) );
  2174. ADD_SIGNAL( MethodInfo("animation_len_changed", PropertyInfo(Variant::REAL,"len") ) );
  2175. }
  2176. AnimationKeyEditor::AnimationKeyEditor(UndoRedo *p_undo_redo, EditorHistory *p_history,EditorSelection *p_selection) {
  2177. alc="animation_len_changed";
  2178. editor_selection=p_selection;
  2179. selected_track=-1;
  2180. updating=false;
  2181. te_drawing=false;
  2182. undo_redo=p_undo_redo;
  2183. history=p_history;
  2184. HBoxContainer *hb = memnew( HBoxContainer );
  2185. add_child(hb);
  2186. root=NULL;
  2187. //menu = memnew( MenuButton );
  2188. //menu->set_flat(true);
  2189. //menu->set_pos(Point2());
  2190. //add_child(menu);
  2191. menu_track = memnew( MenuButton );
  2192. menu_track->set_text("Tracks");
  2193. hb->add_child(menu_track);
  2194. menu_track->get_popup()->connect("item_pressed",this,"_menu_track");
  2195. hb->add_child( memnew( VSeparator ) );
  2196. zoomicon = memnew( TextureFrame );
  2197. hb->add_child(zoomicon);
  2198. zoomicon->set_tooltip("Animation zoom.");
  2199. zoom = memnew( HSlider );
  2200. //hb->add_child(zoom);
  2201. zoom->set_step(0.01);
  2202. zoom->set_min(0.0);
  2203. zoom->set_max(2.0);
  2204. zoom->set_val(1.0);
  2205. zoom->set_h_size_flags(SIZE_EXPAND_FILL);
  2206. zoom->set_stretch_ratio(2);
  2207. hb->add_child(zoom);
  2208. zoom->connect("value_changed",this,"_scroll_changed");
  2209. zoom->set_tooltip("Animation zoom.");
  2210. hb->add_child( memnew( VSeparator ) );
  2211. Label *l = memnew( Label );
  2212. l->set_text("Len(s):");
  2213. hb->add_child(l);
  2214. length = memnew( SpinBox );
  2215. length->set_min(0.01);
  2216. length->set_max(10000);
  2217. length->set_step(0.01);
  2218. length->set_h_size_flags(SIZE_EXPAND_FILL);
  2219. length->set_stretch_ratio(1);
  2220. length->set_tooltip("Animation length (in seconds).");
  2221. hb->add_child(length);
  2222. length->connect("value_changed",this,"_animation_len_changed");
  2223. l = memnew( Label );
  2224. l->set_text("Step(s):");
  2225. hb->add_child(l);
  2226. step = memnew( SpinBox );
  2227. step->set_min(0.00);
  2228. step->set_max(128);
  2229. step->set_step(0.01);
  2230. step->set_val(0.0);
  2231. step->set_h_size_flags(SIZE_EXPAND_FILL);
  2232. step->set_stretch_ratio(1);
  2233. step->set_tooltip("Cursor step snap (in seconds).");
  2234. hb->add_child(step);
  2235. step->connect("value_changed",this,"_step_changed");
  2236. loop = memnew( ToolButton );
  2237. loop->set_toggle_mode(true);
  2238. loop->connect("pressed",this,"_animation_loop_changed");
  2239. hb->add_child(loop);
  2240. loop->set_tooltip("Enable/Disable looping in animation.");
  2241. hb->add_child( memnew( VSeparator ) );
  2242. edit_button = memnew( ToolButton );
  2243. edit_button->set_toggle_mode(true);
  2244. edit_button->set_focus_mode(FOCUS_NONE);
  2245. edit_button->set_disabled(true);
  2246. hb->add_child(edit_button);
  2247. edit_button->set_tooltip("Enable editing of individual keys by clicking them.");
  2248. move_up_button = memnew( ToolButton );
  2249. hb->add_child(move_up_button);
  2250. move_up_button->connect("pressed",this,"_menu_track",make_binds(TRACK_MENU_MOVE_UP));
  2251. move_up_button->set_focus_mode(FOCUS_NONE);
  2252. move_up_button->set_disabled(true);
  2253. move_up_button->set_tooltip("Move current track up.");
  2254. move_down_button = memnew( ToolButton );
  2255. hb->add_child(move_down_button);
  2256. move_down_button->connect("pressed",this,"_menu_track",make_binds(TRACK_MENU_MOVE_DOWN));
  2257. move_down_button->set_focus_mode(FOCUS_NONE);
  2258. move_down_button->set_disabled(true);
  2259. move_down_button->set_tooltip("Move current track down.");
  2260. remove_button = memnew( ToolButton );
  2261. hb->add_child(remove_button);
  2262. remove_button->connect("pressed",this,"_menu_track",make_binds(TRACK_MENU_REMOVE));
  2263. remove_button->set_focus_mode(FOCUS_NONE);
  2264. remove_button->set_disabled(true);
  2265. remove_button->set_tooltip("Remove selected track.");
  2266. optimize_dialog = memnew( ConfirmationDialog );
  2267. add_child(optimize_dialog);
  2268. optimize_dialog->set_title("Anim. Optimizer");
  2269. VBoxContainer *optimize_vb = memnew( VBoxContainer );
  2270. optimize_dialog->add_child(optimize_vb);
  2271. optimize_dialog->set_child_rect(optimize_vb);
  2272. optimize_linear_error = memnew( SpinBox );
  2273. optimize_linear_error->set_max(1.0);
  2274. optimize_linear_error->set_min(0.001);
  2275. optimize_linear_error->set_step(0.001);
  2276. optimize_linear_error->set_val(0.05);
  2277. optimize_vb->add_margin_child("Max. Linear Error:",optimize_linear_error);
  2278. optimize_angular_error = memnew( SpinBox );
  2279. optimize_angular_error->set_max(1.0);
  2280. optimize_angular_error->set_min(0.001);
  2281. optimize_angular_error->set_step(0.001);
  2282. optimize_angular_error->set_val(0.01);
  2283. optimize_vb->add_margin_child("Max. Angular Error:",optimize_angular_error);
  2284. optimize_max_angle = memnew( SpinBox );
  2285. optimize_vb->add_margin_child("Max Optimizable Angle:",optimize_max_angle);
  2286. optimize_max_angle->set_max(360.0);
  2287. optimize_max_angle->set_min(0.0);
  2288. optimize_max_angle->set_step(0.1);
  2289. optimize_max_angle->set_val(22);
  2290. optimize_dialog->get_ok()->set_text("Optimize");
  2291. /*keying = memnew( Button );
  2292. keying->set_toggle_mode(true);
  2293. //keying->set_text("Keys");
  2294. keying->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,60);
  2295. keying->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
  2296. keying->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_BEGIN,55);
  2297. keying->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  2298. //add_child(keying);
  2299. keying->connect("pressed",this,"_keying_toggled");
  2300. */
  2301. /* l = memnew( Label );
  2302. l->set_text("Base: ");
  2303. l->set_pos(Point2(0,3));
  2304. // dr_panel->add_child(l);*/
  2305. // menu->get_popup()->connect("item_pressed",this,"_menu_callback");
  2306. ec = memnew (Control);
  2307. ec->set_custom_minimum_size(Size2(0,50));
  2308. add_child(ec);
  2309. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  2310. hb = memnew( HBoxContainer);
  2311. hb->set_area_as_parent_rect();
  2312. ec->add_child(hb);
  2313. hb->set_v_size_flags(SIZE_EXPAND_FILL);
  2314. track_editor = memnew( Control );
  2315. track_editor->connect("draw",this,"_track_editor_draw");
  2316. hb->add_child(track_editor);
  2317. track_editor->connect("input_event",this,"_track_editor_input_event");
  2318. track_editor->set_focus_mode(Control::FOCUS_ALL);
  2319. track_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2320. track_pos = memnew( Control );
  2321. track_pos->set_area_as_parent_rect();
  2322. track_pos->set_ignore_mouse(true);
  2323. track_editor->add_child(track_pos);
  2324. track_pos->connect("draw",this,"_track_pos_draw");
  2325. v_scroll = memnew( VScrollBar );
  2326. hb->add_child(v_scroll);
  2327. v_scroll->connect("value_changed",this,"_scroll_changed");
  2328. v_scroll->set_val(0);
  2329. h_scroll = memnew( HScrollBar );
  2330. h_scroll->connect("value_changed",this,"_scroll_changed");
  2331. add_child(h_scroll);
  2332. h_scroll->set_val(0);
  2333. track_name = memnew( LineEdit );
  2334. track_name->set_as_toplevel(true);
  2335. track_name->hide();
  2336. add_child(track_name);
  2337. track_name->connect("text_entered",this,"_track_name_changed");
  2338. track_menu = memnew( PopupMenu );
  2339. add_child(track_menu);
  2340. track_menu->connect("item_pressed",this,"_track_menu_selected");
  2341. last_idx =1;
  2342. _update_menu();
  2343. insert_confirm = memnew( ConfirmationDialog );
  2344. add_child(insert_confirm);
  2345. insert_confirm->connect("confirmed",this,"_confirm_insert_list");
  2346. click.click=ClickOver::CLICK_NONE;
  2347. name_column_ratio=0.3;
  2348. timeline_pos=0;
  2349. key_edit_dialog = memnew( PopupDialog );
  2350. key_editor = memnew( PropertyEditor );
  2351. add_child(key_edit_dialog);
  2352. key_editor->set_area_as_parent_rect();
  2353. key_editor->hide_top_label();
  2354. for(int i=0;i<4;i++)
  2355. key_editor->set_margin(Margin(i),5);
  2356. key_edit_dialog->add_child(key_editor);
  2357. key_edit = memnew( AnimationKeyEdit );
  2358. key_edit->undo_redo=undo_redo;
  2359. key_edit->ke_dialog=key_edit_dialog;
  2360. key_editor->edit(key_edit);
  2361. type_menu = memnew( PopupMenu );
  2362. add_child(type_menu);
  2363. for(int i=0;i<Variant::VARIANT_MAX;i++)
  2364. type_menu->add_item(Variant::get_type_name(Variant::Type(i)),i);
  2365. type_menu->connect("item_pressed",this,"_create_value_item");
  2366. keying=false;
  2367. insert_frame=0;
  2368. insert_query=false;
  2369. insert_queue=false;
  2370. editor_selection->connect("selection_changed",track_editor,"update");
  2371. scale_dialog = memnew( ConfirmationDialog );
  2372. VBoxContainer *vbc = memnew( VBoxContainer );
  2373. scale_dialog->add_child(vbc);
  2374. scale_dialog->set_child_rect(vbc);
  2375. scale = memnew( SpinBox );
  2376. scale->set_min(-99999);
  2377. scale->set_max(99999);
  2378. scale->set_step(0.001);
  2379. vbc->add_margin_child("Scale Ratio:",scale);
  2380. scale_dialog->connect("confirmed",this,"_scale");
  2381. add_child(scale_dialog);
  2382. }
  2383. AnimationKeyEditor::~AnimationKeyEditor() {
  2384. memdelete( key_edit );
  2385. }