1 /*************************************************************************/
2 /*  sprite_frames_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 SPRITE_FRAMES_EDITOR_PLUGIN_H
32 #define SPRITE_FRAMES_EDITOR_PLUGIN_H
33 
34 #include "editor/editor_node.h"
35 #include "editor/editor_plugin.h"
36 #include "scene/2d/animated_sprite.h"
37 #include "scene/gui/dialogs.h"
38 #include "scene/gui/file_dialog.h"
39 #include "scene/gui/split_container.h"
40 #include "scene/gui/texture_rect.h"
41 #include "scene/gui/tree.h"
42 
43 class SpriteFramesEditor : public HSplitContainer {
44 
45 	GDCLASS(SpriteFramesEditor, HSplitContainer);
46 
47 	ToolButton *load;
48 	ToolButton *load_sheet;
49 	ToolButton *_delete;
50 	ToolButton *copy;
51 	ToolButton *paste;
52 	ToolButton *empty;
53 	ToolButton *empty2;
54 	ToolButton *move_up;
55 	ToolButton *move_down;
56 	ItemList *tree;
57 	bool loading_scene;
58 	int sel;
59 
60 	HSplitContainer *split;
61 	ToolButton *new_anim;
62 	ToolButton *remove_anim;
63 
64 	Tree *animations;
65 	SpinBox *anim_speed;
66 	CheckButton *anim_loop;
67 
68 	EditorFileDialog *file;
69 
70 	AcceptDialog *dialog;
71 
72 	SpriteFrames *frames;
73 
74 	StringName edited_anim;
75 
76 	ConfirmationDialog *delete_dialog;
77 
78 	ConfirmationDialog *split_sheet_dialog;
79 	ScrollContainer *splite_sheet_scroll;
80 	TextureRect *split_sheet_preview;
81 	SpinBox *split_sheet_h;
82 	SpinBox *split_sheet_v;
83 	EditorFileDialog *file_split_sheet;
84 	Set<int> frames_selected;
85 	int last_frame_selected;
86 
87 	void _load_pressed();
88 	void _load_scene_pressed();
89 	void _file_load_request(const PoolVector<String> &p_path, int p_at_pos = -1);
90 	void _copy_pressed();
91 	void _paste_pressed();
92 	void _empty_pressed();
93 	void _empty2_pressed();
94 	void _delete_pressed();
95 	void _up_pressed();
96 	void _down_pressed();
97 	void _update_library(bool p_skip_selector = false);
98 
99 	void _animation_select();
100 	void _animation_name_edited();
101 	void _animation_add();
102 	void _animation_remove();
103 	void _animation_remove_confirmed();
104 	void _animation_loop_changed();
105 	void _animation_fps_changed(double p_value);
106 
107 	bool updating;
108 
109 	UndoRedo *undo_redo;
110 
111 	bool _is_drop_valid(const Dictionary &p_drag_data, const Dictionary &p_item_data) const;
112 	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
113 	bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
114 	void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
115 
116 	void _open_sprite_sheet();
117 	void _prepare_sprite_sheet(const String &p_file);
118 	void _sheet_preview_draw();
119 	void _sheet_spin_changed(double);
120 	void _sheet_preview_input(const Ref<InputEvent> &p_event);
121 	void _sheet_add_frames();
122 	void _sheet_select_clear_all_frames();
123 
124 protected:
125 	void _notification(int p_what);
126 	void _gui_input(Ref<InputEvent> p_event);
127 	static void _bind_methods();
128 
129 public:
set_undo_redo(UndoRedo * p_undo_redo)130 	void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
131 
132 	void edit(SpriteFrames *p_frames);
133 	SpriteFramesEditor();
134 };
135 
136 class SpriteFramesEditorPlugin : public EditorPlugin {
137 
138 	GDCLASS(SpriteFramesEditorPlugin, EditorPlugin);
139 
140 	SpriteFramesEditor *frames_editor;
141 	EditorNode *editor;
142 	Button *button;
143 
144 public:
get_name()145 	virtual String get_name() const { return "SpriteFrames"; }
has_main_screen()146 	bool has_main_screen() const { return false; }
147 	virtual void edit(Object *p_object);
148 	virtual bool handles(Object *p_object) const;
149 	virtual void make_visible(bool p_visible);
150 
151 	SpriteFramesEditorPlugin(EditorNode *p_node);
152 	~SpriteFramesEditorPlugin();
153 };
154 
155 #endif // SPRITE_FRAMES_EDITOR_PLUGIN_H
156