sprite_frames_editor_plugin.cpp 81 KB

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