sprite_frames_editor_plugin.cpp 44 KB

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