Debian Codegraph @

editor/scene/sprite_frames_editor_plugin.h

raw symbols SCIP
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) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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 #pragma once
32
33 #include "editor/docks/editor_dock.h"
34 #include "editor/plugins/editor_plugin.h"
35 #include "scene/gui/button.h"
36 #include "scene/gui/dialogs.h"
37 #include "scene/gui/item_list.h"
38 #include "scene/gui/line_edit.h"
39 #include "scene/gui/scroll_container.h"
40 #include "scene/gui/spin_box.h"
41 #include "scene/gui/texture_rect.h"
42 #include "scene/gui/tree.h"
43 #include "scene/resources/image_texture.h"
44 #include "scene/resources/sprite_frames.h"
45
46 class OptionButton;
47 class EditorFileDialog;
48
49 class ClipboardSpriteFrames : public Resource {
50 GDCLASS(ClipboardSpriteFrames, Resource);
51
52 public:
53 struct Frame {
54 Ref<Texture2D> texture;
55 float duration;
56 };
57 Vector<Frame> frames;
58 };
59
60 class ClipboardAnimation : public Resource {
61 GDCLASS(ClipboardAnimation, Resource);
62
63 public:
64 String name;
65 Vector<ClipboardSpriteFrames::Frame> frames;
66 float speed = 1.0f;
67 bool loop = false;
68
69 static Ref<ClipboardAnimation> from_sprite_frames(const Ref<SpriteFrames> &p_frames, const String &p_anim);
70 };
71
72 class SpriteFramesEditor : public EditorDock {
73 GDCLASS(SpriteFramesEditor, EditorDock);
74
75 Ref<SpriteFrames> frames;
76 Node *animated_sprite = nullptr;
77
78 enum {
79 PARAM_USE_CURRENT, // Used in callbacks to indicate `dominant_param` should be not updated.
80 PARAM_FRAME_COUNT, // Keep "Horizontal" & "Vertical" values.
81 PARAM_SIZE, // Keep "Size" values.
82 };
83 int dominant_param = PARAM_FRAME_COUNT;
84
85 enum {
86 FRAME_ORDER_SELECTION, // Order frames were selected in.
87
88 // By Row.
89 FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM,
90 FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP,
91 FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM,
92 FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP,
93
94 // By Column.
95 FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT,
96 FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT,
97 FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT,
98 FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT,
99 };
100
101 enum {
102 MENU_SHOW_IN_FILESYSTEM,
103 };
104
105 int right_clicked_frame = -1;
106
107 bool read_only = false;
108
109 Ref<Texture2D> autoplay_icon;
110 Ref<Texture2D> stop_icon;
111 Ref<Texture2D> pause_icon;
112 Ref<Texture2D> empty_icon = memnew(ImageTexture);
113
114 HBoxContainer *playback_container = nullptr;
115 Button *stop = nullptr;
116 Button *play = nullptr;
117 Button *play_from = nullptr;
118 Button *play_bw = nullptr;
119 Button *play_bw_from = nullptr;
120
121 Button *load = nullptr;
122 Button *load_sheet = nullptr;
123 Button *delete_frame = nullptr;
124 Button *copy = nullptr;
125 Button *paste = nullptr;
126 Button *empty_before = nullptr;
127 Button *empty_after = nullptr;
128 Button *move_up = nullptr;
129 Button *move_down = nullptr;
130 Button *zoom_out = nullptr;
131 Button *zoom_reset = nullptr;
132 Button *zoom_in = nullptr;
133 SpinBox *frame_duration = nullptr;
134 ItemList *frame_list = nullptr;
135 bool loading_scene;
136 Vector<int> selection;
137
138 Button *add_anim = nullptr;
139 Button *duplicate_anim = nullptr;
140 Button *cut_anim = nullptr;
141 Button *copy_anim = nullptr;
142 Button *paste_anim = nullptr;
143 Button *delete_anim = nullptr;
144 SpinBox *anim_speed = nullptr;
145 Button *anim_loop = nullptr;
146
147 HBoxContainer *autoplay_container = nullptr;
148 Button *autoplay = nullptr;
149
150 LineEdit *anim_search_box = nullptr;
151 Tree *animations = nullptr;
152
153 Label *missing_anim_label = nullptr;
154 VBoxContainer *anim_frames_vb = nullptr;
155
156 EditorFileDialog *file = nullptr;
157
158 AcceptDialog *dialog = nullptr;
159
160 PopupMenu *menu = nullptr;
161
162 StringName edited_anim;
163
164 ConfirmationDialog *delete_dialog = nullptr;
165
166 ConfirmationDialog *split_sheet_dialog = nullptr;
167 ScrollContainer *split_sheet_scroll = nullptr;
168 TextureRect *split_sheet_preview = nullptr;
169 VBoxContainer *split_sheet_settings_vb = nullptr;
170 SpinBox *split_sheet_h = nullptr;
171 SpinBox *split_sheet_v = nullptr;
172 SpinBox *split_sheet_size_x = nullptr;
173 SpinBox *split_sheet_size_y = nullptr;
174 SpinBox *split_sheet_sep_x = nullptr;
175 SpinBox *split_sheet_sep_y = nullptr;
176 SpinBox *split_sheet_offset_x = nullptr;
177 SpinBox *split_sheet_offset_y = nullptr;
178 Button *split_sheet_zoom_out = nullptr;
179 Button *split_sheet_zoom_reset = nullptr;
180 Button *split_sheet_zoom_in = nullptr;
181 Button *split_sheet_zoom_fit = nullptr;
182 Button *toggle_settings_button = nullptr;
183 OptionButton *split_sheet_order = nullptr;
184 EditorFileDialog *file_split_sheet = nullptr;
185 HashMap<int, int> frames_selected; // Key is frame index. Value is selection order.
186 HashSet<int> frames_toggled_by_mouse_hover;
187 Vector<Pair<int, int>> frames_ordered; // First is the index to be ordered by. Second is the actual frame index.
188 int selected_count = 0;
189 bool frames_need_sort = false;
190 int last_frame_selected = 0;
191
192 Size2i previous_texture_size;
193
194 float scale_ratio;
195 int thumbnail_default_size;
196 float thumbnail_zoom;
197 float max_thumbnail_zoom;
198 float min_thumbnail_zoom;
199 float sheet_zoom;
200 float max_sheet_zoom;
201 float min_sheet_zoom;
202
203 Size2i _get_frame_count() const;
204 Size2i _get_frame_size() const;
205 Size2i _get_offset() const;
206 Size2i _get_separation() const;
207
208 void _load_pressed();
209 void _file_load_request(const Vector<String> &p_path, int p_at_pos = -1);
210 void _copy_pressed();
211 void _paste_pressed();
212 void _paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames);
213 void _paste_texture(const Ref<Texture2D> &p_texture);
214
215 void _empty_pressed();
216 void _empty2_pressed();
217 void _delete_pressed();
218 void _up_pressed();
219 void _down_pressed();
220 void _frame_duration_changed(double p_value);
221 void _update_library(bool p_skip_selector = false);
222 void _update_library_impl();
223
224 void _update_stop_icon();
225 void _play_pressed();
226 void _play_from_pressed();
227 void _play_bw_pressed();
228 void _play_bw_from_pressed();
229 void _autoplay_pressed();
230 void _stop_pressed();
231
232 void _animation_selected();
233 void _animation_name_edited();
234 void _animation_add();
235 void _animation_duplicate();
236 void _animation_cut();
237 void _animation_copy();
238 void _animation_paste();
239 void _animation_remove();
240 void _animation_remove_confirmed();
241 void _animation_search_text_changed(const String &p_text);
242 void _animation_loop_changed();
243 void _animation_speed_resized();
244 void _animation_speed_changed(double p_value);
245 void _animation_remove_undo_redo(const StringName &p_action_name, const Vector<ClipboardSpriteFrames::Frame> *p_frames = nullptr);
246
247 StringName _find_next_animation();
248 String _generate_unique_animation_name(const String &p_base_name) const;
249
250 void _frame_list_gui_input(const Ref<InputEvent> &p_event);
251 void _frame_list_item_selected(int p_index, bool p_selected);
252
253 void _menu_selected(int p_id);
254
255 void _zoom_in();
256 void _zoom_out();
257 void _zoom_reset();
258
259 bool animations_dirty = false;
260 bool pending_update = false;
261
262 bool updating;
263 bool updating_split_settings = false; // Skip SpinBox/Range callback when setting value by code.
264
265 Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
266 bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
267 void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
268
269 void _open_sprite_sheet();
270 void _auto_slice_sprite_sheet();
271 bool _matches_background_color(const Color &p_background_color, const Color &p_pixel_color);
272 Size2i _estimate_sprite_sheet_size(const Ref<Texture2D> p_texture);
273 void _prepare_sprite_sheet(const String &p_file);
274 int _sheet_preview_position_to_frame_index(const Vector2 &p_position);
275 void _sheet_preview_draw();
276 void _sheet_spin_changed(double p_value, int p_dominant_param);
277 void _sheet_preview_input(const Ref<InputEvent> &p_event);
278 void _sheet_scroll_input(const Ref<InputEvent> &p_event);
279 void _sheet_add_frames();
280 void _sheet_update_zoom_label();
281 void _sheet_zoom_on_position(float p_zoom, const Vector2 &p_position);
282 void _sheet_zoom_in();
283 void _sheet_zoom_out();
284 void _sheet_zoom_reset();
285 void _sheet_zoom_fit();
286 void _sheet_order_selected(int p_option);
287 void _sheet_select_all_frames();
288 void _sheet_clear_all_frames();
289 void _sheet_sort_frames();
290 void _toggle_show_settings();
291 void _update_show_settings();
292
293 void _edit();
294 void _fetch_sprite_node();
295 void _remove_sprite_node();
296
297 bool sprite_node_updating = false;
298 void _sync_animation();
299
300 void _select_animation(const String &p_name, bool p_update_node = true);
301 void _rename_node_animation(EditorUndoRedoManager *undo_redo, bool is_undo, const String &p_filter, const String &p_new_animation, const String &p_new_autoplay);
302
303 protected:
304 void _notification(int p_what);
305 void _node_removed(Node *p_node);
306 static void _bind_methods();
307
308 public:
309 void edit(Ref<SpriteFrames> p_frames);
310 Ref<SpriteFrames> get_sprite_frames() const;
311
312 SpriteFramesEditor();
313 };
314
315 class SpriteFramesEditorPlugin : public EditorPlugin {
316 GDCLASS(SpriteFramesEditorPlugin, EditorPlugin);
317
318 SpriteFramesEditor *frames_editor = nullptr;
319
320 public:
321 virtual String get_plugin_name() const override { return "SpriteFrames"; }
322 bool has_main_screen() const override { return false; }
323 virtual void edit(Object *p_object) override;
324 virtual bool handles(Object *p_object) const override;
325 virtual void make_visible(bool p_visible) override;
326
327 SpriteFramesEditorPlugin();
328 };