sprite_frames_editor_plugin.cpp 43 KB

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