sprite_frames_editor_plugin.cpp 92 KB

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