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