sprite_frames_editor_plugin.cpp 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  1. /**************************************************************************/
  2. /* sprite_frames_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #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_command_palette.h"
  35. #include "editor/editor_file_system.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_bottom_panel.h"
  41. #include "editor/gui/editor_file_dialog.h"
  42. #include "editor/scene_tree_dock.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/gui/center_container.h"
  45. #include "scene/gui/flow_container.h"
  46. #include "scene/gui/margin_container.h"
  47. #include "scene/gui/option_button.h"
  48. #include "scene/gui/panel_container.h"
  49. #include "scene/gui/separator.h"
  50. #include "scene/resources/atlas_texture.h"
  51. static void _draw_shadowed_line(Control *p_control, const Point2 &p_from, const Size2 &p_size, const Size2 &p_shadow_offset, Color p_color, Color p_shadow_color) {
  52. p_control->draw_line(p_from, p_from + p_size, p_color);
  53. p_control->draw_line(p_from + p_shadow_offset, p_from + p_size + p_shadow_offset, p_shadow_color);
  54. }
  55. void SpriteFramesEditor::_open_sprite_sheet() {
  56. file_split_sheet->clear_filters();
  57. List<String> extensions;
  58. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  59. for (const String &extension : extensions) {
  60. file_split_sheet->add_filter("*." + extension);
  61. }
  62. file_split_sheet->popup_file_dialog();
  63. }
  64. int SpriteFramesEditor::_sheet_preview_position_to_frame_index(const Point2 &p_position) {
  65. const Size2i offset = _get_offset();
  66. const Size2i frame_size = _get_frame_size();
  67. const Size2i separation = _get_separation();
  68. const Size2i block_size = frame_size + separation;
  69. const Point2i position = p_position / sheet_zoom - offset;
  70. if (position.x < 0 || position.y < 0) {
  71. return -1; // Out of bounds.
  72. }
  73. if (position.x % block_size.x >= frame_size.x || position.y % block_size.y >= frame_size.y) {
  74. return -1; // Gap between frames.
  75. }
  76. const Point2i frame = position / block_size;
  77. const Size2i frame_count = _get_frame_count();
  78. if (frame.x >= frame_count.x || frame.y >= frame_count.y) {
  79. return -1; // Out of bounds.
  80. }
  81. return frame_count.x * frame.y + frame.x;
  82. }
  83. void SpriteFramesEditor::_sheet_preview_draw() {
  84. const Size2i frame_count = _get_frame_count();
  85. const Size2i separation = _get_separation();
  86. const Size2 draw_offset = Size2(_get_offset()) * sheet_zoom;
  87. const Size2 draw_sep = Size2(separation) * sheet_zoom;
  88. const Size2 draw_frame_size = Size2(_get_frame_size()) * sheet_zoom;
  89. const Size2 draw_size = draw_frame_size * frame_count + draw_sep * (frame_count - Size2i(1, 1));
  90. const Color line_color = Color(1, 1, 1, 0.3);
  91. const Color shadow_color = Color(0, 0, 0, 0.3);
  92. // Vertical lines.
  93. _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  94. for (int i = 0; i < frame_count.x - 1; i++) {
  95. const Point2 start = draw_offset + Vector2(i * draw_sep.x + (i + 1) * draw_frame_size.x, 0);
  96. if (separation.x == 0) {
  97. _draw_shadowed_line(split_sheet_preview, start, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  98. } else {
  99. const Size2 size = Size2(draw_sep.x, draw_size.y);
  100. split_sheet_preview->draw_rect(Rect2(start, size), line_color);
  101. }
  102. }
  103. _draw_shadowed_line(split_sheet_preview, draw_offset + Vector2(draw_size.x, 0), Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  104. // Horizontal lines.
  105. _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  106. for (int i = 0; i < frame_count.y - 1; i++) {
  107. const Point2 start = draw_offset + Vector2(0, i * draw_sep.y + (i + 1) * draw_frame_size.y);
  108. if (separation.y == 0) {
  109. _draw_shadowed_line(split_sheet_preview, start, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  110. } else {
  111. const Size2 size = Size2(draw_size.x, draw_sep.y);
  112. split_sheet_preview->draw_rect(Rect2(start, size), line_color);
  113. }
  114. }
  115. _draw_shadowed_line(split_sheet_preview, draw_offset + Vector2(0, draw_size.y), Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  116. if (frames_selected.size() == 0) {
  117. split_sheet_dialog->get_ok_button()->set_disabled(true);
  118. split_sheet_dialog->set_ok_button_text(TTR("No Frames Selected"));
  119. return;
  120. }
  121. Color accent = get_theme_color("accent_color", EditorStringName(Editor));
  122. _sheet_sort_frames();
  123. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  124. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  125. for (int i = 0; i < frames_ordered.size(); ++i) {
  126. const int idx = frames_ordered[i].second;
  127. const int x = idx % frame_count.x;
  128. const int y = idx / frame_count.x;
  129. const Point2 pos = draw_offset + Point2(x, y) * (draw_frame_size + draw_sep);
  130. split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 0.35), true);
  131. split_sheet_preview->draw_rect(Rect2(pos, draw_frame_size), Color(0, 0, 0, 1), false);
  132. split_sheet_preview->draw_rect(Rect2(pos + Size2(1, 1), draw_frame_size - Size2(2, 2)), Color(0, 0, 0, 1), false);
  133. split_sheet_preview->draw_rect(Rect2(pos + Size2(2, 2), draw_frame_size - Size2(4, 4)), accent, false);
  134. split_sheet_preview->draw_rect(Rect2(pos + Size2(3, 3), draw_frame_size - Size2(6, 6)), accent, false);
  135. split_sheet_preview->draw_rect(Rect2(pos + Size2(4, 4), draw_frame_size - Size2(8, 8)), Color(0, 0, 0, 1), false);
  136. split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 1), false);
  137. const String text = itos(i);
  138. const Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  139. // Stop rendering text if too large.
  140. if (string_size.x + 6 < draw_frame_size.x && string_size.y / 2 + 10 < draw_frame_size.y) {
  141. split_sheet_preview->draw_string_outline(font, pos + Size2(5, 7) + Size2(0, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_LEFT, string_size.x, font_size, 1, Color(0, 0, 0, 1));
  142. split_sheet_preview->draw_string(font, pos + Size2(5, 7) + Size2(0, string_size.y / 2), text, HORIZONTAL_ALIGNMENT_LEFT, string_size.x, font_size, Color(1, 1, 1));
  143. }
  144. }
  145. split_sheet_dialog->get_ok_button()->set_disabled(false);
  146. split_sheet_dialog->set_ok_button_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size()));
  147. }
  148. void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
  149. const Ref<InputEventMouseButton> mb = p_event;
  150. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  151. const int idx = _sheet_preview_position_to_frame_index(mb->get_position());
  152. if (idx != -1) {
  153. if (mb->is_shift_pressed() && last_frame_selected >= 0) {
  154. // Select multiple frames.
  155. const int from = last_frame_selected;
  156. const int to = idx;
  157. const int diff = ABS(to - from);
  158. const int dir = SIGN(to - from);
  159. for (int i = 0; i <= diff; i++) {
  160. const int this_idx = from + i * dir;
  161. // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
  162. frames_toggled_by_mouse_hover.insert(this_idx);
  163. if (mb->is_command_or_control_pressed()) {
  164. frames_selected.erase(this_idx);
  165. } else if (!frames_selected.has(this_idx)) {
  166. frames_selected.insert(this_idx, selected_count);
  167. selected_count++;
  168. }
  169. }
  170. } else {
  171. // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
  172. frames_toggled_by_mouse_hover.insert(idx);
  173. if (frames_selected.has(idx)) {
  174. frames_selected.erase(idx);
  175. } else {
  176. frames_selected.insert(idx, selected_count);
  177. selected_count++;
  178. }
  179. }
  180. }
  181. if (last_frame_selected != idx || idx != -1) {
  182. last_frame_selected = idx;
  183. frames_need_sort = true;
  184. split_sheet_preview->queue_redraw();
  185. }
  186. }
  187. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  188. frames_toggled_by_mouse_hover.clear();
  189. }
  190. const Ref<InputEventMouseMotion> mm = p_event;
  191. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  192. // Select by holding down the mouse button on frames.
  193. const int idx = _sheet_preview_position_to_frame_index(mm->get_position());
  194. if (idx != -1 && !frames_toggled_by_mouse_hover.has(idx)) {
  195. // Only allow toggling each tile once per mouse hold.
  196. // Otherwise, the selection would constantly "flicker" in and out when moving the mouse cursor.
  197. // The mouse button must be released before it can be toggled again.
  198. frames_toggled_by_mouse_hover.insert(idx);
  199. if (frames_selected.has(idx)) {
  200. frames_selected.erase(idx);
  201. } else {
  202. frames_selected.insert(idx, selected_count);
  203. selected_count++;
  204. }
  205. last_frame_selected = idx;
  206. frames_need_sort = true;
  207. split_sheet_preview->queue_redraw();
  208. }
  209. }
  210. if (frames_selected.is_empty()) {
  211. selected_count = 0;
  212. }
  213. }
  214. void SpriteFramesEditor::_sheet_scroll_input(const Ref<InputEvent> &p_event) {
  215. const Ref<InputEventMouseButton> mb = p_event;
  216. if (mb.is_valid()) {
  217. // Zoom in/out using Ctrl + mouse wheel. This is done on the ScrollContainer
  218. // to allow performing this action anywhere, even if the cursor isn't
  219. // hovering the texture in the workspace.
  220. // keep CTRL and not CMD_OR_CTRL as CTRL is expected even on MacOS.
  221. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  222. _sheet_zoom_on_position(scale_ratio, mb->get_position());
  223. // Don't scroll up after zooming in.
  224. split_sheet_scroll->accept_event();
  225. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  226. _sheet_zoom_on_position(1 / scale_ratio, mb->get_position());
  227. // Don't scroll down after zooming out.
  228. split_sheet_scroll->accept_event();
  229. }
  230. }
  231. const Ref<InputEventMouseMotion> mm = p_event;
  232. if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
  233. const Vector2 dragged = Input::get_singleton()->warp_mouse_motion(mm, split_sheet_scroll->get_global_rect());
  234. split_sheet_scroll->set_h_scroll(split_sheet_scroll->get_h_scroll() - dragged.x);
  235. split_sheet_scroll->set_v_scroll(split_sheet_scroll->get_v_scroll() - dragged.y);
  236. }
  237. }
  238. void SpriteFramesEditor::_sheet_add_frames() {
  239. const Size2i frame_count = _get_frame_count();
  240. const Size2i frame_size = _get_frame_size();
  241. const Size2i offset = _get_offset();
  242. const Size2i separation = _get_separation();
  243. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  244. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  245. int fc = frames->get_frame_count(edited_anim);
  246. _sheet_sort_frames();
  247. for (const Pair<int, int> &pair : frames_ordered) {
  248. const int idx = pair.second;
  249. const Point2 frame_coords(idx % frame_count.x, idx / frame_count.x);
  250. Ref<AtlasTexture> at;
  251. at.instantiate();
  252. at->set_atlas(split_sheet_preview->get_texture());
  253. at->set_region(Rect2(offset + frame_coords * (frame_size + separation), frame_size));
  254. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, at, 1.0, -1);
  255. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, fc);
  256. }
  257. undo_redo->add_do_method(this, "_update_library");
  258. undo_redo->add_undo_method(this, "_update_library");
  259. undo_redo->commit_action();
  260. }
  261. void SpriteFramesEditor::_sheet_zoom_on_position(float p_zoom, const Vector2 &p_position) {
  262. const float old_zoom = sheet_zoom;
  263. sheet_zoom = CLAMP(sheet_zoom * p_zoom, min_sheet_zoom, max_sheet_zoom);
  264. const Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  265. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  266. Vector2 offset = Vector2(split_sheet_scroll->get_h_scroll(), split_sheet_scroll->get_v_scroll());
  267. offset = (offset + p_position) / old_zoom * sheet_zoom - p_position;
  268. split_sheet_scroll->set_h_scroll(offset.x);
  269. split_sheet_scroll->set_v_scroll(offset.y);
  270. }
  271. void SpriteFramesEditor::_sheet_zoom_in() {
  272. _sheet_zoom_on_position(scale_ratio, Vector2());
  273. }
  274. void SpriteFramesEditor::_sheet_zoom_out() {
  275. _sheet_zoom_on_position(1 / scale_ratio, Vector2());
  276. }
  277. void SpriteFramesEditor::_sheet_zoom_reset() {
  278. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  279. sheet_zoom = MAX(1.0f, EDSCALE);
  280. Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  281. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  282. }
  283. void SpriteFramesEditor::_sheet_order_selected(int p_option) {
  284. frames_need_sort = true;
  285. split_sheet_preview->queue_redraw();
  286. }
  287. void SpriteFramesEditor::_sheet_select_all_frames() {
  288. for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
  289. if (!frames_selected.has(i)) {
  290. frames_selected.insert(i, selected_count);
  291. selected_count++;
  292. frames_need_sort = true;
  293. }
  294. }
  295. split_sheet_preview->queue_redraw();
  296. }
  297. void SpriteFramesEditor::_sheet_clear_all_frames() {
  298. frames_selected.clear();
  299. selected_count = 0;
  300. split_sheet_preview->queue_redraw();
  301. }
  302. void SpriteFramesEditor::_sheet_sort_frames() {
  303. if (!frames_need_sort) {
  304. return;
  305. }
  306. frames_need_sort = false;
  307. frames_ordered.resize(frames_selected.size());
  308. if (frames_selected.is_empty()) {
  309. return;
  310. }
  311. const Size2i frame_count = _get_frame_count();
  312. const int frame_order = split_sheet_order->get_selected_id();
  313. int index = 0;
  314. // Fill based on order.
  315. for (const KeyValue<int, int> &from_pair : frames_selected) {
  316. const int idx = from_pair.key;
  317. const int selection_order = from_pair.value;
  318. // Default to using selection order.
  319. int order_by = selection_order;
  320. // Extract coordinates for sorting.
  321. const int pos_frame_x = idx % frame_count.x;
  322. const int pos_frame_y = idx / frame_count.x;
  323. const int neg_frame_x = frame_count.x - (pos_frame_x + 1);
  324. const int neg_frame_y = frame_count.y - (pos_frame_y + 1);
  325. switch (frame_order) {
  326. case FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM: {
  327. order_by = frame_count.x * pos_frame_y + pos_frame_x;
  328. } break;
  329. case FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP: {
  330. order_by = frame_count.x * neg_frame_y + pos_frame_x;
  331. } break;
  332. case FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM: {
  333. order_by = frame_count.x * pos_frame_y + neg_frame_x;
  334. } break;
  335. case FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP: {
  336. order_by = frame_count.x * neg_frame_y + neg_frame_x;
  337. } break;
  338. case FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT: {
  339. order_by = pos_frame_y + frame_count.y * pos_frame_x;
  340. } break;
  341. case FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT: {
  342. order_by = pos_frame_y + frame_count.y * neg_frame_x;
  343. } break;
  344. case FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT: {
  345. order_by = neg_frame_y + frame_count.y * pos_frame_x;
  346. } break;
  347. case FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT: {
  348. order_by = neg_frame_y + frame_count.y * neg_frame_x;
  349. } break;
  350. }
  351. // Assign in vector.
  352. frames_ordered.set(index, Pair<int, int>(order_by, idx));
  353. index++;
  354. }
  355. // Sort frames.
  356. frames_ordered.sort_custom<PairSort<int, int>>();
  357. }
  358. void SpriteFramesEditor::_sheet_spin_changed(double p_value, int p_dominant_param) {
  359. if (updating_split_settings) {
  360. return;
  361. }
  362. updating_split_settings = true;
  363. if (p_dominant_param != PARAM_USE_CURRENT) {
  364. dominant_param = p_dominant_param;
  365. }
  366. const Size2i texture_size = split_sheet_preview->get_texture()->get_size();
  367. const Size2i size = texture_size - _get_offset();
  368. switch (dominant_param) {
  369. case PARAM_SIZE: {
  370. const Size2i frame_size = _get_frame_size();
  371. const Size2i offset_max = texture_size - frame_size;
  372. split_sheet_offset_x->set_max(offset_max.x);
  373. split_sheet_offset_y->set_max(offset_max.y);
  374. const Size2i sep_max = size - frame_size * 2;
  375. split_sheet_sep_x->set_max(sep_max.x);
  376. split_sheet_sep_y->set_max(sep_max.y);
  377. const Size2i separation = _get_separation();
  378. const Size2i count = (size + separation) / (frame_size + separation);
  379. split_sheet_h->set_value(count.x);
  380. split_sheet_v->set_value(count.y);
  381. } break;
  382. case PARAM_FRAME_COUNT: {
  383. const Size2i count = _get_frame_count();
  384. const Size2i offset_max = texture_size - count;
  385. split_sheet_offset_x->set_max(offset_max.x);
  386. split_sheet_offset_y->set_max(offset_max.y);
  387. const Size2i gap_count = count - Size2i(1, 1);
  388. split_sheet_sep_x->set_max(gap_count.x == 0 ? size.x : (size.x - count.x) / gap_count.x);
  389. split_sheet_sep_y->set_max(gap_count.y == 0 ? size.y : (size.y - count.y) / gap_count.y);
  390. const Size2i separation = _get_separation();
  391. const Size2i frame_size = (size - separation * gap_count) / count;
  392. split_sheet_size_x->set_value(frame_size.x);
  393. split_sheet_size_y->set_value(frame_size.y);
  394. } break;
  395. }
  396. updating_split_settings = false;
  397. frames_selected.clear();
  398. selected_count = 0;
  399. last_frame_selected = -1;
  400. split_sheet_preview->queue_redraw();
  401. }
  402. void SpriteFramesEditor::_toggle_show_settings() {
  403. split_sheet_settings_vb->set_visible(!split_sheet_settings_vb->is_visible());
  404. _update_show_settings();
  405. }
  406. void SpriteFramesEditor::_update_show_settings() {
  407. if (is_layout_rtl()) {
  408. toggle_settings_button->set_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Back") : SNAME("Forward")));
  409. } else {
  410. toggle_settings_button->set_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Forward") : SNAME("Back")));
  411. }
  412. }
  413. void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
  414. Ref<Texture2D> texture = ResourceLoader::load(p_file);
  415. if (texture.is_null()) {
  416. EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
  417. ERR_FAIL_COND(texture.is_null());
  418. }
  419. frames_selected.clear();
  420. selected_count = 0;
  421. last_frame_selected = -1;
  422. bool new_texture = texture != split_sheet_preview->get_texture();
  423. split_sheet_preview->set_texture(texture);
  424. if (new_texture) {
  425. // Reset spin max.
  426. const Size2i size = texture->get_size();
  427. split_sheet_size_x->set_max(size.x);
  428. split_sheet_size_y->set_max(size.y);
  429. split_sheet_sep_x->set_max(size.x);
  430. split_sheet_sep_y->set_max(size.y);
  431. split_sheet_offset_x->set_max(size.x);
  432. split_sheet_offset_y->set_max(size.y);
  433. if (size != previous_texture_size) {
  434. // Different texture, reset to 4x4.
  435. dominant_param = PARAM_FRAME_COUNT;
  436. updating_split_settings = true;
  437. split_sheet_h->set_value(4);
  438. split_sheet_v->set_value(4);
  439. split_sheet_size_x->set_value(size.x / 4);
  440. split_sheet_size_y->set_value(size.y / 4);
  441. split_sheet_sep_x->set_value(0);
  442. split_sheet_sep_y->set_value(0);
  443. split_sheet_offset_x->set_value(0);
  444. split_sheet_offset_y->set_value(0);
  445. updating_split_settings = false;
  446. }
  447. previous_texture_size = size;
  448. // Reset zoom.
  449. _sheet_zoom_reset();
  450. }
  451. split_sheet_dialog->popup_centered_ratio(0.65);
  452. }
  453. void SpriteFramesEditor::_notification(int p_what) {
  454. switch (p_what) {
  455. case NOTIFICATION_ENTER_TREE: {
  456. get_tree()->connect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
  457. [[fallthrough]];
  458. }
  459. case NOTIFICATION_THEME_CHANGED: {
  460. autoplay_icon = get_editor_theme_icon(SNAME("AutoPlay"));
  461. stop_icon = get_editor_theme_icon(SNAME("Stop"));
  462. pause_icon = get_editor_theme_icon(SNAME("Pause"));
  463. _update_stop_icon();
  464. autoplay->set_icon(get_editor_theme_icon(SNAME("AutoPlay")));
  465. anim_loop->set_icon(get_editor_theme_icon(SNAME("Loop")));
  466. play->set_icon(get_editor_theme_icon(SNAME("PlayStart")));
  467. play_from->set_icon(get_editor_theme_icon(SNAME("Play")));
  468. play_bw->set_icon(get_editor_theme_icon(SNAME("PlayStartBackwards")));
  469. play_bw_from->set_icon(get_editor_theme_icon(SNAME("PlayBackwards")));
  470. load->set_icon(get_editor_theme_icon(SNAME("Load")));
  471. load_sheet->set_icon(get_editor_theme_icon(SNAME("SpriteSheet")));
  472. copy->set_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  473. paste->set_icon(get_editor_theme_icon(SNAME("ActionPaste")));
  474. empty_before->set_icon(get_editor_theme_icon(SNAME("InsertBefore")));
  475. empty_after->set_icon(get_editor_theme_icon(SNAME("InsertAfter")));
  476. move_up->set_icon(get_editor_theme_icon(SNAME("MoveLeft")));
  477. move_down->set_icon(get_editor_theme_icon(SNAME("MoveRight")));
  478. delete_frame->set_icon(get_editor_theme_icon(SNAME("Remove")));
  479. zoom_out->set_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  480. zoom_reset->set_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  481. zoom_in->set_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  482. add_anim->set_icon(get_editor_theme_icon(SNAME("New")));
  483. delete_anim->set_icon(get_editor_theme_icon(SNAME("Remove")));
  484. anim_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  485. split_sheet_zoom_out->set_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  486. split_sheet_zoom_reset->set_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  487. split_sheet_zoom_in->set_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  488. split_sheet_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  489. _update_show_settings();
  490. } break;
  491. case NOTIFICATION_READY: {
  492. add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up.
  493. } break;
  494. case NOTIFICATION_EXIT_TREE: {
  495. get_tree()->disconnect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
  496. } break;
  497. }
  498. }
  499. void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_at_pos) {
  500. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  501. List<Ref<Texture2D>> resources;
  502. for (int i = 0; i < p_path.size(); i++) {
  503. Ref<Texture2D> resource;
  504. resource = ResourceLoader::load(p_path[i]);
  505. if (resource.is_null()) {
  506. dialog->set_text(TTR("ERROR: Couldn't load frame resource!"));
  507. dialog->set_title(TTR("Error!"));
  508. //dialog->get_cancel()->set_text("Close");
  509. dialog->set_ok_button_text(TTR("Close"));
  510. dialog->popup_centered();
  511. return; ///beh should show an error i guess
  512. }
  513. resources.push_back(resource);
  514. }
  515. if (resources.is_empty()) {
  516. return;
  517. }
  518. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  519. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  520. int fc = frames->get_frame_count(edited_anim);
  521. int count = 0;
  522. for (const Ref<Texture2D> &E : resources) {
  523. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, E, 1.0, p_at_pos == -1 ? -1 : p_at_pos + count);
  524. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
  525. count++;
  526. }
  527. undo_redo->add_do_method(this, "_update_library");
  528. undo_redo->add_undo_method(this, "_update_library");
  529. undo_redo->commit_action();
  530. }
  531. Size2i SpriteFramesEditor::_get_frame_count() const {
  532. return Size2i(split_sheet_h->get_value(), split_sheet_v->get_value());
  533. }
  534. Size2i SpriteFramesEditor::_get_frame_size() const {
  535. return Size2i(split_sheet_size_x->get_value(), split_sheet_size_y->get_value());
  536. }
  537. Size2i SpriteFramesEditor::_get_offset() const {
  538. return Size2i(split_sheet_offset_x->get_value(), split_sheet_offset_y->get_value());
  539. }
  540. Size2i SpriteFramesEditor::_get_separation() const {
  541. return Size2i(split_sheet_sep_x->get_value(), split_sheet_sep_y->get_value());
  542. }
  543. void SpriteFramesEditor::_load_pressed() {
  544. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  545. loading_scene = false;
  546. file->clear_filters();
  547. List<String> extensions;
  548. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  549. for (const String &extension : extensions) {
  550. file->add_filter("*." + extension);
  551. }
  552. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  553. file->popup_file_dialog();
  554. }
  555. void SpriteFramesEditor::_paste_pressed() {
  556. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  557. Ref<ClipboardSpriteFrames> clipboard_frames = EditorSettings::get_singleton()->get_resource_clipboard();
  558. if (clipboard_frames.is_valid()) {
  559. _paste_frame_array(clipboard_frames);
  560. return;
  561. }
  562. Ref<Texture2D> texture = EditorSettings::get_singleton()->get_resource_clipboard();
  563. if (texture.is_valid()) {
  564. _paste_texture(texture);
  565. return;
  566. }
  567. }
  568. void SpriteFramesEditor::_paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames) {
  569. if (p_clipboard_frames->frames.is_empty()) {
  570. return;
  571. }
  572. Ref<Texture2D> texture;
  573. float duration = 1.0;
  574. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  575. undo_redo->create_action(TTR("Paste Frame(s)"), UndoRedo::MERGE_DISABLE, frames.ptr());
  576. int undo_index = frames->get_frame_count(edited_anim);
  577. for (int index = 0; index < p_clipboard_frames->frames.size(); index++) {
  578. const ClipboardSpriteFrames::Frame &frame = p_clipboard_frames->frames[index];
  579. texture = frame.texture;
  580. duration = frame.duration;
  581. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
  582. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
  583. }
  584. undo_redo->add_do_method(this, "_update_library");
  585. undo_redo->add_undo_method(this, "_update_library");
  586. undo_redo->commit_action();
  587. }
  588. void SpriteFramesEditor::_paste_texture(const Ref<Texture2D> &p_texture) {
  589. float duration = 1.0;
  590. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  591. undo_redo->create_action(TTR("Paste Texture"), UndoRedo::MERGE_DISABLE, frames.ptr());
  592. int undo_index = frames->get_frame_count(edited_anim);
  593. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, p_texture, duration);
  594. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
  595. undo_redo->add_do_method(this, "_update_library");
  596. undo_redo->add_undo_method(this, "_update_library");
  597. undo_redo->commit_action();
  598. }
  599. void SpriteFramesEditor::_copy_pressed() {
  600. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  601. Vector<int> selected_items = frame_list->get_selected_items();
  602. if (selected_items.is_empty()) {
  603. return;
  604. }
  605. Ref<ClipboardSpriteFrames> clipboard_frames = memnew(ClipboardSpriteFrames);
  606. for (const int &frame_index : selected_items) {
  607. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, frame_index);
  608. if (texture.is_null()) {
  609. continue;
  610. }
  611. ClipboardSpriteFrames::Frame frame;
  612. frame.texture = texture;
  613. frame.duration = frames->get_frame_duration(edited_anim, frame_index);
  614. clipboard_frames->frames.push_back(frame);
  615. }
  616. EditorSettings::get_singleton()->set_resource_clipboard(clipboard_frames);
  617. }
  618. void SpriteFramesEditor::_empty_pressed() {
  619. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  620. int from = -1;
  621. Vector<int> selected_items = frame_list->get_selected_items();
  622. if (!selected_items.is_empty()) {
  623. from = selected_items[0];
  624. selection.clear();
  625. selection.push_back(from + 1);
  626. } else {
  627. from = frames->get_frame_count(edited_anim);
  628. }
  629. Ref<Texture2D> texture;
  630. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  631. undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
  632. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from);
  633. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from);
  634. undo_redo->add_do_method(this, "_update_library");
  635. undo_redo->add_undo_method(this, "_update_library");
  636. undo_redo->commit_action();
  637. }
  638. void SpriteFramesEditor::_empty2_pressed() {
  639. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  640. int from = -1;
  641. Vector<int> selected_items = frame_list->get_selected_items();
  642. if (!selected_items.is_empty()) {
  643. from = selected_items[selected_items.size() - 1];
  644. selection.clear();
  645. selection.push_back(from);
  646. } else {
  647. from = frames->get_frame_count(edited_anim);
  648. }
  649. Ref<Texture2D> texture;
  650. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  651. undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
  652. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from + 1);
  653. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from + 1);
  654. undo_redo->add_do_method(this, "_update_library");
  655. undo_redo->add_undo_method(this, "_update_library");
  656. undo_redo->commit_action();
  657. }
  658. void SpriteFramesEditor::_up_pressed() {
  659. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  660. Vector<int> selected_items = frame_list->get_selected_items();
  661. int nb_selected_items = selected_items.size();
  662. if (nb_selected_items <= 0) {
  663. return;
  664. }
  665. int first_selected_frame_index = selected_items[0];
  666. if (first_selected_frame_index < 1) {
  667. return;
  668. }
  669. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  670. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  671. int last_overwritten_frame = -1;
  672. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  673. int to_move = selected_items[selected_index];
  674. int new_index = to_move - 1;
  675. selected_items.set(selected_index, new_index);
  676. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
  677. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
  678. bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == to_move + 1;
  679. if (last_overwritten_frame == -1) {
  680. last_overwritten_frame = new_index;
  681. }
  682. if (!is_next_item_in_selection) {
  683. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, to_move, frames->get_frame_texture(edited_anim, last_overwritten_frame), frames->get_frame_duration(edited_anim, last_overwritten_frame));
  684. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, to_move, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
  685. last_overwritten_frame = -1;
  686. }
  687. }
  688. selection = selected_items;
  689. undo_redo->add_do_method(this, "_update_library");
  690. undo_redo->add_undo_method(this, "_update_library");
  691. undo_redo->commit_action();
  692. }
  693. void SpriteFramesEditor::_down_pressed() {
  694. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  695. Vector<int> selected_items = frame_list->get_selected_items();
  696. int nb_selected_items = selected_items.size();
  697. if (nb_selected_items <= 0) {
  698. return;
  699. }
  700. int last_selected_frame_index = selected_items[nb_selected_items - 1];
  701. if (last_selected_frame_index >= frames->get_frame_count(edited_anim) - 1) {
  702. return;
  703. }
  704. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  705. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  706. int first_moved_frame = -1;
  707. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  708. int to_move = selected_items[selected_index];
  709. int new_index = to_move + 1;
  710. selected_items.set(selected_index, new_index);
  711. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
  712. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
  713. bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == new_index;
  714. if (first_moved_frame == -1) {
  715. first_moved_frame = to_move;
  716. }
  717. if (!is_next_item_in_selection) {
  718. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, first_moved_frame, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
  719. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, first_moved_frame, frames->get_frame_texture(edited_anim, first_moved_frame), frames->get_frame_duration(edited_anim, first_moved_frame));
  720. first_moved_frame = -1;
  721. }
  722. }
  723. selection = selected_items;
  724. undo_redo->add_do_method(this, "_update_library");
  725. undo_redo->add_undo_method(this, "_update_library");
  726. undo_redo->commit_action();
  727. }
  728. void SpriteFramesEditor::_delete_pressed() {
  729. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  730. Vector<int> selected_items = frame_list->get_selected_items();
  731. int nb_selected_items = selected_items.size();
  732. if (nb_selected_items <= 0) {
  733. return;
  734. }
  735. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  736. undo_redo->create_action(TTR("Delete Resource"), UndoRedo::MERGE_DISABLE, frames.ptr());
  737. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  738. int to_delete = selected_items[selected_index];
  739. undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, to_delete - selected_index);
  740. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, frames->get_frame_texture(edited_anim, to_delete), frames->get_frame_duration(edited_anim, to_delete), to_delete);
  741. }
  742. undo_redo->add_do_method(this, "_update_library");
  743. undo_redo->add_undo_method(this, "_update_library");
  744. undo_redo->commit_action();
  745. }
  746. void SpriteFramesEditor::_animation_selected() {
  747. if (updating) {
  748. return;
  749. }
  750. TreeItem *selected = animations->get_selected();
  751. ERR_FAIL_NULL(selected);
  752. edited_anim = selected->get_text(0);
  753. if (animated_sprite) {
  754. sprite_node_updating = true;
  755. animated_sprite->call("set_animation", edited_anim);
  756. sprite_node_updating = false;
  757. }
  758. _update_library(true);
  759. }
  760. void SpriteFramesEditor::_sync_animation() {
  761. if (!animated_sprite || sprite_node_updating) {
  762. return;
  763. }
  764. _select_animation(animated_sprite->call("get_animation"), false);
  765. _update_stop_icon();
  766. }
  767. void SpriteFramesEditor::_select_animation(const String &p_name, bool p_update_node) {
  768. if (!frames->has_animation(p_name)) {
  769. return;
  770. }
  771. edited_anim = p_name;
  772. if (animated_sprite) {
  773. if (p_update_node) {
  774. animated_sprite->call("set_animation", edited_anim);
  775. }
  776. }
  777. _update_library();
  778. }
  779. static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
  780. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  781. if (!edited) {
  782. return;
  783. }
  784. if (p_node != edited && p_node->get_owner() != edited) {
  785. return;
  786. }
  787. {
  788. AnimatedSprite2D *as = Object::cast_to<AnimatedSprite2D>(p_node);
  789. if (as && as->get_sprite_frames() == p_sfames) {
  790. r_nodes->push_back(p_node);
  791. }
  792. }
  793. {
  794. AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
  795. if (as && as->get_sprite_frames() == p_sfames) {
  796. r_nodes->push_back(p_node);
  797. }
  798. }
  799. for (int i = 0; i < p_node->get_child_count(); i++) {
  800. _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
  801. }
  802. }
  803. void SpriteFramesEditor::_animation_name_edited() {
  804. if (updating) {
  805. return;
  806. }
  807. if (!frames->has_animation(edited_anim)) {
  808. return;
  809. }
  810. TreeItem *edited = animations->get_edited();
  811. if (!edited) {
  812. return;
  813. }
  814. String new_name = edited->get_text(0);
  815. if (new_name == String(edited_anim)) {
  816. return;
  817. }
  818. if (new_name.is_empty()) {
  819. new_name = "new_animation";
  820. }
  821. new_name = new_name.replace("/", "_").replace(",", " ");
  822. String name = new_name;
  823. int counter = 0;
  824. while (frames->has_animation(name)) {
  825. if (name == String(edited_anim)) {
  826. edited->set_text(0, name); // The name didn't change, just updated the column text to name.
  827. return;
  828. }
  829. counter++;
  830. name = new_name + "_" + itos(counter);
  831. }
  832. edited->set_text(0, name);
  833. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  834. undo_redo->create_action(TTR("Rename Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  835. undo_redo->add_do_method(frames.ptr(), "rename_animation", edited_anim, name);
  836. undo_redo->add_undo_method(frames.ptr(), "rename_animation", name, edited_anim);
  837. _rename_node_animation(undo_redo, false, edited_anim, name, name);
  838. _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
  839. undo_redo->add_do_method(this, "_select_animation", name);
  840. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  841. undo_redo->add_do_method(this, "_update_library");
  842. undo_redo->add_undo_method(this, "_update_library");
  843. undo_redo->commit_action();
  844. animations->grab_focus();
  845. }
  846. void SpriteFramesEditor::_rename_node_animation(EditorUndoRedoManager *undo_redo, bool is_undo, const String &p_filter, const String &p_new_animation, const String &p_new_autoplay) {
  847. List<Node *> nodes;
  848. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  849. if (is_undo) {
  850. for (Node *E : nodes) {
  851. String current_name = E->call("get_animation");
  852. if (current_name == p_filter) {
  853. undo_redo->force_fixed_history(); // Fixes corner-case when editing SpriteFrames stored as separate file.
  854. undo_redo->add_undo_method(E, "set_animation", p_new_animation);
  855. }
  856. String autoplay_name = E->call("get_autoplay");
  857. if (autoplay_name == p_filter) {
  858. undo_redo->force_fixed_history();
  859. undo_redo->add_undo_method(E, "set_autoplay", p_new_autoplay);
  860. }
  861. }
  862. } else {
  863. for (Node *E : nodes) {
  864. String current_name = E->call("get_animation");
  865. if (current_name == p_filter) {
  866. undo_redo->force_fixed_history();
  867. undo_redo->add_do_method(E, "set_animation", p_new_animation);
  868. }
  869. String autoplay_name = E->call("get_autoplay");
  870. if (autoplay_name == p_filter) {
  871. undo_redo->force_fixed_history();
  872. undo_redo->add_do_method(E, "set_autoplay", p_new_autoplay);
  873. }
  874. }
  875. }
  876. }
  877. void SpriteFramesEditor::_animation_add() {
  878. String name = "new_animation";
  879. int counter = 0;
  880. while (frames->has_animation(name)) {
  881. counter++;
  882. name = vformat("new_animation_%d", counter);
  883. }
  884. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  885. undo_redo->create_action(TTR("Add Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  886. undo_redo->add_do_method(frames.ptr(), "add_animation", name);
  887. undo_redo->add_undo_method(frames.ptr(), "remove_animation", name);
  888. undo_redo->add_do_method(this, "_select_animation", name);
  889. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  890. undo_redo->add_do_method(this, "_update_library");
  891. undo_redo->add_undo_method(this, "_update_library");
  892. undo_redo->commit_action();
  893. animations->grab_focus();
  894. }
  895. void SpriteFramesEditor::_animation_remove() {
  896. if (updating) {
  897. return;
  898. }
  899. if (!frames->has_animation(edited_anim)) {
  900. return;
  901. }
  902. delete_dialog->set_text(TTR("Delete Animation?"));
  903. delete_dialog->popup_centered();
  904. }
  905. void SpriteFramesEditor::_animation_remove_confirmed() {
  906. StringName new_edited;
  907. List<StringName> anim_names;
  908. frames->get_animation_list(&anim_names);
  909. anim_names.sort_custom<StringName::AlphCompare>();
  910. if (anim_names.size() >= 2) {
  911. if (edited_anim == anim_names.get(0)) {
  912. new_edited = anim_names.get(1);
  913. } else {
  914. new_edited = anim_names.get(0);
  915. }
  916. } else {
  917. new_edited = StringName();
  918. }
  919. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  920. undo_redo->create_action(TTR("Remove Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  921. _rename_node_animation(undo_redo, false, edited_anim, new_edited, "");
  922. undo_redo->add_do_method(frames.ptr(), "remove_animation", edited_anim);
  923. undo_redo->add_undo_method(frames.ptr(), "add_animation", edited_anim);
  924. _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
  925. undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  926. undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  927. int fc = frames->get_frame_count(edited_anim);
  928. for (int i = 0; i < fc; i++) {
  929. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
  930. float duration = frames->get_frame_duration(edited_anim, i);
  931. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
  932. }
  933. undo_redo->add_do_method(this, "_select_animation", new_edited);
  934. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  935. undo_redo->add_do_method(this, "_update_library");
  936. undo_redo->add_undo_method(this, "_update_library");
  937. undo_redo->commit_action();
  938. }
  939. void SpriteFramesEditor::_animation_search_text_changed(const String &p_text) {
  940. _update_library();
  941. }
  942. void SpriteFramesEditor::_animation_loop_changed() {
  943. if (updating) {
  944. return;
  945. }
  946. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  947. undo_redo->create_action(TTR("Change Animation Loop"), UndoRedo::MERGE_DISABLE, frames.ptr());
  948. undo_redo->add_do_method(frames.ptr(), "set_animation_loop", edited_anim, anim_loop->is_pressed());
  949. undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  950. undo_redo->add_do_method(this, "_update_library", true);
  951. undo_redo->add_undo_method(this, "_update_library", true);
  952. undo_redo->commit_action();
  953. }
  954. void SpriteFramesEditor::_animation_speed_changed(double p_value) {
  955. if (updating) {
  956. return;
  957. }
  958. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  959. undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS, frames.ptr());
  960. undo_redo->add_do_method(frames.ptr(), "set_animation_speed", edited_anim, p_value);
  961. undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  962. undo_redo->add_do_method(this, "_update_library", true);
  963. undo_redo->add_undo_method(this, "_update_library", true);
  964. undo_redo->commit_action();
  965. }
  966. void SpriteFramesEditor::_frame_list_gui_input(const Ref<InputEvent> &p_event) {
  967. const Ref<InputEventMouseButton> mb = p_event;
  968. if (mb.is_valid()) {
  969. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  970. _zoom_in();
  971. // Don't scroll up after zooming in.
  972. accept_event();
  973. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  974. _zoom_out();
  975. // Don't scroll down after zooming out.
  976. accept_event();
  977. }
  978. }
  979. }
  980. void SpriteFramesEditor::_frame_list_item_selected(int p_index, bool p_selected) {
  981. if (updating) {
  982. return;
  983. }
  984. selection = frame_list->get_selected_items();
  985. if (selection.is_empty() || !p_selected) {
  986. return;
  987. }
  988. updating = true;
  989. frame_duration->set_value(frames->get_frame_duration(edited_anim, selection[0]));
  990. updating = false;
  991. }
  992. void SpriteFramesEditor::_frame_duration_changed(double p_value) {
  993. if (updating) {
  994. return;
  995. }
  996. if (selection.is_empty()) {
  997. return;
  998. }
  999. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1000. undo_redo->create_action(TTR("Set Frame Duration"), UndoRedo::MERGE_ENDS, frames.ptr());
  1001. for (const int &index : selection) {
  1002. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, index);
  1003. float old_duration = frames->get_frame_duration(edited_anim, index);
  1004. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, index, texture, p_value);
  1005. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, index, texture, old_duration);
  1006. }
  1007. undo_redo->add_do_method(this, "_update_library");
  1008. undo_redo->add_undo_method(this, "_update_library");
  1009. undo_redo->commit_action();
  1010. }
  1011. void SpriteFramesEditor::_zoom_in() {
  1012. // Do not zoom in or out with no visible frames
  1013. if (frames->get_frame_count(edited_anim) <= 0) {
  1014. return;
  1015. }
  1016. if (thumbnail_zoom < max_thumbnail_zoom) {
  1017. thumbnail_zoom *= scale_ratio;
  1018. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  1019. frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  1020. frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  1021. }
  1022. }
  1023. void SpriteFramesEditor::_zoom_out() {
  1024. // Do not zoom in or out with no visible frames
  1025. if (frames->get_frame_count(edited_anim) <= 0) {
  1026. return;
  1027. }
  1028. if (thumbnail_zoom > min_thumbnail_zoom) {
  1029. thumbnail_zoom /= scale_ratio;
  1030. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  1031. frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  1032. frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  1033. }
  1034. }
  1035. void SpriteFramesEditor::_zoom_reset() {
  1036. thumbnail_zoom = MAX(1.0f, EDSCALE);
  1037. frame_list->set_fixed_column_width(thumbnail_default_size * 3 / 2);
  1038. frame_list->set_fixed_icon_size(Size2(thumbnail_default_size, thumbnail_default_size));
  1039. }
  1040. void SpriteFramesEditor::_update_library(bool p_skip_selector) {
  1041. if (!p_skip_selector) {
  1042. animations_dirty = true;
  1043. }
  1044. if (pending_update) {
  1045. return;
  1046. }
  1047. pending_update = true;
  1048. callable_mp(this, &SpriteFramesEditor::_update_library_impl).call_deferred();
  1049. }
  1050. void SpriteFramesEditor::_update_library_impl() {
  1051. pending_update = false;
  1052. if (frames.is_null()) {
  1053. return;
  1054. }
  1055. updating = true;
  1056. frame_duration->set_value_no_signal(1.0); // Default.
  1057. if (animations_dirty) {
  1058. animations_dirty = false;
  1059. animations->clear();
  1060. TreeItem *anim_root = animations->create_item();
  1061. List<StringName> anim_names;
  1062. frames->get_animation_list(&anim_names);
  1063. anim_names.sort_custom<StringName::AlphCompare>();
  1064. if (!anim_names.size()) {
  1065. missing_anim_label->show();
  1066. anim_frames_vb->hide();
  1067. return;
  1068. }
  1069. missing_anim_label->hide();
  1070. anim_frames_vb->show();
  1071. bool searching = anim_search_box->get_text().size();
  1072. String searched_string = searching ? anim_search_box->get_text().to_lower() : String();
  1073. TreeItem *selected = nullptr;
  1074. for (const StringName &E : anim_names) {
  1075. String name = E;
  1076. if (searching && !name.to_lower().contains(searched_string)) {
  1077. continue;
  1078. }
  1079. TreeItem *it = animations->create_item(anim_root);
  1080. it->set_metadata(0, name);
  1081. it->set_text(0, name);
  1082. it->set_editable(0, true);
  1083. if (animated_sprite) {
  1084. if (name == String(animated_sprite->call("get_autoplay"))) {
  1085. it->set_icon(0, autoplay_icon);
  1086. }
  1087. }
  1088. if (E == edited_anim) {
  1089. it->select(0);
  1090. selected = it;
  1091. }
  1092. }
  1093. if (selected) {
  1094. animations->scroll_to_item(selected);
  1095. }
  1096. }
  1097. if (animated_sprite) {
  1098. String autoplay_name = animated_sprite->call("get_autoplay");
  1099. if (autoplay_name.is_empty()) {
  1100. autoplay->set_pressed_no_signal(false);
  1101. } else {
  1102. autoplay->set_pressed_no_signal(String(edited_anim) == autoplay_name);
  1103. }
  1104. }
  1105. frame_list->clear();
  1106. if (!frames->has_animation(edited_anim)) {
  1107. updating = false;
  1108. return;
  1109. }
  1110. int anim_frame_count = frames->get_frame_count(edited_anim);
  1111. if (anim_frame_count == 0) {
  1112. selection.clear();
  1113. }
  1114. for (int index = 0; index < selection.size(); index++) {
  1115. int sel = selection[index];
  1116. if (sel == -1) {
  1117. selection.remove_at(index);
  1118. index--;
  1119. }
  1120. if (sel >= anim_frame_count) {
  1121. selection.set(index, anim_frame_count - 1);
  1122. // Since selection is ordered, if we get a frame that is outside of the range
  1123. // we can clip all the other one.
  1124. selection.resize(index + 1);
  1125. break;
  1126. }
  1127. }
  1128. if (selection.is_empty() && frames->get_frame_count(edited_anim)) {
  1129. selection.push_back(0);
  1130. }
  1131. bool is_first_selection = true;
  1132. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  1133. String name = itos(i);
  1134. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
  1135. float duration = frames->get_frame_duration(edited_anim, i);
  1136. if (texture.is_null()) {
  1137. texture = empty_icon;
  1138. name += ": " + TTR("(empty)");
  1139. } else if (!texture->get_name().is_empty()) {
  1140. name += ": " + texture->get_name();
  1141. }
  1142. if (duration != 1.0f) {
  1143. name += String::utf8(" [× ") + String::num(duration, 2) + "]";
  1144. }
  1145. frame_list->add_item(name, texture);
  1146. if (texture.is_valid()) {
  1147. String tooltip = texture->get_path();
  1148. // Frame is often saved as an AtlasTexture subresource within a scene/resource file,
  1149. // thus its path might be not what the user is looking for. So we're also showing
  1150. // subsequent source texture paths.
  1151. String prefix = U"┖╴";
  1152. Ref<AtlasTexture> at = texture;
  1153. while (at.is_valid() && at->get_atlas().is_valid()) {
  1154. tooltip += "\n" + prefix + at->get_atlas()->get_path();
  1155. prefix = " " + prefix;
  1156. at = at->get_atlas();
  1157. }
  1158. frame_list->set_item_tooltip(-1, tooltip);
  1159. }
  1160. if (selection.has(i)) {
  1161. frame_list->select(frame_list->get_item_count() - 1, is_first_selection);
  1162. if (is_first_selection) {
  1163. frame_duration->set_value_no_signal(frames->get_frame_duration(edited_anim, i));
  1164. }
  1165. is_first_selection = false;
  1166. }
  1167. }
  1168. anim_speed->set_value_no_signal(frames->get_animation_speed(edited_anim));
  1169. anim_loop->set_pressed_no_signal(frames->get_animation_loop(edited_anim));
  1170. updating = false;
  1171. }
  1172. void SpriteFramesEditor::_edit() {
  1173. if (!animated_sprite) {
  1174. return;
  1175. }
  1176. edit(animated_sprite->call("get_sprite_frames"));
  1177. }
  1178. void SpriteFramesEditor::edit(Ref<SpriteFrames> p_frames) {
  1179. _update_stop_icon();
  1180. if (p_frames.is_null()) {
  1181. frames.unref();
  1182. _remove_sprite_node();
  1183. hide();
  1184. return;
  1185. }
  1186. frames = p_frames;
  1187. read_only = EditorNode::get_singleton()->is_resource_read_only(p_frames);
  1188. if (!p_frames->has_animation(edited_anim)) {
  1189. List<StringName> anim_names;
  1190. frames->get_animation_list(&anim_names);
  1191. anim_names.sort_custom<StringName::AlphCompare>();
  1192. if (anim_names.size()) {
  1193. edited_anim = anim_names.front()->get();
  1194. } else {
  1195. edited_anim = StringName();
  1196. }
  1197. }
  1198. _update_library();
  1199. // Clear zoom and split sheet texture
  1200. split_sheet_preview->set_texture(Ref<Texture2D>());
  1201. _zoom_reset();
  1202. add_anim->set_disabled(read_only);
  1203. delete_anim->set_disabled(read_only);
  1204. anim_speed->set_editable(!read_only);
  1205. anim_loop->set_disabled(read_only);
  1206. load->set_disabled(read_only);
  1207. load_sheet->set_disabled(read_only);
  1208. copy->set_disabled(read_only);
  1209. paste->set_disabled(read_only);
  1210. empty_before->set_disabled(read_only);
  1211. empty_after->set_disabled(read_only);
  1212. move_up->set_disabled(read_only);
  1213. move_down->set_disabled(read_only);
  1214. delete_frame->set_disabled(read_only);
  1215. _fetch_sprite_node(); // Fetch node after set frames.
  1216. }
  1217. Ref<SpriteFrames> SpriteFramesEditor::get_sprite_frames() const {
  1218. return frames;
  1219. }
  1220. Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1221. if (read_only) {
  1222. return false;
  1223. }
  1224. if (!frames->has_animation(edited_anim)) {
  1225. return false;
  1226. }
  1227. int idx = frame_list->get_item_at_position(p_point, true);
  1228. if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) {
  1229. return Variant();
  1230. }
  1231. Ref<Resource> frame = frames->get_frame_texture(edited_anim, idx);
  1232. if (frame.is_null()) {
  1233. return Variant();
  1234. }
  1235. Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
  1236. drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
  1237. return drag_data;
  1238. }
  1239. bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1240. if (read_only) {
  1241. return false;
  1242. }
  1243. Dictionary d = p_data;
  1244. if (!d.has("type")) {
  1245. return false;
  1246. }
  1247. // reordering frames
  1248. if (d.has("from") && (Object *)(d["from"]) == frame_list) {
  1249. return true;
  1250. }
  1251. if (String(d["type"]) == "resource" && d.has("resource")) {
  1252. Ref<Resource> r = d["resource"];
  1253. Ref<Texture2D> texture = r;
  1254. if (texture.is_valid()) {
  1255. return true;
  1256. }
  1257. }
  1258. if (String(d["type"]) == "files") {
  1259. Vector<String> files = d["files"];
  1260. if (files.size() == 0) {
  1261. return false;
  1262. }
  1263. for (int i = 0; i < files.size(); i++) {
  1264. const String &f = files[i];
  1265. String ftype = EditorFileSystem::get_singleton()->get_file_type(f);
  1266. if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
  1267. return false;
  1268. }
  1269. }
  1270. return true;
  1271. }
  1272. return false;
  1273. }
  1274. void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1275. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1276. return;
  1277. }
  1278. Dictionary d = p_data;
  1279. if (!d.has("type")) {
  1280. return;
  1281. }
  1282. int at_pos = frame_list->get_item_at_position(p_point, true);
  1283. if (String(d["type"]) == "resource" && d.has("resource")) {
  1284. Ref<Resource> r = d["resource"];
  1285. Ref<Texture2D> texture = r;
  1286. if (texture.is_valid()) {
  1287. bool reorder = false;
  1288. if (d.has("from") && (Object *)(d["from"]) == frame_list) {
  1289. reorder = true;
  1290. }
  1291. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1292. if (reorder) { //drop is from reordering frames
  1293. int from_frame = -1;
  1294. float duration = 1.0;
  1295. if (d.has("frame")) {
  1296. from_frame = d["frame"];
  1297. duration = frames->get_frame_duration(edited_anim, from_frame);
  1298. }
  1299. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1300. undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
  1301. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration, at_pos == -1 ? -1 : at_pos);
  1302. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
  1303. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration, from_frame);
  1304. undo_redo->add_do_method(this, "_update_library");
  1305. undo_redo->add_undo_method(this, "_update_library");
  1306. undo_redo->commit_action();
  1307. } else {
  1308. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1309. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, at_pos == -1 ? -1 : at_pos);
  1310. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
  1311. undo_redo->add_do_method(this, "_update_library");
  1312. undo_redo->add_undo_method(this, "_update_library");
  1313. undo_redo->commit_action();
  1314. }
  1315. }
  1316. }
  1317. if (String(d["type"]) == "files") {
  1318. Vector<String> files = d["files"];
  1319. if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1320. _prepare_sprite_sheet(files[0]);
  1321. } else {
  1322. _file_load_request(files, at_pos);
  1323. }
  1324. }
  1325. }
  1326. void SpriteFramesEditor::_update_stop_icon() {
  1327. bool is_playing = false;
  1328. if (animated_sprite) {
  1329. is_playing = animated_sprite->call("is_playing");
  1330. }
  1331. if (is_playing) {
  1332. stop->set_icon(pause_icon);
  1333. } else {
  1334. stop->set_icon(stop_icon);
  1335. }
  1336. }
  1337. void SpriteFramesEditor::_remove_sprite_node() {
  1338. if (!animated_sprite) {
  1339. return;
  1340. }
  1341. if (animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
  1342. animated_sprite->disconnect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
  1343. }
  1344. if (animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
  1345. animated_sprite->disconnect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation));
  1346. }
  1347. if (animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
  1348. animated_sprite->disconnect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
  1349. }
  1350. animated_sprite = nullptr;
  1351. }
  1352. void SpriteFramesEditor::_fetch_sprite_node() {
  1353. Node *selected = nullptr;
  1354. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  1355. if (editor_selection->get_selected_node_list().size() == 1) {
  1356. selected = editor_selection->get_selected_node_list().front()->get();
  1357. }
  1358. bool show_node_edit = false;
  1359. AnimatedSprite2D *as2d = Object::cast_to<AnimatedSprite2D>(selected);
  1360. AnimatedSprite3D *as3d = Object::cast_to<AnimatedSprite3D>(selected);
  1361. if (as2d || as3d) {
  1362. if (frames != selected->call("get_sprite_frames")) {
  1363. _remove_sprite_node();
  1364. } else {
  1365. animated_sprite = selected;
  1366. if (!animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
  1367. animated_sprite->connect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
  1368. }
  1369. if (!animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
  1370. animated_sprite->connect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation), CONNECT_DEFERRED);
  1371. }
  1372. if (!animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
  1373. animated_sprite->connect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
  1374. }
  1375. show_node_edit = true;
  1376. }
  1377. } else {
  1378. _remove_sprite_node();
  1379. }
  1380. if (show_node_edit) {
  1381. _sync_animation();
  1382. autoplay_container->show();
  1383. playback_container->show();
  1384. } else {
  1385. _update_library(); // To init autoplay icon.
  1386. autoplay_container->hide();
  1387. playback_container->hide();
  1388. }
  1389. }
  1390. void SpriteFramesEditor::_play_pressed() {
  1391. if (animated_sprite) {
  1392. animated_sprite->call("stop");
  1393. animated_sprite->call("play", animated_sprite->call("get_animation"));
  1394. }
  1395. _update_stop_icon();
  1396. }
  1397. void SpriteFramesEditor::_play_from_pressed() {
  1398. if (animated_sprite) {
  1399. animated_sprite->call("play", animated_sprite->call("get_animation"));
  1400. }
  1401. _update_stop_icon();
  1402. }
  1403. void SpriteFramesEditor::_play_bw_pressed() {
  1404. if (animated_sprite) {
  1405. animated_sprite->call("stop");
  1406. animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
  1407. }
  1408. _update_stop_icon();
  1409. }
  1410. void SpriteFramesEditor::_play_bw_from_pressed() {
  1411. if (animated_sprite) {
  1412. animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
  1413. }
  1414. _update_stop_icon();
  1415. }
  1416. void SpriteFramesEditor::_stop_pressed() {
  1417. if (animated_sprite) {
  1418. if (animated_sprite->call("is_playing")) {
  1419. animated_sprite->call("pause");
  1420. } else {
  1421. animated_sprite->call("stop");
  1422. }
  1423. }
  1424. _update_stop_icon();
  1425. }
  1426. void SpriteFramesEditor::_autoplay_pressed() {
  1427. if (updating) {
  1428. return;
  1429. }
  1430. if (animated_sprite) {
  1431. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1432. undo_redo->create_action(TTR("Toggle Autoplay"), UndoRedo::MERGE_DISABLE, animated_sprite);
  1433. String current = animated_sprite->call("get_animation");
  1434. String current_auto = animated_sprite->call("get_autoplay");
  1435. if (current == current_auto) {
  1436. //unset
  1437. undo_redo->add_do_method(animated_sprite, "set_autoplay", "");
  1438. undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
  1439. } else {
  1440. //set
  1441. undo_redo->add_do_method(animated_sprite, "set_autoplay", current);
  1442. undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
  1443. }
  1444. undo_redo->add_do_method(this, "_update_library");
  1445. undo_redo->add_undo_method(this, "_update_library");
  1446. undo_redo->commit_action();
  1447. }
  1448. _update_library();
  1449. }
  1450. void SpriteFramesEditor::_bind_methods() {
  1451. ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
  1452. ClassDB::bind_method(D_METHOD("_select_animation", "name", "update_node"), &SpriteFramesEditor::_select_animation, DEFVAL(true));
  1453. }
  1454. void SpriteFramesEditor::_node_removed(Node *p_node) {
  1455. if (animated_sprite) {
  1456. if (animated_sprite != p_node) {
  1457. return;
  1458. }
  1459. _remove_sprite_node();
  1460. }
  1461. }
  1462. SpriteFramesEditor::SpriteFramesEditor() {
  1463. VBoxContainer *vbc_animlist = memnew(VBoxContainer);
  1464. add_child(vbc_animlist);
  1465. vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
  1466. VBoxContainer *sub_vb = memnew(VBoxContainer);
  1467. vbc_animlist->add_margin_child(TTR("Animations:"), sub_vb, true);
  1468. sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1469. HBoxContainer *hbc_animlist = memnew(HBoxContainer);
  1470. sub_vb->add_child(hbc_animlist);
  1471. add_anim = memnew(Button);
  1472. add_anim->set_theme_type_variation("FlatButton");
  1473. hbc_animlist->add_child(add_anim);
  1474. add_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_add));
  1475. delete_anim = memnew(Button);
  1476. delete_anim->set_theme_type_variation("FlatButton");
  1477. hbc_animlist->add_child(delete_anim);
  1478. delete_anim->set_disabled(true);
  1479. delete_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_remove));
  1480. autoplay_container = memnew(HBoxContainer);
  1481. hbc_animlist->add_child(autoplay_container);
  1482. autoplay_container->add_child(memnew(VSeparator));
  1483. autoplay = memnew(Button);
  1484. autoplay->set_theme_type_variation("FlatButton");
  1485. autoplay->set_tooltip_text(TTR("Autoplay on Load"));
  1486. autoplay_container->add_child(autoplay);
  1487. hbc_animlist->add_child(memnew(VSeparator));
  1488. anim_loop = memnew(Button);
  1489. anim_loop->set_toggle_mode(true);
  1490. anim_loop->set_theme_type_variation("FlatButton");
  1491. anim_loop->set_tooltip_text(TTR("Animation Looping"));
  1492. anim_loop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_loop_changed));
  1493. hbc_animlist->add_child(anim_loop);
  1494. anim_speed = memnew(SpinBox);
  1495. anim_speed->set_suffix(TTR("FPS"));
  1496. anim_speed->set_min(0);
  1497. anim_speed->set_max(120);
  1498. anim_speed->set_step(0.01);
  1499. anim_speed->set_custom_arrow_step(1);
  1500. anim_speed->set_tooltip_text(TTR("Animation Speed"));
  1501. anim_speed->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_animation_speed_changed));
  1502. hbc_animlist->add_child(anim_speed);
  1503. anim_search_box = memnew(LineEdit);
  1504. sub_vb->add_child(anim_search_box);
  1505. anim_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1506. anim_search_box->set_placeholder(TTR("Filter Animations"));
  1507. anim_search_box->set_clear_button_enabled(true);
  1508. anim_search_box->connect(SceneStringName(text_changed), callable_mp(this, &SpriteFramesEditor::_animation_search_text_changed));
  1509. animations = memnew(Tree);
  1510. sub_vb->add_child(animations);
  1511. animations->set_v_size_flags(SIZE_EXPAND_FILL);
  1512. animations->set_hide_root(true);
  1513. // HACK: The cell_selected signal is emitted before the FPS spinbox loses focus and applies the change.
  1514. animations->connect("cell_selected", callable_mp(this, &SpriteFramesEditor::_animation_selected), CONNECT_DEFERRED);
  1515. animations->connect("item_edited", callable_mp(this, &SpriteFramesEditor::_animation_name_edited));
  1516. animations->set_allow_reselect(true);
  1517. add_anim->set_shortcut_context(animations);
  1518. add_anim->set_shortcut(ED_SHORTCUT("sprite_frames/new_animation", TTR("Add Animation"), KeyModifierMask::CMD_OR_CTRL | Key::N));
  1519. delete_anim->set_shortcut_context(animations);
  1520. delete_anim->set_shortcut(ED_SHORTCUT("sprite_frames/delete_animation", TTR("Delete Animation"), Key::KEY_DELETE));
  1521. missing_anim_label = memnew(Label);
  1522. missing_anim_label->set_text(TTR("This resource does not have any animations."));
  1523. missing_anim_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1524. missing_anim_label->set_v_size_flags(SIZE_EXPAND_FILL);
  1525. missing_anim_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1526. missing_anim_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1527. missing_anim_label->hide();
  1528. add_child(missing_anim_label);
  1529. anim_frames_vb = memnew(VBoxContainer);
  1530. add_child(anim_frames_vb);
  1531. anim_frames_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1532. anim_frames_vb->hide();
  1533. sub_vb = memnew(VBoxContainer);
  1534. anim_frames_vb->add_margin_child(TTR("Animation Frames:"), sub_vb, true);
  1535. HFlowContainer *hfc = memnew(HFlowContainer);
  1536. sub_vb->add_child(hfc);
  1537. playback_container = memnew(HBoxContainer);
  1538. hfc->add_child(playback_container);
  1539. play_bw_from = memnew(Button);
  1540. play_bw_from->set_theme_type_variation("FlatButton");
  1541. play_bw_from->set_tooltip_text(TTR("Play selected animation backwards from current pos. (A)"));
  1542. playback_container->add_child(play_bw_from);
  1543. play_bw = memnew(Button);
  1544. play_bw->set_theme_type_variation("FlatButton");
  1545. play_bw->set_tooltip_text(TTR("Play selected animation backwards from end. (Shift+A)"));
  1546. playback_container->add_child(play_bw);
  1547. stop = memnew(Button);
  1548. stop->set_theme_type_variation("FlatButton");
  1549. stop->set_tooltip_text(TTR("Pause/stop animation playback. (S)"));
  1550. playback_container->add_child(stop);
  1551. play = memnew(Button);
  1552. play->set_theme_type_variation("FlatButton");
  1553. play->set_tooltip_text(TTR("Play selected animation from start. (Shift+D)"));
  1554. playback_container->add_child(play);
  1555. play_from = memnew(Button);
  1556. play_from->set_theme_type_variation("FlatButton");
  1557. play_from->set_tooltip_text(TTR("Play selected animation from current pos. (D)"));
  1558. playback_container->add_child(play_from);
  1559. playback_container->add_child(memnew(VSeparator));
  1560. autoplay->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_autoplay_pressed));
  1561. autoplay->set_toggle_mode(true);
  1562. play->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_pressed));
  1563. play_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_from_pressed));
  1564. play_bw->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_pressed));
  1565. play_bw_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_from_pressed));
  1566. stop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_stop_pressed));
  1567. HBoxContainer *hbc_actions = memnew(HBoxContainer);
  1568. hfc->add_child(hbc_actions);
  1569. load = memnew(Button);
  1570. load->set_theme_type_variation("FlatButton");
  1571. hbc_actions->add_child(load);
  1572. load_sheet = memnew(Button);
  1573. load_sheet->set_theme_type_variation("FlatButton");
  1574. hbc_actions->add_child(load_sheet);
  1575. hbc_actions->add_child(memnew(VSeparator));
  1576. copy = memnew(Button);
  1577. copy->set_theme_type_variation("FlatButton");
  1578. hbc_actions->add_child(copy);
  1579. paste = memnew(Button);
  1580. paste->set_theme_type_variation("FlatButton");
  1581. hbc_actions->add_child(paste);
  1582. hbc_actions->add_child(memnew(VSeparator));
  1583. empty_before = memnew(Button);
  1584. empty_before->set_theme_type_variation("FlatButton");
  1585. hbc_actions->add_child(empty_before);
  1586. empty_after = memnew(Button);
  1587. empty_after->set_theme_type_variation("FlatButton");
  1588. hbc_actions->add_child(empty_after);
  1589. hbc_actions->add_child(memnew(VSeparator));
  1590. move_up = memnew(Button);
  1591. move_up->set_theme_type_variation("FlatButton");
  1592. hbc_actions->add_child(move_up);
  1593. move_down = memnew(Button);
  1594. move_down->set_theme_type_variation("FlatButton");
  1595. hbc_actions->add_child(move_down);
  1596. delete_frame = memnew(Button);
  1597. delete_frame->set_theme_type_variation("FlatButton");
  1598. hbc_actions->add_child(delete_frame);
  1599. hbc_actions->add_child(memnew(VSeparator));
  1600. HBoxContainer *hbc_frame_duration = memnew(HBoxContainer);
  1601. hfc->add_child(hbc_frame_duration);
  1602. Label *label = memnew(Label);
  1603. label->set_text(TTR("Frame Duration:"));
  1604. hbc_frame_duration->add_child(label);
  1605. frame_duration = memnew(SpinBox);
  1606. frame_duration->set_prefix(String::utf8("×"));
  1607. frame_duration->set_min(SPRITE_FRAME_MINIMUM_DURATION); // Avoid zero div.
  1608. frame_duration->set_max(10);
  1609. frame_duration->set_step(0.01);
  1610. frame_duration->set_custom_arrow_step(0.1);
  1611. frame_duration->set_allow_lesser(false);
  1612. frame_duration->set_allow_greater(true);
  1613. frame_duration->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_frame_duration_changed));
  1614. hbc_frame_duration->add_child(frame_duration);
  1615. // Wide empty separation control. (like BoxContainer::add_spacer())
  1616. Control *c = memnew(Control);
  1617. c->set_mouse_filter(MOUSE_FILTER_PASS);
  1618. c->set_h_size_flags(SIZE_EXPAND_FILL);
  1619. hfc->add_child(c);
  1620. HBoxContainer *hbc_zoom = memnew(HBoxContainer);
  1621. hfc->add_child(hbc_zoom);
  1622. zoom_out = memnew(Button);
  1623. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_out));
  1624. zoom_out->set_flat(true);
  1625. zoom_out->set_tooltip_text(TTR("Zoom Out"));
  1626. hbc_zoom->add_child(zoom_out);
  1627. zoom_reset = memnew(Button);
  1628. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_reset));
  1629. zoom_reset->set_flat(true);
  1630. zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  1631. hbc_zoom->add_child(zoom_reset);
  1632. zoom_in = memnew(Button);
  1633. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_in));
  1634. zoom_in->set_flat(true);
  1635. zoom_in->set_tooltip_text(TTR("Zoom In"));
  1636. hbc_zoom->add_child(zoom_in);
  1637. file = memnew(EditorFileDialog);
  1638. file->connect("files_selected", callable_mp(this, &SpriteFramesEditor::_file_load_request).bind(-1));
  1639. add_child(file);
  1640. frame_list = memnew(ItemList);
  1641. frame_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1642. frame_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1643. frame_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  1644. frame_list->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1645. frame_list->set_select_mode(ItemList::SELECT_MULTI);
  1646. frame_list->set_max_columns(0);
  1647. frame_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  1648. frame_list->set_max_text_lines(2);
  1649. SET_DRAG_FORWARDING_GCD(frame_list, SpriteFramesEditor);
  1650. frame_list->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_frame_list_gui_input));
  1651. // HACK: The item_selected signal is emitted before the Frame Duration spinbox loses focus and applies the change.
  1652. frame_list->connect("multi_selected", callable_mp(this, &SpriteFramesEditor::_frame_list_item_selected), CONNECT_DEFERRED);
  1653. sub_vb->add_child(frame_list);
  1654. dialog = memnew(AcceptDialog);
  1655. add_child(dialog);
  1656. load->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_load_pressed));
  1657. load_sheet->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_open_sprite_sheet));
  1658. delete_frame->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_delete_pressed));
  1659. copy->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_copy_pressed));
  1660. paste->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_paste_pressed));
  1661. empty_before->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty_pressed));
  1662. empty_after->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty2_pressed));
  1663. move_up->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_up_pressed));
  1664. move_down->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_down_pressed));
  1665. load->set_shortcut_context(frame_list);
  1666. load->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_file", TTR("Add frame from file"), KeyModifierMask::CMD_OR_CTRL | Key::O));
  1667. load_sheet->set_shortcut_context(frame_list);
  1668. load_sheet->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_sheet", TTR("Add frames from sprite sheet"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O));
  1669. delete_frame->set_shortcut_context(frame_list);
  1670. delete_frame->set_shortcut(ED_SHORTCUT("sprite_frames/delete", TTR("Delete Frame"), Key::KEY_DELETE));
  1671. copy->set_shortcut_context(frame_list);
  1672. copy->set_shortcut(ED_SHORTCUT("sprite_frames/copy", TTR("Copy Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  1673. paste->set_shortcut_context(frame_list);
  1674. paste->set_shortcut(ED_SHORTCUT("sprite_frames/paste", TTR("Paste Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::V));
  1675. empty_before->set_shortcut_context(frame_list);
  1676. empty_before->set_shortcut(ED_SHORTCUT("sprite_frames/empty_before", TTR("Insert Empty (Before Selected)"), KeyModifierMask::ALT | Key::LEFT));
  1677. empty_after->set_shortcut_context(frame_list);
  1678. empty_after->set_shortcut(ED_SHORTCUT("sprite_frames/empty_after", TTR("Insert Empty (After Selected)"), KeyModifierMask::ALT | Key::RIGHT));
  1679. move_up->set_shortcut_context(frame_list);
  1680. move_up->set_shortcut(ED_SHORTCUT("sprite_frames/move_left", TTR("Move Frame Left"), KeyModifierMask::CMD_OR_CTRL | Key::LEFT));
  1681. move_down->set_shortcut_context(frame_list);
  1682. move_down->set_shortcut(ED_SHORTCUT("sprite_frames/move_right", TTR("Move Frame Right"), KeyModifierMask::CMD_OR_CTRL | Key::RIGHT));
  1683. zoom_out->set_shortcut_context(frame_list);
  1684. zoom_out->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_out", TTR("Zoom Out"),
  1685. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) }));
  1686. zoom_in->set_shortcut_context(frame_list);
  1687. zoom_in->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_in", TTR("Zoom In"),
  1688. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) }));
  1689. loading_scene = false;
  1690. updating = false;
  1691. edited_anim = "default";
  1692. delete_dialog = memnew(ConfirmationDialog);
  1693. add_child(delete_dialog);
  1694. delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_animation_remove_confirmed));
  1695. split_sheet_dialog = memnew(ConfirmationDialog);
  1696. add_child(split_sheet_dialog);
  1697. split_sheet_dialog->set_title(TTR("Select Frames"));
  1698. split_sheet_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_sheet_add_frames));
  1699. HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
  1700. split_sheet_dialog->add_child(split_sheet_hb);
  1701. split_sheet_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1702. split_sheet_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1703. VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
  1704. split_sheet_hb->add_child(split_sheet_vb);
  1705. split_sheet_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1706. split_sheet_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1707. HBoxContainer *split_sheet_menu_hb = memnew(HBoxContainer);
  1708. split_sheet_menu_hb->add_child(memnew(Label(TTR("Frame Order"))));
  1709. split_sheet_order = memnew(OptionButton);
  1710. split_sheet_order->add_item(TTR("As Selected"), FRAME_ORDER_SELECTION);
  1711. split_sheet_order->add_separator(TTR("By Row"));
  1712. split_sheet_order->add_item(TTR("Left to Right, Top to Bottom"), FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM);
  1713. split_sheet_order->add_item(TTR("Left to Right, Bottom to Top"), FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP);
  1714. split_sheet_order->add_item(TTR("Right to Left, Top to Bottom"), FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM);
  1715. split_sheet_order->add_item(TTR("Right to Left, Bottom to Top"), FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP);
  1716. split_sheet_order->add_separator(TTR("By Column"));
  1717. split_sheet_order->add_item(TTR("Top to Bottom, Left to Right"), FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT);
  1718. split_sheet_order->add_item(TTR("Top to Bottom, Right to Left"), FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT);
  1719. split_sheet_order->add_item(TTR("Bottom to Top, Left to Right"), FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT);
  1720. split_sheet_order->add_item(TTR("Bottom to Top, Right to Left"), FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT);
  1721. split_sheet_order->connect(SceneStringName(item_selected), callable_mp(this, &SpriteFramesEditor::_sheet_order_selected));
  1722. split_sheet_menu_hb->add_child(split_sheet_order);
  1723. Button *select_all = memnew(Button);
  1724. select_all->set_text(TTR("Select All"));
  1725. select_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_select_all_frames));
  1726. split_sheet_menu_hb->add_child(select_all);
  1727. Button *clear_all = memnew(Button);
  1728. clear_all->set_text(TTR("Select None"));
  1729. clear_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_clear_all_frames));
  1730. split_sheet_menu_hb->add_child(clear_all);
  1731. split_sheet_menu_hb->add_spacer();
  1732. toggle_settings_button = memnew(Button);
  1733. toggle_settings_button->set_h_size_flags(SIZE_SHRINK_END);
  1734. toggle_settings_button->set_theme_type_variation("FlatButton");
  1735. toggle_settings_button->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_toggle_show_settings));
  1736. toggle_settings_button->set_tooltip_text(TTR("Toggle Settings Panel"));
  1737. split_sheet_menu_hb->add_child(toggle_settings_button);
  1738. split_sheet_vb->add_child(split_sheet_menu_hb);
  1739. PanelContainer *split_sheet_panel = memnew(PanelContainer);
  1740. split_sheet_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1741. split_sheet_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1742. split_sheet_vb->add_child(split_sheet_panel);
  1743. split_sheet_preview = memnew(TextureRect);
  1744. split_sheet_preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1745. split_sheet_preview->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1746. split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
  1747. split_sheet_preview->connect(SceneStringName(draw), callable_mp(this, &SpriteFramesEditor::_sheet_preview_draw));
  1748. split_sheet_preview->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_preview_input));
  1749. split_sheet_scroll = memnew(ScrollContainer);
  1750. split_sheet_scroll->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_scroll_input));
  1751. split_sheet_panel->add_child(split_sheet_scroll);
  1752. CenterContainer *cc = memnew(CenterContainer);
  1753. cc->add_child(split_sheet_preview);
  1754. cc->set_h_size_flags(SIZE_EXPAND_FILL);
  1755. cc->set_v_size_flags(SIZE_EXPAND_FILL);
  1756. split_sheet_scroll->add_child(cc);
  1757. MarginContainer *split_sheet_zoom_margin = memnew(MarginContainer);
  1758. split_sheet_panel->add_child(split_sheet_zoom_margin);
  1759. split_sheet_zoom_margin->set_h_size_flags(0);
  1760. split_sheet_zoom_margin->set_v_size_flags(0);
  1761. split_sheet_zoom_margin->add_theme_constant_override("margin_top", 5);
  1762. split_sheet_zoom_margin->add_theme_constant_override("margin_left", 5);
  1763. HBoxContainer *split_sheet_zoom_hb = memnew(HBoxContainer);
  1764. split_sheet_zoom_margin->add_child(split_sheet_zoom_hb);
  1765. split_sheet_zoom_out = memnew(Button);
  1766. split_sheet_zoom_out->set_theme_type_variation("FlatButton");
  1767. split_sheet_zoom_out->set_focus_mode(FOCUS_NONE);
  1768. split_sheet_zoom_out->set_tooltip_text(TTR("Zoom Out"));
  1769. split_sheet_zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_out));
  1770. split_sheet_zoom_hb->add_child(split_sheet_zoom_out);
  1771. split_sheet_zoom_reset = memnew(Button);
  1772. split_sheet_zoom_reset->set_theme_type_variation("FlatButton");
  1773. split_sheet_zoom_reset->set_focus_mode(FOCUS_NONE);
  1774. split_sheet_zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  1775. split_sheet_zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_reset));
  1776. split_sheet_zoom_hb->add_child(split_sheet_zoom_reset);
  1777. split_sheet_zoom_in = memnew(Button);
  1778. split_sheet_zoom_in->set_theme_type_variation("FlatButton");
  1779. split_sheet_zoom_in->set_focus_mode(FOCUS_NONE);
  1780. split_sheet_zoom_in->set_tooltip_text(TTR("Zoom In"));
  1781. split_sheet_zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_in));
  1782. split_sheet_zoom_hb->add_child(split_sheet_zoom_in);
  1783. split_sheet_settings_vb = memnew(VBoxContainer);
  1784. split_sheet_settings_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1785. HBoxContainer *split_sheet_h_hb = memnew(HBoxContainer);
  1786. split_sheet_h_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1787. Label *split_sheet_h_label = memnew(Label(TTR("Horizontal")));
  1788. split_sheet_h_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1789. split_sheet_h_hb->add_child(split_sheet_h_label);
  1790. split_sheet_h = memnew(SpinBox);
  1791. split_sheet_h->set_h_size_flags(SIZE_EXPAND_FILL);
  1792. split_sheet_h->set_min(1);
  1793. split_sheet_h->set_max(128);
  1794. split_sheet_h->set_step(1);
  1795. split_sheet_h->set_select_all_on_focus(true);
  1796. split_sheet_h_hb->add_child(split_sheet_h);
  1797. split_sheet_h->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  1798. split_sheet_settings_vb->add_child(split_sheet_h_hb);
  1799. HBoxContainer *split_sheet_v_hb = memnew(HBoxContainer);
  1800. split_sheet_v_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1801. Label *split_sheet_v_label = memnew(Label(TTR("Vertical")));
  1802. split_sheet_v_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1803. split_sheet_v_hb->add_child(split_sheet_v_label);
  1804. split_sheet_v = memnew(SpinBox);
  1805. split_sheet_v->set_h_size_flags(SIZE_EXPAND_FILL);
  1806. split_sheet_v->set_min(1);
  1807. split_sheet_v->set_max(128);
  1808. split_sheet_v->set_step(1);
  1809. split_sheet_v->set_select_all_on_focus(true);
  1810. split_sheet_v_hb->add_child(split_sheet_v);
  1811. split_sheet_v->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  1812. split_sheet_settings_vb->add_child(split_sheet_v_hb);
  1813. HBoxContainer *split_sheet_size_hb = memnew(HBoxContainer);
  1814. split_sheet_size_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1815. Label *split_sheet_size_label = memnew(Label(TTR("Size")));
  1816. split_sheet_size_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1817. split_sheet_size_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1818. split_sheet_size_hb->add_child(split_sheet_size_label);
  1819. VBoxContainer *split_sheet_size_vb = memnew(VBoxContainer);
  1820. split_sheet_size_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1821. split_sheet_size_x = memnew(SpinBox);
  1822. split_sheet_size_x->set_h_size_flags(SIZE_EXPAND_FILL);
  1823. split_sheet_size_x->set_min(1);
  1824. split_sheet_size_x->set_step(1);
  1825. split_sheet_size_x->set_suffix("px");
  1826. split_sheet_size_x->set_select_all_on_focus(true);
  1827. split_sheet_size_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  1828. split_sheet_size_vb->add_child(split_sheet_size_x);
  1829. split_sheet_size_y = memnew(SpinBox);
  1830. split_sheet_size_y->set_h_size_flags(SIZE_EXPAND_FILL);
  1831. split_sheet_size_y->set_min(1);
  1832. split_sheet_size_y->set_step(1);
  1833. split_sheet_size_y->set_suffix("px");
  1834. split_sheet_size_y->set_select_all_on_focus(true);
  1835. split_sheet_size_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  1836. split_sheet_size_vb->add_child(split_sheet_size_y);
  1837. split_sheet_size_hb->add_child(split_sheet_size_vb);
  1838. split_sheet_settings_vb->add_child(split_sheet_size_hb);
  1839. HBoxContainer *split_sheet_sep_hb = memnew(HBoxContainer);
  1840. split_sheet_sep_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1841. Label *split_sheet_sep_label = memnew(Label(TTR("Separation")));
  1842. split_sheet_sep_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1843. split_sheet_sep_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1844. split_sheet_sep_hb->add_child(split_sheet_sep_label);
  1845. VBoxContainer *split_sheet_sep_vb = memnew(VBoxContainer);
  1846. split_sheet_sep_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1847. split_sheet_sep_x = memnew(SpinBox);
  1848. split_sheet_sep_x->set_min(0);
  1849. split_sheet_sep_x->set_step(1);
  1850. split_sheet_sep_x->set_suffix("px");
  1851. split_sheet_sep_x->set_select_all_on_focus(true);
  1852. split_sheet_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1853. split_sheet_sep_vb->add_child(split_sheet_sep_x);
  1854. split_sheet_sep_y = memnew(SpinBox);
  1855. split_sheet_sep_y->set_min(0);
  1856. split_sheet_sep_y->set_step(1);
  1857. split_sheet_sep_y->set_suffix("px");
  1858. split_sheet_sep_y->set_select_all_on_focus(true);
  1859. split_sheet_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1860. split_sheet_sep_vb->add_child(split_sheet_sep_y);
  1861. split_sheet_sep_hb->add_child(split_sheet_sep_vb);
  1862. split_sheet_settings_vb->add_child(split_sheet_sep_hb);
  1863. HBoxContainer *split_sheet_offset_hb = memnew(HBoxContainer);
  1864. split_sheet_offset_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1865. Label *split_sheet_offset_label = memnew(Label(TTR("Offset")));
  1866. split_sheet_offset_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1867. split_sheet_offset_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1868. split_sheet_offset_hb->add_child(split_sheet_offset_label);
  1869. VBoxContainer *split_sheet_offset_vb = memnew(VBoxContainer);
  1870. split_sheet_offset_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1871. split_sheet_offset_x = memnew(SpinBox);
  1872. split_sheet_offset_x->set_min(0);
  1873. split_sheet_offset_x->set_step(1);
  1874. split_sheet_offset_x->set_suffix("px");
  1875. split_sheet_offset_x->set_select_all_on_focus(true);
  1876. split_sheet_offset_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1877. split_sheet_offset_vb->add_child(split_sheet_offset_x);
  1878. split_sheet_offset_y = memnew(SpinBox);
  1879. split_sheet_offset_y->set_min(0);
  1880. split_sheet_offset_y->set_step(1);
  1881. split_sheet_offset_y->set_suffix("px");
  1882. split_sheet_offset_y->set_select_all_on_focus(true);
  1883. split_sheet_offset_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1884. split_sheet_offset_vb->add_child(split_sheet_offset_y);
  1885. split_sheet_offset_hb->add_child(split_sheet_offset_vb);
  1886. split_sheet_settings_vb->add_child(split_sheet_offset_hb);
  1887. split_sheet_hb->add_child(split_sheet_settings_vb);
  1888. file_split_sheet = memnew(EditorFileDialog);
  1889. file_split_sheet->set_title(TTR("Create Frames from Sprite Sheet"));
  1890. file_split_sheet->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1891. add_child(file_split_sheet);
  1892. file_split_sheet->connect("file_selected", callable_mp(this, &SpriteFramesEditor::_prepare_sprite_sheet));
  1893. // Config scale.
  1894. scale_ratio = 1.2f;
  1895. thumbnail_default_size = 96 * MAX(1, EDSCALE);
  1896. thumbnail_zoom = MAX(1.0f, EDSCALE);
  1897. max_thumbnail_zoom = 8.0f * MAX(1.0f, EDSCALE);
  1898. min_thumbnail_zoom = 0.1f * MAX(1.0f, EDSCALE);
  1899. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  1900. sheet_zoom = MAX(1.0f, EDSCALE);
  1901. max_sheet_zoom = 128.0f * MAX(1.0f, EDSCALE);
  1902. min_sheet_zoom = 0.01f * MAX(1.0f, EDSCALE);
  1903. _zoom_reset();
  1904. // Ensure the anim search box is wide enough by default.
  1905. // Not by setting its minimum size so it can still be shrunk if desired.
  1906. set_split_offset(56 * EDSCALE);
  1907. }
  1908. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  1909. Ref<SpriteFrames> s;
  1910. AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object);
  1911. if (animated_sprite) {
  1912. s = animated_sprite->get_sprite_frames();
  1913. } else {
  1914. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  1915. if (animated_sprite_3d) {
  1916. s = animated_sprite_3d->get_sprite_frames();
  1917. } else {
  1918. s = p_object;
  1919. }
  1920. }
  1921. frames_editor->edit(s);
  1922. }
  1923. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  1924. AnimatedSprite2D *animated_sprite_2d = Object::cast_to<AnimatedSprite2D>(p_object);
  1925. if (animated_sprite_2d && *animated_sprite_2d->get_sprite_frames()) {
  1926. return true;
  1927. }
  1928. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  1929. if (animated_sprite_3d && *animated_sprite_3d->get_sprite_frames()) {
  1930. return true;
  1931. }
  1932. SpriteFrames *frames = Object::cast_to<SpriteFrames>(p_object);
  1933. if (frames && (frames_editor->get_sprite_frames().is_null() || frames_editor->get_sprite_frames() == frames)) {
  1934. return true;
  1935. }
  1936. return false;
  1937. }
  1938. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  1939. if (p_visible) {
  1940. button->show();
  1941. EditorNode::get_bottom_panel()->make_item_visible(frames_editor);
  1942. } else {
  1943. button->hide();
  1944. if (frames_editor->is_visible_in_tree()) {
  1945. EditorNode::get_bottom_panel()->hide_bottom_panel();
  1946. }
  1947. }
  1948. }
  1949. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin() {
  1950. frames_editor = memnew(SpriteFramesEditor);
  1951. frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  1952. button = EditorNode::get_bottom_panel()->add_item(TTR("SpriteFrames"), frames_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_sprite_frames_bottom_panel", TTR("Toggle SpriteFrames Bottom Panel")));
  1953. button->hide();
  1954. }
  1955. SpriteFramesEditorPlugin::~SpriteFramesEditorPlugin() {
  1956. }