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) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "sprite_frames_editor_plugin.h"
32
33 #include "core/io/resource_loader.h"
34 #include "core/project_settings.h"
35 #include "editor/editor_scale.h"
36 #include "editor/editor_settings.h"
37 #include "scene/3d/sprite_3d.h"
38 #include "scene/gui/center_container.h"
39
_gui_input(Ref<InputEvent> p_event)40 void SpriteFramesEditor::_gui_input(Ref<InputEvent> p_event) {
41 }
42
_open_sprite_sheet()43 void SpriteFramesEditor::_open_sprite_sheet() {
44
45 file_split_sheet->clear_filters();
46 List<String> extensions;
47 ResourceLoader::get_recognized_extensions_for_type("Texture", &extensions);
48 for (int i = 0; i < extensions.size(); i++) {
49 file_split_sheet->add_filter("*." + extensions[i]);
50 }
51
52 file_split_sheet->popup_centered_ratio();
53 }
54
_sheet_preview_draw()55 void SpriteFramesEditor::_sheet_preview_draw() {
56
57 Size2i size = split_sheet_preview->get_size();
58 int h = split_sheet_h->get_value();
59 int v = split_sheet_v->get_value();
60 int width = size.width / h;
61 int height = size.height / v;
62 const float a = 0.3;
63 for (int i = 1; i < h; i++) {
64
65 int x = i * width;
66 split_sheet_preview->draw_line(Point2(x, 0), Point2(x, size.height), Color(1, 1, 1, a));
67 split_sheet_preview->draw_line(Point2(x + 1, 0), Point2(x + 1, size.height), Color(0, 0, 0, a));
68
69 for (int j = 1; j < v; j++) {
70
71 int y = j * height;
72
73 split_sheet_preview->draw_line(Point2(0, y), Point2(size.width, y), Color(1, 1, 1, a));
74 split_sheet_preview->draw_line(Point2(0, y + 1), Point2(size.width, y + 1), Color(0, 0, 0, a));
75 }
76 }
77
78 if (frames_selected.size() == 0) {
79 split_sheet_dialog->get_ok()->set_disabled(true);
80 split_sheet_dialog->get_ok()->set_text(TTR("No Frames Selected"));
81 return;
82 }
83
84 Color accent = get_color("accent_color", "Editor");
85
86 for (Set<int>::Element *E = frames_selected.front(); E; E = E->next()) {
87 int idx = E->get();
88 int xp = idx % h;
89 int yp = (idx - xp) / h;
90 int x = xp * width;
91 int y = yp * height;
92
93 split_sheet_preview->draw_rect(Rect2(x + 5, y + 5, width - 10, height - 10), Color(0, 0, 0, 0.35), true);
94 split_sheet_preview->draw_rect(Rect2(x + 0, y + 0, width - 0, height - 0), Color(0, 0, 0, 1), false);
95 split_sheet_preview->draw_rect(Rect2(x + 1, y + 1, width - 2, height - 2), Color(0, 0, 0, 1), false);
96 split_sheet_preview->draw_rect(Rect2(x + 2, y + 2, width - 4, height - 4), accent, false);
97 split_sheet_preview->draw_rect(Rect2(x + 3, y + 3, width - 6, height - 6), accent, false);
98 split_sheet_preview->draw_rect(Rect2(x + 4, y + 4, width - 8, height - 8), Color(0, 0, 0, 1), false);
99 split_sheet_preview->draw_rect(Rect2(x + 5, y + 5, width - 10, height - 10), Color(0, 0, 0, 1), false);
100 }
101
102 split_sheet_dialog->get_ok()->set_disabled(false);
103 split_sheet_dialog->get_ok()->set_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size()));
104 }
_sheet_preview_input(const Ref<InputEvent> & p_event)105 void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
106
107 Ref<InputEventMouseButton> mb = p_event;
108
109 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
110 Size2i size = split_sheet_preview->get_size();
111 int h = split_sheet_h->get_value();
112 int v = split_sheet_v->get_value();
113
114 int x = CLAMP(int(mb->get_position().x) * h / size.width, 0, h - 1);
115 int y = CLAMP(int(mb->get_position().y) * v / size.height, 0, v - 1);
116
117 int idx = h * y + x;
118
119 if (mb->get_shift() && last_frame_selected >= 0) {
120 //select multiple
121 int from = idx;
122 int to = last_frame_selected;
123 if (from > to) {
124 SWAP(from, to);
125 }
126
127 for (int i = from; i <= to; i++) {
128 if (mb->get_control()) {
129 frames_selected.erase(i);
130 } else {
131 frames_selected.insert(i);
132 }
133 }
134 } else {
135 if (frames_selected.has(idx)) {
136 frames_selected.erase(idx);
137 } else {
138 frames_selected.insert(idx);
139 }
140 }
141
142 last_frame_selected = idx;
143 split_sheet_preview->update();
144 }
145 }
146
_sheet_add_frames()147 void SpriteFramesEditor::_sheet_add_frames() {
148
149 Size2i size = split_sheet_preview->get_size();
150 int h = split_sheet_h->get_value();
151 int v = split_sheet_v->get_value();
152
153 undo_redo->create_action(TTR("Add Frame"));
154
155 int fc = frames->get_frame_count(edited_anim);
156
157 AtlasTexture *atlas_source = Object::cast_to<AtlasTexture>(*split_sheet_preview->get_texture());
158
159 Rect2 region_rect = Rect2();
160
161 if (atlas_source && atlas_source->get_atlas().is_valid())
162 region_rect = atlas_source->get_region();
163
164 for (Set<int>::Element *E = frames_selected.front(); E; E = E->next()) {
165 int idx = E->get();
166 int width = size.width / h;
167 int height = size.height / v;
168 int xp = idx % h;
169 int yp = (idx - xp) / h;
170 int x = (xp * width) + region_rect.position.x;
171 int y = (yp * height) + region_rect.position.y;
172
173 Ref<AtlasTexture> at;
174 at.instance();
175 at->set_atlas(split_sheet_preview->get_texture());
176 at->set_region(Rect2(x, y, width, height));
177
178 undo_redo->add_do_method(frames, "add_frame", edited_anim, at, -1);
179 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, fc);
180 }
181
182 undo_redo->add_do_method(this, "_update_library");
183 undo_redo->add_undo_method(this, "_update_library");
184 undo_redo->commit_action();
185 }
186
_sheet_select_clear_all_frames()187 void SpriteFramesEditor::_sheet_select_clear_all_frames() {
188
189 bool should_clear = true;
190 for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
191 if (!frames_selected.has(i)) {
192 frames_selected.insert(i);
193 should_clear = false;
194 }
195 }
196 if (should_clear) {
197 frames_selected.clear();
198 }
199
200 split_sheet_preview->update();
201 }
202
_sheet_spin_changed(double)203 void SpriteFramesEditor::_sheet_spin_changed(double) {
204
205 frames_selected.clear();
206 last_frame_selected = -1;
207 split_sheet_preview->update();
208 }
209
_prepare_sprite_sheet(const String & p_file)210 void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
211
212 Ref<Resource> texture = ResourceLoader::load(p_file);
213 if (!texture.is_valid()) {
214 EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
215 ERR_FAIL_COND(!texture.is_valid());
216 }
217 if (texture != split_sheet_preview->get_texture()) {
218 //different texture, reset to 4x4
219 split_sheet_h->set_value(4);
220 split_sheet_v->set_value(4);
221 }
222 frames_selected.clear();
223 last_frame_selected = -1;
224
225 split_sheet_preview->set_texture(texture);
226 split_sheet_dialog->popup_centered_ratio(0.65);
227 }
228
_notification(int p_what)229 void SpriteFramesEditor::_notification(int p_what) {
230
231 switch (p_what) {
232 case NOTIFICATION_ENTER_TREE: {
233 load->set_icon(get_icon("Load", "EditorIcons"));
234 load_sheet->set_icon(get_icon("SpriteSheet", "EditorIcons"));
235 copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
236 paste->set_icon(get_icon("ActionPaste", "EditorIcons"));
237 empty->set_icon(get_icon("InsertBefore", "EditorIcons"));
238 empty2->set_icon(get_icon("InsertAfter", "EditorIcons"));
239 move_up->set_icon(get_icon("MoveLeft", "EditorIcons"));
240 move_down->set_icon(get_icon("MoveRight", "EditorIcons"));
241 _delete->set_icon(get_icon("Remove", "EditorIcons"));
242 new_anim->set_icon(get_icon("New", "EditorIcons"));
243 remove_anim->set_icon(get_icon("Remove", "EditorIcons"));
244 FALLTHROUGH;
245 }
246 case NOTIFICATION_THEME_CHANGED: {
247 splite_sheet_scroll->add_style_override("bg", get_stylebox("bg", "Tree"));
248 } break;
249 case NOTIFICATION_READY: {
250 add_constant_override("autohide", 1); // Fixes the dragger always showing up.
251 } break;
252 }
253 }
254
_file_load_request(const PoolVector<String> & p_path,int p_at_pos)255 void SpriteFramesEditor::_file_load_request(const PoolVector<String> &p_path, int p_at_pos) {
256
257 ERR_FAIL_COND(!frames->has_animation(edited_anim));
258
259 List<Ref<Texture> > resources;
260
261 for (int i = 0; i < p_path.size(); i++) {
262
263 Ref<Texture> resource;
264 resource = ResourceLoader::load(p_path[i]);
265
266 if (resource.is_null()) {
267 dialog->set_text(TTR("ERROR: Couldn't load frame resource!"));
268 dialog->set_title(TTR("Error!"));
269
270 //dialog->get_cancel()->set_text("Close");
271 dialog->get_ok()->set_text(TTR("Close"));
272 dialog->popup_centered_minsize();
273 return; ///beh should show an error i guess
274 }
275
276 resources.push_back(resource);
277 }
278
279 if (resources.empty()) {
280 return;
281 }
282
283 undo_redo->create_action(TTR("Add Frame"));
284 int fc = frames->get_frame_count(edited_anim);
285
286 int count = 0;
287
288 for (List<Ref<Texture> >::Element *E = resources.front(); E; E = E->next()) {
289
290 undo_redo->add_do_method(frames, "add_frame", edited_anim, E->get(), p_at_pos == -1 ? -1 : p_at_pos + count);
291 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
292 count++;
293 }
294 undo_redo->add_do_method(this, "_update_library");
295 undo_redo->add_undo_method(this, "_update_library");
296
297 undo_redo->commit_action();
298 }
299
_load_pressed()300 void SpriteFramesEditor::_load_pressed() {
301
302 ERR_FAIL_COND(!frames->has_animation(edited_anim));
303 loading_scene = false;
304
305 file->clear_filters();
306 List<String> extensions;
307 ResourceLoader::get_recognized_extensions_for_type("Texture", &extensions);
308 for (int i = 0; i < extensions.size(); i++)
309 file->add_filter("*." + extensions[i]);
310
311 file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
312
313 file->popup_centered_ratio();
314 }
315
_paste_pressed()316 void SpriteFramesEditor::_paste_pressed() {
317
318 ERR_FAIL_COND(!frames->has_animation(edited_anim));
319
320 Ref<Texture> r = EditorSettings::get_singleton()->get_resource_clipboard();
321 if (!r.is_valid()) {
322 dialog->set_text(TTR("Resource clipboard is empty or not a texture!"));
323 dialog->set_title(TTR("Error!"));
324 //dialog->get_cancel()->set_text("Close");
325 dialog->get_ok()->set_text(TTR("Close"));
326 dialog->popup_centered_minsize();
327 return; ///beh should show an error i guess
328 }
329
330 undo_redo->create_action(TTR("Paste Frame"));
331 undo_redo->add_do_method(frames, "add_frame", edited_anim, r);
332 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, frames->get_frame_count(edited_anim));
333 undo_redo->add_do_method(this, "_update_library");
334 undo_redo->add_undo_method(this, "_update_library");
335 undo_redo->commit_action();
336 }
337
_copy_pressed()338 void SpriteFramesEditor::_copy_pressed() {
339 ERR_FAIL_COND(!frames->has_animation(edited_anim));
340
341 if (tree->get_current() < 0)
342 return;
343 Ref<Texture> r = frames->get_frame(edited_anim, tree->get_current());
344 if (!r.is_valid()) {
345 return;
346 }
347
348 EditorSettings::get_singleton()->set_resource_clipboard(r);
349 }
350
_empty_pressed()351 void SpriteFramesEditor::_empty_pressed() {
352
353 ERR_FAIL_COND(!frames->has_animation(edited_anim));
354
355 int from = -1;
356
357 if (tree->get_current() >= 0) {
358
359 from = tree->get_current();
360 sel = from;
361
362 } else {
363 from = frames->get_frame_count(edited_anim);
364 }
365
366 Ref<Texture> r;
367
368 undo_redo->create_action(TTR("Add Empty"));
369 undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from);
370 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from);
371 undo_redo->add_do_method(this, "_update_library");
372 undo_redo->add_undo_method(this, "_update_library");
373 undo_redo->commit_action();
374 }
375
_empty2_pressed()376 void SpriteFramesEditor::_empty2_pressed() {
377
378 ERR_FAIL_COND(!frames->has_animation(edited_anim));
379
380 int from = -1;
381
382 if (tree->get_current() >= 0) {
383
384 from = tree->get_current();
385 sel = from;
386
387 } else {
388 from = frames->get_frame_count(edited_anim);
389 }
390
391 Ref<Texture> r;
392
393 undo_redo->create_action(TTR("Add Empty"));
394 undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from + 1);
395 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from + 1);
396 undo_redo->add_do_method(this, "_update_library");
397 undo_redo->add_undo_method(this, "_update_library");
398 undo_redo->commit_action();
399 }
400
_up_pressed()401 void SpriteFramesEditor::_up_pressed() {
402
403 ERR_FAIL_COND(!frames->has_animation(edited_anim));
404
405 if (tree->get_current() < 0)
406 return;
407
408 int to_move = tree->get_current();
409 if (to_move < 1)
410 return;
411
412 sel = to_move;
413 sel -= 1;
414
415 undo_redo->create_action(TTR("Delete Resource"));
416 undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move - 1));
417 undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move));
418 undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
419 undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move - 1));
420 undo_redo->add_do_method(this, "_update_library");
421 undo_redo->add_undo_method(this, "_update_library");
422 undo_redo->commit_action();
423 }
424
_down_pressed()425 void SpriteFramesEditor::_down_pressed() {
426
427 ERR_FAIL_COND(!frames->has_animation(edited_anim));
428
429 if (tree->get_current() < 0)
430 return;
431
432 int to_move = tree->get_current();
433 if (to_move < 0 || to_move >= frames->get_frame_count(edited_anim) - 1)
434 return;
435
436 sel = to_move;
437 sel += 1;
438
439 undo_redo->create_action(TTR("Delete Resource"));
440 undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move + 1));
441 undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move));
442 undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
443 undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move + 1));
444 undo_redo->add_do_method(this, "_update_library");
445 undo_redo->add_undo_method(this, "_update_library");
446 undo_redo->commit_action();
447 }
448
_delete_pressed()449 void SpriteFramesEditor::_delete_pressed() {
450
451 ERR_FAIL_COND(!frames->has_animation(edited_anim));
452
453 if (tree->get_current() < 0)
454 return;
455
456 int to_delete = tree->get_current();
457 if (to_delete < 0 || to_delete >= frames->get_frame_count(edited_anim)) {
458 return;
459 }
460
461 undo_redo->create_action(TTR("Delete Resource"));
462 undo_redo->add_do_method(frames, "remove_frame", edited_anim, to_delete);
463 undo_redo->add_undo_method(frames, "add_frame", edited_anim, frames->get_frame(edited_anim, to_delete), to_delete);
464 undo_redo->add_do_method(this, "_update_library");
465 undo_redo->add_undo_method(this, "_update_library");
466 undo_redo->commit_action();
467 }
468
_animation_select()469 void SpriteFramesEditor::_animation_select() {
470
471 if (updating)
472 return;
473
474 if (frames->has_animation(edited_anim)) {
475 double value = anim_speed->get_line_edit()->get_text().to_double();
476 if (!Math::is_equal_approx(value, frames->get_animation_speed(edited_anim)))
477 _animation_fps_changed(value);
478 }
479
480 TreeItem *selected = animations->get_selected();
481 ERR_FAIL_COND(!selected);
482 edited_anim = selected->get_text(0);
483 _update_library(true);
484 }
485
_find_anim_sprites(Node * p_node,List<Node * > * r_nodes,Ref<SpriteFrames> p_sfames)486 static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
487
488 Node *edited = EditorNode::get_singleton()->get_edited_scene();
489 if (!edited)
490 return;
491 if (p_node != edited && p_node->get_owner() != edited)
492 return;
493
494 {
495 AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);
496 if (as && as->get_sprite_frames() == p_sfames) {
497 r_nodes->push_back(p_node);
498 }
499 }
500
501 {
502 AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
503 if (as && as->get_sprite_frames() == p_sfames) {
504 r_nodes->push_back(p_node);
505 }
506 }
507
508 for (int i = 0; i < p_node->get_child_count(); i++) {
509 _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
510 }
511 }
512
_animation_name_edited()513 void SpriteFramesEditor::_animation_name_edited() {
514
515 if (updating)
516 return;
517
518 if (!frames->has_animation(edited_anim))
519 return;
520
521 TreeItem *edited = animations->get_edited();
522 if (!edited)
523 return;
524
525 String new_name = edited->get_text(0);
526
527 if (new_name == String(edited_anim))
528 return;
529
530 new_name = new_name.replace("/", "_").replace(",", " ");
531
532 String name = new_name;
533 int counter = 0;
534 while (frames->has_animation(name)) {
535 counter++;
536 name = new_name + " " + itos(counter);
537 }
538
539 List<Node *> nodes;
540 _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
541
542 undo_redo->create_action(TTR("Rename Animation"));
543 undo_redo->add_do_method(frames, "rename_animation", edited_anim, name);
544 undo_redo->add_undo_method(frames, "rename_animation", name, edited_anim);
545
546 for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
547
548 String current = E->get()->call("get_animation");
549 undo_redo->add_do_method(E->get(), "set_animation", name);
550 undo_redo->add_undo_method(E->get(), "set_animation", edited_anim);
551 }
552
553 undo_redo->add_do_method(this, "_update_library");
554 undo_redo->add_undo_method(this, "_update_library");
555
556 edited_anim = new_name;
557
558 undo_redo->commit_action();
559 }
560
_animation_add()561 void SpriteFramesEditor::_animation_add() {
562
563 String name = "New Anim";
564 int counter = 0;
565 while (frames->has_animation(name)) {
566 counter++;
567 name = "New Anim " + itos(counter);
568 }
569
570 List<Node *> nodes;
571 _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
572
573 undo_redo->create_action(TTR("Add Animation"));
574 undo_redo->add_do_method(frames, "add_animation", name);
575 undo_redo->add_undo_method(frames, "remove_animation", name);
576 undo_redo->add_do_method(this, "_update_library");
577 undo_redo->add_undo_method(this, "_update_library");
578
579 for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
580
581 String current = E->get()->call("get_animation");
582 undo_redo->add_do_method(E->get(), "set_animation", name);
583 undo_redo->add_undo_method(E->get(), "set_animation", current);
584 }
585
586 edited_anim = name;
587
588 undo_redo->commit_action();
589 animations->grab_focus();
590 }
591
_animation_remove()592 void SpriteFramesEditor::_animation_remove() {
593
594 if (updating)
595 return;
596
597 if (!frames->has_animation(edited_anim))
598 return;
599
600 delete_dialog->set_text(TTR("Delete Animation?"));
601 delete_dialog->popup_centered_minsize();
602 }
603
_animation_remove_confirmed()604 void SpriteFramesEditor::_animation_remove_confirmed() {
605
606 undo_redo->create_action(TTR("Remove Animation"));
607 undo_redo->add_do_method(frames, "remove_animation", edited_anim);
608 undo_redo->add_undo_method(frames, "add_animation", edited_anim);
609 undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
610 undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
611 int fc = frames->get_frame_count(edited_anim);
612 for (int i = 0; i < fc; i++) {
613 Ref<Texture> frame = frames->get_frame(edited_anim, i);
614 undo_redo->add_undo_method(frames, "add_frame", edited_anim, frame);
615 }
616 undo_redo->add_do_method(this, "_update_library");
617 undo_redo->add_undo_method(this, "_update_library");
618
619 edited_anim = StringName();
620
621 undo_redo->commit_action();
622 }
623
_animation_loop_changed()624 void SpriteFramesEditor::_animation_loop_changed() {
625
626 if (updating)
627 return;
628
629 undo_redo->create_action(TTR("Change Animation Loop"));
630 undo_redo->add_do_method(frames, "set_animation_loop", edited_anim, anim_loop->is_pressed());
631 undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
632 undo_redo->add_do_method(this, "_update_library", true);
633 undo_redo->add_undo_method(this, "_update_library", true);
634 undo_redo->commit_action();
635 }
636
_animation_fps_changed(double p_value)637 void SpriteFramesEditor::_animation_fps_changed(double p_value) {
638
639 if (updating)
640 return;
641
642 undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS);
643 undo_redo->add_do_method(frames, "set_animation_speed", edited_anim, p_value);
644 undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
645 undo_redo->add_do_method(this, "_update_library", true);
646 undo_redo->add_undo_method(this, "_update_library", true);
647
648 undo_redo->commit_action();
649 }
650
_update_library(bool p_skip_selector)651 void SpriteFramesEditor::_update_library(bool p_skip_selector) {
652
653 updating = true;
654
655 if (!p_skip_selector) {
656 animations->clear();
657
658 TreeItem *anim_root = animations->create_item();
659
660 List<StringName> anim_names;
661
662 frames->get_animation_list(&anim_names);
663
664 anim_names.sort_custom<StringName::AlphCompare>();
665
666 for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
667
668 String name = E->get();
669
670 TreeItem *it = animations->create_item(anim_root);
671
672 it->set_metadata(0, name);
673
674 it->set_text(0, name);
675 it->set_editable(0, true);
676
677 if (E->get() == edited_anim) {
678 it->select(0);
679 }
680 }
681 }
682
683 tree->clear();
684
685 if (!frames->has_animation(edited_anim)) {
686 updating = false;
687 return;
688 }
689
690 if (sel >= frames->get_frame_count(edited_anim))
691 sel = frames->get_frame_count(edited_anim) - 1;
692 else if (sel < 0 && frames->get_frame_count(edited_anim))
693 sel = 0;
694
695 for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
696
697 String name;
698 Ref<Texture> icon;
699
700 if (frames->get_frame(edited_anim, i).is_null()) {
701
702 name = itos(i) + ": " + TTR("(empty)");
703
704 } else {
705 name = itos(i) + ": " + frames->get_frame(edited_anim, i)->get_name();
706 icon = frames->get_frame(edited_anim, i);
707 }
708
709 tree->add_item(name, icon);
710 if (frames->get_frame(edited_anim, i).is_valid())
711 tree->set_item_tooltip(tree->get_item_count() - 1, frames->get_frame(edited_anim, i)->get_path());
712 if (sel == i)
713 tree->select(tree->get_item_count() - 1);
714 }
715
716 anim_speed->set_value(frames->get_animation_speed(edited_anim));
717 anim_loop->set_pressed(frames->get_animation_loop(edited_anim));
718
719 updating = false;
720 //player->add_resource("default",resource);
721 }
722
edit(SpriteFrames * p_frames)723 void SpriteFramesEditor::edit(SpriteFrames *p_frames) {
724
725 if (frames == p_frames)
726 return;
727
728 frames = p_frames;
729
730 if (p_frames) {
731
732 if (!p_frames->has_animation(edited_anim)) {
733
734 List<StringName> anim_names;
735 frames->get_animation_list(&anim_names);
736 anim_names.sort_custom<StringName::AlphCompare>();
737 if (anim_names.size()) {
738 edited_anim = anim_names.front()->get();
739 } else {
740 edited_anim = StringName();
741 }
742 }
743
744 _update_library();
745 } else {
746
747 hide();
748 }
749 }
750
get_drag_data_fw(const Point2 & p_point,Control * p_from)751 Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
752
753 if (!frames->has_animation(edited_anim))
754 return false;
755
756 int idx = tree->get_item_at_position(p_point, true);
757
758 if (idx < 0 || idx >= frames->get_frame_count(edited_anim))
759 return Variant();
760
761 RES frame = frames->get_frame(edited_anim, idx);
762
763 if (frame.is_null())
764 return Variant();
765
766 Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
767 drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
768 return drag_data;
769 }
770
can_drop_data_fw(const Point2 & p_point,const Variant & p_data,Control * p_from) const771 bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
772
773 Dictionary d = p_data;
774
775 if (!d.has("type"))
776 return false;
777
778 // reordering frames
779 if (d.has("from") && (Object *)(d["from"]) == tree)
780 return true;
781
782 if (String(d["type"]) == "resource" && d.has("resource")) {
783 RES r = d["resource"];
784
785 Ref<Texture> texture = r;
786
787 if (texture.is_valid()) {
788
789 return true;
790 }
791 }
792
793 if (String(d["type"]) == "files") {
794
795 Vector<String> files = d["files"];
796
797 if (files.size() == 0)
798 return false;
799
800 for (int i = 0; i < files.size(); i++) {
801 String file = files[i];
802 String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
803
804 if (!ClassDB::is_parent_class(ftype, "Texture")) {
805 return false;
806 }
807 }
808
809 return true;
810 }
811 return false;
812 }
813
drop_data_fw(const Point2 & p_point,const Variant & p_data,Control * p_from)814 void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
815
816 if (!can_drop_data_fw(p_point, p_data, p_from))
817 return;
818
819 Dictionary d = p_data;
820
821 if (!d.has("type"))
822 return;
823
824 int at_pos = tree->get_item_at_position(p_point, true);
825
826 if (String(d["type"]) == "resource" && d.has("resource")) {
827 RES r = d["resource"];
828
829 Ref<Texture> texture = r;
830
831 if (texture.is_valid()) {
832 bool reorder = false;
833 if (d.has("from") && (Object *)(d["from"]) == tree)
834 reorder = true;
835
836 if (reorder) { //drop is from reordering frames
837 int from_frame = -1;
838 if (d.has("frame"))
839 from_frame = d["frame"];
840
841 undo_redo->create_action(TTR("Move Frame"));
842 undo_redo->add_do_method(frames, "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
843 undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
844 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
845 undo_redo->add_undo_method(frames, "add_frame", edited_anim, texture, from_frame);
846 undo_redo->add_do_method(this, "_update_library");
847 undo_redo->add_undo_method(this, "_update_library");
848 undo_redo->commit_action();
849 } else {
850 undo_redo->create_action(TTR("Add Frame"));
851 undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
852 undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
853 undo_redo->add_do_method(this, "_update_library");
854 undo_redo->add_undo_method(this, "_update_library");
855 undo_redo->commit_action();
856 }
857 }
858 }
859
860 if (String(d["type"]) == "files") {
861
862 PoolVector<String> files = d["files"];
863
864 _file_load_request(files, at_pos);
865 }
866 }
867
_bind_methods()868 void SpriteFramesEditor::_bind_methods() {
869
870 ClassDB::bind_method(D_METHOD("_load_pressed"), &SpriteFramesEditor::_load_pressed);
871 ClassDB::bind_method(D_METHOD("_empty_pressed"), &SpriteFramesEditor::_empty_pressed);
872 ClassDB::bind_method(D_METHOD("_empty2_pressed"), &SpriteFramesEditor::_empty2_pressed);
873 ClassDB::bind_method(D_METHOD("_delete_pressed"), &SpriteFramesEditor::_delete_pressed);
874 ClassDB::bind_method(D_METHOD("_copy_pressed"), &SpriteFramesEditor::_copy_pressed);
875 ClassDB::bind_method(D_METHOD("_paste_pressed"), &SpriteFramesEditor::_paste_pressed);
876 ClassDB::bind_method(D_METHOD("_file_load_request", "files", "at_position"), &SpriteFramesEditor::_file_load_request, DEFVAL(-1));
877 ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
878 ClassDB::bind_method(D_METHOD("_up_pressed"), &SpriteFramesEditor::_up_pressed);
879 ClassDB::bind_method(D_METHOD("_down_pressed"), &SpriteFramesEditor::_down_pressed);
880 ClassDB::bind_method(D_METHOD("_animation_select"), &SpriteFramesEditor::_animation_select);
881 ClassDB::bind_method(D_METHOD("_animation_name_edited"), &SpriteFramesEditor::_animation_name_edited);
882 ClassDB::bind_method(D_METHOD("_animation_add"), &SpriteFramesEditor::_animation_add);
883 ClassDB::bind_method(D_METHOD("_animation_remove"), &SpriteFramesEditor::_animation_remove);
884 ClassDB::bind_method(D_METHOD("_animation_remove_confirmed"), &SpriteFramesEditor::_animation_remove_confirmed);
885 ClassDB::bind_method(D_METHOD("_animation_loop_changed"), &SpriteFramesEditor::_animation_loop_changed);
886 ClassDB::bind_method(D_METHOD("_animation_fps_changed"), &SpriteFramesEditor::_animation_fps_changed);
887 ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SpriteFramesEditor::get_drag_data_fw);
888 ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpriteFramesEditor::can_drop_data_fw);
889 ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpriteFramesEditor::drop_data_fw);
890 ClassDB::bind_method(D_METHOD("_prepare_sprite_sheet"), &SpriteFramesEditor::_prepare_sprite_sheet);
891 ClassDB::bind_method(D_METHOD("_open_sprite_sheet"), &SpriteFramesEditor::_open_sprite_sheet);
892 ClassDB::bind_method(D_METHOD("_sheet_preview_draw"), &SpriteFramesEditor::_sheet_preview_draw);
893 ClassDB::bind_method(D_METHOD("_sheet_preview_input"), &SpriteFramesEditor::_sheet_preview_input);
894 ClassDB::bind_method(D_METHOD("_sheet_spin_changed"), &SpriteFramesEditor::_sheet_spin_changed);
895 ClassDB::bind_method(D_METHOD("_sheet_add_frames"), &SpriteFramesEditor::_sheet_add_frames);
896 ClassDB::bind_method(D_METHOD("_sheet_select_clear_all_frames"), &SpriteFramesEditor::_sheet_select_clear_all_frames);
897 }
898
SpriteFramesEditor()899 SpriteFramesEditor::SpriteFramesEditor() {
900
901 VBoxContainer *vbc_animlist = memnew(VBoxContainer);
902 add_child(vbc_animlist);
903 vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
904
905 VBoxContainer *sub_vb = memnew(VBoxContainer);
906 vbc_animlist->add_margin_child(TTR("Animations:"), sub_vb, true);
907 sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
908
909 HBoxContainer *hbc_animlist = memnew(HBoxContainer);
910 sub_vb->add_child(hbc_animlist);
911
912 new_anim = memnew(ToolButton);
913 new_anim->set_tooltip(TTR("New Animation"));
914 hbc_animlist->add_child(new_anim);
915 new_anim->connect("pressed", this, "_animation_add");
916
917 remove_anim = memnew(ToolButton);
918 remove_anim->set_tooltip(TTR("Remove Animation"));
919 hbc_animlist->add_child(remove_anim);
920 remove_anim->connect("pressed", this, "_animation_remove");
921
922 animations = memnew(Tree);
923 sub_vb->add_child(animations);
924 animations->set_v_size_flags(SIZE_EXPAND_FILL);
925 animations->set_hide_root(true);
926 animations->connect("cell_selected", this, "_animation_select");
927 animations->connect("item_edited", this, "_animation_name_edited");
928 animations->set_allow_reselect(true);
929
930 anim_speed = memnew(SpinBox);
931 vbc_animlist->add_margin_child(TTR("Speed (FPS):"), anim_speed);
932 anim_speed->set_min(0);
933 anim_speed->set_max(100);
934 anim_speed->set_step(0.01);
935 anim_speed->connect("value_changed", this, "_animation_fps_changed");
936
937 anim_loop = memnew(CheckButton);
938 anim_loop->set_text(TTR("Loop"));
939 vbc_animlist->add_child(anim_loop);
940 anim_loop->connect("pressed", this, "_animation_loop_changed");
941
942 VBoxContainer *vbc = memnew(VBoxContainer);
943 add_child(vbc);
944 vbc->set_h_size_flags(SIZE_EXPAND_FILL);
945
946 sub_vb = memnew(VBoxContainer);
947 vbc->add_margin_child(TTR("Animation Frames:"), sub_vb, true);
948
949 HBoxContainer *hbc = memnew(HBoxContainer);
950 sub_vb->add_child(hbc);
951
952 load = memnew(ToolButton);
953 load->set_tooltip(TTR("Add a Texture from File"));
954 hbc->add_child(load);
955
956 load_sheet = memnew(ToolButton);
957 load_sheet->set_tooltip(TTR("Add Frames from a Sprite Sheet"));
958 hbc->add_child(load_sheet);
959
960 hbc->add_child(memnew(VSeparator));
961
962 copy = memnew(ToolButton);
963 copy->set_tooltip(TTR("Copy"));
964 hbc->add_child(copy);
965
966 paste = memnew(ToolButton);
967 paste->set_tooltip(TTR("Paste"));
968 hbc->add_child(paste);
969
970 hbc->add_child(memnew(VSeparator));
971
972 empty = memnew(ToolButton);
973 empty->set_tooltip(TTR("Insert Empty (Before)"));
974 hbc->add_child(empty);
975
976 empty2 = memnew(ToolButton);
977 empty2->set_tooltip(TTR("Insert Empty (After)"));
978 hbc->add_child(empty2);
979
980 hbc->add_child(memnew(VSeparator));
981
982 move_up = memnew(ToolButton);
983 move_up->set_tooltip(TTR("Move (Before)"));
984 hbc->add_child(move_up);
985
986 move_down = memnew(ToolButton);
987 move_down->set_tooltip(TTR("Move (After)"));
988 hbc->add_child(move_down);
989
990 _delete = memnew(ToolButton);
991 _delete->set_tooltip(TTR("Delete"));
992 hbc->add_child(_delete);
993
994 file = memnew(EditorFileDialog);
995 add_child(file);
996
997 tree = memnew(ItemList);
998 tree->set_v_size_flags(SIZE_EXPAND_FILL);
999 tree->set_icon_mode(ItemList::ICON_MODE_TOP);
1000
1001 int thumbnail_size = 96;
1002 tree->set_max_columns(0);
1003 tree->set_icon_mode(ItemList::ICON_MODE_TOP);
1004 tree->set_fixed_column_width(thumbnail_size * 3 / 2);
1005 tree->set_max_text_lines(2);
1006 tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
1007 tree->set_drag_forwarding(this);
1008
1009 sub_vb->add_child(tree);
1010
1011 dialog = memnew(AcceptDialog);
1012 add_child(dialog);
1013
1014 load->connect("pressed", this, "_load_pressed");
1015 load_sheet->connect("pressed", this, "_open_sprite_sheet");
1016 _delete->connect("pressed", this, "_delete_pressed");
1017 copy->connect("pressed", this, "_copy_pressed");
1018 paste->connect("pressed", this, "_paste_pressed");
1019 empty->connect("pressed", this, "_empty_pressed");
1020 empty2->connect("pressed", this, "_empty2_pressed");
1021 move_up->connect("pressed", this, "_up_pressed");
1022 move_down->connect("pressed", this, "_down_pressed");
1023 file->connect("files_selected", this, "_file_load_request");
1024 loading_scene = false;
1025 sel = -1;
1026
1027 updating = false;
1028
1029 edited_anim = "default";
1030
1031 delete_dialog = memnew(ConfirmationDialog);
1032 add_child(delete_dialog);
1033 delete_dialog->connect("confirmed", this, "_animation_remove_confirmed");
1034
1035 split_sheet_dialog = memnew(ConfirmationDialog);
1036 add_child(split_sheet_dialog);
1037 VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
1038 split_sheet_dialog->add_child(split_sheet_vb);
1039 split_sheet_dialog->set_title(TTR("Select Frames"));
1040 split_sheet_dialog->connect("confirmed", this, "_sheet_add_frames");
1041
1042 HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
1043
1044 Label *ss_label = memnew(Label(TTR("Horizontal:")));
1045 split_sheet_hb->add_child(ss_label);
1046 split_sheet_h = memnew(SpinBox);
1047 split_sheet_h->set_min(1);
1048 split_sheet_h->set_max(128);
1049 split_sheet_h->set_step(1);
1050 split_sheet_hb->add_child(split_sheet_h);
1051 split_sheet_h->connect("value_changed", this, "_sheet_spin_changed");
1052
1053 ss_label = memnew(Label(TTR("Vertical:")));
1054 split_sheet_hb->add_child(ss_label);
1055 split_sheet_v = memnew(SpinBox);
1056 split_sheet_v->set_min(1);
1057 split_sheet_v->set_max(128);
1058 split_sheet_v->set_step(1);
1059 split_sheet_hb->add_child(split_sheet_v);
1060 split_sheet_v->connect("value_changed", this, "_sheet_spin_changed");
1061
1062 split_sheet_hb->add_spacer();
1063
1064 Button *select_clear_all = memnew(Button);
1065 select_clear_all->set_text(TTR("Select/Clear All Frames"));
1066 select_clear_all->connect("pressed", this, "_sheet_select_clear_all_frames");
1067 split_sheet_hb->add_child(select_clear_all);
1068
1069 split_sheet_vb->add_child(split_sheet_hb);
1070
1071 split_sheet_preview = memnew(TextureRect);
1072 split_sheet_preview->set_expand(false);
1073 split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
1074 split_sheet_preview->connect("draw", this, "_sheet_preview_draw");
1075 split_sheet_preview->connect("gui_input", this, "_sheet_preview_input");
1076
1077 splite_sheet_scroll = memnew(ScrollContainer);
1078 splite_sheet_scroll->set_enable_h_scroll(true);
1079 splite_sheet_scroll->set_enable_v_scroll(true);
1080 splite_sheet_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
1081 CenterContainer *cc = memnew(CenterContainer);
1082 cc->add_child(split_sheet_preview);
1083 cc->set_h_size_flags(SIZE_EXPAND_FILL);
1084 cc->set_v_size_flags(SIZE_EXPAND_FILL);
1085 splite_sheet_scroll->add_child(cc);
1086
1087 split_sheet_vb->add_child(splite_sheet_scroll);
1088
1089 file_split_sheet = memnew(EditorFileDialog);
1090 file_split_sheet->set_title(TTR("Create Frames from Sprite Sheet"));
1091 file_split_sheet->set_mode(EditorFileDialog::MODE_OPEN_FILE);
1092 add_child(file_split_sheet);
1093 file_split_sheet->connect("file_selected", this, "_prepare_sprite_sheet");
1094 }
1095
edit(Object * p_object)1096 void SpriteFramesEditorPlugin::edit(Object *p_object) {
1097
1098 frames_editor->set_undo_redo(&get_undo_redo());
1099
1100 SpriteFrames *s;
1101 AnimatedSprite *animated_sprite = Object::cast_to<AnimatedSprite>(p_object);
1102 if (animated_sprite) {
1103 s = *animated_sprite->get_sprite_frames();
1104 } else {
1105 s = Object::cast_to<SpriteFrames>(p_object);
1106 }
1107
1108 frames_editor->edit(s);
1109 }
1110
handles(Object * p_object) const1111 bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
1112
1113 AnimatedSprite *animated_sprite = Object::cast_to<AnimatedSprite>(p_object);
1114 if (animated_sprite && *animated_sprite->get_sprite_frames()) {
1115 return true;
1116 } else {
1117 return p_object->is_class("SpriteFrames");
1118 }
1119 }
1120
make_visible(bool p_visible)1121 void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
1122
1123 if (p_visible) {
1124 button->show();
1125 editor->make_bottom_panel_item_visible(frames_editor);
1126 } else {
1127
1128 button->hide();
1129 if (frames_editor->is_visible_in_tree())
1130 editor->hide_bottom_panel();
1131 }
1132 }
1133
SpriteFramesEditorPlugin(EditorNode * p_node)1134 SpriteFramesEditorPlugin::SpriteFramesEditorPlugin(EditorNode *p_node) {
1135
1136 editor = p_node;
1137 frames_editor = memnew(SpriteFramesEditor);
1138 frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
1139 button = editor->add_bottom_panel_item(TTR("SpriteFrames"), frames_editor);
1140 button->hide();
1141 }
1142
~SpriteFramesEditorPlugin()1143 SpriteFramesEditorPlugin::~SpriteFramesEditorPlugin() {
1144 }
1145