sprite_frames_editor_plugin.cpp 94 KB

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