animation_editor.cpp 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358
  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-2014 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. animation->optimize();
  493. track_editor->update();
  494. } break;
  495. }
  496. }
  497. float AnimationKeyEditor::_get_zoom_scale() const {
  498. float zv = zoom->get_val();
  499. if (zv<1) {
  500. zv = 1.0-zv;
  501. return Math::pow(1.0+zv,8.0)*100;
  502. } else {
  503. return 1.0/Math::pow(zv,8.0)*100;
  504. }
  505. }
  506. void AnimationKeyEditor::_track_pos_draw() {
  507. if (!animation.is_valid()) {
  508. return;
  509. }
  510. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  511. Size2 size= track_editor->get_size() - style->get_minimum_size();
  512. Size2 ofs = style->get_offset();
  513. int settings_limit = size.width - right_data_size_cache;
  514. int name_limit = settings_limit * name_column_ratio;
  515. float keys_from= h_scroll->get_val();
  516. float zoom_scale = _get_zoom_scale();
  517. float keys_to=keys_from+(settings_limit-name_limit) / zoom_scale;
  518. //will move to separate control! (for speedup)
  519. if (timeline_pos >= keys_from && timeline_pos<keys_to) {
  520. //draw position
  521. int pixel = (timeline_pos - h_scroll->get_val()) * zoom_scale;
  522. pixel+=name_limit;
  523. track_pos->draw_line(ofs+Point2(pixel,0),ofs+Point2(pixel,size.height),Color(1,0.3,0.3,0.8));
  524. }
  525. }
  526. void AnimationKeyEditor::_track_editor_draw() {
  527. VisualServer::get_singleton()->canvas_item_set_clip(track_editor->get_canvas_item(),true);
  528. if (animation.is_valid() && animation->get_track_count()) {
  529. if (selected_track < 0)
  530. selected_track=0;
  531. else if (selected_track>=animation->get_track_count())
  532. selected_track=animation->get_track_count()-1;
  533. }
  534. track_pos->update();
  535. Control *te=track_editor;
  536. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  537. te->draw_style_box(style,Rect2(Point2(),track_editor->get_size()));
  538. if (te->has_focus()) {
  539. te->draw_style_box(get_stylebox("bg_focus","Tree"),Rect2(Point2(),track_editor->get_size()));
  540. }
  541. if (!animation.is_valid()) {
  542. v_scroll->hide();
  543. h_scroll->hide();
  544. menu_track->set_disabled(true);
  545. edit_button->set_disabled(true);
  546. move_up_button->set_disabled(true);
  547. move_down_button->set_disabled(true);
  548. remove_button->set_disabled(true);
  549. return;
  550. }
  551. menu_track->set_disabled(false);
  552. edit_button->set_disabled(false);
  553. move_up_button->set_disabled(false);
  554. move_down_button->set_disabled(false);
  555. remove_button->set_disabled(false);
  556. te_drawing=true;
  557. Size2 size= te->get_size() - style->get_minimum_size();
  558. Size2 ofs = style->get_offset();
  559. Ref<Font> font = te->get_font("font","Tree");
  560. int sep = get_constant("vseparation","Tree");
  561. int hsep = get_constant("hseparation","Tree");
  562. Color color = get_color("font_color","Tree");
  563. Color sepcolor = get_color("guide_color","Tree");
  564. Color timecolor = get_color("prop_subsection","Editor");
  565. timecolor = Color::html("ff4a414f");
  566. Color hover_color = Color(1,1,1,0.05);
  567. Color select_color = Color(1,1,1,0.1);
  568. Color track_select_color =Color::html("ffbd8e8e");
  569. Ref<Texture> remove_icon = get_icon("Remove","EditorIcons");
  570. Ref<Texture> move_up_icon = get_icon("MoveUp","EditorIcons");
  571. Ref<Texture> move_down_icon = get_icon("MoveDown","EditorIcons");
  572. Ref<Texture> remove_icon_hl = get_icon("RemoveHl","EditorIcons");
  573. Ref<Texture> move_up_icon_hl = get_icon("MoveUpHl","EditorIcons");
  574. Ref<Texture> move_down_icon_hl = get_icon("MoveDownHl","EditorIcons");
  575. Ref<Texture> add_key_icon = get_icon("TrackAddKey","EditorIcons");
  576. Ref<Texture> add_key_icon_hl = get_icon("TrackAddKeyHl","EditorIcons");
  577. Ref<Texture> down_icon = get_icon("select_arrow","Tree");
  578. Ref<Texture> interp_icon[3]={
  579. get_icon("InterpRaw","EditorIcons"),
  580. get_icon("InterpLinear","EditorIcons"),
  581. get_icon("InterpCubic","EditorIcons")
  582. };
  583. Ref<Texture> cont_icon[3]={
  584. get_icon("TrackDiscrete","EditorIcons"),
  585. get_icon("TrackContinuous","EditorIcons")
  586. };
  587. Ref<Texture> type_icon[3]={
  588. get_icon("KeyValue","EditorIcons"),
  589. get_icon("KeyXform","EditorIcons"),
  590. get_icon("KeyCall","EditorIcons")
  591. };
  592. Ref<Texture> hsize_icon = get_icon("Hsize","EditorIcons");
  593. Ref<Texture> type_hover=get_icon("KeyHover","EditorIcons");
  594. Ref<Texture> type_selected=get_icon("KeySelected","EditorIcons");
  595. 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;
  596. int h = font->get_height()+sep;
  597. int fit = (size.height / h)-1;
  598. int total = animation->get_track_count();
  599. if (total < fit) {
  600. v_scroll->hide();
  601. v_scroll->set_max( total );
  602. v_scroll->set_page( fit );
  603. } else {
  604. v_scroll->show();
  605. v_scroll->set_max( total );
  606. v_scroll->set_page( fit );
  607. }
  608. int settings_limit = size.width - right_separator_ofs;
  609. int name_limit = settings_limit * name_column_ratio;
  610. te->draw_line(ofs+Point2(name_limit,0),ofs+Point2(name_limit,size.height),color);
  611. te->draw_line(ofs+Point2(settings_limit,0),ofs+Point2(settings_limit,size.height),color);
  612. te->draw_texture(hsize_icon,ofs+Point2(name_limit-hsize_icon->get_width()-hsep,(h-hsize_icon->get_height())/2));
  613. te->draw_line(ofs+Point2(0,h),ofs+Point2(size.width,h),color);
  614. // draw time
  615. float keys_from;
  616. float keys_to;
  617. float zoom_scale;
  618. {
  619. int zoomw = settings_limit-name_limit;
  620. float scale = _get_zoom_scale();
  621. zoom_scale=scale;
  622. float l = animation->get_length();
  623. if (l<=0)
  624. l=0.001; //avoid crashor
  625. int end_px = (l - h_scroll->get_val()) * scale;
  626. int begin_px = -h_scroll->get_val() * scale;
  627. Color notimecol;
  628. notimecol.r=timecolor.gray();
  629. notimecol.g=notimecol.r;
  630. notimecol.b=notimecol.r;
  631. notimecol.a=timecolor.a;
  632. {
  633. te->draw_rect(Rect2( ofs + Point2(name_limit,0), Point2(zoomw-1,h)), notimecol);
  634. if (begin_px < zoomw && end_px >0) {
  635. if (begin_px<0)
  636. begin_px=0;
  637. if (end_px>zoomw)
  638. end_px=zoomw;
  639. te->draw_rect(Rect2( ofs + Point2(name_limit+begin_px,0), Point2(end_px-begin_px-1,h)), timecolor);
  640. }
  641. }
  642. keys_from= h_scroll->get_val();
  643. keys_to=keys_from+zoomw / scale;
  644. {
  645. float time_min=0;
  646. float time_max=animation->get_length();
  647. for(int i=0;i<animation->get_track_count();i++) {
  648. if (animation->track_get_key_count(i)>0) {
  649. float beg = animation->track_get_key_time(i,0);
  650. if (beg<time_min)
  651. time_min=beg;
  652. float end = animation->track_get_key_time(i,animation->track_get_key_count(i)-1);
  653. if (end>time_max)
  654. time_max=end;
  655. }
  656. }
  657. float extra = (zoomw / scale)*0.5;
  658. if (time_min<-0.001)
  659. time_min-=extra;
  660. time_max+=extra;
  661. h_scroll->set_min(time_min);
  662. h_scroll->set_max(time_max);
  663. if (zoomw / scale < (time_max-time_min)) {
  664. h_scroll->show();
  665. } else {
  666. h_scroll->hide();
  667. }
  668. }
  669. h_scroll->set_page(zoomw / scale);
  670. Color color_time_sec = color;
  671. Color color_time_dec = color;
  672. color_time_dec.a*=0.5;
  673. #define SC_ADJ 100
  674. int min=30;
  675. int dec=1;
  676. int step=1;
  677. int decimals=2;
  678. bool step_found=false;
  679. while(!step_found) {
  680. static const int _multp[3]={1,2,5};
  681. for(int i=0;i<3;i++) {
  682. step = (_multp[i] * dec);
  683. if (step*scale/SC_ADJ> min) {
  684. step_found=true;
  685. break;
  686. }
  687. }
  688. if (step_found)
  689. break;
  690. dec*=10;
  691. decimals--;
  692. if (decimals<0)
  693. decimals=0;
  694. }
  695. for(int i=0;i<zoomw;i++) {
  696. float pos = h_scroll->get_val() + double(i)/scale;
  697. float prev = h_scroll->get_val() + (double(i)-1.0)/scale;
  698. int sc = int(Math::floor(pos*SC_ADJ));
  699. int prev_sc = int(Math::floor(prev*SC_ADJ));
  700. bool sub = (sc % SC_ADJ);
  701. if ((sc/step)!=(prev_sc/step) || (prev_sc<0 && sc>=0)) {
  702. int scd = sc < 0 ? prev_sc : sc;
  703. te->draw_line( ofs + Point2(name_limit+i,0), ofs+Point2(name_limit+i,h), color);
  704. 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);
  705. }
  706. }
  707. }
  708. for(int i=0;i<fit;i++) {
  709. int idx = v_scroll->get_val() + i;
  710. if (idx>=animation->get_track_count())
  711. break;
  712. int y = h+i*h+sep;
  713. if (/*mouse_over.over!=MouseOver::OVER_NONE &&*/ idx==mouse_over.track) {
  714. Color sepc=hover_color;
  715. te->draw_rect(Rect2(ofs+Point2(0,y),Size2(size.width,h-1)),sepc);
  716. }
  717. if (selected_track==idx) {
  718. Color tc = select_color;
  719. //tc.a*=0.7;
  720. te->draw_rect(Rect2(ofs+Point2(0,y),Size2(size.width-1,h-1)),tc);
  721. }
  722. te->draw_texture(type_icon[animation->track_get_type(idx)],ofs+Point2(0,y+(h-type_icon[0]->get_height())/2).floor());
  723. NodePath np = animation->track_get_path(idx);
  724. Node *n = root->get_node(np);
  725. Color ncol = color;
  726. if (n && editor_selection->is_selected(n))
  727. ncol=track_select_color;
  728. 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);
  729. te->draw_line(ofs+Point2(0,y+h),ofs+Point2(size.width,y+h),sepcolor);
  730. Point2 icon_ofs = ofs + Point2( size.width, y + (h - remove_icon->get_height() )/2).floor();
  731. icon_ofs.y+=4;
  732. /* icon_ofs.x-=remove_icon->get_width();
  733. te->draw_texture((mouse_over.over==MouseOver::OVER_REMOVE && mouse_over.track==idx)?remove_icon_hl:remove_icon,icon_ofs);
  734. icon_ofs.x-=hsep;
  735. icon_ofs.x-=move_down_icon->get_width();
  736. te->draw_texture((mouse_over.over==MouseOver::OVER_DOWN && mouse_over.track==idx)?move_down_icon_hl:move_down_icon,icon_ofs);
  737. icon_ofs.x-=hsep;
  738. icon_ofs.x-=move_up_icon->get_width();
  739. te->draw_texture((mouse_over.over==MouseOver::OVER_UP && mouse_over.track==idx)?move_up_icon_hl:move_up_icon,icon_ofs);
  740. icon_ofs.x-=hsep;
  741. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  742. icon_ofs.x-=hsep;
  743. */
  744. icon_ofs.x-=down_icon->get_width();
  745. te->draw_texture(down_icon,icon_ofs);
  746. int interp_type = animation->track_get_interpolation_type(idx);
  747. ERR_CONTINUE(interp_type<0 || interp_type>=3);
  748. icon_ofs.x-=hsep;
  749. icon_ofs.x-=interp_icon[interp_type]->get_width();
  750. te->draw_texture(interp_icon[interp_type],icon_ofs);
  751. icon_ofs.x-=hsep;
  752. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  753. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  754. int continuous = animation->value_track_is_continuous(idx)?1:0;
  755. icon_ofs.x-=hsep;
  756. icon_ofs.x-=down_icon->get_width();
  757. te->draw_texture(down_icon,icon_ofs);
  758. icon_ofs.x-=hsep;
  759. icon_ofs.x-=cont_icon[continuous]->get_width();
  760. te->draw_texture(cont_icon[continuous],icon_ofs);
  761. } else {
  762. icon_ofs.x -= hsep*2 + cont_icon[0]->get_width() + down_icon->get_width();
  763. }
  764. icon_ofs.x-=hsep;
  765. te->draw_line(Point2(icon_ofs.x,ofs.y+y),Point2(icon_ofs.x,ofs.y+y+h),sepcolor);
  766. icon_ofs.x-=hsep;
  767. icon_ofs.x-=add_key_icon->get_width();
  768. te->draw_texture((mouse_over.over==MouseOver::OVER_ADD_KEY && mouse_over.track==idx)?add_key_icon_hl:add_key_icon,icon_ofs);
  769. //draw the keys;
  770. int tt = animation->track_get_type(idx);
  771. float key_vofs = Math::floor((h - type_icon[tt]->get_height())/2);
  772. float key_hofs = -Math::floor(type_icon[tt]->get_height()/2);
  773. int kc=animation->track_get_key_count(idx);
  774. for(int i=0;i<kc;i++) {
  775. float time = animation->track_get_key_time(idx,i);
  776. if (time<keys_from)
  777. continue;
  778. if (time>keys_to)
  779. break;
  780. float x = key_hofs + name_limit + (time-keys_from)*zoom_scale;
  781. Ref<Texture> tex = type_icon[tt];
  782. SelectedKey sk;
  783. sk.key=i;
  784. sk.track=idx;
  785. if (selection.has(sk)) {
  786. if (click.click==ClickOver::CLICK_MOVE_KEYS)
  787. continue;
  788. tex=type_selected;
  789. }
  790. if (mouse_over.over==MouseOver::OVER_KEY && mouse_over.track==idx && mouse_over.over_key==i)
  791. tex=type_hover;
  792. te->draw_texture(tex,ofs+Point2(x,y+key_vofs).floor());
  793. }
  794. }
  795. switch(click.click) {
  796. case ClickOver::CLICK_SELECT_KEYS: {
  797. te->draw_rect(Rect2(click.at,click.to-click.at),Color(0.7,0.7,1.0,0.5));
  798. } break;
  799. case ClickOver::CLICK_MOVE_KEYS: {
  800. float from_t = 1e20;
  801. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  802. float t = animation->track_get_key_time(E->key().track,E->key().key);
  803. if (t<from_t)
  804. from_t=t;
  805. }
  806. float motion = from_t+(click.to.x - click.at.x)/zoom_scale;
  807. if (step->get_val())
  808. motion = Math::stepify(motion,step->get_val());
  809. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  810. int idx = E->key().track;
  811. int i = idx-v_scroll->get_val();
  812. if (i<0 || i>=fit)
  813. continue;
  814. int y = h+i*h+sep;
  815. float key_vofs = Math::floor((h - type_selected->get_height())/2);
  816. float key_hofs = -Math::floor(type_selected->get_height()/2);
  817. float time = animation->track_get_key_time(idx,E->key().key);
  818. float diff = time-from_t;
  819. float t = motion + diff;
  820. float x = (t-keys_from)*zoom_scale;
  821. //x+=click.to.x - click.at.x;
  822. if (x<0 || x>=(settings_limit-name_limit))
  823. continue;
  824. x+=name_limit;
  825. te->draw_texture(type_selected,ofs+Point2(x+key_hofs,y+key_vofs).floor());
  826. }
  827. } break;
  828. default: {};
  829. }
  830. te_drawing=false;
  831. }
  832. void AnimationKeyEditor::_track_name_changed(const String& p_name) {
  833. ERR_FAIL_COND(!animation.is_valid());
  834. undo_redo->create_action("Anim Track Rename");
  835. undo_redo->add_do_method(animation.ptr(),"track_set_path",track_name_editing,p_name);
  836. undo_redo->add_undo_method(animation.ptr(),"track_set_path",track_name_editing,animation->track_get_path(track_name_editing));
  837. undo_redo->commit_action();
  838. track_name->hide();
  839. }
  840. void AnimationKeyEditor::_track_menu_selected(int p_idx) {
  841. ERR_FAIL_COND(!animation.is_valid());
  842. if (interp_editing!=-1) {
  843. ERR_FAIL_INDEX(interp_editing,animation->get_track_count());
  844. undo_redo->create_action("Anim Track Change Interpolation");
  845. undo_redo->add_do_method(animation.ptr(),"track_set_interpolation_type",interp_editing,p_idx);
  846. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",interp_editing,animation->track_get_interpolation_type(interp_editing));
  847. undo_redo->commit_action();
  848. }
  849. if (cont_editing!=-1) {
  850. ERR_FAIL_INDEX(cont_editing,animation->get_track_count());
  851. undo_redo->create_action("Anim Track Change Value Mode");
  852. undo_redo->add_do_method(animation.ptr(),"value_track_set_continuous",cont_editing,p_idx);
  853. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",cont_editing,animation->value_track_is_continuous(cont_editing));
  854. undo_redo->commit_action();
  855. }
  856. }
  857. struct _AnimMoveRestore {
  858. int track;
  859. float time;
  860. Variant key;
  861. float transition;
  862. };
  863. void AnimationKeyEditor::_clear_selection_for_anim(const Ref<Animation>& p_anim) {
  864. if (!(animation==p_anim))
  865. return;
  866. selection.clear();
  867. }
  868. void AnimationKeyEditor::_select_at_anim(const Ref<Animation>& p_anim,int p_track,float p_pos){
  869. if (!(animation==p_anim))
  870. return;
  871. int idx = animation->track_find_key(p_track,p_pos,true);
  872. ERR_FAIL_COND(idx<0);
  873. SelectedKey sk;
  874. sk.track=p_track;
  875. sk.key=idx;
  876. KeyInfo ki;
  877. ki.pos=p_pos;
  878. selection.insert(sk,ki);
  879. }
  880. PropertyInfo AnimationKeyEditor::_find_hint_for_track(int p_idx) {
  881. ERR_FAIL_COND_V(!animation.is_valid(),PropertyInfo());
  882. ERR_FAIL_INDEX_V(p_idx,animation->get_track_count(),PropertyInfo());
  883. if (!root)
  884. return PropertyInfo();
  885. NodePath path = animation->track_get_path(p_idx);
  886. String property = path.get_property();
  887. if (property=="")
  888. return PropertyInfo();
  889. if (!root->has_node_and_resource(path))
  890. return PropertyInfo();
  891. RES res;
  892. Node *node = root->get_node_and_resource(path,res);
  893. List<PropertyInfo> pinfo;
  894. if (res.is_valid())
  895. res->get_property_list(&pinfo);
  896. else
  897. node->get_property_list(&pinfo);
  898. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  899. if (E->get().name==property)
  900. return E->get();
  901. }
  902. return PropertyInfo();
  903. }
  904. void AnimationKeyEditor::_track_editor_input_event(const InputEvent& p_input) {
  905. Control *te=track_editor;
  906. Ref<StyleBox> style = get_stylebox("normal","TextEdit");
  907. if (!animation.is_valid()) {
  908. return;
  909. }
  910. Size2 size= te->get_size() - style->get_minimum_size();
  911. Size2 ofs = style->get_offset();
  912. Ref<Font> font = te->get_font("font","Tree");
  913. int sep = get_constant("vseparation","Tree");
  914. int hsep = get_constant("hseparation","Tree");
  915. Color color = get_color("font_color","Tree");
  916. Color sepcolor = get_color("guide_color","Tree");
  917. Ref<Texture> remove_icon = get_icon("Remove","EditorIcons");
  918. Ref<Texture> move_up_icon = get_icon("MoveUp","EditorIcons");
  919. Ref<Texture> move_down_icon = get_icon("MoveDown","EditorIcons");
  920. Ref<Texture> down_icon = get_icon("select_arrow","Tree");
  921. Ref<Texture> hsize_icon = get_icon("Hsize","EditorIcons");
  922. Ref<Texture> add_key_icon = get_icon("TrackAddKey","EditorIcons");
  923. Ref<Texture> interp_icon[3]={
  924. get_icon("InterpRaw","EditorIcons"),
  925. get_icon("InterpLinear","EditorIcons"),
  926. get_icon("InterpCubic","EditorIcons")
  927. };
  928. Ref<Texture> cont_icon[3]={
  929. get_icon("TrackDiscrete","EditorIcons"),
  930. get_icon("TrackContinuous","EditorIcons")
  931. };
  932. Ref<Texture> type_icon[3]={
  933. get_icon("KeyValue","EditorIcons"),
  934. get_icon("KeyXform","EditorIcons"),
  935. get_icon("KeyCall","EditorIcons")
  936. };
  937. 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;
  938. int h = font->get_height()+sep;
  939. int fit = (size.height / h)-1;
  940. int total = animation->get_track_count();
  941. if (total < fit) {
  942. v_scroll->hide();
  943. } else {
  944. v_scroll->show();
  945. v_scroll->set_max( total );
  946. v_scroll->set_page( fit );
  947. }
  948. int settings_limit = size.width - right_separator_ofs;
  949. int name_limit = settings_limit * name_column_ratio;
  950. switch(p_input.type) {
  951. case InputEvent::KEY: {
  952. if (p_input.key.scancode==KEY_D && p_input.key.pressed && p_input.key.mod.command) {
  953. if (p_input.key.mod.shift)
  954. _menu_track(TRACK_MENU_DUPLICATE_TRANSPOSE);
  955. else
  956. _menu_track(TRACK_MENU_DUPLICATE);
  957. accept_event();;
  958. } else if (p_input.key.scancode==KEY_DELETE && p_input.key.pressed && click.click==ClickOver::CLICK_NONE) {
  959. if (selection.size()) {
  960. undo_redo->create_action("Anim Delete Keys");
  961. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  962. undo_redo->add_do_method(animation.ptr(),"track_remove_key",E->key().track,E->key().key);
  963. 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));
  964. }
  965. undo_redo->commit_action();
  966. selection.clear();
  967. accept_event();
  968. }
  969. } else if (animation.is_valid() && animation->get_track_count()>0) {
  970. if (p_input.is_pressed() && (p_input.is_action("ui_up") || p_input.is_action("ui_page_up"))) {
  971. if (p_input.is_action("ui_up"))
  972. selected_track--;
  973. if (v_scroll->is_visible() && p_input.is_action("ui_page_up"))
  974. selected_track=selected_track--;;
  975. if (selected_track<0)
  976. selected_track=0;
  977. if (v_scroll->is_visible()) {
  978. if (v_scroll->get_val() > selected_track)
  979. v_scroll->set_val(selected_track);
  980. }
  981. track_editor->update();
  982. accept_event();
  983. }
  984. if (p_input.is_pressed() && (p_input.is_action("ui_down") || p_input.is_action("ui_page_down"))) {
  985. if (p_input.is_action("ui_down"))
  986. selected_track++;
  987. else if (v_scroll->is_visible() && p_input.is_action("ui_page_down"))
  988. selected_track+=v_scroll->get_page();
  989. if (selected_track >= animation->get_track_count())
  990. selected_track=animation->get_track_count()-1;
  991. if (v_scroll->is_visible() && v_scroll->get_page()+v_scroll->get_val() < selected_track+1) {
  992. v_scroll->set_val(selected_track-v_scroll->get_page()+1);
  993. }
  994. track_editor->update();
  995. accept_event();
  996. }
  997. }
  998. } break;
  999. case InputEvent::MOUSE_BUTTON: {
  1000. const InputEventMouseButton &mb = p_input.mouse_button;
  1001. if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) {
  1002. v_scroll->set_val( v_scroll->get_val() - v_scroll->get_page() / 8 );
  1003. }
  1004. if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) {
  1005. v_scroll->set_val( v_scroll->get_val() + v_scroll->get_page() / 8 );
  1006. }
  1007. if (mb.button_index==BUTTON_LEFT && !(mb.button_mask&~BUTTON_MASK_LEFT)) {
  1008. if (mb.pressed) {
  1009. Point2 mpos = Point2(mb.x,mb.y)-ofs;
  1010. if (mpos.y < h ) {
  1011. if (mpos.x<name_limit && mpos.x > (name_limit - hsep - hsize_icon->get_width())) {
  1012. click.click=ClickOver::CLICK_RESIZE_NAMES;
  1013. click.at=Point2(mb.x,mb.y);
  1014. click.to=click.at;
  1015. click.at.y=name_limit;
  1016. }
  1017. if (mpos.x>=name_limit && mpos.x<settings_limit) {
  1018. //seek
  1019. //int zoomw = settings_limit-name_limit;
  1020. float scale = _get_zoom_scale();
  1021. float pos = h_scroll->get_val() + (mpos.x-name_limit) / scale;
  1022. if (animation->get_step())
  1023. pos=Math::stepify(pos,animation->get_step());
  1024. if (pos<0 )
  1025. pos=0;
  1026. if (pos>=animation->get_length())
  1027. pos=animation->get_length();
  1028. timeline_pos=pos;
  1029. click.click=ClickOver::CLICK_DRAG_TIMELINE;
  1030. click.at=Point2(mb.x,mb.y);
  1031. click.to=click.at;
  1032. emit_signal("timeline_changed",pos);
  1033. }
  1034. return;
  1035. }
  1036. mpos.y -= h;
  1037. int idx = mpos.y / h;
  1038. idx+=v_scroll->get_val();
  1039. if (idx <0)
  1040. break;
  1041. if (idx>=animation->get_track_count()) {
  1042. if (mpos.x >= name_limit && mpos.x<settings_limit) {
  1043. click.click=ClickOver::CLICK_SELECT_KEYS;
  1044. click.at=Point2(mb.x,mb.y);
  1045. click.to=click.at;
  1046. //drag select region
  1047. }
  1048. break;
  1049. }
  1050. if (mpos.x < name_limit) {
  1051. //name column
  1052. // area
  1053. if (idx!=selected_track) {
  1054. selected_track=idx;
  1055. track_editor->update();
  1056. break;
  1057. }
  1058. Rect2 area(ofs.x,ofs.y+((int(mpos.y)/h)+1)*h,name_limit, h );
  1059. track_name->set_text(animation->track_get_path(idx));
  1060. track_name->set_pos( te->get_global_pos() + area.pos);
  1061. track_name->set_size(area.size);
  1062. track_name->show_modal();
  1063. track_name->grab_focus();
  1064. track_name->select_all();
  1065. track_name_editing=idx;
  1066. } else if (mpos.x < settings_limit) {
  1067. float pos = mpos.x - name_limit;
  1068. pos/=_get_zoom_scale();
  1069. pos+=h_scroll->get_val();
  1070. float w_time = (type_icon[0]->get_width() / _get_zoom_scale())/2.0;
  1071. int kidx = animation->track_find_key(idx,pos);
  1072. int kidx_n = kidx+1;
  1073. int key=-1;
  1074. if (kidx>=0 && kidx<animation->track_get_key_count(idx)) {
  1075. float kpos = animation->track_get_key_time(idx,kidx);
  1076. if (ABS(pos-kpos)<=w_time) {
  1077. key=kidx;
  1078. }
  1079. }
  1080. if (key==-1 && kidx_n>=0 && kidx_n<animation->track_get_key_count(idx)) {
  1081. float kpos = animation->track_get_key_time(idx,kidx_n);
  1082. if (ABS(pos-kpos)<=w_time) {
  1083. key=kidx_n;
  1084. }
  1085. }
  1086. if (key==-1) {
  1087. click.click=ClickOver::CLICK_SELECT_KEYS;
  1088. click.at=Point2(mb.x,mb.y);
  1089. click.to=click.at;
  1090. click.shift=mb.mod.shift;
  1091. selected_track=idx;
  1092. track_editor->update();
  1093. //drag select region
  1094. return;
  1095. }
  1096. if (mb.mod.command || edit_button->is_pressed()) {
  1097. key_edit->animation=animation;
  1098. key_edit->track=idx;
  1099. key_edit->key_ofs=animation->track_get_key_time(idx,key);
  1100. key_edit->hint=_find_hint_for_track(idx);
  1101. key_edit->notify_change();
  1102. key_edit_dialog->set_size( Size2( 200,200) );
  1103. key_edit_dialog->set_pos( track_editor->get_global_pos() + ofs + mpos +Point2(-100,20));
  1104. key_edit_dialog->popup();
  1105. }
  1106. SelectedKey sk;
  1107. sk.track=idx;
  1108. sk.key=key;
  1109. KeyInfo ki;
  1110. ki.pos= animation->track_get_key_time(idx,key);
  1111. click.shift=mb.mod.shift;
  1112. click.selk=sk;
  1113. if (!mb.mod.shift && !selection.has(sk))
  1114. selection.clear();
  1115. selection.insert(sk,ki);
  1116. click.click=ClickOver::CLICK_MOVE_KEYS;
  1117. click.at=Point2(mb.x,mb.y);
  1118. click.to=click.at;
  1119. update();
  1120. selected_track=idx;
  1121. track_editor->update();
  1122. } else {
  1123. //button column
  1124. int ofsx = size.width - mpos.x;
  1125. if (ofsx < 0)
  1126. break;
  1127. /*
  1128. if (ofsx < remove_icon->get_width()) {
  1129. undo_redo->create_action("Remove Anim Track");
  1130. undo_redo->add_do_method(animation.ptr(),"remove_track",idx);
  1131. undo_redo->add_undo_method(animation.ptr(),"add_track",animation->track_get_type(idx),idx);
  1132. undo_redo->add_undo_method(animation.ptr(),"track_set_path",idx,animation->track_get_path(idx));
  1133. //todo interpolation
  1134. for(int i=0;i<animation->track_get_key_count(idx);i++) {
  1135. Variant v = animation->track_get_key_value(idx,i);
  1136. float time = animation->track_get_key_time(idx,i);
  1137. float trans = animation->track_get_key_transition(idx,i);
  1138. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,time,v);
  1139. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",idx,i,trans);
  1140. }
  1141. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",idx,animation->track_get_interpolation_type(idx));
  1142. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  1143. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",idx,animation->value_track_is_continuous(idx));
  1144. }
  1145. undo_redo->commit_action();
  1146. return;
  1147. }
  1148. ofsx-=hsep+remove_icon->get_width();
  1149. if (ofsx < move_down_icon->get_width()) {
  1150. if (idx < animation->get_track_count() -1) {
  1151. undo_redo->create_action("Move Anim Track Down");
  1152. undo_redo->add_do_method(animation.ptr(),"track_move_up",idx);
  1153. undo_redo->add_undo_method(animation.ptr(),"track_move_down",idx+1);
  1154. undo_redo->commit_action();
  1155. }
  1156. return;
  1157. }
  1158. ofsx-=hsep+move_down_icon->get_width();
  1159. if (ofsx < move_up_icon->get_width()) {
  1160. if (idx >0) {
  1161. undo_redo->create_action("Move Anim Track Up");
  1162. undo_redo->add_do_method(animation.ptr(),"track_move_down",idx);
  1163. undo_redo->add_undo_method(animation.ptr(),"track_move_up",idx-1);
  1164. undo_redo->commit_action();
  1165. }
  1166. return;
  1167. }
  1168. ofsx-=hsep*3+move_up_icon->get_width();
  1169. */
  1170. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep*2) {
  1171. track_menu->clear();
  1172. track_menu->set_size(Point2(1,1));
  1173. static const char *interp_name[3]={"Nearest","Linear","Cubic"};
  1174. for(int i=0;i<3;i++) {
  1175. track_menu->add_icon_item(interp_icon[i],interp_name[i]);
  1176. }
  1177. 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() ;
  1178. int popup_y = ofs.y+((int(mpos.y)/h)+2)*h;
  1179. int popup_x = ofs.x+size.width-lofs;
  1180. track_menu->set_pos(te->get_global_pos()+Point2(popup_x,popup_y));
  1181. interp_editing=idx;
  1182. cont_editing=-1;
  1183. track_menu->popup();
  1184. return;
  1185. }
  1186. ofsx-=hsep*2+interp_icon[0]->get_width()+down_icon->get_width();
  1187. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width()) {
  1188. track_menu->clear();
  1189. track_menu->set_size(Point2(1,1));
  1190. static const char *cont_name[3]={"Discrete","Continuous"};
  1191. for(int i=0;i<2;i++) {
  1192. track_menu->add_icon_item(cont_icon[i],cont_name[i]);
  1193. }
  1194. int lofs = settings_limit;
  1195. int popup_y = ofs.y+((int(mpos.y)/h)+2)*h;
  1196. int popup_x = ofs.x+lofs;
  1197. track_menu->set_pos(te->get_global_pos()+Point2(popup_x,popup_y));
  1198. interp_editing=-1;
  1199. cont_editing=idx;
  1200. track_menu->popup();
  1201. return;
  1202. }
  1203. ofsx-=hsep*3+cont_icon[0]->get_width()+down_icon->get_width();
  1204. if (ofsx < add_key_icon->get_width()) {
  1205. Animation::TrackType tt = animation->track_get_type(idx);
  1206. float pos = timeline_pos;
  1207. int existing = animation->track_find_key(idx,pos,true);
  1208. Variant newval;
  1209. if (tt==Animation::TYPE_TRANSFORM) {
  1210. Dictionary d;
  1211. d["loc"]=Vector3();
  1212. d["rot"]=Quat();
  1213. d["scale"]=Vector3();
  1214. newval=d;
  1215. } else if (tt==Animation::TYPE_METHOD) {
  1216. Dictionary d;
  1217. d["method"]="";
  1218. d["args"]=Vector<Variant>();
  1219. newval=d;
  1220. } else if (tt==Animation::TYPE_VALUE) {
  1221. PropertyInfo inf = _find_hint_for_track(idx);
  1222. if (inf.type!=Variant::NIL) {
  1223. Variant::CallError err;
  1224. newval=Variant::construct(inf.type,NULL,0,err);
  1225. }
  1226. if (newval.get_type()==Variant::NIL) {
  1227. //popup a new type
  1228. cvi_track=idx;
  1229. cvi_pos=pos;
  1230. type_menu->set_pos( get_global_pos() + mpos +ofs );
  1231. type_menu->popup();
  1232. return;
  1233. }
  1234. }
  1235. undo_redo->create_action("Anim Add Key");
  1236. undo_redo->add_do_method(animation.ptr(),"track_insert_key",idx,pos,newval,1);
  1237. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",idx,pos);
  1238. if (existing!=-1) {
  1239. Variant v = animation->track_get_key_value(idx,existing);
  1240. float trans = animation->track_get_key_transition(idx,existing);
  1241. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,pos,v,trans);
  1242. }
  1243. undo_redo->commit_action();
  1244. return;
  1245. }
  1246. }
  1247. } else {
  1248. switch(click.click) {
  1249. case ClickOver::CLICK_SELECT_KEYS: {
  1250. float zoom_scale=_get_zoom_scale();
  1251. float keys_from = h_scroll->get_val();
  1252. float keys_to = keys_from + (settings_limit-name_limit) / zoom_scale;
  1253. float from_time = keys_from + ( click.at.x - (name_limit+ofs.x)) / zoom_scale;
  1254. float to_time = keys_from + (click.to.x - (name_limit+ofs.x)) / zoom_scale;
  1255. if (to_time < from_time)
  1256. SWAP(from_time,to_time);
  1257. if (from_time > keys_to || to_time < keys_from)
  1258. break;
  1259. if (from_time < keys_from)
  1260. from_time = keys_from;
  1261. if (to_time >= keys_to)
  1262. to_time = keys_to;
  1263. int from_track = int(click.at.y-ofs.y-h-sep) / h + v_scroll->get_val();
  1264. int to_track = int(click.to.y-ofs.y-h-sep) / h + v_scroll->get_val();
  1265. int from_mod = int(click.at.y-ofs.y-sep) % h;
  1266. int to_mod = int(click.to.y-ofs.y-sep) % h;
  1267. if (to_track < from_track) {
  1268. SWAP(from_track,to_track);
  1269. SWAP(from_mod,to_mod);
  1270. }
  1271. if ((from_mod > (h/2)) && ((click.at.y-ofs.y)>=(h+sep))) {
  1272. from_track++;
  1273. }
  1274. if (to_mod < h/2) {
  1275. to_track--;
  1276. }
  1277. if (from_track>to_track) {
  1278. if (!click.shift)
  1279. selection.clear();
  1280. break;
  1281. }
  1282. int tracks_from = v_scroll->get_val();
  1283. int tracks_to = v_scroll->get_val()+fit-1;
  1284. if (tracks_to>=animation->get_track_count())
  1285. tracks_to=animation->get_track_count()-1;
  1286. tracks_from=0;
  1287. tracks_to=animation->get_track_count()-1;
  1288. if (to_track >tracks_to)
  1289. to_track = tracks_to;
  1290. if (from_track<tracks_from)
  1291. from_track=tracks_from;
  1292. if (from_track > tracks_to || to_track < tracks_from) {
  1293. if (!click.shift)
  1294. selection.clear();
  1295. break;
  1296. }
  1297. if (!click.shift)
  1298. selection.clear();
  1299. int higher_track=0x7FFFFFFF;
  1300. for(int i=from_track;i<=to_track;i++) {
  1301. int kc=animation->track_get_key_count(i);
  1302. for(int j=0;j<kc;j++) {
  1303. float t = animation->track_get_key_time(i,j);
  1304. if (t<from_time)
  1305. continue;
  1306. if (t>to_time)
  1307. break;
  1308. if (i<higher_track)
  1309. higher_track=i;
  1310. SelectedKey sk;
  1311. sk.track=i;
  1312. sk.key=j;
  1313. KeyInfo ki;
  1314. ki.pos=t;
  1315. selection[sk]=ki;
  1316. }
  1317. }
  1318. if (higher_track!=0x7FFFFFFF) {
  1319. selected_track=higher_track;
  1320. track_editor->update();
  1321. }
  1322. } break;
  1323. case ClickOver::CLICK_MOVE_KEYS: {
  1324. if (selection.empty())
  1325. break;
  1326. if (click.at==click.to) {
  1327. if (!click.shift) {
  1328. KeyInfo ki=selection[click.selk];
  1329. selection.clear();
  1330. selection[click.selk]=ki;
  1331. }
  1332. break;
  1333. }
  1334. float from_t = 1e20;
  1335. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  1336. float t = animation->track_get_key_time(E->key().track,E->key().key);
  1337. if (t<from_t)
  1338. from_t=t;
  1339. }
  1340. float motion = from_t+(click.to.x - click.at.x)/_get_zoom_scale();
  1341. if (step->get_val())
  1342. motion = Math::stepify(motion,step->get_val());
  1343. undo_redo->create_action("Anim Move Keys");
  1344. List<_AnimMoveRestore> to_restore;
  1345. // 1-remove the keys
  1346. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1347. undo_redo->add_do_method(animation.ptr(),"track_remove_key",E->key().track,E->key().key);
  1348. }
  1349. // 2- remove overlapped keys
  1350. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1351. float newtime = E->get().pos-from_t+motion;
  1352. int idx = animation->track_find_key(E->key().track,newtime,true);
  1353. if (idx==-1)
  1354. continue;
  1355. SelectedKey sk;
  1356. sk.key=idx;
  1357. sk.track=E->key().track;
  1358. if (selection.has(sk))
  1359. continue; //already in selection, don't save
  1360. undo_redo->add_do_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newtime);
  1361. _AnimMoveRestore amr;
  1362. amr.key=animation->track_get_key_value(E->key().track,idx);
  1363. amr.track=E->key().track;
  1364. amr.time=newtime;
  1365. amr.transition=animation->track_get_key_transition(E->key().track,idx);
  1366. to_restore.push_back(amr);
  1367. }
  1368. // 3-move the keys (re insert them)
  1369. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1370. float newpos=E->get().pos-from_t+motion;
  1371. //if (newpos<0)
  1372. // continue; //no add at the begining
  1373. 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));
  1374. }
  1375. // 4-(undo) remove inserted keys
  1376. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1377. float newpos=E->get().pos+-from_t+motion;
  1378. //if (newpos<0)
  1379. // continue; //no remove what no inserted
  1380. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newpos);
  1381. }
  1382. // 5-(undo) reinsert keys
  1383. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1384. 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));
  1385. }
  1386. // 6-(undo) reinsert overlapped keys
  1387. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  1388. _AnimMoveRestore &amr = E->get();
  1389. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  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. undo_redo->add_do_method(this,"_clear_selection_for_anim",animation);
  1397. undo_redo->add_undo_method(this,"_clear_selection_for_anim",animation);
  1398. // 7-reselect
  1399. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  1400. float oldpos=E->get().pos;
  1401. float newpos=oldpos-from_t+motion;
  1402. //if (newpos>=0)
  1403. undo_redo->add_do_method(this,"_select_at_anim",animation,E->key().track,newpos);
  1404. undo_redo->add_undo_method(this,"_select_at_anim",animation,E->key().track,oldpos);
  1405. }
  1406. undo_redo->commit_action();
  1407. } break;
  1408. default: {}
  1409. }
  1410. //button released
  1411. click.click=ClickOver::CLICK_NONE;
  1412. track_editor->update();
  1413. }
  1414. }
  1415. } break;
  1416. case InputEvent::MOUSE_MOTION: {
  1417. const InputEventMouseMotion &mb = p_input.mouse_motion;
  1418. mouse_over.over=MouseOver::OVER_NONE;
  1419. mouse_over.track=-1;
  1420. te->update();
  1421. track_editor->set_tooltip("");
  1422. if (!track_editor->has_focus() && (!get_focus_owner() || !get_focus_owner()->cast_to<LineEdit>()))
  1423. track_editor->call_deferred("grab_focus");
  1424. if (click.click!=ClickOver::CLICK_NONE) {
  1425. switch(click.click) {
  1426. case ClickOver::CLICK_RESIZE_NAMES: {
  1427. float base = click.at.y;
  1428. float clickp = click.at.x-ofs.x;
  1429. float dif = base - clickp;
  1430. float target = mb.x+dif-ofs.x;
  1431. float ratio = target / settings_limit;
  1432. if (ratio>0.9)
  1433. ratio=0.9;
  1434. else if (ratio<0.2)
  1435. ratio=0.2;
  1436. name_column_ratio=ratio;
  1437. } break;
  1438. case ClickOver::CLICK_DRAG_TIMELINE: {
  1439. Point2 mpos = Point2(mb.x,mb.y)-ofs;
  1440. /*
  1441. if (mpos.x<name_limit)
  1442. mpos.x=name_limit;
  1443. if (mpos.x>settings_limit)
  1444. mpos.x=settings_limit;
  1445. */
  1446. //int zoomw = settings_limit-name_limit;
  1447. float scale = _get_zoom_scale();
  1448. float pos = h_scroll->get_val() + (mpos.x-name_limit) / scale;
  1449. if (animation->get_step()) {
  1450. pos=Math::stepify(pos,animation->get_step());
  1451. }
  1452. if (pos<0)
  1453. pos=0;
  1454. if (pos>=animation->get_length())
  1455. pos=animation->get_length();
  1456. if (pos < h_scroll->get_val()) {
  1457. h_scroll->set_val(pos);
  1458. } else if (pos > h_scroll->get_val() + (settings_limit - name_limit) / scale) {
  1459. h_scroll->set_val( pos - (settings_limit - name_limit) / scale );
  1460. }
  1461. timeline_pos=pos;
  1462. emit_signal("timeline_changed",pos);
  1463. } break;
  1464. case ClickOver::CLICK_SELECT_KEYS: {
  1465. click.to=Point2(mb.x,mb.y);
  1466. if (click.to.y<h && click.at.y>h && mb.relative_y<0) {
  1467. float prev = v_scroll->get_val();
  1468. v_scroll->set_val( v_scroll->get_val() -1 );
  1469. if (prev!=v_scroll->get_val())
  1470. click.at.y+=h;
  1471. }
  1472. if (click.to.y>size.height && click.at.y<size.height && mb.relative_y>0) {
  1473. float prev = v_scroll->get_val();
  1474. v_scroll->set_val( v_scroll->get_val() +1 );
  1475. if (prev!=v_scroll->get_val())
  1476. click.at.y-=h;
  1477. }
  1478. } break;
  1479. case ClickOver::CLICK_MOVE_KEYS: {
  1480. click.to=Point2(mb.x,mb.y);
  1481. } break;
  1482. default: {}
  1483. }
  1484. return;
  1485. } else if (mb.button_mask&BUTTON_MASK_MIDDLE) {
  1486. int rel = mb.relative_x;
  1487. float relf = rel / _get_zoom_scale();
  1488. h_scroll->set_val( h_scroll->get_val() - relf );
  1489. }
  1490. if (mb.button_mask==0) {
  1491. Point2 mpos = Point2(mb.x,mb.y)-ofs;
  1492. if (mpos.y < h ) {
  1493. #if 0
  1494. //seek
  1495. //int zoomw = settings_limit-name_limit;
  1496. float scale = _get_zoom_scale();
  1497. float pos = h_scroll->get_val() + (mpos.y-name_limit) / scale;
  1498. if (pos<0 )
  1499. pos=0;
  1500. if (pos>=animation->get_length())
  1501. pos=animation->get_length();
  1502. timeline->set_val(pos);
  1503. #endif
  1504. return;
  1505. }
  1506. mpos.y -= h;
  1507. int idx = mpos.y / h;
  1508. idx+=v_scroll->get_val();
  1509. if (idx <0 || idx>=animation->get_track_count())
  1510. break;
  1511. mouse_over.track=idx;
  1512. if (mpos.x < name_limit) {
  1513. //name column
  1514. mouse_over.over=MouseOver::OVER_NAME;
  1515. } else if (mpos.x < settings_limit) {
  1516. float pos = mpos.x - name_limit;
  1517. pos/=_get_zoom_scale();
  1518. pos+=h_scroll->get_val();
  1519. float w_time = (type_icon[0]->get_width() / _get_zoom_scale())/2.0;
  1520. int kidx = animation->track_find_key(idx,pos);
  1521. int kidx_n = kidx+1;
  1522. bool found = false;
  1523. if (kidx>=0 && kidx<animation->track_get_key_count(idx)) {
  1524. float kpos = animation->track_get_key_time(idx,kidx);
  1525. if (ABS(pos-kpos)<=w_time) {
  1526. mouse_over.over=MouseOver::OVER_KEY;
  1527. mouse_over.track=idx;
  1528. mouse_over.over_key=kidx;
  1529. found=true;
  1530. }
  1531. }
  1532. if (!found && kidx_n>=0 && kidx_n<animation->track_get_key_count(idx)) {
  1533. float kpos = animation->track_get_key_time(idx,kidx_n);
  1534. if (ABS(pos-kpos)<=w_time) {
  1535. mouse_over.over=MouseOver::OVER_KEY;
  1536. mouse_over.track=idx;
  1537. mouse_over.over_key=kidx_n;
  1538. found=true;
  1539. }
  1540. }
  1541. if (found) {
  1542. String text;
  1543. text="time: "+rtos(animation->track_get_key_time(idx,mouse_over.over_key))+"\n";
  1544. switch(animation->track_get_type(idx)) {
  1545. case Animation::TYPE_TRANSFORM: {
  1546. Dictionary d = animation->track_get_key_value(idx,mouse_over.over_key);
  1547. if (d.has("loc"))
  1548. text+="loc: "+String(d["loc"])+"\n";
  1549. if (d.has("rot"))
  1550. text+="rot: "+String(d["rot"])+"\n";
  1551. if (d.has("scale"))
  1552. text+="scale: "+String(d["scale"])+"\n";
  1553. } break;
  1554. case Animation::TYPE_VALUE: {
  1555. Variant v = animation->track_get_key_value(idx,mouse_over.over_key);
  1556. text+="value: "+String(v)+"\n";
  1557. } break;
  1558. case Animation::TYPE_METHOD: {
  1559. Dictionary d = animation->track_get_key_value(idx,mouse_over.over_key);
  1560. if (d.has("method"))
  1561. text+=String(d["method"]);
  1562. text+="(";
  1563. Vector<Variant> args;
  1564. if (d.has("args"))
  1565. args=d["args"];
  1566. for(int i=0;i<args.size();i++) {
  1567. if (i>0)
  1568. text+=", ";
  1569. text+=String(args[i]);
  1570. }
  1571. text+=")\n";
  1572. } break;
  1573. }
  1574. text+="easing: "+rtos(animation->track_get_key_transition(idx,mouse_over.over_key));
  1575. track_editor->set_tooltip(text);
  1576. return;
  1577. }
  1578. } else {
  1579. //button column
  1580. int ofsx = size.width - mpos.x;
  1581. if (ofsx < 0)
  1582. break;
  1583. /*
  1584. if (ofsx < remove_icon->get_width()) {
  1585. mouse_over.over=MouseOver::OVER_REMOVE;
  1586. return;
  1587. }
  1588. ofsx-=hsep+remove_icon->get_width();
  1589. if (ofsx < move_down_icon->get_width()) {
  1590. mouse_over.over=MouseOver::OVER_DOWN;
  1591. return;
  1592. }
  1593. ofsx-=hsep+move_down_icon->get_width();
  1594. if (ofsx < move_up_icon->get_width()) {
  1595. mouse_over.over=MouseOver::OVER_UP;
  1596. return;
  1597. }
  1598. ofsx-=hsep*3+move_up_icon->get_width();
  1599. */
  1600. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep*2) {
  1601. mouse_over.over=MouseOver::OVER_INTERP;
  1602. return;
  1603. }
  1604. ofsx-=hsep*2+interp_icon[0]->get_width() + down_icon->get_width();
  1605. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width() +hsep*3) {
  1606. mouse_over.over=MouseOver::OVER_VALUE;
  1607. return;
  1608. }
  1609. ofsx-=hsep*3+cont_icon[0]->get_width() + down_icon->get_width();
  1610. if (ofsx < add_key_icon->get_width()) {
  1611. mouse_over.over=MouseOver::OVER_ADD_KEY;
  1612. return;
  1613. }
  1614. }
  1615. }
  1616. } break;
  1617. }
  1618. }
  1619. void AnimationKeyEditor::_notification(int p_what) {
  1620. switch(p_what) {
  1621. case NOTIFICATION_ENTER_SCENE: {
  1622. zoomicon->set_texture( get_icon("Zoom","EditorIcons") );
  1623. //menu_track->set_icon(get_icon("AddTrack","EditorIcons"));
  1624. menu_track->get_popup()->add_icon_item(get_icon("KeyValue","EditorIcons"),"Add Normal Track",TRACK_MENU_ADD_VALUE_TRACK);
  1625. menu_track->get_popup()->add_icon_item(get_icon("KeyXform","EditorIcons"),"Add Transform Track",TRACK_MENU_ADD_TRANSFORM_TRACK);
  1626. menu_track->get_popup()->add_icon_item(get_icon("KeyCall","EditorIcons"),"Add Call Func Track",TRACK_MENU_ADD_CALL_TRACK);
  1627. menu_track->get_popup()->add_separator();
  1628. menu_track->get_popup()->add_item("Scale Selection",TRACK_MENU_SCALE);
  1629. menu_track->get_popup()->add_item("Scale From Cursor",TRACK_MENU_SCALE_PIVOT);
  1630. menu_track->get_popup()->add_separator();
  1631. menu_track->get_popup()->add_item("Duplicate Selection",TRACK_MENU_DUPLICATE);
  1632. menu_track->get_popup()->add_item("Duplicate Transposed",TRACK_MENU_DUPLICATE_TRANSPOSE);
  1633. menu_track->get_popup()->add_separator();
  1634. menu_track->get_popup()->add_item("Goto Next Step",TRACK_MENU_NEXT_STEP,KEY_MASK_CMD|KEY_RIGHT);
  1635. menu_track->get_popup()->add_item("Goto Prev Step",TRACK_MENU_PREV_STEP,KEY_MASK_CMD|KEY_LEFT);
  1636. menu_track->get_popup()->add_separator();
  1637. PopupMenu *tpp = memnew( PopupMenu );
  1638. tpp->add_item("Linear",TRACK_MENU_SET_ALL_TRANS_LINEAR);
  1639. tpp->add_item("Constant",TRACK_MENU_SET_ALL_TRANS_CONSTANT);
  1640. tpp->add_item("In",TRACK_MENU_SET_ALL_TRANS_IN);
  1641. tpp->add_item("Out",TRACK_MENU_SET_ALL_TRANS_OUT);
  1642. tpp->add_item("In-Out",TRACK_MENU_SET_ALL_TRANS_INOUT);
  1643. tpp->add_item("Out-In",TRACK_MENU_SET_ALL_TRANS_OUTIN);
  1644. tpp->set_name("Transitions");
  1645. tpp->connect("item_pressed",this,"_menu_track");
  1646. menu_track->get_popup()->add_child(tpp);
  1647. menu_track->get_popup()->add_submenu_item("Set Transitions..","Transitions");
  1648. //menu_track->get_popup()->add_separator();
  1649. //menu_track->get_popup()->add_item("Optimize Animation",TRACK_MENU_OPTIMIZE);
  1650. move_up_button->set_icon(get_icon("MoveUp","EditorIcons"));
  1651. move_down_button->set_icon(get_icon("MoveDown","EditorIcons"));
  1652. remove_button->set_icon(get_icon("Remove","EditorIcons"));
  1653. edit_button->set_icon(get_icon("EditKey","EditorIcons"));
  1654. loop->set_icon(get_icon("Loop","EditorIcons"));
  1655. //edit_button->add_color_override("font_color",get_color("font_color","Tree"));
  1656. //edit_button->add_color_override("font_color_hover",get_color("font_color","Tree"));
  1657. {
  1658. right_data_size_cache=0;
  1659. int hsep = get_constant("hseparation","Tree");
  1660. Ref<Texture> remove_icon = get_icon("Remove","EditorIcons");
  1661. Ref<Texture> move_up_icon = get_icon("MoveUp","EditorIcons");
  1662. Ref<Texture> move_down_icon = get_icon("MoveDown","EditorIcons");
  1663. Ref<Texture> down_icon = get_icon("select_arrow","Tree");
  1664. Ref<Texture> add_key_icon = get_icon("TrackAddKey","EditorIcons");
  1665. Ref<Texture> interp_icon[3]={
  1666. get_icon("InterpRaw","EditorIcons"),
  1667. get_icon("InterpLinear","EditorIcons"),
  1668. get_icon("InterpCubic","EditorIcons")
  1669. };
  1670. Ref<Texture> cont_icon[3]={
  1671. get_icon("TrackDiscrete","EditorIcons"),
  1672. get_icon("TrackContinuous","EditorIcons")
  1673. };
  1674. //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;
  1675. 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;
  1676. }
  1677. // rename_anim->set_icon( get_icon("Rename","EditorIcons") );
  1678. /*
  1679. edit_anim->set_icon( get_icon("Edit","EditorIcons") );
  1680. blend_anim->set_icon( get_icon("Blend","EditorIcons") );
  1681. play->set_icon( get_icon("Play","EditorIcons") );
  1682. stop->set_icon( get_icon("Stop","EditorIcons") );
  1683. pause->set_icon( get_icon("Pause","EditorIcons") );
  1684. */
  1685. // menu->set_icon(get_icon("Animation","EditorIcons"));
  1686. // play->set_icon(get_icon("AnimationPlay","EditorIcons"));
  1687. //menu->set_icon(get_icon("Animation","EditorIcons"));
  1688. _update_menu();
  1689. } break;
  1690. }
  1691. }
  1692. void AnimationKeyEditor::_scroll_changed(double) {
  1693. if (te_drawing)
  1694. return;
  1695. track_editor->update();
  1696. }
  1697. void AnimationKeyEditor::_update_paths() {
  1698. if (animation.is_valid()) {
  1699. //timeline->set_max(animation->get_length());
  1700. //timeline->set_step(0.01);
  1701. track_editor->update();
  1702. length->set_val(animation->get_length());
  1703. step->set_val(animation->get_step());
  1704. }
  1705. }
  1706. void AnimationKeyEditor::_root_removed() {
  1707. root=NULL;
  1708. }
  1709. void AnimationKeyEditor::_update_menu() {
  1710. updating=true;
  1711. bool empty= !animation.is_valid();
  1712. if (animation.is_valid()) {
  1713. length->set_val(animation->get_length());
  1714. loop->set_pressed(animation->has_loop());
  1715. step->set_val(animation->get_step());
  1716. }
  1717. track_editor->update();
  1718. updating=false;
  1719. }
  1720. void AnimationKeyEditor::set_animation(const Ref<Animation>& p_anim) {
  1721. if (animation.is_valid())
  1722. animation->disconnect("changed",this,"_update_paths");
  1723. animation=p_anim;
  1724. if (animation.is_valid())
  1725. animation->connect("changed",this,"_update_paths");
  1726. timeline_pos=0;
  1727. selection.clear();
  1728. _update_paths();
  1729. _update_menu();
  1730. selected_track=-1;
  1731. }
  1732. void AnimationKeyEditor::set_root(Node *p_root) {
  1733. if (root)
  1734. root->disconnect("exit_scene",this,"_root_removed");
  1735. root=p_root;
  1736. if (root)
  1737. root->connect("exit_scene",this,"_root_removed",make_binds(),CONNECT_ONESHOT);
  1738. }
  1739. Node *AnimationKeyEditor::get_root() const {
  1740. return root;
  1741. }
  1742. void AnimationKeyEditor::set_keying(bool p_enabled) {
  1743. keying=p_enabled;
  1744. _update_menu();
  1745. emit_signal("keying_changed",p_enabled);
  1746. }
  1747. bool AnimationKeyEditor::has_keying() const {
  1748. return keying;
  1749. }
  1750. void AnimationKeyEditor::_query_insert(const InsertData& p_id) {
  1751. if (insert_frame!=OS::get_singleton()->get_frames_drawn()) {
  1752. if (insert_confirm->is_visible())
  1753. return; //do nothing
  1754. insert_data.clear();
  1755. insert_query=false;
  1756. }
  1757. insert_frame=OS::get_singleton()->get_frames_drawn();
  1758. insert_data.push_back(p_id);
  1759. if (p_id.track_idx==-1) {
  1760. //potential new key, does not exist
  1761. if (insert_data.size()==1)
  1762. insert_confirm->set_text("Create NEW track for "+p_id.query+" and insert key?");
  1763. else
  1764. insert_confirm->set_text("Create "+itos(insert_data.size())+" NEW tracks and insert keys?");
  1765. insert_confirm->get_ok()->set_text("Create");
  1766. insert_confirm->popup_centered(Size2(300,100));
  1767. insert_query=true;
  1768. } else {
  1769. if (!insert_query && !insert_queue) {
  1770. call_deferred("_insert_delay");
  1771. insert_queue=true;
  1772. }
  1773. }
  1774. }
  1775. void AnimationKeyEditor::insert_transform_key(Spatial *p_node,const String& p_sub,const Transform& p_xform) {
  1776. if (!keying)
  1777. return;
  1778. if (!animation.is_valid())
  1779. return;
  1780. ERR_FAIL_COND(!root);
  1781. //let's build a node path
  1782. String path = root->get_path_to(p_node);
  1783. if (p_sub!="")
  1784. path+=":"+p_sub;
  1785. NodePath np=path;
  1786. int track_idx=-1;
  1787. for(int i=0;i<animation->get_track_count();i++) {
  1788. if (animation->track_get_type(i)!=Animation::TYPE_TRANSFORM)
  1789. continue;
  1790. if (animation->track_get_path(i)!=np)
  1791. continue;
  1792. track_idx=i;
  1793. break;
  1794. }
  1795. InsertData id;
  1796. Dictionary val;
  1797. id.path=np;
  1798. id.track_idx=track_idx;
  1799. id.value=p_xform;
  1800. id.type=Animation::TYPE_TRANSFORM;
  1801. id.query="node '"+p_node->get_name()+"'";
  1802. //dialog insert
  1803. _query_insert(id);
  1804. }
  1805. void AnimationKeyEditor::insert_node_value_key(Node* p_node, const String& p_property,const Variant& p_value,bool p_only_if_exists) {
  1806. ERR_FAIL_COND(!root);
  1807. //let's build a node path
  1808. Node *node = p_node;
  1809. String path = root->get_path_to(node);
  1810. for(int i=1;i<history->get_path_size();i++) {
  1811. String prop = history->get_path_property(i);
  1812. ERR_FAIL_COND(prop=="");
  1813. path+=":"+prop;
  1814. }
  1815. path+=":"+p_property;
  1816. NodePath np = path;
  1817. //locate track
  1818. int track_idx=-1;
  1819. for(int i=0;i<animation->get_track_count();i++) {
  1820. if (animation->track_get_type(i)!=Animation::TYPE_VALUE)
  1821. continue;
  1822. if (animation->track_get_path(i)!=np)
  1823. continue;
  1824. track_idx=i;
  1825. break;
  1826. }
  1827. if (p_only_if_exists && track_idx==-1)
  1828. return;
  1829. InsertData id;
  1830. id.path=np;
  1831. id.track_idx=track_idx;
  1832. id.value=p_value;
  1833. id.type=Animation::TYPE_VALUE;
  1834. id.query="property '"+p_property+"'";
  1835. //dialog insert
  1836. _query_insert(id);
  1837. }
  1838. void AnimationKeyEditor::insert_value_key(const String& p_property,const Variant& p_value) {
  1839. ERR_FAIL_COND(!root);
  1840. //let's build a node path
  1841. ERR_FAIL_COND(history->get_path_size()==0);
  1842. Object *obj = ObjectDB::get_instance(history->get_path_object(0));
  1843. ERR_FAIL_COND(!obj || !obj->cast_to<Node>());
  1844. Node *node = obj->cast_to<Node>();
  1845. String path = root->get_path_to(node);
  1846. for(int i=1;i<history->get_path_size();i++) {
  1847. String prop = history->get_path_property(i);
  1848. ERR_FAIL_COND(prop=="");
  1849. path+=":"+prop;
  1850. }
  1851. path+=":"+p_property;
  1852. NodePath np = path;
  1853. //locate track
  1854. int track_idx=-1;
  1855. for(int i=0;i<animation->get_track_count();i++) {
  1856. if (animation->track_get_type(i)!=Animation::TYPE_VALUE)
  1857. continue;
  1858. if (animation->track_get_path(i)!=np)
  1859. continue;
  1860. track_idx=i;
  1861. break;
  1862. }
  1863. InsertData id;
  1864. id.path=np;
  1865. id.track_idx=track_idx;
  1866. id.value=p_value;
  1867. id.type=Animation::TYPE_VALUE;
  1868. id.query="property '"+p_property+"'";
  1869. //dialog insert
  1870. _query_insert(id);
  1871. }
  1872. void AnimationKeyEditor::_confirm_insert_list() {
  1873. undo_redo->create_action("Anim Create & Insert");
  1874. int last_track = animation->get_track_count();
  1875. while(insert_data.size()) {
  1876. last_track=_confirm_insert(insert_data.front()->get(),last_track);
  1877. insert_data.pop_front();
  1878. }
  1879. undo_redo->commit_action();
  1880. }
  1881. int AnimationKeyEditor::_confirm_insert(InsertData p_id,int p_last_track) {
  1882. if (p_last_track==-1)
  1883. p_last_track=animation->get_track_count();
  1884. bool created=false;
  1885. if (p_id.track_idx<0) {
  1886. created=true;
  1887. undo_redo->create_action("Anim Insert Track & Key");
  1888. bool continuous=false;
  1889. if (p_id.type==Animation::TYPE_VALUE) {
  1890. //wants a new tack
  1891. {
  1892. //shitty hack
  1893. animation->add_track(p_id.type);
  1894. animation->track_set_path(animation->get_track_count()-1,p_id.path);
  1895. PropertyInfo h = _find_hint_for_track(animation->get_track_count()-1);
  1896. animation->remove_track(animation->get_track_count()-1); //hack
  1897. continuous =
  1898. h.type==Variant::REAL ||
  1899. h.type==Variant::VECTOR2 ||
  1900. h.type==Variant::RECT2 ||
  1901. h.type==Variant::VECTOR3 ||
  1902. h.type==Variant::_AABB ||
  1903. h.type==Variant::QUAT ||
  1904. h.type==Variant::COLOR ||
  1905. h.type==Variant::TRANSFORM ;
  1906. }
  1907. }
  1908. p_id.track_idx=p_last_track;
  1909. undo_redo->add_do_method(animation.ptr(),"add_track",p_id.type);
  1910. undo_redo->add_do_method(animation.ptr(),"track_set_path",p_id.track_idx,p_id.path);
  1911. if (p_id.type==Animation::TYPE_VALUE)
  1912. undo_redo->add_do_method(animation.ptr(),"value_track_set_continuous",p_id.track_idx,continuous);
  1913. } else {
  1914. undo_redo->create_action("Anim Insert Key");
  1915. }
  1916. float time = timeline_pos;
  1917. Variant value;
  1918. switch(p_id.type) {
  1919. case Animation::TYPE_VALUE: {
  1920. value = p_id.value;
  1921. } break;
  1922. case Animation::TYPE_TRANSFORM: {
  1923. Transform tr = p_id.value;
  1924. Dictionary d;
  1925. d["loc"]=tr.origin;
  1926. d["scale"]=tr.basis.get_scale();
  1927. d["rot"]=Quat(tr.basis);//.orthonormalized();
  1928. value=d;
  1929. } break;
  1930. default:{}
  1931. }
  1932. undo_redo->add_do_method(animation.ptr(),"track_insert_key",p_id.track_idx,time,value);
  1933. if (created) {
  1934. //just remove the track
  1935. undo_redo->add_undo_method(animation.ptr(),"remove_track",p_last_track);
  1936. p_last_track++;
  1937. } else {
  1938. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",p_id.track_idx,time);
  1939. int existing = animation->track_find_key(p_id.track_idx,time,true);
  1940. if (existing!=-1) {
  1941. Variant v = animation->track_get_key_value(p_id.track_idx,existing);
  1942. float trans = animation->track_get_key_transition(p_id.track_idx,existing);
  1943. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",p_id.track_idx,time,v,trans);
  1944. }
  1945. }
  1946. undo_redo->add_do_method(this,"update");
  1947. undo_redo->add_undo_method(this,"update");
  1948. undo_redo->add_do_method(track_editor,"update");
  1949. undo_redo->add_undo_method(track_editor,"update");
  1950. undo_redo->add_do_method(track_pos,"update");
  1951. undo_redo->add_undo_method(track_pos,"update");
  1952. undo_redo->commit_action();
  1953. return p_last_track;
  1954. }
  1955. Ref<Animation> AnimationKeyEditor::get_current_animation() const {
  1956. return animation;
  1957. }
  1958. void AnimationKeyEditor::_animation_len_changed(float p_len) {
  1959. if (updating)
  1960. return;
  1961. if (!animation.is_null()) {
  1962. undo_redo->create_action("Change Anim Len");
  1963. undo_redo->add_do_method(animation.ptr(),"set_length",p_len);
  1964. undo_redo->add_undo_method(animation.ptr(),"set_length",animation->get_length());
  1965. undo_redo->add_do_method(this,"_animation_len_update");
  1966. undo_redo->add_undo_method(this,"_animation_len_update");
  1967. undo_redo->commit_action();
  1968. }
  1969. }
  1970. void AnimationKeyEditor::_animation_len_update() {
  1971. if (!animation.is_null())
  1972. emit_signal(alc,animation->get_length());
  1973. }
  1974. void AnimationKeyEditor::_animation_changed() {
  1975. if (updating)
  1976. return;
  1977. _update_menu();
  1978. }
  1979. void AnimationKeyEditor::_animation_loop_changed() {
  1980. if (updating)
  1981. return;
  1982. if (!animation.is_null()) {
  1983. undo_redo->create_action("Change Anim Loop");
  1984. undo_redo->add_do_method(animation.ptr(),"set_loop",loop->is_pressed());
  1985. undo_redo->add_undo_method(animation.ptr(),"set_loop",!loop->is_pressed());
  1986. undo_redo->commit_action();
  1987. }
  1988. }
  1989. void AnimationKeyEditor::_create_value_item(int p_type) {
  1990. undo_redo->create_action("Anim Create Typed Value Key");
  1991. Variant::CallError ce;
  1992. Variant v = Variant::construct(Variant::Type(p_type),NULL,0,ce);
  1993. undo_redo->add_do_method(animation.ptr(),"track_insert_key",cvi_track,cvi_pos,v);
  1994. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",cvi_track,cvi_pos);
  1995. int existing = animation->track_find_key(cvi_track,cvi_pos,true);
  1996. if (existing!=-1) {
  1997. Variant v = animation->track_get_key_value(cvi_track,existing);
  1998. float trans = animation->track_get_key_transition(cvi_track,existing);
  1999. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",cvi_track,cvi_pos,v,trans);
  2000. }
  2001. undo_redo->commit_action();
  2002. }
  2003. void AnimationKeyEditor::set_anim_pos(float p_pos) {
  2004. if (animation.is_null())
  2005. return;
  2006. timeline_pos=p_pos;
  2007. update();
  2008. track_pos->update();
  2009. track_editor->update();
  2010. }
  2011. void AnimationKeyEditor::_pane_drag(const Point2& p_delta) {
  2012. Size2 ecs = ec->get_minsize();
  2013. ecs.y-=p_delta.y;
  2014. if (ecs.y<100)
  2015. ecs.y=100;
  2016. ec->set_minsize(ecs);;
  2017. }
  2018. void AnimationKeyEditor::_insert_delay() {
  2019. if (insert_query) {
  2020. //discard since it's entered into query mode
  2021. insert_queue=false;
  2022. return;
  2023. }
  2024. undo_redo->create_action("Anim Insert");
  2025. int last_track = animation->get_track_count();
  2026. while(insert_data.size()) {
  2027. last_track=_confirm_insert(insert_data.front()->get(),last_track);
  2028. insert_data.pop_front();
  2029. }
  2030. undo_redo->commit_action();
  2031. insert_queue=false;
  2032. }
  2033. void AnimationKeyEditor::_step_changed(float p_len) {
  2034. updating=true;
  2035. animation->set_step(p_len);
  2036. updating=false;
  2037. }
  2038. void AnimationKeyEditor::_scale() {
  2039. if (selection.empty())
  2040. return;
  2041. float from_t = 1e20;
  2042. float to_t = -1e20;
  2043. float len = -1e20;
  2044. float pivot=0;
  2045. for(Map<SelectedKey,KeyInfo>::Element *E=selection.front();E;E=E->next()) {
  2046. float t = animation->track_get_key_time(E->key().track,E->key().key);
  2047. if (t<from_t)
  2048. from_t=t;
  2049. if (t>to_t)
  2050. to_t=t;
  2051. }
  2052. len=to_t-from_t;
  2053. if (last_menu_track_opt==TRACK_MENU_SCALE_PIVOT) {
  2054. pivot = timeline_pos;
  2055. } else {
  2056. pivot=from_t;
  2057. }
  2058. float s = scale->get_val();
  2059. if (s==0) {
  2060. ERR_PRINT("Can't scale to 0");
  2061. }
  2062. undo_redo->create_action("Anim Scale Keys");
  2063. List<_AnimMoveRestore> to_restore;
  2064. // 1-remove the keys
  2065. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2066. undo_redo->add_do_method(animation.ptr(),"track_remove_key",E->key().track,E->key().key);
  2067. }
  2068. // 2- remove overlapped keys
  2069. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2070. float newtime = (E->get().pos-from_t)*s+from_t;
  2071. int idx = animation->track_find_key(E->key().track,newtime,true);
  2072. if (idx==-1)
  2073. continue;
  2074. SelectedKey sk;
  2075. sk.key=idx;
  2076. sk.track=E->key().track;
  2077. if (selection.has(sk))
  2078. continue; //already in selection, don't save
  2079. undo_redo->add_do_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newtime);
  2080. _AnimMoveRestore amr;
  2081. amr.key=animation->track_get_key_value(E->key().track,idx);
  2082. amr.track=E->key().track;
  2083. amr.time=newtime;
  2084. amr.transition=animation->track_get_key_transition(E->key().track,idx);
  2085. to_restore.push_back(amr);
  2086. }
  2087. #define _NEW_POS(m_ofs) (((s>0)?m_ofs:from_t+(len-(m_ofs-from_t)))-pivot)*ABS(s)+from_t
  2088. // 3-move the keys (re insert them)
  2089. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2090. float newpos=_NEW_POS(E->get().pos);
  2091. 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));
  2092. }
  2093. // 4-(undo) remove inserted keys
  2094. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2095. float newpos=_NEW_POS(E->get().pos);
  2096. undo_redo->add_undo_method(animation.ptr(),"track_remove_key_at_pos",E->key().track,newpos);
  2097. }
  2098. // 5-(undo) reinsert keys
  2099. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2100. 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));
  2101. }
  2102. // 6-(undo) reinsert overlapped keys
  2103. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  2104. _AnimMoveRestore &amr = E->get();
  2105. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  2106. }
  2107. // 6-(undo) reinsert overlapped keys
  2108. for(List<_AnimMoveRestore>::Element *E=to_restore.front();E;E=E->next()) {
  2109. _AnimMoveRestore &amr = E->get();
  2110. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",amr.track,amr.time,amr.key,amr.transition);
  2111. }
  2112. undo_redo->add_do_method(this,"_clear_selection_for_anim",animation);
  2113. undo_redo->add_undo_method(this,"_clear_selection_for_anim",animation);
  2114. // 7-reselect
  2115. for(Map<SelectedKey,KeyInfo>::Element *E=selection.back();E;E=E->prev()) {
  2116. float oldpos=E->get().pos;
  2117. float newpos=_NEW_POS(oldpos);
  2118. if (newpos>=0)
  2119. undo_redo->add_do_method(this,"_select_at_anim",animation,E->key().track,newpos);
  2120. undo_redo->add_undo_method(this,"_select_at_anim",animation,E->key().track,oldpos);
  2121. }
  2122. #undef _NEW_POS
  2123. undo_redo->commit_action();
  2124. }
  2125. void AnimationKeyEditor::cleanup() {
  2126. set_animation(Ref<Animation>());
  2127. }
  2128. void AnimationKeyEditor::_bind_methods() {
  2129. ObjectTypeDB::bind_method(_MD("_root_removed"),&AnimationKeyEditor::_root_removed);
  2130. ObjectTypeDB::bind_method(_MD("_scale"),&AnimationKeyEditor::_scale);
  2131. ObjectTypeDB::bind_method(_MD("set_root"),&AnimationKeyEditor::set_root);
  2132. // ObjectTypeDB::bind_method(_MD("_confirm_insert"),&AnimationKeyEditor::_confirm_insert);
  2133. ObjectTypeDB::bind_method(_MD("_confirm_insert_list"),&AnimationKeyEditor::_confirm_insert_list);
  2134. ObjectTypeDB::bind_method(_MD("_update_paths"),&AnimationKeyEditor::_update_paths);
  2135. ObjectTypeDB::bind_method(_MD("_track_editor_draw"),&AnimationKeyEditor::_track_editor_draw);
  2136. ObjectTypeDB::bind_method(_MD("_animation_changed"),&AnimationKeyEditor::_animation_changed);
  2137. ObjectTypeDB::bind_method(_MD("_scroll_changed"),&AnimationKeyEditor::_scroll_changed);
  2138. ObjectTypeDB::bind_method(_MD("_track_editor_input_event"),&AnimationKeyEditor::_track_editor_input_event);
  2139. ObjectTypeDB::bind_method(_MD("_track_name_changed"),&AnimationKeyEditor::_track_name_changed);
  2140. ObjectTypeDB::bind_method(_MD("_track_menu_selected"),&AnimationKeyEditor::_track_menu_selected);
  2141. ObjectTypeDB::bind_method(_MD("_menu_track"),&AnimationKeyEditor::_menu_track);
  2142. ObjectTypeDB::bind_method(_MD("_clear_selection_for_anim"),&AnimationKeyEditor::_clear_selection_for_anim);
  2143. ObjectTypeDB::bind_method(_MD("_select_at_anim"),&AnimationKeyEditor::_select_at_anim);
  2144. ObjectTypeDB::bind_method(_MD("_track_pos_draw"),&AnimationKeyEditor::_track_pos_draw);
  2145. ObjectTypeDB::bind_method(_MD("_insert_delay"),&AnimationKeyEditor::_insert_delay);
  2146. ObjectTypeDB::bind_method(_MD("_step_changed"),&AnimationKeyEditor::_step_changed);
  2147. ObjectTypeDB::bind_method(_MD("_animation_loop_changed"),&AnimationKeyEditor::_animation_loop_changed);
  2148. ObjectTypeDB::bind_method(_MD("_animation_len_changed"),&AnimationKeyEditor::_animation_len_changed);
  2149. ObjectTypeDB::bind_method(_MD("_create_value_item"),&AnimationKeyEditor::_create_value_item);
  2150. ObjectTypeDB::bind_method(_MD("_pane_drag"),&AnimationKeyEditor::_pane_drag);
  2151. ObjectTypeDB::bind_method(_MD("_animation_len_update"),&AnimationKeyEditor::_animation_len_update);
  2152. ObjectTypeDB::bind_method(_MD("set_animation"),&AnimationKeyEditor::set_animation);
  2153. ADD_SIGNAL( MethodInfo("resource_selected", PropertyInfo( Variant::OBJECT, "res"),PropertyInfo( Variant::STRING, "prop") ) );
  2154. ADD_SIGNAL( MethodInfo("keying_changed" ) );
  2155. ADD_SIGNAL( MethodInfo("timeline_changed", PropertyInfo(Variant::REAL,"pos") ) );
  2156. ADD_SIGNAL( MethodInfo("animation_len_changed", PropertyInfo(Variant::REAL,"len") ) );
  2157. }
  2158. AnimationKeyEditor::AnimationKeyEditor(UndoRedo *p_undo_redo, EditorHistory *p_history,EditorSelection *p_selection) {
  2159. alc="animation_len_changed";
  2160. editor_selection=p_selection;
  2161. selected_track=-1;
  2162. updating=false;
  2163. te_drawing=false;
  2164. undo_redo=p_undo_redo;
  2165. history=p_history;
  2166. HBoxContainer *hb = memnew( HBoxContainer );
  2167. add_child(hb);
  2168. root=NULL;
  2169. //menu = memnew( MenuButton );
  2170. //menu->set_flat(true);
  2171. //menu->set_pos(Point2());
  2172. //add_child(menu);
  2173. menu_track = memnew( MenuButton );
  2174. menu_track->set_text("Tracks..");
  2175. hb->add_child(menu_track);
  2176. menu_track->get_popup()->connect("item_pressed",this,"_menu_track");
  2177. hb->add_child( memnew( VSeparator ) );
  2178. zoomicon = memnew( TextureFrame );
  2179. hb->add_child(zoomicon);
  2180. zoomicon->set_tooltip("Animation zoom.");
  2181. zoom = memnew( HSlider );
  2182. //hb->add_child(zoom);
  2183. zoom->set_step(0.01);
  2184. zoom->set_min(0.0);
  2185. zoom->set_max(2.0);
  2186. zoom->set_val(1.0);
  2187. zoom->set_h_size_flags(SIZE_EXPAND_FILL);
  2188. zoom->set_stretch_ratio(2);
  2189. hb->add_child(zoom);
  2190. zoom->connect("value_changed",this,"_scroll_changed");
  2191. zoom->set_tooltip("Animation zoom.");
  2192. hb->add_child( memnew( VSeparator ) );
  2193. Label *l = memnew( Label );
  2194. l->set_text("Len(s):");
  2195. hb->add_child(l);
  2196. length = memnew( SpinBox );
  2197. length->set_min(0.01);
  2198. length->set_max(10000);
  2199. length->set_step(0.01);
  2200. length->set_h_size_flags(SIZE_EXPAND_FILL);
  2201. length->set_stretch_ratio(1);
  2202. length->set_tooltip("Animation length (in seconds).");
  2203. hb->add_child(length);
  2204. length->connect("value_changed",this,"_animation_len_changed");
  2205. l = memnew( Label );
  2206. l->set_text("Step(s):");
  2207. hb->add_child(l);
  2208. step = memnew( SpinBox );
  2209. step->set_min(0.00);
  2210. step->set_max(128);
  2211. step->set_step(0.01);
  2212. step->set_val(0.0);
  2213. step->set_h_size_flags(SIZE_EXPAND_FILL);
  2214. step->set_stretch_ratio(1);
  2215. step->set_tooltip("Cursor step snap (in seconds).");
  2216. hb->add_child(step);
  2217. step->connect("value_changed",this,"_step_changed");
  2218. loop = memnew( ToolButton );
  2219. loop->set_toggle_mode(true);
  2220. loop->connect("pressed",this,"_animation_loop_changed");
  2221. hb->add_child(loop);
  2222. loop->set_tooltip("Enable/Disable looping in animation.");
  2223. hb->add_child( memnew( VSeparator ) );
  2224. edit_button = memnew( ToolButton );
  2225. edit_button->set_toggle_mode(true);
  2226. edit_button->set_focus_mode(FOCUS_NONE);
  2227. edit_button->set_disabled(true);
  2228. hb->add_child(edit_button);
  2229. edit_button->set_tooltip("Enable editing of individual keys by clicking them.");
  2230. move_up_button = memnew( ToolButton );
  2231. hb->add_child(move_up_button);
  2232. move_up_button->connect("pressed",this,"_menu_track",make_binds(TRACK_MENU_MOVE_UP));
  2233. move_up_button->set_focus_mode(FOCUS_NONE);
  2234. move_up_button->set_disabled(true);
  2235. move_up_button->set_tooltip("Move current track up.");
  2236. move_down_button = memnew( ToolButton );
  2237. hb->add_child(move_down_button);
  2238. move_down_button->connect("pressed",this,"_menu_track",make_binds(TRACK_MENU_MOVE_DOWN));
  2239. move_down_button->set_focus_mode(FOCUS_NONE);
  2240. move_down_button->set_disabled(true);
  2241. move_down_button->set_tooltip("Move current track dosn.");
  2242. remove_button = memnew( ToolButton );
  2243. hb->add_child(remove_button);
  2244. remove_button->connect("pressed",this,"_menu_track",make_binds(TRACK_MENU_REMOVE));
  2245. remove_button->set_focus_mode(FOCUS_NONE);
  2246. remove_button->set_disabled(true);
  2247. remove_button->set_tooltip("Remove selected track.");
  2248. /*keying = memnew( Button );
  2249. keying->set_toggle_mode(true);
  2250. //keying->set_text("Keys");
  2251. keying->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,60);
  2252. keying->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
  2253. keying->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_BEGIN,55);
  2254. keying->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  2255. //add_child(keying);
  2256. keying->connect("pressed",this,"_keying_toggled");
  2257. */
  2258. l = memnew( Label );
  2259. l->set_text("Base: ");
  2260. l->set_pos(Point2(0,3));
  2261. // dr_panel->add_child(l);
  2262. // menu->get_popup()->connect("item_pressed",this,"_menu_callback");
  2263. ec = memnew (EmptyControl);
  2264. ec->set_minsize(Size2(0,50));
  2265. add_child(ec);
  2266. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  2267. hb = memnew( HBoxContainer);
  2268. hb->set_area_as_parent_rect();
  2269. ec->add_child(hb);
  2270. hb->set_v_size_flags(SIZE_EXPAND_FILL);
  2271. track_editor = memnew( Control );
  2272. track_editor->connect("draw",this,"_track_editor_draw");
  2273. hb->add_child(track_editor);
  2274. track_editor->connect("input_event",this,"_track_editor_input_event");
  2275. track_editor->set_focus_mode(Control::FOCUS_ALL);
  2276. track_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2277. track_pos = memnew( Control );
  2278. track_pos->set_area_as_parent_rect();
  2279. track_pos->set_ignore_mouse(true);
  2280. track_editor->add_child(track_pos);
  2281. track_pos->connect("draw",this,"_track_pos_draw");
  2282. v_scroll = memnew( VScrollBar );
  2283. hb->add_child(v_scroll);
  2284. v_scroll->connect("value_changed",this,"_scroll_changed");
  2285. v_scroll->set_val(0);
  2286. h_scroll = memnew( HScrollBar );
  2287. h_scroll->connect("value_changed",this,"_scroll_changed");
  2288. add_child(h_scroll);
  2289. h_scroll->set_val(0);
  2290. track_name = memnew( LineEdit );
  2291. track_name->set_as_toplevel(true);
  2292. track_name->hide();
  2293. add_child(track_name);
  2294. track_name->connect("text_entered",this,"_track_name_changed");
  2295. track_menu = memnew( PopupMenu );
  2296. add_child(track_menu);
  2297. track_menu->connect("item_pressed",this,"_track_menu_selected");
  2298. last_idx =1;
  2299. _update_menu();
  2300. insert_confirm = memnew( ConfirmationDialog );
  2301. add_child(insert_confirm);
  2302. insert_confirm->connect("confirmed",this,"_confirm_insert_list");
  2303. EDITOR_DEF("animation_editor/confirm_insert_key",true);
  2304. click.click=ClickOver::CLICK_NONE;
  2305. name_column_ratio=0.3;
  2306. timeline_pos=0;
  2307. key_edit_dialog = memnew( PopupDialog );
  2308. key_editor = memnew( PropertyEditor );
  2309. add_child(key_edit_dialog);
  2310. key_editor->set_area_as_parent_rect();
  2311. key_editor->hide_top_label();
  2312. for(int i=0;i<4;i++)
  2313. key_editor->set_margin(Margin(i),5);
  2314. key_edit_dialog->add_child(key_editor);
  2315. key_edit = memnew( AnimationKeyEdit );
  2316. key_edit->undo_redo=undo_redo;
  2317. key_edit->ke_dialog=key_edit_dialog;
  2318. key_editor->edit(key_edit);
  2319. type_menu = memnew( PopupMenu );
  2320. add_child(type_menu);
  2321. for(int i=0;i<Variant::VARIANT_MAX;i++)
  2322. type_menu->add_item(Variant::get_type_name(Variant::Type(i)),i);
  2323. type_menu->connect("item_pressed",this,"_create_value_item");
  2324. keying=false;
  2325. insert_frame=0;
  2326. insert_query=false;
  2327. insert_queue=false;
  2328. editor_selection->connect("selection_changed",track_editor,"update");
  2329. scale_dialog = memnew( ConfirmationDialog );
  2330. VBoxContainer *vbc = memnew( VBoxContainer );
  2331. scale_dialog->add_child(vbc);
  2332. scale_dialog->set_child_rect(vbc);
  2333. scale = memnew( SpinBox );
  2334. scale->set_min(-99999);
  2335. scale->set_max(99999);
  2336. scale->set_step(0.001);
  2337. vbc->add_margin_child("Scale Ratio:",scale);
  2338. scale_dialog->connect("confirmed",this,"_scale");
  2339. add_child(scale_dialog);
  2340. }
  2341. AnimationKeyEditor::~AnimationKeyEditor() {
  2342. memdelete( key_edit );
  2343. }