sprite_frames_editor_plugin.cpp 105 KB

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