animation_editor.cpp 116 KB

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