sprite_frames_editor_plugin.cpp 81 KB

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