sprite_frames_editor_plugin.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/scene_tree_dock.h"
  40. #include "scene/3d/sprite_3d.h"
  41. #include "scene/gui/center_container.h"
  42. #include "scene/gui/margin_container.h"
  43. #include "scene/gui/panel_container.h"
  44. 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) {
  45. p_control->draw_line(p_from, p_from + p_size, p_color);
  46. p_control->draw_line(p_from + p_shadow_offset, p_from + p_size + p_shadow_offset, p_shadow_color);
  47. }
  48. void SpriteFramesEditor::_open_sprite_sheet() {
  49. file_split_sheet->clear_filters();
  50. List<String> extensions;
  51. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  52. for (int i = 0; i < extensions.size(); i++) {
  53. file_split_sheet->add_filter("*." + extensions[i]);
  54. }
  55. file_split_sheet->popup_file_dialog();
  56. }
  57. int SpriteFramesEditor::_sheet_preview_position_to_frame_index(const Point2 &p_position) {
  58. const Size2i offset = _get_offset();
  59. const Size2i frame_size = _get_frame_size();
  60. const Size2i separation = _get_separation();
  61. const Size2i block_size = frame_size + separation;
  62. const Point2i position = p_position / sheet_zoom - offset;
  63. if (position.x % block_size.x > frame_size.x || position.y % block_size.y > frame_size.y) {
  64. return -1; // Gap between frames.
  65. }
  66. const Point2i frame = position / block_size;
  67. const Size2i frame_count = _get_frame_count();
  68. if (frame.x < 0 || frame.y < 0 || frame.x >= frame_count.x || frame.y >= frame_count.y) {
  69. return -1; // Out of bound.
  70. }
  71. return frame_count.x * frame.y + frame.x;
  72. }
  73. void SpriteFramesEditor::_sheet_preview_draw() {
  74. const Size2i frame_count = _get_frame_count();
  75. const Size2i separation = _get_separation();
  76. const Size2 draw_offset = Size2(_get_offset()) * sheet_zoom;
  77. const Size2 draw_sep = Size2(separation) * sheet_zoom;
  78. const Size2 draw_frame_size = Size2(_get_frame_size()) * sheet_zoom;
  79. const Size2 draw_size = draw_frame_size * frame_count + draw_sep * (frame_count - Size2i(1, 1));
  80. const Color line_color = Color(1, 1, 1, 0.3);
  81. const Color shadow_color = Color(0, 0, 0, 0.3);
  82. // Vertical lines.
  83. _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  84. for (int i = 0; i < frame_count.x - 1; i++) {
  85. const Point2 start = draw_offset + Vector2(i * draw_sep.x + (i + 1) * draw_frame_size.x, 0);
  86. if (separation.x == 0) {
  87. _draw_shadowed_line(split_sheet_preview, start, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  88. } else {
  89. const Size2 size = Size2(draw_sep.x, draw_size.y);
  90. split_sheet_preview->draw_rect(Rect2(start, size), line_color);
  91. }
  92. }
  93. _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);
  94. // Horizontal lines.
  95. _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  96. for (int i = 0; i < frame_count.y - 1; i++) {
  97. const Point2 start = draw_offset + Vector2(0, i * draw_sep.y + (i + 1) * draw_frame_size.y);
  98. if (separation.y == 0) {
  99. _draw_shadowed_line(split_sheet_preview, start, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  100. } else {
  101. const Size2 size = Size2(draw_size.x, draw_sep.y);
  102. split_sheet_preview->draw_rect(Rect2(start, size), line_color);
  103. }
  104. }
  105. _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);
  106. if (frames_selected.size() == 0) {
  107. split_sheet_dialog->get_ok_button()->set_disabled(true);
  108. split_sheet_dialog->set_ok_button_text(TTR("No Frames Selected"));
  109. return;
  110. }
  111. Color accent = get_theme_color("accent_color", "Editor");
  112. for (const int &E : frames_selected) {
  113. const int idx = E;
  114. const int x = idx % frame_count.x;
  115. const int y = idx / frame_count.x;
  116. const Point2 pos = draw_offset + Point2(x, y) * (draw_frame_size + draw_sep);
  117. split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 0.35), true);
  118. split_sheet_preview->draw_rect(Rect2(pos, draw_frame_size), Color(0, 0, 0, 1), false);
  119. split_sheet_preview->draw_rect(Rect2(pos + Size2(1, 1), draw_frame_size - Size2(2, 2)), Color(0, 0, 0, 1), false);
  120. split_sheet_preview->draw_rect(Rect2(pos + Size2(2, 2), draw_frame_size - Size2(4, 4)), accent, false);
  121. split_sheet_preview->draw_rect(Rect2(pos + Size2(3, 3), draw_frame_size - Size2(6, 6)), accent, false);
  122. split_sheet_preview->draw_rect(Rect2(pos + Size2(4, 4), draw_frame_size - Size2(8, 8)), Color(0, 0, 0, 1), false);
  123. split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 1), false);
  124. }
  125. split_sheet_dialog->get_ok_button()->set_disabled(false);
  126. split_sheet_dialog->set_ok_button_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size()));
  127. }
  128. void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
  129. const Ref<InputEventMouseButton> mb = p_event;
  130. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  131. const int idx = _sheet_preview_position_to_frame_index(mb->get_position());
  132. if (idx != -1) {
  133. if (mb->is_shift_pressed() && last_frame_selected >= 0) {
  134. //select multiple
  135. int from = idx;
  136. int to = last_frame_selected;
  137. if (from > to) {
  138. SWAP(from, to);
  139. }
  140. for (int i = from; i <= to; i++) {
  141. // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
  142. frames_toggled_by_mouse_hover.insert(idx);
  143. if (mb->is_ctrl_pressed()) {
  144. frames_selected.erase(i);
  145. } else {
  146. frames_selected.insert(i);
  147. }
  148. }
  149. } else {
  150. // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
  151. frames_toggled_by_mouse_hover.insert(idx);
  152. if (frames_selected.has(idx)) {
  153. frames_selected.erase(idx);
  154. } else {
  155. frames_selected.insert(idx);
  156. }
  157. }
  158. }
  159. if (last_frame_selected != idx || idx != -1) {
  160. last_frame_selected = idx;
  161. split_sheet_preview->queue_redraw();
  162. }
  163. }
  164. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  165. frames_toggled_by_mouse_hover.clear();
  166. }
  167. const Ref<InputEventMouseMotion> mm = p_event;
  168. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  169. // Select by holding down the mouse button on frames.
  170. const int idx = _sheet_preview_position_to_frame_index(mm->get_position());
  171. if (idx != -1 && !frames_toggled_by_mouse_hover.has(idx)) {
  172. // Only allow toggling each tile once per mouse hold.
  173. // Otherwise, the selection would constantly "flicker" in and out when moving the mouse cursor.
  174. // The mouse button must be released before it can be toggled again.
  175. frames_toggled_by_mouse_hover.insert(idx);
  176. if (frames_selected.has(idx)) {
  177. frames_selected.erase(idx);
  178. } else {
  179. frames_selected.insert(idx);
  180. }
  181. last_frame_selected = idx;
  182. split_sheet_preview->queue_redraw();
  183. }
  184. }
  185. }
  186. void SpriteFramesEditor::_sheet_scroll_input(const Ref<InputEvent> &p_event) {
  187. const Ref<InputEventMouseButton> mb = p_event;
  188. if (mb.is_valid()) {
  189. // Zoom in/out using Ctrl + mouse wheel. This is done on the ScrollContainer
  190. // to allow performing this action anywhere, even if the cursor isn't
  191. // hovering the texture in the workspace.
  192. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  193. _sheet_zoom_on_position(scale_ratio, mb->get_position());
  194. // Don't scroll up after zooming in.
  195. split_sheet_scroll->accept_event();
  196. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  197. _sheet_zoom_on_position(1 / scale_ratio, mb->get_position());
  198. // Don't scroll down after zooming out.
  199. split_sheet_scroll->accept_event();
  200. }
  201. }
  202. const Ref<InputEventMouseMotion> mm = p_event;
  203. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_MIDDLE) != MouseButton::NONE) {
  204. const Vector2 dragged = Input::get_singleton()->warp_mouse_motion(mm, split_sheet_scroll->get_global_rect());
  205. split_sheet_scroll->set_h_scroll(split_sheet_scroll->get_h_scroll() - dragged.x);
  206. split_sheet_scroll->set_v_scroll(split_sheet_scroll->get_v_scroll() - dragged.y);
  207. }
  208. }
  209. void SpriteFramesEditor::_sheet_add_frames() {
  210. const Size2i frame_count = _get_frame_count();
  211. const Size2i frame_size = _get_frame_size();
  212. const Size2i offset = _get_offset();
  213. const Size2i separation = _get_separation();
  214. undo_redo->create_action(TTR("Add Frame"));
  215. int fc = frames->get_frame_count(edited_anim);
  216. for (const int &E : frames_selected) {
  217. int idx = E;
  218. const Point2 frame_coords(idx % frame_count.x, idx / frame_count.x);
  219. Ref<AtlasTexture> at;
  220. at.instantiate();
  221. at->set_atlas(split_sheet_preview->get_texture());
  222. at->set_region(Rect2(offset + frame_coords * (frame_size + separation), frame_size));
  223. undo_redo->add_do_method(frames, "add_frame", edited_anim, at, -1);
  224. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, fc);
  225. }
  226. undo_redo->add_do_method(this, "_update_library");
  227. undo_redo->add_undo_method(this, "_update_library");
  228. undo_redo->commit_action();
  229. }
  230. void SpriteFramesEditor::_sheet_zoom_on_position(float p_zoom, const Vector2 &p_position) {
  231. const float old_zoom = sheet_zoom;
  232. sheet_zoom = CLAMP(sheet_zoom * p_zoom, min_sheet_zoom, max_sheet_zoom);
  233. const Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  234. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  235. Vector2 offset = Vector2(split_sheet_scroll->get_h_scroll(), split_sheet_scroll->get_v_scroll());
  236. offset = (offset + p_position) / old_zoom * sheet_zoom - p_position;
  237. split_sheet_scroll->set_h_scroll(offset.x);
  238. split_sheet_scroll->set_v_scroll(offset.y);
  239. }
  240. void SpriteFramesEditor::_sheet_zoom_in() {
  241. _sheet_zoom_on_position(scale_ratio, Vector2());
  242. }
  243. void SpriteFramesEditor::_sheet_zoom_out() {
  244. _sheet_zoom_on_position(1 / scale_ratio, Vector2());
  245. }
  246. void SpriteFramesEditor::_sheet_zoom_reset() {
  247. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  248. sheet_zoom = MAX(1.0f, EDSCALE);
  249. Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  250. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  251. }
  252. void SpriteFramesEditor::_sheet_select_clear_all_frames() {
  253. bool should_clear = true;
  254. for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
  255. if (!frames_selected.has(i)) {
  256. frames_selected.insert(i);
  257. should_clear = false;
  258. }
  259. }
  260. if (should_clear) {
  261. frames_selected.clear();
  262. }
  263. split_sheet_preview->queue_redraw();
  264. }
  265. void SpriteFramesEditor::_sheet_spin_changed(double p_value, int p_dominant_param) {
  266. if (updating_split_settings) {
  267. return;
  268. }
  269. updating_split_settings = true;
  270. if (p_dominant_param != PARAM_USE_CURRENT) {
  271. dominant_param = p_dominant_param;
  272. }
  273. const Size2i texture_size = split_sheet_preview->get_texture()->get_size();
  274. const Size2i size = texture_size - _get_offset();
  275. switch (dominant_param) {
  276. case PARAM_SIZE: {
  277. const Size2i frame_size = _get_frame_size();
  278. const Size2i offset_max = texture_size - frame_size;
  279. split_sheet_offset_x->set_max(offset_max.x);
  280. split_sheet_offset_y->set_max(offset_max.y);
  281. const Size2i sep_max = size - frame_size * 2;
  282. split_sheet_sep_x->set_max(sep_max.x);
  283. split_sheet_sep_y->set_max(sep_max.y);
  284. const Size2i separation = _get_separation();
  285. const Size2i count = (size + separation) / (frame_size + separation);
  286. split_sheet_h->set_value(count.x);
  287. split_sheet_v->set_value(count.y);
  288. } break;
  289. case PARAM_FRAME_COUNT: {
  290. const Size2i count = _get_frame_count();
  291. const Size2i offset_max = texture_size - count;
  292. split_sheet_offset_x->set_max(offset_max.x);
  293. split_sheet_offset_y->set_max(offset_max.y);
  294. const Size2i gap_count = count - Size2i(1, 1);
  295. split_sheet_sep_x->set_max(gap_count.x == 0 ? size.x : (size.x - count.x) / gap_count.x);
  296. split_sheet_sep_y->set_max(gap_count.y == 0 ? size.y : (size.y - count.y) / gap_count.y);
  297. const Size2i separation = _get_separation();
  298. const Size2i frame_size = (size - separation * gap_count) / count;
  299. split_sheet_size_x->set_value(frame_size.x);
  300. split_sheet_size_y->set_value(frame_size.y);
  301. } break;
  302. }
  303. updating_split_settings = false;
  304. frames_selected.clear();
  305. last_frame_selected = -1;
  306. split_sheet_preview->queue_redraw();
  307. }
  308. void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
  309. Ref<Texture2D> texture = ResourceLoader::load(p_file);
  310. if (!texture.is_valid()) {
  311. EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
  312. ERR_FAIL_COND(!texture.is_valid());
  313. }
  314. frames_selected.clear();
  315. last_frame_selected = -1;
  316. bool new_texture = texture != split_sheet_preview->get_texture();
  317. split_sheet_preview->set_texture(texture);
  318. if (new_texture) {
  319. // Reset spin max.
  320. const Size2i size = texture->get_size();
  321. split_sheet_size_x->set_max(size.x);
  322. split_sheet_size_y->set_max(size.y);
  323. split_sheet_sep_x->set_max(size.x);
  324. split_sheet_sep_y->set_max(size.y);
  325. split_sheet_offset_x->set_max(size.x);
  326. split_sheet_offset_y->set_max(size.y);
  327. // Different texture, reset to 4x4.
  328. dominant_param = PARAM_FRAME_COUNT;
  329. updating_split_settings = true;
  330. split_sheet_h->set_value(4);
  331. split_sheet_v->set_value(4);
  332. split_sheet_size_x->set_value(size.x / 4);
  333. split_sheet_size_y->set_value(size.y / 4);
  334. split_sheet_sep_x->set_value(0);
  335. split_sheet_sep_y->set_value(0);
  336. split_sheet_offset_x->set_value(0);
  337. split_sheet_offset_y->set_value(0);
  338. updating_split_settings = false;
  339. // Reset zoom.
  340. _sheet_zoom_reset();
  341. }
  342. split_sheet_dialog->popup_centered_ratio(0.65);
  343. }
  344. void SpriteFramesEditor::_notification(int p_what) {
  345. switch (p_what) {
  346. case NOTIFICATION_ENTER_TREE:
  347. case NOTIFICATION_THEME_CHANGED: {
  348. load->set_icon(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")));
  349. load_sheet->set_icon(get_theme_icon(SNAME("SpriteSheet"), SNAME("EditorIcons")));
  350. copy->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  351. paste->set_icon(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")));
  352. empty_before->set_icon(get_theme_icon(SNAME("InsertBefore"), SNAME("EditorIcons")));
  353. empty_after->set_icon(get_theme_icon(SNAME("InsertAfter"), SNAME("EditorIcons")));
  354. move_up->set_icon(get_theme_icon(SNAME("MoveLeft"), SNAME("EditorIcons")));
  355. move_down->set_icon(get_theme_icon(SNAME("MoveRight"), SNAME("EditorIcons")));
  356. delete_frame->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  357. zoom_out->set_icon(get_theme_icon(SNAME("ZoomLess"), SNAME("EditorIcons")));
  358. zoom_reset->set_icon(get_theme_icon(SNAME("ZoomReset"), SNAME("EditorIcons")));
  359. zoom_in->set_icon(get_theme_icon(SNAME("ZoomMore"), SNAME("EditorIcons")));
  360. add_anim->set_icon(get_theme_icon(SNAME("New"), SNAME("EditorIcons")));
  361. delete_anim->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  362. anim_search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  363. split_sheet_zoom_out->set_icon(get_theme_icon(SNAME("ZoomLess"), SNAME("EditorIcons")));
  364. split_sheet_zoom_reset->set_icon(get_theme_icon(SNAME("ZoomReset"), SNAME("EditorIcons")));
  365. split_sheet_zoom_in->set_icon(get_theme_icon(SNAME("ZoomMore"), SNAME("EditorIcons")));
  366. split_sheet_scroll->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  367. } break;
  368. case NOTIFICATION_READY: {
  369. add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up.
  370. } break;
  371. }
  372. }
  373. void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_at_pos) {
  374. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  375. List<Ref<Texture2D>> resources;
  376. for (int i = 0; i < p_path.size(); i++) {
  377. Ref<Texture2D> resource;
  378. resource = ResourceLoader::load(p_path[i]);
  379. if (resource.is_null()) {
  380. dialog->set_text(TTR("ERROR: Couldn't load frame resource!"));
  381. dialog->set_title(TTR("Error!"));
  382. //dialog->get_cancel()->set_text("Close");
  383. dialog->set_ok_button_text(TTR("Close"));
  384. dialog->popup_centered();
  385. return; ///beh should show an error i guess
  386. }
  387. resources.push_back(resource);
  388. }
  389. if (resources.is_empty()) {
  390. return;
  391. }
  392. undo_redo->create_action(TTR("Add Frame"));
  393. int fc = frames->get_frame_count(edited_anim);
  394. int count = 0;
  395. for (const Ref<Texture2D> &E : resources) {
  396. undo_redo->add_do_method(frames, "add_frame", edited_anim, E, p_at_pos == -1 ? -1 : p_at_pos + count);
  397. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
  398. count++;
  399. }
  400. undo_redo->add_do_method(this, "_update_library");
  401. undo_redo->add_undo_method(this, "_update_library");
  402. undo_redo->commit_action();
  403. }
  404. Size2i SpriteFramesEditor::_get_frame_count() const {
  405. return Size2i(split_sheet_h->get_value(), split_sheet_v->get_value());
  406. }
  407. Size2i SpriteFramesEditor::_get_frame_size() const {
  408. return Size2i(split_sheet_size_x->get_value(), split_sheet_size_y->get_value());
  409. }
  410. Size2i SpriteFramesEditor::_get_offset() const {
  411. return Size2i(split_sheet_offset_x->get_value(), split_sheet_offset_y->get_value());
  412. }
  413. Size2i SpriteFramesEditor::_get_separation() const {
  414. return Size2i(split_sheet_sep_x->get_value(), split_sheet_sep_y->get_value());
  415. }
  416. void SpriteFramesEditor::_load_pressed() {
  417. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  418. loading_scene = false;
  419. file->clear_filters();
  420. List<String> extensions;
  421. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  422. for (int i = 0; i < extensions.size(); i++) {
  423. file->add_filter("*." + extensions[i]);
  424. }
  425. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  426. file->popup_file_dialog();
  427. }
  428. void SpriteFramesEditor::_paste_pressed() {
  429. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  430. Ref<Texture2D> r = EditorSettings::get_singleton()->get_resource_clipboard();
  431. if (!r.is_valid()) {
  432. dialog->set_text(TTR("Resource clipboard is empty or not a texture!"));
  433. dialog->set_title(TTR("Error!"));
  434. //dialog->get_cancel()->set_text("Close");
  435. dialog->set_ok_button_text(TTR("Close"));
  436. dialog->popup_centered();
  437. return; ///beh should show an error i guess
  438. }
  439. undo_redo->create_action(TTR("Paste Frame"));
  440. undo_redo->add_do_method(frames, "add_frame", edited_anim, r);
  441. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, frames->get_frame_count(edited_anim));
  442. undo_redo->add_do_method(this, "_update_library");
  443. undo_redo->add_undo_method(this, "_update_library");
  444. undo_redo->commit_action();
  445. }
  446. void SpriteFramesEditor::_copy_pressed() {
  447. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  448. if (tree->get_current() < 0) {
  449. return;
  450. }
  451. Ref<Texture2D> r = frames->get_frame(edited_anim, tree->get_current());
  452. if (!r.is_valid()) {
  453. return;
  454. }
  455. EditorSettings::get_singleton()->set_resource_clipboard(r);
  456. }
  457. void SpriteFramesEditor::_empty_pressed() {
  458. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  459. int from = -1;
  460. if (tree->get_current() >= 0) {
  461. from = tree->get_current();
  462. sel = from;
  463. } else {
  464. from = frames->get_frame_count(edited_anim);
  465. }
  466. Ref<Texture2D> r;
  467. undo_redo->create_action(TTR("Add Empty"));
  468. undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from);
  469. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from);
  470. undo_redo->add_do_method(this, "_update_library");
  471. undo_redo->add_undo_method(this, "_update_library");
  472. undo_redo->commit_action();
  473. }
  474. void SpriteFramesEditor::_empty2_pressed() {
  475. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  476. int from = -1;
  477. if (tree->get_current() >= 0) {
  478. from = tree->get_current();
  479. sel = from;
  480. } else {
  481. from = frames->get_frame_count(edited_anim);
  482. }
  483. Ref<Texture2D> r;
  484. undo_redo->create_action(TTR("Add Empty"));
  485. undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from + 1);
  486. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from + 1);
  487. undo_redo->add_do_method(this, "_update_library");
  488. undo_redo->add_undo_method(this, "_update_library");
  489. undo_redo->commit_action();
  490. }
  491. void SpriteFramesEditor::_up_pressed() {
  492. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  493. if (tree->get_current() < 0) {
  494. return;
  495. }
  496. int to_move = tree->get_current();
  497. if (to_move < 1) {
  498. return;
  499. }
  500. sel = to_move;
  501. sel -= 1;
  502. undo_redo->create_action(TTR("Delete Resource"));
  503. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move - 1));
  504. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move));
  505. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
  506. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move - 1));
  507. undo_redo->add_do_method(this, "_update_library");
  508. undo_redo->add_undo_method(this, "_update_library");
  509. undo_redo->commit_action();
  510. }
  511. void SpriteFramesEditor::_down_pressed() {
  512. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  513. if (tree->get_current() < 0) {
  514. return;
  515. }
  516. int to_move = tree->get_current();
  517. if (to_move < 0 || to_move >= frames->get_frame_count(edited_anim) - 1) {
  518. return;
  519. }
  520. sel = to_move;
  521. sel += 1;
  522. undo_redo->create_action(TTR("Delete Resource"));
  523. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move + 1));
  524. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move));
  525. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
  526. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move + 1));
  527. undo_redo->add_do_method(this, "_update_library");
  528. undo_redo->add_undo_method(this, "_update_library");
  529. undo_redo->commit_action();
  530. }
  531. void SpriteFramesEditor::_delete_pressed() {
  532. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  533. if (tree->get_current() < 0) {
  534. return;
  535. }
  536. int to_delete = tree->get_current();
  537. if (to_delete < 0 || to_delete >= frames->get_frame_count(edited_anim)) {
  538. return;
  539. }
  540. undo_redo->create_action(TTR("Delete Resource"));
  541. undo_redo->add_do_method(frames, "remove_frame", edited_anim, to_delete);
  542. undo_redo->add_undo_method(frames, "add_frame", edited_anim, frames->get_frame(edited_anim, to_delete), to_delete);
  543. undo_redo->add_do_method(this, "_update_library");
  544. undo_redo->add_undo_method(this, "_update_library");
  545. undo_redo->commit_action();
  546. }
  547. void SpriteFramesEditor::_animation_select() {
  548. if (updating) {
  549. return;
  550. }
  551. if (frames->has_animation(edited_anim)) {
  552. double value = anim_speed->get_line_edit()->get_text().to_float();
  553. if (!Math::is_equal_approx(value, (double)frames->get_animation_speed(edited_anim))) {
  554. _animation_fps_changed(value);
  555. }
  556. }
  557. TreeItem *selected = animations->get_selected();
  558. ERR_FAIL_COND(!selected);
  559. edited_anim = selected->get_text(0);
  560. _update_library(true);
  561. }
  562. static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
  563. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  564. if (!edited) {
  565. return;
  566. }
  567. if (p_node != edited && p_node->get_owner() != edited) {
  568. return;
  569. }
  570. {
  571. AnimatedSprite2D *as = Object::cast_to<AnimatedSprite2D>(p_node);
  572. if (as && as->get_sprite_frames() == p_sfames) {
  573. r_nodes->push_back(p_node);
  574. }
  575. }
  576. {
  577. AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
  578. if (as && as->get_sprite_frames() == p_sfames) {
  579. r_nodes->push_back(p_node);
  580. }
  581. }
  582. for (int i = 0; i < p_node->get_child_count(); i++) {
  583. _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
  584. }
  585. }
  586. void SpriteFramesEditor::_animation_name_edited() {
  587. if (updating) {
  588. return;
  589. }
  590. if (!frames->has_animation(edited_anim)) {
  591. return;
  592. }
  593. TreeItem *edited = animations->get_edited();
  594. if (!edited) {
  595. return;
  596. }
  597. String new_name = edited->get_text(0);
  598. if (new_name == String(edited_anim)) {
  599. return;
  600. }
  601. new_name = new_name.replace("/", "_").replace(",", " ");
  602. String name = new_name;
  603. int counter = 0;
  604. while (frames->has_animation(name)) {
  605. counter++;
  606. name = new_name + " " + itos(counter);
  607. }
  608. List<Node *> nodes;
  609. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  610. undo_redo->create_action(TTR("Rename Animation"));
  611. undo_redo->add_do_method(frames, "rename_animation", edited_anim, name);
  612. undo_redo->add_undo_method(frames, "rename_animation", name, edited_anim);
  613. for (Node *E : nodes) {
  614. String current = E->call("get_animation");
  615. undo_redo->add_do_method(E, "set_animation", name);
  616. undo_redo->add_undo_method(E, "set_animation", edited_anim);
  617. }
  618. undo_redo->add_do_method(this, "_update_library");
  619. undo_redo->add_undo_method(this, "_update_library");
  620. edited_anim = name;
  621. undo_redo->commit_action();
  622. }
  623. void SpriteFramesEditor::_animation_add() {
  624. String name = "New Anim";
  625. int counter = 0;
  626. while (frames->has_animation(name)) {
  627. counter++;
  628. name = "New Anim " + itos(counter);
  629. }
  630. List<Node *> nodes;
  631. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  632. undo_redo->create_action(TTR("Add Animation"));
  633. undo_redo->add_do_method(frames, "add_animation", name);
  634. undo_redo->add_undo_method(frames, "remove_animation", name);
  635. undo_redo->add_do_method(this, "_update_library");
  636. undo_redo->add_undo_method(this, "_update_library");
  637. for (Node *E : nodes) {
  638. String current = E->call("get_animation");
  639. undo_redo->add_do_method(E, "set_animation", name);
  640. undo_redo->add_undo_method(E, "set_animation", current);
  641. }
  642. edited_anim = name;
  643. undo_redo->commit_action();
  644. animations->grab_focus();
  645. }
  646. void SpriteFramesEditor::_animation_remove() {
  647. if (updating) {
  648. return;
  649. }
  650. if (!frames->has_animation(edited_anim)) {
  651. return;
  652. }
  653. delete_dialog->set_text(TTR("Delete Animation?"));
  654. delete_dialog->popup_centered();
  655. }
  656. void SpriteFramesEditor::_animation_remove_confirmed() {
  657. undo_redo->create_action(TTR("Remove Animation"));
  658. undo_redo->add_do_method(frames, "remove_animation", edited_anim);
  659. undo_redo->add_undo_method(frames, "add_animation", edited_anim);
  660. undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  661. undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  662. int fc = frames->get_frame_count(edited_anim);
  663. for (int i = 0; i < fc; i++) {
  664. Ref<Texture2D> frame = frames->get_frame(edited_anim, i);
  665. undo_redo->add_undo_method(frames, "add_frame", edited_anim, frame);
  666. }
  667. undo_redo->add_do_method(this, "_update_library");
  668. undo_redo->add_undo_method(this, "_update_library");
  669. edited_anim = StringName();
  670. undo_redo->commit_action();
  671. }
  672. void SpriteFramesEditor::_animation_search_text_changed(const String &p_text) {
  673. _update_library();
  674. }
  675. void SpriteFramesEditor::_animation_loop_changed() {
  676. if (updating) {
  677. return;
  678. }
  679. undo_redo->create_action(TTR("Change Animation Loop"));
  680. undo_redo->add_do_method(frames, "set_animation_loop", edited_anim, anim_loop->is_pressed());
  681. undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  682. undo_redo->add_do_method(this, "_update_library", true);
  683. undo_redo->add_undo_method(this, "_update_library", true);
  684. undo_redo->commit_action();
  685. }
  686. void SpriteFramesEditor::_animation_fps_changed(double p_value) {
  687. if (updating) {
  688. return;
  689. }
  690. undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS);
  691. undo_redo->add_do_method(frames, "set_animation_speed", edited_anim, p_value);
  692. undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  693. undo_redo->add_do_method(this, "_update_library", true);
  694. undo_redo->add_undo_method(this, "_update_library", true);
  695. undo_redo->commit_action();
  696. }
  697. void SpriteFramesEditor::_tree_input(const Ref<InputEvent> &p_event) {
  698. const Ref<InputEventMouseButton> mb = p_event;
  699. if (mb.is_valid()) {
  700. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  701. _zoom_in();
  702. // Don't scroll up after zooming in.
  703. accept_event();
  704. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  705. _zoom_out();
  706. // Don't scroll down after zooming out.
  707. accept_event();
  708. }
  709. }
  710. }
  711. void SpriteFramesEditor::_zoom_in() {
  712. // Do not zoom in or out with no visible frames
  713. if (frames->get_frame_count(edited_anim) <= 0) {
  714. return;
  715. }
  716. if (thumbnail_zoom < max_thumbnail_zoom) {
  717. thumbnail_zoom *= scale_ratio;
  718. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  719. tree->set_fixed_column_width(thumbnail_size * 3 / 2);
  720. tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  721. }
  722. }
  723. void SpriteFramesEditor::_zoom_out() {
  724. // Do not zoom in or out with no visible frames
  725. if (frames->get_frame_count(edited_anim) <= 0) {
  726. return;
  727. }
  728. if (thumbnail_zoom > min_thumbnail_zoom) {
  729. thumbnail_zoom /= scale_ratio;
  730. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  731. tree->set_fixed_column_width(thumbnail_size * 3 / 2);
  732. tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  733. }
  734. }
  735. void SpriteFramesEditor::_zoom_reset() {
  736. thumbnail_zoom = MAX(1.0f, EDSCALE);
  737. tree->set_fixed_column_width(thumbnail_default_size * 3 / 2);
  738. tree->set_fixed_icon_size(Size2(thumbnail_default_size, thumbnail_default_size));
  739. }
  740. void SpriteFramesEditor::_update_library(bool p_skip_selector) {
  741. updating = true;
  742. if (!p_skip_selector) {
  743. animations->clear();
  744. TreeItem *anim_root = animations->create_item();
  745. List<StringName> anim_names;
  746. frames->get_animation_list(&anim_names);
  747. anim_names.sort_custom<StringName::AlphCompare>();
  748. bool searching = anim_search_box->get_text().size();
  749. String searched_string = searching ? anim_search_box->get_text().to_lower() : String();
  750. for (const StringName &E : anim_names) {
  751. String name = E;
  752. if (searching && name.to_lower().find(searched_string) < 0) {
  753. continue;
  754. }
  755. TreeItem *it = animations->create_item(anim_root);
  756. it->set_metadata(0, name);
  757. it->set_text(0, name);
  758. it->set_editable(0, true);
  759. if (E == edited_anim) {
  760. it->select(0);
  761. }
  762. }
  763. }
  764. tree->clear();
  765. if (!frames->has_animation(edited_anim)) {
  766. updating = false;
  767. return;
  768. }
  769. if (sel >= frames->get_frame_count(edited_anim)) {
  770. sel = frames->get_frame_count(edited_anim) - 1;
  771. } else if (sel < 0 && frames->get_frame_count(edited_anim)) {
  772. sel = 0;
  773. }
  774. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  775. String name;
  776. Ref<Texture2D> frame = frames->get_frame(edited_anim, i);
  777. if (frame.is_null()) {
  778. name = itos(i) + ": " + TTR("(empty)");
  779. } else {
  780. name = itos(i) + ": " + frame->get_name();
  781. }
  782. tree->add_item(name, frame);
  783. if (frame.is_valid()) {
  784. String tooltip = frame->get_path();
  785. // Frame is often saved as an AtlasTexture subresource within a scene/resource file,
  786. // thus its path might be not what the user is looking for. So we're also showing
  787. // subsequent source texture paths.
  788. String prefix = String::utf8("┖╴");
  789. Ref<AtlasTexture> at = frame;
  790. while (at.is_valid() && at->get_atlas().is_valid()) {
  791. tooltip += "\n" + prefix + at->get_atlas()->get_path();
  792. prefix = " " + prefix;
  793. at = at->get_atlas();
  794. }
  795. tree->set_item_tooltip(-1, tooltip);
  796. }
  797. if (sel == i) {
  798. tree->select(tree->get_item_count() - 1);
  799. }
  800. }
  801. anim_speed->set_value(frames->get_animation_speed(edited_anim));
  802. anim_loop->set_pressed(frames->get_animation_loop(edited_anim));
  803. updating = false;
  804. }
  805. void SpriteFramesEditor::edit(SpriteFrames *p_frames) {
  806. bool new_read_only_state = false;
  807. if (p_frames) {
  808. new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_frames);
  809. }
  810. if (frames == p_frames && new_read_only_state == read_only) {
  811. return;
  812. }
  813. frames = p_frames;
  814. read_only = new_read_only_state;
  815. if (p_frames) {
  816. if (!p_frames->has_animation(edited_anim)) {
  817. List<StringName> anim_names;
  818. frames->get_animation_list(&anim_names);
  819. anim_names.sort_custom<StringName::AlphCompare>();
  820. if (anim_names.size()) {
  821. edited_anim = anim_names.front()->get();
  822. } else {
  823. edited_anim = StringName();
  824. }
  825. }
  826. _update_library();
  827. // Clear zoom and split sheet texture
  828. split_sheet_preview->set_texture(Ref<Texture2D>());
  829. _zoom_reset();
  830. } else {
  831. hide();
  832. }
  833. add_anim->set_disabled(read_only);
  834. delete_anim->set_disabled(read_only);
  835. anim_speed->set_editable(!read_only);
  836. anim_loop->set_disabled(read_only);
  837. load->set_disabled(read_only);
  838. load_sheet->set_disabled(read_only);
  839. copy->set_disabled(read_only);
  840. paste->set_disabled(read_only);
  841. empty_before->set_disabled(read_only);
  842. empty_after->set_disabled(read_only);
  843. move_up->set_disabled(read_only);
  844. move_down->set_disabled(read_only);
  845. delete_frame->set_disabled(read_only);
  846. }
  847. void SpriteFramesEditor::set_undo_redo(Ref<EditorUndoRedoManager> p_undo_redo) {
  848. undo_redo = p_undo_redo;
  849. }
  850. Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  851. if (read_only) {
  852. return false;
  853. }
  854. if (!frames->has_animation(edited_anim)) {
  855. return false;
  856. }
  857. int idx = tree->get_item_at_position(p_point, true);
  858. if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) {
  859. return Variant();
  860. }
  861. Ref<Resource> frame = frames->get_frame(edited_anim, idx);
  862. if (frame.is_null()) {
  863. return Variant();
  864. }
  865. Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
  866. drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
  867. return drag_data;
  868. }
  869. bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  870. if (read_only) {
  871. return false;
  872. }
  873. Dictionary d = p_data;
  874. if (!d.has("type")) {
  875. return false;
  876. }
  877. // reordering frames
  878. if (d.has("from") && (Object *)(d["from"]) == tree) {
  879. return true;
  880. }
  881. if (String(d["type"]) == "resource" && d.has("resource")) {
  882. Ref<Resource> r = d["resource"];
  883. Ref<Texture2D> texture = r;
  884. if (texture.is_valid()) {
  885. return true;
  886. }
  887. }
  888. if (String(d["type"]) == "files") {
  889. Vector<String> files = d["files"];
  890. if (files.size() == 0) {
  891. return false;
  892. }
  893. for (int i = 0; i < files.size(); i++) {
  894. String f = files[i];
  895. String ftype = EditorFileSystem::get_singleton()->get_file_type(f);
  896. if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
  897. return false;
  898. }
  899. }
  900. return true;
  901. }
  902. return false;
  903. }
  904. void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  905. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  906. return;
  907. }
  908. Dictionary d = p_data;
  909. if (!d.has("type")) {
  910. return;
  911. }
  912. int at_pos = tree->get_item_at_position(p_point, true);
  913. if (String(d["type"]) == "resource" && d.has("resource")) {
  914. Ref<Resource> r = d["resource"];
  915. Ref<Texture2D> texture = r;
  916. if (texture.is_valid()) {
  917. bool reorder = false;
  918. if (d.has("from") && (Object *)(d["from"]) == tree) {
  919. reorder = true;
  920. }
  921. if (reorder) { //drop is from reordering frames
  922. int from_frame = -1;
  923. if (d.has("frame")) {
  924. from_frame = d["frame"];
  925. }
  926. undo_redo->create_action(TTR("Move Frame"));
  927. undo_redo->add_do_method(frames, "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
  928. undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
  929. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
  930. undo_redo->add_undo_method(frames, "add_frame", edited_anim, texture, from_frame);
  931. undo_redo->add_do_method(this, "_update_library");
  932. undo_redo->add_undo_method(this, "_update_library");
  933. undo_redo->commit_action();
  934. } else {
  935. undo_redo->create_action(TTR("Add Frame"));
  936. undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
  937. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
  938. undo_redo->add_do_method(this, "_update_library");
  939. undo_redo->add_undo_method(this, "_update_library");
  940. undo_redo->commit_action();
  941. }
  942. }
  943. }
  944. if (String(d["type"]) == "files") {
  945. Vector<String> files = d["files"];
  946. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  947. _prepare_sprite_sheet(files[0]);
  948. } else {
  949. _file_load_request(files, at_pos);
  950. }
  951. }
  952. }
  953. void SpriteFramesEditor::_bind_methods() {
  954. ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
  955. ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &SpriteFramesEditor::get_drag_data_fw);
  956. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &SpriteFramesEditor::can_drop_data_fw);
  957. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &SpriteFramesEditor::drop_data_fw);
  958. }
  959. SpriteFramesEditor::SpriteFramesEditor() {
  960. VBoxContainer *vbc_animlist = memnew(VBoxContainer);
  961. add_child(vbc_animlist);
  962. vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
  963. VBoxContainer *sub_vb = memnew(VBoxContainer);
  964. vbc_animlist->add_margin_child(TTR("Animations:"), sub_vb, true);
  965. sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  966. HBoxContainer *hbc_animlist = memnew(HBoxContainer);
  967. sub_vb->add_child(hbc_animlist);
  968. add_anim = memnew(Button);
  969. add_anim->set_flat(true);
  970. hbc_animlist->add_child(add_anim);
  971. add_anim->connect("pressed", callable_mp(this, &SpriteFramesEditor::_animation_add));
  972. delete_anim = memnew(Button);
  973. delete_anim->set_flat(true);
  974. hbc_animlist->add_child(delete_anim);
  975. delete_anim->set_disabled(true);
  976. delete_anim->connect("pressed", callable_mp(this, &SpriteFramesEditor::_animation_remove));
  977. anim_search_box = memnew(LineEdit);
  978. hbc_animlist->add_child(anim_search_box);
  979. anim_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  980. anim_search_box->set_placeholder(TTR("Filter Animations"));
  981. anim_search_box->set_clear_button_enabled(true);
  982. anim_search_box->connect("text_changed", callable_mp(this, &SpriteFramesEditor::_animation_search_text_changed));
  983. animations = memnew(Tree);
  984. sub_vb->add_child(animations);
  985. animations->set_v_size_flags(SIZE_EXPAND_FILL);
  986. animations->set_hide_root(true);
  987. animations->connect("cell_selected", callable_mp(this, &SpriteFramesEditor::_animation_select));
  988. animations->connect("item_edited", callable_mp(this, &SpriteFramesEditor::_animation_name_edited));
  989. animations->set_allow_reselect(true);
  990. add_anim->set_shortcut_context(animations);
  991. add_anim->set_shortcut(ED_SHORTCUT("sprite_frames/new_animation", TTR("Add Animation"), KeyModifierMask::CMD_OR_CTRL | Key::N));
  992. delete_anim->set_shortcut_context(animations);
  993. delete_anim->set_shortcut(ED_SHORTCUT("sprite_frames/delete_animation", TTR("Delete Animation"), Key::KEY_DELETE));
  994. HBoxContainer *hbc_anim_speed = memnew(HBoxContainer);
  995. hbc_anim_speed->add_child(memnew(Label(TTR("Speed:"))));
  996. vbc_animlist->add_child(hbc_anim_speed);
  997. anim_speed = memnew(SpinBox);
  998. anim_speed->set_suffix(TTR("FPS"));
  999. anim_speed->set_min(0);
  1000. anim_speed->set_max(100);
  1001. anim_speed->set_step(0.01);
  1002. anim_speed->set_h_size_flags(SIZE_EXPAND_FILL);
  1003. hbc_anim_speed->add_child(anim_speed);
  1004. anim_speed->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_animation_fps_changed));
  1005. anim_loop = memnew(CheckButton);
  1006. anim_loop->set_text(TTR("Loop"));
  1007. vbc_animlist->add_child(anim_loop);
  1008. anim_loop->connect("pressed", callable_mp(this, &SpriteFramesEditor::_animation_loop_changed));
  1009. VBoxContainer *vbc = memnew(VBoxContainer);
  1010. add_child(vbc);
  1011. vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  1012. sub_vb = memnew(VBoxContainer);
  1013. vbc->add_margin_child(TTR("Animation Frames:"), sub_vb, true);
  1014. HBoxContainer *hbc = memnew(HBoxContainer);
  1015. sub_vb->add_child(hbc);
  1016. load = memnew(Button);
  1017. load->set_flat(true);
  1018. hbc->add_child(load);
  1019. load_sheet = memnew(Button);
  1020. load_sheet->set_flat(true);
  1021. hbc->add_child(load_sheet);
  1022. hbc->add_child(memnew(VSeparator));
  1023. copy = memnew(Button);
  1024. copy->set_flat(true);
  1025. hbc->add_child(copy);
  1026. paste = memnew(Button);
  1027. paste->set_flat(true);
  1028. hbc->add_child(paste);
  1029. hbc->add_child(memnew(VSeparator));
  1030. empty_before = memnew(Button);
  1031. empty_before->set_flat(true);
  1032. hbc->add_child(empty_before);
  1033. empty_after = memnew(Button);
  1034. empty_after->set_flat(true);
  1035. hbc->add_child(empty_after);
  1036. hbc->add_child(memnew(VSeparator));
  1037. move_up = memnew(Button);
  1038. move_up->set_flat(true);
  1039. hbc->add_child(move_up);
  1040. move_down = memnew(Button);
  1041. move_down->set_flat(true);
  1042. hbc->add_child(move_down);
  1043. delete_frame = memnew(Button);
  1044. delete_frame->set_flat(true);
  1045. hbc->add_child(delete_frame);
  1046. hbc->add_spacer();
  1047. zoom_out = memnew(Button);
  1048. zoom_out->connect("pressed", callable_mp(this, &SpriteFramesEditor::_zoom_out));
  1049. zoom_out->set_flat(true);
  1050. zoom_out->set_tooltip_text(TTR("Zoom Out"));
  1051. hbc->add_child(zoom_out);
  1052. zoom_reset = memnew(Button);
  1053. zoom_reset->connect("pressed", callable_mp(this, &SpriteFramesEditor::_zoom_reset));
  1054. zoom_reset->set_flat(true);
  1055. zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  1056. hbc->add_child(zoom_reset);
  1057. zoom_in = memnew(Button);
  1058. zoom_in->connect("pressed", callable_mp(this, &SpriteFramesEditor::_zoom_in));
  1059. zoom_in->set_flat(true);
  1060. zoom_in->set_tooltip_text(TTR("Zoom In"));
  1061. hbc->add_child(zoom_in);
  1062. file = memnew(EditorFileDialog);
  1063. add_child(file);
  1064. tree = memnew(ItemList);
  1065. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1066. tree->set_icon_mode(ItemList::ICON_MODE_TOP);
  1067. tree->set_max_columns(0);
  1068. tree->set_icon_mode(ItemList::ICON_MODE_TOP);
  1069. tree->set_max_text_lines(2);
  1070. tree->set_drag_forwarding(this);
  1071. tree->connect("gui_input", callable_mp(this, &SpriteFramesEditor::_tree_input));
  1072. sub_vb->add_child(tree);
  1073. dialog = memnew(AcceptDialog);
  1074. add_child(dialog);
  1075. load->connect("pressed", callable_mp(this, &SpriteFramesEditor::_load_pressed));
  1076. load_sheet->connect("pressed", callable_mp(this, &SpriteFramesEditor::_open_sprite_sheet));
  1077. delete_frame->connect("pressed", callable_mp(this, &SpriteFramesEditor::_delete_pressed));
  1078. copy->connect("pressed", callable_mp(this, &SpriteFramesEditor::_copy_pressed));
  1079. paste->connect("pressed", callable_mp(this, &SpriteFramesEditor::_paste_pressed));
  1080. empty_before->connect("pressed", callable_mp(this, &SpriteFramesEditor::_empty_pressed));
  1081. empty_after->connect("pressed", callable_mp(this, &SpriteFramesEditor::_empty2_pressed));
  1082. move_up->connect("pressed", callable_mp(this, &SpriteFramesEditor::_up_pressed));
  1083. move_down->connect("pressed", callable_mp(this, &SpriteFramesEditor::_down_pressed));
  1084. load->set_shortcut_context(tree);
  1085. load->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_file", TTR("Add frame from file"), KeyModifierMask::CMD_OR_CTRL | Key::O));
  1086. load_sheet->set_shortcut_context(tree);
  1087. 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));
  1088. delete_frame->set_shortcut_context(tree);
  1089. delete_frame->set_shortcut(ED_SHORTCUT("sprite_frames/delete", TTR("Delete Frame"), Key::KEY_DELETE));
  1090. copy->set_shortcut_context(tree);
  1091. copy->set_shortcut(ED_SHORTCUT("sprite_frames/copy", TTR("Copy Frame"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  1092. paste->set_shortcut_context(tree);
  1093. paste->set_shortcut(ED_SHORTCUT("sprite_frames/paste", TTR("Paste Frame"), KeyModifierMask::CMD_OR_CTRL | Key::V));
  1094. empty_before->set_shortcut_context(tree);
  1095. empty_before->set_shortcut(ED_SHORTCUT("sprite_frames/empty_before", TTR("Insert Empty (Before Selected)"), KeyModifierMask::ALT | Key::LEFT));
  1096. empty_after->set_shortcut_context(tree);
  1097. empty_after->set_shortcut(ED_SHORTCUT("sprite_frames/empty_after", TTR("Insert Empty (After Selected)"), KeyModifierMask::ALT | Key::RIGHT));
  1098. move_up->set_shortcut_context(tree);
  1099. move_up->set_shortcut(ED_SHORTCUT("sprite_frames/move_left", TTR("Move Frame Left"), KeyModifierMask::CMD_OR_CTRL | Key::LEFT));
  1100. move_down->set_shortcut_context(tree);
  1101. move_down->set_shortcut(ED_SHORTCUT("sprite_frames/move_right", TTR("Move Frame Right"), KeyModifierMask::CMD_OR_CTRL | Key::RIGHT));
  1102. zoom_out->set_shortcut_context(tree);
  1103. zoom_out->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_out", TTR("Zoom Out"),
  1104. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) }));
  1105. zoom_in->set_shortcut_context(tree);
  1106. zoom_in->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_in", TTR("Zoom In"),
  1107. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) }));
  1108. file->connect("files_selected", callable_mp(this, &SpriteFramesEditor::_file_load_request).bind(-1));
  1109. loading_scene = false;
  1110. sel = -1;
  1111. updating = false;
  1112. edited_anim = "default";
  1113. delete_dialog = memnew(ConfirmationDialog);
  1114. add_child(delete_dialog);
  1115. delete_dialog->connect("confirmed", callable_mp(this, &SpriteFramesEditor::_animation_remove_confirmed));
  1116. split_sheet_dialog = memnew(ConfirmationDialog);
  1117. add_child(split_sheet_dialog);
  1118. VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
  1119. split_sheet_dialog->add_child(split_sheet_vb);
  1120. split_sheet_dialog->set_title(TTR("Select Frames"));
  1121. split_sheet_dialog->connect("confirmed", callable_mp(this, &SpriteFramesEditor::_sheet_add_frames));
  1122. HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
  1123. split_sheet_hb->add_child(memnew(Label(TTR("Horizontal:"))));
  1124. split_sheet_h = memnew(SpinBox);
  1125. split_sheet_h->set_min(1);
  1126. split_sheet_h->set_max(128);
  1127. split_sheet_h->set_step(1);
  1128. split_sheet_hb->add_child(split_sheet_h);
  1129. split_sheet_h->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  1130. split_sheet_hb->add_child(memnew(Label(TTR("Vertical:"))));
  1131. split_sheet_v = memnew(SpinBox);
  1132. split_sheet_v->set_min(1);
  1133. split_sheet_v->set_max(128);
  1134. split_sheet_v->set_step(1);
  1135. split_sheet_hb->add_child(split_sheet_v);
  1136. split_sheet_v->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  1137. split_sheet_hb->add_child(memnew(VSeparator));
  1138. split_sheet_hb->add_child(memnew(Label(TTR("Size:"))));
  1139. split_sheet_size_x = memnew(SpinBox);
  1140. split_sheet_size_x->set_min(1);
  1141. split_sheet_size_x->set_step(1);
  1142. split_sheet_size_x->set_suffix("px");
  1143. split_sheet_size_x->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  1144. split_sheet_hb->add_child(split_sheet_size_x);
  1145. split_sheet_size_y = memnew(SpinBox);
  1146. split_sheet_size_y->set_min(1);
  1147. split_sheet_size_y->set_step(1);
  1148. split_sheet_size_y->set_suffix("px");
  1149. split_sheet_size_y->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  1150. split_sheet_hb->add_child(split_sheet_size_y);
  1151. split_sheet_hb->add_child(memnew(VSeparator));
  1152. split_sheet_hb->add_child(memnew(Label(TTR("Separation:"))));
  1153. split_sheet_sep_x = memnew(SpinBox);
  1154. split_sheet_sep_x->set_min(0);
  1155. split_sheet_sep_x->set_step(1);
  1156. split_sheet_sep_x->set_suffix("px");
  1157. split_sheet_sep_x->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1158. split_sheet_hb->add_child(split_sheet_sep_x);
  1159. split_sheet_sep_y = memnew(SpinBox);
  1160. split_sheet_sep_y->set_min(0);
  1161. split_sheet_sep_y->set_step(1);
  1162. split_sheet_sep_y->set_suffix("px");
  1163. split_sheet_sep_y->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1164. split_sheet_hb->add_child(split_sheet_sep_y);
  1165. split_sheet_hb->add_child(memnew(VSeparator));
  1166. split_sheet_hb->add_child(memnew(Label(TTR("Offset:"))));
  1167. split_sheet_offset_x = memnew(SpinBox);
  1168. split_sheet_offset_x->set_min(0);
  1169. split_sheet_offset_x->set_step(1);
  1170. split_sheet_offset_x->set_suffix("px");
  1171. split_sheet_offset_x->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1172. split_sheet_hb->add_child(split_sheet_offset_x);
  1173. split_sheet_offset_y = memnew(SpinBox);
  1174. split_sheet_offset_y->set_min(0);
  1175. split_sheet_offset_y->set_step(1);
  1176. split_sheet_offset_y->set_suffix("px");
  1177. split_sheet_offset_y->connect("value_changed", callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  1178. split_sheet_hb->add_child(split_sheet_offset_y);
  1179. split_sheet_hb->add_spacer();
  1180. Button *select_clear_all = memnew(Button);
  1181. select_clear_all->set_text(TTR("Select/Clear All Frames"));
  1182. select_clear_all->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_select_clear_all_frames));
  1183. split_sheet_hb->add_child(select_clear_all);
  1184. split_sheet_vb->add_child(split_sheet_hb);
  1185. PanelContainer *split_sheet_panel = memnew(PanelContainer);
  1186. split_sheet_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1187. split_sheet_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1188. split_sheet_vb->add_child(split_sheet_panel);
  1189. split_sheet_preview = memnew(TextureRect);
  1190. split_sheet_preview->set_ignore_texture_size(true);
  1191. split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
  1192. split_sheet_preview->connect("draw", callable_mp(this, &SpriteFramesEditor::_sheet_preview_draw));
  1193. split_sheet_preview->connect("gui_input", callable_mp(this, &SpriteFramesEditor::_sheet_preview_input));
  1194. split_sheet_scroll = memnew(ScrollContainer);
  1195. split_sheet_scroll->connect("gui_input", callable_mp(this, &SpriteFramesEditor::_sheet_scroll_input));
  1196. split_sheet_panel->add_child(split_sheet_scroll);
  1197. CenterContainer *cc = memnew(CenterContainer);
  1198. cc->add_child(split_sheet_preview);
  1199. cc->set_h_size_flags(SIZE_EXPAND_FILL);
  1200. cc->set_v_size_flags(SIZE_EXPAND_FILL);
  1201. split_sheet_scroll->add_child(cc);
  1202. MarginContainer *split_sheet_zoom_margin = memnew(MarginContainer);
  1203. split_sheet_panel->add_child(split_sheet_zoom_margin);
  1204. split_sheet_zoom_margin->set_h_size_flags(0);
  1205. split_sheet_zoom_margin->set_v_size_flags(0);
  1206. split_sheet_zoom_margin->add_theme_constant_override("margin_top", 5);
  1207. split_sheet_zoom_margin->add_theme_constant_override("margin_left", 5);
  1208. HBoxContainer *split_sheet_zoom_hb = memnew(HBoxContainer);
  1209. split_sheet_zoom_margin->add_child(split_sheet_zoom_hb);
  1210. split_sheet_zoom_out = memnew(Button);
  1211. split_sheet_zoom_out->set_flat(true);
  1212. split_sheet_zoom_out->set_focus_mode(FOCUS_NONE);
  1213. split_sheet_zoom_out->set_tooltip_text(TTR("Zoom Out"));
  1214. split_sheet_zoom_out->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_zoom_out));
  1215. split_sheet_zoom_hb->add_child(split_sheet_zoom_out);
  1216. split_sheet_zoom_reset = memnew(Button);
  1217. split_sheet_zoom_reset->set_flat(true);
  1218. split_sheet_zoom_reset->set_focus_mode(FOCUS_NONE);
  1219. split_sheet_zoom_reset->set_tooltip_text(TTR("Zoom Reset"));
  1220. split_sheet_zoom_reset->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_zoom_reset));
  1221. split_sheet_zoom_hb->add_child(split_sheet_zoom_reset);
  1222. split_sheet_zoom_in = memnew(Button);
  1223. split_sheet_zoom_in->set_flat(true);
  1224. split_sheet_zoom_in->set_focus_mode(FOCUS_NONE);
  1225. split_sheet_zoom_in->set_tooltip_text(TTR("Zoom In"));
  1226. split_sheet_zoom_in->connect("pressed", callable_mp(this, &SpriteFramesEditor::_sheet_zoom_in));
  1227. split_sheet_zoom_hb->add_child(split_sheet_zoom_in);
  1228. file_split_sheet = memnew(EditorFileDialog);
  1229. file_split_sheet->set_title(TTR("Create Frames from Sprite Sheet"));
  1230. file_split_sheet->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1231. add_child(file_split_sheet);
  1232. file_split_sheet->connect("file_selected", callable_mp(this, &SpriteFramesEditor::_prepare_sprite_sheet));
  1233. // Config scale.
  1234. scale_ratio = 1.2f;
  1235. thumbnail_default_size = 96 * MAX(1, EDSCALE);
  1236. thumbnail_zoom = MAX(1.0f, EDSCALE);
  1237. max_thumbnail_zoom = 8.0f * MAX(1.0f, EDSCALE);
  1238. min_thumbnail_zoom = 0.1f * MAX(1.0f, EDSCALE);
  1239. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  1240. sheet_zoom = MAX(1.0f, EDSCALE);
  1241. max_sheet_zoom = 16.0f * MAX(1.0f, EDSCALE);
  1242. min_sheet_zoom = 0.01f * MAX(1.0f, EDSCALE);
  1243. _zoom_reset();
  1244. // Ensure the anim search box is wide enough by default.
  1245. // Not by setting its minimum size so it can still be shrinked if desired.
  1246. set_split_offset(56 * EDSCALE);
  1247. }
  1248. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  1249. frames_editor->set_undo_redo(get_undo_redo());
  1250. SpriteFrames *s;
  1251. AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object);
  1252. if (animated_sprite) {
  1253. s = *animated_sprite->get_sprite_frames();
  1254. } else {
  1255. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  1256. if (animated_sprite_3d) {
  1257. s = *animated_sprite_3d->get_sprite_frames();
  1258. } else {
  1259. s = Object::cast_to<SpriteFrames>(p_object);
  1260. }
  1261. }
  1262. frames_editor->edit(s);
  1263. }
  1264. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  1265. AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object);
  1266. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  1267. if (animated_sprite && *animated_sprite->get_sprite_frames()) {
  1268. return true;
  1269. } else if (animated_sprite_3d && *animated_sprite_3d->get_sprite_frames()) {
  1270. return true;
  1271. } else {
  1272. return p_object->is_class("SpriteFrames");
  1273. }
  1274. }
  1275. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  1276. if (p_visible) {
  1277. button->show();
  1278. EditorNode::get_singleton()->make_bottom_panel_item_visible(frames_editor);
  1279. } else {
  1280. button->hide();
  1281. if (frames_editor->is_visible_in_tree()) {
  1282. EditorNode::get_singleton()->hide_bottom_panel();
  1283. }
  1284. }
  1285. }
  1286. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin() {
  1287. frames_editor = memnew(SpriteFramesEditor);
  1288. frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  1289. button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("SpriteFrames"), frames_editor);
  1290. button->hide();
  1291. }
  1292. SpriteFramesEditorPlugin::~SpriteFramesEditorPlugin() {
  1293. }