animation_editor.cpp 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995
  1. /*************************************************************************/
  2. /* animation_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. }
  1143. te_drawing = false;
  1144. }
  1145. void AnimationKeyEditor::_track_name_changed(const String &p_name) {
  1146. ERR_FAIL_COND(!animation.is_valid());
  1147. undo_redo->create_action(TTR("Anim Track Rename"));
  1148. undo_redo->add_do_method(animation.ptr(), "track_set_path", track_name_editing, p_name);
  1149. undo_redo->add_undo_method(animation.ptr(), "track_set_path", track_name_editing, animation->track_get_path(track_name_editing));
  1150. undo_redo->commit_action();
  1151. track_name->hide();
  1152. }
  1153. void AnimationKeyEditor::_track_menu_selected(int p_idx) {
  1154. ERR_FAIL_COND(!animation.is_valid());
  1155. if (interp_editing != -1) {
  1156. ERR_FAIL_INDEX(interp_editing, animation->get_track_count());
  1157. undo_redo->create_action(TTR("Anim Track Change Interpolation"));
  1158. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", interp_editing, p_idx);
  1159. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", interp_editing, animation->track_get_interpolation_type(interp_editing));
  1160. undo_redo->commit_action();
  1161. } else if (cont_editing != -1) {
  1162. ERR_FAIL_INDEX(cont_editing, animation->get_track_count());
  1163. undo_redo->create_action(TTR("Anim Track Change Value Mode"));
  1164. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", cont_editing, p_idx);
  1165. undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", cont_editing, animation->value_track_get_update_mode(cont_editing));
  1166. undo_redo->commit_action();
  1167. } else {
  1168. switch (p_idx) {
  1169. case RIGHT_MENU_DUPLICATE:
  1170. _anim_duplicate_keys();
  1171. break;
  1172. case RIGHT_MENU_DUPLICATE_TRANSPOSE:
  1173. _anim_duplicate_keys(true);
  1174. break;
  1175. case RIGHT_MENU_REMOVE:
  1176. _anim_delete_keys();
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. struct _AnimMoveRestore {
  1182. int track;
  1183. float time;
  1184. Variant key;
  1185. float transition;
  1186. };
  1187. void AnimationKeyEditor::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
  1188. if (!(animation == p_anim))
  1189. return;
  1190. //selection.clear();
  1191. _clear_selection();
  1192. }
  1193. void AnimationKeyEditor::_select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos) {
  1194. if (!(animation == p_anim))
  1195. return;
  1196. int idx = animation->track_find_key(p_track, p_pos, true);
  1197. ERR_FAIL_COND(idx < 0);
  1198. SelectedKey sk;
  1199. sk.track = p_track;
  1200. sk.key = idx;
  1201. KeyInfo ki;
  1202. ki.pos = p_pos;
  1203. selection.insert(sk, ki);
  1204. }
  1205. PropertyInfo AnimationKeyEditor::_find_hint_for_track(int p_idx, NodePath &r_base_path) {
  1206. r_base_path = NodePath();
  1207. ERR_FAIL_COND_V(!animation.is_valid(), PropertyInfo());
  1208. ERR_FAIL_INDEX_V(p_idx, animation->get_track_count(), PropertyInfo());
  1209. if (!root)
  1210. return PropertyInfo();
  1211. NodePath path = animation->track_get_path(p_idx);
  1212. if (!root->has_node_and_resource(path))
  1213. return PropertyInfo();
  1214. RES res;
  1215. Node *node = root->get_node_and_resource(path, res);
  1216. if (node) {
  1217. r_base_path = node->get_path();
  1218. }
  1219. String property = path.get_property();
  1220. if (property == "")
  1221. return PropertyInfo();
  1222. List<PropertyInfo> pinfo;
  1223. if (res.is_valid())
  1224. res->get_property_list(&pinfo);
  1225. else
  1226. node->get_property_list(&pinfo);
  1227. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1228. if (E->get().name == property)
  1229. return E->get();
  1230. }
  1231. return PropertyInfo();
  1232. }
  1233. void AnimationKeyEditor::_curve_transition_changed(float p_what) {
  1234. if (selection.size() == 0)
  1235. return;
  1236. if (selection.size() == 1)
  1237. undo_redo->create_action(TTR("Edit Node Curve"), UndoRedo::MERGE_ENDS);
  1238. else
  1239. undo_redo->create_action(TTR("Edit Selection Curve"), UndoRedo::MERGE_ENDS);
  1240. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1241. int track = E->key().track;
  1242. int key = E->key().key;
  1243. float prev_val = animation->track_get_key_transition(track, key);
  1244. undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", track, key, p_what);
  1245. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", track, key, prev_val);
  1246. }
  1247. undo_redo->commit_action();
  1248. }
  1249. void AnimationKeyEditor::_toggle_edit_curves() {
  1250. if (edit_button->is_pressed())
  1251. key_editor_tab->show();
  1252. else
  1253. key_editor_tab->hide();
  1254. }
  1255. bool AnimationKeyEditor::_edit_if_single_selection() {
  1256. if (selection.size() != 1) {
  1257. if (selection.size() == 0) {
  1258. curve_edit->set_mode(AnimationCurveEdit::MODE_DISABLED);
  1259. //print_line("disable");
  1260. } else {
  1261. curve_edit->set_mode(AnimationCurveEdit::MODE_MULTIPLE);
  1262. curve_edit->set_transition(1.0);
  1263. curve_edit->clear_multiples();
  1264. //add all
  1265. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1266. curve_edit->set_multiple(animation->track_get_key_transition(E->key().track, E->key().key));
  1267. }
  1268. //print_line("multiple");
  1269. }
  1270. return false;
  1271. }
  1272. curve_edit->set_mode(AnimationCurveEdit::MODE_SINGLE);
  1273. //print_line("regular");
  1274. int idx = selection.front()->key().track;
  1275. int key = selection.front()->key().key;
  1276. {
  1277. key_edit->animation = animation;
  1278. key_edit->track = idx;
  1279. key_edit->key_ofs = animation->track_get_key_time(idx, key);
  1280. key_edit->hint = _find_hint_for_track(idx, key_edit->base);
  1281. key_edit->notify_change();
  1282. curve_edit->set_transition(animation->track_get_key_transition(idx, key));
  1283. /*key_edit_dialog->set_size( Size2( 200,200) );
  1284. key_edit_dialog->set_pos( track_editor->get_global_pos() + ofs + mpos +Point2(-100,20));
  1285. key_edit_dialog->popup();*/
  1286. }
  1287. return true;
  1288. }
  1289. void AnimationKeyEditor::_anim_delete_keys() {
  1290. if (selection.size()) {
  1291. undo_redo->create_action(TTR("Anim Delete Keys"));
  1292. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1293. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  1294. 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));
  1295. }
  1296. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1297. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1298. undo_redo->commit_action();
  1299. //selection.clear();
  1300. accept_event();
  1301. _edit_if_single_selection();
  1302. }
  1303. }
  1304. void AnimationKeyEditor::_track_editor_input_event(const InputEvent &p_input) {
  1305. Control *te = track_editor;
  1306. Ref<StyleBox> style = get_stylebox("normal", "TextEdit");
  1307. if (!animation.is_valid()) {
  1308. return;
  1309. }
  1310. Size2 size = te->get_size() - style->get_minimum_size();
  1311. Size2 ofs = style->get_offset();
  1312. Ref<Font> font = te->get_font("font", "Tree");
  1313. int sep = get_constant("vseparation", "Tree");
  1314. int hsep = get_constant("hseparation", "Tree");
  1315. Ref<Texture> remove_icon = get_icon("Remove", "EditorIcons");
  1316. Ref<Texture> move_up_icon = get_icon("MoveUp", "EditorIcons");
  1317. Ref<Texture> move_down_icon = get_icon("MoveDown", "EditorIcons");
  1318. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  1319. Ref<Texture> hsize_icon = get_icon("Hsize", "EditorIcons");
  1320. Ref<Texture> add_key_icon = get_icon("TrackAddKey", "EditorIcons");
  1321. Ref<Texture> interp_icon[3] = {
  1322. get_icon("InterpRaw", "EditorIcons"),
  1323. get_icon("InterpLinear", "EditorIcons"),
  1324. get_icon("InterpCubic", "EditorIcons")
  1325. };
  1326. Ref<Texture> cont_icon[3] = {
  1327. get_icon("TrackContinuous", "EditorIcons"),
  1328. get_icon("TrackDiscrete", "EditorIcons"),
  1329. get_icon("TrackTrigger", "EditorIcons")
  1330. };
  1331. Ref<Texture> type_icon[3] = {
  1332. get_icon("KeyValue", "EditorIcons"),
  1333. get_icon("KeyXform", "EditorIcons"),
  1334. get_icon("KeyCall", "EditorIcons")
  1335. };
  1336. 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;
  1337. int h = font->get_height() + sep;
  1338. int fit = (size.height / h) - 1;
  1339. int total = animation->get_track_count();
  1340. if (total < fit) {
  1341. v_scroll->hide();
  1342. } else {
  1343. v_scroll->show();
  1344. v_scroll->set_max(total);
  1345. v_scroll->set_page(fit);
  1346. }
  1347. int settings_limit = size.width - right_separator_ofs;
  1348. int name_limit = settings_limit * name_column_ratio;
  1349. switch (p_input.type) {
  1350. case InputEvent::KEY: {
  1351. if (p_input.key.scancode == KEY_D && p_input.key.pressed && p_input.key.mod.command) {
  1352. if (p_input.key.mod.shift)
  1353. _menu_track(TRACK_MENU_DUPLICATE_TRANSPOSE);
  1354. else
  1355. _menu_track(TRACK_MENU_DUPLICATE);
  1356. accept_event();
  1357. } else if (p_input.key.scancode == KEY_DELETE && p_input.key.pressed && click.click == ClickOver::CLICK_NONE) {
  1358. _anim_delete_keys();
  1359. } else if (animation.is_valid() && animation->get_track_count() > 0) {
  1360. if (p_input.is_pressed() && (p_input.is_action("ui_up") || p_input.is_action("ui_page_up"))) {
  1361. if (p_input.is_action("ui_up"))
  1362. selected_track--;
  1363. if (v_scroll->is_visible() && p_input.is_action("ui_page_up"))
  1364. selected_track--;
  1365. if (selected_track < 0)
  1366. selected_track = 0;
  1367. if (v_scroll->is_visible()) {
  1368. if (v_scroll->get_val() > selected_track)
  1369. v_scroll->set_val(selected_track);
  1370. }
  1371. track_editor->update();
  1372. accept_event();
  1373. }
  1374. if (p_input.is_pressed() && (p_input.is_action("ui_down") || p_input.is_action("ui_page_down"))) {
  1375. if (p_input.is_action("ui_down"))
  1376. selected_track++;
  1377. else if (v_scroll->is_visible() && p_input.is_action("ui_page_down"))
  1378. selected_track += v_scroll->get_page();
  1379. if (selected_track >= animation->get_track_count())
  1380. selected_track = animation->get_track_count() - 1;
  1381. if (v_scroll->is_visible() && v_scroll->get_page() + v_scroll->get_val() < selected_track + 1) {
  1382. v_scroll->set_val(selected_track - v_scroll->get_page() + 1);
  1383. }
  1384. track_editor->update();
  1385. accept_event();
  1386. }
  1387. }
  1388. } break;
  1389. case InputEvent::MOUSE_BUTTON: {
  1390. const InputEventMouseButton &mb = p_input.mouse_button;
  1391. if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) {
  1392. if (mb.mod.command) {
  1393. zoom->set_val(zoom->get_val() + zoom->get_step());
  1394. } else {
  1395. v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() * mb.factor / 8);
  1396. }
  1397. }
  1398. if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) {
  1399. if (mb.mod.command) {
  1400. zoom->set_val(zoom->get_val() - zoom->get_step());
  1401. } else {
  1402. v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() * mb.factor / 8);
  1403. }
  1404. }
  1405. if (mb.button_index == BUTTON_WHEEL_RIGHT && mb.pressed) {
  1406. h_scroll->set_val(h_scroll->get_val() - h_scroll->get_page() * mb.factor / 8);
  1407. }
  1408. if (mb.button_index == BUTTON_WHEEL_LEFT && mb.pressed) {
  1409. v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() * mb.factor / 8);
  1410. }
  1411. if (mb.button_index == BUTTON_RIGHT && mb.pressed) {
  1412. Point2 mpos = Point2(mb.x, mb.y) - ofs;
  1413. if (selection.size() == 0) {
  1414. // Auto-select on right-click if nothing is selected
  1415. // Note: This code is pretty much duplicated from the left click code,
  1416. // both codes could be moved into a function to avoid the duplicated code.
  1417. Point2 mpos = Point2(mb.x, mb.y) - ofs;
  1418. if (mpos.y < h) {
  1419. return;
  1420. }
  1421. mpos.y -= h;
  1422. int idx = mpos.y / h;
  1423. idx += v_scroll->get_val();
  1424. if (idx < 0 || idx >= animation->get_track_count())
  1425. break;
  1426. if (mpos.x < name_limit) {
  1427. } else if (mpos.x < settings_limit) {
  1428. float pos = mpos.x - name_limit;
  1429. pos /= _get_zoom_scale();
  1430. pos += h_scroll->get_val();
  1431. float w_time = (type_icon[0]->get_width() / _get_zoom_scale()) / 2.0;
  1432. int kidx = animation->track_find_key(idx, pos);
  1433. int kidx_n = kidx + 1;
  1434. int key = -1;
  1435. if (kidx >= 0 && kidx < animation->track_get_key_count(idx)) {
  1436. float kpos = animation->track_get_key_time(idx, kidx);
  1437. if (ABS(pos - kpos) <= w_time) {
  1438. key = kidx;
  1439. }
  1440. }
  1441. if (key == -1 && kidx_n >= 0 && kidx_n < animation->track_get_key_count(idx)) {
  1442. float kpos = animation->track_get_key_time(idx, kidx_n);
  1443. if (ABS(pos - kpos) <= w_time) {
  1444. key = kidx_n;
  1445. }
  1446. }
  1447. if (key == -1) {
  1448. click.click = ClickOver::CLICK_SELECT_KEYS;
  1449. click.at = Point2(mb.x, mb.y);
  1450. click.to = click.at;
  1451. click.shift = mb.mod.shift;
  1452. selected_track = idx;
  1453. track_editor->update();
  1454. //drag select region
  1455. return;
  1456. }
  1457. SelectedKey sk;
  1458. sk.track = idx;
  1459. sk.key = key;
  1460. KeyInfo ki;
  1461. ki.pos = animation->track_get_key_time(idx, key);
  1462. click.shift = mb.mod.shift;
  1463. click.selk = sk;
  1464. if (!mb.mod.shift && !selection.has(sk))
  1465. _clear_selection();
  1466. selection.insert(sk, ki);
  1467. click.click = ClickOver::CLICK_MOVE_KEYS;
  1468. click.at = Point2(mb.x, mb.y);
  1469. click.to = click.at;
  1470. update();
  1471. selected_track = idx;
  1472. track_editor->update();
  1473. if (_edit_if_single_selection() && mb.mod.command) {
  1474. edit_button->set_pressed(true);
  1475. key_editor_tab->show();
  1476. }
  1477. }
  1478. }
  1479. if (selection.size()) {
  1480. // User has right clicked and we have a selection, show a popup menu with options
  1481. track_menu->clear();
  1482. track_menu->set_size(Point2(1, 1));
  1483. track_menu->add_item(TTR("Duplicate Selection"), RIGHT_MENU_DUPLICATE);
  1484. track_menu->add_item(TTR("Duplicate Transposed"), RIGHT_MENU_DUPLICATE_TRANSPOSE);
  1485. track_menu->add_item(TTR("Remove Selection"), RIGHT_MENU_REMOVE);
  1486. track_menu->set_pos(te->get_global_pos() + mpos);
  1487. interp_editing = -1;
  1488. cont_editing = -1;
  1489. track_menu->popup();
  1490. }
  1491. }
  1492. if (mb.button_index == BUTTON_LEFT && !(mb.button_mask & ~BUTTON_MASK_LEFT)) {
  1493. if (mb.pressed) {
  1494. Point2 mpos = Point2(mb.x, mb.y) - ofs;
  1495. if (mpos.y < h) {
  1496. if (mpos.x < name_limit && mpos.x > (name_limit - hsep - hsize_icon->get_width())) {
  1497. click.click = ClickOver::CLICK_RESIZE_NAMES;
  1498. click.at = Point2(mb.x, mb.y);
  1499. click.to = click.at;
  1500. click.at.y = name_limit;
  1501. }
  1502. if (mpos.x >= name_limit && mpos.x < settings_limit) {
  1503. //seek
  1504. //int zoomw = settings_limit-name_limit;
  1505. float scale = _get_zoom_scale();
  1506. float pos = h_scroll->get_val() + (mpos.x - name_limit) / scale;
  1507. if (animation->get_step())
  1508. pos = Math::stepify(pos, animation->get_step());
  1509. if (pos < 0)
  1510. pos = 0;
  1511. if (pos >= animation->get_length())
  1512. pos = animation->get_length();
  1513. timeline_pos = pos;
  1514. click.click = ClickOver::CLICK_DRAG_TIMELINE;
  1515. click.at = Point2(mb.x, mb.y);
  1516. click.to = click.at;
  1517. emit_signal("timeline_changed", pos, false);
  1518. }
  1519. return;
  1520. }
  1521. mpos.y -= h;
  1522. int idx = mpos.y / h;
  1523. idx += v_scroll->get_val();
  1524. if (idx < 0)
  1525. break;
  1526. if (idx >= animation->get_track_count()) {
  1527. if (mpos.x >= name_limit && mpos.x < settings_limit) {
  1528. click.click = ClickOver::CLICK_SELECT_KEYS;
  1529. click.at = Point2(mb.x, mb.y);
  1530. click.to = click.at;
  1531. //drag select region
  1532. }
  1533. break;
  1534. }
  1535. if (mpos.x < name_limit) {
  1536. //name column
  1537. // area
  1538. if (idx != selected_track) {
  1539. selected_track = idx;
  1540. track_editor->update();
  1541. break;
  1542. }
  1543. Rect2 area(ofs.x, ofs.y + ((int(mpos.y) / h) + 1) * h, name_limit, h);
  1544. track_name->set_text(animation->track_get_path(idx));
  1545. track_name->set_pos(te->get_global_pos() + area.pos);
  1546. track_name->set_size(area.size);
  1547. track_name->show_modal();
  1548. track_name->grab_focus();
  1549. track_name->select_all();
  1550. track_name_editing = idx;
  1551. } else if (mpos.x < settings_limit) {
  1552. float pos = mpos.x - name_limit;
  1553. pos /= _get_zoom_scale();
  1554. pos += h_scroll->get_val();
  1555. float w_time = (type_icon[0]->get_width() / _get_zoom_scale()) / 2.0;
  1556. int kidx = animation->track_find_key(idx, pos);
  1557. int kidx_n = kidx + 1;
  1558. int key = -1;
  1559. if (kidx >= 0 && kidx < animation->track_get_key_count(idx)) {
  1560. float kpos = animation->track_get_key_time(idx, kidx);
  1561. if (ABS(pos - kpos) <= w_time) {
  1562. key = kidx;
  1563. }
  1564. }
  1565. if (key == -1 && kidx_n >= 0 && kidx_n < animation->track_get_key_count(idx)) {
  1566. float kpos = animation->track_get_key_time(idx, kidx_n);
  1567. if (ABS(pos - kpos) <= w_time) {
  1568. key = kidx_n;
  1569. }
  1570. }
  1571. if (key == -1) {
  1572. click.click = ClickOver::CLICK_SELECT_KEYS;
  1573. click.at = Point2(mb.x, mb.y);
  1574. click.to = click.at;
  1575. click.shift = mb.mod.shift;
  1576. selected_track = idx;
  1577. track_editor->update();
  1578. //drag select region
  1579. return;
  1580. }
  1581. SelectedKey sk;
  1582. sk.track = idx;
  1583. sk.key = key;
  1584. KeyInfo ki;
  1585. ki.pos = animation->track_get_key_time(idx, key);
  1586. click.shift = mb.mod.shift;
  1587. click.selk = sk;
  1588. if (!mb.mod.shift && !selection.has(sk))
  1589. _clear_selection();
  1590. selection.insert(sk, ki);
  1591. click.click = ClickOver::CLICK_MOVE_KEYS;
  1592. click.at = Point2(mb.x, mb.y);
  1593. click.to = click.at;
  1594. update();
  1595. selected_track = idx;
  1596. track_editor->update();
  1597. if (_edit_if_single_selection() && mb.mod.command) {
  1598. edit_button->set_pressed(true);
  1599. key_editor_tab->show();
  1600. }
  1601. } else {
  1602. //button column
  1603. int ofsx = size.width - mpos.x;
  1604. if (ofsx < 0)
  1605. break;
  1606. /*
  1607. if (ofsx < remove_icon->get_width()) {
  1608. undo_redo->create_action("Remove Anim Track");
  1609. undo_redo->add_do_method(animation.ptr(),"remove_track",idx);
  1610. undo_redo->add_undo_method(animation.ptr(),"add_track",animation->track_get_type(idx),idx);
  1611. undo_redo->add_undo_method(animation.ptr(),"track_set_path",idx,animation->track_get_path(idx));
  1612. //todo interpolation
  1613. for(int i=0;i<animation->track_get_key_count(idx);i++) {
  1614. Variant v = animation->track_get_key_value(idx,i);
  1615. float time = animation->track_get_key_time(idx,i);
  1616. float trans = animation->track_get_key_transition(idx,i);
  1617. undo_redo->add_undo_method(animation.ptr(),"track_insert_key",idx,time,v);
  1618. undo_redo->add_undo_method(animation.ptr(),"track_set_key_transition",idx,i,trans);
  1619. }
  1620. undo_redo->add_undo_method(animation.ptr(),"track_set_interpolation_type",idx,animation->track_get_interpolation_type(idx));
  1621. if (animation->track_get_type(idx)==Animation::TYPE_VALUE) {
  1622. undo_redo->add_undo_method(animation.ptr(),"value_track_set_continuous",idx,animation->value_track_is_continuous(idx));
  1623. }
  1624. undo_redo->commit_action();
  1625. return;
  1626. }
  1627. ofsx-=hsep+remove_icon->get_width();
  1628. if (ofsx < move_down_icon->get_width()) {
  1629. if (idx < animation->get_track_count() -1) {
  1630. undo_redo->create_action("Move Anim Track Down");
  1631. undo_redo->add_do_method(animation.ptr(),"track_move_up",idx);
  1632. undo_redo->add_undo_method(animation.ptr(),"track_move_down",idx+1);
  1633. undo_redo->commit_action();
  1634. }
  1635. return;
  1636. }
  1637. ofsx-=hsep+move_down_icon->get_width();
  1638. if (ofsx < move_up_icon->get_width()) {
  1639. if (idx >0) {
  1640. undo_redo->create_action("Move Anim Track Up");
  1641. undo_redo->add_do_method(animation.ptr(),"track_move_down",idx);
  1642. undo_redo->add_undo_method(animation.ptr(),"track_move_up",idx-1);
  1643. undo_redo->commit_action();
  1644. }
  1645. return;
  1646. }
  1647. ofsx-=hsep*3+move_up_icon->get_width();
  1648. */
  1649. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep * 2) {
  1650. track_menu->clear();
  1651. track_menu->set_size(Point2(1, 1));
  1652. static const char *interp_name[3] = { "Nearest", "Linear", "Cubic" };
  1653. for (int i = 0; i < 3; i++) {
  1654. track_menu->add_icon_item(interp_icon[i], interp_name[i]);
  1655. }
  1656. 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() ;
  1657. int popup_y = ofs.y + ((int(mpos.y) / h) + 2) * h;
  1658. int popup_x = ofs.x + size.width - lofs;
  1659. track_menu->set_pos(te->get_global_pos() + Point2(popup_x, popup_y));
  1660. interp_editing = idx;
  1661. cont_editing = -1;
  1662. track_menu->popup();
  1663. return;
  1664. }
  1665. ofsx -= hsep * 2 + interp_icon[0]->get_width() + down_icon->get_width();
  1666. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width()) {
  1667. track_menu->clear();
  1668. track_menu->set_size(Point2(1, 1));
  1669. String cont_name[3] = { TTR("Continuous"), TTR("Discrete"), TTR("Trigger") };
  1670. for (int i = 0; i < 3; i++) {
  1671. track_menu->add_icon_item(cont_icon[i], cont_name[i]);
  1672. }
  1673. int lofs = settings_limit;
  1674. int popup_y = ofs.y + ((int(mpos.y) / h) + 2) * h;
  1675. int popup_x = ofs.x + lofs;
  1676. track_menu->set_pos(te->get_global_pos() + Point2(popup_x, popup_y));
  1677. interp_editing = -1;
  1678. cont_editing = idx;
  1679. track_menu->popup();
  1680. return;
  1681. }
  1682. ofsx -= hsep * 3 + cont_icon[0]->get_width() + down_icon->get_width();
  1683. if (ofsx < add_key_icon->get_width()) {
  1684. Animation::TrackType tt = animation->track_get_type(idx);
  1685. float pos = timeline_pos;
  1686. int existing = animation->track_find_key(idx, pos, true);
  1687. Variant newval;
  1688. if (tt == Animation::TYPE_TRANSFORM) {
  1689. Dictionary d;
  1690. d["loc"] = Vector3();
  1691. d["rot"] = Quat();
  1692. d["scale"] = Vector3();
  1693. newval = d;
  1694. } else if (tt == Animation::TYPE_METHOD) {
  1695. Dictionary d;
  1696. d["method"] = "";
  1697. d["args"] = Vector<Variant>();
  1698. newval = d;
  1699. } else if (tt == Animation::TYPE_VALUE) {
  1700. NodePath np;
  1701. PropertyInfo inf = _find_hint_for_track(idx, np);
  1702. if (inf.type != Variant::NIL) {
  1703. Variant::CallError err;
  1704. newval = Variant::construct(inf.type, NULL, 0, err);
  1705. }
  1706. if (newval.get_type() == Variant::NIL) {
  1707. //popup a new type
  1708. cvi_track = idx;
  1709. cvi_pos = pos;
  1710. type_menu->set_pos(get_global_pos() + mpos + ofs);
  1711. type_menu->popup();
  1712. return;
  1713. }
  1714. }
  1715. undo_redo->create_action(TTR("Anim Add Key"));
  1716. undo_redo->add_do_method(animation.ptr(), "track_insert_key", idx, pos, newval, 1);
  1717. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_pos", idx, pos);
  1718. if (existing != -1) {
  1719. Variant v = animation->track_get_key_value(idx, existing);
  1720. float trans = animation->track_get_key_transition(idx, existing);
  1721. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", idx, pos, v, trans);
  1722. }
  1723. undo_redo->commit_action();
  1724. return;
  1725. }
  1726. }
  1727. } else {
  1728. switch (click.click) {
  1729. case ClickOver::CLICK_SELECT_KEYS: {
  1730. float zoom_scale = _get_zoom_scale();
  1731. float keys_from = h_scroll->get_val();
  1732. float keys_to = keys_from + (settings_limit - name_limit) / zoom_scale;
  1733. float from_time = keys_from + (click.at.x - (name_limit + ofs.x)) / zoom_scale;
  1734. float to_time = keys_from + (click.to.x - (name_limit + ofs.x)) / zoom_scale;
  1735. if (to_time < from_time)
  1736. SWAP(from_time, to_time);
  1737. if (from_time > keys_to || to_time < keys_from)
  1738. break;
  1739. if (from_time < keys_from)
  1740. from_time = keys_from;
  1741. if (to_time >= keys_to)
  1742. to_time = keys_to;
  1743. int from_track = int(click.at.y - ofs.y - h - sep) / h + v_scroll->get_val();
  1744. int to_track = int(click.to.y - ofs.y - h - sep) / h + v_scroll->get_val();
  1745. int from_mod = int(click.at.y - ofs.y - sep) % h;
  1746. int to_mod = int(click.to.y - ofs.y - sep) % h;
  1747. if (to_track < from_track) {
  1748. SWAP(from_track, to_track);
  1749. SWAP(from_mod, to_mod);
  1750. }
  1751. if ((from_mod > (h / 2)) && ((click.at.y - ofs.y) >= (h + sep))) {
  1752. from_track++;
  1753. }
  1754. if (to_mod < h / 2) {
  1755. to_track--;
  1756. }
  1757. if (from_track > to_track) {
  1758. if (!click.shift)
  1759. _clear_selection();
  1760. _edit_if_single_selection();
  1761. break;
  1762. }
  1763. int tracks_from = v_scroll->get_val();
  1764. int tracks_to = v_scroll->get_val() + fit - 1;
  1765. if (tracks_to >= animation->get_track_count())
  1766. tracks_to = animation->get_track_count() - 1;
  1767. tracks_from = 0;
  1768. tracks_to = animation->get_track_count() - 1;
  1769. if (to_track > tracks_to)
  1770. to_track = tracks_to;
  1771. if (from_track < tracks_from)
  1772. from_track = tracks_from;
  1773. if (from_track > tracks_to || to_track < tracks_from) {
  1774. if (!click.shift)
  1775. _clear_selection();
  1776. _edit_if_single_selection();
  1777. break;
  1778. }
  1779. if (!click.shift)
  1780. _clear_selection();
  1781. int higher_track = 0x7FFFFFFF;
  1782. for (int i = from_track; i <= to_track; i++) {
  1783. int kc = animation->track_get_key_count(i);
  1784. for (int j = 0; j < kc; j++) {
  1785. float t = animation->track_get_key_time(i, j);
  1786. if (t < from_time)
  1787. continue;
  1788. if (t > to_time)
  1789. break;
  1790. if (i < higher_track)
  1791. higher_track = i;
  1792. SelectedKey sk;
  1793. sk.track = i;
  1794. sk.key = j;
  1795. KeyInfo ki;
  1796. ki.pos = t;
  1797. selection[sk] = ki;
  1798. }
  1799. }
  1800. if (higher_track != 0x7FFFFFFF) {
  1801. selected_track = higher_track;
  1802. track_editor->update();
  1803. }
  1804. _edit_if_single_selection();
  1805. } break;
  1806. case ClickOver::CLICK_MOVE_KEYS: {
  1807. if (selection.empty())
  1808. break;
  1809. if (click.at == click.to) {
  1810. if (!click.shift) {
  1811. KeyInfo ki = selection[click.selk];
  1812. _clear_selection();
  1813. selection[click.selk] = ki;
  1814. _edit_if_single_selection();
  1815. }
  1816. break;
  1817. }
  1818. float from_t = 1e20;
  1819. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  1820. float t = animation->track_get_key_time(E->key().track, E->key().key);
  1821. if (t < from_t)
  1822. from_t = t;
  1823. }
  1824. float motion = from_t + (click.to.x - click.at.x) / _get_zoom_scale();
  1825. if (step->get_val())
  1826. motion = Math::stepify(motion, step->get_val());
  1827. undo_redo->create_action(TTR("Anim Move Keys"));
  1828. List<_AnimMoveRestore> to_restore;
  1829. // 1-remove the keys
  1830. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1831. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  1832. }
  1833. // 2- remove overlapped keys
  1834. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1835. float newtime = E->get().pos - from_t + motion;
  1836. int idx = animation->track_find_key(E->key().track, newtime, true);
  1837. if (idx == -1)
  1838. continue;
  1839. SelectedKey sk;
  1840. sk.key = idx;
  1841. sk.track = E->key().track;
  1842. if (selection.has(sk))
  1843. continue; //already in selection, don't save
  1844. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_pos", E->key().track, newtime);
  1845. _AnimMoveRestore amr;
  1846. amr.key = animation->track_get_key_value(E->key().track, idx);
  1847. amr.track = E->key().track;
  1848. amr.time = newtime;
  1849. amr.transition = animation->track_get_key_transition(E->key().track, idx);
  1850. to_restore.push_back(amr);
  1851. }
  1852. // 3-move the keys (re insert them)
  1853. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1854. float newpos = E->get().pos - from_t + motion;
  1855. //if (newpos<0)
  1856. // continue; //no add at the begining
  1857. 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));
  1858. }
  1859. // 4-(undo) remove inserted keys
  1860. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1861. float newpos = E->get().pos + -from_t + motion;
  1862. //if (newpos<0)
  1863. // continue; //no remove what no inserted
  1864. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_pos", E->key().track, newpos);
  1865. }
  1866. // 5-(undo) reinsert keys
  1867. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1868. 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));
  1869. }
  1870. // 6-(undo) reinsert overlapped keys
  1871. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  1872. _AnimMoveRestore &amr = E->get();
  1873. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  1874. }
  1875. // 6-(undo) reinsert overlapped keys
  1876. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  1877. _AnimMoveRestore &amr = E->get();
  1878. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  1879. }
  1880. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1881. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1882. // 7-reselect
  1883. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  1884. float oldpos = E->get().pos;
  1885. float newpos = oldpos - from_t + motion;
  1886. //if (newpos>=0)
  1887. undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
  1888. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
  1889. }
  1890. undo_redo->commit_action();
  1891. _edit_if_single_selection();
  1892. } break;
  1893. default: {
  1894. }
  1895. }
  1896. //button released
  1897. click.click = ClickOver::CLICK_NONE;
  1898. track_editor->update();
  1899. }
  1900. }
  1901. } break;
  1902. case InputEvent::MOUSE_MOTION: {
  1903. const InputEventMouseMotion &mb = p_input.mouse_motion;
  1904. mouse_over.over = MouseOver::OVER_NONE;
  1905. mouse_over.track = -1;
  1906. te->update();
  1907. track_editor->set_tooltip("");
  1908. if (!track_editor->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  1909. track_editor->call_deferred("grab_focus");
  1910. if (click.click != ClickOver::CLICK_NONE) {
  1911. switch (click.click) {
  1912. case ClickOver::CLICK_RESIZE_NAMES: {
  1913. float base = click.at.y;
  1914. float clickp = click.at.x - ofs.x;
  1915. float dif = base - clickp;
  1916. float target = mb.x + dif - ofs.x;
  1917. float ratio = target / settings_limit;
  1918. if (ratio > 0.9)
  1919. ratio = 0.9;
  1920. else if (ratio < 0.2)
  1921. ratio = 0.2;
  1922. name_column_ratio = ratio;
  1923. } break;
  1924. case ClickOver::CLICK_DRAG_TIMELINE: {
  1925. Point2 mpos = Point2(mb.x, mb.y) - ofs;
  1926. /*
  1927. if (mpos.x<name_limit)
  1928. mpos.x=name_limit;
  1929. if (mpos.x>settings_limit)
  1930. mpos.x=settings_limit;
  1931. */
  1932. //int zoomw = settings_limit-name_limit;
  1933. float scale = _get_zoom_scale();
  1934. float pos = h_scroll->get_val() + (mpos.x - name_limit) / scale;
  1935. if (animation->get_step()) {
  1936. pos = Math::stepify(pos, animation->get_step());
  1937. }
  1938. if (pos < 0)
  1939. pos = 0;
  1940. if (pos >= animation->get_length())
  1941. pos = animation->get_length();
  1942. if (pos < h_scroll->get_val()) {
  1943. h_scroll->set_val(pos);
  1944. } else if (pos > h_scroll->get_val() + (settings_limit - name_limit) / scale) {
  1945. h_scroll->set_val(pos - (settings_limit - name_limit) / scale);
  1946. }
  1947. timeline_pos = pos;
  1948. emit_signal("timeline_changed", pos, true);
  1949. } break;
  1950. case ClickOver::CLICK_SELECT_KEYS: {
  1951. click.to = Point2(mb.x, mb.y);
  1952. if (click.to.y < h && click.at.y > h && mb.relative_y < 0) {
  1953. float prev = v_scroll->get_val();
  1954. v_scroll->set_val(v_scroll->get_val() - 1);
  1955. if (prev != v_scroll->get_val())
  1956. click.at.y += h;
  1957. }
  1958. if (click.to.y > size.height && click.at.y < size.height && mb.relative_y > 0) {
  1959. float prev = v_scroll->get_val();
  1960. v_scroll->set_val(v_scroll->get_val() + 1);
  1961. if (prev != v_scroll->get_val())
  1962. click.at.y -= h;
  1963. }
  1964. } break;
  1965. case ClickOver::CLICK_MOVE_KEYS: {
  1966. click.to = Point2(mb.x, mb.y);
  1967. } break;
  1968. default: {
  1969. }
  1970. }
  1971. return;
  1972. } else if (mb.button_mask & BUTTON_MASK_MIDDLE) {
  1973. int rel = mb.relative_x;
  1974. float relf = rel / _get_zoom_scale();
  1975. h_scroll->set_val(h_scroll->get_val() - relf);
  1976. }
  1977. if (mb.button_mask == 0) {
  1978. Point2 mpos = Point2(mb.x, mb.y) - ofs;
  1979. if (mpos.y < h) {
  1980. #if 0
  1981. //seek
  1982. //int zoomw = settings_limit-name_limit;
  1983. float scale = _get_zoom_scale();
  1984. float pos = h_scroll->get_val() + (mpos.y-name_limit) / scale;
  1985. if (pos<0 )
  1986. pos=0;
  1987. if (pos>=animation->get_length())
  1988. pos=animation->get_length();
  1989. timeline->set_val(pos);
  1990. #endif
  1991. return;
  1992. }
  1993. mpos.y -= h;
  1994. int idx = mpos.y / h;
  1995. idx += v_scroll->get_val();
  1996. if (idx < 0 || idx >= animation->get_track_count())
  1997. break;
  1998. mouse_over.track = idx;
  1999. if (mpos.x < name_limit) {
  2000. //name column
  2001. mouse_over.over = MouseOver::OVER_NAME;
  2002. } else if (mpos.x < settings_limit) {
  2003. float pos = mpos.x - name_limit;
  2004. pos /= _get_zoom_scale();
  2005. pos += h_scroll->get_val();
  2006. float w_time = (type_icon[0]->get_width() / _get_zoom_scale()) / 2.0;
  2007. int kidx = animation->track_find_key(idx, pos);
  2008. int kidx_n = kidx + 1;
  2009. bool found = false;
  2010. if (kidx >= 0 && kidx < animation->track_get_key_count(idx)) {
  2011. float kpos = animation->track_get_key_time(idx, kidx);
  2012. if (ABS(pos - kpos) <= w_time) {
  2013. mouse_over.over = MouseOver::OVER_KEY;
  2014. mouse_over.track = idx;
  2015. mouse_over.over_key = kidx;
  2016. found = true;
  2017. }
  2018. }
  2019. if (!found && kidx_n >= 0 && kidx_n < animation->track_get_key_count(idx)) {
  2020. float kpos = animation->track_get_key_time(idx, kidx_n);
  2021. if (ABS(pos - kpos) <= w_time) {
  2022. mouse_over.over = MouseOver::OVER_KEY;
  2023. mouse_over.track = idx;
  2024. mouse_over.over_key = kidx_n;
  2025. found = true;
  2026. }
  2027. }
  2028. if (found) {
  2029. String text;
  2030. text = "time: " + rtos(animation->track_get_key_time(idx, mouse_over.over_key)) + "\n";
  2031. switch (animation->track_get_type(idx)) {
  2032. case Animation::TYPE_TRANSFORM: {
  2033. Dictionary d = animation->track_get_key_value(idx, mouse_over.over_key);
  2034. if (d.has("loc"))
  2035. text += "loc: " + String(d["loc"]) + "\n";
  2036. if (d.has("rot"))
  2037. text += "rot: " + String(d["rot"]) + "\n";
  2038. if (d.has("scale"))
  2039. text += "scale: " + String(d["scale"]) + "\n";
  2040. } break;
  2041. case Animation::TYPE_VALUE: {
  2042. Variant v = animation->track_get_key_value(idx, mouse_over.over_key);
  2043. //text+="value: "+String(v)+"\n";
  2044. bool prop_exists = false;
  2045. Variant::Type valid_type = Variant::NIL;
  2046. Object *obj = NULL;
  2047. RES res;
  2048. Node *node = root->get_node_and_resource(animation->track_get_path(idx), res);
  2049. if (res.is_valid()) {
  2050. obj = res.ptr();
  2051. } else if (node) {
  2052. obj = node;
  2053. }
  2054. if (obj) {
  2055. valid_type = obj->get_static_property_type(animation->track_get_path(idx).get_property(), &prop_exists);
  2056. }
  2057. text += "type: " + Variant::get_type_name(v.get_type()) + "\n";
  2058. if (prop_exists && !Variant::can_convert(v.get_type(), valid_type)) {
  2059. text += "value: " + String(v) + " (Invalid, expected type: " + Variant::get_type_name(valid_type) + ")\n";
  2060. } else {
  2061. text += "value: " + String(v) + "\n";
  2062. }
  2063. } break;
  2064. case Animation::TYPE_METHOD: {
  2065. Dictionary d = animation->track_get_key_value(idx, mouse_over.over_key);
  2066. if (d.has("method"))
  2067. text += String(d["method"]);
  2068. text += "(";
  2069. Vector<Variant> args;
  2070. if (d.has("args"))
  2071. args = d["args"];
  2072. for (int i = 0; i < args.size(); i++) {
  2073. if (i > 0)
  2074. text += ", ";
  2075. text += String(args[i]);
  2076. }
  2077. text += ")\n";
  2078. } break;
  2079. }
  2080. text += "easing: " + rtos(animation->track_get_key_transition(idx, mouse_over.over_key));
  2081. track_editor->set_tooltip(text);
  2082. return;
  2083. }
  2084. } else {
  2085. //button column
  2086. int ofsx = size.width - mpos.x;
  2087. if (ofsx < 0)
  2088. break;
  2089. /*
  2090. if (ofsx < remove_icon->get_width()) {
  2091. mouse_over.over=MouseOver::OVER_REMOVE;
  2092. return;
  2093. }
  2094. ofsx-=hsep+remove_icon->get_width();
  2095. if (ofsx < move_down_icon->get_width()) {
  2096. mouse_over.over=MouseOver::OVER_DOWN;
  2097. return;
  2098. }
  2099. ofsx-=hsep+move_down_icon->get_width();
  2100. if (ofsx < move_up_icon->get_width()) {
  2101. mouse_over.over=MouseOver::OVER_UP;
  2102. return;
  2103. }
  2104. ofsx-=hsep*3+move_up_icon->get_width();
  2105. */
  2106. if (ofsx < down_icon->get_width() + interp_icon[0]->get_width() + hsep * 2) {
  2107. mouse_over.over = MouseOver::OVER_INTERP;
  2108. return;
  2109. }
  2110. ofsx -= hsep * 2 + interp_icon[0]->get_width() + down_icon->get_width();
  2111. if (ofsx < down_icon->get_width() + cont_icon[0]->get_width() + hsep * 3) {
  2112. mouse_over.over = MouseOver::OVER_VALUE;
  2113. return;
  2114. }
  2115. ofsx -= hsep * 3 + cont_icon[0]->get_width() + down_icon->get_width();
  2116. if (ofsx < add_key_icon->get_width()) {
  2117. mouse_over.over = MouseOver::OVER_ADD_KEY;
  2118. return;
  2119. }
  2120. }
  2121. }
  2122. } break;
  2123. }
  2124. }
  2125. void AnimationKeyEditor::_notification(int p_what) {
  2126. switch (p_what) {
  2127. case NOTIFICATION_VISIBILITY_CHANGED: {
  2128. EditorNode::get_singleton()->update_keying();
  2129. emit_signal("keying_changed");
  2130. } break;
  2131. case NOTIFICATION_ENTER_TREE: {
  2132. key_editor->edit(key_edit);
  2133. zoomicon->set_texture(get_icon("Zoom", "EditorIcons"));
  2134. menu_add_track->set_icon(get_icon("AddTrack", "EditorIcons"));
  2135. menu_add_track->get_popup()->add_icon_item(get_icon("KeyValue", "EditorIcons"), "Add Normal Track", ADD_TRACK_MENU_ADD_VALUE_TRACK);
  2136. menu_add_track->get_popup()->add_icon_item(get_icon("KeyXform", "EditorIcons"), "Add Transform Track", ADD_TRACK_MENU_ADD_TRANSFORM_TRACK);
  2137. menu_add_track->get_popup()->add_icon_item(get_icon("KeyCall", "EditorIcons"), "Add Call Func Track", ADD_TRACK_MENU_ADD_CALL_TRACK);
  2138. menu_track->set_icon(get_icon("Tools", "EditorIcons"));
  2139. menu_track->get_popup()->add_item(TTR("Scale Selection"), TRACK_MENU_SCALE);
  2140. menu_track->get_popup()->add_item(TTR("Scale From Cursor"), TRACK_MENU_SCALE_PIVOT);
  2141. menu_track->get_popup()->add_separator();
  2142. menu_track->get_popup()->add_item(TTR("Duplicate Selection"), TRACK_MENU_DUPLICATE);
  2143. menu_track->get_popup()->add_item(TTR("Duplicate Transposed"), TRACK_MENU_DUPLICATE_TRANSPOSE);
  2144. menu_track->get_popup()->add_separator();
  2145. menu_track->get_popup()->add_item(TTR("Goto Next Step"), TRACK_MENU_NEXT_STEP, KEY_MASK_CMD | KEY_RIGHT);
  2146. menu_track->get_popup()->add_item(TTR("Goto Prev Step"), TRACK_MENU_PREV_STEP, KEY_MASK_CMD | KEY_LEFT);
  2147. menu_track->get_popup()->add_separator();
  2148. PopupMenu *tpp = memnew(PopupMenu);
  2149. tpp->add_item(TTR("Linear"), TRACK_MENU_SET_ALL_TRANS_LINEAR);
  2150. tpp->add_item(TTR("Constant"), TRACK_MENU_SET_ALL_TRANS_CONSTANT);
  2151. tpp->add_item(TTR("In"), TRACK_MENU_SET_ALL_TRANS_IN);
  2152. tpp->add_item(TTR("Out"), TRACK_MENU_SET_ALL_TRANS_OUT);
  2153. tpp->add_item(TTR("In-Out"), TRACK_MENU_SET_ALL_TRANS_INOUT);
  2154. tpp->add_item(TTR("Out-In"), TRACK_MENU_SET_ALL_TRANS_OUTIN);
  2155. tpp->set_name(TTR("Transitions"));
  2156. tpp->connect("item_pressed", this, "_menu_track");
  2157. optimize_dialog->connect("confirmed", this, "_animation_optimize");
  2158. menu_track->get_popup()->add_child(tpp);
  2159. //menu_track->get_popup()->add_submenu_item("Set Transitions..","Transitions");
  2160. //menu_track->get_popup()->add_separator();
  2161. menu_track->get_popup()->add_item(TTR("Optimize Animation"), TRACK_MENU_OPTIMIZE);
  2162. menu_track->get_popup()->add_item(TTR("Clean-Up Animation"), TRACK_MENU_CLEAN_UP);
  2163. curve_linear->set_icon(get_icon("CurveLinear", "EditorIcons"));
  2164. curve_in->set_icon(get_icon("CurveIn", "EditorIcons"));
  2165. curve_out->set_icon(get_icon("CurveOut", "EditorIcons"));
  2166. curve_inout->set_icon(get_icon("CurveInOut", "EditorIcons"));
  2167. curve_outin->set_icon(get_icon("CurveOutIn", "EditorIcons"));
  2168. curve_constant->set_icon(get_icon("CurveConstant", "EditorIcons"));
  2169. curve_linear->connect("pressed", this, "_menu_track", varray(CURVE_SET_LINEAR));
  2170. curve_in->connect("pressed", this, "_menu_track", varray(CURVE_SET_IN));
  2171. curve_out->connect("pressed", this, "_menu_track", varray(CURVE_SET_OUT));
  2172. curve_inout->connect("pressed", this, "_menu_track", varray(CURVE_SET_INOUT));
  2173. curve_outin->connect("pressed", this, "_menu_track", varray(CURVE_SET_OUTIN));
  2174. curve_constant->connect("pressed", this, "_menu_track", varray(CURVE_SET_CONSTANT));
  2175. move_up_button->set_icon(get_icon("MoveUp", "EditorIcons"));
  2176. move_down_button->set_icon(get_icon("MoveDown", "EditorIcons"));
  2177. remove_button->set_icon(get_icon("Remove", "EditorIcons"));
  2178. edit_button->set_icon(get_icon("EditKey", "EditorIcons"));
  2179. edit_button->connect("pressed", this, "_toggle_edit_curves");
  2180. loop->set_icon(get_icon("Loop", "EditorIcons"));
  2181. curve_edit->connect("transition_changed", this, "_curve_transition_changed");
  2182. //edit_button->add_color_override("font_color",get_color("font_color","Tree"));
  2183. //edit_button->add_color_override("font_color_hover",get_color("font_color","Tree"));
  2184. {
  2185. right_data_size_cache = 0;
  2186. int hsep = get_constant("hseparation", "Tree");
  2187. Ref<Texture> remove_icon = get_icon("Remove", "EditorIcons");
  2188. Ref<Texture> move_up_icon = get_icon("MoveUp", "EditorIcons");
  2189. Ref<Texture> move_down_icon = get_icon("MoveDown", "EditorIcons");
  2190. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  2191. Ref<Texture> add_key_icon = get_icon("TrackAddKey", "EditorIcons");
  2192. Ref<Texture> interp_icon[3] = {
  2193. get_icon("InterpRaw", "EditorIcons"),
  2194. get_icon("InterpLinear", "EditorIcons"),
  2195. get_icon("InterpCubic", "EditorIcons")
  2196. };
  2197. Ref<Texture> cont_icon[3] = {
  2198. get_icon("TrackContinuous", "EditorIcons"),
  2199. get_icon("TrackDiscrete", "EditorIcons"),
  2200. get_icon("TrackTrigger", "EditorIcons")
  2201. };
  2202. //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;
  2203. 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;
  2204. }
  2205. call_select->connect("selected", this, "_add_call_track");
  2206. // rename_anim->set_icon( get_icon("Rename","EditorIcons") );
  2207. /*
  2208. edit_anim->set_icon( get_icon("Edit","EditorIcons") );
  2209. blend_anim->set_icon( get_icon("Blend","EditorIcons") );
  2210. play->set_icon( get_icon("Play","EditorIcons") );
  2211. stop->set_icon( get_icon("Stop","EditorIcons") );
  2212. pause->set_icon( get_icon("Pause","EditorIcons") );
  2213. */
  2214. // menu->set_icon(get_icon("Animation","EditorIcons"));
  2215. // play->set_icon(get_icon("AnimationPlay","EditorIcons"));
  2216. //menu->set_icon(get_icon("Animation","EditorIcons"));
  2217. _update_menu();
  2218. } break;
  2219. }
  2220. }
  2221. void AnimationKeyEditor::_scroll_changed(double) {
  2222. if (te_drawing)
  2223. return;
  2224. track_editor->update();
  2225. }
  2226. void AnimationKeyEditor::_update_paths() {
  2227. if (animation.is_valid()) {
  2228. //timeline->set_max(animation->get_length());
  2229. //timeline->set_step(0.01);
  2230. track_editor->update();
  2231. length->set_val(animation->get_length());
  2232. step->set_val(animation->get_step());
  2233. }
  2234. }
  2235. void AnimationKeyEditor::_root_removed() {
  2236. root = NULL;
  2237. }
  2238. void AnimationKeyEditor::_update_menu() {
  2239. updating = true;
  2240. if (animation.is_valid()) {
  2241. length->set_val(animation->get_length());
  2242. loop->set_pressed(animation->has_loop());
  2243. step->set_val(animation->get_step());
  2244. }
  2245. track_editor->update();
  2246. updating = false;
  2247. }
  2248. void AnimationKeyEditor::_clear_selection() {
  2249. selection.clear();
  2250. key_edit->animation = Ref<Animation>();
  2251. key_edit->track = 0;
  2252. key_edit->key_ofs = 0;
  2253. key_edit->hint = PropertyInfo();
  2254. key_edit->base = NodePath();
  2255. key_edit->notify_change();
  2256. }
  2257. void AnimationKeyEditor::set_animation(const Ref<Animation> &p_anim) {
  2258. if (animation.is_valid())
  2259. animation->disconnect("changed", this, "_update_paths");
  2260. animation = p_anim;
  2261. if (animation.is_valid())
  2262. animation->connect("changed", this, "_update_paths");
  2263. timeline_pos = 0;
  2264. _clear_selection();
  2265. _update_paths();
  2266. _update_menu();
  2267. selected_track = -1;
  2268. _edit_if_single_selection();
  2269. EditorNode::get_singleton()->update_keying();
  2270. }
  2271. void AnimationKeyEditor::set_root(Node *p_root) {
  2272. if (root)
  2273. root->disconnect("exit_tree", this, "_root_removed");
  2274. root = p_root;
  2275. if (root)
  2276. root->connect("exit_tree", this, "_root_removed", make_binds(), CONNECT_ONESHOT);
  2277. }
  2278. Node *AnimationKeyEditor::get_root() const {
  2279. return root;
  2280. }
  2281. void AnimationKeyEditor::update_keying() {
  2282. bool keying_enabled = is_visible() && animation.is_valid();
  2283. if (keying_enabled == keying)
  2284. return;
  2285. keying = keying_enabled;
  2286. _update_menu();
  2287. emit_signal("keying_changed");
  2288. }
  2289. bool AnimationKeyEditor::has_keying() const {
  2290. return keying;
  2291. }
  2292. void AnimationKeyEditor::_query_insert(const InsertData &p_id) {
  2293. if (insert_frame != OS::get_singleton()->get_frames_drawn()) {
  2294. //clear insert list for the frame if frame changed
  2295. if (insert_confirm->is_visible())
  2296. return; //do nothing
  2297. insert_data.clear();
  2298. insert_query = false;
  2299. }
  2300. insert_frame = OS::get_singleton()->get_frames_drawn();
  2301. for (List<InsertData>::Element *E = insert_data.front(); E; E = E->next()) {
  2302. //prevent insertion of multiple tracks
  2303. if (E->get().path == p_id.path)
  2304. return; //already inserted a track for this on this frame
  2305. }
  2306. insert_data.push_back(p_id);
  2307. if (p_id.track_idx == -1) {
  2308. if (bool(EDITOR_DEF("animation/confirm_insert_track", true))) {
  2309. //potential new key, does not exist
  2310. if (insert_data.size() == 1)
  2311. insert_confirm->set_text(vformat(TTR("Create NEW track for %s and insert key?"), p_id.query));
  2312. else
  2313. insert_confirm->set_text(vformat(TTR("Create %d NEW tracks and insert keys?"), insert_data.size()));
  2314. insert_confirm->get_ok()->set_text(TTR("Create"));
  2315. insert_confirm->popup_centered_minsize();
  2316. insert_query = true;
  2317. } else {
  2318. call_deferred("_insert_delay");
  2319. insert_queue = true;
  2320. }
  2321. } else {
  2322. if (!insert_query && !insert_queue) {
  2323. call_deferred("_insert_delay");
  2324. insert_queue = true;
  2325. }
  2326. }
  2327. }
  2328. void AnimationKeyEditor::insert_transform_key(Spatial *p_node, const String &p_sub, const Transform &p_xform) {
  2329. if (!keying)
  2330. return;
  2331. if (!animation.is_valid())
  2332. return;
  2333. ERR_FAIL_COND(!root);
  2334. //let's build a node path
  2335. String path = root->get_path_to(p_node);
  2336. if (p_sub != "")
  2337. path += ":" + p_sub;
  2338. NodePath np = path;
  2339. int track_idx = -1;
  2340. for (int i = 0; i < animation->get_track_count(); i++) {
  2341. if (animation->track_get_type(i) != Animation::TYPE_TRANSFORM)
  2342. continue;
  2343. if (animation->track_get_path(i) != np)
  2344. continue;
  2345. track_idx = i;
  2346. break;
  2347. }
  2348. InsertData id;
  2349. Dictionary val;
  2350. id.path = np;
  2351. id.track_idx = track_idx;
  2352. id.value = p_xform;
  2353. id.type = Animation::TYPE_TRANSFORM;
  2354. id.query = "node '" + p_node->get_name() + "'";
  2355. id.advance = false;
  2356. //dialog insert
  2357. _query_insert(id);
  2358. }
  2359. void AnimationKeyEditor::insert_node_value_key(Node *p_node, const String &p_property, const Variant &p_value, bool p_only_if_exists) {
  2360. ERR_FAIL_COND(!root);
  2361. //let's build a node path
  2362. Node *node = p_node;
  2363. String path = root->get_path_to(node);
  2364. for (int i = 1; i < history->get_path_size(); i++) {
  2365. String prop = history->get_path_property(i);
  2366. ERR_FAIL_COND(prop == "");
  2367. path += ":" + prop;
  2368. }
  2369. path += ":" + p_property;
  2370. NodePath np = path;
  2371. //locate track
  2372. int track_idx = -1;
  2373. for (int i = 0; i < animation->get_track_count(); i++) {
  2374. if (animation->track_get_type(i) != Animation::TYPE_VALUE)
  2375. continue;
  2376. if (animation->track_get_path(i) != np)
  2377. continue;
  2378. track_idx = i;
  2379. break;
  2380. }
  2381. if (p_only_if_exists && track_idx == -1)
  2382. return;
  2383. InsertData id;
  2384. id.path = np;
  2385. id.track_idx = track_idx;
  2386. id.value = p_value;
  2387. id.type = Animation::TYPE_VALUE;
  2388. id.query = "property '" + p_property + "'";
  2389. id.advance = false;
  2390. //dialog insert
  2391. _query_insert(id);
  2392. }
  2393. void AnimationKeyEditor::insert_value_key(const String &p_property, const Variant &p_value, bool p_advance) {
  2394. ERR_FAIL_COND(!root);
  2395. //let's build a node path
  2396. ERR_FAIL_COND(history->get_path_size() == 0);
  2397. Object *obj = ObjectDB::get_instance(history->get_path_object(0));
  2398. ERR_FAIL_COND(!obj || !obj->cast_to<Node>());
  2399. Node *node = obj->cast_to<Node>();
  2400. String path = root->get_path_to(node);
  2401. for (int i = 1; i < history->get_path_size(); i++) {
  2402. String prop = history->get_path_property(i);
  2403. ERR_FAIL_COND(prop == "");
  2404. path += ":" + prop;
  2405. }
  2406. path += ":" + p_property;
  2407. NodePath np = path;
  2408. //locate track
  2409. int track_idx = -1;
  2410. for (int i = 0; i < animation->get_track_count(); i++) {
  2411. if (animation->track_get_type(i) != Animation::TYPE_VALUE)
  2412. continue;
  2413. if (animation->track_get_path(i) != np)
  2414. continue;
  2415. track_idx = i;
  2416. break;
  2417. }
  2418. InsertData id;
  2419. id.path = np;
  2420. id.track_idx = track_idx;
  2421. id.value = p_value;
  2422. id.type = Animation::TYPE_VALUE;
  2423. id.query = "property '" + p_property + "'";
  2424. id.advance = p_advance;
  2425. //dialog insert
  2426. _query_insert(id);
  2427. }
  2428. void AnimationKeyEditor::_confirm_insert_list() {
  2429. undo_redo->create_action(TTR("Anim Create & Insert"));
  2430. int last_track = animation->get_track_count();
  2431. while (insert_data.size()) {
  2432. last_track = _confirm_insert(insert_data.front()->get(), last_track);
  2433. insert_data.pop_front();
  2434. }
  2435. undo_redo->commit_action();
  2436. }
  2437. int AnimationKeyEditor::_confirm_insert(InsertData p_id, int p_last_track) {
  2438. if (p_last_track == -1)
  2439. p_last_track = animation->get_track_count();
  2440. bool created = false;
  2441. if (p_id.track_idx < 0) {
  2442. created = true;
  2443. undo_redo->create_action(TTR("Anim Insert Track & Key"));
  2444. Animation::UpdateMode update_mode = Animation::UPDATE_DISCRETE;
  2445. if (p_id.type == Animation::TYPE_VALUE) {
  2446. //wants a new tack
  2447. {
  2448. //shitty hack
  2449. NodePath np;
  2450. animation->add_track(p_id.type);
  2451. animation->track_set_path(animation->get_track_count() - 1, p_id.path);
  2452. PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
  2453. animation->remove_track(animation->get_track_count() - 1); //hack
  2454. if (h.type == Variant::REAL ||
  2455. h.type == Variant::VECTOR2 ||
  2456. h.type == Variant::RECT2 ||
  2457. h.type == Variant::VECTOR3 ||
  2458. h.type == Variant::_AABB ||
  2459. h.type == Variant::QUAT ||
  2460. h.type == Variant::COLOR ||
  2461. h.type == Variant::TRANSFORM) {
  2462. update_mode = Animation::UPDATE_CONTINUOUS;
  2463. }
  2464. if (h.usage & PROPERTY_USAGE_ANIMATE_AS_TRIGGER) {
  2465. update_mode = Animation::UPDATE_TRIGGER;
  2466. }
  2467. }
  2468. }
  2469. p_id.track_idx = p_last_track;
  2470. undo_redo->add_do_method(animation.ptr(), "add_track", p_id.type);
  2471. undo_redo->add_do_method(animation.ptr(), "track_set_path", p_id.track_idx, p_id.path);
  2472. if (p_id.type == Animation::TYPE_VALUE)
  2473. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", p_id.track_idx, update_mode);
  2474. } else {
  2475. undo_redo->create_action(TTR("Anim Insert Key"));
  2476. }
  2477. float time = timeline_pos;
  2478. Variant value;
  2479. switch (p_id.type) {
  2480. case Animation::TYPE_VALUE: {
  2481. value = p_id.value;
  2482. } break;
  2483. case Animation::TYPE_TRANSFORM: {
  2484. Transform tr = p_id.value;
  2485. Dictionary d;
  2486. d["loc"] = tr.origin;
  2487. d["scale"] = tr.basis.get_scale();
  2488. d["rot"] = Quat(tr.basis); //.orthonormalized();
  2489. value = d;
  2490. } break;
  2491. default: {
  2492. }
  2493. }
  2494. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, value);
  2495. if (created) {
  2496. //just remove the track
  2497. undo_redo->add_undo_method(animation.ptr(), "remove_track", p_last_track);
  2498. p_last_track++;
  2499. } else {
  2500. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_pos", p_id.track_idx, time);
  2501. int existing = animation->track_find_key(p_id.track_idx, time, true);
  2502. if (existing != -1) {
  2503. Variant v = animation->track_get_key_value(p_id.track_idx, existing);
  2504. float trans = animation->track_get_key_transition(p_id.track_idx, existing);
  2505. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, v, trans);
  2506. }
  2507. }
  2508. undo_redo->add_do_method(this, "update");
  2509. undo_redo->add_undo_method(this, "update");
  2510. undo_redo->add_do_method(track_editor, "update");
  2511. undo_redo->add_undo_method(track_editor, "update");
  2512. undo_redo->add_do_method(track_pos, "update");
  2513. undo_redo->add_undo_method(track_pos, "update");
  2514. undo_redo->commit_action();
  2515. return p_last_track;
  2516. }
  2517. Ref<Animation> AnimationKeyEditor::get_current_animation() const {
  2518. return animation;
  2519. }
  2520. void AnimationKeyEditor::_animation_len_changed(float p_len) {
  2521. if (updating)
  2522. return;
  2523. if (!animation.is_null()) {
  2524. undo_redo->create_action(TTR("Change Anim Len"));
  2525. undo_redo->add_do_method(animation.ptr(), "set_length", p_len);
  2526. undo_redo->add_undo_method(animation.ptr(), "set_length", animation->get_length());
  2527. undo_redo->add_do_method(this, "_animation_len_update");
  2528. undo_redo->add_undo_method(this, "_animation_len_update");
  2529. undo_redo->commit_action();
  2530. }
  2531. }
  2532. void AnimationKeyEditor::_animation_len_update() {
  2533. if (!animation.is_null())
  2534. emit_signal(alc, animation->get_length());
  2535. }
  2536. void AnimationKeyEditor::_animation_changed() {
  2537. if (updating)
  2538. return;
  2539. _update_menu();
  2540. }
  2541. void AnimationKeyEditor::_animation_loop_changed() {
  2542. if (updating)
  2543. return;
  2544. if (!animation.is_null()) {
  2545. undo_redo->create_action(TTR("Change Anim Loop"));
  2546. undo_redo->add_do_method(animation.ptr(), "set_loop", loop->is_pressed());
  2547. undo_redo->add_undo_method(animation.ptr(), "set_loop", !loop->is_pressed());
  2548. undo_redo->commit_action();
  2549. }
  2550. }
  2551. void AnimationKeyEditor::_create_value_item(int p_type) {
  2552. undo_redo->create_action(TTR("Anim Create Typed Value Key"));
  2553. Variant::CallError ce;
  2554. Variant v = Variant::construct(Variant::Type(p_type), NULL, 0, ce);
  2555. undo_redo->add_do_method(animation.ptr(), "track_insert_key", cvi_track, cvi_pos, v);
  2556. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_pos", cvi_track, cvi_pos);
  2557. int existing = animation->track_find_key(cvi_track, cvi_pos, true);
  2558. if (existing != -1) {
  2559. Variant v = animation->track_get_key_value(cvi_track, existing);
  2560. float trans = animation->track_get_key_transition(cvi_track, existing);
  2561. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", cvi_track, cvi_pos, v, trans);
  2562. }
  2563. undo_redo->commit_action();
  2564. }
  2565. void AnimationKeyEditor::set_anim_pos(float p_pos) {
  2566. if (animation.is_null())
  2567. return;
  2568. timeline_pos = p_pos;
  2569. update();
  2570. track_pos->update();
  2571. track_editor->update();
  2572. }
  2573. void AnimationKeyEditor::_pane_drag(const Point2 &p_delta) {
  2574. Size2 ecs = ec->get_custom_minimum_size();
  2575. ecs.y -= p_delta.y;
  2576. if (ecs.y < 100)
  2577. ecs.y = 100;
  2578. ec->set_custom_minimum_size(ecs);
  2579. }
  2580. void AnimationKeyEditor::_insert_delay() {
  2581. if (insert_query) {
  2582. //discard since it's entered into query mode
  2583. insert_queue = false;
  2584. return;
  2585. }
  2586. undo_redo->create_action(TTR("Anim Insert"));
  2587. int last_track = animation->get_track_count();
  2588. bool advance = false;
  2589. while (insert_data.size()) {
  2590. if (insert_data.front()->get().advance)
  2591. advance = true;
  2592. last_track = _confirm_insert(insert_data.front()->get(), last_track);
  2593. insert_data.pop_front();
  2594. }
  2595. undo_redo->commit_action();
  2596. if (advance) {
  2597. float step = animation->get_step();
  2598. if (step == 0)
  2599. step = 1;
  2600. float pos = timeline_pos;
  2601. pos = Math::stepify(pos + step, step);
  2602. if (pos > animation->get_length())
  2603. pos = animation->get_length();
  2604. timeline_pos = pos;
  2605. track_pos->update();
  2606. emit_signal("timeline_changed", pos, true);
  2607. }
  2608. insert_queue = false;
  2609. }
  2610. void AnimationKeyEditor::_step_changed(float p_len) {
  2611. updating = true;
  2612. if (!animation.is_null()) {
  2613. animation->set_step(p_len);
  2614. emit_signal("animation_step_changed", animation->get_step());
  2615. }
  2616. updating = false;
  2617. }
  2618. void AnimationKeyEditor::_scale() {
  2619. if (selection.empty())
  2620. return;
  2621. float from_t = 1e20;
  2622. float to_t = -1e20;
  2623. float len = -1e20;
  2624. float pivot = 0;
  2625. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  2626. float t = animation->track_get_key_time(E->key().track, E->key().key);
  2627. if (t < from_t)
  2628. from_t = t;
  2629. if (t > to_t)
  2630. to_t = t;
  2631. }
  2632. len = to_t - from_t;
  2633. if (last_menu_track_opt == TRACK_MENU_SCALE_PIVOT) {
  2634. pivot = timeline_pos;
  2635. } else {
  2636. pivot = from_t;
  2637. }
  2638. float s = scale->get_val();
  2639. if (s == 0) {
  2640. ERR_PRINT("Can't scale to 0");
  2641. }
  2642. undo_redo->create_action(TTR("Anim Scale Keys"));
  2643. List<_AnimMoveRestore> to_restore;
  2644. // 1-remove the keys
  2645. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2646. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  2647. }
  2648. // 2- remove overlapped keys
  2649. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2650. float newtime = (E->get().pos - from_t) * s + from_t;
  2651. int idx = animation->track_find_key(E->key().track, newtime, true);
  2652. if (idx == -1)
  2653. continue;
  2654. SelectedKey sk;
  2655. sk.key = idx;
  2656. sk.track = E->key().track;
  2657. if (selection.has(sk))
  2658. continue; //already in selection, don't save
  2659. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_pos", E->key().track, newtime);
  2660. _AnimMoveRestore amr;
  2661. amr.key = animation->track_get_key_value(E->key().track, idx);
  2662. amr.track = E->key().track;
  2663. amr.time = newtime;
  2664. amr.transition = animation->track_get_key_transition(E->key().track, idx);
  2665. to_restore.push_back(amr);
  2666. }
  2667. #define _NEW_POS(m_ofs) (((s > 0) ? m_ofs : from_t + (len - (m_ofs - from_t))) - pivot) * ABS(s) + from_t
  2668. // 3-move the keys (re insert them)
  2669. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2670. float newpos = _NEW_POS(E->get().pos);
  2671. 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));
  2672. }
  2673. // 4-(undo) remove inserted keys
  2674. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2675. float newpos = _NEW_POS(E->get().pos);
  2676. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_pos", E->key().track, newpos);
  2677. }
  2678. // 5-(undo) reinsert keys
  2679. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2680. 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));
  2681. }
  2682. // 6-(undo) reinsert overlapped keys
  2683. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  2684. _AnimMoveRestore &amr = E->get();
  2685. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  2686. }
  2687. // 6-(undo) reinsert overlapped keys
  2688. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  2689. _AnimMoveRestore &amr = E->get();
  2690. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  2691. }
  2692. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  2693. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  2694. // 7-reselect
  2695. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  2696. float oldpos = E->get().pos;
  2697. float newpos = _NEW_POS(oldpos);
  2698. if (newpos >= 0)
  2699. undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
  2700. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
  2701. }
  2702. #undef _NEW_POS
  2703. undo_redo->commit_action();
  2704. }
  2705. void AnimationKeyEditor::_add_call_track(const NodePath &p_base) {
  2706. Node *base = EditorNode::get_singleton()->get_edited_scene();
  2707. if (!base)
  2708. return;
  2709. Node *from = base->get_node(p_base);
  2710. if (!from || !root)
  2711. return;
  2712. NodePath path = root->get_path_to(from);
  2713. //print_line("root: "+String(root->get_path()));
  2714. //print_line("path: "+String(path));
  2715. undo_redo->create_action(TTR("Anim Add Call Track"));
  2716. undo_redo->add_do_method(animation.ptr(), "add_track", Animation::TYPE_METHOD);
  2717. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path);
  2718. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  2719. undo_redo->commit_action();
  2720. }
  2721. void AnimationKeyEditor::cleanup() {
  2722. set_animation(Ref<Animation>());
  2723. }
  2724. void AnimationKeyEditor::_bind_methods() {
  2725. ObjectTypeDB::bind_method(_MD("_root_removed"), &AnimationKeyEditor::_root_removed);
  2726. ObjectTypeDB::bind_method(_MD("_scale"), &AnimationKeyEditor::_scale);
  2727. ObjectTypeDB::bind_method(_MD("set_root"), &AnimationKeyEditor::set_root);
  2728. // ObjectTypeDB::bind_method(_MD("_confirm_insert"),&AnimationKeyEditor::_confirm_insert);
  2729. ObjectTypeDB::bind_method(_MD("_confirm_insert_list"), &AnimationKeyEditor::_confirm_insert_list);
  2730. ObjectTypeDB::bind_method(_MD("_update_paths"), &AnimationKeyEditor::_update_paths);
  2731. ObjectTypeDB::bind_method(_MD("_track_editor_draw"), &AnimationKeyEditor::_track_editor_draw);
  2732. ObjectTypeDB::bind_method(_MD("_animation_changed"), &AnimationKeyEditor::_animation_changed);
  2733. ObjectTypeDB::bind_method(_MD("_scroll_changed"), &AnimationKeyEditor::_scroll_changed);
  2734. ObjectTypeDB::bind_method(_MD("_track_editor_input_event"), &AnimationKeyEditor::_track_editor_input_event);
  2735. ObjectTypeDB::bind_method(_MD("_track_name_changed"), &AnimationKeyEditor::_track_name_changed);
  2736. ObjectTypeDB::bind_method(_MD("_track_menu_selected"), &AnimationKeyEditor::_track_menu_selected);
  2737. ObjectTypeDB::bind_method(_MD("_menu_add_track"), &AnimationKeyEditor::_menu_add_track);
  2738. ObjectTypeDB::bind_method(_MD("_menu_track"), &AnimationKeyEditor::_menu_track);
  2739. ObjectTypeDB::bind_method(_MD("_clear_selection_for_anim"), &AnimationKeyEditor::_clear_selection_for_anim);
  2740. ObjectTypeDB::bind_method(_MD("_select_at_anim"), &AnimationKeyEditor::_select_at_anim);
  2741. ObjectTypeDB::bind_method(_MD("_track_pos_draw"), &AnimationKeyEditor::_track_pos_draw);
  2742. ObjectTypeDB::bind_method(_MD("_insert_delay"), &AnimationKeyEditor::_insert_delay);
  2743. ObjectTypeDB::bind_method(_MD("_step_changed"), &AnimationKeyEditor::_step_changed);
  2744. ObjectTypeDB::bind_method(_MD("_animation_loop_changed"), &AnimationKeyEditor::_animation_loop_changed);
  2745. ObjectTypeDB::bind_method(_MD("_animation_len_changed"), &AnimationKeyEditor::_animation_len_changed);
  2746. ObjectTypeDB::bind_method(_MD("_create_value_item"), &AnimationKeyEditor::_create_value_item);
  2747. ObjectTypeDB::bind_method(_MD("_pane_drag"), &AnimationKeyEditor::_pane_drag);
  2748. ObjectTypeDB::bind_method(_MD("_animation_len_update"), &AnimationKeyEditor::_animation_len_update);
  2749. ObjectTypeDB::bind_method(_MD("set_animation"), &AnimationKeyEditor::set_animation);
  2750. ObjectTypeDB::bind_method(_MD("_animation_optimize"), &AnimationKeyEditor::_animation_optimize);
  2751. ObjectTypeDB::bind_method(_MD("_curve_transition_changed"), &AnimationKeyEditor::_curve_transition_changed);
  2752. ObjectTypeDB::bind_method(_MD("_toggle_edit_curves"), &AnimationKeyEditor::_toggle_edit_curves);
  2753. ObjectTypeDB::bind_method(_MD("_add_call_track"), &AnimationKeyEditor::_add_call_track);
  2754. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "res"), PropertyInfo(Variant::STRING, "prop")));
  2755. ADD_SIGNAL(MethodInfo("keying_changed"));
  2756. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::REAL, "pos"), PropertyInfo(Variant::BOOL, "drag")));
  2757. ADD_SIGNAL(MethodInfo("animation_len_changed", PropertyInfo(Variant::REAL, "len")));
  2758. ADD_SIGNAL(MethodInfo("animation_step_changed", PropertyInfo(Variant::REAL, "step")));
  2759. ADD_SIGNAL(MethodInfo("key_edited", PropertyInfo(Variant::INT, "track"), PropertyInfo(Variant::INT, "key")));
  2760. }
  2761. AnimationKeyEditor::AnimationKeyEditor() {
  2762. alc = "animation_len_changed";
  2763. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  2764. selected_track = -1;
  2765. updating = false;
  2766. te_drawing = false;
  2767. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2768. history = EditorNode::get_singleton()->get_editor_history();
  2769. ec = memnew(Control);
  2770. ec->set_custom_minimum_size(Size2(0, 150));
  2771. add_child(ec);
  2772. ec->set_v_size_flags(SIZE_EXPAND_FILL);
  2773. h_scroll = memnew(HScrollBar);
  2774. h_scroll->connect("value_changed", this, "_scroll_changed");
  2775. add_child(h_scroll);
  2776. h_scroll->set_val(0);
  2777. HBoxContainer *hb = memnew(HBoxContainer);
  2778. add_child(hb);
  2779. root = NULL;
  2780. //menu = memnew( MenuButton );
  2781. //menu->set_flat(true);
  2782. //menu->set_pos(Point2());
  2783. //add_child(menu);
  2784. zoomicon = memnew(TextureFrame);
  2785. hb->add_child(zoomicon);
  2786. zoomicon->set_tooltip(TTR("Animation zoom."));
  2787. zoom = memnew(HSlider);
  2788. //hb->add_child(zoom);
  2789. zoom->set_step(0.01);
  2790. zoom->set_min(0.0);
  2791. zoom->set_max(2.0);
  2792. zoom->set_val(1.0);
  2793. zoom->set_h_size_flags(SIZE_EXPAND_FILL);
  2794. zoom->set_stretch_ratio(2);
  2795. hb->add_child(zoom);
  2796. zoom->connect("value_changed", this, "_scroll_changed");
  2797. zoom->set_tooltip(TTR("Animation zoom."));
  2798. hb->add_child(memnew(VSeparator));
  2799. Label *l = memnew(Label);
  2800. l->set_text(TTR("Length (s):"));
  2801. hb->add_child(l);
  2802. length = memnew(SpinBox);
  2803. length->set_min(0.01);
  2804. length->set_max(10000);
  2805. length->set_step(0.01);
  2806. length->set_h_size_flags(SIZE_EXPAND_FILL);
  2807. length->set_stretch_ratio(1);
  2808. length->set_tooltip(TTR("Animation length (in seconds)."));
  2809. hb->add_child(length);
  2810. length->connect("value_changed", this, "_animation_len_changed");
  2811. l = memnew(Label);
  2812. l->set_text(TTR("Step (s):"));
  2813. hb->add_child(l);
  2814. step = memnew(SpinBox);
  2815. step->set_min(0.00);
  2816. step->set_max(128);
  2817. step->set_step(0.01);
  2818. step->set_val(0.0);
  2819. step->set_h_size_flags(SIZE_EXPAND_FILL);
  2820. step->set_stretch_ratio(1);
  2821. step->set_tooltip(TTR("Cursor step snap (in seconds)."));
  2822. hb->add_child(step);
  2823. step->connect("value_changed", this, "_step_changed");
  2824. loop = memnew(ToolButton);
  2825. loop->set_toggle_mode(true);
  2826. loop->connect("pressed", this, "_animation_loop_changed");
  2827. hb->add_child(loop);
  2828. loop->set_tooltip(TTR("Enable/Disable looping in animation."));
  2829. hb->add_child(memnew(VSeparator));
  2830. menu_add_track = memnew(MenuButton);
  2831. hb->add_child(menu_add_track);
  2832. menu_add_track->get_popup()->connect("item_pressed", this, "_menu_add_track");
  2833. menu_add_track->set_tooltip(TTR("Add new tracks."));
  2834. move_up_button = memnew(ToolButton);
  2835. hb->add_child(move_up_button);
  2836. move_up_button->connect("pressed", this, "_menu_track", make_binds(TRACK_MENU_MOVE_UP));
  2837. move_up_button->set_focus_mode(FOCUS_NONE);
  2838. move_up_button->set_disabled(true);
  2839. move_up_button->set_tooltip(TTR("Move current track up."));
  2840. move_down_button = memnew(ToolButton);
  2841. hb->add_child(move_down_button);
  2842. move_down_button->connect("pressed", this, "_menu_track", make_binds(TRACK_MENU_MOVE_DOWN));
  2843. move_down_button->set_focus_mode(FOCUS_NONE);
  2844. move_down_button->set_disabled(true);
  2845. move_down_button->set_tooltip(TTR("Move current track down."));
  2846. remove_button = memnew(ToolButton);
  2847. hb->add_child(remove_button);
  2848. remove_button->connect("pressed", this, "_menu_track", make_binds(TRACK_MENU_REMOVE));
  2849. remove_button->set_focus_mode(FOCUS_NONE);
  2850. remove_button->set_disabled(true);
  2851. remove_button->set_tooltip(TTR("Remove selected track."));
  2852. hb->add_child(memnew(VSeparator));
  2853. menu_track = memnew(MenuButton);
  2854. hb->add_child(menu_track);
  2855. menu_track->get_popup()->connect("item_pressed", this, "_menu_track");
  2856. menu_track->set_tooltip(TTR("Track tools"));
  2857. edit_button = memnew(ToolButton);
  2858. edit_button->set_toggle_mode(true);
  2859. edit_button->set_focus_mode(FOCUS_NONE);
  2860. edit_button->set_disabled(true);
  2861. hb->add_child(edit_button);
  2862. edit_button->set_tooltip(TTR("Enable editing of individual keys by clicking them."));
  2863. optimize_dialog = memnew(ConfirmationDialog);
  2864. add_child(optimize_dialog);
  2865. optimize_dialog->set_title(TTR("Anim. Optimizer"));
  2866. VBoxContainer *optimize_vb = memnew(VBoxContainer);
  2867. optimize_dialog->add_child(optimize_vb);
  2868. optimize_dialog->set_child_rect(optimize_vb);
  2869. optimize_linear_error = memnew(SpinBox);
  2870. optimize_linear_error->set_max(1.0);
  2871. optimize_linear_error->set_min(0.001);
  2872. optimize_linear_error->set_step(0.001);
  2873. optimize_linear_error->set_val(0.05);
  2874. optimize_vb->add_margin_child(TTR("Max. Linear Error:"), optimize_linear_error);
  2875. optimize_angular_error = memnew(SpinBox);
  2876. optimize_angular_error->set_max(1.0);
  2877. optimize_angular_error->set_min(0.001);
  2878. optimize_angular_error->set_step(0.001);
  2879. optimize_angular_error->set_val(0.01);
  2880. optimize_vb->add_margin_child(TTR("Max. Angular Error:"), optimize_angular_error);
  2881. optimize_max_angle = memnew(SpinBox);
  2882. optimize_vb->add_margin_child(TTR("Max Optimizable Angle:"), optimize_max_angle);
  2883. optimize_max_angle->set_max(360.0);
  2884. optimize_max_angle->set_min(0.0);
  2885. optimize_max_angle->set_step(0.1);
  2886. optimize_max_angle->set_val(22);
  2887. optimize_dialog->get_ok()->set_text(TTR("Optimize"));
  2888. /*keying = memnew( Button );
  2889. keying->set_toggle_mode(true);
  2890. //keying->set_text("Keys");
  2891. keying->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,60);
  2892. keying->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
  2893. keying->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_BEGIN,55);
  2894. keying->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  2895. //add_child(keying);
  2896. keying->connect("pressed",this,"_keying_toggled");
  2897. */
  2898. /* l = memnew( Label );
  2899. l->set_text("Base: ");
  2900. l->set_pos(Point2(0,3));
  2901. // dr_panel->add_child(l);*/
  2902. // menu->get_popup()->connect("item_pressed",this,"_menu_callback");
  2903. hb = memnew(HBoxContainer);
  2904. hb->set_area_as_parent_rect();
  2905. ec->add_child(hb);
  2906. hb->set_v_size_flags(SIZE_EXPAND_FILL);
  2907. track_editor = memnew(Control);
  2908. track_editor->connect("draw", this, "_track_editor_draw");
  2909. hb->add_child(track_editor);
  2910. track_editor->connect("input_event", this, "_track_editor_input_event");
  2911. track_editor->set_focus_mode(Control::FOCUS_ALL);
  2912. track_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2913. track_pos = memnew(Control);
  2914. track_pos->set_area_as_parent_rect();
  2915. track_pos->set_ignore_mouse(true);
  2916. track_editor->add_child(track_pos);
  2917. track_pos->connect("draw", this, "_track_pos_draw");
  2918. select_anim_warning = memnew(Label);
  2919. track_editor->add_child(select_anim_warning);
  2920. select_anim_warning->set_area_as_parent_rect();
  2921. select_anim_warning->set_text(TTR("Select an AnimationPlayer from the Scene Tree to edit animations."));
  2922. select_anim_warning->set_autowrap(true);
  2923. select_anim_warning->set_align(Label::ALIGN_CENTER);
  2924. select_anim_warning->set_valign(Label::VALIGN_CENTER);
  2925. v_scroll = memnew(VScrollBar);
  2926. hb->add_child(v_scroll);
  2927. v_scroll->connect("value_changed", this, "_scroll_changed");
  2928. v_scroll->set_val(0);
  2929. key_editor_tab = memnew(TabContainer);
  2930. hb->add_child(key_editor_tab);
  2931. key_editor_tab->set_custom_minimum_size(Size2(200, 0));
  2932. key_editor = memnew(PropertyEditor);
  2933. key_editor->set_area_as_parent_rect();
  2934. key_editor->hide_top_label();
  2935. key_editor->set_name(TTR("Key"));
  2936. key_editor_tab->add_child(key_editor);
  2937. key_edit = memnew(AnimationKeyEdit);
  2938. key_edit->undo_redo = undo_redo;
  2939. //key_edit->ke_dialog=key_edit_dialog;
  2940. type_menu = memnew(PopupMenu);
  2941. add_child(type_menu);
  2942. for (int i = 0; i < Variant::VARIANT_MAX; i++)
  2943. type_menu->add_item(Variant::get_type_name(Variant::Type(i)), i);
  2944. type_menu->connect("item_pressed", this, "_create_value_item");
  2945. VBoxContainer *curve_vb = memnew(VBoxContainer);
  2946. curve_vb->set_name(TTR("Transition"));
  2947. HBoxContainer *curve_hb = memnew(HBoxContainer);
  2948. curve_vb->add_child(curve_hb);
  2949. curve_linear = memnew(ToolButton);
  2950. curve_linear->set_focus_mode(FOCUS_NONE);
  2951. curve_hb->add_child(curve_linear);
  2952. curve_in = memnew(ToolButton);
  2953. curve_in->set_focus_mode(FOCUS_NONE);
  2954. curve_hb->add_child(curve_in);
  2955. curve_out = memnew(ToolButton);
  2956. curve_out->set_focus_mode(FOCUS_NONE);
  2957. curve_hb->add_child(curve_out);
  2958. curve_inout = memnew(ToolButton);
  2959. curve_inout->set_focus_mode(FOCUS_NONE);
  2960. curve_hb->add_child(curve_inout);
  2961. curve_outin = memnew(ToolButton);
  2962. curve_outin->set_focus_mode(FOCUS_NONE);
  2963. curve_hb->add_child(curve_outin);
  2964. curve_constant = memnew(ToolButton);
  2965. curve_constant->set_focus_mode(FOCUS_NONE);
  2966. curve_hb->add_child(curve_constant);
  2967. curve_edit = memnew(AnimationCurveEdit);
  2968. curve_vb->add_child(curve_edit);
  2969. curve_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  2970. key_editor_tab->add_child(curve_vb);
  2971. track_name = memnew(LineEdit);
  2972. track_name->set_as_toplevel(true);
  2973. track_name->hide();
  2974. add_child(track_name);
  2975. track_name->connect("text_entered", this, "_track_name_changed");
  2976. track_menu = memnew(PopupMenu);
  2977. add_child(track_menu);
  2978. track_menu->connect("item_pressed", this, "_track_menu_selected");
  2979. key_editor_tab->hide();
  2980. last_idx = 1;
  2981. _update_menu();
  2982. insert_confirm = memnew(ConfirmationDialog);
  2983. add_child(insert_confirm);
  2984. insert_confirm->connect("confirmed", this, "_confirm_insert_list");
  2985. click.click = ClickOver::CLICK_NONE;
  2986. name_column_ratio = 0.3;
  2987. timeline_pos = 0;
  2988. keying = false;
  2989. insert_frame = 0;
  2990. insert_query = false;
  2991. insert_queue = false;
  2992. editor_selection->connect("selection_changed", track_editor, "update");
  2993. scale_dialog = memnew(ConfirmationDialog);
  2994. VBoxContainer *vbc = memnew(VBoxContainer);
  2995. scale_dialog->add_child(vbc);
  2996. scale_dialog->set_child_rect(vbc);
  2997. scale = memnew(SpinBox);
  2998. scale->set_min(-99999);
  2999. scale->set_max(99999);
  3000. scale->set_step(0.001);
  3001. vbc->add_margin_child(TTR("Scale Ratio:"), scale);
  3002. scale_dialog->connect("confirmed", this, "_scale");
  3003. add_child(scale_dialog);
  3004. call_select = memnew(SceneTreeDialog);
  3005. add_child(call_select);
  3006. call_select->set_title(TTR("Call Functions in Which Node?"));
  3007. cleanup_dialog = memnew(ConfirmationDialog);
  3008. add_child(cleanup_dialog);
  3009. VBoxContainer *cleanup_vb = memnew(VBoxContainer);
  3010. cleanup_dialog->add_child(cleanup_vb);
  3011. cleanup_dialog->set_child_rect(cleanup_vb);
  3012. cleanup_keys = memnew(CheckButton);
  3013. cleanup_keys->set_text(TTR("Remove invalid keys"));
  3014. cleanup_keys->set_pressed(true);
  3015. cleanup_vb->add_child(cleanup_keys);
  3016. cleanup_tracks = memnew(CheckButton);
  3017. cleanup_tracks->set_text(TTR("Remove unresolved and empty tracks"));
  3018. cleanup_tracks->set_pressed(true);
  3019. cleanup_vb->add_child(cleanup_tracks);
  3020. cleanup_all = memnew(CheckButton);
  3021. cleanup_all->set_text(TTR("Clean-up all animations"));
  3022. cleanup_vb->add_child(cleanup_all);
  3023. cleanup_dialog->set_title(TTR("Clean-Up Animation(s) (NO UNDO!)"));
  3024. cleanup_dialog->get_ok()->set_text(TTR("Clean-Up"));
  3025. cleanup_dialog->connect("confirmed", this, "_menu_track", varray(TRACK_MENU_CLEAN_UP_CONFIRM));
  3026. add_constant_override("separation", get_constant("separation", "VBoxContainer"));
  3027. }
  3028. AnimationKeyEditor::~AnimationKeyEditor() {
  3029. memdelete(key_edit);
  3030. }