sprite_frames_editor_plugin.cpp 86 KB

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