sprite_frames_editor_plugin.cpp 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592
  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/docks/filesystem_dock.h"
  34. #include "editor/docks/scene_tree_dock.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/file_system/editor_file_system.h"
  39. #include "editor/gui/editor_bottom_panel.h"
  40. #include "editor/gui/editor_file_dialog.h"
  41. #include "editor/settings/editor_command_palette.h"
  42. #include "editor/settings/editor_settings.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/2d/animated_sprite_2d.h"
  45. #include "scene/3d/sprite_3d.h"
  46. #include "scene/gui/center_container.h"
  47. #include "scene/gui/flow_container.h"
  48. #include "scene/gui/margin_container.h"
  49. #include "scene/gui/option_button.h"
  50. #include "scene/gui/panel_container.h"
  51. #include "scene/gui/separator.h"
  52. #include "scene/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.is_empty()) {
  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. if (image->is_compressed()) {
  447. image = image->duplicate();
  448. ERR_FAIL_COND_V(image->decompress() != OK, p_texture->get_size());
  449. }
  450. Size2i size = image->get_size();
  451. Color assumed_background_color = image->get_pixel(0, 0);
  452. Size2i sheet_size;
  453. bool previous_line_background = true;
  454. for (int x = 0; x < size.x; x++) {
  455. int y = 0;
  456. while (y < size.y && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) {
  457. y++;
  458. }
  459. bool current_line_background = (y == size.y);
  460. if (previous_line_background && !current_line_background) {
  461. sheet_size.x++;
  462. }
  463. previous_line_background = current_line_background;
  464. }
  465. previous_line_background = true;
  466. for (int y = 0; y < size.y; y++) {
  467. int x = 0;
  468. while (x < size.x && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) {
  469. x++;
  470. }
  471. bool current_line_background = (x == size.x);
  472. if (previous_line_background && !current_line_background) {
  473. sheet_size.y++;
  474. }
  475. previous_line_background = current_line_background;
  476. }
  477. if (sheet_size == Size2i(0, 0) || sheet_size == Size2i(1, 1)) {
  478. sheet_size = Size2i(4, 4);
  479. }
  480. return sheet_size;
  481. }
  482. void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
  483. Ref<Texture2D> texture = ResourceLoader::load(p_file);
  484. if (texture.is_null()) {
  485. EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
  486. ERR_FAIL_COND(texture.is_null());
  487. }
  488. frames_selected.clear();
  489. selected_count = 0;
  490. last_frame_selected = -1;
  491. bool new_texture = texture != split_sheet_preview->get_texture();
  492. split_sheet_preview->set_texture(texture);
  493. if (new_texture) {
  494. // Reset spin max.
  495. const Size2i size = texture->get_size();
  496. split_sheet_size_x->set_max(size.x);
  497. split_sheet_size_y->set_max(size.y);
  498. split_sheet_sep_x->set_max(size.x);
  499. split_sheet_sep_y->set_max(size.y);
  500. split_sheet_offset_x->set_max(size.x);
  501. split_sheet_offset_y->set_max(size.y);
  502. if (size != previous_texture_size) {
  503. // Different texture, reset to 4x4.
  504. dominant_param = PARAM_FRAME_COUNT;
  505. updating_split_settings = true;
  506. const Size2i split_sheet = Size2i(4, 4);
  507. split_sheet_h->set_value(split_sheet.x);
  508. split_sheet_v->set_value(split_sheet.y);
  509. split_sheet_size_x->set_value(size.x / split_sheet.x);
  510. split_sheet_size_y->set_value(size.y / split_sheet.y);
  511. split_sheet_sep_x->set_value(0);
  512. split_sheet_sep_y->set_value(0);
  513. split_sheet_offset_x->set_value(0);
  514. split_sheet_offset_y->set_value(0);
  515. updating_split_settings = false;
  516. }
  517. previous_texture_size = size;
  518. // Reset zoom.
  519. _sheet_zoom_reset();
  520. }
  521. split_sheet_dialog->popup_centered_ratio(0.65);
  522. }
  523. void SpriteFramesEditor::_notification(int p_what) {
  524. switch (p_what) {
  525. case NOTIFICATION_ENTER_TREE: {
  526. get_tree()->connect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
  527. [[fallthrough]];
  528. }
  529. case NOTIFICATION_THEME_CHANGED: {
  530. autoplay_icon = get_editor_theme_icon(SNAME("AutoPlay"));
  531. stop_icon = get_editor_theme_icon(SNAME("Stop"));
  532. pause_icon = get_editor_theme_icon(SNAME("Pause"));
  533. _update_stop_icon();
  534. autoplay->set_button_icon(get_editor_theme_icon(SNAME("AutoPlay")));
  535. anim_loop->set_button_icon(get_editor_theme_icon(SNAME("Loop")));
  536. play->set_button_icon(get_editor_theme_icon(SNAME("PlayStart")));
  537. play_from->set_button_icon(get_editor_theme_icon(SNAME("Play")));
  538. play_bw->set_button_icon(get_editor_theme_icon(SNAME("PlayStartBackwards")));
  539. play_bw_from->set_button_icon(get_editor_theme_icon(SNAME("PlayBackwards")));
  540. load->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  541. load_sheet->set_button_icon(get_editor_theme_icon(SNAME("SpriteSheet")));
  542. copy->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  543. paste->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
  544. empty_before->set_button_icon(get_editor_theme_icon(SNAME("InsertBefore")));
  545. empty_after->set_button_icon(get_editor_theme_icon(SNAME("InsertAfter")));
  546. move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveLeft")));
  547. move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveRight")));
  548. delete_frame->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  549. zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  550. zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  551. zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  552. add_anim->set_button_icon(get_editor_theme_icon(SNAME("New")));
  553. duplicate_anim->set_button_icon(get_editor_theme_icon(SNAME("Duplicate")));
  554. delete_anim->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  555. anim_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  556. split_sheet_zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  557. split_sheet_zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  558. split_sheet_zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  559. split_sheet_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  560. _update_show_settings();
  561. } break;
  562. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  563. case NOTIFICATION_TRANSLATION_CHANGED: {
  564. _update_show_settings();
  565. } break;
  566. case NOTIFICATION_READY: {
  567. add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up.
  568. } break;
  569. case NOTIFICATION_EXIT_TREE: {
  570. get_tree()->disconnect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
  571. } break;
  572. }
  573. }
  574. void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_at_pos) {
  575. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  576. List<Ref<Texture2D>> resources;
  577. for (int i = 0; i < p_path.size(); i++) {
  578. Ref<Texture2D> resource;
  579. resource = ResourceLoader::load(p_path[i]);
  580. if (resource.is_null()) {
  581. dialog->set_text(TTR("ERROR: Couldn't load frame resource!"));
  582. dialog->set_title(TTR("Error!"));
  583. //dialog->get_cancel()->set_text("Close");
  584. dialog->set_ok_button_text(TTR("Close"));
  585. dialog->popup_centered();
  586. return; ///beh should show an error i guess
  587. }
  588. resources.push_back(resource);
  589. }
  590. if (resources.is_empty()) {
  591. return;
  592. }
  593. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  594. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  595. int fc = frames->get_frame_count(edited_anim);
  596. int count = 0;
  597. for (const Ref<Texture2D> &E : resources) {
  598. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, E, 1.0, p_at_pos == -1 ? -1 : p_at_pos + count);
  599. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
  600. count++;
  601. }
  602. undo_redo->add_do_method(this, "_update_library");
  603. undo_redo->add_undo_method(this, "_update_library");
  604. undo_redo->commit_action();
  605. }
  606. Size2i SpriteFramesEditor::_get_frame_count() const {
  607. return Size2i(split_sheet_h->get_value(), split_sheet_v->get_value());
  608. }
  609. Size2i SpriteFramesEditor::_get_frame_size() const {
  610. return Size2i(split_sheet_size_x->get_value(), split_sheet_size_y->get_value());
  611. }
  612. Size2i SpriteFramesEditor::_get_offset() const {
  613. return Size2i(split_sheet_offset_x->get_value(), split_sheet_offset_y->get_value());
  614. }
  615. Size2i SpriteFramesEditor::_get_separation() const {
  616. return Size2i(split_sheet_sep_x->get_value(), split_sheet_sep_y->get_value());
  617. }
  618. void SpriteFramesEditor::_load_pressed() {
  619. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  620. loading_scene = false;
  621. file->clear_filters();
  622. List<String> extensions;
  623. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  624. for (const String &extension : extensions) {
  625. file->add_filter("*." + extension);
  626. }
  627. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  628. file->popup_file_dialog();
  629. }
  630. void SpriteFramesEditor::_paste_pressed() {
  631. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  632. Ref<ClipboardSpriteFrames> clipboard_frames = EditorSettings::get_singleton()->get_resource_clipboard();
  633. if (clipboard_frames.is_valid()) {
  634. _paste_frame_array(clipboard_frames);
  635. return;
  636. }
  637. Ref<Texture2D> texture = EditorSettings::get_singleton()->get_resource_clipboard();
  638. if (texture.is_valid()) {
  639. _paste_texture(texture);
  640. return;
  641. }
  642. }
  643. void SpriteFramesEditor::_paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames) {
  644. if (p_clipboard_frames->frames.is_empty()) {
  645. return;
  646. }
  647. Ref<Texture2D> texture;
  648. float duration = 1.0;
  649. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  650. undo_redo->create_action(TTR("Paste Frame(s)"), UndoRedo::MERGE_DISABLE, frames.ptr());
  651. int undo_index = frames->get_frame_count(edited_anim);
  652. for (int index = 0; index < p_clipboard_frames->frames.size(); index++) {
  653. const ClipboardSpriteFrames::Frame &frame = p_clipboard_frames->frames[index];
  654. texture = frame.texture;
  655. duration = frame.duration;
  656. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
  657. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
  658. }
  659. undo_redo->add_do_method(this, "_update_library");
  660. undo_redo->add_undo_method(this, "_update_library");
  661. undo_redo->commit_action();
  662. }
  663. void SpriteFramesEditor::_paste_texture(const Ref<Texture2D> &p_texture) {
  664. float duration = 1.0;
  665. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  666. undo_redo->create_action(TTR("Paste Texture"), UndoRedo::MERGE_DISABLE, frames.ptr());
  667. int undo_index = frames->get_frame_count(edited_anim);
  668. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, p_texture, duration);
  669. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
  670. undo_redo->add_do_method(this, "_update_library");
  671. undo_redo->add_undo_method(this, "_update_library");
  672. undo_redo->commit_action();
  673. }
  674. void SpriteFramesEditor::_copy_pressed() {
  675. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  676. Vector<int> selected_items = frame_list->get_selected_items();
  677. if (selected_items.is_empty()) {
  678. return;
  679. }
  680. Ref<ClipboardSpriteFrames> clipboard_frames = memnew(ClipboardSpriteFrames);
  681. for (const int &frame_index : selected_items) {
  682. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, frame_index);
  683. if (texture.is_null()) {
  684. continue;
  685. }
  686. ClipboardSpriteFrames::Frame frame;
  687. frame.texture = texture;
  688. frame.duration = frames->get_frame_duration(edited_anim, frame_index);
  689. clipboard_frames->frames.push_back(frame);
  690. }
  691. EditorSettings::get_singleton()->set_resource_clipboard(clipboard_frames);
  692. }
  693. void SpriteFramesEditor::_empty_pressed() {
  694. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  695. int from = -1;
  696. Vector<int> selected_items = frame_list->get_selected_items();
  697. if (!selected_items.is_empty()) {
  698. from = selected_items[0];
  699. selection.clear();
  700. selection.push_back(from + 1);
  701. } else {
  702. from = frames->get_frame_count(edited_anim);
  703. }
  704. Ref<Texture2D> texture;
  705. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  706. undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
  707. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from);
  708. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from);
  709. undo_redo->add_do_method(this, "_update_library");
  710. undo_redo->add_undo_method(this, "_update_library");
  711. undo_redo->commit_action();
  712. }
  713. void SpriteFramesEditor::_empty2_pressed() {
  714. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  715. int from = -1;
  716. Vector<int> selected_items = frame_list->get_selected_items();
  717. if (!selected_items.is_empty()) {
  718. from = selected_items[selected_items.size() - 1];
  719. selection.clear();
  720. selection.push_back(from);
  721. } else {
  722. from = frames->get_frame_count(edited_anim);
  723. }
  724. Ref<Texture2D> texture;
  725. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  726. undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
  727. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from + 1);
  728. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from + 1);
  729. undo_redo->add_do_method(this, "_update_library");
  730. undo_redo->add_undo_method(this, "_update_library");
  731. undo_redo->commit_action();
  732. }
  733. void SpriteFramesEditor::_up_pressed() {
  734. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  735. Vector<int> selected_items = frame_list->get_selected_items();
  736. int nb_selected_items = selected_items.size();
  737. if (nb_selected_items <= 0) {
  738. return;
  739. }
  740. int first_selected_frame_index = selected_items[0];
  741. if (first_selected_frame_index < 1) {
  742. return;
  743. }
  744. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  745. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  746. int last_overwritten_frame = -1;
  747. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  748. int to_move = selected_items[selected_index];
  749. int new_index = to_move - 1;
  750. selected_items.set(selected_index, new_index);
  751. 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));
  752. 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));
  753. bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == to_move + 1;
  754. if (last_overwritten_frame == -1) {
  755. last_overwritten_frame = new_index;
  756. }
  757. if (!is_next_item_in_selection) {
  758. 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));
  759. 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));
  760. last_overwritten_frame = -1;
  761. }
  762. }
  763. selection = selected_items;
  764. undo_redo->add_do_method(this, "_update_library");
  765. undo_redo->add_undo_method(this, "_update_library");
  766. undo_redo->commit_action();
  767. }
  768. void SpriteFramesEditor::_down_pressed() {
  769. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  770. Vector<int> selected_items = frame_list->get_selected_items();
  771. int nb_selected_items = selected_items.size();
  772. if (nb_selected_items <= 0) {
  773. return;
  774. }
  775. int last_selected_frame_index = selected_items[nb_selected_items - 1];
  776. if (last_selected_frame_index >= frames->get_frame_count(edited_anim) - 1) {
  777. return;
  778. }
  779. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  780. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  781. int first_moved_frame = -1;
  782. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  783. int to_move = selected_items[selected_index];
  784. int new_index = to_move + 1;
  785. selected_items.set(selected_index, new_index);
  786. 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));
  787. 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));
  788. bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == new_index;
  789. if (first_moved_frame == -1) {
  790. first_moved_frame = to_move;
  791. }
  792. if (!is_next_item_in_selection) {
  793. 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));
  794. 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));
  795. first_moved_frame = -1;
  796. }
  797. }
  798. selection = selected_items;
  799. undo_redo->add_do_method(this, "_update_library");
  800. undo_redo->add_undo_method(this, "_update_library");
  801. undo_redo->commit_action();
  802. }
  803. void SpriteFramesEditor::_delete_pressed() {
  804. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  805. Vector<int> selected_items = frame_list->get_selected_items();
  806. int nb_selected_items = selected_items.size();
  807. if (nb_selected_items <= 0) {
  808. return;
  809. }
  810. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  811. undo_redo->create_action(TTR("Delete Resource"), UndoRedo::MERGE_DISABLE, frames.ptr());
  812. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  813. int to_delete = selected_items[selected_index];
  814. undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, to_delete - selected_index);
  815. 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);
  816. }
  817. undo_redo->add_do_method(this, "_update_library");
  818. undo_redo->add_undo_method(this, "_update_library");
  819. undo_redo->commit_action();
  820. }
  821. void SpriteFramesEditor::_animation_selected() {
  822. if (updating) {
  823. return;
  824. }
  825. TreeItem *selected = animations->get_selected();
  826. ERR_FAIL_NULL(selected);
  827. edited_anim = selected->get_text(0);
  828. if (animated_sprite) {
  829. sprite_node_updating = true;
  830. animated_sprite->call("set_animation", edited_anim);
  831. sprite_node_updating = false;
  832. }
  833. _update_library(true);
  834. }
  835. void SpriteFramesEditor::_sync_animation() {
  836. if (!animated_sprite || sprite_node_updating) {
  837. return;
  838. }
  839. _select_animation(animated_sprite->call("get_animation"), false);
  840. _update_stop_icon();
  841. }
  842. void SpriteFramesEditor::_select_animation(const String &p_name, bool p_update_node) {
  843. if (frames.is_null() || !frames->has_animation(p_name)) {
  844. return;
  845. }
  846. edited_anim = p_name;
  847. if (animated_sprite) {
  848. if (p_update_node) {
  849. animated_sprite->call("set_animation", edited_anim);
  850. }
  851. }
  852. _update_library();
  853. }
  854. static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
  855. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  856. if (!edited) {
  857. return;
  858. }
  859. if (p_node != edited && p_node->get_owner() != edited) {
  860. return;
  861. }
  862. {
  863. AnimatedSprite2D *as = Object::cast_to<AnimatedSprite2D>(p_node);
  864. if (as && as->get_sprite_frames() == p_sfames) {
  865. r_nodes->push_back(p_node);
  866. }
  867. }
  868. {
  869. AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
  870. if (as && as->get_sprite_frames() == p_sfames) {
  871. r_nodes->push_back(p_node);
  872. }
  873. }
  874. for (int i = 0; i < p_node->get_child_count(); i++) {
  875. _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
  876. }
  877. }
  878. void SpriteFramesEditor::_animation_name_edited() {
  879. if (updating) {
  880. return;
  881. }
  882. if (!frames->has_animation(edited_anim)) {
  883. return;
  884. }
  885. TreeItem *edited = animations->get_edited();
  886. if (!edited) {
  887. return;
  888. }
  889. String new_name = edited->get_text(0);
  890. if (new_name == String(edited_anim)) {
  891. return;
  892. }
  893. if (new_name.is_empty()) {
  894. new_name = "new_animation";
  895. }
  896. new_name = new_name.replace_char('/', '_').replace_char(',', ' ');
  897. String name = new_name;
  898. int counter = 0;
  899. while (frames->has_animation(name)) {
  900. if (name == String(edited_anim)) {
  901. edited->set_text(0, name); // The name didn't change, just updated the column text to name.
  902. return;
  903. }
  904. counter++;
  905. name = new_name + "_" + itos(counter);
  906. }
  907. edited->set_text(0, name);
  908. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  909. undo_redo->create_action(TTR("Rename Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  910. undo_redo->add_do_method(frames.ptr(), "rename_animation", edited_anim, name);
  911. undo_redo->add_undo_method(frames.ptr(), "rename_animation", name, edited_anim);
  912. _rename_node_animation(undo_redo, false, edited_anim, name, name);
  913. _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
  914. undo_redo->add_do_method(this, "_select_animation", name);
  915. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  916. undo_redo->add_do_method(this, "_update_library");
  917. undo_redo->add_undo_method(this, "_update_library");
  918. undo_redo->commit_action();
  919. animations->grab_focus();
  920. }
  921. 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) {
  922. List<Node *> nodes;
  923. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  924. if (is_undo) {
  925. for (Node *E : nodes) {
  926. String current_name = E->call("get_animation");
  927. if (current_name == p_filter) {
  928. undo_redo->force_fixed_history(); // Fixes corner-case when editing SpriteFrames stored as separate file.
  929. undo_redo->add_undo_method(E, "set_animation", p_new_animation);
  930. }
  931. String autoplay_name = E->call("get_autoplay");
  932. if (autoplay_name == p_filter) {
  933. undo_redo->force_fixed_history();
  934. undo_redo->add_undo_method(E, "set_autoplay", p_new_autoplay);
  935. }
  936. }
  937. } else {
  938. for (Node *E : nodes) {
  939. String current_name = E->call("get_animation");
  940. if (current_name == p_filter) {
  941. undo_redo->force_fixed_history();
  942. undo_redo->add_do_method(E, "set_animation", p_new_animation);
  943. }
  944. String autoplay_name = E->call("get_autoplay");
  945. if (autoplay_name == p_filter) {
  946. undo_redo->force_fixed_history();
  947. undo_redo->add_do_method(E, "set_autoplay", p_new_autoplay);
  948. }
  949. }
  950. }
  951. }
  952. void SpriteFramesEditor::_animation_add() {
  953. String name = "new_animation";
  954. int counter = 0;
  955. while (frames->has_animation(name)) {
  956. counter++;
  957. name = vformat("new_animation_%d", counter);
  958. }
  959. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  960. undo_redo->create_action(TTR("Add Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  961. undo_redo->add_do_method(frames.ptr(), "add_animation", name);
  962. undo_redo->add_undo_method(frames.ptr(), "remove_animation", name);
  963. undo_redo->add_do_method(this, "_select_animation", name);
  964. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  965. undo_redo->add_do_method(this, "_update_library");
  966. undo_redo->add_undo_method(this, "_update_library");
  967. undo_redo->commit_action();
  968. animations->grab_focus();
  969. }
  970. void SpriteFramesEditor::_animation_duplicate() {
  971. if (updating) {
  972. return;
  973. }
  974. if (!frames->has_animation(edited_anim)) {
  975. return;
  976. }
  977. int counter = 1;
  978. String new_name = edited_anim;
  979. PackedStringArray name_component = new_name.rsplit("_", true, 1);
  980. String base_name = name_component[0];
  981. if (name_component.size() > 1 && name_component[1].is_valid_int() && name_component[1].to_int() >= 0) {
  982. counter = name_component[1].to_int();
  983. }
  984. new_name = base_name + "_" + itos(counter);
  985. while (frames->has_animation(new_name)) {
  986. counter++;
  987. new_name = base_name + "_" + itos(counter);
  988. }
  989. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  990. undo_redo->create_action(TTR("Duplicate Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
  991. undo_redo->add_do_method(frames.ptr(), "duplicate_animation", edited_anim, new_name);
  992. undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
  993. undo_redo->add_do_method(this, "_select_animation", new_name);
  994. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  995. undo_redo->add_do_method(this, "_update_library");
  996. undo_redo->add_undo_method(this, "_update_library");
  997. undo_redo->commit_action();
  998. animations->grab_focus();
  999. }
  1000. void SpriteFramesEditor::_animation_remove() {
  1001. if (updating) {
  1002. return;
  1003. }
  1004. if (!frames->has_animation(edited_anim)) {
  1005. return;
  1006. }
  1007. delete_dialog->set_text(TTR("Delete Animation?"));
  1008. delete_dialog->popup_centered();
  1009. }
  1010. void SpriteFramesEditor::_animation_remove_confirmed() {
  1011. StringName new_edited;
  1012. List<StringName> anim_names;
  1013. frames->get_animation_list(&anim_names);
  1014. anim_names.sort_custom<StringName::AlphCompare>();
  1015. if (anim_names.size() >= 2) {
  1016. if (edited_anim == anim_names.get(0)) {
  1017. new_edited = anim_names.get(1);
  1018. } else {
  1019. new_edited = anim_names.get(0);
  1020. }
  1021. } else {
  1022. new_edited = StringName();
  1023. }
  1024. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1025. undo_redo->create_action(TTR("Remove Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1026. _rename_node_animation(undo_redo, false, edited_anim, new_edited, "");
  1027. undo_redo->add_do_method(frames.ptr(), "remove_animation", edited_anim);
  1028. undo_redo->add_undo_method(frames.ptr(), "add_animation", edited_anim);
  1029. _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
  1030. undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  1031. undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  1032. int fc = frames->get_frame_count(edited_anim);
  1033. for (int i = 0; i < fc; i++) {
  1034. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
  1035. float duration = frames->get_frame_duration(edited_anim, i);
  1036. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
  1037. }
  1038. undo_redo->add_do_method(this, "_select_animation", new_edited);
  1039. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  1040. undo_redo->add_do_method(this, "_update_library");
  1041. undo_redo->add_undo_method(this, "_update_library");
  1042. undo_redo->commit_action();
  1043. }
  1044. void SpriteFramesEditor::_animation_search_text_changed(const String &p_text) {
  1045. _update_library();
  1046. }
  1047. void SpriteFramesEditor::_animation_loop_changed() {
  1048. if (updating) {
  1049. return;
  1050. }
  1051. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1052. undo_redo->create_action(TTR("Change Animation Loop"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1053. undo_redo->add_do_method(frames.ptr(), "set_animation_loop", edited_anim, anim_loop->is_pressed());
  1054. undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  1055. undo_redo->add_do_method(this, "_update_library", true);
  1056. undo_redo->add_undo_method(this, "_update_library", true);
  1057. undo_redo->commit_action();
  1058. }
  1059. void SpriteFramesEditor::_animation_speed_resized() {
  1060. anim_speed->update_minimum_size();
  1061. }
  1062. void SpriteFramesEditor::_animation_speed_changed(double p_value) {
  1063. if (frames.is_null()) {
  1064. return;
  1065. }
  1066. if (updating) {
  1067. return;
  1068. }
  1069. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1070. undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS, frames.ptr());
  1071. undo_redo->add_do_method(frames.ptr(), "set_animation_speed", edited_anim, p_value);
  1072. undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  1073. undo_redo->add_do_method(this, "_update_library", true);
  1074. undo_redo->add_undo_method(this, "_update_library", true);
  1075. undo_redo->commit_action();
  1076. }
  1077. void SpriteFramesEditor::_frame_list_gui_input(const Ref<InputEvent> &p_event) {
  1078. const Ref<InputEventMouseButton> mb = p_event;
  1079. if (mb.is_valid()) {
  1080. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  1081. _zoom_in();
  1082. // Don't scroll up after zooming in.
  1083. accept_event();
  1084. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  1085. _zoom_out();
  1086. // Don't scroll down after zooming out.
  1087. accept_event();
  1088. } else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  1089. Point2 pos = mb->get_position();
  1090. right_clicked_frame = frame_list->get_item_at_position(pos, true);
  1091. if (right_clicked_frame != -1) {
  1092. Ref<Texture2D> tex = frames->get_frame_texture(edited_anim, right_clicked_frame);
  1093. if (tex.is_null()) {
  1094. return;
  1095. }
  1096. if (!menu) {
  1097. menu = memnew(PopupMenu);
  1098. add_child(menu);
  1099. menu->connect(SceneStringName(id_pressed), callable_mp(this, &SpriteFramesEditor::_menu_selected));
  1100. menu->add_icon_item(get_editor_theme_icon(SNAME("ShowInFileSystem")), TTRC("Show in FileSystem"), MENU_SHOW_IN_FILESYSTEM);
  1101. }
  1102. menu->set_position(get_screen_position() + get_local_mouse_position());
  1103. menu->popup();
  1104. }
  1105. }
  1106. }
  1107. }
  1108. void SpriteFramesEditor::_menu_selected(int p_id) {
  1109. switch (p_id) {
  1110. case MENU_SHOW_IN_FILESYSTEM: {
  1111. Ref<Texture2D> frame_texture = frames->get_frame_texture(edited_anim, right_clicked_frame);
  1112. ERR_FAIL_COND(frame_texture.is_null());
  1113. String path = frame_texture->get_path();
  1114. // Check if the file is an atlas resource, if it is find the source texture.
  1115. Ref<AtlasTexture> at = frame_texture;
  1116. while (at.is_valid() && at->get_atlas().is_valid()) {
  1117. path = at->get_atlas()->get_path();
  1118. at = at->get_atlas();
  1119. }
  1120. FileSystemDock::get_singleton()->navigate_to_path(path);
  1121. } break;
  1122. }
  1123. }
  1124. void SpriteFramesEditor::_frame_list_item_selected(int p_index, bool p_selected) {
  1125. if (updating) {
  1126. return;
  1127. }
  1128. selection = frame_list->get_selected_items();
  1129. if (selection.is_empty() || !p_selected) {
  1130. return;
  1131. }
  1132. updating = true;
  1133. frame_duration->set_value(frames->get_frame_duration(edited_anim, selection[0]));
  1134. updating = false;
  1135. }
  1136. void SpriteFramesEditor::_frame_duration_changed(double p_value) {
  1137. if (frames.is_null()) {
  1138. return;
  1139. }
  1140. if (updating) {
  1141. return;
  1142. }
  1143. if (selection.is_empty()) {
  1144. return;
  1145. }
  1146. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1147. undo_redo->create_action(TTR("Set Frame Duration"), UndoRedo::MERGE_ENDS, frames.ptr());
  1148. for (const int &index : selection) {
  1149. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, index);
  1150. float old_duration = frames->get_frame_duration(edited_anim, index);
  1151. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, index, texture, p_value);
  1152. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, index, texture, old_duration);
  1153. }
  1154. undo_redo->add_do_method(this, "_update_library");
  1155. undo_redo->add_undo_method(this, "_update_library");
  1156. undo_redo->commit_action();
  1157. }
  1158. void SpriteFramesEditor::_zoom_in() {
  1159. // Do not zoom in or out with no visible frames
  1160. if (frames->get_frame_count(edited_anim) <= 0) {
  1161. return;
  1162. }
  1163. if (thumbnail_zoom < max_thumbnail_zoom) {
  1164. thumbnail_zoom *= scale_ratio;
  1165. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  1166. frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  1167. frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  1168. }
  1169. }
  1170. void SpriteFramesEditor::_zoom_out() {
  1171. // Do not zoom in or out with no visible frames
  1172. if (frames->get_frame_count(edited_anim) <= 0) {
  1173. return;
  1174. }
  1175. if (thumbnail_zoom > min_thumbnail_zoom) {
  1176. thumbnail_zoom /= scale_ratio;
  1177. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  1178. frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  1179. frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  1180. }
  1181. }
  1182. void SpriteFramesEditor::_zoom_reset() {
  1183. thumbnail_zoom = MAX(1.0f, EDSCALE);
  1184. frame_list->set_fixed_column_width(thumbnail_default_size * 3 / 2);
  1185. frame_list->set_fixed_icon_size(Size2(thumbnail_default_size, thumbnail_default_size));
  1186. }
  1187. void SpriteFramesEditor::_update_library(bool p_skip_selector) {
  1188. if (!p_skip_selector) {
  1189. animations_dirty = true;
  1190. }
  1191. if (pending_update) {
  1192. return;
  1193. }
  1194. pending_update = true;
  1195. callable_mp(this, &SpriteFramesEditor::_update_library_impl).call_deferred();
  1196. }
  1197. void SpriteFramesEditor::_update_library_impl() {
  1198. pending_update = false;
  1199. if (frames.is_null()) {
  1200. return;
  1201. }
  1202. updating = true;
  1203. frame_duration->set_value_no_signal(1.0); // Default.
  1204. if (animations_dirty) {
  1205. animations_dirty = false;
  1206. animations->clear();
  1207. TreeItem *anim_root = animations->create_item();
  1208. List<StringName> anim_names;
  1209. frames->get_animation_list(&anim_names);
  1210. anim_names.sort_custom<StringName::AlphCompare>();
  1211. if (!anim_names.size()) {
  1212. missing_anim_label->show();
  1213. anim_frames_vb->hide();
  1214. return;
  1215. }
  1216. missing_anim_label->hide();
  1217. anim_frames_vb->show();
  1218. bool searching = anim_search_box->get_text().size();
  1219. String searched_string = searching ? anim_search_box->get_text().to_lower() : String();
  1220. TreeItem *selected = nullptr;
  1221. for (const StringName &E : anim_names) {
  1222. String name = E;
  1223. if (searching && !name.to_lower().contains(searched_string)) {
  1224. continue;
  1225. }
  1226. TreeItem *it = animations->create_item(anim_root);
  1227. it->set_metadata(0, name);
  1228. it->set_text(0, name);
  1229. it->set_editable(0, true);
  1230. if (animated_sprite) {
  1231. if (name == String(animated_sprite->call("get_autoplay"))) {
  1232. it->set_icon(0, autoplay_icon);
  1233. }
  1234. }
  1235. if (E == edited_anim) {
  1236. it->select(0);
  1237. selected = it;
  1238. }
  1239. }
  1240. if (selected) {
  1241. animations->scroll_to_item(selected);
  1242. }
  1243. }
  1244. if (animated_sprite) {
  1245. String autoplay_name = animated_sprite->call("get_autoplay");
  1246. if (autoplay_name.is_empty()) {
  1247. autoplay->set_pressed_no_signal(false);
  1248. } else {
  1249. autoplay->set_pressed_no_signal(String(edited_anim) == autoplay_name);
  1250. }
  1251. }
  1252. frame_list->clear();
  1253. if (!frames->has_animation(edited_anim)) {
  1254. updating = false;
  1255. return;
  1256. }
  1257. int anim_frame_count = frames->get_frame_count(edited_anim);
  1258. if (anim_frame_count == 0) {
  1259. selection.clear();
  1260. }
  1261. for (int index = 0; index < selection.size(); index++) {
  1262. int sel = selection[index];
  1263. if (sel == -1) {
  1264. selection.remove_at(index);
  1265. index--;
  1266. }
  1267. if (sel >= anim_frame_count) {
  1268. selection.set(index, anim_frame_count - 1);
  1269. // Since selection is ordered, if we get a frame that is outside of the range
  1270. // we can clip all the other one.
  1271. selection.resize(index + 1);
  1272. break;
  1273. }
  1274. }
  1275. if (selection.is_empty() && frames->get_frame_count(edited_anim)) {
  1276. selection.push_back(0);
  1277. }
  1278. bool is_first_selection = true;
  1279. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  1280. String name = itos(i);
  1281. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
  1282. float duration = frames->get_frame_duration(edited_anim, i);
  1283. if (texture.is_null()) {
  1284. texture = empty_icon;
  1285. name += ": " + TTR("(empty)");
  1286. } else if (!texture->get_name().is_empty()) {
  1287. name += ": " + texture->get_name();
  1288. }
  1289. if (duration != 1.0f) {
  1290. name += String::utf8(" [× ") + String::num(duration, 2) + "]";
  1291. }
  1292. frame_list->add_item(name, texture);
  1293. if (texture.is_valid()) {
  1294. String tooltip = texture->get_path();
  1295. // Frame is often saved as an AtlasTexture subresource within a scene/resource file,
  1296. // thus its path might be not what the user is looking for. So we're also showing
  1297. // subsequent source texture paths.
  1298. String prefix = U"┖╴";
  1299. Ref<AtlasTexture> at = texture;
  1300. while (at.is_valid() && at->get_atlas().is_valid()) {
  1301. tooltip += "\n" + prefix + at->get_atlas()->get_path();
  1302. prefix = " " + prefix;
  1303. at = at->get_atlas();
  1304. }
  1305. frame_list->set_item_tooltip(-1, tooltip);
  1306. }
  1307. if (selection.has(i)) {
  1308. frame_list->select(frame_list->get_item_count() - 1, is_first_selection);
  1309. if (is_first_selection) {
  1310. frame_duration->set_value_no_signal(frames->get_frame_duration(edited_anim, i));
  1311. }
  1312. is_first_selection = false;
  1313. }
  1314. }
  1315. anim_speed->set_value_no_signal(frames->get_animation_speed(edited_anim));
  1316. anim_loop->set_pressed_no_signal(frames->get_animation_loop(edited_anim));
  1317. updating = false;
  1318. }
  1319. void SpriteFramesEditor::_edit() {
  1320. if (!animated_sprite) {
  1321. return;
  1322. }
  1323. edit(animated_sprite->call("get_sprite_frames"));
  1324. }
  1325. void SpriteFramesEditor::edit(Ref<SpriteFrames> p_frames) {
  1326. _update_stop_icon();
  1327. if (p_frames.is_null()) {
  1328. frames.unref();
  1329. _remove_sprite_node();
  1330. hide();
  1331. return;
  1332. }
  1333. frames = p_frames;
  1334. read_only = EditorNode::get_singleton()->is_resource_read_only(p_frames);
  1335. if (!p_frames->has_animation(edited_anim)) {
  1336. List<StringName> anim_names;
  1337. frames->get_animation_list(&anim_names);
  1338. anim_names.sort_custom<StringName::AlphCompare>();
  1339. if (anim_names.size()) {
  1340. edited_anim = anim_names.front()->get();
  1341. } else {
  1342. edited_anim = StringName();
  1343. }
  1344. }
  1345. _update_library();
  1346. // Clear zoom and split sheet texture
  1347. split_sheet_preview->set_texture(Ref<Texture2D>());
  1348. _zoom_reset();
  1349. add_anim->set_disabled(read_only);
  1350. duplicate_anim->set_disabled(read_only);
  1351. delete_anim->set_disabled(read_only);
  1352. anim_speed->set_editable(!read_only);
  1353. anim_loop->set_disabled(read_only);
  1354. load->set_disabled(read_only);
  1355. load_sheet->set_disabled(read_only);
  1356. copy->set_disabled(read_only);
  1357. paste->set_disabled(read_only);
  1358. empty_before->set_disabled(read_only);
  1359. empty_after->set_disabled(read_only);
  1360. move_up->set_disabled(read_only);
  1361. move_down->set_disabled(read_only);
  1362. delete_frame->set_disabled(read_only);
  1363. _fetch_sprite_node(); // Fetch node after set frames.
  1364. }
  1365. Ref<SpriteFrames> SpriteFramesEditor::get_sprite_frames() const {
  1366. return frames;
  1367. }
  1368. Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1369. if (read_only) {
  1370. return false;
  1371. }
  1372. if (!frames->has_animation(edited_anim)) {
  1373. return false;
  1374. }
  1375. int idx = -1;
  1376. if (p_point == Vector2(Math::INF, Math::INF)) {
  1377. if (frame_list->is_anything_selected()) {
  1378. idx = frame_list->get_selected_items()[0];
  1379. }
  1380. } else {
  1381. idx = frame_list->get_item_at_position(p_point, true);
  1382. }
  1383. if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) {
  1384. return Variant();
  1385. }
  1386. Ref<Resource> frame = frames->get_frame_texture(edited_anim, idx);
  1387. if (frame.is_null()) {
  1388. return Variant();
  1389. }
  1390. Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
  1391. drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
  1392. return drag_data;
  1393. }
  1394. bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1395. if (read_only) {
  1396. return false;
  1397. }
  1398. Dictionary d = p_data;
  1399. if (!d.has("type")) {
  1400. return false;
  1401. }
  1402. // reordering frames
  1403. if (d.has("from") && (Object *)(d["from"]) == frame_list) {
  1404. return true;
  1405. }
  1406. if (String(d["type"]) == "resource" && d.has("resource")) {
  1407. Ref<Resource> r = d["resource"];
  1408. Ref<Texture2D> texture = r;
  1409. if (texture.is_valid()) {
  1410. return true;
  1411. }
  1412. }
  1413. if (String(d["type"]) == "files") {
  1414. Vector<String> files = d["files"];
  1415. if (files.is_empty()) {
  1416. return false;
  1417. }
  1418. for (int i = 0; i < files.size(); i++) {
  1419. const String &f = files[i];
  1420. String ftype = EditorFileSystem::get_singleton()->get_file_type(f);
  1421. if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
  1422. return false;
  1423. }
  1424. }
  1425. return true;
  1426. }
  1427. return false;
  1428. }
  1429. void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1430. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1431. return;
  1432. }
  1433. Dictionary d = p_data;
  1434. if (!d.has("type")) {
  1435. return;
  1436. }
  1437. int at_pos = -1;
  1438. if (p_point == Vector2(Math::INF, Math::INF)) {
  1439. if (frame_list->is_anything_selected()) {
  1440. at_pos = frame_list->get_selected_items()[0];
  1441. }
  1442. } else {
  1443. at_pos = frame_list->get_item_at_position(p_point, true);
  1444. }
  1445. if (String(d["type"]) == "resource" && d.has("resource")) {
  1446. Ref<Resource> r = d["resource"];
  1447. Ref<Texture2D> texture = r;
  1448. if (texture.is_valid()) {
  1449. bool reorder = false;
  1450. if (d.has("from") && (Object *)(d["from"]) == frame_list) {
  1451. reorder = true;
  1452. }
  1453. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1454. if (reorder) { //drop is from reordering frames
  1455. int from_frame = -1;
  1456. float duration = 1.0;
  1457. if (d.has("frame")) {
  1458. from_frame = d["frame"];
  1459. duration = frames->get_frame_duration(edited_anim, from_frame);
  1460. }
  1461. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1462. undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
  1463. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration, at_pos == -1 ? -1 : at_pos);
  1464. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
  1465. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration, from_frame);
  1466. undo_redo->add_do_method(this, "_update_library");
  1467. undo_redo->add_undo_method(this, "_update_library");
  1468. undo_redo->commit_action();
  1469. } else {
  1470. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1471. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, at_pos == -1 ? -1 : at_pos);
  1472. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
  1473. undo_redo->add_do_method(this, "_update_library");
  1474. undo_redo->add_undo_method(this, "_update_library");
  1475. undo_redo->commit_action();
  1476. }
  1477. }
  1478. }
  1479. if (String(d["type"]) == "files") {
  1480. Vector<String> files = d["files"];
  1481. if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1482. _prepare_sprite_sheet(files[0]);
  1483. } else {
  1484. _file_load_request(files, at_pos);
  1485. }
  1486. }
  1487. }
  1488. void SpriteFramesEditor::_update_stop_icon() {
  1489. bool is_playing = false;
  1490. if (animated_sprite) {
  1491. is_playing = animated_sprite->call("is_playing");
  1492. }
  1493. if (is_playing) {
  1494. stop->set_button_icon(pause_icon);
  1495. } else {
  1496. stop->set_button_icon(stop_icon);
  1497. }
  1498. }
  1499. void SpriteFramesEditor::_remove_sprite_node() {
  1500. if (!animated_sprite) {
  1501. return;
  1502. }
  1503. if (animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
  1504. animated_sprite->disconnect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
  1505. }
  1506. if (animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
  1507. animated_sprite->disconnect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation));
  1508. }
  1509. if (animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
  1510. animated_sprite->disconnect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
  1511. }
  1512. animated_sprite = nullptr;
  1513. }
  1514. void SpriteFramesEditor::_fetch_sprite_node() {
  1515. Node *selected = nullptr;
  1516. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  1517. const List<Node *> &top_node_list = editor_selection->get_top_selected_node_list();
  1518. if (top_node_list.size() == 1) {
  1519. selected = top_node_list.front()->get();
  1520. }
  1521. bool show_node_edit = false;
  1522. AnimatedSprite2D *as2d = Object::cast_to<AnimatedSprite2D>(selected);
  1523. AnimatedSprite3D *as3d = Object::cast_to<AnimatedSprite3D>(selected);
  1524. if (as2d || as3d) {
  1525. if (frames != selected->call("get_sprite_frames")) {
  1526. _remove_sprite_node();
  1527. } else {
  1528. animated_sprite = selected;
  1529. if (!animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
  1530. animated_sprite->connect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
  1531. }
  1532. if (!animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
  1533. animated_sprite->connect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation), CONNECT_DEFERRED);
  1534. }
  1535. if (!animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
  1536. animated_sprite->connect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
  1537. }
  1538. show_node_edit = true;
  1539. }
  1540. } else {
  1541. _remove_sprite_node();
  1542. }
  1543. if (show_node_edit) {
  1544. _sync_animation();
  1545. autoplay_container->show();
  1546. playback_container->show();
  1547. } else {
  1548. _update_library(); // To init autoplay icon.
  1549. autoplay_container->hide();
  1550. playback_container->hide();
  1551. }
  1552. }
  1553. void SpriteFramesEditor::_play_pressed() {
  1554. if (animated_sprite) {
  1555. animated_sprite->call("stop");
  1556. animated_sprite->call("play", animated_sprite->call("get_animation"));
  1557. }
  1558. _update_stop_icon();
  1559. }
  1560. void SpriteFramesEditor::_play_from_pressed() {
  1561. if (animated_sprite) {
  1562. animated_sprite->call("play", animated_sprite->call("get_animation"));
  1563. }
  1564. _update_stop_icon();
  1565. }
  1566. void SpriteFramesEditor::_play_bw_pressed() {
  1567. if (animated_sprite) {
  1568. animated_sprite->call("stop");
  1569. animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
  1570. }
  1571. _update_stop_icon();
  1572. }
  1573. void SpriteFramesEditor::_play_bw_from_pressed() {
  1574. if (animated_sprite) {
  1575. animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
  1576. }
  1577. _update_stop_icon();
  1578. }
  1579. void SpriteFramesEditor::_stop_pressed() {
  1580. if (animated_sprite) {
  1581. if (animated_sprite->call("is_playing")) {
  1582. animated_sprite->call("pause");
  1583. } else {
  1584. animated_sprite->call("stop");
  1585. }
  1586. }
  1587. _update_stop_icon();
  1588. }
  1589. void SpriteFramesEditor::_autoplay_pressed() {
  1590. if (updating) {
  1591. return;
  1592. }
  1593. if (animated_sprite) {
  1594. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1595. undo_redo->create_action(TTR("Toggle Autoplay"), UndoRedo::MERGE_DISABLE, animated_sprite);
  1596. String current = animated_sprite->call("get_animation");
  1597. String current_auto = animated_sprite->call("get_autoplay");
  1598. if (current == current_auto) {
  1599. //unset
  1600. undo_redo->add_do_method(animated_sprite, "set_autoplay", "");
  1601. undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
  1602. } else {
  1603. //set
  1604. undo_redo->add_do_method(animated_sprite, "set_autoplay", current);
  1605. undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
  1606. }
  1607. undo_redo->add_do_method(this, "_update_library");
  1608. undo_redo->add_undo_method(this, "_update_library");
  1609. undo_redo->commit_action();
  1610. }
  1611. _update_library();
  1612. }
  1613. void SpriteFramesEditor::_bind_methods() {
  1614. ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
  1615. ClassDB::bind_method(D_METHOD("_select_animation", "name", "update_node"), &SpriteFramesEditor::_select_animation, DEFVAL(true));
  1616. }
  1617. void SpriteFramesEditor::_node_removed(Node *p_node) {
  1618. if (animated_sprite) {
  1619. if (animated_sprite != p_node) {
  1620. return;
  1621. }
  1622. _remove_sprite_node();
  1623. }
  1624. }
  1625. SpriteFramesEditor::SpriteFramesEditor() {
  1626. VBoxContainer *vbc_animlist = memnew(VBoxContainer);
  1627. add_child(vbc_animlist);
  1628. vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
  1629. VBoxContainer *sub_vb = memnew(VBoxContainer);
  1630. vbc_animlist->add_margin_child(TTR("Animations:"), sub_vb, true);
  1631. sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1632. HBoxContainer *hbc_animlist = memnew(HBoxContainer);
  1633. sub_vb->add_child(hbc_animlist);
  1634. add_anim = memnew(Button);
  1635. add_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1636. add_anim->set_accessibility_name(TTRC("Add Animation"));
  1637. hbc_animlist->add_child(add_anim);
  1638. add_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_add));
  1639. duplicate_anim = memnew(Button);
  1640. duplicate_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1641. duplicate_anim->set_accessibility_name(TTRC("Duplicate Animation"));
  1642. hbc_animlist->add_child(duplicate_anim);
  1643. duplicate_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_duplicate));
  1644. delete_anim = memnew(Button);
  1645. delete_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1646. delete_anim->set_accessibility_name(TTRC("Delete Animation"));
  1647. hbc_animlist->add_child(delete_anim);
  1648. delete_anim->set_disabled(true);
  1649. delete_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_remove));
  1650. autoplay_container = memnew(HBoxContainer);
  1651. hbc_animlist->add_child(autoplay_container);
  1652. autoplay_container->add_child(memnew(VSeparator));
  1653. autoplay = memnew(Button);
  1654. autoplay->set_theme_type_variation(SceneStringName(FlatButton));
  1655. autoplay->set_tooltip_text(TTR("Autoplay on Load"));
  1656. autoplay_container->add_child(autoplay);
  1657. hbc_animlist->add_child(memnew(VSeparator));
  1658. anim_loop = memnew(Button);
  1659. anim_loop->set_toggle_mode(true);
  1660. anim_loop->set_theme_type_variation(SceneStringName(FlatButton));
  1661. anim_loop->set_tooltip_text(TTR("Animation Looping"));
  1662. anim_loop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_loop_changed));
  1663. hbc_animlist->add_child(anim_loop);
  1664. anim_speed = memnew(SpinBox);
  1665. anim_speed->set_suffix(TTR("FPS"));
  1666. anim_speed->set_min(0);
  1667. anim_speed->set_max(120);
  1668. anim_speed->set_step(0.01);
  1669. anim_speed->set_custom_arrow_step(1);
  1670. anim_speed->set_tooltip_text(TTR("Animation Speed"));
  1671. anim_speed->get_line_edit()->set_expand_to_text_length_enabled(true);
  1672. anim_speed->get_line_edit()->connect(SceneStringName(resized), callable_mp(this, &SpriteFramesEditor::_animation_speed_resized));
  1673. anim_speed->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_animation_speed_changed));
  1674. hbc_animlist->add_child(anim_speed);
  1675. anim_search_box = memnew(LineEdit);
  1676. sub_vb->add_child(anim_search_box);
  1677. anim_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1678. anim_search_box->set_placeholder(TTR("Filter Animations"));
  1679. anim_search_box->set_clear_button_enabled(true);
  1680. anim_search_box->connect(SceneStringName(text_changed), callable_mp(this, &SpriteFramesEditor::_animation_search_text_changed));
  1681. animations = memnew(Tree);
  1682. sub_vb->add_child(animations);
  1683. animations->set_v_size_flags(SIZE_EXPAND_FILL);
  1684. animations->set_hide_root(true);
  1685. // HACK: The cell_selected signal is emitted before the FPS spinbox loses focus and applies the change.
  1686. animations->connect("cell_selected", callable_mp(this, &SpriteFramesEditor::_animation_selected), CONNECT_DEFERRED);
  1687. animations->connect("item_edited", callable_mp(this, &SpriteFramesEditor::_animation_name_edited));
  1688. animations->set_theme_type_variation("TreeSecondary");
  1689. animations->set_allow_reselect(true);
  1690. add_anim->set_shortcut_context(animations);
  1691. add_anim->set_shortcut(ED_SHORTCUT("sprite_frames/new_animation", TTRC("Add Animation"), KeyModifierMask::CMD_OR_CTRL | Key::N));
  1692. duplicate_anim->set_shortcut_context(animations);
  1693. duplicate_anim->set_shortcut(ED_SHORTCUT("sprite_frames/duplicate_animation", TTRC("Duplicate Animation"), KeyModifierMask::CMD_OR_CTRL | Key::D));
  1694. delete_anim->set_shortcut_context(animations);
  1695. delete_anim->set_shortcut(ED_SHORTCUT("sprite_frames/delete_animation", TTRC("Delete Animation"), Key::KEY_DELETE));
  1696. missing_anim_label = memnew(Label);
  1697. missing_anim_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  1698. missing_anim_label->set_text(TTR("This resource does not have any animations."));
  1699. missing_anim_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1700. missing_anim_label->set_v_size_flags(SIZE_EXPAND_FILL);
  1701. missing_anim_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1702. missing_anim_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1703. missing_anim_label->hide();
  1704. add_child(missing_anim_label);
  1705. anim_frames_vb = memnew(VBoxContainer);
  1706. add_child(anim_frames_vb);
  1707. anim_frames_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1708. anim_frames_vb->hide();
  1709. sub_vb = memnew(VBoxContainer);
  1710. anim_frames_vb->add_margin_child(TTR("Animation Frames:"), sub_vb, true);
  1711. HFlowContainer *hfc = memnew(HFlowContainer);
  1712. sub_vb->add_child(hfc);
  1713. playback_container = memnew(HBoxContainer);
  1714. playback_container->set_layout_direction(LAYOUT_DIRECTION_LTR);
  1715. hfc->add_child(playback_container);
  1716. play_bw_from = memnew(Button);
  1717. play_bw_from->set_theme_type_variation(SceneStringName(FlatButton));
  1718. play_bw_from->set_tooltip_text(TTR("Play selected animation backwards from current pos. (A)"));
  1719. playback_container->add_child(play_bw_from);
  1720. play_bw = memnew(Button);
  1721. play_bw->set_theme_type_variation(SceneStringName(FlatButton));
  1722. play_bw->set_tooltip_text(TTR("Play selected animation backwards from end. (Shift+A)"));
  1723. playback_container->add_child(play_bw);
  1724. stop = memnew(Button);
  1725. stop->set_theme_type_variation(SceneStringName(FlatButton));
  1726. stop->set_tooltip_text(TTR("Pause/stop animation playback. (S)"));
  1727. playback_container->add_child(stop);
  1728. play = memnew(Button);
  1729. play->set_theme_type_variation(SceneStringName(FlatButton));
  1730. play->set_tooltip_text(TTR("Play selected animation from start. (Shift+D)"));
  1731. playback_container->add_child(play);
  1732. play_from = memnew(Button);
  1733. play_from->set_theme_type_variation(SceneStringName(FlatButton));
  1734. play_from->set_tooltip_text(TTR("Play selected animation from current pos. (D)"));
  1735. playback_container->add_child(play_from);
  1736. hfc->add_child(memnew(VSeparator));
  1737. autoplay->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_autoplay_pressed));
  1738. autoplay->set_toggle_mode(true);
  1739. play->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_pressed));
  1740. play_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_from_pressed));
  1741. play_bw->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_pressed));
  1742. play_bw_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_from_pressed));
  1743. stop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_stop_pressed));
  1744. HBoxContainer *hbc_actions = memnew(HBoxContainer);
  1745. hfc->add_child(hbc_actions);
  1746. load = memnew(Button);
  1747. load->set_accessibility_name(TTRC("Load"));
  1748. load->set_theme_type_variation(SceneStringName(FlatButton));
  1749. hbc_actions->add_child(load);
  1750. load_sheet = memnew(Button);
  1751. load_sheet->set_accessibility_name(TTRC("Load Sheet"));
  1752. load_sheet->set_theme_type_variation(SceneStringName(FlatButton));
  1753. hbc_actions->add_child(load_sheet);
  1754. hbc_actions->add_child(memnew(VSeparator));
  1755. copy = memnew(Button);
  1756. copy->set_accessibility_name(TTRC("Copy"));
  1757. copy->set_theme_type_variation(SceneStringName(FlatButton));
  1758. hbc_actions->add_child(copy);
  1759. paste = memnew(Button);
  1760. paste->set_accessibility_name(TTRC("Paste"));
  1761. paste->set_theme_type_variation(SceneStringName(FlatButton));
  1762. hbc_actions->add_child(paste);
  1763. hbc_actions->add_child(memnew(VSeparator));
  1764. empty_before = memnew(Button);
  1765. empty_before->set_accessibility_name(TTRC("Empty Before"));
  1766. empty_before->set_theme_type_variation(SceneStringName(FlatButton));
  1767. hbc_actions->add_child(empty_before);
  1768. empty_after = memnew(Button);
  1769. empty_after->set_accessibility_name(TTRC("Empty After"));
  1770. empty_after->set_theme_type_variation(SceneStringName(FlatButton));
  1771. hbc_actions->add_child(empty_after);
  1772. hbc_actions->add_child(memnew(VSeparator));
  1773. move_up = memnew(Button);
  1774. move_up->set_accessibility_name(TTRC("Move Up"));
  1775. move_up->set_theme_type_variation(SceneStringName(FlatButton));
  1776. hbc_actions->add_child(move_up);
  1777. move_down = memnew(Button);
  1778. move_down->set_accessibility_name(TTRC("Move Down"));
  1779. move_down->set_theme_type_variation(SceneStringName(FlatButton));
  1780. hbc_actions->add_child(move_down);
  1781. delete_frame = memnew(Button);
  1782. delete_frame->set_accessibility_name(TTRC("Delete Frame"));
  1783. delete_frame->set_theme_type_variation(SceneStringName(FlatButton));
  1784. hbc_actions->add_child(delete_frame);
  1785. hbc_actions->add_child(memnew(VSeparator));
  1786. HBoxContainer *hbc_frame_duration = memnew(HBoxContainer);
  1787. hfc->add_child(hbc_frame_duration);
  1788. Label *label = memnew(Label);
  1789. label->set_text(TTR("Frame Duration:"));
  1790. hbc_frame_duration->add_child(label);
  1791. frame_duration = memnew(SpinBox);
  1792. frame_duration->set_prefix(String::utf8("×"));
  1793. frame_duration->set_min(SPRITE_FRAME_MINIMUM_DURATION); // Avoid zero div.
  1794. frame_duration->set_max(10);
  1795. frame_duration->set_step(0.01);
  1796. frame_duration->set_custom_arrow_step(0.1);
  1797. frame_duration->set_allow_lesser(false);
  1798. frame_duration->set_allow_greater(true);
  1799. frame_duration->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_frame_duration_changed));
  1800. frame_duration->set_accessibility_name(TTRC("Frame Duration:"));
  1801. hbc_frame_duration->add_child(frame_duration);
  1802. // Wide empty separation control. (like BoxContainer::add_spacer())
  1803. Control *c = memnew(Control);
  1804. c->set_mouse_filter(MOUSE_FILTER_PASS);
  1805. c->set_h_size_flags(SIZE_EXPAND_FILL);
  1806. hfc->add_child(c);
  1807. HBoxContainer *hbc_zoom = memnew(HBoxContainer);
  1808. hfc->add_child(hbc_zoom);
  1809. zoom_out = memnew(Button);
  1810. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_out));
  1811. zoom_out->set_flat(true);
  1812. zoom_out->set_tooltip_text(TTRC("Zoom Out"));
  1813. hbc_zoom->add_child(zoom_out);
  1814. zoom_reset = memnew(Button);
  1815. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_reset));
  1816. zoom_reset->set_flat(true);
  1817. zoom_reset->set_tooltip_text(TTRC("Zoom Reset"));
  1818. hbc_zoom->add_child(zoom_reset);
  1819. zoom_in = memnew(Button);
  1820. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_in));
  1821. zoom_in->set_flat(true);
  1822. zoom_in->set_tooltip_text(TTRC("Zoom In"));
  1823. hbc_zoom->add_child(zoom_in);
  1824. file = memnew(EditorFileDialog);
  1825. file->connect("files_selected", callable_mp(this, &SpriteFramesEditor::_file_load_request).bind(-1));
  1826. add_child(file);
  1827. frame_list = memnew(ItemList);
  1828. frame_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1829. frame_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1830. frame_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  1831. frame_list->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1832. frame_list->set_select_mode(ItemList::SELECT_MULTI);
  1833. frame_list->set_max_columns(0);
  1834. frame_list->set_max_text_lines(2);
  1835. SET_DRAG_FORWARDING_GCD(frame_list, SpriteFramesEditor);
  1836. frame_list->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_frame_list_gui_input));
  1837. // HACK: The item_selected signal is emitted before the Frame Duration spinbox loses focus and applies the change.
  1838. frame_list->connect("multi_selected", callable_mp(this, &SpriteFramesEditor::_frame_list_item_selected), CONNECT_DEFERRED);
  1839. sub_vb->add_child(frame_list);
  1840. dialog = memnew(AcceptDialog);
  1841. add_child(dialog);
  1842. load->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_load_pressed));
  1843. load_sheet->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_open_sprite_sheet));
  1844. delete_frame->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_delete_pressed));
  1845. copy->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_copy_pressed));
  1846. paste->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_paste_pressed));
  1847. empty_before->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty_pressed));
  1848. empty_after->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty2_pressed));
  1849. move_up->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_up_pressed));
  1850. move_down->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_down_pressed));
  1851. load->set_shortcut_context(frame_list);
  1852. load->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_file", TTRC("Add frame from file"), KeyModifierMask::CMD_OR_CTRL | Key::O));
  1853. load_sheet->set_shortcut_context(frame_list);
  1854. 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));
  1855. delete_frame->set_shortcut_context(frame_list);
  1856. delete_frame->set_shortcut(ED_SHORTCUT("sprite_frames/delete", TTRC("Delete Frame"), Key::KEY_DELETE));
  1857. copy->set_shortcut_context(frame_list);
  1858. copy->set_shortcut(ED_SHORTCUT("sprite_frames/copy", TTRC("Copy Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  1859. paste->set_shortcut_context(frame_list);
  1860. paste->set_shortcut(ED_SHORTCUT("sprite_frames/paste", TTRC("Paste Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::V));
  1861. empty_before->set_shortcut_context(frame_list);
  1862. empty_before->set_shortcut(ED_SHORTCUT("sprite_frames/empty_before", TTRC("Insert Empty (Before Selected)"), KeyModifierMask::ALT | Key::LEFT));
  1863. empty_after->set_shortcut_context(frame_list);
  1864. empty_after->set_shortcut(ED_SHORTCUT("sprite_frames/empty_after", TTRC("Insert Empty (After Selected)"), KeyModifierMask::ALT | Key::RIGHT));
  1865. move_up->set_shortcut_context(frame_list);
  1866. move_up->set_shortcut(ED_SHORTCUT("sprite_frames/move_left", TTRC("Move Frame Left"), KeyModifierMask::CMD_OR_CTRL | Key::LEFT));
  1867. move_down->set_shortcut_context(frame_list);
  1868. move_down->set_shortcut(ED_SHORTCUT("sprite_frames/move_right", TTRC("Move Frame Right"), KeyModifierMask::CMD_OR_CTRL | Key::RIGHT));
  1869. zoom_out->set_shortcut_context(frame_list);
  1870. zoom_out->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_out", TTRC("Zoom Out"),
  1871. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) }));
  1872. zoom_in->set_shortcut_context(frame_list);
  1873. zoom_in->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_in", TTRC("Zoom In"),
  1874. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) }));
  1875. loading_scene = false;
  1876. updating = false;
  1877. edited_anim = SceneStringName(default_);
  1878. delete_dialog = memnew(ConfirmationDialog);
  1879. add_child(delete_dialog);
  1880. delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_animation_remove_confirmed));
  1881. split_sheet_dialog = memnew(ConfirmationDialog);
  1882. add_child(split_sheet_dialog);
  1883. split_sheet_dialog->set_title(TTR("Select Frames"));
  1884. split_sheet_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_sheet_add_frames));
  1885. HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
  1886. split_sheet_dialog->add_child(split_sheet_hb);
  1887. split_sheet_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1888. split_sheet_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1889. VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
  1890. split_sheet_hb->add_child(split_sheet_vb);
  1891. split_sheet_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1892. split_sheet_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1893. HBoxContainer *split_sheet_menu_hb = memnew(HBoxContainer);
  1894. split_sheet_menu_hb->add_child(memnew(Label(TTR("Frame Order"))));
  1895. split_sheet_order = memnew(OptionButton);
  1896. split_sheet_order->add_item(TTR("As Selected"), FRAME_ORDER_SELECTION);
  1897. split_sheet_order->add_separator(TTR("By Row"));
  1898. split_sheet_order->add_item(TTR("Left to Right, Top to Bottom"), FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM);
  1899. split_sheet_order->add_item(TTR("Left to Right, Bottom to Top"), FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP);
  1900. split_sheet_order->add_item(TTR("Right to Left, Top to Bottom"), FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM);
  1901. split_sheet_order->add_item(TTR("Right to Left, Bottom to Top"), FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP);
  1902. split_sheet_order->add_separator(TTR("By Column"));
  1903. split_sheet_order->add_item(TTR("Top to Bottom, Left to Right"), FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT);
  1904. split_sheet_order->add_item(TTR("Top to Bottom, Right to Left"), FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT);
  1905. split_sheet_order->add_item(TTR("Bottom to Top, Left to Right"), FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT);
  1906. split_sheet_order->add_item(TTR("Bottom to Top, Right to Left"), FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT);
  1907. split_sheet_order->connect(SceneStringName(item_selected), callable_mp(this, &SpriteFramesEditor::_sheet_order_selected));
  1908. split_sheet_menu_hb->add_child(split_sheet_order);
  1909. Button *select_all = memnew(Button);
  1910. select_all->set_text(TTR("Select All"));
  1911. select_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_select_all_frames));
  1912. split_sheet_menu_hb->add_child(select_all);
  1913. Button *clear_all = memnew(Button);
  1914. clear_all->set_text(TTR("Select None"));
  1915. clear_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_clear_all_frames));
  1916. split_sheet_menu_hb->add_child(clear_all);
  1917. split_sheet_menu_hb->add_spacer();
  1918. toggle_settings_button = memnew(Button);
  1919. toggle_settings_button->set_h_size_flags(SIZE_SHRINK_END);
  1920. toggle_settings_button->set_theme_type_variation(SceneStringName(FlatButton));
  1921. toggle_settings_button->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_toggle_show_settings));
  1922. toggle_settings_button->set_tooltip_text(TTR("Toggle Settings Panel"));
  1923. split_sheet_menu_hb->add_child(toggle_settings_button);
  1924. split_sheet_vb->add_child(split_sheet_menu_hb);
  1925. PanelContainer *split_sheet_panel = memnew(PanelContainer);
  1926. split_sheet_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1927. split_sheet_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1928. split_sheet_vb->add_child(split_sheet_panel);
  1929. split_sheet_preview = memnew(TextureRect);
  1930. split_sheet_preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1931. split_sheet_preview->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1932. split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
  1933. split_sheet_preview->connect(SceneStringName(draw), callable_mp(this, &SpriteFramesEditor::_sheet_preview_draw));
  1934. split_sheet_preview->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_preview_input));
  1935. split_sheet_scroll = memnew(ScrollContainer);
  1936. split_sheet_scroll->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_scroll_input));
  1937. split_sheet_panel->add_child(split_sheet_scroll);
  1938. CenterContainer *cc = memnew(CenterContainer);
  1939. cc->add_child(split_sheet_preview);
  1940. cc->set_h_size_flags(SIZE_EXPAND_FILL);
  1941. cc->set_v_size_flags(SIZE_EXPAND_FILL);
  1942. split_sheet_scroll->add_child(cc);
  1943. MarginContainer *split_sheet_zoom_margin = memnew(MarginContainer);
  1944. split_sheet_panel->add_child(split_sheet_zoom_margin);
  1945. split_sheet_zoom_margin->set_h_size_flags(0);
  1946. split_sheet_zoom_margin->set_v_size_flags(0);
  1947. split_sheet_zoom_margin->add_theme_constant_override("margin_top", 5);
  1948. split_sheet_zoom_margin->add_theme_constant_override("margin_left", 5);
  1949. HBoxContainer *split_sheet_zoom_hb = memnew(HBoxContainer);
  1950. split_sheet_zoom_margin->add_child(split_sheet_zoom_hb);
  1951. split_sheet_zoom_out = memnew(Button);
  1952. split_sheet_zoom_out->set_theme_type_variation(SceneStringName(FlatButton));
  1953. split_sheet_zoom_out->set_focus_mode(FOCUS_ACCESSIBILITY);
  1954. split_sheet_zoom_out->set_tooltip_text(TTR("Zoom Out"));
  1955. split_sheet_zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_out));
  1956. split_sheet_zoom_hb->add_child(split_sheet_zoom_out);
  1957. split_sheet_zoom_reset = memnew(Button);
  1958. split_sheet_zoom_reset->set_theme_type_variation(SceneStringName(FlatButton));
  1959. split_sheet_zoom_reset->set_focus_mode(FOCUS_ACCESSIBILITY);
  1960. split_sheet_zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  1961. split_sheet_zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_reset));
  1962. split_sheet_zoom_hb->add_child(split_sheet_zoom_reset);
  1963. split_sheet_zoom_in = memnew(Button);
  1964. split_sheet_zoom_in->set_theme_type_variation(SceneStringName(FlatButton));
  1965. split_sheet_zoom_in->set_focus_mode(FOCUS_ACCESSIBILITY);
  1966. split_sheet_zoom_in->set_tooltip_text(TTR("Zoom In"));
  1967. split_sheet_zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_in));
  1968. split_sheet_zoom_hb->add_child(split_sheet_zoom_in);
  1969. split_sheet_settings_vb = memnew(VBoxContainer);
  1970. split_sheet_settings_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1971. HBoxContainer *split_sheet_h_hb = memnew(HBoxContainer);
  1972. split_sheet_h_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1973. Label *split_sheet_h_label = memnew(Label(TTR("Horizontal")));
  1974. split_sheet_h_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1975. split_sheet_h_hb->add_child(split_sheet_h_label);
  1976. split_sheet_h = memnew(SpinBox);
  1977. split_sheet_h->set_h_size_flags(SIZE_EXPAND_FILL);
  1978. split_sheet_h->set_min(1);
  1979. split_sheet_h->set_max(128);
  1980. split_sheet_h->set_step(1);
  1981. split_sheet_h->set_select_all_on_focus(true);
  1982. split_sheet_h->set_accessibility_name(TTRC("Horizontal"));
  1983. split_sheet_h_hb->add_child(split_sheet_h);
  1984. split_sheet_h->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  1985. split_sheet_settings_vb->add_child(split_sheet_h_hb);
  1986. HBoxContainer *split_sheet_v_hb = memnew(HBoxContainer);
  1987. split_sheet_v_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1988. Label *split_sheet_v_label = memnew(Label(TTR("Vertical")));
  1989. split_sheet_v_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1990. split_sheet_v_hb->add_child(split_sheet_v_label);
  1991. split_sheet_v = memnew(SpinBox);
  1992. split_sheet_v->set_h_size_flags(SIZE_EXPAND_FILL);
  1993. split_sheet_v->set_min(1);
  1994. split_sheet_v->set_max(128);
  1995. split_sheet_v->set_step(1);
  1996. split_sheet_v->set_select_all_on_focus(true);
  1997. split_sheet_v->set_accessibility_name(TTRC("Vertical"));
  1998. split_sheet_v_hb->add_child(split_sheet_v);
  1999. split_sheet_v->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  2000. split_sheet_settings_vb->add_child(split_sheet_v_hb);
  2001. HBoxContainer *split_sheet_size_hb = memnew(HBoxContainer);
  2002. split_sheet_size_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2003. Label *split_sheet_size_label = memnew(Label(TTR("Size")));
  2004. split_sheet_size_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2005. split_sheet_size_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  2006. split_sheet_size_hb->add_child(split_sheet_size_label);
  2007. VBoxContainer *split_sheet_size_vb = memnew(VBoxContainer);
  2008. split_sheet_size_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2009. split_sheet_size_x = memnew(SpinBox);
  2010. split_sheet_size_x->set_h_size_flags(SIZE_EXPAND_FILL);
  2011. split_sheet_size_x->set_min(1);
  2012. split_sheet_size_x->set_step(1);
  2013. split_sheet_size_x->set_suffix("px");
  2014. split_sheet_size_x->set_select_all_on_focus(true);
  2015. split_sheet_size_x->set_accessibility_name(TTRC("X Size"));
  2016. split_sheet_size_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  2017. split_sheet_size_vb->add_child(split_sheet_size_x);
  2018. split_sheet_size_y = memnew(SpinBox);
  2019. split_sheet_size_y->set_h_size_flags(SIZE_EXPAND_FILL);
  2020. split_sheet_size_y->set_min(1);
  2021. split_sheet_size_y->set_step(1);
  2022. split_sheet_size_y->set_suffix("px");
  2023. split_sheet_size_y->set_select_all_on_focus(true);
  2024. split_sheet_size_y->set_accessibility_name(TTRC("Y Size"));
  2025. split_sheet_size_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  2026. split_sheet_size_vb->add_child(split_sheet_size_y);
  2027. split_sheet_size_hb->add_child(split_sheet_size_vb);
  2028. split_sheet_settings_vb->add_child(split_sheet_size_hb);
  2029. HBoxContainer *split_sheet_sep_hb = memnew(HBoxContainer);
  2030. split_sheet_sep_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2031. Label *split_sheet_sep_label = memnew(Label(TTR("Separation")));
  2032. split_sheet_sep_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2033. split_sheet_sep_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  2034. split_sheet_sep_hb->add_child(split_sheet_sep_label);
  2035. VBoxContainer *split_sheet_sep_vb = memnew(VBoxContainer);
  2036. split_sheet_sep_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2037. split_sheet_sep_x = memnew(SpinBox);
  2038. split_sheet_sep_x->set_min(0);
  2039. split_sheet_sep_x->set_step(1);
  2040. split_sheet_sep_x->set_suffix("px");
  2041. split_sheet_sep_x->set_select_all_on_focus(true);
  2042. split_sheet_sep_x->set_accessibility_name(TTRC("X Separation"));
  2043. split_sheet_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2044. split_sheet_sep_vb->add_child(split_sheet_sep_x);
  2045. split_sheet_sep_y = memnew(SpinBox);
  2046. split_sheet_sep_y->set_min(0);
  2047. split_sheet_sep_y->set_step(1);
  2048. split_sheet_sep_y->set_suffix("px");
  2049. split_sheet_sep_y->set_select_all_on_focus(true);
  2050. split_sheet_sep_y->set_accessibility_name(TTRC("Y Separation"));
  2051. split_sheet_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2052. split_sheet_sep_vb->add_child(split_sheet_sep_y);
  2053. split_sheet_sep_hb->add_child(split_sheet_sep_vb);
  2054. split_sheet_settings_vb->add_child(split_sheet_sep_hb);
  2055. HBoxContainer *split_sheet_offset_hb = memnew(HBoxContainer);
  2056. split_sheet_offset_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2057. Label *split_sheet_offset_label = memnew(Label(TTR("Offset")));
  2058. split_sheet_offset_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2059. split_sheet_offset_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  2060. split_sheet_offset_hb->add_child(split_sheet_offset_label);
  2061. VBoxContainer *split_sheet_offset_vb = memnew(VBoxContainer);
  2062. split_sheet_offset_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2063. split_sheet_offset_x = memnew(SpinBox);
  2064. split_sheet_offset_x->set_min(0);
  2065. split_sheet_offset_x->set_step(1);
  2066. split_sheet_offset_x->set_suffix("px");
  2067. split_sheet_offset_x->set_select_all_on_focus(true);
  2068. split_sheet_offset_x->set_accessibility_name(TTRC("X Offset"));
  2069. split_sheet_offset_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2070. split_sheet_offset_vb->add_child(split_sheet_offset_x);
  2071. split_sheet_offset_y = memnew(SpinBox);
  2072. split_sheet_offset_y->set_min(0);
  2073. split_sheet_offset_y->set_step(1);
  2074. split_sheet_offset_y->set_suffix("px");
  2075. split_sheet_offset_y->set_select_all_on_focus(true);
  2076. split_sheet_offset_y->set_accessibility_name(TTRC("Y Offset"));
  2077. split_sheet_offset_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2078. split_sheet_offset_vb->add_child(split_sheet_offset_y);
  2079. split_sheet_offset_hb->add_child(split_sheet_offset_vb);
  2080. split_sheet_settings_vb->add_child(split_sheet_offset_hb);
  2081. Button *auto_slice = memnew(Button);
  2082. auto_slice->set_text(TTR("Auto Slice"));
  2083. auto_slice->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_auto_slice_sprite_sheet));
  2084. split_sheet_settings_vb->add_child(auto_slice);
  2085. split_sheet_hb->add_child(split_sheet_settings_vb);
  2086. file_split_sheet = memnew(EditorFileDialog);
  2087. file_split_sheet->set_title(TTR("Create Frames from Sprite Sheet"));
  2088. file_split_sheet->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  2089. add_child(file_split_sheet);
  2090. file_split_sheet->connect("file_selected", callable_mp(this, &SpriteFramesEditor::_prepare_sprite_sheet));
  2091. // Config scale.
  2092. scale_ratio = 1.2f;
  2093. thumbnail_default_size = 96 * MAX(1, EDSCALE);
  2094. thumbnail_zoom = MAX(1.0f, EDSCALE);
  2095. max_thumbnail_zoom = 8.0f * MAX(1.0f, EDSCALE);
  2096. min_thumbnail_zoom = 0.1f * MAX(1.0f, EDSCALE);
  2097. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  2098. sheet_zoom = MAX(1.0f, EDSCALE);
  2099. max_sheet_zoom = 128.0f * MAX(1.0f, EDSCALE);
  2100. min_sheet_zoom = 0.01f * MAX(1.0f, EDSCALE);
  2101. _zoom_reset();
  2102. // Ensure the anim search box is wide enough by default.
  2103. // Not by setting its minimum size so it can still be shrunk if desired.
  2104. set_split_offset(56 * EDSCALE);
  2105. }
  2106. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  2107. Ref<SpriteFrames> s;
  2108. AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object);
  2109. if (animated_sprite) {
  2110. s = animated_sprite->get_sprite_frames();
  2111. } else {
  2112. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  2113. if (animated_sprite_3d) {
  2114. s = animated_sprite_3d->get_sprite_frames();
  2115. } else {
  2116. s = p_object;
  2117. }
  2118. }
  2119. frames_editor->edit(s);
  2120. }
  2121. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  2122. AnimatedSprite2D *animated_sprite_2d = Object::cast_to<AnimatedSprite2D>(p_object);
  2123. if (animated_sprite_2d && *animated_sprite_2d->get_sprite_frames()) {
  2124. return true;
  2125. }
  2126. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  2127. if (animated_sprite_3d && *animated_sprite_3d->get_sprite_frames()) {
  2128. return true;
  2129. }
  2130. SpriteFrames *frames = Object::cast_to<SpriteFrames>(p_object);
  2131. if (frames && (frames_editor->get_sprite_frames().is_null() || frames_editor->get_sprite_frames() == frames)) {
  2132. return true;
  2133. }
  2134. return false;
  2135. }
  2136. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  2137. if (p_visible) {
  2138. button->show();
  2139. EditorNode::get_bottom_panel()->make_item_visible(frames_editor);
  2140. } else {
  2141. button->hide();
  2142. if (frames_editor->is_visible_in_tree()) {
  2143. EditorNode::get_bottom_panel()->hide_bottom_panel();
  2144. }
  2145. }
  2146. }
  2147. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin() {
  2148. frames_editor = memnew(SpriteFramesEditor);
  2149. frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2150. button = EditorNode::get_bottom_panel()->add_item(TTRC("SpriteFrames"), frames_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_sprite_frames_bottom_panel", TTRC("Toggle SpriteFrames Bottom Panel")));
  2151. button->hide();
  2152. }