sprite_frames_editor_plugin.cpp 72 KB

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