sprite_frames_editor_plugin.cpp 82 KB

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