sprite_frames_editor_plugin.cpp 52 KB

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