123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416 |
- /*************************************************************************/
- /* animation_player_editor_plugin.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2015 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_editor_plugin.h"
- #include "io/resource_loader.h"
- #include "os/keyboard.h"
- #include "tools/editor/editor_settings.h"
- void AnimationPlayerEditor::_node_removed(Node *p_node) {
- if (player && player == p_node) {
- player=NULL;
- hide();
- set_process(false);
- if (edit_anim->is_pressed()) {
- editor->get_animation_editor()->set_animation(Ref<Animation>());
- editor->get_animation_editor()->set_root(NULL);
- editor->animation_editor_make_visible(false);
- edit_anim->set_pressed(false);
- }
- }
- }
- void AnimationPlayerEditor::_input_event(InputEvent p_event) {
- }
- void AnimationPlayerEditor::_notification(int p_what) {
- if (p_what==NOTIFICATION_PROCESS) {
- if (!player)
- return;
- updating = true;
- if (player->is_playing()) {
- {
- String animname=player->get_current_animation();
- if (player->has_animation(animname)) {
- Ref<Animation> anim = player->get_animation(animname);
- if (!anim.is_null()) {
- seek->set_max(anim->get_length());
- }
- }
- }
- seek->set_val(player->get_current_animation_pos());
- if (edit_anim->is_pressed())
- editor->get_animation_editor()->set_anim_pos(player->get_current_animation_pos());
- EditorNode::get_singleton()->get_property_editor()->refresh();
- } else if (last_active) {
- //need the last frame after it stopped
- seek->set_val(player->get_current_animation_pos());
- }
- last_active=player->is_playing();
- //seek->set_val(player->get_pos());
- updating = false;
- }
- if (p_what==NOTIFICATION_ENTER_TREE) {
- editor->connect("hide_animation_player_editors",this,"_hide_anim_editors");
- add_anim->set_icon( get_icon("New","EditorIcons") );
- rename_anim->set_icon( get_icon("Rename","EditorIcons") );
- duplicate_anim->set_icon( get_icon("Duplicate","EditorIcons") );
- autoplay->set_icon( get_icon("AutoPlay","EditorIcons") );
- load_anim->set_icon( get_icon("Folder","EditorIcons") );
- remove_anim->set_icon( get_icon("Remove","EditorIcons") );
- edit_anim->set_icon( get_icon("Edit","EditorIcons") );
- blend_anim->set_icon( get_icon("Blend","EditorIcons") );
- play->set_icon( get_icon("PlayStart","EditorIcons") );
- play_from->set_icon( get_icon("Play","EditorIcons") );
- play_bw->set_icon( get_icon("PlayStartBackwards","EditorIcons") );
- play_bw_from->set_icon( get_icon("PlayBackwards","EditorIcons") );
- autoplay_icon=get_icon("AutoPlay","EditorIcons");
- stop->set_icon( get_icon("Stop","EditorIcons") );
- resource_edit_anim->set_icon( get_icon("EditResource","EditorIcons") );
- pin->set_normal_texture(get_icon("Pin","EditorIcons") );
- pin->set_pressed_texture( get_icon("PinPressed","EditorIcons") );
- tool_anim->set_icon(get_icon("Tools","EditorIcons"));
- tool_anim->get_popup()->connect("item_pressed",this,"_animation_tool_menu");
- blend_editor.next->connect("text_changed",this,"_blend_editor_next_changed");
- /*
- anim_editor_load->set_normal_texture( get_icon("AnimGet","EditorIcons"));
- anim_editor_store->set_normal_texture( get_icon("AnimSet","EditorIcons"));
- anim_editor_load->set_pressed_texture( get_icon("AnimGet","EditorIcons"));
- anim_editor_store->set_pressed_texture( get_icon("AnimSet","EditorIcons"));
- anim_editor_load->set_hover_texture( get_icon("AnimGetHl","EditorIcons"));
- anim_editor_store->set_hover_texture( get_icon("AnimSetHl","EditorIcons"));
- */
- }
- if (p_what==NOTIFICATION_READY) {
- get_tree()->connect("node_removed",this,"_node_removed");
- }
- if (p_what==NOTIFICATION_DRAW) {
- }
- }
- void AnimationPlayerEditor::_autoplay_pressed() {
- if (updating)
- return;
- if (animation->get_item_count()==0) {
- return;
- }
- String current = animation->get_item_text( animation->get_selected() );
- if (player->get_autoplay()==current) {
- //unset
- undo_redo->create_action("Toggle Autoplay");
- undo_redo->add_do_method(player,"set_autoplay","");
- undo_redo->add_undo_method(player,"set_autoplay",player->get_autoplay());
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- } else {
- //set
- undo_redo->create_action("Toggle Autoplay");
- undo_redo->add_do_method(player,"set_autoplay",current);
- undo_redo->add_undo_method(player,"set_autoplay",player->get_autoplay());
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- }
- }
- void AnimationPlayerEditor::_play_pressed() {
- String current;
- if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
- current = animation->get_item_text( animation->get_selected() );
- }
- if (current!="") {
- if (current==player->get_current_animation())
- player->stop(); //so it wont blend with itself
- player->play(current );
- }
- //unstop
- stop->set_pressed(false);
- //unpause
- //pause->set_pressed(false);
- }
- void AnimationPlayerEditor::_play_from_pressed() {
- String current;
- if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
- current = animation->get_item_text( animation->get_selected() );
- }
- if (current!="") {
- float time = player->get_current_animation_pos();
- if (current==player->get_current_animation() && player->is_playing()) {
- player->stop(); //so it wont blend with itself
- }
- player->play( current );
- player->seek(time);
- }
- //unstop
- stop->set_pressed(false);
- //unpause
- //pause->set_pressed(false);
- }
- void AnimationPlayerEditor::_play_bw_pressed() {
- String current;
- if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
- current = animation->get_item_text( animation->get_selected() );
- }
- if (current!="") {
- if (current==player->get_current_animation())
- player->stop(); //so it wont blend with itself
- player->play(current,-1,-1,true);
- }
- //unstop
- stop->set_pressed(false);
- //unpause
- //pause->set_pressed(false);
- }
- void AnimationPlayerEditor::_play_bw_from_pressed() {
- String current;
- if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
- current = animation->get_item_text( animation->get_selected() );
- }
- if (current!="") {
- float time = player->get_current_animation_pos();
- if (current==player->get_current_animation())
- player->stop(); //so it wont blend with itself
- player->play(current,-1,-1,true);
- player->seek(time);
- }
- //unstop
- stop->set_pressed(false);
- //unpause
- //pause->set_pressed(false);
- }
- void AnimationPlayerEditor::_stop_pressed() {
- player->stop(false);
- play->set_pressed(false);
- stop->set_pressed(true);
- //pause->set_pressed(false);
- //player->set_pause(false);
- }
- void AnimationPlayerEditor::_pause_pressed() {
- //player->set_pause( pause->is_pressed() );
- }
- void AnimationPlayerEditor::_animation_selected(int p_which) {
- if (updating)
- return;
- // when selecting an animation, the idea is that the only interesting behavior
- // ui-wise is that it should play/blend the next one if currently playing
- String current;
- if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
- current = animation->get_item_text( animation->get_selected() );
- }
- if (current!="") {
- player->set_current_animation( current );
- Ref<Animation> anim = player->get_animation(current);
- if (edit_anim->is_pressed()) {
- Ref<Animation> anim = player->get_animation(current);
- editor->get_animation_editor()->set_animation(anim);
- Node *root = player->get_node(player->get_root());
- if (root) {
- editor->get_animation_editor()->set_root(root);
- }
- }
- seek->set_max(anim->get_length());
- } else {
- if (edit_anim->is_pressed()) {
- editor->get_animation_editor()->set_animation(Ref<Animation>());
- editor->get_animation_editor()->set_root(NULL);
- }
- }
- autoplay->set_pressed(current==player->get_autoplay());
- }
- void AnimationPlayerEditor::_animation_new() {
- renaming=false;
- name_title->set_text("New Animation Name:");
- int count=1;
- String base="New Anim";
- while(true) {
- String attempt = base;
- if (count>1)
- attempt+=" ("+itos(count)+")";
- if (player->has_animation(attempt)) {
- count++;
- continue;
- }
- base=attempt;
- break;
- }
- name->set_text(base);
- name_dialog->popup_centered(Size2(300,90));
- name->select_all();
- name->grab_focus();
- }
- void AnimationPlayerEditor::_animation_rename() {
- if (animation->get_item_count()==0)
- return;
- int selected = animation->get_selected();
- String selected_name = animation->get_item_text(selected);
- name_title->set_text("Change Animation Name:");
- name->set_text(selected_name);
- renaming=true;
- name_dialog->popup_centered(Size2(300,90));
- name->select_all();
- name->grab_focus();
- }
- void AnimationPlayerEditor::_animation_load() {
- ERR_FAIL_COND(!player);
- file->set_mode( EditorFileDialog::MODE_OPEN_FILE );
- file->clear_filters();
- List<String> extensions;
- ResourceLoader::get_recognized_extensions_for_type("Animation",&extensions);
- for (List<String>::Element *E=extensions.front();E;E=E->next()) {
- file->add_filter("*."+E->get()+" ; "+E->get().to_upper() );
- }
- file->popup_centered_ratio();
- }
- void AnimationPlayerEditor::_animation_remove() {
- if (animation->get_item_count()==0)
- return;
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- undo_redo->create_action("Remove Animation");
- undo_redo->add_do_method(player,"remove_animation",current);
- undo_redo->add_undo_method(player,"add_animation",current,anim);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- }
- void AnimationPlayerEditor::_select_anim_by_name(const String& p_anim) {
- int idx=-1;
- for(int i=0;i<animation->get_item_count();i++) {
- if (animation->get_item_text(i)==p_anim) {
- idx=i;
- break;
- }
- }
- ERR_FAIL_COND(idx==-1);
- animation->select(idx);
- _animation_selected(idx);
- }
- void AnimationPlayerEditor::_animation_name_edited() {
- player->stop();
- String new_name = name->get_text();
- if (new_name=="" || new_name.find(":")!=-1 || new_name.find("/")!=-1) {
- error_dialog->set_text("ERROR: Invalid animation name!");
- error_dialog->popup_centered_minsize();
- return;
- }
- if (renaming && animation->get_item_count()>0 && animation->get_item_text(animation->get_selected())==new_name) {
- name_dialog->hide();
- return;
- }
- if (player->has_animation(new_name)) {
- error_dialog->set_text("ERROR: Animation Name Already Exists!");
- error_dialog->popup_centered_minsize();
- return;
- }
- if (renaming) {
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- undo_redo->create_action("Rename Animation");
- undo_redo->add_do_method(player,"rename_animation",current,new_name);
- undo_redo->add_do_method(anim.ptr(),"set_name",new_name);
- undo_redo->add_undo_method(player,"rename_animation",new_name,current);
- undo_redo->add_undo_method(anim.ptr(),"set_name",current);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- _select_anim_by_name(new_name);
- } else {
- Ref<Animation> new_anim = Ref<Animation>(memnew( Animation ));
- new_anim->set_name(new_name);
- undo_redo->create_action("Add Animation");
- undo_redo->add_do_method(player,"add_animation",new_name,new_anim);
- undo_redo->add_undo_method(player,"remove_animation",new_name);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- _select_anim_by_name(new_name);
- }
- name_dialog->hide();
- }
- void AnimationPlayerEditor::_blend_editor_next_changed(const String& p_string) {
- if (animation->get_item_count()==0)
- return;
- String current = animation->get_item_text(animation->get_selected());
- player->animation_set_next(current,p_string);
- }
- void AnimationPlayerEditor::_animation_blend() {
- if (updating_blends)
- return;
- blend_editor.tree->clear();
- if (animation->get_item_count()==0)
- return;
- String current = animation->get_item_text(animation->get_selected());
- blend_editor.dialog->popup_centered(Size2(400,400));
- blend_editor.tree->set_hide_root(true);
- blend_editor.tree->set_column_min_width(0,10);
- blend_editor.tree->set_column_min_width(1,3);
- List<StringName> anims;
- player->get_animation_list(&anims);
- TreeItem *root = blend_editor.tree->create_item();
- updating_blends=true;
- for(List<StringName>::Element *E=anims.front();E;E=E->next()) {
- String to=E->get();
- TreeItem *blend=blend_editor.tree->create_item(root);
- blend->set_editable(0,false);
- blend->set_editable(1,true);
- blend->set_text(0,to);
- blend->set_cell_mode(1,TreeItem::CELL_MODE_RANGE);
- blend->set_range_config(1,0,3600,0.001);
- blend->set_range(1,player->get_blend_time(current,to));
- }
- blend_editor.next->set_text( player->animation_get_next(current) );
- updating_blends=false;
- }
- void AnimationPlayerEditor::_blend_edited() {
- if (updating_blends)
- return;
- if (animation->get_item_count()==0)
- return;
- String current = animation->get_item_text(animation->get_selected());
- TreeItem *selected = blend_editor.tree->get_edited();
- if (!selected)
- return;
- updating_blends=true;
- String to=selected->get_text(0);
- float blend_time = selected->get_range(1);
- float prev_blend_time = player->get_blend_time(current,to);
- undo_redo->create_action("Change Blend Time");
- undo_redo->add_do_method(player,"set_blend_time",current,to,blend_time);
- undo_redo->add_undo_method(player,"set_blend_time",current,to,prev_blend_time);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- updating_blends=false;
- }
- void AnimationPlayerEditor::ensure_visibility() {
- _animation_edit();
- }
- Dictionary AnimationPlayerEditor::get_state() const {
- Dictionary d;
- d["visible"]=is_visible();
- if (is_visible() && player) {
- d["player"]=EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
- d["animation"]=player->get_current_animation();
- d["editing"]=edit_anim->is_pressed();
- }
- return d;
- }
- void AnimationPlayerEditor::set_state(const Dictionary& p_state) {
- if (p_state.has("visible") && p_state["visible"]) {
- Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
- if (n && n->cast_to<AnimationPlayer>()) {
- player=n->cast_to<AnimationPlayer>();
- _update_player();
- show();
- set_process(true);
- ensure_visibility();
- EditorNode::get_singleton()->animation_panel_make_visible(true);
- if (p_state.has("animation")) {
- String anim = p_state["animation"];
- _select_anim_by_name(anim);
- if (p_state.has("editing") && p_state["editing"]) {
- edit_anim->set_pressed(true);
- _animation_edit();
- }
- }
- }
- }
- }
- void AnimationPlayerEditor::_animation_resource_edit() {
- if (animation->get_item_count()) {
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- editor->edit_resource(anim);
- }
- }
- void AnimationPlayerEditor::_animation_edit() {
- // if (animation->get_item_count()==0)
- // return;
- if (edit_anim->is_pressed()) {
- editor->animation_editor_make_visible(true);
- //editor->get_animation_editor()->set_root(player->get_roo); - get root pending
- if (animation->get_item_count()) {
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- editor->get_animation_editor()->set_animation(anim);
- Node *root = player->get_node(player->get_root());
- if (root) {
- editor->get_animation_editor()->set_root(root);
- }
- } else {
- editor->get_animation_editor()->set_animation(Ref<Animation>());
- editor->get_animation_editor()->set_root(NULL);
- }
- } else {
- editor->animation_editor_make_visible(false);
- editor->get_animation_editor()->set_animation(Ref<Animation>());
- editor->get_animation_editor()->set_root(NULL);
- }
- //get_scene()->get_root_node()->call("_resource_selected",anim,"");
- }
- void AnimationPlayerEditor::_file_selected(String p_file) {
- ERR_FAIL_COND(!player);
- Ref<Resource> res = ResourceLoader::load(p_file,"Animation");
- ERR_FAIL_COND(res.is_null());
- ERR_FAIL_COND( !res->is_type("Animation") );
- if (p_file.find_last("/")!=-1) {
- p_file=p_file.substr( p_file.find_last("/")+1, p_file.length() );
- }
- if (p_file.find_last("\\")!=-1) {
- p_file=p_file.substr( p_file.find_last("\\")+1, p_file.length() );
- }
- if (p_file.find(".")!=-1)
- p_file=p_file.substr(0,p_file.find("."));
- undo_redo->create_action("Load Animation");
- undo_redo->add_do_method(player,"add_animation",p_file,res);
- undo_redo->add_undo_method(player,"remove_animation",p_file);
- if (player->has_animation(p_file)) {
- undo_redo->add_undo_method(player,"add_animation",p_file,player->get_animation(p_file));
- }
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- }
- void AnimationPlayerEditor::_scale_changed(const String& p_scale) {
- player->set_speed(p_scale.to_double());
- }
- void AnimationPlayerEditor::_update_animation() {
- // the purpose of _update_animation is to reflect the current state
- // of the animation player in the current editor..
- updating=true;
- if (player->is_playing()) {
- play->set_pressed(true);
- stop->set_pressed(false);
- } else {
- play->set_pressed(false);
- stop->set_pressed(true);
- }
- scale->set_text( String::num(player->get_speed(),2) );
- String current=player->get_current_animation();
- for (int i=0;i<animation->get_item_count();i++) {
- if (animation->get_item_text(i)==current) {
- animation->select(i);
- break;
- }
- }
- updating=false;
- }
- void AnimationPlayerEditor::_update_player() {
- if (!player)
- return;
- updating=true;
- List<StringName> animlist;
- player->get_animation_list(&animlist);
- animation->clear();
- nodename->set_text(player->get_name());
- stop->set_disabled(animlist.size()==0);
- play->set_disabled(animlist.size()==0);
- play_bw->set_disabled(animlist.size()==0);
- play_bw_from->set_disabled(animlist.size()==0);
- play_from->set_disabled(animlist.size()==0);
- autoplay->set_disabled(animlist.size()==0);
- duplicate_anim->set_disabled(animlist.size()==0);
- rename_anim->set_disabled(animlist.size()==0);
- blend_anim->set_disabled(animlist.size()==0);
- remove_anim->set_disabled(animlist.size()==0);
- resource_edit_anim->set_disabled(animlist.size()==0);
- int active_idx=-1;
- for (List<StringName>::Element *E=animlist.front();E;E=E->next()) {
- if (player->get_autoplay()==E->get())
- animation->add_icon_item(autoplay_icon,E->get());
- else
- animation->add_item(E->get());
- if (player->get_current_animation()==E->get())
- active_idx=animation->get_item_count()-1;
- }
- updating=false;
- if (active_idx!=-1) {
- animation->select(active_idx);
- autoplay->set_pressed(animation->get_item_text(active_idx)==player->get_autoplay());
- _animation_selected(active_idx);
- } else if (animation->get_item_count()>0){
- animation->select(0);
- autoplay->set_pressed(animation->get_item_text(0)==player->get_autoplay());
- _animation_selected(0);
- }
- //pause->set_pressed(player->is_paused());
- if (edit_anim->is_pressed()) {
- if (animation->get_item_count()) {
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- editor->get_animation_editor()->set_animation(anim);
- Node *root = player->get_node(player->get_root());
- if (root) {
- editor->get_animation_editor()->set_root(root);
- }
- }
- }
- _update_animation();
- }
- void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
- if (player && pin->is_pressed())
- return; //ignore, pinned
- player=p_player;
- if (player)
- _update_player();
- else {
- // hide();
- }
- }
- void AnimationPlayerEditor::_animation_duplicate() {
- if (!animation->get_item_count())
- return;
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- if (!anim.is_valid())
- return;
- Ref<Animation> new_anim = memnew( Animation );
- List<PropertyInfo> plist;
- anim->get_property_list(&plist);
- for (List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
- if (E->get().usage&PROPERTY_USAGE_STORAGE) {
- new_anim->set(E->get().name, anim->get(E->get().name));
- }
- }
- new_anim->set_path("");
- String new_name = current;
- while(player->has_animation(new_name)) {
- new_name=new_name+" (copy)";
- }
- undo_redo->create_action("Duplicate Animation");
- undo_redo->add_do_method(player,"add_animation",new_name,new_anim);
- undo_redo->add_undo_method(player,"remove_animation",new_name);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- for(int i=0;i<animation->get_item_count();i++) {
- if (animation->get_item_text(i)==new_name) {
- animation->select(i);
- _animation_selected(i);
- return;
- }
- }
- }
- void AnimationPlayerEditor::_seek_value_changed(float p_value) {
- if (updating || !player || player->is_playing()) {
- return;
- };
- updating=true;
- String current=player->get_current_animation(); //animation->get_item_text( animation->get_selected() );
- if (current == "" || !player->has_animation(current)) {
- updating=false;
- current="";
- return;
- };
- Ref<Animation> anim;
- anim=player->get_animation(current);
- float pos = anim->get_length() * (p_value / seek->get_max());
- if (player->is_valid()) {
- float cpos = player->get_current_animation_pos();
- player->seek_delta(pos,pos-cpos);
- } else {
- player->seek(pos,true);
- }
- if (edit_anim->is_pressed())
- editor->get_animation_editor()->set_anim_pos(pos);
- updating=true;
- };
- void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
- if (player==p_pl && is_visible()) {
- _update_player();
- if (blend_editor.dialog->is_visible())
- _animation_blend(); //update
- }
- }
- void AnimationPlayerEditor::_list_changed() {
- if(is_visible())
- _update_player();
- }
- #if 0
- void AnimationPlayerEditor::_editor_store() {
- if (animation->get_item_count()==0)
- return;
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- if (editor->get_animation_editor()->get_current_animation()==anim)
- return; //already there
- undo_redo->create_action("Store anim in editor");
- undo_redo->add_do_method(editor->get_animation_editor(),"set_animation",anim);
- undo_redo->add_undo_method(editor->get_animation_editor(),"remove_animation",anim);
- undo_redo->commit_action();
- }
- void AnimationPlayerEditor::_editor_load(){
- Ref<Animation> anim = editor->get_animation_editor()->get_current_animation();
- if (anim.is_null())
- return;
- String existing = player->find_animation(anim);
- if (existing!="") {
- _select_anim_by_name(existing);
- return; //already has
- }
- int count=1;
- String base=anim->get_name();
- bool noname=false;
- if (base=="") {
- base="New Anim";
- noname=true;
- }
- while(true) {
- String attempt = base;
- if (count>1)
- attempt+=" ("+itos(count)+")";
- if (player->has_animation(attempt)) {
- count++;
- continue;
- }
- base=attempt;
- break;
- }
- if (noname)
- anim->set_name(base);
- undo_redo->create_action("Add Animation From Editor");
- undo_redo->add_do_method(player,"add_animation",base,anim);
- undo_redo->add_undo_method(player,"remove_animation",base);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- _select_anim_by_name(base);
- }
- #endif
- void AnimationPlayerEditor::_animation_key_editor_anim_len_changed(float p_len) {
- seek->set_max(p_len);
- }
- void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos) {
- if (!is_visible())
- return;
- if (!player)
- return;
- if (player->is_playing() )
- return;
- seek->set_val(p_pos);
- EditorNode::get_singleton()->get_property_editor()->refresh();
- //seekit
- }
- void AnimationPlayerEditor::_hide_anim_editors() {
- player=NULL;
- hide();
- set_process(false);
- if (edit_anim->is_pressed()) {
- editor->get_animation_editor()->set_animation(Ref<Animation>());
- editor->get_animation_editor()->set_root(NULL);
- editor->animation_editor_make_visible(false);
- edit_anim->set_pressed(false);
- }
- }
- void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
- switch(p_option) {
- case TOOL_COPY_ANIM: {
- if (!animation->get_item_count()) {
- error_dialog->set_text("ERROR: No animation to copy!");
- error_dialog->popup_centered_minsize();
- return;
- }
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- //editor->edit_resource(anim);
- EditorSettings::get_singleton()->set_resource_clipboard(anim);
- } break;
- case TOOL_PASTE_ANIM: {
- Ref<Animation> anim = EditorSettings::get_singleton()->get_resource_clipboard();
- if (!anim.is_valid()) {
- error_dialog->set_text("ERROR: No animation resource on clipboard!");
- error_dialog->popup_centered_minsize();
- return;
- }
- String name = anim->get_name();
- if (name=="") {
- name="Pasted Animation";
- }
- int idx=1;
- String base = name;
- while (player->has_animation(name)) {
- idx++;
- name=base+" "+itos(idx);
- }
- undo_redo->create_action("Paste Animation");
- undo_redo->add_do_method(player,"add_animation",name,anim);
- undo_redo->add_undo_method(player,"remove_animation",name);
- undo_redo->add_do_method(this,"_animation_player_changed",player);
- undo_redo->add_undo_method(this,"_animation_player_changed",player);
- undo_redo->commit_action();
- _select_anim_by_name(name);
- } break;
- case TOOL_EDIT_RESOURCE: {
- if (!animation->get_item_count()) {
- error_dialog->set_text("ERROR: No animation to edit!");
- error_dialog->popup_centered_minsize();
- return;
- }
- String current = animation->get_item_text(animation->get_selected());
- Ref<Animation> anim = player->get_animation(current);
- editor->edit_resource(anim);
- } break;
- }
- }
- void AnimationPlayerEditor::_unhandled_key_input(const InputEvent& p_ev) {
- if (is_visible() && p_ev.type==InputEvent::KEY && p_ev.key.pressed && !p_ev.key.echo && !p_ev.key.mod.alt && !p_ev.key.mod.control && !p_ev.key.mod.meta) {
- switch(p_ev.key.scancode) {
- case KEY_A: {
- if (!p_ev.key.mod.shift)
- _play_bw_from_pressed();
- else
- _play_bw_pressed();
- } break;
- case KEY_S: {
- _stop_pressed();
- } break;
- case KEY_D: {
- if (!p_ev.key.mod.shift)
- _play_from_pressed();
- else
- _play_pressed();
- } break;
- }
- }
- }
- void AnimationPlayerEditor::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("_input_event"),&AnimationPlayerEditor::_input_event);
- ObjectTypeDB::bind_method(_MD("_node_removed"),&AnimationPlayerEditor::_node_removed);
- ObjectTypeDB::bind_method(_MD("_play_pressed"),&AnimationPlayerEditor::_play_pressed);
- ObjectTypeDB::bind_method(_MD("_play_from_pressed"),&AnimationPlayerEditor::_play_from_pressed);
- ObjectTypeDB::bind_method(_MD("_play_bw_pressed"),&AnimationPlayerEditor::_play_bw_pressed);
- ObjectTypeDB::bind_method(_MD("_play_bw_from_pressed"),&AnimationPlayerEditor::_play_bw_from_pressed);
- ObjectTypeDB::bind_method(_MD("_stop_pressed"),&AnimationPlayerEditor::_stop_pressed);
- ObjectTypeDB::bind_method(_MD("_autoplay_pressed"),&AnimationPlayerEditor::_autoplay_pressed);
- ObjectTypeDB::bind_method(_MD("_pause_pressed"),&AnimationPlayerEditor::_pause_pressed);
- ObjectTypeDB::bind_method(_MD("_animation_selected"),&AnimationPlayerEditor::_animation_selected);
- ObjectTypeDB::bind_method(_MD("_animation_name_edited"),&AnimationPlayerEditor::_animation_name_edited);
- ObjectTypeDB::bind_method(_MD("_animation_new"),&AnimationPlayerEditor::_animation_new);
- ObjectTypeDB::bind_method(_MD("_animation_rename"),&AnimationPlayerEditor::_animation_rename);
- ObjectTypeDB::bind_method(_MD("_animation_load"),&AnimationPlayerEditor::_animation_load);
- ObjectTypeDB::bind_method(_MD("_animation_remove"),&AnimationPlayerEditor::_animation_remove);
- ObjectTypeDB::bind_method(_MD("_animation_blend"),&AnimationPlayerEditor::_animation_blend);
- ObjectTypeDB::bind_method(_MD("_animation_edit"),&AnimationPlayerEditor::_animation_edit);
- ObjectTypeDB::bind_method(_MD("_animation_resource_edit"),&AnimationPlayerEditor::_animation_resource_edit);
- ObjectTypeDB::bind_method(_MD("_file_selected"),&AnimationPlayerEditor::_file_selected);
- ObjectTypeDB::bind_method(_MD("_seek_value_changed"),&AnimationPlayerEditor::_seek_value_changed);
- ObjectTypeDB::bind_method(_MD("_animation_player_changed"),&AnimationPlayerEditor::_animation_player_changed);
- ObjectTypeDB::bind_method(_MD("_blend_edited"),&AnimationPlayerEditor::_blend_edited);
- // ObjectTypeDB::bind_method(_MD("_seek_frame_changed"),&AnimationPlayerEditor::_seek_frame_changed);
- ObjectTypeDB::bind_method(_MD("_scale_changed"),&AnimationPlayerEditor::_scale_changed);
- //ObjectTypeDB::bind_method(_MD("_editor_store_all"),&AnimationPlayerEditor::_editor_store_all);
- ///jectTypeDB::bind_method(_MD("_editor_load_all"),&AnimationPlayerEditor::_editor_load_all);
- ObjectTypeDB::bind_method(_MD("_list_changed"),&AnimationPlayerEditor::_list_changed);
- ObjectTypeDB::bind_method(_MD("_animation_key_editor_seek"),&AnimationPlayerEditor::_animation_key_editor_seek);
- ObjectTypeDB::bind_method(_MD("_animation_key_editor_anim_len_changed"),&AnimationPlayerEditor::_animation_key_editor_anim_len_changed);
- ObjectTypeDB::bind_method(_MD("_hide_anim_editors"),&AnimationPlayerEditor::_hide_anim_editors);
- ObjectTypeDB::bind_method(_MD("_animation_duplicate"),&AnimationPlayerEditor::_animation_duplicate);
- ObjectTypeDB::bind_method(_MD("_blend_editor_next_changed"),&AnimationPlayerEditor::_blend_editor_next_changed);
- ObjectTypeDB::bind_method(_MD("_unhandled_key_input"),&AnimationPlayerEditor::_unhandled_key_input);
- ObjectTypeDB::bind_method(_MD("_animation_tool_menu"),&AnimationPlayerEditor::_animation_tool_menu);
- }
- AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor) {
- editor=p_editor;
- updating=false;
- set_focus_mode(FOCUS_ALL);
- player=NULL;
- add_style_override("panel", get_stylebox("panel","Panel"));
- Label * l;
- /*l= memnew( Label );
- l->set_text("Animation Player:");
- add_child(l);*/
- HBoxContainer *hb = memnew( HBoxContainer );
- add_child(hb);
- add_anim = memnew( ToolButton );
- add_anim->set_tooltip("Create new animation in player.");
- hb->add_child(add_anim);
- load_anim = memnew( ToolButton );
- load_anim->set_tooltip("Load an animation from disk.");
- hb->add_child(load_anim);
- duplicate_anim = memnew( ToolButton );
- hb->add_child(duplicate_anim);
- duplicate_anim->set_tooltip("Duplicate Animation");
- rename_anim = memnew( ToolButton );
- hb->add_child(rename_anim);
- rename_anim->set_tooltip("Rename Animation");
- remove_anim = memnew( ToolButton );
- hb->add_child(remove_anim);
- remove_anim->set_tooltip("Remove Animation");
- animation = memnew( OptionButton );
- hb->add_child(animation);
- animation->set_h_size_flags(SIZE_EXPAND_FILL);
- animation->set_tooltip("Display list of animations in player.");
- autoplay = memnew( ToolButton );
- hb->add_child(autoplay);
- autoplay->set_tooltip("Autoplay On Load");
- blend_anim = memnew( ToolButton );
- hb->add_child(blend_anim);
- blend_anim->set_tooltip("Edit Target Blend Times");
- tool_anim = memnew( MenuButton);
- //tool_anim->set_flat(false);
- tool_anim->set_tooltip("Animation Tools");
- tool_anim->get_popup()->add_item("Copy Animation",TOOL_COPY_ANIM);
- tool_anim->get_popup()->add_item("Paste Animation",TOOL_PASTE_ANIM);
- //tool_anim->get_popup()->add_separator();
- //tool_anim->get_popup()->add_item("Edit Anim Resource",TOOL_PASTE_ANIM);
- hb->add_child(tool_anim);
- edit_anim = memnew( ToolButton );
- edit_anim->set_toggle_mode(true);
- hb->add_child(edit_anim);
- edit_anim->set_tooltip("Open animation editor.\nProperty editor will displays all editable keys too.");
- hb = memnew (HBoxContainer);
- add_child(hb);
- play_bw_from = memnew( ToolButton );
- play_bw_from->set_tooltip("Play backwards selected animation from current pos. (A)");
- hb->add_child(play_bw_from);
- play_bw = memnew( ToolButton );
- play_bw->set_tooltip("Play backwards selected animation from end. (Shift+A)");
- hb->add_child(play_bw);
- stop = memnew( ToolButton );
- stop->set_toggle_mode(true);
- hb->add_child(stop);
- stop->set_tooltip("Stop animation playback. (S)");
- play = memnew( ToolButton );
- play->set_tooltip("Play selected animation from start. (Shift+D)");
- hb->add_child(play);
- play_from = memnew( ToolButton );
- play_from->set_tooltip("Play selected animation from current pos. (D)");
- hb->add_child(play_from);
- //pause = memnew( Button );
- //pause->set_toggle_mode(true);
- //hb->add_child(pause);
- seek = memnew( HSlider );
- seek->set_val(0);
- seek->set_step(0.01);
- hb->add_child(seek);
- seek->set_h_size_flags(SIZE_EXPAND_FILL);
- seek->set_stretch_ratio(8);
- seek->set_tooltip("Seek animation (when stopped).");
- frame = memnew( SpinBox );
- hb->add_child(frame);
- frame->set_h_size_flags(SIZE_EXPAND_FILL);
- frame->set_stretch_ratio(2);
- frame->set_tooltip("Animation position (in seconds).");
- seek->share(frame);
- scale = memnew( LineEdit );
- hb->add_child(scale);
- scale->set_h_size_flags(SIZE_EXPAND_FILL);
- scale->set_stretch_ratio(1);
- scale->set_tooltip("Scale animation playback globally for the node.");
- scale->hide();
- resource_edit_anim= memnew( Button );
- hb->add_child(resource_edit_anim);
- resource_edit_anim->hide();
- file = memnew(EditorFileDialog);
- add_child(file);
- name_dialog = memnew( ConfirmationDialog );
- name_dialog->set_hide_on_ok(false);
- add_child(name_dialog);
- name = memnew( LineEdit );
- name_dialog->add_child(name);
- name->set_pos(Point2(18,30));
- name->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
- name_dialog->register_text_enter(name);
- l = memnew( Label );
- l->set_text("Animation Name:");
- l->set_pos( Point2(10,10) );
- name_dialog->add_child(l);
- name_title=l;
- error_dialog = memnew( ConfirmationDialog );
- error_dialog->get_ok()->set_text("Close");
- //error_dialog->get_cancel()->set_text("Close");
- error_dialog->set_text("Error!");
- add_child(error_dialog);
- name_dialog->connect("confirmed", this,"_animation_name_edited");
-
- blend_editor.dialog = memnew( AcceptDialog );
- add_child(blend_editor.dialog);
- blend_editor.dialog->get_ok()->set_text("Close");
- blend_editor.dialog->set_hide_on_ok(true);
- VBoxContainer *blend_vb = memnew( VBoxContainer);
- blend_editor.dialog->add_child(blend_vb);
- blend_editor.dialog->set_child_rect(blend_vb);
- blend_editor.tree = memnew( Tree );
- blend_editor.tree->set_columns(2);
- blend_vb->add_margin_child("Blend Times: ",blend_editor.tree,true);
- blend_editor.next = memnew( LineEdit );
- blend_vb->add_margin_child("Next (Auto Queue):",blend_editor.next);
- blend_editor.dialog->set_title("Cross-Animation Blend Times");
- updating_blends=false;
- blend_editor.tree->connect("item_edited",this,"_blend_edited");
-
- autoplay->connect("pressed", this,"_autoplay_pressed");
- autoplay->set_toggle_mode(true);
- play->connect("pressed", this,"_play_pressed");
- play_from->connect("pressed", this,"_play_from_pressed");
- play_bw->connect("pressed", this,"_play_bw_pressed");
- play_bw_from->connect("pressed", this,"_play_bw_from_pressed");
- stop->connect("pressed", this,"_stop_pressed");
- //pause->connect("pressed", this,"_pause_pressed");
- add_anim->connect("pressed", this,"_animation_new");
- rename_anim->connect("pressed", this,"_animation_rename");
- load_anim->connect("pressed", this,"_animation_load");
- duplicate_anim->connect("pressed", this,"_animation_duplicate");
- //frame->connect("text_entered", this,"_seek_frame_changed");
- edit_anim->connect("pressed", this,"_animation_edit");
- blend_anim->connect("pressed", this,"_animation_blend");
- remove_anim->connect("pressed", this,"_animation_remove");
- animation->connect("item_selected", this,"_animation_selected",Vector<Variant>(),true);
- resource_edit_anim->connect("pressed", this,"_animation_resource_edit");
- file->connect("file_selected", this,"_file_selected");
- seek->connect("value_changed", this, "_seek_value_changed",Vector<Variant>(),true);
- scale->connect("text_entered", this, "_scale_changed",Vector<Variant>(),true);
- editor->get_animation_editor()->connect("timeline_changed",this,"_animation_key_editor_seek");
- editor->get_animation_editor()->connect("animation_len_changed",this,"_animation_key_editor_anim_len_changed");
- HBoxContainer *ahb = editor->get_animation_panel_hb();
- nodename = memnew( Label );
- ahb->add_child(nodename);
- nodename->set_h_size_flags(SIZE_EXPAND_FILL);
- nodename->set_opacity(0.5);
- pin = memnew( TextureButton );
- pin->set_toggle_mode(true);
- ahb->add_child(pin);
- renaming=false;
- last_active=false;
- set_process_unhandled_key_input(true);
- }
- void AnimationPlayerEditorPlugin::edit(Object *p_object) {
- anim_editor->set_undo_redo(&get_undo_redo());
- if (!p_object)
- return;
- anim_editor->edit(p_object->cast_to<AnimationPlayer>());
- }
- bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
- return p_object->is_type("AnimationPlayer");
- }
- void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
- if (p_visible) {
- anim_editor->show();
- anim_editor->set_process(true);
- anim_editor->ensure_visibility();
- editor->animation_panel_make_visible(true);
- } else {
- // anim_editor->hide();
- // anim_editor->set_idle_process(false);
- }
- }
- AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin(EditorNode *p_node) {
- editor=p_node;
- anim_editor = memnew( AnimationPlayerEditor(editor) );
- anim_editor->set_undo_redo(editor->get_undo_redo());
- editor->get_animation_panel()->add_child(anim_editor);
- /*
- editor->get_viewport()->add_child(anim_editor);
- anim_editor->set_area_as_parent_rect();
- anim_editor->set_anchor( MARGIN_TOP, Control::ANCHOR_END);
- anim_editor->set_margin( MARGIN_TOP, 75 );
- anim_editor->set_anchor( MARGIN_RIGHT, Control::ANCHOR_END);
- anim_editor->set_margin( MARGIN_RIGHT, 0 );*/
- anim_editor->hide();
- }
- AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin()
- {
- }
|