123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263 |
- /*************************************************************************/
- /* animation_player.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "animation_player.h"
-
- #include "message_queue.h"
- #include "scene/scene_string_names.h"
- bool AnimationPlayer::_set(const StringName& p_name, const Variant& p_value) {
- String name=p_name;
- if (name=="playback/speed" || name=="speed") { //bw compatibility
- set_speed(p_value);
- } else if (name=="playback/active") {
- set_active(p_value);
- } else if (name.begins_with("playback/play")) {
- String which=p_value;
- if (which=="[stop]")
- stop();
- else
- play(which);
- } else if (name.begins_with("anims/")) {
-
- String which=name.get_slice("/",1);
-
- add_animation(which,p_value);
- } else if (name.begins_with("next/")) {
- String which=name.get_slice("/",1);
- animation_set_next(which,p_value);
- } else if (name=="blend_times") {
-
- Array array=p_value;
- int len = array.size();
- ERR_FAIL_COND_V(len%3,false);
- for(int i=0;i<len/3;i++) {
- StringName from = array[i*3+0];
- StringName to = array[i*3+1];
- float time = array[i*3+2];
- set_blend_time(from,to,time);
- }
- } else if (name=="autoplay") {
- autoplay=p_value;
-
- } else
- return false;
- return true;
- }
- bool AnimationPlayer::_get(const StringName& p_name,Variant &r_ret) const {
- String name=p_name;
- if (name=="playback/speed") { //bw compatibility
-
- r_ret=speed_scale;
- } else if (name=="playback/active") {
- r_ret=is_active();
- } else if (name=="playback/play") {
- if (is_active() && is_playing())
- r_ret=playback.assigned;
- else
- r_ret="[stop]";
- } else if (name.begins_with("anims/")) {
-
- String which=name.get_slice("/",1);
-
- r_ret= get_animation(which).get_ref_ptr();
- } else if (name.begins_with("next/")) {
- String which=name.get_slice("/",1);
- r_ret= animation_get_next(which);
- } else if (name=="blend_times") {
-
- Array array;
- array.resize(blend_times.size()*3);
- int idx=0;
- for(Map<BlendKey, float >::Element *E=blend_times.front();E;E=E->next()) {
- array.set(idx*3+0,E->key().from);
- array.set(idx*3+1,E->key().to);
- array.set(idx*3+2,E->get());
- idx++;
- }
- r_ret=array;
- } else if (name=="autoplay") {
- r_ret=autoplay;
- } else
- return false;
- return true;
- }
- void AnimationPlayer::_get_property_list( List<PropertyInfo> *p_list) const {
- List<String> names;
- for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
- p_list->push_back( PropertyInfo( Variant::OBJECT, "anims/"+String(E->key()), PROPERTY_HINT_RESOURCE_TYPE, "Animation",PROPERTY_USAGE_NOEDITOR) );
- if (E->get().next!=StringName())
- p_list->push_back( PropertyInfo( Variant::STRING, "next/"+String(E->key()), PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR) );
- names.push_back(E->key());
- }
- {
- names.sort();
- names.push_front("[stop]");
- String hint;
- for(List<String>::Element *E=names.front();E;E=E->next()) {
- if (E!=names.front())
- hint+=",";
- hint+=E->get();
- }
- p_list->push_back( PropertyInfo( Variant::STRING, "playback/play", PROPERTY_HINT_ENUM, hint,PROPERTY_USAGE_EDITOR) );
- p_list->push_back( PropertyInfo( Variant::BOOL, "playback/active", PROPERTY_HINT_NONE,"" ) );
- p_list->push_back( PropertyInfo( Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01") );
- }
- p_list->push_back( PropertyInfo( Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR) );
- p_list->push_back( PropertyInfo( Variant::STRING, "autoplay", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR) );
- }
- void AnimationPlayer::_notification(int p_what) {
- switch(p_what) {
-
- case NOTIFICATION_ENTER_SCENE: {
- if (!processing) {
- //make sure that a previous process state was not saved
- //only process if "processing" is set
- set_fixed_process(false);
- set_process(false);
- }
- //_set_process(false);
- clear_caches();
- } break;
- case NOTIFICATION_READY: {
- if (!get_scene()->is_editor_hint() && animation_set.has(autoplay)) {
- play(autoplay);
- }
- } break;
- case NOTIFICATION_PROCESS: {
- if (animation_process_mode==ANIMATION_PROCESS_FIXED)
- break;
- if (processing)
- _animation_process( get_process_delta_time() );
- } break;
- case NOTIFICATION_FIXED_PROCESS: {
-
- if (animation_process_mode==ANIMATION_PROCESS_IDLE)
- break;
- if (processing)
- _animation_process( get_fixed_process_delta_time() );
- } break;
- case NOTIFICATION_EXIT_SCENE: {
-
- stop_all();
- clear_caches();
- } break;
- }
- }
- void AnimationPlayer::_generate_node_caches(AnimationData* p_anim) {
- Node *parent = get_node(root);
-
- ERR_FAIL_COND(!parent);
- Animation *a=p_anim->animation.operator->();
-
- p_anim->node_cache.resize( a->get_track_count() );
-
- for (int i=0;i<a->get_track_count();i++) {
-
- p_anim->node_cache[i]=NULL;
- RES resource;
- Node *child = parent->get_node_and_resource(a->track_get_path(i),resource);
- if (!child) {
- ERR_EXPLAIN("On Animation: '"+p_anim->name+"', couldn't resolve track: '"+String(a->track_get_path(i))+"'");
- }
- ERR_CONTINUE(!child); // couldn't find the child node
- uint32_t id=resource.is_valid()?resource->get_instance_ID():child->get_instance_ID();
- int bone_idx=-1;
- if (a->track_get_path(i).get_property() && child->cast_to<Skeleton>()) {
- bone_idx = child->cast_to<Skeleton>()->find_bone( a->track_get_path(i).get_property() );
- if (bone_idx==-1) {
- continue;
- }
- }
-
- {
- if (!child->is_connected("exit_scene",this,"_node_removed"))
- child->connect("exit_scene",this,"_node_removed",make_binds(child),CONNECT_ONESHOT);
- }
- TrackNodeCacheKey key;
- key.id=id;
- key.bone_idx=bone_idx;
-
- if (node_cache_map.has(key)) {
-
- p_anim->node_cache[i]=&node_cache_map[key];
- } else {
-
- node_cache_map[key]=TrackNodeCache();
-
- p_anim->node_cache[i]=&node_cache_map[key];
- p_anim->node_cache[i]->node=child;
- p_anim->node_cache[i]->resource=resource;
- p_anim->node_cache[i]->node_2d=child->cast_to<Node2D>();
- if (a->track_get_type(i)==Animation::TYPE_TRANSFORM) {
- // special cases and caches for transform tracks
-
- // cache spatial
- p_anim->node_cache[i]->spatial=child->cast_to<Spatial>();
- // cache skeleton
- p_anim->node_cache[i]->skeleton=child->cast_to<Skeleton>();
- if (p_anim->node_cache[i]->skeleton) {
- StringName bone_name=a->track_get_path(i).get_property();
- if (bone_name.operator String()!="") {
-
- p_anim->node_cache[i]->bone_idx=p_anim->node_cache[i]->skeleton->find_bone(bone_name);
- if (p_anim->node_cache[i]->bone_idx<0) {
- // broken track (unexisting bone)
- p_anim->node_cache[i]->skeleton=NULL;
- p_anim->node_cache[i]->spatial=NULL;
- printf("bone is %ls\n", String(bone_name).c_str());
- ERR_CONTINUE( p_anim->node_cache[i]->bone_idx<0 );
- } else {
- }
- } else {
- // no property, just use spatialnode
- p_anim->node_cache[i]->skeleton=NULL;
- }
-
- }
- }
- }
- if (a->track_get_type(i)==Animation::TYPE_VALUE) {
- StringName property = a->track_get_path(i).get_property();
- if (!p_anim->node_cache[i]->property_anim.has(property)) {
- TrackNodeCache::PropertyAnim pa;
- pa.prop=property;
- pa.object=resource.is_valid()?(Object*)resource.ptr():(Object*)child;
- pa.special=SP_NONE;
- if (false && p_anim->node_cache[i]->node_2d) {
- if (pa.prop==SceneStringNames::get_singleton()->transform_pos)
- pa.special=SP_NODE2D_POS;
- else if (pa.prop==SceneStringNames::get_singleton()->transform_rot)
- pa.special=SP_NODE2D_ROT;
- else if (pa.prop==SceneStringNames::get_singleton()->transform_scale)
- pa.special=SP_NODE2D_SCALE;
- }
- p_anim->node_cache[i]->property_anim[property]=pa;
- }
- }
- }
- }
- void AnimationPlayer::_animation_process_animation(AnimationData* p_anim,float p_time, float p_delta,float p_interp, bool p_allow_discrete) {
- if (p_anim->node_cache.size() != p_anim->animation->get_track_count()) {
- // animation hasn't been "node-cached"
- _generate_node_caches(p_anim);
- }
- ERR_FAIL_COND( p_anim->node_cache.size() != p_anim->animation->get_track_count() );
-
- Animation *a=p_anim->animation.operator->();
- bool can_call = is_inside_scene() && !get_scene()->is_editor_hint();
-
- for (int i=0;i<a->get_track_count();i++) {
-
- TrackNodeCache *nc=p_anim->node_cache[i];
-
- if (!nc) // no node cache for this track, skip it
- continue;
-
- if (a->track_get_key_count(i)==0)
- continue; // do nothing if track is empty
- switch(a->track_get_type(i)) {
-
- case Animation::TYPE_TRANSFORM: {
-
- if (!nc->spatial)
- continue;
-
-
- Vector3 loc;
- Quat rot;
- Vector3 scale;
- Error err = a->transform_track_interpolate(i,p_time,&loc,&rot,&scale);
- ERR_CONTINUE(err!=OK); //used for testing, should be removed
- if (err!=OK)
- continue;
- if (nc->accum_pass!=accum_pass) {
- ERR_CONTINUE( cache_update_size >= NODE_CACHE_UPDATE_MAX );
- cache_update[cache_update_size++]=nc;
- nc->accum_pass=accum_pass;
- nc->loc_accum=loc;
- nc->rot_accum=rot;
- nc->scale_accum=scale;
- } else {
- nc->loc_accum=nc->loc_accum.linear_interpolate(loc,p_interp);
- nc->rot_accum=nc->rot_accum.slerp(rot,p_interp);
- nc->scale_accum=nc->scale_accum.linear_interpolate(scale,p_interp);
- }
-
- } break;
- case Animation::TYPE_VALUE: {
-
- if (!nc->node)
- continue;
- //StringName property=a->track_get_path(i).get_property();
- Map<StringName,TrackNodeCache::PropertyAnim>::Element *E=nc->property_anim.find(a->track_get_path(i).get_property());
- ERR_CONTINUE(!E); //should it continue, or create a new one?
- TrackNodeCache::PropertyAnim *pa = &E->get();
- if (a->value_track_is_continuous(i) || p_delta==0) {
- Variant value=a->value_track_interpolate(i,p_time);
- if (p_delta==0 && value.get_type()==Variant::STRING)
- continue; // doing this with strings is messy, should find another way
- if (pa->accum_pass!=accum_pass) {
- ERR_CONTINUE( cache_update_prop_size >= NODE_CACHE_UPDATE_MAX );
- cache_update_prop[cache_update_prop_size++]=pa;
- pa->value_accum=value;
- pa->accum_pass=accum_pass;
- } else {
- Variant::interpolate(pa->value_accum,value,p_interp,pa->value_accum);
- }
- } else if (p_allow_discrete) {
- List<int> indices;
- a->value_track_get_key_indices(i,p_time,p_delta,&indices);
- for(List<int>::Element *F=indices.front();F;F=F->next()) {
- Variant value=a->track_get_key_value(i,F->get());
- switch(pa->special) {
- case SP_NONE: pa->object->set(pa->prop,value); break; //you are not speshul
- case SP_NODE2D_POS: static_cast<Node2D*>(pa->object)->set_pos(value); break;
- case SP_NODE2D_ROT: static_cast<Node2D*>(pa->object)->set_rot(Math::deg2rad(value)); break;
- case SP_NODE2D_SCALE: static_cast<Node2D*>(pa->object)->set_scale(value); break;
- }
- }
- }
-
- } break;
- case Animation::TYPE_METHOD: {
-
- if (!nc->node)
- continue;
- if (p_delta==0)
- continue;
- if (!p_allow_discrete)
- break;
-
- List<int> indices;
- a->method_track_get_key_indices(i,p_time,p_delta,&indices);
-
- for(List<int>::Element *E=indices.front();E;E=E->next()) {
- StringName method=a->method_track_get_name(i,E->get());
- Vector<Variant> params=a->method_track_get_params(i,E->get());
- int s=params.size();
- ERR_CONTINUE( s > VARIANT_ARG_MAX );
- if (can_call) {
- MessageQueue::get_singleton()->push_call(
- nc->node,
- method,
- s>=1 ? params[0] : Variant(),
- s>=2 ? params[1] : Variant(),
- s>=3 ? params[2] : Variant(),
- s>=4 ? params[3] : Variant(),
- s>=5 ? params[4] : Variant()
- );
- }
- }
-
-
- } break;
- }
- }
- }
- void AnimationPlayer::_animation_process_data(PlaybackData &cd,float p_delta,float p_blend) {
- float delta=p_delta*speed_scale*cd.speed_scale;
- bool backwards=delta<0;
- float next_pos=cd.pos+delta;
-
- float len=cd.from->animation->get_length();
- bool loop=cd.from->animation->has_loop();
-
- if (!loop) {
-
- if (next_pos<0)
- next_pos=0;
- else if (next_pos>len)
- next_pos=len;
-
- // fix delta
- delta=next_pos-cd.pos;
- if (&cd == &playback.current) {
- if (!backwards && cd.pos < len && next_pos==len /*&& playback.blend.empty()*/) {
- //playback finished
- end_notify=true;
- }
- if (backwards && cd.pos > 0 && next_pos==0 /*&& playback.blend.empty()*/) {
- //playback finished
- end_notify=true;
- }
- }
- } else {
-
- next_pos=Math::fposmod(next_pos,len);
- }
-
- cd.pos=next_pos;
- _animation_process_animation(cd.from,cd.pos,delta,p_blend,&cd == &playback.current);
-
- }
- void AnimationPlayer::_animation_process2(float p_delta) {
- Playback &c=playback;
-
- float prev_blend=1.0;
- accum_pass++;
-
- int pop_count=1;
- int pop=0; // if >0, then amount of elements to pop from the back
- for (List<Blend>::Element *E=c.blend.back();E;E=E->prev(),pop_count++) {
-
- Blend& b=E->get();
- _animation_process_data(b.data,p_delta,prev_blend);
-
- prev_blend=1.0-b.blend_left/b.blend_time;
-
- b.blend_left-=Math::absf(speed_scale*p_delta);
-
- if (b.blend_left<0) {
- pop=pop_count;
- }
- }
- while(pop--) {
- c.blend.pop_back();
- }
-
- _animation_process_data(c.current,p_delta,prev_blend);
-
- }
- void AnimationPlayer::_animation_update_transforms() {
- for (int i=0;i<cache_update_size;i++) {
-
- TrackNodeCache *nc=cache_update[i];
- ERR_CONTINUE( nc->accum_pass!=accum_pass );
- if (nc->spatial) {
-
- Transform t;
- t.origin=nc->loc_accum;
- t.basis=nc->rot_accum;
- t.basis.scale( nc->scale_accum );
- if (nc->skeleton && nc->bone_idx>=0) {
- nc->skeleton->set_bone_pose( nc->bone_idx, t );
- } else if (nc->spatial) {
- nc->spatial->set_transform(t);
- }
- }
-
- }
-
- cache_update_size=0;
- for (int i=0;i<cache_update_prop_size;i++) {
- TrackNodeCache::PropertyAnim *pa=cache_update_prop[i];
- ERR_CONTINUE( pa->accum_pass!=accum_pass );
- #if 1
- switch(pa->special) {
- case SP_NONE: pa->object->set(pa->prop,pa->value_accum); break; //you are not speshul
- case SP_NODE2D_POS: static_cast<Node2D*>(pa->object)->set_pos(pa->value_accum); break;
- case SP_NODE2D_ROT: static_cast<Node2D*>(pa->object)->set_rot(Math::deg2rad(pa->value_accum)); break;
- case SP_NODE2D_SCALE: static_cast<Node2D*>(pa->object)->set_scale(pa->value_accum); break;
- }
- #else
- pa->object->set(pa->prop,pa->value_accum);
- #endif
- }
- cache_update_prop_size=0;
- }
- void AnimationPlayer::_animation_process(float p_delta) {
- // bool any_active=false;
-
- if (playback.current.from) {
- end_notify=false;
- _animation_process2(p_delta);
- _animation_update_transforms();
- if (end_notify) {
- if (queued.size()) {
- String old = playback.assigned;
- play(queued.front()->get());
- String new_name = playback.assigned;
- queued.pop_front();
- end_notify=false;
- emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
- } else {
- //stop();
- playing = false;
- _set_process(false);
- end_notify=false;
- emit_signal(SceneStringNames::get_singleton()->finished);
- }
- }
- } else {
- _set_process(false);
- }
- }
- Error AnimationPlayer::add_animation(const StringName& p_name, const Ref<Animation>& p_animation) {
- ERR_EXPLAIN("Invalid animation name: "+String(p_name));
- ERR_FAIL_COND_V( String(p_name).find("/")!=-1 || String(p_name).find(":")!=-1 || String(p_name).find(",")!=-1 || String(p_name).find("[")!=-1, ERR_INVALID_PARAMETER );
- ERR_FAIL_COND_V( p_animation.is_null() , ERR_INVALID_PARAMETER );
-
- //print_line("Add anim: "+String(p_name)+" name: "+p_animation->get_name());
- if (animation_set.has(p_name)) {
- _unref_anim(animation_set[p_name].animation);
- animation_set[p_name].animation=p_animation;
- clear_caches();
- } else {
-
- AnimationData ad;
- ad.animation=p_animation;
- ad.name=p_name;
- animation_set[p_name]=ad;
- }
- _ref_anim(p_animation);
- _change_notify();
- return OK;
- }
- void AnimationPlayer::remove_animation(const StringName& p_name) {
- ERR_FAIL_COND(!animation_set.has(p_name) );
-
- stop_all();
- _unref_anim(animation_set[p_name].animation);
- animation_set.erase(p_name);
- clear_caches();
- _change_notify();
- }
- void AnimationPlayer::_ref_anim(const Ref<Animation>& p_anim) {
- if (used_anims.has(p_anim))
- used_anims[p_anim]++;
- else {
- used_anims[p_anim]=1;
- Ref<Animation>(p_anim)->connect("changed",this,"_animation_changed");
- }
- }
- void AnimationPlayer::_unref_anim(const Ref<Animation>& p_anim) {
- ERR_FAIL_COND(!used_anims.has(p_anim));
- int & n = used_anims[p_anim];
- n--;
- if (n==0) {
- Ref<Animation>(p_anim)->disconnect("changed",this,"_animation_changed");
- used_anims.erase(p_anim);
- }
- }
- void AnimationPlayer::rename_animation(const StringName& p_name,const StringName& p_new_name) {
- ERR_FAIL_COND(!animation_set.has(p_name) );
- ERR_FAIL_COND( String(p_new_name).find("/")!=-1 || String(p_new_name).find(":")!=-1 );
- ERR_FAIL_COND( animation_set.has(p_new_name) );
- //print_line("Rename anim: "+String(p_name)+" name: "+String(p_new_name));
- stop_all();
- AnimationData ad = animation_set[p_name];
- ad.name=p_new_name;
- animation_set.erase(p_name);
- animation_set[p_new_name]=ad;
- List<BlendKey> to_erase;
- Map<BlendKey,float> to_insert;
- for(Map<BlendKey, float >::Element *E=blend_times.front();E;E=E->next()) {
- BlendKey bk=E->key();
- BlendKey new_bk=bk;
- bool erase=false;
- if (bk.from==p_name) {
- new_bk.from=p_new_name;
- erase=true;
- }
- if (bk.to==p_name) {
- new_bk.to=p_new_name;
- erase=true;
- }
- if (erase) {
- to_erase.push_back(bk);
- to_insert[new_bk]=E->get();
- }
- }
- while(to_erase.size()) {
- blend_times.erase(to_erase.front()->get());
- to_erase.pop_front();;
- }
- while(to_insert.size()) {
- blend_times[to_insert.front()->key()]=to_insert.front()->get();
- to_insert.erase(to_insert.front());
- }
- if (autoplay==p_name)
- autoplay=p_new_name;
- clear_caches();
- _change_notify();
- }
- bool AnimationPlayer::has_animation(const StringName& p_name) const {
- return animation_set.has(p_name);
- }
- Ref<Animation> AnimationPlayer::get_animation(const StringName& p_name) const {
- ERR_FAIL_COND_V( !animation_set.has(p_name), Ref<Animation>() );
- const AnimationData& data = animation_set[p_name];
-
- return data.animation;
- }
- void AnimationPlayer::get_animation_list( List<StringName> * p_animations) const {
- List<String> anims;
- for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
- anims.push_back(E->key());
- }
- anims.sort();
- for(List<String>::Element *E=anims.front();E;E=E->next()) {
- p_animations->push_back(E->get());
- }
- }
- void AnimationPlayer::set_blend_time(const StringName& p_animation1, const StringName& p_animation2, float p_time) {
- ERR_FAIL_COND(p_time<0);
- BlendKey bk;
- bk.from=p_animation1;
- bk.to=p_animation2;
- if (p_time==0)
- blend_times.erase(bk);
- else
- blend_times[bk]=p_time;
- }
- float AnimationPlayer::get_blend_time( const StringName& p_animation1, const StringName& p_animation2) const {
- BlendKey bk;
- bk.from=p_animation1;
- bk.to=p_animation2;
- if (blend_times.has(bk))
- return blend_times[bk];
- else
- return 0;
- }
- void AnimationPlayer::queue(const StringName& p_name) {
- if (!is_playing())
- play(p_name);
- else
- queued.push_back(p_name);
- }
- void AnimationPlayer::clear_queue() {
- queued.clear();
- };
- void AnimationPlayer::play(const StringName& p_name, float p_custom_blend, float p_custom_scale,bool p_from_end) {
- //printf("animation is %ls\n", String(p_name).c_str());
- //ERR_FAIL_COND(!is_inside_scene());
- StringName name=p_name;
-
- if (String(name)=="")
- name=playback.assigned;
- if (!animation_set.has(name)) {
- ERR_EXPLAIN("Animation not found: "+name);
- ERR_FAIL();
- }
-
- Playback &c=playback;
- if (c.current.from) {
-
- float blend_time=0;
- // find if it can blend
- BlendKey bk;
- bk.from=c.current.from->name;
- bk.to=name;
- if (p_custom_blend>=0) {
- blend_time=p_custom_blend;
- } else if (blend_times.has(bk)) {
-
- blend_time=blend_times[bk];
- } else {
-
- bk.from="*";
- if (blend_times.has(bk)) {
- blend_time=blend_times[bk];
- } else {
-
- bk.from=c.current.from->name;
- bk.to="*";
-
- if (blend_times.has(bk)) {
-
- blend_time=blend_times[bk];
- }
- }
- }
-
- if (p_custom_blend<0 && blend_time==0 && default_blend_time)
- blend_time=default_blend_time;
- if (blend_time>0) {
-
- Blend b;
- b.data=c.current;
- b.blend_time=b.blend_left=blend_time;
- c.blend.push_back(b);
- }
- }
-
- c.current.from=&animation_set[name];
- c.current.pos=p_from_end ? c.current.from->animation->get_length() : 0;
- c.current.speed_scale=p_custom_scale;
- c.assigned=p_name;
- if (!end_notify)
- queued.clear();
- _set_process(true); // always process when starting an animation
- playing = true;
- if (is_inside_scene() && get_scene()->is_editor_hint())
- return; // no next in this case
- StringName next=animation_get_next(p_name);
- if (next!=StringName()) {
- queue(next);
- }
- }
- bool AnimationPlayer::is_playing() const {
- return playing;
- /*
- if (playback.current.from==NULL)
- return false;
- float len=playback.current.from->animation->get_length();
- float pos = playback.current.pos;
- bool loop=playback.current.from->animation->has_loop();
- if (!loop && pos >= len) {
- return false;
- };
- return true;
- */
- }
- void AnimationPlayer::set_current_animation(const String& p_anim) {
- if (is_playing()) {
- play(p_anim);
- } else {
- ERR_FAIL_COND(!animation_set.has(p_anim));
- playback.current.pos=0;
- playback.current.from=&animation_set[p_anim];
- playback.assigned=p_anim;
- }
- }
- String AnimationPlayer::get_current_animation() const {
- return (playback.assigned);
- }
- void AnimationPlayer::stop() {
-
- Playback &c=playback;
- c.blend.clear();
- c.current.from=NULL;
- _set_process(false);
- queued.clear();
- playing = false;
- }
- void AnimationPlayer::stop_all() {
- stop();
-
- _set_process(false); // always process when starting an animation
-
- }
- void AnimationPlayer::set_speed(float p_speed) {
- speed_scale=p_speed;
-
- }
- float AnimationPlayer::get_speed() const {
- return speed_scale;
- }
- void AnimationPlayer::seek(float p_time,bool p_update) {
- if (!playback.current.from) {
- if (playback.assigned)
- set_current_animation(playback.assigned);
- ERR_FAIL_COND(!playback.current.from);
- }
- playback.current.pos=p_time;
- if (p_update) {
- _animation_process(0);
- }
- }
- void AnimationPlayer::seek_delta(float p_time,float p_delta) {
- if (!playback.current.from) {
- if (playback.assigned)
- set_current_animation(playback.assigned);
- ERR_FAIL_COND(!playback.current.from);
- }
- playback.current.pos=p_time-p_delta;
- if (speed_scale!=0.0)
- p_delta/=speed_scale;
- _animation_process(p_delta);
- //playback.current.pos=p_time;
- }
- bool AnimationPlayer::is_valid() const {
- return (playback.current.from);
- }
- float AnimationPlayer::get_current_animation_pos() const {
- ERR_FAIL_COND_V(!playback.current.from,0);
- return playback.current.pos;
- }
- float AnimationPlayer::get_current_animation_length() const {
- ERR_FAIL_COND_V(!playback.current.from,0);
- return playback.current.from->animation->get_length();
- }
- void AnimationPlayer::_animation_changed() {
- clear_caches();
- }
- void AnimationPlayer::_node_removed(Node *p_node) {
- clear_caches(); // nodes contained here ar being removed, clear the caches
- }
- void AnimationPlayer::clear_caches() {
-
- node_cache_map.clear();
- for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
- E->get().node_cache.clear();
- }
- cache_update_size=0;
- cache_update_prop_size=0;
- }
- void AnimationPlayer::set_active(bool p_active) {
- if (active==p_active)
- return;
- active=p_active;
- _set_process(processing,true);
- }
- bool AnimationPlayer::is_active() const {
- return active;
- }
- StringName AnimationPlayer::find_animation(const Ref<Animation>& p_animation) const {
- for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
- if (E->get().animation==p_animation)
- return E->key();
- }
- return "";
- }
- void AnimationPlayer::set_autoplay(const String& p_name) {
- autoplay=p_name;
- }
- String AnimationPlayer::get_autoplay() const{
- return autoplay;
- }
- void AnimationPlayer::set_animation_process_mode(AnimationProcessMode p_mode) {
- if (animation_process_mode==p_mode)
- return;
- bool pr = processing;
- if (pr)
- _set_process(false);
- animation_process_mode=p_mode;
- if (pr)
- _set_process(true);
- }
- AnimationPlayer::AnimationProcessMode AnimationPlayer::get_animation_process_mode() const{
- return animation_process_mode;
- }
- void AnimationPlayer::_set_process(bool p_process,bool p_force) {
- if (processing==p_process && !p_force)
- return;
- switch(animation_process_mode) {
- case ANIMATION_PROCESS_FIXED: set_fixed_process(p_process && active); break;
- case ANIMATION_PROCESS_IDLE: set_process(p_process && active); break;
- }
- processing=p_process;
- }
- void AnimationPlayer::animation_set_next(const StringName& p_animation, const StringName& p_next) {
- ERR_FAIL_COND(!animation_set.has(p_animation));
- animation_set[p_animation].next=p_next;
- }
- StringName AnimationPlayer::animation_get_next(const StringName& p_animation) const{
- if (!animation_set.has(p_animation))
- return StringName();
- return animation_set[p_animation].next;
- }
- void AnimationPlayer::set_default_blend_time(float p_default) {
- default_blend_time=p_default;
- }
- float AnimationPlayer::get_default_blend_time() const {
- return default_blend_time;
- }
- void AnimationPlayer::set_root(const NodePath& p_root) {
- root=p_root;
- clear_caches();
- }
- NodePath AnimationPlayer::get_root() const {
- return root;
- }
- void AnimationPlayer::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("_node_removed"),&AnimationPlayer::_node_removed);
- ObjectTypeDB::bind_method(_MD("_animation_changed"),&AnimationPlayer::_animation_changed);
- ObjectTypeDB::bind_method(_MD("add_animation","name","animation:Animation"),&AnimationPlayer::add_animation);
- ObjectTypeDB::bind_method(_MD("remove_animation","name"),&AnimationPlayer::remove_animation);
- ObjectTypeDB::bind_method(_MD("rename_animation","name","newname"),&AnimationPlayer::rename_animation);
- ObjectTypeDB::bind_method(_MD("has_animation","name"),&AnimationPlayer::has_animation);
- ObjectTypeDB::bind_method(_MD("get_animation:Animation","name"),&AnimationPlayer::get_animation);
- ObjectTypeDB::bind_method(_MD("get_animation_list"),&AnimationPlayer::_get_animation_list);
-
- ObjectTypeDB::bind_method(_MD("set_blend_time","anim_from","anim_to","sec"),&AnimationPlayer::set_blend_time);
- ObjectTypeDB::bind_method(_MD("get_blend_time","anim_from","anim_to"),&AnimationPlayer::get_blend_time);
- ObjectTypeDB::bind_method(_MD("set_default_blend_time","sec"),&AnimationPlayer::set_default_blend_time);
- ObjectTypeDB::bind_method(_MD("get_default_blend_time"),&AnimationPlayer::get_default_blend_time);
- ObjectTypeDB::bind_method(_MD("play","name","custom_blend","custom_speed","from_end"),&AnimationPlayer::play,DEFVAL(""),DEFVAL(-1),DEFVAL(1.0),DEFVAL(false));
- ObjectTypeDB::bind_method(_MD("stop"),&AnimationPlayer::stop);
- ObjectTypeDB::bind_method(_MD("stop_all"),&AnimationPlayer::stop_all);
- ObjectTypeDB::bind_method(_MD("is_playing"),&AnimationPlayer::is_playing);
- ObjectTypeDB::bind_method(_MD("set_current_animation","anim"),&AnimationPlayer::set_current_animation);
- ObjectTypeDB::bind_method(_MD("get_current_animation"),&AnimationPlayer::get_current_animation);
- ObjectTypeDB::bind_method(_MD("queue","name"),&AnimationPlayer::queue);
- ObjectTypeDB::bind_method(_MD("clear_queue"),&AnimationPlayer::clear_queue);
- ObjectTypeDB::bind_method(_MD("set_active","active"),&AnimationPlayer::set_active);
- ObjectTypeDB::bind_method(_MD("is_active"),&AnimationPlayer::is_active);
-
- ObjectTypeDB::bind_method(_MD("set_speed","speed"),&AnimationPlayer::set_speed);
- ObjectTypeDB::bind_method(_MD("get_speed"),&AnimationPlayer::get_speed);
- ObjectTypeDB::bind_method(_MD("set_autoplay","name"),&AnimationPlayer::set_autoplay);
- ObjectTypeDB::bind_method(_MD("get_autoplay"),&AnimationPlayer::get_autoplay);
- ObjectTypeDB::bind_method(_MD("set_root","path"),&AnimationPlayer::set_root);
- ObjectTypeDB::bind_method(_MD("get_root"),&AnimationPlayer::get_root);
- ObjectTypeDB::bind_method(_MD("seek","pos_sec","update"),&AnimationPlayer::seek,DEFVAL(false));
- ObjectTypeDB::bind_method(_MD("get_pos"),&AnimationPlayer::get_current_animation_pos);
- ObjectTypeDB::bind_method(_MD("find_animation","animation:Animation"),&AnimationPlayer::find_animation);
- ObjectTypeDB::bind_method(_MD("clear_caches"),&AnimationPlayer::clear_caches);
- ObjectTypeDB::bind_method(_MD("set_animation_process_mode","mode"),&AnimationPlayer::set_animation_process_mode);
- ObjectTypeDB::bind_method(_MD("get_animation_process_mode"),&AnimationPlayer::get_animation_process_mode);
- ObjectTypeDB::bind_method(_MD("get_current_animation_pos"),&AnimationPlayer::get_current_animation_pos);
- ObjectTypeDB::bind_method(_MD("get_current_animation_length"),&AnimationPlayer::get_current_animation_length);
- ADD_PROPERTY( PropertyInfo( Variant::INT, "playback/process_mode", PROPERTY_HINT_ENUM, "Fixed,Idle"), _SCS("set_animation_process_mode"), _SCS("get_animation_process_mode"));
- ADD_PROPERTY( PropertyInfo( Variant::REAL, "playback/default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01"), _SCS("set_default_blend_time"), _SCS("get_default_blend_time"));
- ADD_PROPERTY( PropertyInfo( Variant::NODE_PATH, "root/root"), _SCS("set_root"), _SCS("get_root"));
- ADD_SIGNAL( MethodInfo("finished") );
- ADD_SIGNAL( MethodInfo("animation_changed", PropertyInfo(Variant::STRING,"old_name"), PropertyInfo(Variant::STRING,"new_name")) );
- BIND_CONSTANT( ANIMATION_PROCESS_FIXED );
- BIND_CONSTANT( ANIMATION_PROCESS_IDLE );
- }
- AnimationPlayer::AnimationPlayer() {
- accum_pass=1;
- cache_update_size=0;
- cache_update_prop_size=0;
- speed_scale=1;
- end_notify=false;
- animation_process_mode=ANIMATION_PROCESS_IDLE;
- processing=false;
- default_blend_time=0;
- root=NodePath("..");
- playing = false;
- active=true;
- }
- AnimationPlayer::~AnimationPlayer()
- {
- }
|