sprite_frames_editor_plugin.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. #include "sprite_frames_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/project_settings.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/3d/sprite_3d.h"
  36. #include "scene/gui/center_container.h"
  37. void SpriteFramesEditor::_gui_input(Ref<InputEvent> p_event) {
  38. }
  39. void SpriteFramesEditor::_open_sprite_sheet() {
  40. file_split_sheet->clear_filters();
  41. List<String> extensions;
  42. ResourceLoader::get_recognized_extensions_for_type("Texture", &extensions);
  43. for (int i = 0; i < extensions.size(); i++) {
  44. file_split_sheet->add_filter("*." + extensions[i]);
  45. }
  46. file_split_sheet->popup_centered_ratio();
  47. }
  48. void SpriteFramesEditor::_sheet_preview_draw() {
  49. Size2i size = split_sheet_preview->get_size();
  50. int h = split_sheet_h->get_value();
  51. int v = split_sheet_v->get_value();
  52. int width = size.width / h;
  53. int height = size.height / v;
  54. const float a = 0.3;
  55. for (int i = 1; i < h; i++) {
  56. int x = i * width;
  57. split_sheet_preview->draw_line(Point2(x, 0), Point2(x, size.height), Color(1, 1, 1, a));
  58. split_sheet_preview->draw_line(Point2(x + 1, 0), Point2(x + 1, size.height), Color(0, 0, 0, a));
  59. for (int j = 1; j < v; j++) {
  60. int y = j * height;
  61. split_sheet_preview->draw_line(Point2(0, y), Point2(size.width, y), Color(1, 1, 1, a));
  62. split_sheet_preview->draw_line(Point2(0, y + 1), Point2(size.width, y + 1), Color(0, 0, 0, a));
  63. }
  64. }
  65. if (frames_selected.size() == 0) {
  66. split_sheet_dialog->get_ok()->set_disabled(true);
  67. split_sheet_dialog->get_ok()->set_text(TTR("No Frames Selected"));
  68. return;
  69. }
  70. Color accent = get_color("accent_color", "Editor");
  71. for (Set<int>::Element *E = frames_selected.front(); E; E = E->next()) {
  72. int idx = E->get();
  73. int xp = idx % h;
  74. int yp = (idx - xp) / h;
  75. int x = xp * width;
  76. int y = yp * height;
  77. split_sheet_preview->draw_rect(Rect2(x + 5, y + 5, width - 10, height - 10), Color(0, 0, 0, 0.35), true);
  78. split_sheet_preview->draw_rect(Rect2(x + 0, y + 0, width - 0, height - 0), Color(0, 0, 0, 1), false);
  79. split_sheet_preview->draw_rect(Rect2(x + 1, y + 1, width - 2, height - 2), Color(0, 0, 0, 1), false);
  80. split_sheet_preview->draw_rect(Rect2(x + 2, y + 2, width - 4, height - 4), accent, false);
  81. split_sheet_preview->draw_rect(Rect2(x + 3, y + 3, width - 6, height - 6), accent, false);
  82. split_sheet_preview->draw_rect(Rect2(x + 4, y + 4, width - 8, height - 8), Color(0, 0, 0, 1), false);
  83. split_sheet_preview->draw_rect(Rect2(x + 5, y + 5, width - 10, height - 10), Color(0, 0, 0, 1), false);
  84. }
  85. split_sheet_dialog->get_ok()->set_disabled(false);
  86. split_sheet_dialog->get_ok()->set_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size()));
  87. }
  88. void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
  89. Ref<InputEventMouseButton> mb = p_event;
  90. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  91. Size2i size = split_sheet_preview->get_size();
  92. int h = split_sheet_h->get_value();
  93. int v = split_sheet_v->get_value();
  94. int x = CLAMP(int(mb->get_position().x) * h / size.width, 0, h - 1);
  95. int y = CLAMP(int(mb->get_position().y) * v / size.height, 0, v - 1);
  96. int idx = h * y + x;
  97. if (mb->get_shift() && last_frame_selected >= 0) {
  98. //select multiple
  99. int from = idx;
  100. int to = last_frame_selected;
  101. if (from > to) {
  102. SWAP(from, to);
  103. }
  104. for (int i = from; i <= to; i++) {
  105. if (mb->get_control()) {
  106. frames_selected.erase(i);
  107. } else {
  108. frames_selected.insert(i);
  109. }
  110. }
  111. } else {
  112. if (frames_selected.has(idx)) {
  113. frames_selected.erase(idx);
  114. } else {
  115. frames_selected.insert(idx);
  116. }
  117. }
  118. last_frame_selected = idx;
  119. split_sheet_preview->update();
  120. }
  121. }
  122. void SpriteFramesEditor::_sheet_add_frames() {
  123. Size2i size = split_sheet_preview->get_size();
  124. int h = split_sheet_h->get_value();
  125. int v = split_sheet_v->get_value();
  126. undo_redo->create_action(TTR("Add Frame"));
  127. int fc = frames->get_frame_count(edited_anim);
  128. for (Set<int>::Element *E = frames_selected.front(); E; E = E->next()) {
  129. int idx = E->get();
  130. int width = size.width / h;
  131. int height = size.height / v;
  132. int xp = idx % h;
  133. int yp = (idx - xp) / h;
  134. int x = xp * width;
  135. int y = yp * height;
  136. Ref<AtlasTexture> at;
  137. at.instance();
  138. at->set_atlas(split_sheet_preview->get_texture());
  139. at->set_region(Rect2(x, y, width, height));
  140. undo_redo->add_do_method(frames, "add_frame", edited_anim, at, -1);
  141. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, fc);
  142. }
  143. undo_redo->add_do_method(this, "_update_library");
  144. undo_redo->add_undo_method(this, "_update_library");
  145. undo_redo->commit_action();
  146. }
  147. void SpriteFramesEditor::_sheet_select_clear_all_frames() {
  148. bool should_clear = true;
  149. for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
  150. if (!frames_selected.has(i)) {
  151. frames_selected.insert(i);
  152. should_clear = false;
  153. }
  154. }
  155. if (should_clear) {
  156. frames_selected.clear();
  157. }
  158. split_sheet_preview->update();
  159. }
  160. void SpriteFramesEditor::_sheet_spin_changed(double) {
  161. frames_selected.clear();
  162. last_frame_selected = -1;
  163. split_sheet_preview->update();
  164. }
  165. void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
  166. Ref<Resource> texture = ResourceLoader::load(p_file);
  167. if (!texture.is_valid()) {
  168. EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
  169. ERR_FAIL_COND(!texture.is_valid());
  170. }
  171. if (texture != split_sheet_preview->get_texture()) {
  172. //different texture, reset to 4x4
  173. split_sheet_h->set_value(4);
  174. split_sheet_v->set_value(4);
  175. }
  176. frames_selected.clear();
  177. last_frame_selected = -1;
  178. split_sheet_preview->set_texture(texture);
  179. split_sheet_dialog->popup_centered_ratio(0.65);
  180. }
  181. void SpriteFramesEditor::_notification(int p_what) {
  182. switch (p_what) {
  183. case NOTIFICATION_ENTER_TREE: {
  184. load->set_icon(get_icon("Load", "EditorIcons"));
  185. load_sheet->set_icon(get_icon("SpriteSheet", "EditorIcons"));
  186. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  187. paste->set_icon(get_icon("ActionPaste", "EditorIcons"));
  188. empty->set_icon(get_icon("InsertBefore", "EditorIcons"));
  189. empty2->set_icon(get_icon("InsertAfter", "EditorIcons"));
  190. move_up->set_icon(get_icon("MoveLeft", "EditorIcons"));
  191. move_down->set_icon(get_icon("MoveRight", "EditorIcons"));
  192. _delete->set_icon(get_icon("Remove", "EditorIcons"));
  193. new_anim->set_icon(get_icon("New", "EditorIcons"));
  194. remove_anim->set_icon(get_icon("Remove", "EditorIcons"));
  195. FALLTHROUGH;
  196. }
  197. case NOTIFICATION_THEME_CHANGED: {
  198. splite_sheet_scroll->add_style_override("bg", get_stylebox("bg", "Tree"));
  199. } break;
  200. case NOTIFICATION_READY: {
  201. add_constant_override("autohide", 1); // Fixes the dragger always showing up.
  202. } break;
  203. }
  204. }
  205. void SpriteFramesEditor::_file_load_request(const PoolVector<String> &p_path, int p_at_pos) {
  206. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  207. List<Ref<Texture> > resources;
  208. for (int i = 0; i < p_path.size(); i++) {
  209. Ref<Texture> resource;
  210. resource = ResourceLoader::load(p_path[i]);
  211. if (resource.is_null()) {
  212. dialog->set_text(TTR("ERROR: Couldn't load frame resource!"));
  213. dialog->set_title(TTR("Error!"));
  214. //dialog->get_cancel()->set_text("Close");
  215. dialog->get_ok()->set_text(TTR("Close"));
  216. dialog->popup_centered_minsize();
  217. return; ///beh should show an error i guess
  218. }
  219. resources.push_back(resource);
  220. }
  221. if (resources.empty()) {
  222. return;
  223. }
  224. undo_redo->create_action(TTR("Add Frame"));
  225. int fc = frames->get_frame_count(edited_anim);
  226. int count = 0;
  227. for (List<Ref<Texture> >::Element *E = resources.front(); E; E = E->next()) {
  228. undo_redo->add_do_method(frames, "add_frame", edited_anim, E->get(), p_at_pos == -1 ? -1 : p_at_pos + count);
  229. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
  230. count++;
  231. }
  232. undo_redo->add_do_method(this, "_update_library");
  233. undo_redo->add_undo_method(this, "_update_library");
  234. undo_redo->commit_action();
  235. }
  236. void SpriteFramesEditor::_load_pressed() {
  237. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  238. loading_scene = false;
  239. file->clear_filters();
  240. List<String> extensions;
  241. ResourceLoader::get_recognized_extensions_for_type("Texture", &extensions);
  242. for (int i = 0; i < extensions.size(); i++)
  243. file->add_filter("*." + extensions[i]);
  244. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  245. file->popup_centered_ratio();
  246. }
  247. void SpriteFramesEditor::_paste_pressed() {
  248. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  249. Ref<Texture> r = EditorSettings::get_singleton()->get_resource_clipboard();
  250. if (!r.is_valid()) {
  251. dialog->set_text(TTR("Resource clipboard is empty or not a texture!"));
  252. dialog->set_title(TTR("Error!"));
  253. //dialog->get_cancel()->set_text("Close");
  254. dialog->get_ok()->set_text(TTR("Close"));
  255. dialog->popup_centered_minsize();
  256. return; ///beh should show an error i guess
  257. }
  258. undo_redo->create_action(TTR("Paste Frame"));
  259. undo_redo->add_do_method(frames, "add_frame", edited_anim, r);
  260. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, frames->get_frame_count(edited_anim));
  261. undo_redo->add_do_method(this, "_update_library");
  262. undo_redo->add_undo_method(this, "_update_library");
  263. undo_redo->commit_action();
  264. }
  265. void SpriteFramesEditor::_copy_pressed() {
  266. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  267. if (tree->get_current() < 0)
  268. return;
  269. Ref<Texture> r = frames->get_frame(edited_anim, tree->get_current());
  270. if (!r.is_valid()) {
  271. return;
  272. }
  273. EditorSettings::get_singleton()->set_resource_clipboard(r);
  274. }
  275. void SpriteFramesEditor::_empty_pressed() {
  276. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  277. int from = -1;
  278. if (tree->get_current() >= 0) {
  279. from = tree->get_current();
  280. sel = from;
  281. } else {
  282. from = frames->get_frame_count(edited_anim);
  283. }
  284. Ref<Texture> r;
  285. undo_redo->create_action(TTR("Add Empty"));
  286. undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from);
  287. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from);
  288. undo_redo->add_do_method(this, "_update_library");
  289. undo_redo->add_undo_method(this, "_update_library");
  290. undo_redo->commit_action();
  291. }
  292. void SpriteFramesEditor::_empty2_pressed() {
  293. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  294. int from = -1;
  295. if (tree->get_current() >= 0) {
  296. from = tree->get_current();
  297. sel = from;
  298. } else {
  299. from = frames->get_frame_count(edited_anim);
  300. }
  301. Ref<Texture> r;
  302. undo_redo->create_action(TTR("Add Empty"));
  303. undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from + 1);
  304. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from + 1);
  305. undo_redo->add_do_method(this, "_update_library");
  306. undo_redo->add_undo_method(this, "_update_library");
  307. undo_redo->commit_action();
  308. }
  309. void SpriteFramesEditor::_up_pressed() {
  310. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  311. if (tree->get_current() < 0)
  312. return;
  313. int to_move = tree->get_current();
  314. if (to_move < 1)
  315. return;
  316. sel = to_move;
  317. sel -= 1;
  318. undo_redo->create_action(TTR("Delete Resource"));
  319. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move - 1));
  320. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move));
  321. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
  322. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move - 1));
  323. undo_redo->add_do_method(this, "_update_library");
  324. undo_redo->add_undo_method(this, "_update_library");
  325. undo_redo->commit_action();
  326. }
  327. void SpriteFramesEditor::_down_pressed() {
  328. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  329. if (tree->get_current() < 0)
  330. return;
  331. int to_move = tree->get_current();
  332. if (to_move < 0 || to_move >= frames->get_frame_count(edited_anim) - 1)
  333. return;
  334. sel = to_move;
  335. sel += 1;
  336. undo_redo->create_action(TTR("Delete Resource"));
  337. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move + 1));
  338. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move));
  339. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
  340. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move + 1));
  341. undo_redo->add_do_method(this, "_update_library");
  342. undo_redo->add_undo_method(this, "_update_library");
  343. undo_redo->commit_action();
  344. }
  345. void SpriteFramesEditor::_delete_pressed() {
  346. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  347. if (tree->get_current() < 0)
  348. return;
  349. int to_delete = tree->get_current();
  350. if (to_delete < 0 || to_delete >= frames->get_frame_count(edited_anim)) {
  351. return;
  352. }
  353. undo_redo->create_action(TTR("Delete Resource"));
  354. undo_redo->add_do_method(frames, "remove_frame", edited_anim, to_delete);
  355. undo_redo->add_undo_method(frames, "add_frame", edited_anim, frames->get_frame(edited_anim, to_delete), to_delete);
  356. undo_redo->add_do_method(this, "_update_library");
  357. undo_redo->add_undo_method(this, "_update_library");
  358. undo_redo->commit_action();
  359. }
  360. void SpriteFramesEditor::_animation_select() {
  361. if (updating)
  362. return;
  363. if (frames->has_animation(edited_anim)) {
  364. double value = anim_speed->get_line_edit()->get_text().to_double();
  365. if (!Math::is_equal_approx(value, frames->get_animation_speed(edited_anim)))
  366. _animation_fps_changed(value);
  367. }
  368. TreeItem *selected = animations->get_selected();
  369. ERR_FAIL_COND(!selected);
  370. edited_anim = selected->get_text(0);
  371. _update_library(true);
  372. }
  373. static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
  374. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  375. if (!edited)
  376. return;
  377. if (p_node != edited && p_node->get_owner() != edited)
  378. return;
  379. {
  380. AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);
  381. if (as && as->get_sprite_frames() == p_sfames) {
  382. r_nodes->push_back(p_node);
  383. }
  384. }
  385. {
  386. AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
  387. if (as && as->get_sprite_frames() == p_sfames) {
  388. r_nodes->push_back(p_node);
  389. }
  390. }
  391. for (int i = 0; i < p_node->get_child_count(); i++) {
  392. _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
  393. }
  394. }
  395. void SpriteFramesEditor::_animation_name_edited() {
  396. if (updating)
  397. return;
  398. if (!frames->has_animation(edited_anim))
  399. return;
  400. TreeItem *edited = animations->get_edited();
  401. if (!edited)
  402. return;
  403. String new_name = edited->get_text(0);
  404. if (new_name == String(edited_anim))
  405. return;
  406. new_name = new_name.replace("/", "_").replace(",", " ");
  407. String name = new_name;
  408. int counter = 0;
  409. while (frames->has_animation(name)) {
  410. counter++;
  411. name = new_name + " " + itos(counter);
  412. }
  413. List<Node *> nodes;
  414. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  415. undo_redo->create_action(TTR("Rename Animation"));
  416. undo_redo->add_do_method(frames, "rename_animation", edited_anim, name);
  417. undo_redo->add_undo_method(frames, "rename_animation", name, edited_anim);
  418. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  419. String current = E->get()->call("get_animation");
  420. undo_redo->add_do_method(E->get(), "set_animation", name);
  421. undo_redo->add_undo_method(E->get(), "set_animation", edited_anim);
  422. }
  423. undo_redo->add_do_method(this, "_update_library");
  424. undo_redo->add_undo_method(this, "_update_library");
  425. edited_anim = new_name;
  426. undo_redo->commit_action();
  427. }
  428. void SpriteFramesEditor::_animation_add() {
  429. String name = "New Anim";
  430. int counter = 0;
  431. while (frames->has_animation(name)) {
  432. counter++;
  433. name = "New Anim " + itos(counter);
  434. }
  435. List<Node *> nodes;
  436. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  437. undo_redo->create_action(TTR("Add Animation"));
  438. undo_redo->add_do_method(frames, "add_animation", name);
  439. undo_redo->add_undo_method(frames, "remove_animation", name);
  440. undo_redo->add_do_method(this, "_update_library");
  441. undo_redo->add_undo_method(this, "_update_library");
  442. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  443. String current = E->get()->call("get_animation");
  444. undo_redo->add_do_method(E->get(), "set_animation", name);
  445. undo_redo->add_undo_method(E->get(), "set_animation", current);
  446. }
  447. edited_anim = name;
  448. undo_redo->commit_action();
  449. animations->grab_focus();
  450. }
  451. void SpriteFramesEditor::_animation_remove() {
  452. if (updating)
  453. return;
  454. if (!frames->has_animation(edited_anim))
  455. return;
  456. delete_dialog->set_text(TTR("Delete Animation?"));
  457. delete_dialog->popup_centered_minsize();
  458. }
  459. void SpriteFramesEditor::_animation_remove_confirmed() {
  460. undo_redo->create_action(TTR("Remove Animation"));
  461. undo_redo->add_do_method(frames, "remove_animation", edited_anim);
  462. undo_redo->add_undo_method(frames, "add_animation", edited_anim);
  463. undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  464. undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  465. int fc = frames->get_frame_count(edited_anim);
  466. for (int i = 0; i < fc; i++) {
  467. Ref<Texture> frame = frames->get_frame(edited_anim, i);
  468. undo_redo->add_undo_method(frames, "add_frame", edited_anim, frame);
  469. }
  470. undo_redo->add_do_method(this, "_update_library");
  471. undo_redo->add_undo_method(this, "_update_library");
  472. edited_anim = StringName();
  473. undo_redo->commit_action();
  474. }
  475. void SpriteFramesEditor::_animation_loop_changed() {
  476. if (updating)
  477. return;
  478. undo_redo->create_action(TTR("Change Animation Loop"));
  479. undo_redo->add_do_method(frames, "set_animation_loop", edited_anim, anim_loop->is_pressed());
  480. undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  481. undo_redo->add_do_method(this, "_update_library", true);
  482. undo_redo->add_undo_method(this, "_update_library", true);
  483. undo_redo->commit_action();
  484. }
  485. void SpriteFramesEditor::_animation_fps_changed(double p_value) {
  486. if (updating)
  487. return;
  488. undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS);
  489. undo_redo->add_do_method(frames, "set_animation_speed", edited_anim, p_value);
  490. undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  491. undo_redo->add_do_method(this, "_update_library", true);
  492. undo_redo->add_undo_method(this, "_update_library", true);
  493. undo_redo->commit_action();
  494. }
  495. void SpriteFramesEditor::_update_library(bool p_skip_selector) {
  496. updating = true;
  497. if (!p_skip_selector) {
  498. animations->clear();
  499. TreeItem *anim_root = animations->create_item();
  500. List<StringName> anim_names;
  501. frames->get_animation_list(&anim_names);
  502. anim_names.sort_custom<StringName::AlphCompare>();
  503. for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
  504. String name = E->get();
  505. TreeItem *it = animations->create_item(anim_root);
  506. it->set_metadata(0, name);
  507. it->set_text(0, name);
  508. it->set_editable(0, true);
  509. if (E->get() == edited_anim) {
  510. it->select(0);
  511. }
  512. }
  513. }
  514. tree->clear();
  515. if (!frames->has_animation(edited_anim)) {
  516. updating = false;
  517. return;
  518. }
  519. if (sel >= frames->get_frame_count(edited_anim))
  520. sel = frames->get_frame_count(edited_anim) - 1;
  521. else if (sel < 0 && frames->get_frame_count(edited_anim))
  522. sel = 0;
  523. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  524. String name;
  525. Ref<Texture> icon;
  526. if (frames->get_frame(edited_anim, i).is_null()) {
  527. name = itos(i) + ": " + TTR("(empty)");
  528. } else {
  529. name = itos(i) + ": " + frames->get_frame(edited_anim, i)->get_name();
  530. icon = frames->get_frame(edited_anim, i);
  531. }
  532. tree->add_item(name, icon);
  533. if (frames->get_frame(edited_anim, i).is_valid())
  534. tree->set_item_tooltip(tree->get_item_count() - 1, frames->get_frame(edited_anim, i)->get_path());
  535. if (sel == i)
  536. tree->select(tree->get_item_count() - 1);
  537. }
  538. anim_speed->set_value(frames->get_animation_speed(edited_anim));
  539. anim_loop->set_pressed(frames->get_animation_loop(edited_anim));
  540. updating = false;
  541. //player->add_resource("default",resource);
  542. }
  543. void SpriteFramesEditor::edit(SpriteFrames *p_frames) {
  544. if (frames == p_frames)
  545. return;
  546. frames = p_frames;
  547. if (p_frames) {
  548. if (!p_frames->has_animation(edited_anim)) {
  549. List<StringName> anim_names;
  550. frames->get_animation_list(&anim_names);
  551. anim_names.sort_custom<StringName::AlphCompare>();
  552. if (anim_names.size()) {
  553. edited_anim = anim_names.front()->get();
  554. } else {
  555. edited_anim = StringName();
  556. }
  557. }
  558. _update_library();
  559. } else {
  560. hide();
  561. }
  562. }
  563. Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  564. if (!frames->has_animation(edited_anim))
  565. return false;
  566. int idx = tree->get_item_at_position(p_point, true);
  567. if (idx < 0 || idx >= frames->get_frame_count(edited_anim))
  568. return Variant();
  569. RES frame = frames->get_frame(edited_anim, idx);
  570. if (frame.is_null())
  571. return Variant();
  572. Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
  573. drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
  574. return drag_data;
  575. }
  576. bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  577. Dictionary d = p_data;
  578. if (!d.has("type"))
  579. return false;
  580. // reordering frames
  581. if (d.has("from") && (Object *)(d["from"]) == tree)
  582. return true;
  583. if (String(d["type"]) == "resource" && d.has("resource")) {
  584. RES r = d["resource"];
  585. Ref<Texture> texture = r;
  586. if (texture.is_valid()) {
  587. return true;
  588. }
  589. }
  590. if (String(d["type"]) == "files") {
  591. Vector<String> files = d["files"];
  592. if (files.size() == 0)
  593. return false;
  594. for (int i = 0; i < files.size(); i++) {
  595. String file = files[i];
  596. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  597. if (!ClassDB::is_parent_class(ftype, "Texture")) {
  598. return false;
  599. }
  600. }
  601. return true;
  602. }
  603. return false;
  604. }
  605. void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  606. if (!can_drop_data_fw(p_point, p_data, p_from))
  607. return;
  608. Dictionary d = p_data;
  609. if (!d.has("type"))
  610. return;
  611. int at_pos = tree->get_item_at_position(p_point, true);
  612. if (String(d["type"]) == "resource" && d.has("resource")) {
  613. RES r = d["resource"];
  614. Ref<Texture> texture = r;
  615. if (texture.is_valid()) {
  616. bool reorder = false;
  617. if (d.has("from") && (Object *)(d["from"]) == tree)
  618. reorder = true;
  619. if (reorder) { //drop is from reordering frames
  620. int from_frame = -1;
  621. if (d.has("frame"))
  622. from_frame = d["frame"];
  623. undo_redo->create_action(TTR("Move Frame"));
  624. undo_redo->add_do_method(frames, "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
  625. undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
  626. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
  627. undo_redo->add_undo_method(frames, "add_frame", edited_anim, texture, from_frame);
  628. undo_redo->add_do_method(this, "_update_library");
  629. undo_redo->add_undo_method(this, "_update_library");
  630. undo_redo->commit_action();
  631. } else {
  632. undo_redo->create_action(TTR("Add Frame"));
  633. undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
  634. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
  635. undo_redo->add_do_method(this, "_update_library");
  636. undo_redo->add_undo_method(this, "_update_library");
  637. undo_redo->commit_action();
  638. }
  639. }
  640. }
  641. if (String(d["type"]) == "files") {
  642. PoolVector<String> files = d["files"];
  643. _file_load_request(files, at_pos);
  644. }
  645. }
  646. void SpriteFramesEditor::_bind_methods() {
  647. ClassDB::bind_method(D_METHOD("_load_pressed"), &SpriteFramesEditor::_load_pressed);
  648. ClassDB::bind_method(D_METHOD("_empty_pressed"), &SpriteFramesEditor::_empty_pressed);
  649. ClassDB::bind_method(D_METHOD("_empty2_pressed"), &SpriteFramesEditor::_empty2_pressed);
  650. ClassDB::bind_method(D_METHOD("_delete_pressed"), &SpriteFramesEditor::_delete_pressed);
  651. ClassDB::bind_method(D_METHOD("_copy_pressed"), &SpriteFramesEditor::_copy_pressed);
  652. ClassDB::bind_method(D_METHOD("_paste_pressed"), &SpriteFramesEditor::_paste_pressed);
  653. ClassDB::bind_method(D_METHOD("_file_load_request", "files", "at_position"), &SpriteFramesEditor::_file_load_request, DEFVAL(-1));
  654. ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
  655. ClassDB::bind_method(D_METHOD("_up_pressed"), &SpriteFramesEditor::_up_pressed);
  656. ClassDB::bind_method(D_METHOD("_down_pressed"), &SpriteFramesEditor::_down_pressed);
  657. ClassDB::bind_method(D_METHOD("_animation_select"), &SpriteFramesEditor::_animation_select);
  658. ClassDB::bind_method(D_METHOD("_animation_name_edited"), &SpriteFramesEditor::_animation_name_edited);
  659. ClassDB::bind_method(D_METHOD("_animation_add"), &SpriteFramesEditor::_animation_add);
  660. ClassDB::bind_method(D_METHOD("_animation_remove"), &SpriteFramesEditor::_animation_remove);
  661. ClassDB::bind_method(D_METHOD("_animation_remove_confirmed"), &SpriteFramesEditor::_animation_remove_confirmed);
  662. ClassDB::bind_method(D_METHOD("_animation_loop_changed"), &SpriteFramesEditor::_animation_loop_changed);
  663. ClassDB::bind_method(D_METHOD("_animation_fps_changed"), &SpriteFramesEditor::_animation_fps_changed);
  664. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SpriteFramesEditor::get_drag_data_fw);
  665. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpriteFramesEditor::can_drop_data_fw);
  666. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpriteFramesEditor::drop_data_fw);
  667. ClassDB::bind_method(D_METHOD("_prepare_sprite_sheet"), &SpriteFramesEditor::_prepare_sprite_sheet);
  668. ClassDB::bind_method(D_METHOD("_open_sprite_sheet"), &SpriteFramesEditor::_open_sprite_sheet);
  669. ClassDB::bind_method(D_METHOD("_sheet_preview_draw"), &SpriteFramesEditor::_sheet_preview_draw);
  670. ClassDB::bind_method(D_METHOD("_sheet_preview_input"), &SpriteFramesEditor::_sheet_preview_input);
  671. ClassDB::bind_method(D_METHOD("_sheet_spin_changed"), &SpriteFramesEditor::_sheet_spin_changed);
  672. ClassDB::bind_method(D_METHOD("_sheet_add_frames"), &SpriteFramesEditor::_sheet_add_frames);
  673. ClassDB::bind_method(D_METHOD("_sheet_select_clear_all_frames"), &SpriteFramesEditor::_sheet_select_clear_all_frames);
  674. }
  675. SpriteFramesEditor::SpriteFramesEditor() {
  676. VBoxContainer *vbc_animlist = memnew(VBoxContainer);
  677. add_child(vbc_animlist);
  678. vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
  679. VBoxContainer *sub_vb = memnew(VBoxContainer);
  680. vbc_animlist->add_margin_child(TTR("Animations:"), sub_vb, true);
  681. sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  682. HBoxContainer *hbc_animlist = memnew(HBoxContainer);
  683. sub_vb->add_child(hbc_animlist);
  684. new_anim = memnew(ToolButton);
  685. new_anim->set_tooltip(TTR("New Animation"));
  686. hbc_animlist->add_child(new_anim);
  687. new_anim->connect("pressed", this, "_animation_add");
  688. remove_anim = memnew(ToolButton);
  689. remove_anim->set_tooltip(TTR("Remove Animation"));
  690. hbc_animlist->add_child(remove_anim);
  691. remove_anim->connect("pressed", this, "_animation_remove");
  692. animations = memnew(Tree);
  693. sub_vb->add_child(animations);
  694. animations->set_v_size_flags(SIZE_EXPAND_FILL);
  695. animations->set_hide_root(true);
  696. animations->connect("cell_selected", this, "_animation_select");
  697. animations->connect("item_edited", this, "_animation_name_edited");
  698. animations->set_allow_reselect(true);
  699. anim_speed = memnew(SpinBox);
  700. vbc_animlist->add_margin_child(TTR("Speed (FPS):"), anim_speed);
  701. anim_speed->set_min(0);
  702. anim_speed->set_max(100);
  703. anim_speed->set_step(0.01);
  704. anim_speed->connect("value_changed", this, "_animation_fps_changed");
  705. anim_loop = memnew(CheckButton);
  706. anim_loop->set_text(TTR("Loop"));
  707. vbc_animlist->add_child(anim_loop);
  708. anim_loop->connect("pressed", this, "_animation_loop_changed");
  709. VBoxContainer *vbc = memnew(VBoxContainer);
  710. add_child(vbc);
  711. vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  712. sub_vb = memnew(VBoxContainer);
  713. vbc->add_margin_child(TTR("Animation Frames:"), sub_vb, true);
  714. HBoxContainer *hbc = memnew(HBoxContainer);
  715. sub_vb->add_child(hbc);
  716. load = memnew(ToolButton);
  717. load->set_tooltip(TTR("Add a Texture from File"));
  718. hbc->add_child(load);
  719. load_sheet = memnew(ToolButton);
  720. load_sheet->set_tooltip(TTR("Add Frames from a Sprite Sheet"));
  721. hbc->add_child(load_sheet);
  722. hbc->add_child(memnew(VSeparator));
  723. copy = memnew(ToolButton);
  724. copy->set_tooltip(TTR("Copy"));
  725. hbc->add_child(copy);
  726. paste = memnew(ToolButton);
  727. paste->set_tooltip(TTR("Paste"));
  728. hbc->add_child(paste);
  729. hbc->add_child(memnew(VSeparator));
  730. empty = memnew(ToolButton);
  731. empty->set_tooltip(TTR("Insert Empty (Before)"));
  732. hbc->add_child(empty);
  733. empty2 = memnew(ToolButton);
  734. empty2->set_tooltip(TTR("Insert Empty (After)"));
  735. hbc->add_child(empty2);
  736. hbc->add_child(memnew(VSeparator));
  737. move_up = memnew(ToolButton);
  738. move_up->set_tooltip(TTR("Move (Before)"));
  739. hbc->add_child(move_up);
  740. move_down = memnew(ToolButton);
  741. move_down->set_tooltip(TTR("Move (After)"));
  742. hbc->add_child(move_down);
  743. _delete = memnew(ToolButton);
  744. _delete->set_tooltip(TTR("Delete"));
  745. hbc->add_child(_delete);
  746. file = memnew(EditorFileDialog);
  747. add_child(file);
  748. tree = memnew(ItemList);
  749. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  750. tree->set_icon_mode(ItemList::ICON_MODE_TOP);
  751. int thumbnail_size = 96;
  752. tree->set_max_columns(0);
  753. tree->set_icon_mode(ItemList::ICON_MODE_TOP);
  754. tree->set_fixed_column_width(thumbnail_size * 3 / 2);
  755. tree->set_max_text_lines(2);
  756. tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  757. tree->set_drag_forwarding(this);
  758. sub_vb->add_child(tree);
  759. dialog = memnew(AcceptDialog);
  760. add_child(dialog);
  761. load->connect("pressed", this, "_load_pressed");
  762. load_sheet->connect("pressed", this, "_open_sprite_sheet");
  763. _delete->connect("pressed", this, "_delete_pressed");
  764. copy->connect("pressed", this, "_copy_pressed");
  765. paste->connect("pressed", this, "_paste_pressed");
  766. empty->connect("pressed", this, "_empty_pressed");
  767. empty2->connect("pressed", this, "_empty2_pressed");
  768. move_up->connect("pressed", this, "_up_pressed");
  769. move_down->connect("pressed", this, "_down_pressed");
  770. file->connect("files_selected", this, "_file_load_request");
  771. loading_scene = false;
  772. sel = -1;
  773. updating = false;
  774. edited_anim = "default";
  775. delete_dialog = memnew(ConfirmationDialog);
  776. add_child(delete_dialog);
  777. delete_dialog->connect("confirmed", this, "_animation_remove_confirmed");
  778. split_sheet_dialog = memnew(ConfirmationDialog);
  779. add_child(split_sheet_dialog);
  780. VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
  781. split_sheet_dialog->add_child(split_sheet_vb);
  782. split_sheet_dialog->set_title(TTR("Select Frames"));
  783. split_sheet_dialog->connect("confirmed", this, "_sheet_add_frames");
  784. HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
  785. Label *ss_label = memnew(Label(TTR("Horizontal:")));
  786. split_sheet_hb->add_child(ss_label);
  787. split_sheet_h = memnew(SpinBox);
  788. split_sheet_h->set_min(1);
  789. split_sheet_h->set_max(128);
  790. split_sheet_h->set_step(1);
  791. split_sheet_hb->add_child(split_sheet_h);
  792. split_sheet_h->connect("value_changed", this, "_sheet_spin_changed");
  793. ss_label = memnew(Label(TTR("Vertical:")));
  794. split_sheet_hb->add_child(ss_label);
  795. split_sheet_v = memnew(SpinBox);
  796. split_sheet_v->set_min(1);
  797. split_sheet_v->set_max(128);
  798. split_sheet_v->set_step(1);
  799. split_sheet_hb->add_child(split_sheet_v);
  800. split_sheet_v->connect("value_changed", this, "_sheet_spin_changed");
  801. split_sheet_hb->add_spacer();
  802. Button *select_clear_all = memnew(Button);
  803. select_clear_all->set_text(TTR("Select/Clear All Frames"));
  804. select_clear_all->connect("pressed", this, "_sheet_select_clear_all_frames");
  805. split_sheet_hb->add_child(select_clear_all);
  806. split_sheet_vb->add_child(split_sheet_hb);
  807. split_sheet_preview = memnew(TextureRect);
  808. split_sheet_preview->set_expand(false);
  809. split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
  810. split_sheet_preview->connect("draw", this, "_sheet_preview_draw");
  811. split_sheet_preview->connect("gui_input", this, "_sheet_preview_input");
  812. splite_sheet_scroll = memnew(ScrollContainer);
  813. splite_sheet_scroll->set_enable_h_scroll(true);
  814. splite_sheet_scroll->set_enable_v_scroll(true);
  815. splite_sheet_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  816. CenterContainer *cc = memnew(CenterContainer);
  817. cc->add_child(split_sheet_preview);
  818. cc->set_h_size_flags(SIZE_EXPAND_FILL);
  819. cc->set_v_size_flags(SIZE_EXPAND_FILL);
  820. splite_sheet_scroll->add_child(cc);
  821. split_sheet_vb->add_child(splite_sheet_scroll);
  822. file_split_sheet = memnew(EditorFileDialog);
  823. file_split_sheet->set_title(TTR("Create Frames from Sprite Sheet"));
  824. file_split_sheet->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  825. add_child(file_split_sheet);
  826. file_split_sheet->connect("file_selected", this, "_prepare_sprite_sheet");
  827. }
  828. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  829. frames_editor->set_undo_redo(&get_undo_redo());
  830. SpriteFrames *s;
  831. AnimatedSprite *animated_sprite = Object::cast_to<AnimatedSprite>(p_object);
  832. if (animated_sprite) {
  833. s = *animated_sprite->get_sprite_frames();
  834. } else {
  835. s = Object::cast_to<SpriteFrames>(p_object);
  836. }
  837. frames_editor->edit(s);
  838. }
  839. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  840. AnimatedSprite *animated_sprite = Object::cast_to<AnimatedSprite>(p_object);
  841. if (animated_sprite && *animated_sprite->get_sprite_frames()) {
  842. return true;
  843. } else {
  844. return p_object->is_class("SpriteFrames");
  845. }
  846. }
  847. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  848. if (p_visible) {
  849. button->show();
  850. editor->make_bottom_panel_item_visible(frames_editor);
  851. } else {
  852. button->hide();
  853. if (frames_editor->is_visible_in_tree())
  854. editor->hide_bottom_panel();
  855. }
  856. }
  857. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin(EditorNode *p_node) {
  858. editor = p_node;
  859. frames_editor = memnew(SpriteFramesEditor);
  860. frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  861. button = editor->add_bottom_panel_item(TTR("SpriteFrames"), frames_editor);
  862. button->hide();
  863. }
  864. SpriteFramesEditorPlugin::~SpriteFramesEditorPlugin() {
  865. }