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_player_changed(Object *p_pl) {

	if (player==p_pl && is_visible()) {

		_update_player();
		if (blend_editor.dialog->is_visible())
			_animation_blend(); //update
	}
}
void AnimationPlayerEditor::_node_removed(Node *p_node) {

	if (player && player == p_node) {
		player=NULL;

		set_process(false);

		key_editor->set_animation(Ref<Animation>());
		key_editor->set_root(NULL);
		_update_player();
		//editor->animation_editor_make_visible(false);

	}
}
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::edit(AnimationPlayer *p_player) {


    if (player && pin->is_pressed())
        return; //ignore, pinned
    player=p_player;

    if (player) {
        _update_player();
        key_editor->show_select_node_warning(false);
    } else {
        key_editor->show_select_node_warning(true);

//		hide();

    }

}
void AnimationPlayerEditor::_list_changed() {

	if(is_visible())
		_update_player();
}
AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor) {
	editor=p_editor;
	singleton=this;

	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(TTR("Animation Player:"));
	add_child(l);*/

	HBoxContainer *hb = memnew( HBoxContainer );
	add_child(hb);


	play_bw_from = memnew( ToolButton );
	play_bw_from->set_tooltip(TTR("Play backwards selected animation from current pos. (A)"));
	hb->add_child(play_bw_from);

	play_bw = memnew( ToolButton );
	play_bw->set_tooltip(TTR("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(TTR("Stop animation playback. (S)"));

	play = memnew( ToolButton );
	play->set_tooltip(TTR("Play selected animation from start. (Shift+D)"));
	hb->add_child(play);


	play_from = memnew( ToolButton );
	play_from->set_tooltip(TTR("Play selected animation from current pos. (D)"));
	hb->add_child(play_from);



	//pause = memnew( Button );
	//pause->set_toggle_mode(true);
	//hb->add_child(pause);

	frame = memnew( SpinBox );
	hb->add_child(frame);
	frame->set_custom_minimum_size(Size2(60,0));
	frame->set_stretch_ratio(2);
	frame->set_tooltip(TTR("Animation position (in seconds)."));

	hb->add_child( memnew( VSeparator));

	scale = memnew( LineEdit );
	hb->add_child(scale);
	scale->set_h_size_flags(SIZE_EXPAND_FILL);
	scale->set_stretch_ratio(1);
	scale->set_tooltip(TTR("Scale animation playback globally for the node."));
	scale->hide();


	add_anim = memnew( ToolButton );
	add_anim->set_tooltip(TTR("Create new animation in player."));

	hb->add_child(add_anim);


	load_anim = memnew( ToolButton );
	load_anim->set_tooltip(TTR("Load an animation from disk."));
	hb->add_child(load_anim);

	save_anim = memnew(MenuButton);
	save_anim->set_tooltip(TTR("Save the current animation"));
	save_anim->get_popup()->add_item(TTR("Save"), ANIM_SAVE);
	save_anim->get_popup()->add_item(TTR("Save As.."), ANIM_SAVE_AS);
	save_anim->set_focus_mode(Control::FOCUS_NONE);
	hb->add_child(save_anim);

	accept = memnew(AcceptDialog);
	add_child(accept);
	accept->connect("confirmed", this, "_menu_confirm_current");

	duplicate_anim = memnew( ToolButton );
	hb->add_child(duplicate_anim);
	duplicate_anim->set_tooltip(TTR("Duplicate Animation"));

	rename_anim = memnew( ToolButton );
	hb->add_child(rename_anim);
	rename_anim->set_tooltip(TTR("Rename Animation"));

	remove_anim = memnew( ToolButton );

	hb->add_child(remove_anim);
	remove_anim->set_tooltip(TTR("Remove Animation"));


	animation = memnew( OptionButton );
	hb->add_child(animation);
	animation->set_h_size_flags(SIZE_EXPAND_FILL);
	animation->set_tooltip(TTR("Display list of animations in player."));

	autoplay = memnew( ToolButton );
	hb->add_child(autoplay);
	autoplay->set_tooltip(TTR("Autoplay On Load"));



	blend_anim = memnew( ToolButton );
	hb->add_child(blend_anim);
	blend_anim->set_tooltip(TTR("Edit Target Blend Times"));

	tool_anim = memnew( MenuButton);
	//tool_anim->set_flat(false);
	tool_anim->set_tooltip(TTR("Animation Tools"));
	tool_anim->get_popup()->add_item(TTR("Copy Animation"),TOOL_COPY_ANIM);
	tool_anim->get_popup()->add_item(TTR("Paste Animation"),TOOL_PASTE_ANIM);
	//tool_anim->get_popup()->add_separator();
	//tool_anim->get_popup()->add_item(TTR("Edit Anim Resource"),TOOL_PASTE_ANIM);
	hb->add_child(tool_anim);

	nodename = memnew( Button );
	hb->add_child(nodename);
	pin = memnew( ToolButton );
	pin->set_toggle_mode(true);
	hb->add_child(pin);



	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_title(TTR("Create New Animation"));
	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(TTR("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(TTR("Close"));
	//error_dialog->get_cancel()->set_text(TTR("Close"));
	error_dialog->set_text(TTR("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(TTR("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(TTR("Blend Times: "),blend_editor.tree,true);
	blend_editor.next = memnew( OptionButton );
	blend_vb->add_margin_child(TTR("Next (Auto Queue):"),blend_editor.next);
	blend_editor.dialog->set_title(TTR("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");

	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,"_dialog_action");
	 frame->connect("value_changed", this, "_seek_value_changed",Vector<Variant>(),true);
	 scale->connect("text_entered", this, "_scale_changed",Vector<Variant>(),true);



	renaming=false;
	last_active=false;

	set_process_unhandled_key_input(true);

	key_editor = memnew( AnimationKeyEditor);
	add_child(key_editor);
	add_constant_override("separation",get_constant("separation","VBoxContainer"));
	key_editor->set_v_size_flags(SIZE_EXPAND_FILL);
	key_editor->connect("timeline_changed",this,"_animation_key_editor_seek");
	key_editor->connect("animation_len_changed",this,"_animation_key_editor_anim_len_changed");
	key_editor->connect("animation_step_changed",this,"_animation_key_editor_anim_step_changed");

	_update_player();
}