Debian Codegraph @

editor/scene/sprite_frames_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* sprite_frames_editor_plugin.cpp */
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 #include "sprite_frames_editor_plugin.h"
32
33 #include "core/io/resource_loader.h"
34 #include "core/os/keyboard.h"
35 #include "core/string/translation_server.h"
36 #include "editor/docks/editor_dock_manager.h"
37 #include "editor/docks/filesystem_dock.h"
38 #include "editor/docks/scene_tree_dock.h"
39 #include "editor/editor_node.h"
40 #include "editor/editor_string_names.h"
41 #include "editor/editor_undo_redo_manager.h"
42 #include "editor/file_system/editor_file_system.h"
43 #include "editor/gui/editor_file_dialog.h"
44 #include "editor/settings/editor_command_palette.h"
45 #include "editor/settings/editor_settings.h"
46 #include "editor/themes/editor_scale.h"
47 #include "scene/2d/animated_sprite_2d.h"
48 #include "scene/3d/sprite_3d.h"
49 #include "scene/gui/center_container.h"
50 #include "scene/gui/flow_container.h"
51 #include "scene/gui/margin_container.h"
52 #include "scene/gui/option_button.h"
53 #include "scene/gui/panel_container.h"
54 #include "scene/gui/separator.h"
55 #include "scene/gui/split_container.h"
56 #include "scene/resources/atlas_texture.h"
57
58 static void _draw_shadowed_line(Control *p_control, const Point2 &p_from, const Size2 &p_size, const Size2 &p_shadow_offset, Color p_color, Color p_shadow_color) {
59 p_control->draw_line(p_from, p_from + p_size, p_color);
60 p_control->draw_line(p_from + p_shadow_offset, p_from + p_size + p_shadow_offset, p_shadow_color);
61 }
62
63 void SpriteFramesEditor::_open_sprite_sheet() {
64 file_split_sheet->clear_filters();
65 List<String> extensions;
66 ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
67 for (const String &extension : extensions) {
68 file_split_sheet->add_filter("*." + extension);
69 }
70
71 file_split_sheet->popup_file_dialog();
72 }
73
74 int SpriteFramesEditor::_sheet_preview_position_to_frame_index(const Point2 &p_position) {
75 const Size2i offset = _get_offset();
76 const Size2i frame_size = _get_frame_size();
77 const Size2i separation = _get_separation();
78 const Size2i block_size = frame_size + separation;
79 const Point2i position = p_position / sheet_zoom - offset;
80
81 if (position.x < 0 || position.y < 0) {
82 return -1; // Out of bounds.
83 }
84
85 if (position.x % block_size.x >= frame_size.x || position.y % block_size.y >= frame_size.y) {
86 return -1; // Gap between frames.
87 }
88
89 const Point2i frame = position / block_size;
90 const Size2i frame_count = _get_frame_count();
91 if (frame.x >= frame_count.x || frame.y >= frame_count.y) {
92 return -1; // Out of bounds.
93 }
94
95 return frame_count.x * frame.y + frame.x;
96 }
97
98 void SpriteFramesEditor::_sheet_preview_draw() {
99 const Size2i frame_count = _get_frame_count();
100 const Size2i separation = _get_separation();
101
102 const Size2 draw_offset = Size2(_get_offset()) * sheet_zoom;
103 const Size2 draw_sep = Size2(separation) * sheet_zoom;
104 const Size2 draw_frame_size = Size2(_get_frame_size()) * sheet_zoom;
105 const Size2 draw_size = draw_frame_size * frame_count + draw_sep * (frame_count - Size2i(1, 1));
106
107 const Color line_color = Color(1, 1, 1, 0.3);
108 const Color shadow_color = Color(0, 0, 0, 0.3);
109
110 // Vertical lines.
111 _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
112 for (int i = 0; i < frame_count.x - 1; i++) {
113 const Point2 start = draw_offset + Vector2(i * draw_sep.x + (i + 1) * draw_frame_size.x, 0);
114 if (separation.x == 0) {
115 _draw_shadowed_line(split_sheet_preview, start, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
116 } else {
117 const Size2 size = Size2(draw_sep.x, draw_size.y);
118 split_sheet_preview->draw_rect(Rect2(start, size), line_color);
119 }
120 }
121 _draw_shadowed_line(split_sheet_preview, draw_offset + Vector2(draw_size.x, 0), Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
122
123 // Horizontal lines.
124 _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
125 for (int i = 0; i < frame_count.y - 1; i++) {
126 const Point2 start = draw_offset + Vector2(0, i * draw_sep.y + (i + 1) * draw_frame_size.y);
127 if (separation.y == 0) {
128 _draw_shadowed_line(split_sheet_preview, start, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
129 } else {
130 const Size2 size = Size2(draw_size.x, draw_sep.y);
131 split_sheet_preview->draw_rect(Rect2(start, size), line_color);
132 }
133 }
134 _draw_shadowed_line(split_sheet_preview, draw_offset + Vector2(0, draw_size.y), Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
135
136 if (frames_selected.is_empty()) {
137 split_sheet_dialog->get_ok_button()->set_disabled(true);
138 split_sheet_dialog->set_ok_button_text(TTRC("No Frames Selected"));
139 return;
140 }
141
142 Color accent = get_theme_color("accent_color", EditorStringName(Editor));
143
144 _sheet_sort_frames();
145
146 Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
147 int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
148
149 for (int i = 0; i < frames_ordered.size(); ++i) {
150 const int idx = frames_ordered[i].second;
151
152 const int x = idx % frame_count.x;
153 const int y = idx / frame_count.x;
154 const Point2 pos = draw_offset + Point2(x, y) * (draw_frame_size + draw_sep);
155 split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 0.35), true);
156 split_sheet_preview->draw_rect(Rect2(pos, draw_frame_size), Color(0, 0, 0, 1), false);
157 split_sheet_preview->draw_rect(Rect2(pos + Size2(1, 1), draw_frame_size - Size2(2, 2)), Color(0, 0, 0, 1), false);
158 split_sheet_preview->draw_rect(Rect2(pos + Size2(2, 2), draw_frame_size - Size2(4, 4)), accent, false);
159 split_sheet_preview->draw_rect(Rect2(pos + Size2(3, 3), draw_frame_size - Size2(6, 6)), accent, false);
160 split_sheet_preview->draw_rect(Rect2(pos + Size2(4, 4), draw_frame_size - Size2(8, 8)), Color(0, 0, 0, 1), false);
161 split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 1), false);
162
163 const String text = itos(i);
164 const Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
165
166 // Stop rendering text if too large.
167 if (string_size.x + 6 < draw_frame_size.x && string_size.y / 2 + 10 < draw_frame_size.y) {
168 split_sheet_preview->draw_string_outline(font, pos + Size2(5, 7) + Size2(0, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_LEFT, string_size.x, font_size, 1, Color(0, 0, 0, 1));
169 split_sheet_preview->draw_string(font, pos + Size2(5, 7) + Size2(0, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_LEFT, string_size.x, font_size, Color(1, 1, 1));
170 }
171 }
172
173 split_sheet_dialog->get_ok_button()->set_disabled(false);
174 split_sheet_dialog->set_ok_button_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size()));
175 }
176
177 void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
178 const Ref<InputEventMouseButton> mb = p_event;
179 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
180 const int idx = _sheet_preview_position_to_frame_index(mb->get_position());
181
182 if (idx != -1) {
183 if (mb->is_shift_pressed() && last_frame_selected >= 0) {
184 // Select multiple frames.
185 const int from = last_frame_selected;
186 const int to = idx;
187
188 const int diff = Math::abs(to - from);
189 const int dir = SIGN(to - from);
190
191 for (int i = 0; i <= diff; i++) {
192 const int this_idx = from + i * dir;
193
194 // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
195 frames_toggled_by_mouse_hover.insert(this_idx);
196
197 if (mb->is_command_or_control_pressed()) {
198 frames_selected.erase(this_idx);
199 } else if (!frames_selected.has(this_idx)) {
200 frames_selected.insert(this_idx, selected_count);
201 selected_count++;
202 }
203 }
204 } else {
205 // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
206 frames_toggled_by_mouse_hover.insert(idx);
207
208 if (frames_selected.has(idx)) {
209 frames_selected.erase(idx);
210 } else {
211 frames_selected.insert(idx, selected_count);
212 selected_count++;
213 }
214 }
215 }
216
217 if (last_frame_selected != idx || idx != -1) {
218 last_frame_selected = idx;
219 frames_need_sort = true;
220 split_sheet_preview->queue_redraw();
221 }
222 }
223
224 if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
225 frames_toggled_by_mouse_hover.clear();
226 }
227
228 const Ref<InputEventMouseMotion> mm = p_event;
229 if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
230 // Select by holding down the mouse button on frames.
231 const int idx = _sheet_preview_position_to_frame_index(mm->get_position());
232
233 if (idx != -1 && !frames_toggled_by_mouse_hover.has(idx)) {
234 // Only allow toggling each tile once per mouse hold.
235 // Otherwise, the selection would constantly "flicker" in and out when moving the mouse cursor.
236 // The mouse button must be released before it can be toggled again.
237 frames_toggled_by_mouse_hover.insert(idx);
238
239 if (frames_selected.has(idx)) {
240 frames_selected.erase(idx);
241 } else {
242 frames_selected.insert(idx, selected_count);
243 selected_count++;
244 }
245
246 last_frame_selected = idx;
247 frames_need_sort = true;
248 split_sheet_preview->queue_redraw();
249 }
250 }
251
252 if (frames_selected.is_empty()) {
253 selected_count = 0;
254 }
255 }
256
257 void SpriteFramesEditor::_sheet_scroll_input(const Ref<InputEvent> &p_event) {
258 const Ref<InputEventMouseButton> mb = p_event;
259
260 if (mb.is_valid()) {
261 // Zoom in/out using Ctrl + mouse wheel. This is done on the ScrollContainer
262 // to allow performing this action anywhere, even if the cursor isn't
263 // hovering the texture in the workspace.
264 // keep CTRL and not CMD_OR_CTRL as CTRL is expected even on MacOS.
265 if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
266 _sheet_zoom_on_position(scale_ratio, mb->get_position());
267 // Don't scroll up after zooming in.
268 split_sheet_scroll->accept_event();
269 } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
270 _sheet_zoom_on_position(1 / scale_ratio, mb->get_position());
271 // Don't scroll down after zooming out.
272 split_sheet_scroll->accept_event();
273 }
274 }
275
276 const Ref<InputEventMouseMotion> mm = p_event;
277 if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
278 const Vector2 dragged = Input::get_singleton()->warp_mouse_motion(mm, split_sheet_scroll->get_global_rect());
279 split_sheet_scroll->set_h_scroll(split_sheet_scroll->get_h_scroll() - dragged.x);
280 split_sheet_scroll->set_v_scroll(split_sheet_scroll->get_v_scroll() - dragged.y);
281 }
282 }
283
284 void SpriteFramesEditor::_sheet_add_frames() {
285 const Size2i frame_count = _get_frame_count();
286 const Size2i frame_size = _get_frame_size();
287 const Size2i offset = _get_offset();
288 const Size2i separation = _get_separation();
289
290 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
291 undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
292 int fc = frames->get_frame_count(edited_anim);
293
294 _sheet_sort_frames();
295
296 for (const Pair<int, int> &pair : frames_ordered) {
297 const int idx = pair.second;
298
299 const Point2 frame_coords(idx % frame_count.x, idx / frame_count.x);
300
301 Ref<AtlasTexture> at;
302 at.instantiate();
303 at->set_atlas(split_sheet_preview->get_texture());
304 at->set_region(Rect2(offset + frame_coords * (frame_size + separation), frame_size));
305
306 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, at, 1.0, -1);
307 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, fc);
308 }
309
310 undo_redo->add_do_method(this, "_update_library");
311 undo_redo->add_undo_method(this, "_update_library");
312 undo_redo->commit_action();
313 }
314
315 void SpriteFramesEditor::_sheet_update_zoom_label() {
316 String zoom_text;
317 // The zoom level displayed is relative to the editor scale
318 // (like in most image editors). Its lower bound is clamped to 1 as some people
319 // lower the editor scale to increase the available real estate,
320 // even if their display doesn't have a particularly low DPI.
321 TranslationServer *translation_server = TranslationServer::get_singleton();
322 String locale = translation_server->get_tool_locale();
323 if (sheet_zoom >= 10) {
324 zoom_text = translation_server->format_number(rtos(Math::round((sheet_zoom / MAX(1, EDSCALE)) * 100)), locale);
325 } else {
326 // 2 decimal places if the zoom is below 10%, 1 decimal place if it's below 1000%.
327 zoom_text = translation_server->format_number(rtos(Math::snapped((sheet_zoom / MAX(1, EDSCALE)) * 100, (sheet_zoom >= 0.1) ? 0.1 : 0.01)), locale);
328 }
329 zoom_text += " " + translation_server->get_percent_sign(locale);
330 split_sheet_zoom_reset->set_text(zoom_text);
331 }
332
333 void SpriteFramesEditor::_sheet_zoom_on_position(float p_zoom, const Vector2 &p_position) {
334 const float old_zoom = sheet_zoom;
335 sheet_zoom = CLAMP(sheet_zoom * p_zoom, min_sheet_zoom, max_sheet_zoom);
336
337 const Size2 texture_size = split_sheet_preview->get_texture()->get_size();
338 split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
339
340 Vector2 offset = Vector2(split_sheet_scroll->get_h_scroll(), split_sheet_scroll->get_v_scroll());
341 offset = (offset + p_position) / old_zoom * sheet_zoom - p_position;
342 split_sheet_scroll->set_h_scroll(offset.x);
343 split_sheet_scroll->set_v_scroll(offset.y);
344
345 _sheet_update_zoom_label();
346 }
347
348 void SpriteFramesEditor::_sheet_zoom_in() {
349 _sheet_zoom_on_position(scale_ratio, Vector2());
350 }
351
352 void SpriteFramesEditor::_sheet_zoom_out() {
353 _sheet_zoom_on_position(1 / scale_ratio, Vector2());
354 }
355
356 void SpriteFramesEditor::_sheet_zoom_reset() {
357 // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
358 sheet_zoom = MAX(1.0f, EDSCALE);
359 Size2 texture_size = split_sheet_preview->get_texture()->get_size();
360 split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
361
362 _sheet_update_zoom_label();
363 }
364
365 void SpriteFramesEditor::_sheet_zoom_fit() {
366 const float margin_percentage = 0.1f;
367 const float max_margin = 64.0f;
368 const Size2 margin = (margin_percentage * split_sheet_scroll->get_size()).minf(max_margin);
369 const Size2 display_area_size = split_sheet_scroll->get_size() - margin;
370 const Size2 texture_size = split_sheet_preview->get_texture()->get_size();
371 const Vector2 texture_ratio = display_area_size / texture_size;
372 float texture_fit_zoom = MIN(texture_ratio.x, texture_ratio.y);
373
374 // Quantize the zoom level to avoid subpixel rendering
375 if (texture_fit_zoom > 1.0) {
376 texture_fit_zoom = Math::floor(texture_fit_zoom);
377 } else if (!Math::is_zero_approx(texture_fit_zoom)) {
378 texture_fit_zoom = 1.0f / Math::ceil(1.0f / texture_fit_zoom);
379 }
380
381 sheet_zoom = CLAMP(texture_fit_zoom, min_sheet_zoom, max_sheet_zoom);
382 split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
383
384 _sheet_update_zoom_label();
385 }
386
387 void SpriteFramesEditor::_sheet_order_selected(int p_option) {
388 frames_need_sort = true;
389 split_sheet_preview->queue_redraw();
390 }
391
392 void SpriteFramesEditor::_sheet_select_all_frames() {
393 for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
394 if (!frames_selected.has(i)) {
395 frames_selected.insert(i, selected_count);
396 selected_count++;
397 frames_need_sort = true;
398 }
399 }
400
401 split_sheet_preview->queue_redraw();
402 }
403
404 void SpriteFramesEditor::_sheet_clear_all_frames() {
405 frames_selected.clear();
406 selected_count = 0;
407
408 split_sheet_preview->queue_redraw();
409 }
410
411 void SpriteFramesEditor::_sheet_sort_frames() {
412 if (!frames_need_sort) {
413 return;
414 }
415 frames_need_sort = false;
416 frames_ordered.resize(frames_selected.size());
417 if (frames_selected.is_empty()) {
418 return;
419 }
420
421 const Size2i frame_count = _get_frame_count();
422 const int frame_order = split_sheet_order->get_selected_id();
423 int index = 0;
424
425 // Fill based on order.
426 for (const KeyValue<int, int> &from_pair : frames_selected) {
427 const int idx = from_pair.key;
428
429 const int selection_order = from_pair.value;
430
431 // Default to using selection order.
432 int order_by = selection_order;
433
434 // Extract coordinates for sorting.
435 const int pos_frame_x = idx % frame_count.x;
436 const int pos_frame_y = idx / frame_count.x;
437
438 const int neg_frame_x = frame_count.x - (pos_frame_x + 1);
439 const int neg_frame_y = frame_count.y - (pos_frame_y + 1);
440
441 switch (frame_order) {
442 case FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM: {
443 order_by = frame_count.x * pos_frame_y + pos_frame_x;
444 } break;
445
446 case FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP: {
447 order_by = frame_count.x * neg_frame_y + pos_frame_x;
448 } break;
449
450 case FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM: {
451 order_by = frame_count.x * pos_frame_y + neg_frame_x;
452 } break;
453
454 case FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP: {
455 order_by = frame_count.x * neg_frame_y + neg_frame_x;
456 } break;
457
458 case FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT: {
459 order_by = pos_frame_y + frame_count.y * pos_frame_x;
460 } break;
461
462 case FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT: {
463 order_by = pos_frame_y + frame_count.y * neg_frame_x;
464 } break;
465
466 case FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT: {
467 order_by = neg_frame_y + frame_count.y * pos_frame_x;
468 } break;
469
470 case FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT: {
471 order_by = neg_frame_y + frame_count.y * neg_frame_x;
472 } break;
473 }
474
475 // Assign in vector.
476 frames_ordered.set(index, Pair<int, int>(order_by, idx));
477 index++;
478 }
479
480 // Sort frames.
481 frames_ordered.sort_custom<PairSort<int, int>>();
482 }
483
484 void SpriteFramesEditor::_sheet_spin_changed(double p_value, int p_dominant_param) {
485 if (updating_split_settings) {
486 return;
487 }
488 updating_split_settings = true;
489
490 if (p_dominant_param != PARAM_USE_CURRENT) {
491 dominant_param = p_dominant_param;
492 }
493
494 const Size2i texture_size = split_sheet_preview->get_texture()->get_size();
495 const Size2i size = texture_size - _get_offset();
496
497 switch (dominant_param) {
498 case PARAM_SIZE: {
499 const Size2i frame_size = _get_frame_size();
500
501 const Size2i offset_max = texture_size - frame_size;
502 split_sheet_offset_x->set_max(offset_max.x);
503 split_sheet_offset_y->set_max(offset_max.y);
504
505 const Size2i sep_max = size - frame_size * 2;
506 split_sheet_sep_x->set_max(sep_max.x);
507 split_sheet_sep_y->set_max(sep_max.y);
508
509 const Size2i separation = _get_separation();
510 const Size2i count = (size + separation) / (frame_size + separation);
511 split_sheet_h->set_value(count.x);
512 split_sheet_v->set_value(count.y);
513 } break;
514
515 case PARAM_FRAME_COUNT: {
516 const Size2i count = _get_frame_count();
517
518 const Size2i offset_max = texture_size - count;
519 split_sheet_offset_x->set_max(offset_max.x);
520 split_sheet_offset_y->set_max(offset_max.y);
521
522 const Size2i gap_count = count - Size2i(1, 1);
523 split_sheet_sep_x->set_max(gap_count.x == 0 ? size.x : (size.x - count.x) / gap_count.x);
524 split_sheet_sep_y->set_max(gap_count.y == 0 ? size.y : (size.y - count.y) / gap_count.y);
525
526 const Size2i separation = _get_separation();
527 const Size2i frame_size = (size - separation * gap_count) / count;
528 split_sheet_size_x->set_value(frame_size.x);
529 split_sheet_size_y->set_value(frame_size.y);
530 } break;
531 }
532
533 updating_split_settings = false;
534
535 frames_selected.clear();
536 selected_count = 0;
537 last_frame_selected = -1;
538 split_sheet_preview->queue_redraw();
539 }
540
541 void SpriteFramesEditor::_toggle_show_settings() {
542 split_sheet_settings_vb->set_visible(!split_sheet_settings_vb->is_visible());
543
544 _update_show_settings();
545 }
546
547 void SpriteFramesEditor::_update_show_settings() {
548 if (is_layout_rtl()) {
549 toggle_settings_button->set_button_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Back") : SNAME("Forward")));
550 } else {
551 toggle_settings_button->set_button_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Forward") : SNAME("Back")));
552 }
553 }
554
555 void SpriteFramesEditor::_auto_slice_sprite_sheet() {
556 if (updating_split_settings) {
557 return;
558 }
559 updating_split_settings = true;
560
561 const Size2i size = split_sheet_preview->get_texture()->get_size();
562
563 const Size2i split_sheet = _estimate_sprite_sheet_size(split_sheet_preview->get_texture());
564 split_sheet_h->set_value(split_sheet.x);
565 split_sheet_v->set_value(split_sheet.y);
566 split_sheet_size_x->set_value(size.x / split_sheet.x);
567 split_sheet_size_y->set_value(size.y / split_sheet.y);
568 split_sheet_sep_x->set_value(0);
569 split_sheet_sep_y->set_value(0);
570 split_sheet_offset_x->set_value(0);
571 split_sheet_offset_y->set_value(0);
572
573 updating_split_settings = false;
574
575 frames_selected.clear();
576 selected_count = 0;
577 last_frame_selected = -1;
578 split_sheet_preview->queue_redraw();
579 }
580
581 bool SpriteFramesEditor::_matches_background_color(const Color &p_background_color, const Color &p_pixel_color) {
582 if ((p_background_color.a == 0 && p_pixel_color.a == 0) || p_background_color.is_equal_approx(p_pixel_color)) {
583 return true;
584 }
585
586 Color d = p_background_color - p_pixel_color;
587 // 0.04f is the threshold for how much a colour can deviate from background colour and still be considered a match. Arrived at through experimentation, can be tweaked.
588 return (d.r * d.r) + (d.g * d.g) + (d.b * d.b) + (d.a * d.a) < 0.04f;
589 }
590
591 Size2i SpriteFramesEditor::_estimate_sprite_sheet_size(const Ref<Texture2D> p_texture) {
592 Ref<Image> image = p_texture->get_image();
593 if (image->is_compressed()) {
594 image = image->duplicate();
595 ERR_FAIL_COND_V(image->decompress() != OK, p_texture->get_size());
596 }
597 Size2i size = image->get_size();
598
599 Color assumed_background_color = image->get_pixel(0, 0);
600 Size2i sheet_size;
601
602 bool previous_line_background = true;
603 for (int x = 0; x < size.x; x++) {
604 int y = 0;
605 while (y < size.y && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) {
606 y++;
607 }
608 bool current_line_background = (y == size.y);
609 if (previous_line_background && !current_line_background) {
610 sheet_size.x++;
611 }
612 previous_line_background = current_line_background;
613 }
614
615 previous_line_background = true;
616 for (int y = 0; y < size.y; y++) {
617 int x = 0;
618 while (x < size.x && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) {
619 x++;
620 }
621 bool current_line_background = (x == size.x);
622 if (previous_line_background && !current_line_background) {
623 sheet_size.y++;
624 }
625 previous_line_background = current_line_background;
626 }
627
628 if (sheet_size == Size2i(0, 0) || sheet_size == Size2i(1, 1)) {
629 sheet_size = Size2i(4, 4);
630 }
631
632 return sheet_size;
633 }
634
635 void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
636 Ref<Texture2D> texture = ResourceLoader::load(p_file);
637 if (texture.is_null()) {
638 EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
639 ERR_FAIL_COND(texture.is_null());
640 }
641 frames_selected.clear();
642 selected_count = 0;
643 last_frame_selected = -1;
644
645 bool first_open = split_sheet_preview->get_texture().is_null();
646 bool new_texture = texture != split_sheet_preview->get_texture();
647 split_sheet_preview->set_texture(texture);
648 if (new_texture) {
649 // Reset spin max.
650 const Size2i size = texture->get_size();
651 split_sheet_size_x->set_max(size.x);
652 split_sheet_size_y->set_max(size.y);
653 split_sheet_sep_x->set_max(size.x);
654 split_sheet_sep_y->set_max(size.y);
655 split_sheet_offset_x->set_max(size.x);
656 split_sheet_offset_y->set_max(size.y);
657
658 if (size != previous_texture_size) {
659 // Different texture, reset to 4x4.
660 dominant_param = PARAM_FRAME_COUNT;
661 updating_split_settings = true;
662 const Size2i split_sheet = Size2i(4, 4);
663 split_sheet_h->set_value(split_sheet.x);
664 split_sheet_v->set_value(split_sheet.y);
665 split_sheet_size_x->set_value(size.x / split_sheet.x);
666 split_sheet_size_y->set_value(size.y / split_sheet.y);
667 split_sheet_sep_x->set_value(0);
668 split_sheet_sep_y->set_value(0);
669 split_sheet_offset_x->set_value(0);
670 split_sheet_offset_y->set_value(0);
671 updating_split_settings = false;
672 }
673 previous_texture_size = size;
674
675 // Reset zoom.
676 if (first_open) {
677 _sheet_zoom_reset();
678 split_sheet_dialog->connect(SceneStringName(focus_entered), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_fit), CONNECT_ONE_SHOT);
679 } else {
680 _sheet_zoom_fit();
681 }
682 }
683
684 split_sheet_dialog->popup_centered_ratio(0.65);
685 }
686
687 void SpriteFramesEditor::_notification(int p_what) {
688 switch (p_what) {
689 case NOTIFICATION_ENTER_TREE: {
690 get_tree()->connect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
691
692 [[fallthrough]];
693 }
694 case NOTIFICATION_THEME_CHANGED: {
695 autoplay_icon = get_editor_theme_icon(SNAME("AutoPlay"));
696 stop_icon = get_editor_theme_icon(SNAME("Stop"));
697 pause_icon = get_editor_theme_icon(SNAME("Pause"));
698 _update_stop_icon();
699
700 autoplay->set_button_icon(get_editor_theme_icon(SNAME("AutoPlay")));
701 anim_loop->set_button_icon(get_editor_theme_icon(SNAME("Loop")));
702 play->set_button_icon(get_editor_theme_icon(SNAME("PlayStart")));
703 play_from->set_button_icon(get_editor_theme_icon(SNAME("Play")));
704 play_bw->set_button_icon(get_editor_theme_icon(SNAME("PlayStartBackwards")));
705 play_bw_from->set_button_icon(get_editor_theme_icon(SNAME("PlayBackwards")));
706
707 load->set_button_icon(get_editor_theme_icon(SNAME("Load")));
708 load_sheet->set_button_icon(get_editor_theme_icon(SNAME("SpriteSheet")));
709 copy->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
710 paste->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
711 empty_before->set_button_icon(get_editor_theme_icon(SNAME("InsertBefore")));
712 empty_after->set_button_icon(get_editor_theme_icon(SNAME("InsertAfter")));
713 move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveLeft")));
714 move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveRight")));
715 delete_frame->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
716 zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
717 zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
718 zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
719 add_anim->set_button_icon(get_editor_theme_icon(SNAME("New")));
720 duplicate_anim->set_button_icon(get_editor_theme_icon(SNAME("Duplicate")));
721 cut_anim->set_button_icon(get_editor_theme_icon(SNAME("ActionCut")));
722 copy_anim->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
723 paste_anim->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
724 delete_anim->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
725 anim_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
726 split_sheet_zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
727 split_sheet_zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
728 split_sheet_zoom_fit->set_button_icon(get_editor_theme_icon(SNAME("DistractionFree")));
729 split_sheet_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
730
731 _update_show_settings();
732 } break;
733
734 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
735 _update_show_settings();
736 } break;
737
738 case NOTIFICATION_TRANSLATION_CHANGED: {
739 _update_show_settings();
740 anim_speed->set_suffix(TTR("FPS"));
741
742 // Similar to `_update_library_impl()`, but only updates text for "empty" items.
743 if (frames.is_valid()) {
744 for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
745 Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
746 if (texture.is_null()) {
747 String name = itos(i);
748 float duration = frames->get_frame_duration(edited_anim, i);
749 texture = empty_icon;
750 name += ": " + TTR("(empty)");
751 if (duration != 1.0f) {
752 name += String::utf8(" [× ") + String::num(duration, 2) + "]";
753 }
754 frame_list->set_item_text(i, name);
755 }
756 }
757 }
758 } break;
759
760 case NOTIFICATION_READY: {
761 add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up.
762 } break;
763
764 case NOTIFICATION_EXIT_TREE: {
765 get_tree()->disconnect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
766 } break;
767 }
768 }
769
770 void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_at_pos) {
771 ERR_FAIL_COND(!frames->has_animation(edited_anim));
772
773 List<Ref<Texture2D>> resources;
774
775 for (int i = 0; i < p_path.size(); i++) {
776 Ref<Texture2D> resource;
777 resource = ResourceLoader::load(p_path[i]);
778
779 if (resource.is_null()) {
780 dialog->set_text(TTRC("ERROR: Couldn't load frame resource!"));
781 dialog->set_title(TTRC("Error!"));
782
783 //dialog->get_cancel()->set_text("Close");
784 dialog->set_ok_button_text(TTRC("Close"));
785 dialog->popup_centered();
786 return; ///beh should show an error i guess
787 }
788
789 resources.push_back(resource);
790 }
791
792 if (resources.is_empty()) {
793 return;
794 }
795
796 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
797 undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
798 int fc = frames->get_frame_count(edited_anim);
799
800 int count = 0;
801
802 for (const Ref<Texture2D> &E : resources) {
803 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, E, 1.0, p_at_pos == -1 ? -1 : p_at_pos + count);
804 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
805 count++;
806 }
807 undo_redo->add_do_method(this, "_update_library");
808 undo_redo->add_undo_method(this, "_update_library");
809
810 undo_redo->commit_action();
811 }
812
813 Size2i SpriteFramesEditor::_get_frame_count() const {
814 return Size2i(split_sheet_h->get_value(), split_sheet_v->get_value());
815 }
816
817 Size2i SpriteFramesEditor::_get_frame_size() const {
818 return Size2i(split_sheet_size_x->get_value(), split_sheet_size_y->get_value());
819 }
820
821 Size2i SpriteFramesEditor::_get_offset() const {
822 return Size2i(split_sheet_offset_x->get_value(), split_sheet_offset_y->get_value());
823 }
824
825 Size2i SpriteFramesEditor::_get_separation() const {
826 return Size2i(split_sheet_sep_x->get_value(), split_sheet_sep_y->get_value());
827 }
828
829 void SpriteFramesEditor::_load_pressed() {
830 ERR_FAIL_COND(!frames->has_animation(edited_anim));
831 loading_scene = false;
832
833 file->clear_filters();
834 List<String> extensions;
835 ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
836 for (const String &extension : extensions) {
837 file->add_filter("*." + extension);
838 }
839
840 file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
841 file->popup_file_dialog();
842 }
843
844 void SpriteFramesEditor::_paste_pressed() {
845 ERR_FAIL_COND(!frames->has_animation(edited_anim));
846
847 Ref<ClipboardSpriteFrames> clipboard_frames = EditorSettings::get_singleton()->get_resource_clipboard();
848 if (clipboard_frames.is_valid()) {
849 _paste_frame_array(clipboard_frames);
850 return;
851 }
852
853 Ref<Texture2D> texture = EditorSettings::get_singleton()->get_resource_clipboard();
854 if (texture.is_valid()) {
855 _paste_texture(texture);
856 return;
857 }
858 }
859
860 void SpriteFramesEditor::_paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames) {
861 if (p_clipboard_frames->frames.is_empty()) {
862 return;
863 }
864
865 Ref<Texture2D> texture;
866 float duration = 1.0;
867
868 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
869 undo_redo->create_action(TTR("Paste Frame(s)"), UndoRedo::MERGE_DISABLE, frames.ptr());
870
871 int undo_index = frames->get_frame_count(edited_anim);
872
873 for (int index = 0; index < p_clipboard_frames->frames.size(); index++) {
874 const ClipboardSpriteFrames::Frame &frame = p_clipboard_frames->frames[index];
875 texture = frame.texture;
876 duration = frame.duration;
877
878 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
879 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
880 }
881
882 undo_redo->add_do_method(this, "_update_library");
883 undo_redo->add_undo_method(this, "_update_library");
884 undo_redo->commit_action();
885 }
886
887 void SpriteFramesEditor::_paste_texture(const Ref<Texture2D> &p_texture) {
888 float duration = 1.0;
889
890 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
891 undo_redo->create_action(TTR("Paste Texture"), UndoRedo::MERGE_DISABLE, frames.ptr());
892
893 int undo_index = frames->get_frame_count(edited_anim);
894
895 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, p_texture, duration);
896 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
897
898 undo_redo->add_do_method(this, "_update_library");
899 undo_redo->add_undo_method(this, "_update_library");
900 undo_redo->commit_action();
901 }
902
903 void SpriteFramesEditor::_copy_pressed() {
904 ERR_FAIL_COND(!frames->has_animation(edited_anim));
905
906 Vector<int> selected_items = frame_list->get_selected_items();
907
908 if (selected_items.is_empty()) {
909 return;
910 }
911
912 Ref<ClipboardSpriteFrames> clipboard_frames = memnew(ClipboardSpriteFrames);
913
914 for (const int &frame_index : selected_items) {
915 Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, frame_index);
916 if (texture.is_null()) {
917 continue;
918 }
919
920 ClipboardSpriteFrames::Frame frame;
921 frame.texture = texture;
922 frame.duration = frames->get_frame_duration(edited_anim, frame_index);
923
924 clipboard_frames->frames.push_back(frame);
925 }
926 EditorSettings::get_singleton()->set_resource_clipboard(clipboard_frames);
927 }
928
929 void SpriteFramesEditor::_empty_pressed() {
930 ERR_FAIL_COND(!frames->has_animation(edited_anim));
931
932 int from = -1;
933 Vector<int> selected_items = frame_list->get_selected_items();
934
935 if (!selected_items.is_empty()) {
936 from = selected_items[0];
937 selection.clear();
938 selection.push_back(from + 1);
939 } else {
940 from = frames->get_frame_count(edited_anim);
941 }
942
943 Ref<Texture2D> texture;
944
945 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
946 undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
947 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from);
948 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from);
949 undo_redo->add_do_method(this, "_update_library");
950 undo_redo->add_undo_method(this, "_update_library");
951 undo_redo->commit_action();
952 }
953
954 void SpriteFramesEditor::_empty2_pressed() {
955 ERR_FAIL_COND(!frames->has_animation(edited_anim));
956
957 int from = -1;
958 Vector<int> selected_items = frame_list->get_selected_items();
959
960 if (!selected_items.is_empty()) {
961 from = selected_items[selected_items.size() - 1];
962 selection.clear();
963 selection.push_back(from);
964 } else {
965 from = frames->get_frame_count(edited_anim);
966 }
967
968 Ref<Texture2D> texture;
969
970 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
971 undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
972 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from + 1);
973 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from + 1);
974 undo_redo->add_do_method(this, "_update_library");
975 undo_redo->add_undo_method(this, "_update_library");
976 undo_redo->commit_action();
977 }
978
979 void SpriteFramesEditor::_up_pressed() {
980 ERR_FAIL_COND(!frames->has_animation(edited_anim));
981
982 Vector<int> selected_items = frame_list->get_selected_items();
983
984 int nb_selected_items = selected_items.size();
985 if (nb_selected_items <= 0) {
986 return;
987 }
988
989 int first_selected_frame_index = selected_items[0];
990 if (first_selected_frame_index < 1) {
991 return;
992 }
993
994 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
995 undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
996
997 int last_overwritten_frame = -1;
998
999 for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
1000 int to_move = selected_items[selected_index];
1001 int new_index = to_move - 1;
1002 selected_items.set(selected_index, new_index);
1003
1004 undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
1005 undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
1006
1007 bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == to_move + 1;
1008 if (last_overwritten_frame == -1) {
1009 last_overwritten_frame = new_index;
1010 }
1011
1012 if (!is_next_item_in_selection) {
1013 undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, to_move, frames->get_frame_texture(edited_anim, last_overwritten_frame), frames->get_frame_duration(edited_anim, last_overwritten_frame));
1014 undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, to_move, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
1015 last_overwritten_frame = -1;
1016 }
1017 }
1018 selection = selected_items;
1019
1020 undo_redo->add_do_method(this, "_update_library");
1021 undo_redo->add_undo_method(this, "_update_library");
1022 undo_redo->commit_action();
1023 }
1024
1025 void SpriteFramesEditor::_down_pressed() {
1026 ERR_FAIL_COND(!frames->has_animation(edited_anim));
1027
1028 Vector<int> selected_items = frame_list->get_selected_items();
1029
1030 int nb_selected_items = selected_items.size();
1031 if (nb_selected_items <= 0) {
1032 return;
1033 }
1034
1035 int last_selected_frame_index = selected_items[nb_selected_items - 1];
1036 if (last_selected_frame_index >= frames->get_frame_count(edited_anim) - 1) {
1037 return;
1038 }
1039
1040 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1041 undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
1042
1043 int first_moved_frame = -1;
1044
1045 for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
1046 int to_move = selected_items[selected_index];
1047 int new_index = to_move + 1;
1048 selected_items.set(selected_index, new_index);
1049
1050 undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
1051 undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
1052
1053 bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == new_index;
1054 if (first_moved_frame == -1) {
1055 first_moved_frame = to_move;
1056 }
1057
1058 if (!is_next_item_in_selection) {
1059 undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, first_moved_frame, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
1060 undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, first_moved_frame, frames->get_frame_texture(edited_anim, first_moved_frame), frames->get_frame_duration(edited_anim, first_moved_frame));
1061 first_moved_frame = -1;
1062 }
1063 }
1064 selection = selected_items;
1065
1066 undo_redo->add_do_method(this, "_update_library");
1067 undo_redo->add_undo_method(this, "_update_library");
1068 undo_redo->commit_action();
1069 }
1070
1071 void SpriteFramesEditor::_delete_pressed() {
1072 ERR_FAIL_COND(!frames->has_animation(edited_anim));
1073
1074 Vector<int> selected_items = frame_list->get_selected_items();
1075
1076 int nb_selected_items = selected_items.size();
1077 if (nb_selected_items <= 0) {
1078 return;
1079 }
1080
1081 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1082 undo_redo->create_action(TTR("Delete Resource"), UndoRedo::MERGE_DISABLE, frames.ptr());
1083 for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
1084 int to_delete = selected_items[selected_index];
1085 undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, to_delete - selected_index);
1086 undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, frames->get_frame_texture(edited_anim, to_delete), frames->get_frame_duration(edited_anim, to_delete), to_delete);
1087 }
1088
1089 undo_redo->add_do_method(this, "_update_library");
1090 undo_redo->add_undo_method(this, "_update_library");
1091 undo_redo->commit_action();
1092 }
1093
1094 void SpriteFramesEditor::_animation_selected() {
1095 if (updating) {
1096 return;
1097 }
1098
1099 TreeItem *selected = animations->get_selected();
1100 ERR_FAIL_NULL(selected);
1101 edited_anim = selected->get_text(0);
1102
1103 if (animated_sprite) {
1104 sprite_node_updating = true;
1105 animated_sprite->call("set_animation", edited_anim);
1106 sprite_node_updating = false;
1107 }
1108
1109 _update_library(true);
1110 }
1111
1112 void SpriteFramesEditor::_sync_animation() {
1113 if (!animated_sprite || sprite_node_updating) {
1114 return;
1115 }
1116 _select_animation(animated_sprite->call("get_animation"), false);
1117 _update_stop_icon();
1118 }
1119
1120 void SpriteFramesEditor::_select_animation(const String &p_name, bool p_update_node) {
1121 if (frames.is_null() || !frames->has_animation(p_name)) {
1122 return;
1123 }
1124 edited_anim = p_name;
1125
1126 if (animated_sprite) {
1127 if (p_update_node) {
1128 animated_sprite->call("set_animation", edited_anim);
1129 }
1130 }
1131
1132 _update_library();
1133 }
1134
1135 static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
1136 Node *edited = EditorNode::get_singleton()->get_edited_scene();
1137 if (!edited) {
1138 return;
1139 }
1140 if (p_node != edited && p_node->get_owner() != edited) {
1141 return;
1142 }
1143
1144 {
1145 AnimatedSprite2D *as = Object::cast_to<AnimatedSprite2D>(p_node);
1146 if (as && as->get_sprite_frames() == p_sfames) {
1147 r_nodes->push_back(p_node);
1148 }
1149 }
1150
1151 {
1152 AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
1153 if (as && as->get_sprite_frames() == p_sfames) {
1154 r_nodes->push_back(p_node);
1155 }
1156 }
1157
1158 for (int i = 0; i < p_node->get_child_count(); i++) {
1159 _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
1160 }
1161 }
1162
1163 void SpriteFramesEditor::_animation_name_edited() {
1164 if (updating) {
1165 return;
1166 }
1167
1168 if (!frames->has_animation(edited_anim)) {
1169 return;
1170 }
1171
1172 TreeItem *edited = animations->get_edited();
1173 if (!edited) {
1174 return;
1175 }
1176
1177 String new_name = edited->get_text(0);
1178
1179 if (new_name == String(edited_anim)) {
1180 return;
1181 }
1182
1183 if (new_name.is_empty()) {
1184 new_name = "new_animation";
1185 }
1186
1187 new_name = new_name.replace_char('/', '_').replace_char(',', ' ');
1188
1189 String name = new_name;
1190 int counter = 0;
1191 while (frames->has_animation(name)) {
1192 if (name == String(edited_anim)) {
1193 edited->set_text(0, name); // The name didn't change, just updated the column text to name.
1194 return;
1195 }
1196 counter++;
1197 name = new_name + "_" + itos(counter);
1198 }
1199 edited->set_text(0, name);
1200
1201 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1202 undo_redo->create_action(TTR("Rename Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
1203 undo_redo->add_do_method(frames.ptr(), "rename_animation", edited_anim, name);
1204 undo_redo->add_undo_method(frames.ptr(), "rename_animation", name, edited_anim);
1205 _rename_node_animation(undo_redo, false, edited_anim, name, name);
1206 _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
1207 undo_redo->add_do_method(this, "_select_animation", name);
1208 undo_redo->add_undo_method(this, "_select_animation", edited_anim);
1209 undo_redo->add_do_method(this, "_update_library");
1210 undo_redo->add_undo_method(this, "_update_library");
1211 undo_redo->commit_action();
1212
1213 animations->grab_focus();
1214 }
1215
1216 void SpriteFramesEditor::_rename_node_animation(EditorUndoRedoManager *undo_redo, bool is_undo, const String &p_filter, const String &p_new_animation, const String &p_new_autoplay) {
1217 List<Node *> nodes;
1218 _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
1219
1220 if (is_undo) {
1221 for (Node *E : nodes) {
1222 String current_name = E->call("get_animation");
1223 if (current_name == p_filter) {
1224 undo_redo->force_fixed_history(); // Fixes corner-case when editing SpriteFrames stored as separate file.
1225 undo_redo->add_undo_method(E, "set_animation", p_new_animation);
1226 }
1227 String autoplay_name = E->call("get_autoplay");
1228 if (autoplay_name == p_filter) {
1229 undo_redo->force_fixed_history();
1230 undo_redo->add_undo_method(E, "set_autoplay", p_new_autoplay);
1231 }
1232 }
1233 } else {
1234 for (Node *E : nodes) {
1235 String current_name = E->call("get_animation");
1236 if (current_name == p_filter) {
1237 undo_redo->force_fixed_history();
1238 undo_redo->add_do_method(E, "set_animation", p_new_animation);
1239 }
1240 String autoplay_name = E->call("get_autoplay");
1241 if (autoplay_name == p_filter) {
1242 undo_redo->force_fixed_history();
1243 undo_redo->add_do_method(E, "set_autoplay", p_new_autoplay);
1244 }
1245 }
1246 }
1247 }
1248
1249 void SpriteFramesEditor::_animation_add() {
1250 String name = "new_animation";
1251 int counter = 0;
1252 while (frames->has_animation(name)) {
1253 counter++;
1254 name = vformat("new_animation_%d", counter);
1255 }
1256
1257 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1258 undo_redo->create_action(TTR("Add Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
1259 undo_redo->add_do_method(frames.ptr(), "add_animation", name);
1260 undo_redo->add_undo_method(frames.ptr(), "remove_animation", name);
1261 undo_redo->add_do_method(this, "_select_animation", name);
1262 undo_redo->add_undo_method(this, "_select_animation", edited_anim);
1263 undo_redo->add_do_method(this, "_update_library");
1264 undo_redo->add_undo_method(this, "_update_library");
1265 undo_redo->commit_action();
1266
1267 animations->grab_focus();
1268 }
1269
1270 void SpriteFramesEditor::_animation_duplicate() {
1271 if (updating) {
1272 return;
1273 }
1274
1275 if (!frames->has_animation(edited_anim)) {
1276 return;
1277 }
1278
1279 String new_name = _generate_unique_animation_name(edited_anim);
1280 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1281 undo_redo->create_action(TTR("Duplicate Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
1282 undo_redo->add_do_method(frames.ptr(), "duplicate_animation", edited_anim, new_name);
1283 undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
1284 undo_redo->add_do_method(this, "_select_animation", new_name);
1285 undo_redo->add_undo_method(this, "_select_animation", edited_anim);
1286 undo_redo->add_do_method(this, "_update_library");
1287 undo_redo->add_undo_method(this, "_update_library");
1288 undo_redo->commit_action();
1289
1290 animations->grab_focus();
1291 }
1292
1293 void SpriteFramesEditor::_animation_cut() {
1294 if (!frames->has_animation(edited_anim)) {
1295 return;
1296 }
1297
1298 // Copy animation to clipboard.
1299 Ref<ClipboardAnimation> clipboard_anim = ClipboardAnimation::from_sprite_frames(frames, edited_anim);
1300 EditorSettings::get_singleton()->set_resource_clipboard(clipboard_anim);
1301
1302 // Remove animation with undo/redo (no confirmation dialog).
1303 _animation_remove_undo_redo(TTR("Cut Animation"), &clipboard_anim->frames);
1304 }
1305
1306 void SpriteFramesEditor::_animation_copy() {
1307 if (!frames->has_animation(edited_anim)) {
1308 return;
1309 }
1310
1311 Ref<ClipboardAnimation> clipboard_anim = ClipboardAnimation::from_sprite_frames(frames, edited_anim);
1312 EditorSettings::get_singleton()->set_resource_clipboard(clipboard_anim);
1313 }
1314
1315 void SpriteFramesEditor::_animation_paste() {
1316 if (updating) {
1317 return;
1318 }
1319
1320 Ref<ClipboardAnimation> clipboard_anim = EditorSettings::get_singleton()->get_resource_clipboard();
1321 if (clipboard_anim.is_null()) {
1322 return;
1323 }
1324
1325 String new_name = _generate_unique_animation_name(clipboard_anim->name);
1326 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1327 undo_redo->create_action(TTR("Paste Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
1328 undo_redo->add_do_method(frames.ptr(), "add_animation", new_name);
1329 undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
1330 undo_redo->add_do_method(frames.ptr(), "set_animation_speed", new_name, clipboard_anim->speed);
1331 undo_redo->add_do_method(frames.ptr(), "set_animation_loop", new_name, clipboard_anim->loop);
1332
1333 for (ClipboardSpriteFrames::Frame &frame : clipboard_anim->frames) {
1334 undo_redo->add_do_method(frames.ptr(), "add_frame", new_name, frame.texture, frame.duration);
1335 }
1336
1337 undo_redo->add_do_method(this, "_select_animation", new_name);
1338 undo_redo->add_undo_method(this, "_select_animation", edited_anim);
1339 undo_redo->add_do_method(this, "_update_library");
1340 undo_redo->add_undo_method(this, "_update_library");
1341 undo_redo->commit_action();
1342 }
1343
1344 void SpriteFramesEditor::_animation_remove() {
1345 if (updating) {
1346 return;
1347 }
1348
1349 if (!frames->has_animation(edited_anim)) {
1350 return;
1351 }
1352
1353 delete_dialog->set_text(TTRC("Delete Animation?"));
1354 delete_dialog->popup_centered();
1355 }
1356
1357 void SpriteFramesEditor::_animation_remove_confirmed() {
1358 _animation_remove_undo_redo(TTR("Remove Animation"), nullptr);
1359 }
1360
1361 void SpriteFramesEditor::_animation_search_text_changed(const String &p_text) {
1362 _update_library();
1363 }
1364
1365 void SpriteFramesEditor::_animation_loop_changed() {
1366 if (updating) {
1367 return;
1368 }
1369
1370 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1371 undo_redo->create_action(TTR("Change Animation Loop"), UndoRedo::MERGE_DISABLE, frames.ptr());
1372 undo_redo->add_do_method(frames.ptr(), "set_animation_loop", edited_anim, anim_loop->is_pressed());
1373 undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
1374 undo_redo->add_do_method(this, "_update_library", true);
1375 undo_redo->add_undo_method(this, "_update_library", true);
1376 undo_redo->commit_action();
1377 }
1378
1379 void SpriteFramesEditor::_animation_speed_resized() {
1380 anim_speed->update_minimum_size();
1381 }
1382
1383 void SpriteFramesEditor::_animation_speed_changed(double p_value) {
1384 if (frames.is_null()) {
1385 return;
1386 }
1387
1388 if (updating) {
1389 return;
1390 }
1391
1392 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1393 undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS, frames.ptr());
1394 undo_redo->add_do_method(frames.ptr(), "set_animation_speed", edited_anim, p_value);
1395 undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
1396 undo_redo->add_do_method(this, "_update_library", true);
1397 undo_redo->add_undo_method(this, "_update_library", true);
1398 undo_redo->commit_action();
1399 }
1400
1401 void SpriteFramesEditor::_animation_remove_undo_redo(const StringName &p_action_name, const Vector<ClipboardSpriteFrames::Frame> *p_frames) {
1402 StringName new_edited = _find_next_animation();
1403 int frame_count = frames->get_frame_count(edited_anim);
1404 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1405 undo_redo->create_action(p_action_name, UndoRedo::MERGE_DISABLE, frames.ptr());
1406 _rename_node_animation(undo_redo, false, edited_anim, new_edited, "");
1407 undo_redo->add_do_method(frames.ptr(), "remove_animation", edited_anim);
1408 undo_redo->add_undo_method(frames.ptr(), "add_animation", edited_anim);
1409 _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
1410 undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
1411 undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
1412 for (int i = 0; i < frame_count; i++) {
1413 Ref<Texture2D> texture;
1414 float duration;
1415 if (p_frames) {
1416 texture = (*p_frames)[i].texture;
1417 duration = (*p_frames)[i].duration;
1418 } else {
1419 texture = frames->get_frame_texture(edited_anim, i);
1420 duration = frames->get_frame_duration(edited_anim, i);
1421 }
1422 undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
1423 }
1424 undo_redo->add_do_method(this, "_select_animation", new_edited);
1425 undo_redo->add_undo_method(this, "_select_animation", edited_anim);
1426 undo_redo->add_do_method(this, "_update_library");
1427 undo_redo->add_undo_method(this, "_update_library");
1428 undo_redo->commit_action();
1429 }
1430
1431 StringName SpriteFramesEditor::_find_next_animation() {
1432 List<StringName> anim_names;
1433 frames->get_animation_list(&anim_names);
1434 anim_names.sort_custom<StringName::AlphCompare>();
1435 if (anim_names.size() >= 2) {
1436 if (edited_anim == anim_names.get(0)) {
1437 return anim_names.get(1);
1438 } else {
1439 return anim_names.get(0);
1440 }
1441 } else {
1442 return StringName();
1443 }
1444 }
1445
1446 String SpriteFramesEditor::_generate_unique_animation_name(const String &p_base_name) const {
1447 if (!frames->has_animation(p_base_name)) {
1448 return p_base_name;
1449 }
1450
1451 int count = 2;
1452 String new_name = p_base_name;
1453 PackedStringArray split = new_name.split("_");
1454 int last_index = split.size() - 1;
1455 if (last_index > 0 && split[last_index].is_valid_int() && split[last_index].to_int() >= 0) {
1456 count = split[last_index].to_int();
1457 split.remove_at(last_index);
1458 new_name = String("_").join(split);
1459 }
1460 while (true) {
1461 String attempt = new_name;
1462 attempt += vformat("_%d", count);
1463 if (frames->has_animation(attempt)) {
1464 count++;
1465 continue;
1466 }
1467 new_name = attempt;
1468 break;
1469 }
1470 return new_name;
1471 }
1472
1473 void SpriteFramesEditor::_frame_list_gui_input(const Ref<InputEvent> &p_event) {
1474 const Ref<InputEventMouseButton> mb = p_event;
1475
1476 if (mb.is_valid()) {
1477 if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
1478 _zoom_in();
1479 // Don't scroll up after zooming in.
1480 accept_event();
1481 } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
1482 _zoom_out();
1483 // Don't scroll down after zooming out.
1484 accept_event();
1485 } else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
1486 Point2 pos = mb->get_position();
1487 right_clicked_frame = frame_list->get_item_at_position(pos, true);
1488 if (right_clicked_frame != -1) {
1489 Ref<Texture2D> tex = frames->get_frame_texture(edited_anim, right_clicked_frame);
1490 if (tex.is_null()) {
1491 return;
1492 }
1493 if (!menu) {
1494 menu = memnew(PopupMenu);
1495 add_child(menu);
1496 menu->connect(SceneStringName(id_pressed), callable_mp(this, &SpriteFramesEditor::_menu_selected));
1497 menu->add_icon_item(get_editor_theme_icon(SNAME("ShowInFileSystem")), TTRC("Show in FileSystem"), MENU_SHOW_IN_FILESYSTEM);
1498 }
1499
1500 menu->set_position(get_screen_position() + get_local_mouse_position());
1501 menu->popup();
1502 }
1503 }
1504 }
1505 }
1506
1507 void SpriteFramesEditor::_menu_selected(int p_id) {
1508 switch (p_id) {
1509 case MENU_SHOW_IN_FILESYSTEM: {
1510 Ref<Texture2D> frame_texture = frames->get_frame_texture(edited_anim, right_clicked_frame);
1511 ERR_FAIL_COND(frame_texture.is_null());
1512 String path = frame_texture->get_path();
1513 // Check if the file is an atlas resource, if it is find the source texture.
1514 Ref<AtlasTexture> at = frame_texture;
1515 while (at.is_valid() && at->get_atlas().is_valid()) {
1516 path = at->get_atlas()->get_path();
1517 at = at->get_atlas();
1518 }
1519 FileSystemDock::get_singleton()->navigate_to_path(path);
1520 } break;
1521 }
1522 }
1523
1524 void SpriteFramesEditor::_frame_list_item_selected(int p_index, bool p_selected) {
1525 if (updating) {
1526 return;
1527 }
1528
1529 selection = frame_list->get_selected_items();
1530 if (selection.is_empty() || !p_selected) {
1531 return;
1532 }
1533
1534 updating = true;
1535 frame_duration->set_value(frames->get_frame_duration(edited_anim, selection[0]));
1536 updating = false;
1537 }
1538
1539 void SpriteFramesEditor::_frame_duration_changed(double p_value) {
1540 if (frames.is_null()) {
1541 return;
1542 }
1543
1544 if (updating) {
1545 return;
1546 }
1547
1548 if (selection.is_empty()) {
1549 return;
1550 }
1551
1552 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1553 undo_redo->create_action(TTR("Set Frame Duration"), UndoRedo::MERGE_ENDS, frames.ptr());
1554
1555 for (const int &index : selection) {
1556 Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, index);
1557 float old_duration = frames->get_frame_duration(edited_anim, index);
1558
1559 undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, index, texture, p_value);
1560 undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, index, texture, old_duration);
1561 }
1562
1563 undo_redo->add_do_method(this, "_update_library");
1564 undo_redo->add_undo_method(this, "_update_library");
1565 undo_redo->commit_action();
1566 }
1567
1568 void SpriteFramesEditor::_zoom_in() {
1569 // Do not zoom in or out with no visible frames
1570 if (frames->get_frame_count(edited_anim) <= 0) {
1571 return;
1572 }
1573 if (thumbnail_zoom < max_thumbnail_zoom) {
1574 thumbnail_zoom *= scale_ratio;
1575 int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
1576 frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
1577 frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
1578 }
1579 }
1580
1581 void SpriteFramesEditor::_zoom_out() {
1582 // Do not zoom in or out with no visible frames
1583 if (frames->get_frame_count(edited_anim) <= 0) {
1584 return;
1585 }
1586 if (thumbnail_zoom > min_thumbnail_zoom) {
1587 thumbnail_zoom /= scale_ratio;
1588 int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
1589 frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
1590 frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
1591 }
1592 }
1593
1594 void SpriteFramesEditor::_zoom_reset() {
1595 thumbnail_zoom = MAX(1.0f, EDSCALE);
1596 frame_list->set_fixed_column_width(thumbnail_default_size * 3 / 2);
1597 frame_list->set_fixed_icon_size(Size2(thumbnail_default_size, thumbnail_default_size));
1598 }
1599
1600 void SpriteFramesEditor::_update_library(bool p_skip_selector) {
1601 if (!p_skip_selector) {
1602 animations_dirty = true;
1603 }
1604
1605 if (pending_update) {
1606 return;
1607 }
1608 pending_update = true;
1609 callable_mp(this, &SpriteFramesEditor::_update_library_impl).call_deferred();
1610 }
1611
1612 void SpriteFramesEditor::_update_library_impl() {
1613 pending_update = false;
1614
1615 if (frames.is_null()) {
1616 return;
1617 }
1618
1619 updating = true;
1620
1621 frame_duration->set_value_no_signal(1.0); // Default.
1622
1623 if (animations_dirty) {
1624 animations_dirty = false;
1625 animations->clear();
1626
1627 TreeItem *anim_root = animations->create_item();
1628
1629 List<StringName> anim_names;
1630 frames->get_animation_list(&anim_names);
1631 anim_names.sort_custom<StringName::AlphCompare>();
1632 if (!anim_names.size()) {
1633 missing_anim_label->show();
1634 anim_frames_vb->hide();
1635 updating = false;
1636 return;
1637 }
1638 missing_anim_label->hide();
1639 anim_frames_vb->show();
1640 bool searching = anim_search_box->get_text().size();
1641 String searched_string = searching ? anim_search_box->get_text().to_lower() : String();
1642
1643 TreeItem *selected = nullptr;
1644 for (const StringName &E : anim_names) {
1645 String name = E;
1646 if (searching && !name.to_lower().contains(searched_string)) {
1647 continue;
1648 }
1649 TreeItem *it = animations->create_item(anim_root);
1650 it->set_metadata(0, name);
1651 it->set_text(0, name);
1652 it->set_editable(0, true);
1653 if (animated_sprite) {
1654 if (name == String(animated_sprite->call("get_autoplay"))) {
1655 it->set_icon(0, autoplay_icon);
1656 }
1657 }
1658 if (E == edited_anim) {
1659 it->select(0);
1660 selected = it;
1661 }
1662 }
1663 if (selected) {
1664 animations->scroll_to_item(selected);
1665 }
1666 }
1667
1668 if (animated_sprite) {
1669 String autoplay_name = animated_sprite->call("get_autoplay");
1670 if (autoplay_name.is_empty()) {
1671 autoplay->set_pressed_no_signal(false);
1672 } else {
1673 autoplay->set_pressed_no_signal(String(edited_anim) == autoplay_name);
1674 }
1675 }
1676
1677 frame_list->clear();
1678
1679 if (!frames->has_animation(edited_anim)) {
1680 updating = false;
1681 return;
1682 }
1683
1684 int anim_frame_count = frames->get_frame_count(edited_anim);
1685 if (anim_frame_count == 0) {
1686 selection.clear();
1687 }
1688
1689 for (int index = 0; index < selection.size(); index++) {
1690 int sel = selection[index];
1691 if (sel == -1) {
1692 selection.remove_at(index);
1693 index--;
1694 }
1695 if (sel >= anim_frame_count) {
1696 selection.set(index, anim_frame_count - 1);
1697 // Since selection is ordered, if we get a frame that is outside of the range
1698 // we can clip all the other one.
1699 selection.resize(index + 1);
1700 break;
1701 }
1702 }
1703
1704 if (selection.is_empty() && frames->get_frame_count(edited_anim)) {
1705 selection.push_back(0);
1706 }
1707
1708 bool is_first_selection = true;
1709 // NOTE: When the language is changed, the text of the items is updated in `NOTIFICATION_TRANSLATION_CHANGED`.
1710 // If there are changes related to the items and their text in the loop below, the code in `NOTIFICATION_TRANSLATION_CHANGED` must also be changed.
1711 for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
1712 String name = itos(i);
1713 Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
1714 float duration = frames->get_frame_duration(edited_anim, i);
1715
1716 if (texture.is_null()) {
1717 texture = empty_icon;
1718 name += ": " + TTR("(empty)");
1719 } else if (!texture->get_name().is_empty()) {
1720 name += ": " + texture->get_name();
1721 }
1722
1723 if (duration != 1.0f) {
1724 name += String::utf8(" [× ") + String::num(duration, 2) + "]";
1725 }
1726
1727 frame_list->add_item(name, texture);
1728 if (texture.is_valid()) {
1729 String tooltip = texture->get_path();
1730
1731 // Frame is often saved as an AtlasTexture subresource within a scene/resource file,
1732 // thus its path might be not what the user is looking for. So we're also showing
1733 // subsequent source texture paths.
1734 String prefix = U"â”–â•´";
1735 Ref<AtlasTexture> at = texture;
1736 while (at.is_valid() && at->get_atlas().is_valid()) {
1737 tooltip += "\n" + prefix + at->get_atlas()->get_path();
1738 prefix = " " + prefix;
1739 at = at->get_atlas();
1740 }
1741
1742 frame_list->set_item_tooltip(-1, tooltip);
1743 }
1744 if (selection.has(i)) {
1745 frame_list->select(frame_list->get_item_count() - 1, is_first_selection);
1746 if (is_first_selection) {
1747 frame_duration->set_value_no_signal(frames->get_frame_duration(edited_anim, i));
1748 }
1749 is_first_selection = false;
1750 }
1751 }
1752
1753 anim_speed->set_value_no_signal(frames->get_animation_speed(edited_anim));
1754 anim_loop->set_pressed_no_signal(frames->get_animation_loop(edited_anim));
1755
1756 updating = false;
1757 }
1758
1759 void SpriteFramesEditor::_edit() {
1760 if (!animated_sprite) {
1761 return;
1762 }
1763 edit(animated_sprite->call("get_sprite_frames"));
1764 }
1765
1766 void SpriteFramesEditor::edit(Ref<SpriteFrames> p_frames) {
1767 _update_stop_icon();
1768
1769 if (p_frames.is_null()) {
1770 frames.unref();
1771 _remove_sprite_node();
1772 return;
1773 }
1774
1775 frames = p_frames;
1776 read_only = EditorNode::get_singleton()->is_resource_read_only(p_frames);
1777
1778 if (!p_frames->has_animation(edited_anim)) {
1779 List<StringName> anim_names;
1780 frames->get_animation_list(&anim_names);
1781 anim_names.sort_custom<StringName::AlphCompare>();
1782 if (anim_names.size()) {
1783 edited_anim = anim_names.front()->get();
1784 } else {
1785 edited_anim = StringName();
1786 }
1787 }
1788
1789 _update_library();
1790 // Clear zoom and split sheet texture
1791 split_sheet_preview->set_texture(Ref<Texture2D>());
1792 _zoom_reset();
1793
1794 add_anim->set_disabled(read_only);
1795 duplicate_anim->set_disabled(read_only);
1796 cut_anim->set_disabled(read_only);
1797 copy_anim->set_disabled(read_only);
1798 paste_anim->set_disabled(read_only);
1799 delete_anim->set_disabled(read_only);
1800 anim_speed->set_editable(!read_only);
1801 anim_loop->set_disabled(read_only);
1802 load->set_disabled(read_only);
1803 load_sheet->set_disabled(read_only);
1804 copy->set_disabled(read_only);
1805 paste->set_disabled(read_only);
1806 empty_before->set_disabled(read_only);
1807 empty_after->set_disabled(read_only);
1808 move_up->set_disabled(read_only);
1809 move_down->set_disabled(read_only);
1810 delete_frame->set_disabled(read_only);
1811
1812 _fetch_sprite_node(); // Fetch node after set frames.
1813 }
1814
1815 Ref<SpriteFrames> SpriteFramesEditor::get_sprite_frames() const {
1816 return frames;
1817 }
1818
1819 Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
1820 if (read_only) {
1821 return false;
1822 }
1823
1824 if (!frames->has_animation(edited_anim)) {
1825 return false;
1826 }
1827
1828 int idx = -1;
1829 if (p_point == Vector2(Math::INF, Math::INF)) {
1830 if (frame_list->is_anything_selected()) {
1831 idx = frame_list->get_selected_items()[0];
1832 }
1833 } else {
1834 idx = frame_list->get_item_at_position(p_point, true);
1835 }
1836
1837 if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) {
1838 return Variant();
1839 }
1840
1841 Ref<Resource> frame = frames->get_frame_texture(edited_anim, idx);
1842
1843 if (frame.is_null()) {
1844 return Variant();
1845 }
1846
1847 Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
1848 drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
1849 return drag_data;
1850 }
1851
1852 bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
1853 if (read_only) {
1854 return false;
1855 }
1856
1857 Dictionary d = p_data;
1858
1859 if (!d.has("type")) {
1860 return false;
1861 }
1862
1863 // reordering frames
1864 if (d.has("from") && (Object *)(d["from"]) == frame_list) {
1865 return true;
1866 }
1867
1868 if (String(d["type"]) == "resource" && d.has("resource")) {
1869 Ref<Resource> r = d["resource"];
1870
1871 Ref<Texture2D> texture = r;
1872
1873 if (texture.is_valid()) {
1874 return true;
1875 }
1876 }
1877
1878 if (String(d["type"]) == "files") {
1879 Vector<String> files = d["files"];
1880
1881 if (files.is_empty()) {
1882 return false;
1883 }
1884
1885 for (int i = 0; i < files.size(); i++) {
1886 const String &f = files[i];
1887 String ftype = EditorFileSystem::get_singleton()->get_file_type(f);
1888
1889 if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
1890 return false;
1891 }
1892 }
1893
1894 return true;
1895 }
1896 return false;
1897 }
1898
1899 void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
1900 if (!can_drop_data_fw(p_point, p_data, p_from)) {
1901 return;
1902 }
1903
1904 Dictionary d = p_data;
1905
1906 if (!d.has("type")) {
1907 return;
1908 }
1909
1910 int at_pos = -1;
1911 if (p_point == Vector2(Math::INF, Math::INF)) {
1912 if (frame_list->is_anything_selected()) {
1913 at_pos = frame_list->get_selected_items()[0];
1914 }
1915 } else {
1916 at_pos = frame_list->get_item_at_position(p_point, true);
1917 }
1918
1919 if (String(d["type"]) == "resource" && d.has("resource")) {
1920 Ref<Resource> r = d["resource"];
1921
1922 Ref<Texture2D> texture = r;
1923
1924 if (texture.is_valid()) {
1925 bool reorder = false;
1926 if (d.has("from") && (Object *)(d["from"]) == frame_list) {
1927 reorder = true;
1928 }
1929
1930 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1931 if (reorder) { //drop is from reordering frames
1932 int from_frame = -1;
1933 float duration = 1.0;
1934 if (d.has("frame")) {
1935 from_frame = d["frame"];
1936 duration = frames->get_frame_duration(edited_anim, from_frame);
1937 }
1938
1939 undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
1940 undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
1941 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration, at_pos == -1 ? -1 : at_pos);
1942 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
1943 undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration, from_frame);
1944 undo_redo->add_do_method(this, "_update_library");
1945 undo_redo->add_undo_method(this, "_update_library");
1946 undo_redo->commit_action();
1947 } else {
1948 undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
1949 undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, at_pos == -1 ? -1 : at_pos);
1950 undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
1951 undo_redo->add_do_method(this, "_update_library");
1952 undo_redo->add_undo_method(this, "_update_library");
1953 undo_redo->commit_action();
1954 }
1955 }
1956 }
1957
1958 if (String(d["type"]) == "files") {
1959 Vector<String> files = d["files"];
1960
1961 if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
1962 _prepare_sprite_sheet(files[0]);
1963 } else {
1964 _file_load_request(files, at_pos);
1965 }
1966 }
1967 }
1968
1969 void SpriteFramesEditor::_update_stop_icon() {
1970 bool is_playing = false;
1971 if (animated_sprite) {
1972 is_playing = animated_sprite->call("is_playing");
1973 }
1974 if (is_playing) {
1975 stop->set_button_icon(pause_icon);
1976 } else {
1977 stop->set_button_icon(stop_icon);
1978 }
1979 }
1980
1981 void SpriteFramesEditor::_remove_sprite_node() {
1982 if (!animated_sprite) {
1983 return;
1984 }
1985 if (animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
1986 animated_sprite->disconnect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
1987 }
1988 if (animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
1989 animated_sprite->disconnect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation));
1990 }
1991 if (animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
1992 animated_sprite->disconnect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
1993 }
1994 animated_sprite = nullptr;
1995 }
1996
1997 void SpriteFramesEditor::_fetch_sprite_node() {
1998 Node *selected = nullptr;
1999 EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
2000 const List<Node *> &top_node_list = editor_selection->get_top_selected_node_list();
2001 if (top_node_list.size() == 1) {
2002 selected = top_node_list.front()->get();
2003 }
2004
2005 bool show_node_edit = false;
2006 AnimatedSprite2D *as2d = Object::cast_to<AnimatedSprite2D>(selected);
2007 AnimatedSprite3D *as3d = Object::cast_to<AnimatedSprite3D>(selected);
2008 if (as2d || as3d) {
2009 if (frames != selected->call("get_sprite_frames")) {
2010 _remove_sprite_node();
2011 } else {
2012 animated_sprite = selected;
2013 if (!animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
2014 animated_sprite->connect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
2015 }
2016 if (!animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
2017 animated_sprite->connect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation), CONNECT_DEFERRED);
2018 }
2019 if (!animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
2020 animated_sprite->connect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
2021 }
2022 show_node_edit = true;
2023 }
2024 } else {
2025 _remove_sprite_node();
2026 }
2027
2028 if (show_node_edit) {
2029 _sync_animation();
2030 autoplay_container->show();
2031 playback_container->show();
2032 } else {
2033 _update_library(); // To init autoplay icon.
2034 autoplay_container->hide();
2035 playback_container->hide();
2036 }
2037 }
2038
2039 void SpriteFramesEditor::_play_pressed() {
2040 if (animated_sprite) {
2041 animated_sprite->call("stop");
2042 animated_sprite->call("play", animated_sprite->call("get_animation"));
2043 }
2044 _update_stop_icon();
2045 }
2046
2047 void SpriteFramesEditor::_play_from_pressed() {
2048 if (animated_sprite) {
2049 animated_sprite->call("play", animated_sprite->call("get_animation"));
2050 }
2051 _update_stop_icon();
2052 }
2053
2054 void SpriteFramesEditor::_play_bw_pressed() {
2055 if (animated_sprite) {
2056 animated_sprite->call("stop");
2057 animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
2058 }
2059 _update_stop_icon();
2060 }
2061
2062 void SpriteFramesEditor::_play_bw_from_pressed() {
2063 if (animated_sprite) {
2064 animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
2065 }
2066 _update_stop_icon();
2067 }
2068
2069 void SpriteFramesEditor::_stop_pressed() {
2070 if (animated_sprite) {
2071 if (animated_sprite->call("is_playing")) {
2072 animated_sprite->call("pause");
2073 } else {
2074 animated_sprite->call("stop");
2075 }
2076 }
2077 _update_stop_icon();
2078 }
2079
2080 void SpriteFramesEditor::_autoplay_pressed() {
2081 if (updating) {
2082 return;
2083 }
2084
2085 if (animated_sprite) {
2086 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2087 undo_redo->create_action(TTR("Toggle Autoplay"), UndoRedo::MERGE_DISABLE, animated_sprite);
2088 String current = animated_sprite->call("get_animation");
2089 String current_auto = animated_sprite->call("get_autoplay");
2090 if (current == current_auto) {
2091 //unset
2092 undo_redo->add_do_method(animated_sprite, "set_autoplay", "");
2093 undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
2094 } else {
2095 //set
2096 undo_redo->add_do_method(animated_sprite, "set_autoplay", current);
2097 undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
2098 }
2099 undo_redo->add_do_method(this, "_update_library");
2100 undo_redo->add_undo_method(this, "_update_library");
2101 undo_redo->commit_action();
2102 }
2103
2104 _update_library();
2105 }
2106
2107 void SpriteFramesEditor::_bind_methods() {
2108 ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
2109 ClassDB::bind_method(D_METHOD("_select_animation", "name", "update_node"), &SpriteFramesEditor::_select_animation, DEFVAL(true));
2110 }
2111
2112 void SpriteFramesEditor::_node_removed(Node *p_node) {
2113 if (animated_sprite) {
2114 if (animated_sprite != p_node) {
2115 return;
2116 }
2117 _remove_sprite_node();
2118 }
2119 }
2120
2121 SpriteFramesEditor::SpriteFramesEditor() {
2122 set_name(TTRC("SpriteFrames"));
2123 set_icon_name("SpriteFrames");
2124 set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_sprite_frames_bottom_panel", TTRC("Open SpriteFrames Dock")));
2125 set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
2126 set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);
2127 set_global(false);
2128 set_transient(true);
2129
2130 HSplitContainer *main_split = memnew(HSplitContainer);
2131 add_child(main_split);
2132
2133 VBoxContainer *vbc_animlist = memnew(VBoxContainer);
2134 main_split->add_child(vbc_animlist);
2135 vbc_animlist->set_custom_minimum_size(Size2(150 * EDSCALE, 0));
2136
2137 VBoxContainer *sub_vb = memnew(VBoxContainer);
2138 vbc_animlist->add_margin_child(TTRC("Animations:"), sub_vb, true);
2139 sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
2140
2141 HBoxContainer *hbc_animlist = memnew(HBoxContainer);
2142 sub_vb->add_child(hbc_animlist);
2143
2144 add_anim = memnew(Button);
2145 add_anim->set_theme_type_variation(SceneStringName(FlatButton));
2146 add_anim->set_accessibility_name(TTRC("Add Animation"));
2147 hbc_animlist->add_child(add_anim);
2148 add_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_add));
2149
2150 duplicate_anim = memnew(Button);
2151 duplicate_anim->set_theme_type_variation(SceneStringName(FlatButton));
2152 duplicate_anim->set_accessibility_name(TTRC("Duplicate Animation"));
2153 hbc_animlist->add_child(duplicate_anim);
2154 duplicate_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_duplicate));
2155 duplicate_anim->set_visible(false);
2156
2157 cut_anim = memnew(Button);
2158 cut_anim->set_theme_type_variation(SceneStringName(FlatButton));
2159 cut_anim->set_accessibility_name(TTRC("Cut Animation"));
2160 hbc_animlist->add_child(cut_anim);
2161 cut_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_cut));
2162
2163 copy_anim = memnew(Button);
2164 copy_anim->set_theme_type_variation(SceneStringName(FlatButton));
2165 copy_anim->set_accessibility_name(TTRC("Copy Animation"));
2166 hbc_animlist->add_child(copy_anim);
2167 copy_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_copy));
2168
2169 paste_anim = memnew(Button);
2170 paste_anim->set_theme_type_variation(SceneStringName(FlatButton));
2171 paste_anim->set_accessibility_name(TTRC("Paste Animation"));
2172 hbc_animlist->add_child(paste_anim);
2173 paste_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_paste));
2174
2175 delete_anim = memnew(Button);
2176 delete_anim->set_theme_type_variation(SceneStringName(FlatButton));
2177 delete_anim->set_accessibility_name(TTRC("Delete Animation"));
2178 hbc_animlist->add_child(delete_anim);
2179 delete_anim->set_disabled(true);
2180 delete_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_remove));
2181
2182 autoplay_container = memnew(HBoxContainer);
2183 hbc_animlist->add_child(autoplay_container);
2184
2185 autoplay_container->add_child(memnew(VSeparator));
2186
2187 autoplay = memnew(Button);
2188 autoplay->set_theme_type_variation(SceneStringName(FlatButton));
2189 autoplay->set_tooltip_text(TTRC("Autoplay on Load"));
2190 autoplay_container->add_child(autoplay);
2191
2192 hbc_animlist->add_child(memnew(VSeparator));
2193
2194 anim_loop = memnew(Button);
2195 anim_loop->set_toggle_mode(true);
2196 anim_loop->set_theme_type_variation(SceneStringName(FlatButton));
2197 anim_loop->set_tooltip_text(TTRC("Animation Looping"));
2198 anim_loop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_loop_changed));
2199 hbc_animlist->add_child(anim_loop);
2200
2201 anim_speed = memnew(SpinBox);
2202 anim_speed->set_suffix(TTR("FPS"));
2203 anim_speed->set_min(0);
2204 anim_speed->set_max(120);
2205 anim_speed->set_step(0.01);
2206 anim_speed->set_custom_arrow_step(1);
2207 anim_speed->set_tooltip_text(TTRC("Animation Speed"));
2208 anim_speed->get_line_edit()->set_expand_to_text_length_enabled(true);
2209 anim_speed->get_line_edit()->connect(SceneStringName(resized), callable_mp(this, &SpriteFramesEditor::_animation_speed_resized));
2210 anim_speed->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_animation_speed_changed));
2211 hbc_animlist->add_child(anim_speed);
2212
2213 anim_search_box = memnew(LineEdit);
2214 sub_vb->add_child(anim_search_box);
2215 anim_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
2216 anim_search_box->set_placeholder(TTRC("Filter Animations"));
2217 anim_search_box->set_clear_button_enabled(true);
2218 anim_search_box->connect(SceneStringName(text_changed), callable_mp(this, &SpriteFramesEditor::_animation_search_text_changed));
2219
2220 animations = memnew(Tree);
2221 sub_vb->add_child(animations);
2222 animations->set_v_size_flags(SIZE_EXPAND_FILL);
2223 animations->set_hide_root(true);
2224 animations->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2225 // HACK: The cell_selected signal is emitted before the FPS spinbox loses focus and applies the change.
2226 animations->connect("cell_selected", callable_mp(this, &SpriteFramesEditor::_animation_selected), CONNECT_DEFERRED);
2227 animations->connect("item_edited", callable_mp(this, &SpriteFramesEditor::_animation_name_edited));
2228 animations->set_theme_type_variation("TreeSecondary");
2229 animations->set_allow_reselect(true);
2230
2231 add_anim->set_shortcut_context(animations);
2232 add_anim->set_shortcut(ED_SHORTCUT("sprite_frames/new_animation", TTRC("Add Animation"), KeyModifierMask::CMD_OR_CTRL | Key::N));
2233 duplicate_anim->set_shortcut_context(animations);
2234 duplicate_anim->set_shortcut(ED_SHORTCUT("sprite_frames/duplicate_animation", TTRC("Duplicate Animation"), KeyModifierMask::CMD_OR_CTRL | Key::D));
2235 cut_anim->set_shortcut_context(animations);
2236 cut_anim->set_shortcut(ED_SHORTCUT("sprite_frames/cut_animation", TTRC("Cut Animation"), KeyModifierMask::CMD_OR_CTRL | Key::X));
2237 copy_anim->set_shortcut_context(animations);
2238 copy_anim->set_shortcut(ED_SHORTCUT("sprite_frames/copy_animation", TTRC("Copy Animation"), KeyModifierMask::CMD_OR_CTRL | Key::C));
2239 paste_anim->set_shortcut_context(animations);
2240 paste_anim->set_shortcut(ED_SHORTCUT("sprite_frames/paste_animation", TTRC("Paste Animation"), KeyModifierMask::CMD_OR_CTRL | Key::V));
2241 delete_anim->set_shortcut_context(animations);
2242 delete_anim->set_shortcut(ED_SHORTCUT("sprite_frames/delete_animation", TTRC("Delete Animation"), Key::KEY_DELETE));
2243
2244 missing_anim_label = memnew(Label);
2245 missing_anim_label->set_focus_mode(FOCUS_ACCESSIBILITY);
2246 missing_anim_label->set_text(TTRC("This resource does not have any animations."));
2247 missing_anim_label->set_h_size_flags(SIZE_EXPAND_FILL);
2248 missing_anim_label->set_v_size_flags(SIZE_EXPAND_FILL);
2249 missing_anim_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
2250 missing_anim_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
2251 missing_anim_label->hide();
2252 main_split->add_child(missing_anim_label);
2253
2254 anim_frames_vb = memnew(VBoxContainer);
2255 main_split->add_child(anim_frames_vb);
2256 anim_frames_vb->set_h_size_flags(SIZE_EXPAND_FILL);
2257 anim_frames_vb->hide();
2258
2259 sub_vb = memnew(VBoxContainer);
2260 anim_frames_vb->add_margin_child(TTRC("Animation Frames:"), sub_vb, true);
2261
2262 HFlowContainer *hfc = memnew(HFlowContainer);
2263 sub_vb->add_child(hfc);
2264
2265 playback_container = memnew(HBoxContainer);
2266 playback_container->set_layout_direction(LAYOUT_DIRECTION_LTR);
2267 hfc->add_child(playback_container);
2268
2269 play_bw_from = memnew(Button);
2270 play_bw_from->set_theme_type_variation(SceneStringName(FlatButton));
2271 playback_container->add_child(play_bw_from);
2272
2273 play_bw = memnew(Button);
2274 play_bw->set_theme_type_variation(SceneStringName(FlatButton));
2275 playback_container->add_child(play_bw);
2276
2277 stop = memnew(Button);
2278 stop->set_theme_type_variation(SceneStringName(FlatButton));
2279 playback_container->add_child(stop);
2280
2281 play = memnew(Button);
2282 play->set_theme_type_variation(SceneStringName(FlatButton));
2283 playback_container->add_child(play);
2284
2285 play_from = memnew(Button);
2286 play_from->set_theme_type_variation(SceneStringName(FlatButton));
2287 playback_container->add_child(play_from);
2288
2289 hfc->add_child(memnew(VSeparator));
2290
2291 autoplay->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_autoplay_pressed));
2292 autoplay->set_toggle_mode(true);
2293 play->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_pressed));
2294 play_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_from_pressed));
2295 play_bw->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_pressed));
2296 play_bw_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_from_pressed));
2297 stop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_stop_pressed));
2298
2299 HBoxContainer *hbc_actions = memnew(HBoxContainer);
2300 hfc->add_child(hbc_actions);
2301
2302 load = memnew(Button);
2303 load->set_accessibility_name(TTRC("Load"));
2304 load->set_theme_type_variation(SceneStringName(FlatButton));
2305 hbc_actions->add_child(load);
2306
2307 load_sheet = memnew(Button);
2308 load_sheet->set_accessibility_name(TTRC("Load Sheet"));
2309 load_sheet->set_theme_type_variation(SceneStringName(FlatButton));
2310 hbc_actions->add_child(load_sheet);
2311
2312 hbc_actions->add_child(memnew(VSeparator));
2313
2314 copy = memnew(Button);
2315 copy->set_accessibility_name(TTRC("Copy"));
2316 copy->set_theme_type_variation(SceneStringName(FlatButton));
2317 hbc_actions->add_child(copy);
2318
2319 paste = memnew(Button);
2320 paste->set_accessibility_name(TTRC("Paste"));
2321 paste->set_theme_type_variation(SceneStringName(FlatButton));
2322 hbc_actions->add_child(paste);
2323
2324 hbc_actions->add_child(memnew(VSeparator));
2325
2326 empty_before = memnew(Button);
2327 empty_before->set_accessibility_name(TTRC("Empty Before"));
2328 empty_before->set_theme_type_variation(SceneStringName(FlatButton));
2329 hbc_actions->add_child(empty_before);
2330
2331 empty_after = memnew(Button);
2332 empty_after->set_accessibility_name(TTRC("Empty After"));
2333 empty_after->set_theme_type_variation(SceneStringName(FlatButton));
2334 hbc_actions->add_child(empty_after);
2335
2336 hbc_actions->add_child(memnew(VSeparator));
2337
2338 move_up = memnew(Button);
2339 move_up->set_accessibility_name(TTRC("Move Up"));
2340 move_up->set_theme_type_variation(SceneStringName(FlatButton));
2341 hbc_actions->add_child(move_up);
2342
2343 move_down = memnew(Button);
2344 move_down->set_accessibility_name(TTRC("Move Down"));
2345 move_down->set_theme_type_variation(SceneStringName(FlatButton));
2346 hbc_actions->add_child(move_down);
2347
2348 delete_frame = memnew(Button);
2349 delete_frame->set_accessibility_name(TTRC("Delete Frame"));
2350 delete_frame->set_theme_type_variation(SceneStringName(FlatButton));
2351 hbc_actions->add_child(delete_frame);
2352
2353 hbc_actions->add_child(memnew(VSeparator));
2354
2355 HBoxContainer *hbc_frame_duration = memnew(HBoxContainer);
2356 hfc->add_child(hbc_frame_duration);
2357
2358 Label *label = memnew(Label);
2359 label->set_text(TTRC("Frame Duration:"));
2360 hbc_frame_duration->add_child(label);
2361
2362 frame_duration = memnew(SpinBox);
2363 frame_duration->set_prefix(String::utf8("×"));
2364 frame_duration->set_min(SPRITE_FRAME_MINIMUM_DURATION); // Avoid zero div.
2365 frame_duration->set_max(10);
2366 frame_duration->set_step(0.01);
2367 frame_duration->set_custom_arrow_step(0.1);
2368 frame_duration->set_allow_lesser(false);
2369 frame_duration->set_allow_greater(true);
2370 frame_duration->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_frame_duration_changed));
2371 frame_duration->set_accessibility_name(TTRC("Frame Duration:"));
2372 hbc_frame_duration->add_child(frame_duration);
2373
2374 // Wide empty separation control. (like BoxContainer::add_spacer())
2375 Control *c = memnew(Control);
2376 c->set_mouse_filter(MOUSE_FILTER_PASS);
2377 c->set_h_size_flags(SIZE_EXPAND_FILL);
2378 hfc->add_child(c);
2379
2380 HBoxContainer *hbc_zoom = memnew(HBoxContainer);
2381 hfc->add_child(hbc_zoom);
2382
2383 zoom_out = memnew(Button);
2384 zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_out));
2385 zoom_out->set_flat(true);
2386 zoom_out->set_tooltip_text(TTRC("Zoom Out"));
2387 hbc_zoom->add_child(zoom_out);
2388
2389 zoom_reset = memnew(Button);
2390 zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_reset));
2391 zoom_reset->set_flat(true);
2392 zoom_reset->set_tooltip_text(TTRC("Zoom Reset"));
2393 hbc_zoom->add_child(zoom_reset);
2394
2395 zoom_in = memnew(Button);
2396 zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_in));
2397 zoom_in->set_flat(true);
2398 zoom_in->set_tooltip_text(TTRC("Zoom In"));
2399 hbc_zoom->add_child(zoom_in);
2400
2401 file = memnew(EditorFileDialog);
2402 file->connect("files_selected", callable_mp(this, &SpriteFramesEditor::_file_load_request).bind(-1));
2403 add_child(file);
2404
2405 frame_list = memnew(ItemList);
2406 frame_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2407 frame_list->set_v_size_flags(SIZE_EXPAND_FILL);
2408 frame_list->set_icon_mode(ItemList::ICON_MODE_TOP);
2409 frame_list->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
2410 frame_list->set_select_mode(ItemList::SELECT_MULTI);
2411 frame_list->set_theme_type_variation("ItemListSecondary");
2412
2413 frame_list->set_max_columns(0);
2414 frame_list->set_max_text_lines(2);
2415 SET_DRAG_FORWARDING_GCD(frame_list, SpriteFramesEditor);
2416 frame_list->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_frame_list_gui_input));
2417 // HACK: The item_selected signal is emitted before the Frame Duration spinbox loses focus and applies the change.
2418 frame_list->connect("multi_selected", callable_mp(this, &SpriteFramesEditor::_frame_list_item_selected), CONNECT_DEFERRED);
2419
2420 sub_vb->add_child(frame_list);
2421
2422 dialog = memnew(AcceptDialog);
2423 add_child(dialog);
2424
2425 load->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_load_pressed));
2426 load_sheet->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_open_sprite_sheet));
2427 delete_frame->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_delete_pressed));
2428 copy->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_copy_pressed));
2429 paste->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_paste_pressed));
2430 empty_before->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty_pressed));
2431 empty_after->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty2_pressed));
2432 move_up->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_up_pressed));
2433 move_down->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_down_pressed));
2434
2435 play_bw_from->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation_backwards", TTRC("Play Animation Backwards"), Key::A));
2436 play_bw->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation_from_end", TTRC("Play Animation Backwards from End"), KeyModifierMask::SHIFT + Key::A));
2437 stop->set_shortcut(ED_SHORTCUT("sprite_frames/stop_animation", TTRC("Pause/Stop Animation"), Key::S));
2438 play->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation_from_start", TTRC("Play Animation from Start"), KeyModifierMask::SHIFT + Key::D));
2439 play_from->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation", TTRC("Play Animation"), Key::D));
2440 load->set_shortcut_context(frame_list);
2441 load->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_file", TTRC("Add Frame from File"), KeyModifierMask::CMD_OR_CTRL | Key::O));
2442 load_sheet->set_shortcut_context(frame_list);
2443 load_sheet->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_sheet", TTRC("Add Frames from Sprite Sheet"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O));
2444 delete_frame->set_shortcut_context(frame_list);
2445 delete_frame->set_shortcut(ED_SHORTCUT("sprite_frames/delete", TTRC("Delete Frame"), Key::KEY_DELETE));
2446 copy->set_shortcut_context(frame_list);
2447 copy->set_shortcut(ED_SHORTCUT("sprite_frames/copy", TTRC("Copy Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::C));
2448 paste->set_shortcut_context(frame_list);
2449 paste->set_shortcut(ED_SHORTCUT("sprite_frames/paste", TTRC("Paste Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::V));
2450 empty_before->set_shortcut_context(frame_list);
2451 empty_before->set_shortcut(ED_SHORTCUT("sprite_frames/empty_before", TTRC("Insert Empty (Before Selected)"), KeyModifierMask::ALT | Key::LEFT));
2452 empty_after->set_shortcut_context(frame_list);
2453 empty_after->set_shortcut(ED_SHORTCUT("sprite_frames/empty_after", TTRC("Insert Empty (After Selected)"), KeyModifierMask::ALT | Key::RIGHT));
2454 move_up->set_shortcut_context(frame_list);
2455 move_up->set_shortcut(ED_SHORTCUT("sprite_frames/move_left", TTRC("Move Frame Left"), KeyModifierMask::CMD_OR_CTRL | Key::LEFT));
2456 move_down->set_shortcut_context(frame_list);
2457 move_down->set_shortcut(ED_SHORTCUT("sprite_frames/move_right", TTRC("Move Frame Right"), KeyModifierMask::CMD_OR_CTRL | Key::RIGHT));
2458
2459 zoom_out->set_shortcut_context(frame_list);
2460 zoom_out->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_out", TTRC("Zoom Out"),
2461 { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) }));
2462 zoom_in->set_shortcut_context(frame_list);
2463 zoom_in->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_in", TTRC("Zoom In"),
2464 { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) }));
2465
2466 loading_scene = false;
2467
2468 updating = false;
2469
2470 edited_anim = SceneStringName(default_);
2471
2472 delete_dialog = memnew(ConfirmationDialog);
2473 add_child(delete_dialog);
2474 delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_animation_remove_confirmed));
2475
2476 split_sheet_dialog = memnew(ConfirmationDialog);
2477 split_sheet_dialog->set_flag(Window::FLAG_MAXIMIZE_DISABLED, false);
2478 add_child(split_sheet_dialog);
2479 split_sheet_dialog->set_title(TTRC("Select Frames"));
2480 split_sheet_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_sheet_add_frames));
2481
2482 HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
2483 split_sheet_dialog->add_child(split_sheet_hb);
2484 split_sheet_hb->set_h_size_flags(SIZE_EXPAND_FILL);
2485 split_sheet_hb->set_v_size_flags(SIZE_EXPAND_FILL);
2486
2487 VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
2488 split_sheet_hb->add_child(split_sheet_vb);
2489 split_sheet_vb->set_h_size_flags(SIZE_EXPAND_FILL);
2490 split_sheet_vb->set_v_size_flags(SIZE_EXPAND_FILL);
2491
2492 HBoxContainer *split_sheet_menu_hb = memnew(HBoxContainer);
2493
2494 split_sheet_menu_hb->add_child(memnew(Label(TTRC("Frame Order"))));
2495
2496 split_sheet_order = memnew(OptionButton);
2497 split_sheet_order->add_item(TTRC("As Selected"), FRAME_ORDER_SELECTION);
2498 split_sheet_order->add_separator(TTRC("By Row"));
2499 split_sheet_order->add_item(TTRC("Left to Right, Top to Bottom"), FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM);
2500 split_sheet_order->add_item(TTRC("Left to Right, Bottom to Top"), FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP);
2501 split_sheet_order->add_item(TTRC("Right to Left, Top to Bottom"), FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM);
2502 split_sheet_order->add_item(TTRC("Right to Left, Bottom to Top"), FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP);
2503 split_sheet_order->add_separator(TTRC("By Column"));
2504 split_sheet_order->add_item(TTRC("Top to Bottom, Left to Right"), FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT);
2505 split_sheet_order->add_item(TTRC("Top to Bottom, Right to Left"), FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT);
2506 split_sheet_order->add_item(TTRC("Bottom to Top, Left to Right"), FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT);
2507 split_sheet_order->add_item(TTRC("Bottom to Top, Right to Left"), FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT);
2508 split_sheet_order->connect(SceneStringName(item_selected), callable_mp(this, &SpriteFramesEditor::_sheet_order_selected));
2509 split_sheet_menu_hb->add_child(split_sheet_order);
2510
2511 Button *select_all = memnew(Button);
2512 select_all->set_text(TTRC("Select All"));
2513 select_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_select_all_frames));
2514 split_sheet_menu_hb->add_child(select_all);
2515
2516 Button *clear_all = memnew(Button);
2517 clear_all->set_text(TTRC("Select None"));
2518 clear_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_clear_all_frames));
2519 split_sheet_menu_hb->add_child(clear_all);
2520
2521 split_sheet_menu_hb->add_spacer();
2522
2523 toggle_settings_button = memnew(Button);
2524 toggle_settings_button->set_h_size_flags(SIZE_SHRINK_END);
2525 toggle_settings_button->set_theme_type_variation(SceneStringName(FlatButton));
2526 toggle_settings_button->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_toggle_show_settings));
2527 toggle_settings_button->set_tooltip_text(TTRC("Toggle Settings Panel"));
2528 split_sheet_menu_hb->add_child(toggle_settings_button);
2529
2530 split_sheet_vb->add_child(split_sheet_menu_hb);
2531
2532 PanelContainer *split_sheet_panel = memnew(PanelContainer);
2533 split_sheet_panel->set_h_size_flags(SIZE_EXPAND_FILL);
2534 split_sheet_panel->set_v_size_flags(SIZE_EXPAND_FILL);
2535 split_sheet_vb->add_child(split_sheet_panel);
2536
2537 split_sheet_preview = memnew(TextureRect);
2538 split_sheet_preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
2539 split_sheet_preview->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
2540 split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
2541 split_sheet_preview->connect(SceneStringName(draw), callable_mp(this, &SpriteFramesEditor::_sheet_preview_draw));
2542 split_sheet_preview->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_preview_input));
2543
2544 split_sheet_scroll = memnew(ScrollContainer);
2545 split_sheet_scroll->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_scroll_input));
2546 split_sheet_panel->add_child(split_sheet_scroll);
2547 CenterContainer *cc = memnew(CenterContainer);
2548 cc->add_child(split_sheet_preview);
2549 cc->set_h_size_flags(SIZE_EXPAND_FILL);
2550 cc->set_v_size_flags(SIZE_EXPAND_FILL);
2551 split_sheet_scroll->add_child(cc);
2552
2553 MarginContainer *split_sheet_zoom_margin = memnew(MarginContainer);
2554 split_sheet_panel->add_child(split_sheet_zoom_margin);
2555 split_sheet_zoom_margin->set_h_size_flags(0);
2556 split_sheet_zoom_margin->set_v_size_flags(0);
2557 split_sheet_zoom_margin->add_theme_constant_override("margin_top", 5);
2558 split_sheet_zoom_margin->add_theme_constant_override("margin_left", 5);
2559 HBoxContainer *split_sheet_zoom_hb = memnew(HBoxContainer);
2560 split_sheet_zoom_margin->add_child(split_sheet_zoom_hb);
2561
2562 split_sheet_zoom_out = memnew(Button);
2563 split_sheet_zoom_out->set_theme_type_variation(SceneStringName(FlatButton));
2564 split_sheet_zoom_out->set_focus_mode(FOCUS_ACCESSIBILITY);
2565 split_sheet_zoom_out->set_tooltip_text(TTRC("Zoom Out"));
2566 split_sheet_zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_out));
2567 split_sheet_zoom_hb->add_child(split_sheet_zoom_out);
2568
2569 split_sheet_zoom_reset = memnew(Button);
2570 split_sheet_zoom_reset->set_theme_type_variation(SceneStringName(FlatButton));
2571 split_sheet_zoom_reset->set_focus_mode(FOCUS_ACCESSIBILITY);
2572 split_sheet_zoom_reset->set_tooltip_text(TTRC("Zoom Reset"));
2573 split_sheet_zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_reset));
2574 split_sheet_zoom_hb->add_child(split_sheet_zoom_reset);
2575
2576 split_sheet_zoom_in = memnew(Button);
2577 split_sheet_zoom_in->set_theme_type_variation(SceneStringName(FlatButton));
2578 split_sheet_zoom_in->set_focus_mode(FOCUS_ACCESSIBILITY);
2579 split_sheet_zoom_in->set_tooltip_text(TTRC("Zoom In"));
2580 split_sheet_zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_in));
2581 split_sheet_zoom_hb->add_child(split_sheet_zoom_in);
2582
2583 split_sheet_zoom_fit = memnew(Button);
2584 split_sheet_zoom_fit->set_theme_type_variation(SceneStringName(FlatButton));
2585 split_sheet_zoom_fit->set_focus_mode(FOCUS_ACCESSIBILITY);
2586 split_sheet_zoom_fit->set_tooltip_text(TTRC("Zoom to Fit"));
2587 split_sheet_zoom_fit->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_fit));
2588 split_sheet_zoom_hb->add_child(split_sheet_zoom_fit);
2589
2590 split_sheet_settings_vb = memnew(VBoxContainer);
2591 split_sheet_settings_vb->set_v_size_flags(SIZE_EXPAND_FILL);
2592
2593 HBoxContainer *split_sheet_h_hb = memnew(HBoxContainer);
2594 split_sheet_h_hb->set_h_size_flags(SIZE_EXPAND_FILL);
2595
2596 Label *split_sheet_h_label = memnew(Label(TTRC("Horizontal")));
2597 split_sheet_h_label->set_h_size_flags(SIZE_EXPAND_FILL);
2598 split_sheet_h_hb->add_child(split_sheet_h_label);
2599
2600 split_sheet_h = memnew(SpinBox);
2601 split_sheet_h->set_h_size_flags(SIZE_EXPAND_FILL);
2602 split_sheet_h->set_min(1);
2603 split_sheet_h->set_max(128);
2604 split_sheet_h->set_step(1);
2605 split_sheet_h->set_select_all_on_focus(true);
2606 split_sheet_h->set_accessibility_name(TTRC("Horizontal"));
2607 split_sheet_h_hb->add_child(split_sheet_h);
2608 split_sheet_h->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
2609 split_sheet_settings_vb->add_child(split_sheet_h_hb);
2610
2611 HBoxContainer *split_sheet_v_hb = memnew(HBoxContainer);
2612 split_sheet_v_hb->set_h_size_flags(SIZE_EXPAND_FILL);
2613
2614 Label *split_sheet_v_label = memnew(Label(TTRC("Vertical")));
2615 split_sheet_v_label->set_h_size_flags(SIZE_EXPAND_FILL);
2616 split_sheet_v_hb->add_child(split_sheet_v_label);
2617
2618 split_sheet_v = memnew(SpinBox);
2619 split_sheet_v->set_h_size_flags(SIZE_EXPAND_FILL);
2620 split_sheet_v->set_min(1);
2621 split_sheet_v->set_max(128);
2622 split_sheet_v->set_step(1);
2623 split_sheet_v->set_select_all_on_focus(true);
2624 split_sheet_v->set_accessibility_name(TTRC("Vertical"));
2625 split_sheet_v_hb->add_child(split_sheet_v);
2626 split_sheet_v->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
2627 split_sheet_settings_vb->add_child(split_sheet_v_hb);
2628
2629 HBoxContainer *split_sheet_size_hb = memnew(HBoxContainer);
2630 split_sheet_size_hb->set_h_size_flags(SIZE_EXPAND_FILL);
2631
2632 Label *split_sheet_size_label = memnew(Label(TTRC("Size")));
2633 split_sheet_size_label->set_h_size_flags(SIZE_EXPAND_FILL);
2634 split_sheet_size_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
2635 split_sheet_size_hb->add_child(split_sheet_size_label);
2636
2637 VBoxContainer *split_sheet_size_vb = memnew(VBoxContainer);
2638 split_sheet_size_vb->set_h_size_flags(SIZE_EXPAND_FILL);
2639 split_sheet_size_x = memnew(SpinBox);
2640 split_sheet_size_x->set_h_size_flags(SIZE_EXPAND_FILL);
2641 split_sheet_size_x->set_min(1);
2642 split_sheet_size_x->set_step(1);
2643 split_sheet_size_x->set_suffix("px");
2644 split_sheet_size_x->set_select_all_on_focus(true);
2645 split_sheet_size_x->set_accessibility_name(TTRC("X Size"));
2646 split_sheet_size_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
2647 split_sheet_size_vb->add_child(split_sheet_size_x);
2648 split_sheet_size_y = memnew(SpinBox);
2649 split_sheet_size_y->set_h_size_flags(SIZE_EXPAND_FILL);
2650 split_sheet_size_y->set_min(1);
2651 split_sheet_size_y->set_step(1);
2652 split_sheet_size_y->set_suffix("px");
2653 split_sheet_size_y->set_select_all_on_focus(true);
2654 split_sheet_size_y->set_accessibility_name(TTRC("Y Size"));
2655 split_sheet_size_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
2656 split_sheet_size_vb->add_child(split_sheet_size_y);
2657 split_sheet_size_hb->add_child(split_sheet_size_vb);
2658 split_sheet_settings_vb->add_child(split_sheet_size_hb);
2659
2660 HBoxContainer *split_sheet_sep_hb = memnew(HBoxContainer);
2661 split_sheet_sep_hb->set_h_size_flags(SIZE_EXPAND_FILL);
2662
2663 Label *split_sheet_sep_label = memnew(Label(TTRC("Separation")));
2664 split_sheet_sep_label->set_h_size_flags(SIZE_EXPAND_FILL);
2665 split_sheet_sep_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
2666 split_sheet_sep_hb->add_child(split_sheet_sep_label);
2667
2668 VBoxContainer *split_sheet_sep_vb = memnew(VBoxContainer);
2669 split_sheet_sep_vb->set_h_size_flags(SIZE_EXPAND_FILL);
2670 split_sheet_sep_x = memnew(SpinBox);
2671 split_sheet_sep_x->set_min(0);
2672 split_sheet_sep_x->set_step(1);
2673 split_sheet_sep_x->set_suffix("px");
2674 split_sheet_sep_x->set_select_all_on_focus(true);
2675 split_sheet_sep_x->set_accessibility_name(TTRC("X Separation"));
2676 split_sheet_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
2677 split_sheet_sep_vb->add_child(split_sheet_sep_x);
2678 split_sheet_sep_y = memnew(SpinBox);
2679 split_sheet_sep_y->set_min(0);
2680 split_sheet_sep_y->set_step(1);
2681 split_sheet_sep_y->set_suffix("px");
2682 split_sheet_sep_y->set_select_all_on_focus(true);
2683 split_sheet_sep_y->set_accessibility_name(TTRC("Y Separation"));
2684 split_sheet_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
2685 split_sheet_sep_vb->add_child(split_sheet_sep_y);
2686 split_sheet_sep_hb->add_child(split_sheet_sep_vb);
2687 split_sheet_settings_vb->add_child(split_sheet_sep_hb);
2688
2689 HBoxContainer *split_sheet_offset_hb = memnew(HBoxContainer);
2690 split_sheet_offset_hb->set_h_size_flags(SIZE_EXPAND_FILL);
2691
2692 Label *split_sheet_offset_label = memnew(Label(TTRC("Offset")));
2693 split_sheet_offset_label->set_h_size_flags(SIZE_EXPAND_FILL);
2694 split_sheet_offset_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
2695 split_sheet_offset_hb->add_child(split_sheet_offset_label);
2696
2697 VBoxContainer *split_sheet_offset_vb = memnew(VBoxContainer);
2698 split_sheet_offset_vb->set_h_size_flags(SIZE_EXPAND_FILL);
2699 split_sheet_offset_x = memnew(SpinBox);
2700 split_sheet_offset_x->set_min(0);
2701 split_sheet_offset_x->set_step(1);
2702 split_sheet_offset_x->set_suffix("px");
2703 split_sheet_offset_x->set_select_all_on_focus(true);
2704 split_sheet_offset_x->set_accessibility_name(TTRC("X Offset"));
2705 split_sheet_offset_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
2706 split_sheet_offset_vb->add_child(split_sheet_offset_x);
2707 split_sheet_offset_y = memnew(SpinBox);
2708 split_sheet_offset_y->set_min(0);
2709 split_sheet_offset_y->set_step(1);
2710 split_sheet_offset_y->set_suffix("px");
2711 split_sheet_offset_y->set_select_all_on_focus(true);
2712 split_sheet_offset_y->set_accessibility_name(TTRC("Y Offset"));
2713 split_sheet_offset_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
2714 split_sheet_offset_vb->add_child(split_sheet_offset_y);
2715 split_sheet_offset_hb->add_child(split_sheet_offset_vb);
2716 split_sheet_settings_vb->add_child(split_sheet_offset_hb);
2717
2718 Button *auto_slice = memnew(Button);
2719 auto_slice->set_text(TTRC("Auto Slice"));
2720 auto_slice->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_auto_slice_sprite_sheet));
2721 split_sheet_settings_vb->add_child(auto_slice);
2722
2723 split_sheet_hb->add_child(split_sheet_settings_vb);
2724
2725 file_split_sheet = memnew(EditorFileDialog);
2726 file_split_sheet->set_title(TTRC("Create Frames from Sprite Sheet"));
2727 file_split_sheet->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
2728 add_child(file_split_sheet);
2729 // Deferred so file dialog is hidden when sprite sheet dialog popups. Otherwise, after allowing
2730 // sprite sheet dialog to be maximized, it would complain about already having exclusive child window.
2731 file_split_sheet->connect("file_selected", callable_mp(this, &SpriteFramesEditor::_prepare_sprite_sheet), CONNECT_DEFERRED);
2732
2733 // Config scale.
2734 scale_ratio = 1.2f;
2735 thumbnail_default_size = 96 * MAX(1, EDSCALE);
2736 thumbnail_zoom = MAX(1.0f, EDSCALE);
2737 max_thumbnail_zoom = 8.0f * MAX(1.0f, EDSCALE);
2738 min_thumbnail_zoom = 0.1f * MAX(1.0f, EDSCALE);
2739 // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
2740 sheet_zoom = MAX(1.0f, EDSCALE);
2741 max_sheet_zoom = 128.0f * MAX(1.0f, EDSCALE);
2742 min_sheet_zoom = 0.01f * MAX(1.0f, EDSCALE);
2743 _zoom_reset();
2744
2745 // Ensure the anim search box is wide enough by default.
2746 // Not by setting its minimum size so it can still be shrunk if desired.
2747 main_split->set_split_offset(56 * EDSCALE);
2748 }
2749
2750 void SpriteFramesEditorPlugin::edit(Object *p_object) {
2751 Ref<SpriteFrames> s;
2752 AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object);
2753 if (animated_sprite) {
2754 s = animated_sprite->get_sprite_frames();
2755 } else {
2756 AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
2757 if (animated_sprite_3d) {
2758 s = animated_sprite_3d->get_sprite_frames();
2759 } else {
2760 s = p_object;
2761 }
2762 }
2763
2764 frames_editor->edit(s);
2765 }
2766
2767 bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
2768 AnimatedSprite2D *animated_sprite_2d = Object::cast_to<AnimatedSprite2D>(p_object);
2769 if (animated_sprite_2d && *animated_sprite_2d->get_sprite_frames()) {
2770 return true;
2771 }
2772 AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
2773 if (animated_sprite_3d && *animated_sprite_3d->get_sprite_frames()) {
2774 return true;
2775 }
2776 SpriteFrames *frames = Object::cast_to<SpriteFrames>(p_object);
2777 if (frames && (frames_editor->get_sprite_frames().is_null() || frames_editor->get_sprite_frames() == frames)) {
2778 return true;
2779 }
2780 return false;
2781 }
2782
2783 void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
2784 if (p_visible) {
2785 frames_editor->make_visible();
2786 } else {
2787 frames_editor->close();
2788 }
2789 }
2790
2791 SpriteFramesEditorPlugin::SpriteFramesEditorPlugin() {
2792 frames_editor = memnew(SpriteFramesEditor);
2793 frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
2794 EditorDockManager::get_singleton()->add_dock(frames_editor);
2795 frames_editor->close();
2796 }
2797
2798 Ref<ClipboardAnimation> ClipboardAnimation::from_sprite_frames(const Ref<SpriteFrames> &p_frames, const String &p_anim) {
2799 Ref<ClipboardAnimation> clipboard_anim;
2800 clipboard_anim.instantiate();
2801 clipboard_anim->name = p_anim;
2802 clipboard_anim->speed = p_frames->get_animation_speed(p_anim);
2803 clipboard_anim->loop = p_frames->get_animation_loop(p_anim);
2804
2805 int frame_count = p_frames->get_frame_count(p_anim);
2806 for (int i = 0; i < frame_count; ++i) {
2807 ClipboardSpriteFrames::Frame frame;
2808 frame.texture = p_frames->get_frame_texture(p_anim, i);
2809 frame.duration = p_frames->get_frame_duration(p_anim, i);
2810 clipboard_anim->frames.push_back(frame);
2811 }
2812 return clipboard_anim;
2813 }