sprite_frames_editor_plugin.cpp 94 KB

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