1 /*************************************************************************/
2 /*  animation_player_editor_plugin.h                                     */
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 
31 #ifndef ANIMATION_PLAYER_EDITOR_PLUGIN_H
32 #define ANIMATION_PLAYER_EDITOR_PLUGIN_H
33 
34 #include "editor/editor_node.h"
35 #include "editor/editor_plugin.h"
36 #include "scene/animation/animation_player.h"
37 #include "scene/gui/dialogs.h"
38 #include "scene/gui/slider.h"
39 #include "scene/gui/spin_box.h"
40 #include "scene/gui/texture_button.h"
41 
42 class AnimationTrackEditor;
43 class AnimationPlayerEditorPlugin;
44 
45 class AnimationPlayerEditor : public VBoxContainer {
46 
47 	GDCLASS(AnimationPlayerEditor, VBoxContainer);
48 
49 	EditorNode *editor;
50 	AnimationPlayerEditorPlugin *plugin;
51 	AnimationPlayer *player;
52 
53 	enum {
54 		TOOL_NEW_ANIM,
55 		TOOL_LOAD_ANIM,
56 		TOOL_SAVE_ANIM,
57 		TOOL_SAVE_AS_ANIM,
58 		TOOL_DUPLICATE_ANIM,
59 		TOOL_RENAME_ANIM,
60 		TOOL_EDIT_TRANSITIONS,
61 		TOOL_REMOVE_ANIM,
62 		TOOL_COPY_ANIM,
63 		TOOL_PASTE_ANIM,
64 		TOOL_EDIT_RESOURCE
65 	};
66 
67 	enum {
68 		ONION_SKINNING_ENABLE,
69 		ONION_SKINNING_PAST,
70 		ONION_SKINNING_FUTURE,
71 		ONION_SKINNING_1_STEP,
72 		ONION_SKINNING_2_STEPS,
73 		ONION_SKINNING_3_STEPS,
74 		ONION_SKINNING_LAST_STEPS_OPTION = ONION_SKINNING_3_STEPS,
75 		ONION_SKINNING_DIFFERENCES_ONLY,
76 		ONION_SKINNING_FORCE_WHITE_MODULATE,
77 		ONION_SKINNING_INCLUDE_GIZMOS,
78 	};
79 
80 	enum {
81 		ANIM_OPEN,
82 		ANIM_SAVE,
83 		ANIM_SAVE_AS
84 	};
85 
86 	enum {
87 		RESOURCE_LOAD,
88 		RESOURCE_SAVE
89 	};
90 
91 	OptionButton *animation;
92 	Button *stop;
93 	Button *play;
94 	Button *play_from;
95 	Button *play_bw;
96 	Button *play_bw_from;
97 	Button *autoplay;
98 
99 	MenuButton *tool_anim;
100 	ToolButton *onion_toggle;
101 	MenuButton *onion_skinning;
102 	ToolButton *pin;
103 	SpinBox *frame;
104 	LineEdit *scale;
105 	LineEdit *name;
106 	Label *name_title;
107 	UndoRedo *undo_redo;
108 	Ref<Texture> autoplay_icon;
109 	bool last_active;
110 	float timeline_position;
111 
112 	EditorFileDialog *file;
113 	AcceptDialog *accept;
114 	ConfirmationDialog *delete_dialog;
115 	int current_option;
116 
117 	struct BlendEditor {
118 
119 		AcceptDialog *dialog;
120 		Tree *tree;
121 		OptionButton *next;
122 
123 	} blend_editor;
124 
125 	ConfirmationDialog *name_dialog;
126 	ConfirmationDialog *error_dialog;
127 	bool renaming;
128 
129 	bool updating;
130 	bool updating_blends;
131 
132 	AnimationTrackEditor *track_editor;
133 
134 	// Onion skinning.
135 	struct {
136 		// Settings.
137 		bool enabled;
138 		bool past;
139 		bool future;
140 		int steps;
141 		bool differences_only;
142 		bool force_white_modulate;
143 		bool include_gizmos;
144 
get_needed_capture_count__anon6671c8650508145 		int get_needed_capture_count() const {
146 			// 'Differences only' needs a capture of the present.
147 			return (past && future ? 2 * steps : steps) + (differences_only ? 1 : 0);
148 		}
149 
150 		// Rendering.
151 		int64_t last_frame;
152 		int can_overlay;
153 		Size2 capture_size;
154 		Vector<RID> captures;
155 		Vector<bool> captures_valid;
156 		struct {
157 			RID canvas;
158 			RID canvas_item;
159 			Ref<ShaderMaterial> material;
160 			Ref<Shader> shader;
161 		} capture;
162 	} onion;
163 
164 	void _select_anim_by_name(const String &p_anim);
165 	double _get_editor_step() const;
166 	void _play_pressed();
167 	void _play_from_pressed();
168 	void _play_bw_pressed();
169 	void _play_bw_from_pressed();
170 	void _autoplay_pressed();
171 	void _stop_pressed();
172 	void _animation_selected(int p_which);
173 	void _animation_new();
174 	void _animation_rename();
175 	void _animation_name_edited();
176 	void _animation_load();
177 
178 	void _animation_save_in_path(const Ref<Resource> &p_resource, const String &p_path);
179 	void _animation_save(const Ref<Resource> &p_resource);
180 	void _animation_save_as(const Ref<Resource> &p_resource);
181 
182 	void _animation_remove();
183 	void _animation_remove_confirmed();
184 	void _animation_blend();
185 	void _animation_edit();
186 	void _animation_duplicate();
187 	void _animation_resource_edit();
188 	void _scale_changed(const String &p_scale);
189 	void _dialog_action(String p_file);
190 	void _seek_frame_changed(const String &p_frame);
191 	void _seek_value_changed(float p_value, bool p_set = false);
192 	void _blend_editor_next_changed(const int p_idx);
193 
194 	void _list_changed();
195 	void _update_animation();
196 	void _update_player();
197 	void _blend_edited();
198 
199 	void _animation_player_changed(Object *p_pl);
200 
201 	void _animation_key_editor_seek(float p_pos, bool p_drag);
202 	void _animation_key_editor_anim_len_changed(float p_len);
203 
204 	void _unhandled_key_input(const Ref<InputEvent> &p_ev);
205 	void _animation_tool_menu(int p_option);
206 	void _onion_skinning_menu(int p_option);
207 
208 	void _editor_visibility_changed();
209 	bool _are_onion_layers_valid();
210 	void _allocate_onion_layers();
211 	void _free_onion_layers();
212 	void _prepare_onion_layers_1();
213 	void _prepare_onion_layers_2();
214 	void _start_onion_skinning();
215 	void _stop_onion_skinning();
216 
217 	void _pin_pressed();
218 
219 	AnimationPlayerEditor();
220 	~AnimationPlayerEditor();
221 
222 protected:
223 	void _notification(int p_what);
224 	void _node_removed(Node *p_node);
225 	static void _bind_methods();
226 
227 public:
228 	AnimationPlayer *get_player() const;
229 	static AnimationPlayerEditor *singleton;
230 
is_pinned()231 	bool is_pinned() const { return pin->is_pressed(); }
unpin()232 	void unpin() { pin->set_pressed(false); }
get_track_editor()233 	AnimationTrackEditor *get_track_editor() { return track_editor; }
234 	Dictionary get_state() const;
235 	void set_state(const Dictionary &p_state);
236 
237 	void ensure_visibility();
238 
set_undo_redo(UndoRedo * p_undo_redo)239 	void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
240 	void edit(AnimationPlayer *p_player);
241 	void forward_canvas_force_draw_over_viewport(Control *p_overlay);
242 
243 	AnimationPlayerEditor(EditorNode *p_editor, AnimationPlayerEditorPlugin *p_plugin);
244 };
245 
246 class AnimationPlayerEditorPlugin : public EditorPlugin {
247 
248 	GDCLASS(AnimationPlayerEditorPlugin, EditorPlugin);
249 
250 	AnimationPlayerEditor *anim_editor;
251 	EditorNode *editor;
252 
253 protected:
254 	void _notification(int p_what);
255 
256 public:
get_state()257 	virtual Dictionary get_state() const { return anim_editor->get_state(); }
set_state(const Dictionary & p_state)258 	virtual void set_state(const Dictionary &p_state) { anim_editor->set_state(p_state); }
259 
get_name()260 	virtual String get_name() const { return "Anim"; }
has_main_screen()261 	bool has_main_screen() const { return false; }
262 	virtual void edit(Object *p_object);
263 	virtual bool handles(Object *p_object) const;
264 	virtual void make_visible(bool p_visible);
265 
forward_canvas_force_draw_over_viewport(Control * p_overlay)266 	virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay) { anim_editor->forward_canvas_force_draw_over_viewport(p_overlay); }
267 
268 	AnimationPlayerEditorPlugin(EditorNode *p_node);
269 	~AnimationPlayerEditorPlugin();
270 };
271 
272 #endif // ANIMATION_PLAYER_EDITOR_PLUGIN_H
273