sprite_frames_editor_plugin.cpp 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813
  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/io/resource_loader.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/translation_server.h"
  34. #include "editor/docks/editor_dock_manager.h"
  35. #include "editor/docks/filesystem_dock.h"
  36. #include "editor/docks/scene_tree_dock.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/file_system/editor_file_system.h"
  41. #include "editor/gui/editor_file_dialog.h"
  42. #include "editor/settings/editor_command_palette.h"
  43. #include "editor/settings/editor_settings.h"
  44. #include "editor/themes/editor_scale.h"
  45. #include "scene/2d/animated_sprite_2d.h"
  46. #include "scene/3d/sprite_3d.h"
  47. #include "scene/gui/center_container.h"
  48. #include "scene/gui/flow_container.h"
  49. #include "scene/gui/margin_container.h"
  50. #include "scene/gui/option_button.h"
  51. #include "scene/gui/panel_container.h"
  52. #include "scene/gui/separator.h"
  53. #include "scene/gui/split_container.h"
  54. #include "scene/resources/atlas_texture.h"
  55. 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) {
  56. p_control->draw_line(p_from, p_from + p_size, p_color);
  57. p_control->draw_line(p_from + p_shadow_offset, p_from + p_size + p_shadow_offset, p_shadow_color);
  58. }
  59. void SpriteFramesEditor::_open_sprite_sheet() {
  60. file_split_sheet->clear_filters();
  61. List<String> extensions;
  62. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  63. for (const String &extension : extensions) {
  64. file_split_sheet->add_filter("*." + extension);
  65. }
  66. file_split_sheet->popup_file_dialog();
  67. }
  68. int SpriteFramesEditor::_sheet_preview_position_to_frame_index(const Point2 &p_position) {
  69. const Size2i offset = _get_offset();
  70. const Size2i frame_size = _get_frame_size();
  71. const Size2i separation = _get_separation();
  72. const Size2i block_size = frame_size + separation;
  73. const Point2i position = p_position / sheet_zoom - offset;
  74. if (position.x < 0 || position.y < 0) {
  75. return -1; // Out of bounds.
  76. }
  77. if (position.x % block_size.x >= frame_size.x || position.y % block_size.y >= frame_size.y) {
  78. return -1; // Gap between frames.
  79. }
  80. const Point2i frame = position / block_size;
  81. const Size2i frame_count = _get_frame_count();
  82. if (frame.x >= frame_count.x || frame.y >= frame_count.y) {
  83. return -1; // Out of bounds.
  84. }
  85. return frame_count.x * frame.y + frame.x;
  86. }
  87. void SpriteFramesEditor::_sheet_preview_draw() {
  88. const Size2i frame_count = _get_frame_count();
  89. const Size2i separation = _get_separation();
  90. const Size2 draw_offset = Size2(_get_offset()) * sheet_zoom;
  91. const Size2 draw_sep = Size2(separation) * sheet_zoom;
  92. const Size2 draw_frame_size = Size2(_get_frame_size()) * sheet_zoom;
  93. const Size2 draw_size = draw_frame_size * frame_count + draw_sep * (frame_count - Size2i(1, 1));
  94. const Color line_color = Color(1, 1, 1, 0.3);
  95. const Color shadow_color = Color(0, 0, 0, 0.3);
  96. // Vertical lines.
  97. _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  98. for (int i = 0; i < frame_count.x - 1; i++) {
  99. const Point2 start = draw_offset + Vector2(i * draw_sep.x + (i + 1) * draw_frame_size.x, 0);
  100. if (separation.x == 0) {
  101. _draw_shadowed_line(split_sheet_preview, start, Vector2(0, draw_size.y), Vector2(1, 0), line_color, shadow_color);
  102. } else {
  103. const Size2 size = Size2(draw_sep.x, draw_size.y);
  104. split_sheet_preview->draw_rect(Rect2(start, size), line_color);
  105. }
  106. }
  107. _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);
  108. // Horizontal lines.
  109. _draw_shadowed_line(split_sheet_preview, draw_offset, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  110. for (int i = 0; i < frame_count.y - 1; i++) {
  111. const Point2 start = draw_offset + Vector2(0, i * draw_sep.y + (i + 1) * draw_frame_size.y);
  112. if (separation.y == 0) {
  113. _draw_shadowed_line(split_sheet_preview, start, Vector2(draw_size.x, 0), Vector2(0, 1), line_color, shadow_color);
  114. } else {
  115. const Size2 size = Size2(draw_size.x, draw_sep.y);
  116. split_sheet_preview->draw_rect(Rect2(start, size), line_color);
  117. }
  118. }
  119. _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);
  120. if (frames_selected.is_empty()) {
  121. split_sheet_dialog->get_ok_button()->set_disabled(true);
  122. split_sheet_dialog->set_ok_button_text(TTRC("No Frames Selected"));
  123. return;
  124. }
  125. Color accent = get_theme_color("accent_color", EditorStringName(Editor));
  126. _sheet_sort_frames();
  127. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  128. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  129. for (int i = 0; i < frames_ordered.size(); ++i) {
  130. const int idx = frames_ordered[i].second;
  131. const int x = idx % frame_count.x;
  132. const int y = idx / frame_count.x;
  133. const Point2 pos = draw_offset + Point2(x, y) * (draw_frame_size + draw_sep);
  134. split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 0.35), true);
  135. split_sheet_preview->draw_rect(Rect2(pos, draw_frame_size), Color(0, 0, 0, 1), false);
  136. split_sheet_preview->draw_rect(Rect2(pos + Size2(1, 1), draw_frame_size - Size2(2, 2)), Color(0, 0, 0, 1), false);
  137. split_sheet_preview->draw_rect(Rect2(pos + Size2(2, 2), draw_frame_size - Size2(4, 4)), accent, false);
  138. split_sheet_preview->draw_rect(Rect2(pos + Size2(3, 3), draw_frame_size - Size2(6, 6)), accent, false);
  139. split_sheet_preview->draw_rect(Rect2(pos + Size2(4, 4), draw_frame_size - Size2(8, 8)), Color(0, 0, 0, 1), false);
  140. split_sheet_preview->draw_rect(Rect2(pos + Size2(5, 5), draw_frame_size - Size2(10, 10)), Color(0, 0, 0, 1), false);
  141. const String text = itos(i);
  142. const Vector2 string_size = font->get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  143. // Stop rendering text if too large.
  144. if (string_size.x + 6 < draw_frame_size.x && string_size.y / 2 + 10 < draw_frame_size.y) {
  145. 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));
  146. 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));
  147. }
  148. }
  149. split_sheet_dialog->get_ok_button()->set_disabled(false);
  150. split_sheet_dialog->set_ok_button_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size()));
  151. }
  152. void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
  153. const Ref<InputEventMouseButton> mb = p_event;
  154. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  155. const int idx = _sheet_preview_position_to_frame_index(mb->get_position());
  156. if (idx != -1) {
  157. if (mb->is_shift_pressed() && last_frame_selected >= 0) {
  158. // Select multiple frames.
  159. const int from = last_frame_selected;
  160. const int to = idx;
  161. const int diff = Math::abs(to - from);
  162. const int dir = SIGN(to - from);
  163. for (int i = 0; i <= diff; i++) {
  164. const int this_idx = from + i * dir;
  165. // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
  166. frames_toggled_by_mouse_hover.insert(this_idx);
  167. if (mb->is_command_or_control_pressed()) {
  168. frames_selected.erase(this_idx);
  169. } else if (!frames_selected.has(this_idx)) {
  170. frames_selected.insert(this_idx, selected_count);
  171. selected_count++;
  172. }
  173. }
  174. } else {
  175. // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
  176. frames_toggled_by_mouse_hover.insert(idx);
  177. if (frames_selected.has(idx)) {
  178. frames_selected.erase(idx);
  179. } else {
  180. frames_selected.insert(idx, selected_count);
  181. selected_count++;
  182. }
  183. }
  184. }
  185. if (last_frame_selected != idx || idx != -1) {
  186. last_frame_selected = idx;
  187. frames_need_sort = true;
  188. split_sheet_preview->queue_redraw();
  189. }
  190. }
  191. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  192. frames_toggled_by_mouse_hover.clear();
  193. }
  194. const Ref<InputEventMouseMotion> mm = p_event;
  195. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  196. // Select by holding down the mouse button on frames.
  197. const int idx = _sheet_preview_position_to_frame_index(mm->get_position());
  198. if (idx != -1 && !frames_toggled_by_mouse_hover.has(idx)) {
  199. // Only allow toggling each tile once per mouse hold.
  200. // Otherwise, the selection would constantly "flicker" in and out when moving the mouse cursor.
  201. // The mouse button must be released before it can be toggled again.
  202. frames_toggled_by_mouse_hover.insert(idx);
  203. if (frames_selected.has(idx)) {
  204. frames_selected.erase(idx);
  205. } else {
  206. frames_selected.insert(idx, selected_count);
  207. selected_count++;
  208. }
  209. last_frame_selected = idx;
  210. frames_need_sort = true;
  211. split_sheet_preview->queue_redraw();
  212. }
  213. }
  214. if (frames_selected.is_empty()) {
  215. selected_count = 0;
  216. }
  217. }
  218. void SpriteFramesEditor::_sheet_scroll_input(const Ref<InputEvent> &p_event) {
  219. const Ref<InputEventMouseButton> mb = p_event;
  220. if (mb.is_valid()) {
  221. // Zoom in/out using Ctrl + mouse wheel. This is done on the ScrollContainer
  222. // to allow performing this action anywhere, even if the cursor isn't
  223. // hovering the texture in the workspace.
  224. // keep CTRL and not CMD_OR_CTRL as CTRL is expected even on MacOS.
  225. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  226. _sheet_zoom_on_position(scale_ratio, mb->get_position());
  227. // Don't scroll up after zooming in.
  228. split_sheet_scroll->accept_event();
  229. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  230. _sheet_zoom_on_position(1 / scale_ratio, mb->get_position());
  231. // Don't scroll down after zooming out.
  232. split_sheet_scroll->accept_event();
  233. }
  234. }
  235. const Ref<InputEventMouseMotion> mm = p_event;
  236. if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
  237. const Vector2 dragged = Input::get_singleton()->warp_mouse_motion(mm, split_sheet_scroll->get_global_rect());
  238. split_sheet_scroll->set_h_scroll(split_sheet_scroll->get_h_scroll() - dragged.x);
  239. split_sheet_scroll->set_v_scroll(split_sheet_scroll->get_v_scroll() - dragged.y);
  240. }
  241. }
  242. void SpriteFramesEditor::_sheet_add_frames() {
  243. const Size2i frame_count = _get_frame_count();
  244. const Size2i frame_size = _get_frame_size();
  245. const Size2i offset = _get_offset();
  246. const Size2i separation = _get_separation();
  247. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  248. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  249. int fc = frames->get_frame_count(edited_anim);
  250. _sheet_sort_frames();
  251. for (const Pair<int, int> &pair : frames_ordered) {
  252. const int idx = pair.second;
  253. const Point2 frame_coords(idx % frame_count.x, idx / frame_count.x);
  254. Ref<AtlasTexture> at;
  255. at.instantiate();
  256. at->set_atlas(split_sheet_preview->get_texture());
  257. at->set_region(Rect2(offset + frame_coords * (frame_size + separation), frame_size));
  258. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, at, 1.0, -1);
  259. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, fc);
  260. }
  261. undo_redo->add_do_method(this, "_update_library");
  262. undo_redo->add_undo_method(this, "_update_library");
  263. undo_redo->commit_action();
  264. }
  265. void SpriteFramesEditor::_sheet_update_zoom_label() {
  266. String zoom_text;
  267. // The zoom level displayed is relative to the editor scale
  268. // (like in most image editors). Its lower bound is clamped to 1 as some people
  269. // lower the editor scale to increase the available real estate,
  270. // even if their display doesn't have a particularly low DPI.
  271. TranslationServer *translation_server = TranslationServer::get_singleton();
  272. String locale = translation_server->get_tool_locale();
  273. if (sheet_zoom >= 10) {
  274. zoom_text = translation_server->format_number(rtos(Math::round((sheet_zoom / MAX(1, EDSCALE)) * 100)), locale);
  275. } else {
  276. // 2 decimal places if the zoom is below 10%, 1 decimal place if it's below 1000%.
  277. zoom_text = translation_server->format_number(rtos(Math::snapped((sheet_zoom / MAX(1, EDSCALE)) * 100, (sheet_zoom >= 0.1) ? 0.1 : 0.01)), locale);
  278. }
  279. zoom_text += " " + translation_server->get_percent_sign(locale);
  280. split_sheet_zoom_reset->set_text(zoom_text);
  281. }
  282. void SpriteFramesEditor::_sheet_zoom_on_position(float p_zoom, const Vector2 &p_position) {
  283. const float old_zoom = sheet_zoom;
  284. sheet_zoom = CLAMP(sheet_zoom * p_zoom, min_sheet_zoom, max_sheet_zoom);
  285. const Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  286. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  287. Vector2 offset = Vector2(split_sheet_scroll->get_h_scroll(), split_sheet_scroll->get_v_scroll());
  288. offset = (offset + p_position) / old_zoom * sheet_zoom - p_position;
  289. split_sheet_scroll->set_h_scroll(offset.x);
  290. split_sheet_scroll->set_v_scroll(offset.y);
  291. _sheet_update_zoom_label();
  292. }
  293. void SpriteFramesEditor::_sheet_zoom_in() {
  294. _sheet_zoom_on_position(scale_ratio, Vector2());
  295. }
  296. void SpriteFramesEditor::_sheet_zoom_out() {
  297. _sheet_zoom_on_position(1 / scale_ratio, Vector2());
  298. }
  299. void SpriteFramesEditor::_sheet_zoom_reset() {
  300. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  301. sheet_zoom = MAX(1.0f, EDSCALE);
  302. Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  303. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  304. _sheet_update_zoom_label();
  305. }
  306. void SpriteFramesEditor::_sheet_zoom_fit() {
  307. const float margin_percentage = 0.1f;
  308. const float max_margin = 64.0f;
  309. const Size2 margin = (margin_percentage * split_sheet_scroll->get_size()).minf(max_margin);
  310. const Size2 display_area_size = split_sheet_scroll->get_size() - margin;
  311. const Size2 texture_size = split_sheet_preview->get_texture()->get_size();
  312. const Vector2 texture_ratio = display_area_size / texture_size;
  313. float texture_fit_zoom = MIN(texture_ratio.x, texture_ratio.y);
  314. // Quantize the zoom level to avoid subpixel rendering
  315. if (texture_fit_zoom > 1.0) {
  316. texture_fit_zoom = Math::floor(texture_fit_zoom);
  317. } else if (!Math::is_zero_approx(texture_fit_zoom)) {
  318. texture_fit_zoom = 1.0f / Math::ceil(1.0f / texture_fit_zoom);
  319. }
  320. sheet_zoom = CLAMP(texture_fit_zoom, min_sheet_zoom, max_sheet_zoom);
  321. split_sheet_preview->set_custom_minimum_size(texture_size * sheet_zoom);
  322. _sheet_update_zoom_label();
  323. }
  324. void SpriteFramesEditor::_sheet_order_selected(int p_option) {
  325. frames_need_sort = true;
  326. split_sheet_preview->queue_redraw();
  327. }
  328. void SpriteFramesEditor::_sheet_select_all_frames() {
  329. for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) {
  330. if (!frames_selected.has(i)) {
  331. frames_selected.insert(i, selected_count);
  332. selected_count++;
  333. frames_need_sort = true;
  334. }
  335. }
  336. split_sheet_preview->queue_redraw();
  337. }
  338. void SpriteFramesEditor::_sheet_clear_all_frames() {
  339. frames_selected.clear();
  340. selected_count = 0;
  341. split_sheet_preview->queue_redraw();
  342. }
  343. void SpriteFramesEditor::_sheet_sort_frames() {
  344. if (!frames_need_sort) {
  345. return;
  346. }
  347. frames_need_sort = false;
  348. frames_ordered.resize(frames_selected.size());
  349. if (frames_selected.is_empty()) {
  350. return;
  351. }
  352. const Size2i frame_count = _get_frame_count();
  353. const int frame_order = split_sheet_order->get_selected_id();
  354. int index = 0;
  355. // Fill based on order.
  356. for (const KeyValue<int, int> &from_pair : frames_selected) {
  357. const int idx = from_pair.key;
  358. const int selection_order = from_pair.value;
  359. // Default to using selection order.
  360. int order_by = selection_order;
  361. // Extract coordinates for sorting.
  362. const int pos_frame_x = idx % frame_count.x;
  363. const int pos_frame_y = idx / frame_count.x;
  364. const int neg_frame_x = frame_count.x - (pos_frame_x + 1);
  365. const int neg_frame_y = frame_count.y - (pos_frame_y + 1);
  366. switch (frame_order) {
  367. case FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM: {
  368. order_by = frame_count.x * pos_frame_y + pos_frame_x;
  369. } break;
  370. case FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP: {
  371. order_by = frame_count.x * neg_frame_y + pos_frame_x;
  372. } break;
  373. case FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM: {
  374. order_by = frame_count.x * pos_frame_y + neg_frame_x;
  375. } break;
  376. case FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP: {
  377. order_by = frame_count.x * neg_frame_y + neg_frame_x;
  378. } break;
  379. case FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT: {
  380. order_by = pos_frame_y + frame_count.y * pos_frame_x;
  381. } break;
  382. case FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT: {
  383. order_by = pos_frame_y + frame_count.y * neg_frame_x;
  384. } break;
  385. case FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT: {
  386. order_by = neg_frame_y + frame_count.y * pos_frame_x;
  387. } break;
  388. case FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT: {
  389. order_by = neg_frame_y + frame_count.y * neg_frame_x;
  390. } break;
  391. }
  392. // Assign in vector.
  393. frames_ordered.set(index, Pair<int, int>(order_by, idx));
  394. index++;
  395. }
  396. // Sort frames.
  397. frames_ordered.sort_custom<PairSort<int, int>>();
  398. }
  399. void SpriteFramesEditor::_sheet_spin_changed(double p_value, int p_dominant_param) {
  400. if (updating_split_settings) {
  401. return;
  402. }
  403. updating_split_settings = true;
  404. if (p_dominant_param != PARAM_USE_CURRENT) {
  405. dominant_param = p_dominant_param;
  406. }
  407. const Size2i texture_size = split_sheet_preview->get_texture()->get_size();
  408. const Size2i size = texture_size - _get_offset();
  409. switch (dominant_param) {
  410. case PARAM_SIZE: {
  411. const Size2i frame_size = _get_frame_size();
  412. const Size2i offset_max = texture_size - frame_size;
  413. split_sheet_offset_x->set_max(offset_max.x);
  414. split_sheet_offset_y->set_max(offset_max.y);
  415. const Size2i sep_max = size - frame_size * 2;
  416. split_sheet_sep_x->set_max(sep_max.x);
  417. split_sheet_sep_y->set_max(sep_max.y);
  418. const Size2i separation = _get_separation();
  419. const Size2i count = (size + separation) / (frame_size + separation);
  420. split_sheet_h->set_value(count.x);
  421. split_sheet_v->set_value(count.y);
  422. } break;
  423. case PARAM_FRAME_COUNT: {
  424. const Size2i count = _get_frame_count();
  425. const Size2i offset_max = texture_size - count;
  426. split_sheet_offset_x->set_max(offset_max.x);
  427. split_sheet_offset_y->set_max(offset_max.y);
  428. const Size2i gap_count = count - Size2i(1, 1);
  429. split_sheet_sep_x->set_max(gap_count.x == 0 ? size.x : (size.x - count.x) / gap_count.x);
  430. split_sheet_sep_y->set_max(gap_count.y == 0 ? size.y : (size.y - count.y) / gap_count.y);
  431. const Size2i separation = _get_separation();
  432. const Size2i frame_size = (size - separation * gap_count) / count;
  433. split_sheet_size_x->set_value(frame_size.x);
  434. split_sheet_size_y->set_value(frame_size.y);
  435. } break;
  436. }
  437. updating_split_settings = false;
  438. frames_selected.clear();
  439. selected_count = 0;
  440. last_frame_selected = -1;
  441. split_sheet_preview->queue_redraw();
  442. }
  443. void SpriteFramesEditor::_toggle_show_settings() {
  444. split_sheet_settings_vb->set_visible(!split_sheet_settings_vb->is_visible());
  445. _update_show_settings();
  446. }
  447. void SpriteFramesEditor::_update_show_settings() {
  448. if (is_layout_rtl()) {
  449. toggle_settings_button->set_button_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Back") : SNAME("Forward")));
  450. } else {
  451. toggle_settings_button->set_button_icon(get_editor_theme_icon(split_sheet_settings_vb->is_visible() ? SNAME("Forward") : SNAME("Back")));
  452. }
  453. }
  454. void SpriteFramesEditor::_auto_slice_sprite_sheet() {
  455. if (updating_split_settings) {
  456. return;
  457. }
  458. updating_split_settings = true;
  459. const Size2i size = split_sheet_preview->get_texture()->get_size();
  460. const Size2i split_sheet = _estimate_sprite_sheet_size(split_sheet_preview->get_texture());
  461. split_sheet_h->set_value(split_sheet.x);
  462. split_sheet_v->set_value(split_sheet.y);
  463. split_sheet_size_x->set_value(size.x / split_sheet.x);
  464. split_sheet_size_y->set_value(size.y / split_sheet.y);
  465. split_sheet_sep_x->set_value(0);
  466. split_sheet_sep_y->set_value(0);
  467. split_sheet_offset_x->set_value(0);
  468. split_sheet_offset_y->set_value(0);
  469. updating_split_settings = false;
  470. frames_selected.clear();
  471. selected_count = 0;
  472. last_frame_selected = -1;
  473. split_sheet_preview->queue_redraw();
  474. }
  475. bool SpriteFramesEditor::_matches_background_color(const Color &p_background_color, const Color &p_pixel_color) {
  476. if ((p_background_color.a == 0 && p_pixel_color.a == 0) || p_background_color.is_equal_approx(p_pixel_color)) {
  477. return true;
  478. }
  479. Color d = p_background_color - p_pixel_color;
  480. // 0.04f is the threshold for how much a colour can deviate from background colour and still be considered a match. Arrived at through experimentation, can be tweaked.
  481. return (d.r * d.r) + (d.g * d.g) + (d.b * d.b) + (d.a * d.a) < 0.04f;
  482. }
  483. Size2i SpriteFramesEditor::_estimate_sprite_sheet_size(const Ref<Texture2D> p_texture) {
  484. Ref<Image> image = p_texture->get_image();
  485. if (image->is_compressed()) {
  486. image = image->duplicate();
  487. ERR_FAIL_COND_V(image->decompress() != OK, p_texture->get_size());
  488. }
  489. Size2i size = image->get_size();
  490. Color assumed_background_color = image->get_pixel(0, 0);
  491. Size2i sheet_size;
  492. bool previous_line_background = true;
  493. for (int x = 0; x < size.x; x++) {
  494. int y = 0;
  495. while (y < size.y && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) {
  496. y++;
  497. }
  498. bool current_line_background = (y == size.y);
  499. if (previous_line_background && !current_line_background) {
  500. sheet_size.x++;
  501. }
  502. previous_line_background = current_line_background;
  503. }
  504. previous_line_background = true;
  505. for (int y = 0; y < size.y; y++) {
  506. int x = 0;
  507. while (x < size.x && _matches_background_color(assumed_background_color, image->get_pixel(x, y))) {
  508. x++;
  509. }
  510. bool current_line_background = (x == size.x);
  511. if (previous_line_background && !current_line_background) {
  512. sheet_size.y++;
  513. }
  514. previous_line_background = current_line_background;
  515. }
  516. if (sheet_size == Size2i(0, 0) || sheet_size == Size2i(1, 1)) {
  517. sheet_size = Size2i(4, 4);
  518. }
  519. return sheet_size;
  520. }
  521. void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) {
  522. Ref<Texture2D> texture = ResourceLoader::load(p_file);
  523. if (texture.is_null()) {
  524. EditorNode::get_singleton()->show_warning(TTR("Unable to load images"));
  525. ERR_FAIL_COND(texture.is_null());
  526. }
  527. frames_selected.clear();
  528. selected_count = 0;
  529. last_frame_selected = -1;
  530. bool first_open = split_sheet_preview->get_texture().is_null();
  531. bool new_texture = texture != split_sheet_preview->get_texture();
  532. split_sheet_preview->set_texture(texture);
  533. if (new_texture) {
  534. // Reset spin max.
  535. const Size2i size = texture->get_size();
  536. split_sheet_size_x->set_max(size.x);
  537. split_sheet_size_y->set_max(size.y);
  538. split_sheet_sep_x->set_max(size.x);
  539. split_sheet_sep_y->set_max(size.y);
  540. split_sheet_offset_x->set_max(size.x);
  541. split_sheet_offset_y->set_max(size.y);
  542. if (size != previous_texture_size) {
  543. // Different texture, reset to 4x4.
  544. dominant_param = PARAM_FRAME_COUNT;
  545. updating_split_settings = true;
  546. const Size2i split_sheet = Size2i(4, 4);
  547. split_sheet_h->set_value(split_sheet.x);
  548. split_sheet_v->set_value(split_sheet.y);
  549. split_sheet_size_x->set_value(size.x / split_sheet.x);
  550. split_sheet_size_y->set_value(size.y / split_sheet.y);
  551. split_sheet_sep_x->set_value(0);
  552. split_sheet_sep_y->set_value(0);
  553. split_sheet_offset_x->set_value(0);
  554. split_sheet_offset_y->set_value(0);
  555. updating_split_settings = false;
  556. }
  557. previous_texture_size = size;
  558. // Reset zoom.
  559. if (first_open) {
  560. _sheet_zoom_reset();
  561. split_sheet_dialog->connect(SceneStringName(focus_entered), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_fit), CONNECT_ONE_SHOT);
  562. } else {
  563. _sheet_zoom_fit();
  564. }
  565. }
  566. split_sheet_dialog->popup_centered_ratio(0.65);
  567. }
  568. void SpriteFramesEditor::_notification(int p_what) {
  569. switch (p_what) {
  570. case NOTIFICATION_ENTER_TREE: {
  571. get_tree()->connect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
  572. [[fallthrough]];
  573. }
  574. case NOTIFICATION_THEME_CHANGED: {
  575. autoplay_icon = get_editor_theme_icon(SNAME("AutoPlay"));
  576. stop_icon = get_editor_theme_icon(SNAME("Stop"));
  577. pause_icon = get_editor_theme_icon(SNAME("Pause"));
  578. _update_stop_icon();
  579. autoplay->set_button_icon(get_editor_theme_icon(SNAME("AutoPlay")));
  580. anim_loop->set_button_icon(get_editor_theme_icon(SNAME("Loop")));
  581. play->set_button_icon(get_editor_theme_icon(SNAME("PlayStart")));
  582. play_from->set_button_icon(get_editor_theme_icon(SNAME("Play")));
  583. play_bw->set_button_icon(get_editor_theme_icon(SNAME("PlayStartBackwards")));
  584. play_bw_from->set_button_icon(get_editor_theme_icon(SNAME("PlayBackwards")));
  585. load->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  586. load_sheet->set_button_icon(get_editor_theme_icon(SNAME("SpriteSheet")));
  587. copy->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  588. paste->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
  589. empty_before->set_button_icon(get_editor_theme_icon(SNAME("InsertBefore")));
  590. empty_after->set_button_icon(get_editor_theme_icon(SNAME("InsertAfter")));
  591. move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveLeft")));
  592. move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveRight")));
  593. delete_frame->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  594. zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  595. zoom_reset->set_button_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  596. zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  597. add_anim->set_button_icon(get_editor_theme_icon(SNAME("New")));
  598. duplicate_anim->set_button_icon(get_editor_theme_icon(SNAME("Duplicate")));
  599. cut_anim->set_button_icon(get_editor_theme_icon(SNAME("ActionCut")));
  600. copy_anim->set_button_icon(get_editor_theme_icon(SNAME("ActionCopy")));
  601. paste_anim->set_button_icon(get_editor_theme_icon(SNAME("ActionPaste")));
  602. delete_anim->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  603. anim_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  604. split_sheet_zoom_out->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  605. split_sheet_zoom_in->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  606. split_sheet_zoom_fit->set_button_icon(get_editor_theme_icon(SNAME("DistractionFree")));
  607. split_sheet_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  608. _update_show_settings();
  609. } break;
  610. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  611. _update_show_settings();
  612. } break;
  613. case NOTIFICATION_TRANSLATION_CHANGED: {
  614. _update_show_settings();
  615. anim_speed->set_suffix(TTR("FPS"));
  616. // Similar to `_update_library_impl()`, but only updates text for "empty" items.
  617. if (frames.is_valid()) {
  618. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  619. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
  620. if (texture.is_null()) {
  621. String name = itos(i);
  622. float duration = frames->get_frame_duration(edited_anim, i);
  623. texture = empty_icon;
  624. name += ": " + TTR("(empty)");
  625. if (duration != 1.0f) {
  626. name += String::utf8(" [× ") + String::num(duration, 2) + "]";
  627. }
  628. frame_list->set_item_text(i, name);
  629. }
  630. }
  631. }
  632. } break;
  633. case NOTIFICATION_READY: {
  634. add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up.
  635. } break;
  636. case NOTIFICATION_EXIT_TREE: {
  637. get_tree()->disconnect("node_removed", callable_mp(this, &SpriteFramesEditor::_node_removed));
  638. } break;
  639. }
  640. }
  641. void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_at_pos) {
  642. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  643. List<Ref<Texture2D>> resources;
  644. for (int i = 0; i < p_path.size(); i++) {
  645. Ref<Texture2D> resource;
  646. resource = ResourceLoader::load(p_path[i]);
  647. if (resource.is_null()) {
  648. dialog->set_text(TTRC("ERROR: Couldn't load frame resource!"));
  649. dialog->set_title(TTRC("Error!"));
  650. //dialog->get_cancel()->set_text("Close");
  651. dialog->set_ok_button_text(TTRC("Close"));
  652. dialog->popup_centered();
  653. return; ///beh should show an error i guess
  654. }
  655. resources.push_back(resource);
  656. }
  657. if (resources.is_empty()) {
  658. return;
  659. }
  660. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  661. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  662. int fc = frames->get_frame_count(edited_anim);
  663. int count = 0;
  664. for (const Ref<Texture2D> &E : resources) {
  665. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, E, 1.0, p_at_pos == -1 ? -1 : p_at_pos + count);
  666. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
  667. count++;
  668. }
  669. undo_redo->add_do_method(this, "_update_library");
  670. undo_redo->add_undo_method(this, "_update_library");
  671. undo_redo->commit_action();
  672. }
  673. Size2i SpriteFramesEditor::_get_frame_count() const {
  674. return Size2i(split_sheet_h->get_value(), split_sheet_v->get_value());
  675. }
  676. Size2i SpriteFramesEditor::_get_frame_size() const {
  677. return Size2i(split_sheet_size_x->get_value(), split_sheet_size_y->get_value());
  678. }
  679. Size2i SpriteFramesEditor::_get_offset() const {
  680. return Size2i(split_sheet_offset_x->get_value(), split_sheet_offset_y->get_value());
  681. }
  682. Size2i SpriteFramesEditor::_get_separation() const {
  683. return Size2i(split_sheet_sep_x->get_value(), split_sheet_sep_y->get_value());
  684. }
  685. void SpriteFramesEditor::_load_pressed() {
  686. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  687. loading_scene = false;
  688. file->clear_filters();
  689. List<String> extensions;
  690. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
  691. for (const String &extension : extensions) {
  692. file->add_filter("*." + extension);
  693. }
  694. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);
  695. file->popup_file_dialog();
  696. }
  697. void SpriteFramesEditor::_paste_pressed() {
  698. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  699. Ref<ClipboardSpriteFrames> clipboard_frames = EditorSettings::get_singleton()->get_resource_clipboard();
  700. if (clipboard_frames.is_valid()) {
  701. _paste_frame_array(clipboard_frames);
  702. return;
  703. }
  704. Ref<Texture2D> texture = EditorSettings::get_singleton()->get_resource_clipboard();
  705. if (texture.is_valid()) {
  706. _paste_texture(texture);
  707. return;
  708. }
  709. }
  710. void SpriteFramesEditor::_paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames) {
  711. if (p_clipboard_frames->frames.is_empty()) {
  712. return;
  713. }
  714. Ref<Texture2D> texture;
  715. float duration = 1.0;
  716. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  717. undo_redo->create_action(TTR("Paste Frame(s)"), UndoRedo::MERGE_DISABLE, frames.ptr());
  718. int undo_index = frames->get_frame_count(edited_anim);
  719. for (int index = 0; index < p_clipboard_frames->frames.size(); index++) {
  720. const ClipboardSpriteFrames::Frame &frame = p_clipboard_frames->frames[index];
  721. texture = frame.texture;
  722. duration = frame.duration;
  723. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
  724. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
  725. }
  726. undo_redo->add_do_method(this, "_update_library");
  727. undo_redo->add_undo_method(this, "_update_library");
  728. undo_redo->commit_action();
  729. }
  730. void SpriteFramesEditor::_paste_texture(const Ref<Texture2D> &p_texture) {
  731. float duration = 1.0;
  732. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  733. undo_redo->create_action(TTR("Paste Texture"), UndoRedo::MERGE_DISABLE, frames.ptr());
  734. int undo_index = frames->get_frame_count(edited_anim);
  735. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, p_texture, duration);
  736. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, undo_index);
  737. undo_redo->add_do_method(this, "_update_library");
  738. undo_redo->add_undo_method(this, "_update_library");
  739. undo_redo->commit_action();
  740. }
  741. void SpriteFramesEditor::_copy_pressed() {
  742. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  743. Vector<int> selected_items = frame_list->get_selected_items();
  744. if (selected_items.is_empty()) {
  745. return;
  746. }
  747. Ref<ClipboardSpriteFrames> clipboard_frames = memnew(ClipboardSpriteFrames);
  748. for (const int &frame_index : selected_items) {
  749. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, frame_index);
  750. if (texture.is_null()) {
  751. continue;
  752. }
  753. ClipboardSpriteFrames::Frame frame;
  754. frame.texture = texture;
  755. frame.duration = frames->get_frame_duration(edited_anim, frame_index);
  756. clipboard_frames->frames.push_back(frame);
  757. }
  758. EditorSettings::get_singleton()->set_resource_clipboard(clipboard_frames);
  759. }
  760. void SpriteFramesEditor::_empty_pressed() {
  761. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  762. int from = -1;
  763. Vector<int> selected_items = frame_list->get_selected_items();
  764. if (!selected_items.is_empty()) {
  765. from = selected_items[0];
  766. selection.clear();
  767. selection.push_back(from + 1);
  768. } else {
  769. from = frames->get_frame_count(edited_anim);
  770. }
  771. Ref<Texture2D> texture;
  772. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  773. undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
  774. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from);
  775. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from);
  776. undo_redo->add_do_method(this, "_update_library");
  777. undo_redo->add_undo_method(this, "_update_library");
  778. undo_redo->commit_action();
  779. }
  780. void SpriteFramesEditor::_empty2_pressed() {
  781. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  782. int from = -1;
  783. Vector<int> selected_items = frame_list->get_selected_items();
  784. if (!selected_items.is_empty()) {
  785. from = selected_items[selected_items.size() - 1];
  786. selection.clear();
  787. selection.push_back(from);
  788. } else {
  789. from = frames->get_frame_count(edited_anim);
  790. }
  791. Ref<Texture2D> texture;
  792. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  793. undo_redo->create_action(TTR("Add Empty"), UndoRedo::MERGE_DISABLE, frames.ptr());
  794. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, from + 1);
  795. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, from + 1);
  796. undo_redo->add_do_method(this, "_update_library");
  797. undo_redo->add_undo_method(this, "_update_library");
  798. undo_redo->commit_action();
  799. }
  800. void SpriteFramesEditor::_up_pressed() {
  801. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  802. Vector<int> selected_items = frame_list->get_selected_items();
  803. int nb_selected_items = selected_items.size();
  804. if (nb_selected_items <= 0) {
  805. return;
  806. }
  807. int first_selected_frame_index = selected_items[0];
  808. if (first_selected_frame_index < 1) {
  809. return;
  810. }
  811. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  812. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  813. int last_overwritten_frame = -1;
  814. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  815. int to_move = selected_items[selected_index];
  816. int new_index = to_move - 1;
  817. selected_items.set(selected_index, new_index);
  818. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
  819. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
  820. bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == to_move + 1;
  821. if (last_overwritten_frame == -1) {
  822. last_overwritten_frame = new_index;
  823. }
  824. if (!is_next_item_in_selection) {
  825. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, to_move, frames->get_frame_texture(edited_anim, last_overwritten_frame), frames->get_frame_duration(edited_anim, last_overwritten_frame));
  826. 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));
  827. last_overwritten_frame = -1;
  828. }
  829. }
  830. selection = selected_items;
  831. undo_redo->add_do_method(this, "_update_library");
  832. undo_redo->add_undo_method(this, "_update_library");
  833. undo_redo->commit_action();
  834. }
  835. void SpriteFramesEditor::_down_pressed() {
  836. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  837. Vector<int> selected_items = frame_list->get_selected_items();
  838. int nb_selected_items = selected_items.size();
  839. if (nb_selected_items <= 0) {
  840. return;
  841. }
  842. int last_selected_frame_index = selected_items[nb_selected_items - 1];
  843. if (last_selected_frame_index >= frames->get_frame_count(edited_anim) - 1) {
  844. return;
  845. }
  846. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  847. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  848. int first_moved_frame = -1;
  849. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  850. int to_move = selected_items[selected_index];
  851. int new_index = to_move + 1;
  852. selected_items.set(selected_index, new_index);
  853. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, to_move), frames->get_frame_duration(edited_anim, to_move));
  854. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, new_index, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
  855. bool is_next_item_in_selection = selected_index + 1 < nb_selected_items && selected_items[selected_index + 1] == new_index;
  856. if (first_moved_frame == -1) {
  857. first_moved_frame = to_move;
  858. }
  859. if (!is_next_item_in_selection) {
  860. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, first_moved_frame, frames->get_frame_texture(edited_anim, new_index), frames->get_frame_duration(edited_anim, new_index));
  861. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, first_moved_frame, frames->get_frame_texture(edited_anim, first_moved_frame), frames->get_frame_duration(edited_anim, first_moved_frame));
  862. first_moved_frame = -1;
  863. }
  864. }
  865. selection = selected_items;
  866. undo_redo->add_do_method(this, "_update_library");
  867. undo_redo->add_undo_method(this, "_update_library");
  868. undo_redo->commit_action();
  869. }
  870. void SpriteFramesEditor::_delete_pressed() {
  871. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  872. Vector<int> selected_items = frame_list->get_selected_items();
  873. int nb_selected_items = selected_items.size();
  874. if (nb_selected_items <= 0) {
  875. return;
  876. }
  877. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  878. undo_redo->create_action(TTR("Delete Resource"), UndoRedo::MERGE_DISABLE, frames.ptr());
  879. for (int selected_index = 0; selected_index < nb_selected_items; selected_index++) {
  880. int to_delete = selected_items[selected_index];
  881. undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, to_delete - selected_index);
  882. 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);
  883. }
  884. undo_redo->add_do_method(this, "_update_library");
  885. undo_redo->add_undo_method(this, "_update_library");
  886. undo_redo->commit_action();
  887. }
  888. void SpriteFramesEditor::_animation_selected() {
  889. if (updating) {
  890. return;
  891. }
  892. TreeItem *selected = animations->get_selected();
  893. ERR_FAIL_NULL(selected);
  894. edited_anim = selected->get_text(0);
  895. if (animated_sprite) {
  896. sprite_node_updating = true;
  897. animated_sprite->call("set_animation", edited_anim);
  898. sprite_node_updating = false;
  899. }
  900. _update_library(true);
  901. }
  902. void SpriteFramesEditor::_sync_animation() {
  903. if (!animated_sprite || sprite_node_updating) {
  904. return;
  905. }
  906. _select_animation(animated_sprite->call("get_animation"), false);
  907. _update_stop_icon();
  908. }
  909. void SpriteFramesEditor::_select_animation(const String &p_name, bool p_update_node) {
  910. if (frames.is_null() || !frames->has_animation(p_name)) {
  911. return;
  912. }
  913. edited_anim = p_name;
  914. if (animated_sprite) {
  915. if (p_update_node) {
  916. animated_sprite->call("set_animation", edited_anim);
  917. }
  918. }
  919. _update_library();
  920. }
  921. static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
  922. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  923. if (!edited) {
  924. return;
  925. }
  926. if (p_node != edited && p_node->get_owner() != edited) {
  927. return;
  928. }
  929. {
  930. AnimatedSprite2D *as = Object::cast_to<AnimatedSprite2D>(p_node);
  931. if (as && as->get_sprite_frames() == p_sfames) {
  932. r_nodes->push_back(p_node);
  933. }
  934. }
  935. {
  936. AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
  937. if (as && as->get_sprite_frames() == p_sfames) {
  938. r_nodes->push_back(p_node);
  939. }
  940. }
  941. for (int i = 0; i < p_node->get_child_count(); i++) {
  942. _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
  943. }
  944. }
  945. void SpriteFramesEditor::_animation_name_edited() {
  946. if (updating) {
  947. return;
  948. }
  949. if (!frames->has_animation(edited_anim)) {
  950. return;
  951. }
  952. TreeItem *edited = animations->get_edited();
  953. if (!edited) {
  954. return;
  955. }
  956. String new_name = edited->get_text(0);
  957. if (new_name == String(edited_anim)) {
  958. return;
  959. }
  960. if (new_name.is_empty()) {
  961. new_name = "new_animation";
  962. }
  963. new_name = new_name.replace_char('/', '_').replace_char(',', ' ');
  964. String name = new_name;
  965. int counter = 0;
  966. while (frames->has_animation(name)) {
  967. if (name == String(edited_anim)) {
  968. edited->set_text(0, name); // The name didn't change, just updated the column text to name.
  969. return;
  970. }
  971. counter++;
  972. name = new_name + "_" + itos(counter);
  973. }
  974. edited->set_text(0, name);
  975. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  976. undo_redo->create_action(TTR("Rename Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  977. undo_redo->add_do_method(frames.ptr(), "rename_animation", edited_anim, name);
  978. undo_redo->add_undo_method(frames.ptr(), "rename_animation", name, edited_anim);
  979. _rename_node_animation(undo_redo, false, edited_anim, name, name);
  980. _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
  981. undo_redo->add_do_method(this, "_select_animation", name);
  982. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  983. undo_redo->add_do_method(this, "_update_library");
  984. undo_redo->add_undo_method(this, "_update_library");
  985. undo_redo->commit_action();
  986. animations->grab_focus();
  987. }
  988. 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) {
  989. List<Node *> nodes;
  990. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  991. if (is_undo) {
  992. for (Node *E : nodes) {
  993. String current_name = E->call("get_animation");
  994. if (current_name == p_filter) {
  995. undo_redo->force_fixed_history(); // Fixes corner-case when editing SpriteFrames stored as separate file.
  996. undo_redo->add_undo_method(E, "set_animation", p_new_animation);
  997. }
  998. String autoplay_name = E->call("get_autoplay");
  999. if (autoplay_name == p_filter) {
  1000. undo_redo->force_fixed_history();
  1001. undo_redo->add_undo_method(E, "set_autoplay", p_new_autoplay);
  1002. }
  1003. }
  1004. } else {
  1005. for (Node *E : nodes) {
  1006. String current_name = E->call("get_animation");
  1007. if (current_name == p_filter) {
  1008. undo_redo->force_fixed_history();
  1009. undo_redo->add_do_method(E, "set_animation", p_new_animation);
  1010. }
  1011. String autoplay_name = E->call("get_autoplay");
  1012. if (autoplay_name == p_filter) {
  1013. undo_redo->force_fixed_history();
  1014. undo_redo->add_do_method(E, "set_autoplay", p_new_autoplay);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. void SpriteFramesEditor::_animation_add() {
  1020. String name = "new_animation";
  1021. int counter = 0;
  1022. while (frames->has_animation(name)) {
  1023. counter++;
  1024. name = vformat("new_animation_%d", counter);
  1025. }
  1026. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1027. undo_redo->create_action(TTR("Add Animation"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1028. undo_redo->add_do_method(frames.ptr(), "add_animation", name);
  1029. undo_redo->add_undo_method(frames.ptr(), "remove_animation", name);
  1030. undo_redo->add_do_method(this, "_select_animation", name);
  1031. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  1032. undo_redo->add_do_method(this, "_update_library");
  1033. undo_redo->add_undo_method(this, "_update_library");
  1034. undo_redo->commit_action();
  1035. animations->grab_focus();
  1036. }
  1037. void SpriteFramesEditor::_animation_duplicate() {
  1038. if (updating) {
  1039. return;
  1040. }
  1041. if (!frames->has_animation(edited_anim)) {
  1042. return;
  1043. }
  1044. String new_name = _generate_unique_animation_name(edited_anim);
  1045. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1046. undo_redo->create_action(TTR("Duplicate Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
  1047. undo_redo->add_do_method(frames.ptr(), "duplicate_animation", edited_anim, new_name);
  1048. undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
  1049. undo_redo->add_do_method(this, "_select_animation", new_name);
  1050. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  1051. undo_redo->add_do_method(this, "_update_library");
  1052. undo_redo->add_undo_method(this, "_update_library");
  1053. undo_redo->commit_action();
  1054. animations->grab_focus();
  1055. }
  1056. void SpriteFramesEditor::_animation_cut() {
  1057. if (!frames->has_animation(edited_anim)) {
  1058. return;
  1059. }
  1060. // Copy animation to clipboard.
  1061. Ref<ClipboardAnimation> clipboard_anim = ClipboardAnimation::from_sprite_frames(frames, edited_anim);
  1062. EditorSettings::get_singleton()->set_resource_clipboard(clipboard_anim);
  1063. // Remove animation with undo/redo (no confirmation dialog).
  1064. _animation_remove_undo_redo(TTR("Cut Animation"), &clipboard_anim->frames);
  1065. }
  1066. void SpriteFramesEditor::_animation_copy() {
  1067. if (!frames->has_animation(edited_anim)) {
  1068. return;
  1069. }
  1070. Ref<ClipboardAnimation> clipboard_anim = ClipboardAnimation::from_sprite_frames(frames, edited_anim);
  1071. EditorSettings::get_singleton()->set_resource_clipboard(clipboard_anim);
  1072. }
  1073. void SpriteFramesEditor::_animation_paste() {
  1074. if (updating) {
  1075. return;
  1076. }
  1077. Ref<ClipboardAnimation> clipboard_anim = EditorSettings::get_singleton()->get_resource_clipboard();
  1078. if (clipboard_anim.is_null()) {
  1079. return;
  1080. }
  1081. String new_name = _generate_unique_animation_name(clipboard_anim->name);
  1082. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1083. undo_redo->create_action(TTR("Paste Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
  1084. undo_redo->add_do_method(frames.ptr(), "add_animation", new_name);
  1085. undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
  1086. undo_redo->add_do_method(frames.ptr(), "set_animation_speed", new_name, clipboard_anim->speed);
  1087. undo_redo->add_do_method(frames.ptr(), "set_animation_loop", new_name, clipboard_anim->loop);
  1088. for (ClipboardSpriteFrames::Frame &frame : clipboard_anim->frames) {
  1089. undo_redo->add_do_method(frames.ptr(), "add_frame", new_name, frame.texture, frame.duration);
  1090. }
  1091. undo_redo->add_do_method(this, "_select_animation", new_name);
  1092. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  1093. undo_redo->add_do_method(this, "_update_library");
  1094. undo_redo->add_undo_method(this, "_update_library");
  1095. undo_redo->commit_action();
  1096. }
  1097. void SpriteFramesEditor::_animation_remove() {
  1098. if (updating) {
  1099. return;
  1100. }
  1101. if (!frames->has_animation(edited_anim)) {
  1102. return;
  1103. }
  1104. delete_dialog->set_text(TTRC("Delete Animation?"));
  1105. delete_dialog->popup_centered();
  1106. }
  1107. void SpriteFramesEditor::_animation_remove_confirmed() {
  1108. _animation_remove_undo_redo(TTR("Remove Animation"), nullptr);
  1109. }
  1110. void SpriteFramesEditor::_animation_search_text_changed(const String &p_text) {
  1111. _update_library();
  1112. }
  1113. void SpriteFramesEditor::_animation_loop_changed() {
  1114. if (updating) {
  1115. return;
  1116. }
  1117. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1118. undo_redo->create_action(TTR("Change Animation Loop"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1119. undo_redo->add_do_method(frames.ptr(), "set_animation_loop", edited_anim, anim_loop->is_pressed());
  1120. undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  1121. undo_redo->add_do_method(this, "_update_library", true);
  1122. undo_redo->add_undo_method(this, "_update_library", true);
  1123. undo_redo->commit_action();
  1124. }
  1125. void SpriteFramesEditor::_animation_speed_resized() {
  1126. anim_speed->update_minimum_size();
  1127. }
  1128. void SpriteFramesEditor::_animation_speed_changed(double p_value) {
  1129. if (frames.is_null()) {
  1130. return;
  1131. }
  1132. if (updating) {
  1133. return;
  1134. }
  1135. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1136. undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS, frames.ptr());
  1137. undo_redo->add_do_method(frames.ptr(), "set_animation_speed", edited_anim, p_value);
  1138. undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  1139. undo_redo->add_do_method(this, "_update_library", true);
  1140. undo_redo->add_undo_method(this, "_update_library", true);
  1141. undo_redo->commit_action();
  1142. }
  1143. void SpriteFramesEditor::_animation_remove_undo_redo(const StringName &p_action_name, const Vector<ClipboardSpriteFrames::Frame> *p_frames) {
  1144. StringName new_edited = _find_next_animation();
  1145. int frame_count = frames->get_frame_count(edited_anim);
  1146. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1147. undo_redo->create_action(p_action_name, UndoRedo::MERGE_DISABLE, frames.ptr());
  1148. _rename_node_animation(undo_redo, false, edited_anim, new_edited, "");
  1149. undo_redo->add_do_method(frames.ptr(), "remove_animation", edited_anim);
  1150. undo_redo->add_undo_method(frames.ptr(), "add_animation", edited_anim);
  1151. _rename_node_animation(undo_redo, true, edited_anim, edited_anim, edited_anim);
  1152. undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  1153. undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  1154. for (int i = 0; i < frame_count; i++) {
  1155. Ref<Texture2D> texture;
  1156. float duration;
  1157. if (p_frames) {
  1158. texture = (*p_frames)[i].texture;
  1159. duration = (*p_frames)[i].duration;
  1160. } else {
  1161. texture = frames->get_frame_texture(edited_anim, i);
  1162. duration = frames->get_frame_duration(edited_anim, i);
  1163. }
  1164. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration);
  1165. }
  1166. undo_redo->add_do_method(this, "_select_animation", new_edited);
  1167. undo_redo->add_undo_method(this, "_select_animation", edited_anim);
  1168. undo_redo->add_do_method(this, "_update_library");
  1169. undo_redo->add_undo_method(this, "_update_library");
  1170. undo_redo->commit_action();
  1171. }
  1172. StringName SpriteFramesEditor::_find_next_animation() {
  1173. List<StringName> anim_names;
  1174. frames->get_animation_list(&anim_names);
  1175. anim_names.sort_custom<StringName::AlphCompare>();
  1176. if (anim_names.size() >= 2) {
  1177. if (edited_anim == anim_names.get(0)) {
  1178. return anim_names.get(1);
  1179. } else {
  1180. return anim_names.get(0);
  1181. }
  1182. } else {
  1183. return StringName();
  1184. }
  1185. }
  1186. String SpriteFramesEditor::_generate_unique_animation_name(const String &p_base_name) const {
  1187. if (!frames->has_animation(p_base_name)) {
  1188. return p_base_name;
  1189. }
  1190. int count = 2;
  1191. String new_name = p_base_name;
  1192. PackedStringArray split = new_name.split("_");
  1193. int last_index = split.size() - 1;
  1194. if (last_index > 0 && split[last_index].is_valid_int() && split[last_index].to_int() >= 0) {
  1195. count = split[last_index].to_int();
  1196. split.remove_at(last_index);
  1197. new_name = String("_").join(split);
  1198. }
  1199. while (true) {
  1200. String attempt = new_name;
  1201. attempt += vformat("_%d", count);
  1202. if (frames->has_animation(attempt)) {
  1203. count++;
  1204. continue;
  1205. }
  1206. new_name = attempt;
  1207. break;
  1208. }
  1209. return new_name;
  1210. }
  1211. void SpriteFramesEditor::_frame_list_gui_input(const Ref<InputEvent> &p_event) {
  1212. const Ref<InputEventMouseButton> mb = p_event;
  1213. if (mb.is_valid()) {
  1214. if (mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed() && mb->is_ctrl_pressed()) {
  1215. _zoom_in();
  1216. // Don't scroll up after zooming in.
  1217. accept_event();
  1218. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed() && mb->is_ctrl_pressed()) {
  1219. _zoom_out();
  1220. // Don't scroll down after zooming out.
  1221. accept_event();
  1222. } else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  1223. Point2 pos = mb->get_position();
  1224. right_clicked_frame = frame_list->get_item_at_position(pos, true);
  1225. if (right_clicked_frame != -1) {
  1226. Ref<Texture2D> tex = frames->get_frame_texture(edited_anim, right_clicked_frame);
  1227. if (tex.is_null()) {
  1228. return;
  1229. }
  1230. if (!menu) {
  1231. menu = memnew(PopupMenu);
  1232. add_child(menu);
  1233. menu->connect(SceneStringName(id_pressed), callable_mp(this, &SpriteFramesEditor::_menu_selected));
  1234. menu->add_icon_item(get_editor_theme_icon(SNAME("ShowInFileSystem")), TTRC("Show in FileSystem"), MENU_SHOW_IN_FILESYSTEM);
  1235. }
  1236. menu->set_position(get_screen_position() + get_local_mouse_position());
  1237. menu->popup();
  1238. }
  1239. }
  1240. }
  1241. }
  1242. void SpriteFramesEditor::_menu_selected(int p_id) {
  1243. switch (p_id) {
  1244. case MENU_SHOW_IN_FILESYSTEM: {
  1245. Ref<Texture2D> frame_texture = frames->get_frame_texture(edited_anim, right_clicked_frame);
  1246. ERR_FAIL_COND(frame_texture.is_null());
  1247. String path = frame_texture->get_path();
  1248. // Check if the file is an atlas resource, if it is find the source texture.
  1249. Ref<AtlasTexture> at = frame_texture;
  1250. while (at.is_valid() && at->get_atlas().is_valid()) {
  1251. path = at->get_atlas()->get_path();
  1252. at = at->get_atlas();
  1253. }
  1254. FileSystemDock::get_singleton()->navigate_to_path(path);
  1255. } break;
  1256. }
  1257. }
  1258. void SpriteFramesEditor::_frame_list_item_selected(int p_index, bool p_selected) {
  1259. if (updating) {
  1260. return;
  1261. }
  1262. selection = frame_list->get_selected_items();
  1263. if (selection.is_empty() || !p_selected) {
  1264. return;
  1265. }
  1266. updating = true;
  1267. frame_duration->set_value(frames->get_frame_duration(edited_anim, selection[0]));
  1268. updating = false;
  1269. }
  1270. void SpriteFramesEditor::_frame_duration_changed(double p_value) {
  1271. if (frames.is_null()) {
  1272. return;
  1273. }
  1274. if (updating) {
  1275. return;
  1276. }
  1277. if (selection.is_empty()) {
  1278. return;
  1279. }
  1280. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1281. undo_redo->create_action(TTR("Set Frame Duration"), UndoRedo::MERGE_ENDS, frames.ptr());
  1282. for (const int &index : selection) {
  1283. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, index);
  1284. float old_duration = frames->get_frame_duration(edited_anim, index);
  1285. undo_redo->add_do_method(frames.ptr(), "set_frame", edited_anim, index, texture, p_value);
  1286. undo_redo->add_undo_method(frames.ptr(), "set_frame", edited_anim, index, texture, old_duration);
  1287. }
  1288. undo_redo->add_do_method(this, "_update_library");
  1289. undo_redo->add_undo_method(this, "_update_library");
  1290. undo_redo->commit_action();
  1291. }
  1292. void SpriteFramesEditor::_zoom_in() {
  1293. // Do not zoom in or out with no visible frames
  1294. if (frames->get_frame_count(edited_anim) <= 0) {
  1295. return;
  1296. }
  1297. if (thumbnail_zoom < max_thumbnail_zoom) {
  1298. thumbnail_zoom *= scale_ratio;
  1299. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  1300. frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  1301. frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  1302. }
  1303. }
  1304. void SpriteFramesEditor::_zoom_out() {
  1305. // Do not zoom in or out with no visible frames
  1306. if (frames->get_frame_count(edited_anim) <= 0) {
  1307. return;
  1308. }
  1309. if (thumbnail_zoom > min_thumbnail_zoom) {
  1310. thumbnail_zoom /= scale_ratio;
  1311. int thumbnail_size = (int)(thumbnail_default_size * thumbnail_zoom);
  1312. frame_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  1313. frame_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  1314. }
  1315. }
  1316. void SpriteFramesEditor::_zoom_reset() {
  1317. thumbnail_zoom = MAX(1.0f, EDSCALE);
  1318. frame_list->set_fixed_column_width(thumbnail_default_size * 3 / 2);
  1319. frame_list->set_fixed_icon_size(Size2(thumbnail_default_size, thumbnail_default_size));
  1320. }
  1321. void SpriteFramesEditor::_update_library(bool p_skip_selector) {
  1322. if (!p_skip_selector) {
  1323. animations_dirty = true;
  1324. }
  1325. if (pending_update) {
  1326. return;
  1327. }
  1328. pending_update = true;
  1329. callable_mp(this, &SpriteFramesEditor::_update_library_impl).call_deferred();
  1330. }
  1331. void SpriteFramesEditor::_update_library_impl() {
  1332. pending_update = false;
  1333. if (frames.is_null()) {
  1334. return;
  1335. }
  1336. updating = true;
  1337. frame_duration->set_value_no_signal(1.0); // Default.
  1338. if (animations_dirty) {
  1339. animations_dirty = false;
  1340. animations->clear();
  1341. TreeItem *anim_root = animations->create_item();
  1342. List<StringName> anim_names;
  1343. frames->get_animation_list(&anim_names);
  1344. anim_names.sort_custom<StringName::AlphCompare>();
  1345. if (!anim_names.size()) {
  1346. missing_anim_label->show();
  1347. anim_frames_vb->hide();
  1348. updating = false;
  1349. return;
  1350. }
  1351. missing_anim_label->hide();
  1352. anim_frames_vb->show();
  1353. bool searching = anim_search_box->get_text().size();
  1354. String searched_string = searching ? anim_search_box->get_text().to_lower() : String();
  1355. TreeItem *selected = nullptr;
  1356. for (const StringName &E : anim_names) {
  1357. String name = E;
  1358. if (searching && !name.to_lower().contains(searched_string)) {
  1359. continue;
  1360. }
  1361. TreeItem *it = animations->create_item(anim_root);
  1362. it->set_metadata(0, name);
  1363. it->set_text(0, name);
  1364. it->set_editable(0, true);
  1365. if (animated_sprite) {
  1366. if (name == String(animated_sprite->call("get_autoplay"))) {
  1367. it->set_icon(0, autoplay_icon);
  1368. }
  1369. }
  1370. if (E == edited_anim) {
  1371. it->select(0);
  1372. selected = it;
  1373. }
  1374. }
  1375. if (selected) {
  1376. animations->scroll_to_item(selected);
  1377. }
  1378. }
  1379. if (animated_sprite) {
  1380. String autoplay_name = animated_sprite->call("get_autoplay");
  1381. if (autoplay_name.is_empty()) {
  1382. autoplay->set_pressed_no_signal(false);
  1383. } else {
  1384. autoplay->set_pressed_no_signal(String(edited_anim) == autoplay_name);
  1385. }
  1386. }
  1387. frame_list->clear();
  1388. if (!frames->has_animation(edited_anim)) {
  1389. updating = false;
  1390. return;
  1391. }
  1392. int anim_frame_count = frames->get_frame_count(edited_anim);
  1393. if (anim_frame_count == 0) {
  1394. selection.clear();
  1395. }
  1396. for (int index = 0; index < selection.size(); index++) {
  1397. int sel = selection[index];
  1398. if (sel == -1) {
  1399. selection.remove_at(index);
  1400. index--;
  1401. }
  1402. if (sel >= anim_frame_count) {
  1403. selection.set(index, anim_frame_count - 1);
  1404. // Since selection is ordered, if we get a frame that is outside of the range
  1405. // we can clip all the other one.
  1406. selection.resize(index + 1);
  1407. break;
  1408. }
  1409. }
  1410. if (selection.is_empty() && frames->get_frame_count(edited_anim)) {
  1411. selection.push_back(0);
  1412. }
  1413. bool is_first_selection = true;
  1414. // NOTE: When the language is changed, the text of the items is updated in `NOTIFICATION_TRANSLATION_CHANGED`.
  1415. // If there are changes related to the items and their text in the loop below, the code in `NOTIFICATION_TRANSLATION_CHANGED` must also be changed.
  1416. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  1417. String name = itos(i);
  1418. Ref<Texture2D> texture = frames->get_frame_texture(edited_anim, i);
  1419. float duration = frames->get_frame_duration(edited_anim, i);
  1420. if (texture.is_null()) {
  1421. texture = empty_icon;
  1422. name += ": " + TTR("(empty)");
  1423. } else if (!texture->get_name().is_empty()) {
  1424. name += ": " + texture->get_name();
  1425. }
  1426. if (duration != 1.0f) {
  1427. name += String::utf8(" [× ") + String::num(duration, 2) + "]";
  1428. }
  1429. frame_list->add_item(name, texture);
  1430. if (texture.is_valid()) {
  1431. String tooltip = texture->get_path();
  1432. // Frame is often saved as an AtlasTexture subresource within a scene/resource file,
  1433. // thus its path might be not what the user is looking for. So we're also showing
  1434. // subsequent source texture paths.
  1435. String prefix = U"┖╴";
  1436. Ref<AtlasTexture> at = texture;
  1437. while (at.is_valid() && at->get_atlas().is_valid()) {
  1438. tooltip += "\n" + prefix + at->get_atlas()->get_path();
  1439. prefix = " " + prefix;
  1440. at = at->get_atlas();
  1441. }
  1442. frame_list->set_item_tooltip(-1, tooltip);
  1443. }
  1444. if (selection.has(i)) {
  1445. frame_list->select(frame_list->get_item_count() - 1, is_first_selection);
  1446. if (is_first_selection) {
  1447. frame_duration->set_value_no_signal(frames->get_frame_duration(edited_anim, i));
  1448. }
  1449. is_first_selection = false;
  1450. }
  1451. }
  1452. anim_speed->set_value_no_signal(frames->get_animation_speed(edited_anim));
  1453. anim_loop->set_pressed_no_signal(frames->get_animation_loop(edited_anim));
  1454. updating = false;
  1455. }
  1456. void SpriteFramesEditor::_edit() {
  1457. if (!animated_sprite) {
  1458. return;
  1459. }
  1460. edit(animated_sprite->call("get_sprite_frames"));
  1461. }
  1462. void SpriteFramesEditor::edit(Ref<SpriteFrames> p_frames) {
  1463. _update_stop_icon();
  1464. if (p_frames.is_null()) {
  1465. frames.unref();
  1466. _remove_sprite_node();
  1467. return;
  1468. }
  1469. frames = p_frames;
  1470. read_only = EditorNode::get_singleton()->is_resource_read_only(p_frames);
  1471. if (!p_frames->has_animation(edited_anim)) {
  1472. List<StringName> anim_names;
  1473. frames->get_animation_list(&anim_names);
  1474. anim_names.sort_custom<StringName::AlphCompare>();
  1475. if (anim_names.size()) {
  1476. edited_anim = anim_names.front()->get();
  1477. } else {
  1478. edited_anim = StringName();
  1479. }
  1480. }
  1481. _update_library();
  1482. // Clear zoom and split sheet texture
  1483. split_sheet_preview->set_texture(Ref<Texture2D>());
  1484. _zoom_reset();
  1485. add_anim->set_disabled(read_only);
  1486. duplicate_anim->set_disabled(read_only);
  1487. cut_anim->set_disabled(read_only);
  1488. copy_anim->set_disabled(read_only);
  1489. paste_anim->set_disabled(read_only);
  1490. delete_anim->set_disabled(read_only);
  1491. anim_speed->set_editable(!read_only);
  1492. anim_loop->set_disabled(read_only);
  1493. load->set_disabled(read_only);
  1494. load_sheet->set_disabled(read_only);
  1495. copy->set_disabled(read_only);
  1496. paste->set_disabled(read_only);
  1497. empty_before->set_disabled(read_only);
  1498. empty_after->set_disabled(read_only);
  1499. move_up->set_disabled(read_only);
  1500. move_down->set_disabled(read_only);
  1501. delete_frame->set_disabled(read_only);
  1502. _fetch_sprite_node(); // Fetch node after set frames.
  1503. }
  1504. Ref<SpriteFrames> SpriteFramesEditor::get_sprite_frames() const {
  1505. return frames;
  1506. }
  1507. Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1508. if (read_only) {
  1509. return false;
  1510. }
  1511. if (!frames->has_animation(edited_anim)) {
  1512. return false;
  1513. }
  1514. int idx = -1;
  1515. if (p_point == Vector2(Math::INF, Math::INF)) {
  1516. if (frame_list->is_anything_selected()) {
  1517. idx = frame_list->get_selected_items()[0];
  1518. }
  1519. } else {
  1520. idx = frame_list->get_item_at_position(p_point, true);
  1521. }
  1522. if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) {
  1523. return Variant();
  1524. }
  1525. Ref<Resource> frame = frames->get_frame_texture(edited_anim, idx);
  1526. if (frame.is_null()) {
  1527. return Variant();
  1528. }
  1529. Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from);
  1530. drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw'
  1531. return drag_data;
  1532. }
  1533. bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1534. if (read_only) {
  1535. return false;
  1536. }
  1537. Dictionary d = p_data;
  1538. if (!d.has("type")) {
  1539. return false;
  1540. }
  1541. // reordering frames
  1542. if (d.has("from") && (Object *)(d["from"]) == frame_list) {
  1543. return true;
  1544. }
  1545. if (String(d["type"]) == "resource" && d.has("resource")) {
  1546. Ref<Resource> r = d["resource"];
  1547. Ref<Texture2D> texture = r;
  1548. if (texture.is_valid()) {
  1549. return true;
  1550. }
  1551. }
  1552. if (String(d["type"]) == "files") {
  1553. Vector<String> files = d["files"];
  1554. if (files.is_empty()) {
  1555. return false;
  1556. }
  1557. for (int i = 0; i < files.size(); i++) {
  1558. const String &f = files[i];
  1559. String ftype = EditorFileSystem::get_singleton()->get_file_type(f);
  1560. if (!ClassDB::is_parent_class(ftype, "Texture2D")) {
  1561. return false;
  1562. }
  1563. }
  1564. return true;
  1565. }
  1566. return false;
  1567. }
  1568. void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1569. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1570. return;
  1571. }
  1572. Dictionary d = p_data;
  1573. if (!d.has("type")) {
  1574. return;
  1575. }
  1576. int at_pos = -1;
  1577. if (p_point == Vector2(Math::INF, Math::INF)) {
  1578. if (frame_list->is_anything_selected()) {
  1579. at_pos = frame_list->get_selected_items()[0];
  1580. }
  1581. } else {
  1582. at_pos = frame_list->get_item_at_position(p_point, true);
  1583. }
  1584. if (String(d["type"]) == "resource" && d.has("resource")) {
  1585. Ref<Resource> r = d["resource"];
  1586. Ref<Texture2D> texture = r;
  1587. if (texture.is_valid()) {
  1588. bool reorder = false;
  1589. if (d.has("from") && (Object *)(d["from"]) == frame_list) {
  1590. reorder = true;
  1591. }
  1592. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1593. if (reorder) { //drop is from reordering frames
  1594. int from_frame = -1;
  1595. float duration = 1.0;
  1596. if (d.has("frame")) {
  1597. from_frame = d["frame"];
  1598. duration = frames->get_frame_duration(edited_anim, from_frame);
  1599. }
  1600. undo_redo->create_action(TTR("Move Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1601. undo_redo->add_do_method(frames.ptr(), "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame);
  1602. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, duration, at_pos == -1 ? -1 : at_pos);
  1603. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) - 1 : at_pos);
  1604. undo_redo->add_undo_method(frames.ptr(), "add_frame", edited_anim, texture, duration, from_frame);
  1605. undo_redo->add_do_method(this, "_update_library");
  1606. undo_redo->add_undo_method(this, "_update_library");
  1607. undo_redo->commit_action();
  1608. } else {
  1609. undo_redo->create_action(TTR("Add Frame"), UndoRedo::MERGE_DISABLE, frames.ptr());
  1610. undo_redo->add_do_method(frames.ptr(), "add_frame", edited_anim, texture, 1.0, at_pos == -1 ? -1 : at_pos);
  1611. undo_redo->add_undo_method(frames.ptr(), "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
  1612. undo_redo->add_do_method(this, "_update_library");
  1613. undo_redo->add_undo_method(this, "_update_library");
  1614. undo_redo->commit_action();
  1615. }
  1616. }
  1617. }
  1618. if (String(d["type"]) == "files") {
  1619. Vector<String> files = d["files"];
  1620. if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1621. _prepare_sprite_sheet(files[0]);
  1622. } else {
  1623. _file_load_request(files, at_pos);
  1624. }
  1625. }
  1626. }
  1627. void SpriteFramesEditor::_update_stop_icon() {
  1628. bool is_playing = false;
  1629. if (animated_sprite) {
  1630. is_playing = animated_sprite->call("is_playing");
  1631. }
  1632. if (is_playing) {
  1633. stop->set_button_icon(pause_icon);
  1634. } else {
  1635. stop->set_button_icon(stop_icon);
  1636. }
  1637. }
  1638. void SpriteFramesEditor::_remove_sprite_node() {
  1639. if (!animated_sprite) {
  1640. return;
  1641. }
  1642. if (animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
  1643. animated_sprite->disconnect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
  1644. }
  1645. if (animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
  1646. animated_sprite->disconnect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation));
  1647. }
  1648. if (animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
  1649. animated_sprite->disconnect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
  1650. }
  1651. animated_sprite = nullptr;
  1652. }
  1653. void SpriteFramesEditor::_fetch_sprite_node() {
  1654. Node *selected = nullptr;
  1655. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  1656. const List<Node *> &top_node_list = editor_selection->get_top_selected_node_list();
  1657. if (top_node_list.size() == 1) {
  1658. selected = top_node_list.front()->get();
  1659. }
  1660. bool show_node_edit = false;
  1661. AnimatedSprite2D *as2d = Object::cast_to<AnimatedSprite2D>(selected);
  1662. AnimatedSprite3D *as3d = Object::cast_to<AnimatedSprite3D>(selected);
  1663. if (as2d || as3d) {
  1664. if (frames != selected->call("get_sprite_frames")) {
  1665. _remove_sprite_node();
  1666. } else {
  1667. animated_sprite = selected;
  1668. if (!animated_sprite->is_connected("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit))) {
  1669. animated_sprite->connect("sprite_frames_changed", callable_mp(this, &SpriteFramesEditor::_edit));
  1670. }
  1671. if (!animated_sprite->is_connected(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation))) {
  1672. animated_sprite->connect(SceneStringName(animation_changed), callable_mp(this, &SpriteFramesEditor::_sync_animation), CONNECT_DEFERRED);
  1673. }
  1674. if (!animated_sprite->is_connected(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon))) {
  1675. animated_sprite->connect(SceneStringName(animation_finished), callable_mp(this, &SpriteFramesEditor::_update_stop_icon));
  1676. }
  1677. show_node_edit = true;
  1678. }
  1679. } else {
  1680. _remove_sprite_node();
  1681. }
  1682. if (show_node_edit) {
  1683. _sync_animation();
  1684. autoplay_container->show();
  1685. playback_container->show();
  1686. } else {
  1687. _update_library(); // To init autoplay icon.
  1688. autoplay_container->hide();
  1689. playback_container->hide();
  1690. }
  1691. }
  1692. void SpriteFramesEditor::_play_pressed() {
  1693. if (animated_sprite) {
  1694. animated_sprite->call("stop");
  1695. animated_sprite->call("play", animated_sprite->call("get_animation"));
  1696. }
  1697. _update_stop_icon();
  1698. }
  1699. void SpriteFramesEditor::_play_from_pressed() {
  1700. if (animated_sprite) {
  1701. animated_sprite->call("play", animated_sprite->call("get_animation"));
  1702. }
  1703. _update_stop_icon();
  1704. }
  1705. void SpriteFramesEditor::_play_bw_pressed() {
  1706. if (animated_sprite) {
  1707. animated_sprite->call("stop");
  1708. animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
  1709. }
  1710. _update_stop_icon();
  1711. }
  1712. void SpriteFramesEditor::_play_bw_from_pressed() {
  1713. if (animated_sprite) {
  1714. animated_sprite->call("play_backwards", animated_sprite->call("get_animation"));
  1715. }
  1716. _update_stop_icon();
  1717. }
  1718. void SpriteFramesEditor::_stop_pressed() {
  1719. if (animated_sprite) {
  1720. if (animated_sprite->call("is_playing")) {
  1721. animated_sprite->call("pause");
  1722. } else {
  1723. animated_sprite->call("stop");
  1724. }
  1725. }
  1726. _update_stop_icon();
  1727. }
  1728. void SpriteFramesEditor::_autoplay_pressed() {
  1729. if (updating) {
  1730. return;
  1731. }
  1732. if (animated_sprite) {
  1733. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1734. undo_redo->create_action(TTR("Toggle Autoplay"), UndoRedo::MERGE_DISABLE, animated_sprite);
  1735. String current = animated_sprite->call("get_animation");
  1736. String current_auto = animated_sprite->call("get_autoplay");
  1737. if (current == current_auto) {
  1738. //unset
  1739. undo_redo->add_do_method(animated_sprite, "set_autoplay", "");
  1740. undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
  1741. } else {
  1742. //set
  1743. undo_redo->add_do_method(animated_sprite, "set_autoplay", current);
  1744. undo_redo->add_undo_method(animated_sprite, "set_autoplay", current_auto);
  1745. }
  1746. undo_redo->add_do_method(this, "_update_library");
  1747. undo_redo->add_undo_method(this, "_update_library");
  1748. undo_redo->commit_action();
  1749. }
  1750. _update_library();
  1751. }
  1752. void SpriteFramesEditor::_bind_methods() {
  1753. ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
  1754. ClassDB::bind_method(D_METHOD("_select_animation", "name", "update_node"), &SpriteFramesEditor::_select_animation, DEFVAL(true));
  1755. }
  1756. void SpriteFramesEditor::_node_removed(Node *p_node) {
  1757. if (animated_sprite) {
  1758. if (animated_sprite != p_node) {
  1759. return;
  1760. }
  1761. _remove_sprite_node();
  1762. }
  1763. }
  1764. SpriteFramesEditor::SpriteFramesEditor() {
  1765. set_name(TTRC("SpriteFrames"));
  1766. set_icon_name("SpriteFrames");
  1767. set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_sprite_frames_bottom_panel", TTRC("Open SpriteFrames Dock")));
  1768. set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
  1769. set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);
  1770. set_global(false);
  1771. set_transient(true);
  1772. HSplitContainer *main_split = memnew(HSplitContainer);
  1773. add_child(main_split);
  1774. VBoxContainer *vbc_animlist = memnew(VBoxContainer);
  1775. main_split->add_child(vbc_animlist);
  1776. vbc_animlist->set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1777. VBoxContainer *sub_vb = memnew(VBoxContainer);
  1778. vbc_animlist->add_margin_child(TTRC("Animations:"), sub_vb, true);
  1779. sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1780. HBoxContainer *hbc_animlist = memnew(HBoxContainer);
  1781. sub_vb->add_child(hbc_animlist);
  1782. add_anim = memnew(Button);
  1783. add_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1784. add_anim->set_accessibility_name(TTRC("Add Animation"));
  1785. hbc_animlist->add_child(add_anim);
  1786. add_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_add));
  1787. duplicate_anim = memnew(Button);
  1788. duplicate_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1789. duplicate_anim->set_accessibility_name(TTRC("Duplicate Animation"));
  1790. hbc_animlist->add_child(duplicate_anim);
  1791. duplicate_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_duplicate));
  1792. duplicate_anim->set_visible(false);
  1793. cut_anim = memnew(Button);
  1794. cut_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1795. cut_anim->set_accessibility_name(TTRC("Cut Animation"));
  1796. hbc_animlist->add_child(cut_anim);
  1797. cut_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_cut));
  1798. copy_anim = memnew(Button);
  1799. copy_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1800. copy_anim->set_accessibility_name(TTRC("Copy Animation"));
  1801. hbc_animlist->add_child(copy_anim);
  1802. copy_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_copy));
  1803. paste_anim = memnew(Button);
  1804. paste_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1805. paste_anim->set_accessibility_name(TTRC("Paste Animation"));
  1806. hbc_animlist->add_child(paste_anim);
  1807. paste_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_paste));
  1808. delete_anim = memnew(Button);
  1809. delete_anim->set_theme_type_variation(SceneStringName(FlatButton));
  1810. delete_anim->set_accessibility_name(TTRC("Delete Animation"));
  1811. hbc_animlist->add_child(delete_anim);
  1812. delete_anim->set_disabled(true);
  1813. delete_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_remove));
  1814. autoplay_container = memnew(HBoxContainer);
  1815. hbc_animlist->add_child(autoplay_container);
  1816. autoplay_container->add_child(memnew(VSeparator));
  1817. autoplay = memnew(Button);
  1818. autoplay->set_theme_type_variation(SceneStringName(FlatButton));
  1819. autoplay->set_tooltip_text(TTRC("Autoplay on Load"));
  1820. autoplay_container->add_child(autoplay);
  1821. hbc_animlist->add_child(memnew(VSeparator));
  1822. anim_loop = memnew(Button);
  1823. anim_loop->set_toggle_mode(true);
  1824. anim_loop->set_theme_type_variation(SceneStringName(FlatButton));
  1825. anim_loop->set_tooltip_text(TTRC("Animation Looping"));
  1826. anim_loop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_loop_changed));
  1827. hbc_animlist->add_child(anim_loop);
  1828. anim_speed = memnew(SpinBox);
  1829. anim_speed->set_suffix(TTR("FPS"));
  1830. anim_speed->set_min(0);
  1831. anim_speed->set_max(120);
  1832. anim_speed->set_step(0.01);
  1833. anim_speed->set_custom_arrow_step(1);
  1834. anim_speed->set_tooltip_text(TTRC("Animation Speed"));
  1835. anim_speed->get_line_edit()->set_expand_to_text_length_enabled(true);
  1836. anim_speed->get_line_edit()->connect(SceneStringName(resized), callable_mp(this, &SpriteFramesEditor::_animation_speed_resized));
  1837. anim_speed->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_animation_speed_changed));
  1838. hbc_animlist->add_child(anim_speed);
  1839. anim_search_box = memnew(LineEdit);
  1840. sub_vb->add_child(anim_search_box);
  1841. anim_search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1842. anim_search_box->set_placeholder(TTRC("Filter Animations"));
  1843. anim_search_box->set_clear_button_enabled(true);
  1844. anim_search_box->connect(SceneStringName(text_changed), callable_mp(this, &SpriteFramesEditor::_animation_search_text_changed));
  1845. animations = memnew(Tree);
  1846. sub_vb->add_child(animations);
  1847. animations->set_v_size_flags(SIZE_EXPAND_FILL);
  1848. animations->set_hide_root(true);
  1849. animations->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1850. // HACK: The cell_selected signal is emitted before the FPS spinbox loses focus and applies the change.
  1851. animations->connect("cell_selected", callable_mp(this, &SpriteFramesEditor::_animation_selected), CONNECT_DEFERRED);
  1852. animations->connect("item_edited", callable_mp(this, &SpriteFramesEditor::_animation_name_edited));
  1853. animations->set_theme_type_variation("TreeSecondary");
  1854. animations->set_allow_reselect(true);
  1855. add_anim->set_shortcut_context(animations);
  1856. add_anim->set_shortcut(ED_SHORTCUT("sprite_frames/new_animation", TTRC("Add Animation"), KeyModifierMask::CMD_OR_CTRL | Key::N));
  1857. duplicate_anim->set_shortcut_context(animations);
  1858. duplicate_anim->set_shortcut(ED_SHORTCUT("sprite_frames/duplicate_animation", TTRC("Duplicate Animation"), KeyModifierMask::CMD_OR_CTRL | Key::D));
  1859. cut_anim->set_shortcut_context(animations);
  1860. cut_anim->set_shortcut(ED_SHORTCUT("sprite_frames/cut_animation", TTRC("Cut Animation"), KeyModifierMask::CMD_OR_CTRL | Key::X));
  1861. copy_anim->set_shortcut_context(animations);
  1862. copy_anim->set_shortcut(ED_SHORTCUT("sprite_frames/copy_animation", TTRC("Copy Animation"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  1863. paste_anim->set_shortcut_context(animations);
  1864. paste_anim->set_shortcut(ED_SHORTCUT("sprite_frames/paste_animation", TTRC("Paste Animation"), KeyModifierMask::CMD_OR_CTRL | Key::V));
  1865. delete_anim->set_shortcut_context(animations);
  1866. delete_anim->set_shortcut(ED_SHORTCUT("sprite_frames/delete_animation", TTRC("Delete Animation"), Key::KEY_DELETE));
  1867. missing_anim_label = memnew(Label);
  1868. missing_anim_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  1869. missing_anim_label->set_text(TTRC("This resource does not have any animations."));
  1870. missing_anim_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1871. missing_anim_label->set_v_size_flags(SIZE_EXPAND_FILL);
  1872. missing_anim_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1873. missing_anim_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1874. missing_anim_label->hide();
  1875. main_split->add_child(missing_anim_label);
  1876. anim_frames_vb = memnew(VBoxContainer);
  1877. main_split->add_child(anim_frames_vb);
  1878. anim_frames_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1879. anim_frames_vb->hide();
  1880. sub_vb = memnew(VBoxContainer);
  1881. anim_frames_vb->add_margin_child(TTRC("Animation Frames:"), sub_vb, true);
  1882. HFlowContainer *hfc = memnew(HFlowContainer);
  1883. sub_vb->add_child(hfc);
  1884. playback_container = memnew(HBoxContainer);
  1885. playback_container->set_layout_direction(LAYOUT_DIRECTION_LTR);
  1886. hfc->add_child(playback_container);
  1887. play_bw_from = memnew(Button);
  1888. play_bw_from->set_theme_type_variation(SceneStringName(FlatButton));
  1889. playback_container->add_child(play_bw_from);
  1890. play_bw = memnew(Button);
  1891. play_bw->set_theme_type_variation(SceneStringName(FlatButton));
  1892. playback_container->add_child(play_bw);
  1893. stop = memnew(Button);
  1894. stop->set_theme_type_variation(SceneStringName(FlatButton));
  1895. playback_container->add_child(stop);
  1896. play = memnew(Button);
  1897. play->set_theme_type_variation(SceneStringName(FlatButton));
  1898. playback_container->add_child(play);
  1899. play_from = memnew(Button);
  1900. play_from->set_theme_type_variation(SceneStringName(FlatButton));
  1901. playback_container->add_child(play_from);
  1902. hfc->add_child(memnew(VSeparator));
  1903. autoplay->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_autoplay_pressed));
  1904. autoplay->set_toggle_mode(true);
  1905. play->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_pressed));
  1906. play_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_from_pressed));
  1907. play_bw->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_pressed));
  1908. play_bw_from->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_play_bw_from_pressed));
  1909. stop->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_stop_pressed));
  1910. HBoxContainer *hbc_actions = memnew(HBoxContainer);
  1911. hfc->add_child(hbc_actions);
  1912. load = memnew(Button);
  1913. load->set_accessibility_name(TTRC("Load"));
  1914. load->set_theme_type_variation(SceneStringName(FlatButton));
  1915. hbc_actions->add_child(load);
  1916. load_sheet = memnew(Button);
  1917. load_sheet->set_accessibility_name(TTRC("Load Sheet"));
  1918. load_sheet->set_theme_type_variation(SceneStringName(FlatButton));
  1919. hbc_actions->add_child(load_sheet);
  1920. hbc_actions->add_child(memnew(VSeparator));
  1921. copy = memnew(Button);
  1922. copy->set_accessibility_name(TTRC("Copy"));
  1923. copy->set_theme_type_variation(SceneStringName(FlatButton));
  1924. hbc_actions->add_child(copy);
  1925. paste = memnew(Button);
  1926. paste->set_accessibility_name(TTRC("Paste"));
  1927. paste->set_theme_type_variation(SceneStringName(FlatButton));
  1928. hbc_actions->add_child(paste);
  1929. hbc_actions->add_child(memnew(VSeparator));
  1930. empty_before = memnew(Button);
  1931. empty_before->set_accessibility_name(TTRC("Empty Before"));
  1932. empty_before->set_theme_type_variation(SceneStringName(FlatButton));
  1933. hbc_actions->add_child(empty_before);
  1934. empty_after = memnew(Button);
  1935. empty_after->set_accessibility_name(TTRC("Empty After"));
  1936. empty_after->set_theme_type_variation(SceneStringName(FlatButton));
  1937. hbc_actions->add_child(empty_after);
  1938. hbc_actions->add_child(memnew(VSeparator));
  1939. move_up = memnew(Button);
  1940. move_up->set_accessibility_name(TTRC("Move Up"));
  1941. move_up->set_theme_type_variation(SceneStringName(FlatButton));
  1942. hbc_actions->add_child(move_up);
  1943. move_down = memnew(Button);
  1944. move_down->set_accessibility_name(TTRC("Move Down"));
  1945. move_down->set_theme_type_variation(SceneStringName(FlatButton));
  1946. hbc_actions->add_child(move_down);
  1947. delete_frame = memnew(Button);
  1948. delete_frame->set_accessibility_name(TTRC("Delete Frame"));
  1949. delete_frame->set_theme_type_variation(SceneStringName(FlatButton));
  1950. hbc_actions->add_child(delete_frame);
  1951. hbc_actions->add_child(memnew(VSeparator));
  1952. HBoxContainer *hbc_frame_duration = memnew(HBoxContainer);
  1953. hfc->add_child(hbc_frame_duration);
  1954. Label *label = memnew(Label);
  1955. label->set_text(TTRC("Frame Duration:"));
  1956. hbc_frame_duration->add_child(label);
  1957. frame_duration = memnew(SpinBox);
  1958. frame_duration->set_prefix(String::utf8("×"));
  1959. frame_duration->set_min(SPRITE_FRAME_MINIMUM_DURATION); // Avoid zero div.
  1960. frame_duration->set_max(10);
  1961. frame_duration->set_step(0.01);
  1962. frame_duration->set_custom_arrow_step(0.1);
  1963. frame_duration->set_allow_lesser(false);
  1964. frame_duration->set_allow_greater(true);
  1965. frame_duration->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_frame_duration_changed));
  1966. frame_duration->set_accessibility_name(TTRC("Frame Duration:"));
  1967. hbc_frame_duration->add_child(frame_duration);
  1968. // Wide empty separation control. (like BoxContainer::add_spacer())
  1969. Control *c = memnew(Control);
  1970. c->set_mouse_filter(MOUSE_FILTER_PASS);
  1971. c->set_h_size_flags(SIZE_EXPAND_FILL);
  1972. hfc->add_child(c);
  1973. HBoxContainer *hbc_zoom = memnew(HBoxContainer);
  1974. hfc->add_child(hbc_zoom);
  1975. zoom_out = memnew(Button);
  1976. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_out));
  1977. zoom_out->set_flat(true);
  1978. zoom_out->set_tooltip_text(TTRC("Zoom Out"));
  1979. hbc_zoom->add_child(zoom_out);
  1980. zoom_reset = memnew(Button);
  1981. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_reset));
  1982. zoom_reset->set_flat(true);
  1983. zoom_reset->set_tooltip_text(TTRC("Zoom Reset"));
  1984. hbc_zoom->add_child(zoom_reset);
  1985. zoom_in = memnew(Button);
  1986. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_zoom_in));
  1987. zoom_in->set_flat(true);
  1988. zoom_in->set_tooltip_text(TTRC("Zoom In"));
  1989. hbc_zoom->add_child(zoom_in);
  1990. file = memnew(EditorFileDialog);
  1991. file->connect("files_selected", callable_mp(this, &SpriteFramesEditor::_file_load_request).bind(-1));
  1992. add_child(file);
  1993. frame_list = memnew(ItemList);
  1994. frame_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1995. frame_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1996. frame_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  1997. frame_list->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1998. frame_list->set_select_mode(ItemList::SELECT_MULTI);
  1999. frame_list->set_theme_type_variation("ItemListSecondary");
  2000. frame_list->set_max_columns(0);
  2001. frame_list->set_max_text_lines(2);
  2002. SET_DRAG_FORWARDING_GCD(frame_list, SpriteFramesEditor);
  2003. frame_list->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_frame_list_gui_input));
  2004. // HACK: The item_selected signal is emitted before the Frame Duration spinbox loses focus and applies the change.
  2005. frame_list->connect("multi_selected", callable_mp(this, &SpriteFramesEditor::_frame_list_item_selected), CONNECT_DEFERRED);
  2006. sub_vb->add_child(frame_list);
  2007. dialog = memnew(AcceptDialog);
  2008. add_child(dialog);
  2009. load->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_load_pressed));
  2010. load_sheet->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_open_sprite_sheet));
  2011. delete_frame->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_delete_pressed));
  2012. copy->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_copy_pressed));
  2013. paste->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_paste_pressed));
  2014. empty_before->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty_pressed));
  2015. empty_after->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_empty2_pressed));
  2016. move_up->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_up_pressed));
  2017. move_down->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_down_pressed));
  2018. play_bw_from->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation_backwards", TTRC("Play Animation Backwards"), Key::A));
  2019. play_bw->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation_from_end", TTRC("Play Animation Backwards from End"), KeyModifierMask::SHIFT + Key::A));
  2020. stop->set_shortcut(ED_SHORTCUT("sprite_frames/stop_animation", TTRC("Pause/Stop Animation"), Key::S));
  2021. play->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation_from_start", TTRC("Play Animation from Start"), KeyModifierMask::SHIFT + Key::D));
  2022. play_from->set_shortcut(ED_SHORTCUT("sprite_frames/play_animation", TTRC("Play Animation"), Key::D));
  2023. load->set_shortcut_context(frame_list);
  2024. load->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_file", TTRC("Add Frame from File"), KeyModifierMask::CMD_OR_CTRL | Key::O));
  2025. load_sheet->set_shortcut_context(frame_list);
  2026. load_sheet->set_shortcut(ED_SHORTCUT("sprite_frames/load_from_sheet", TTRC("Add Frames from Sprite Sheet"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O));
  2027. delete_frame->set_shortcut_context(frame_list);
  2028. delete_frame->set_shortcut(ED_SHORTCUT("sprite_frames/delete", TTRC("Delete Frame"), Key::KEY_DELETE));
  2029. copy->set_shortcut_context(frame_list);
  2030. copy->set_shortcut(ED_SHORTCUT("sprite_frames/copy", TTRC("Copy Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::C));
  2031. paste->set_shortcut_context(frame_list);
  2032. paste->set_shortcut(ED_SHORTCUT("sprite_frames/paste", TTRC("Paste Frame(s)"), KeyModifierMask::CMD_OR_CTRL | Key::V));
  2033. empty_before->set_shortcut_context(frame_list);
  2034. empty_before->set_shortcut(ED_SHORTCUT("sprite_frames/empty_before", TTRC("Insert Empty (Before Selected)"), KeyModifierMask::ALT | Key::LEFT));
  2035. empty_after->set_shortcut_context(frame_list);
  2036. empty_after->set_shortcut(ED_SHORTCUT("sprite_frames/empty_after", TTRC("Insert Empty (After Selected)"), KeyModifierMask::ALT | Key::RIGHT));
  2037. move_up->set_shortcut_context(frame_list);
  2038. move_up->set_shortcut(ED_SHORTCUT("sprite_frames/move_left", TTRC("Move Frame Left"), KeyModifierMask::CMD_OR_CTRL | Key::LEFT));
  2039. move_down->set_shortcut_context(frame_list);
  2040. move_down->set_shortcut(ED_SHORTCUT("sprite_frames/move_right", TTRC("Move Frame Right"), KeyModifierMask::CMD_OR_CTRL | Key::RIGHT));
  2041. zoom_out->set_shortcut_context(frame_list);
  2042. zoom_out->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_out", TTRC("Zoom Out"),
  2043. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) }));
  2044. zoom_in->set_shortcut_context(frame_list);
  2045. zoom_in->set_shortcut(ED_SHORTCUT_ARRAY("sprite_frames/zoom_in", TTRC("Zoom In"),
  2046. { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) }));
  2047. loading_scene = false;
  2048. updating = false;
  2049. edited_anim = SceneStringName(default_);
  2050. delete_dialog = memnew(ConfirmationDialog);
  2051. add_child(delete_dialog);
  2052. delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_animation_remove_confirmed));
  2053. split_sheet_dialog = memnew(ConfirmationDialog);
  2054. split_sheet_dialog->set_flag(Window::FLAG_MAXIMIZE_DISABLED, false);
  2055. add_child(split_sheet_dialog);
  2056. split_sheet_dialog->set_title(TTRC("Select Frames"));
  2057. split_sheet_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SpriteFramesEditor::_sheet_add_frames));
  2058. HBoxContainer *split_sheet_hb = memnew(HBoxContainer);
  2059. split_sheet_dialog->add_child(split_sheet_hb);
  2060. split_sheet_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2061. split_sheet_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  2062. VBoxContainer *split_sheet_vb = memnew(VBoxContainer);
  2063. split_sheet_hb->add_child(split_sheet_vb);
  2064. split_sheet_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2065. split_sheet_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2066. HBoxContainer *split_sheet_menu_hb = memnew(HBoxContainer);
  2067. split_sheet_menu_hb->add_child(memnew(Label(TTRC("Frame Order"))));
  2068. split_sheet_order = memnew(OptionButton);
  2069. split_sheet_order->add_item(TTRC("As Selected"), FRAME_ORDER_SELECTION);
  2070. split_sheet_order->add_separator(TTRC("By Row"));
  2071. split_sheet_order->add_item(TTRC("Left to Right, Top to Bottom"), FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM);
  2072. split_sheet_order->add_item(TTRC("Left to Right, Bottom to Top"), FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP);
  2073. split_sheet_order->add_item(TTRC("Right to Left, Top to Bottom"), FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM);
  2074. split_sheet_order->add_item(TTRC("Right to Left, Bottom to Top"), FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP);
  2075. split_sheet_order->add_separator(TTRC("By Column"));
  2076. split_sheet_order->add_item(TTRC("Top to Bottom, Left to Right"), FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT);
  2077. split_sheet_order->add_item(TTRC("Top to Bottom, Right to Left"), FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT);
  2078. split_sheet_order->add_item(TTRC("Bottom to Top, Left to Right"), FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT);
  2079. split_sheet_order->add_item(TTRC("Bottom to Top, Right to Left"), FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT);
  2080. split_sheet_order->connect(SceneStringName(item_selected), callable_mp(this, &SpriteFramesEditor::_sheet_order_selected));
  2081. split_sheet_menu_hb->add_child(split_sheet_order);
  2082. Button *select_all = memnew(Button);
  2083. select_all->set_text(TTRC("Select All"));
  2084. select_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_select_all_frames));
  2085. split_sheet_menu_hb->add_child(select_all);
  2086. Button *clear_all = memnew(Button);
  2087. clear_all->set_text(TTRC("Select None"));
  2088. clear_all->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_clear_all_frames));
  2089. split_sheet_menu_hb->add_child(clear_all);
  2090. split_sheet_menu_hb->add_spacer();
  2091. toggle_settings_button = memnew(Button);
  2092. toggle_settings_button->set_h_size_flags(SIZE_SHRINK_END);
  2093. toggle_settings_button->set_theme_type_variation(SceneStringName(FlatButton));
  2094. toggle_settings_button->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_toggle_show_settings));
  2095. toggle_settings_button->set_tooltip_text(TTRC("Toggle Settings Panel"));
  2096. split_sheet_menu_hb->add_child(toggle_settings_button);
  2097. split_sheet_vb->add_child(split_sheet_menu_hb);
  2098. PanelContainer *split_sheet_panel = memnew(PanelContainer);
  2099. split_sheet_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  2100. split_sheet_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  2101. split_sheet_vb->add_child(split_sheet_panel);
  2102. split_sheet_preview = memnew(TextureRect);
  2103. split_sheet_preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  2104. split_sheet_preview->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  2105. split_sheet_preview->set_mouse_filter(MOUSE_FILTER_PASS);
  2106. split_sheet_preview->connect(SceneStringName(draw), callable_mp(this, &SpriteFramesEditor::_sheet_preview_draw));
  2107. split_sheet_preview->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_preview_input));
  2108. split_sheet_scroll = memnew(ScrollContainer);
  2109. split_sheet_scroll->connect(SceneStringName(gui_input), callable_mp(this, &SpriteFramesEditor::_sheet_scroll_input));
  2110. split_sheet_panel->add_child(split_sheet_scroll);
  2111. CenterContainer *cc = memnew(CenterContainer);
  2112. cc->add_child(split_sheet_preview);
  2113. cc->set_h_size_flags(SIZE_EXPAND_FILL);
  2114. cc->set_v_size_flags(SIZE_EXPAND_FILL);
  2115. split_sheet_scroll->add_child(cc);
  2116. MarginContainer *split_sheet_zoom_margin = memnew(MarginContainer);
  2117. split_sheet_panel->add_child(split_sheet_zoom_margin);
  2118. split_sheet_zoom_margin->set_h_size_flags(0);
  2119. split_sheet_zoom_margin->set_v_size_flags(0);
  2120. split_sheet_zoom_margin->add_theme_constant_override("margin_top", 5);
  2121. split_sheet_zoom_margin->add_theme_constant_override("margin_left", 5);
  2122. HBoxContainer *split_sheet_zoom_hb = memnew(HBoxContainer);
  2123. split_sheet_zoom_margin->add_child(split_sheet_zoom_hb);
  2124. split_sheet_zoom_out = memnew(Button);
  2125. split_sheet_zoom_out->set_theme_type_variation(SceneStringName(FlatButton));
  2126. split_sheet_zoom_out->set_focus_mode(FOCUS_ACCESSIBILITY);
  2127. split_sheet_zoom_out->set_tooltip_text(TTRC("Zoom Out"));
  2128. split_sheet_zoom_out->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_out));
  2129. split_sheet_zoom_hb->add_child(split_sheet_zoom_out);
  2130. split_sheet_zoom_reset = memnew(Button);
  2131. split_sheet_zoom_reset->set_theme_type_variation(SceneStringName(FlatButton));
  2132. split_sheet_zoom_reset->set_focus_mode(FOCUS_ACCESSIBILITY);
  2133. split_sheet_zoom_reset->set_tooltip_text(TTRC("Zoom Reset"));
  2134. split_sheet_zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_reset));
  2135. split_sheet_zoom_hb->add_child(split_sheet_zoom_reset);
  2136. split_sheet_zoom_in = memnew(Button);
  2137. split_sheet_zoom_in->set_theme_type_variation(SceneStringName(FlatButton));
  2138. split_sheet_zoom_in->set_focus_mode(FOCUS_ACCESSIBILITY);
  2139. split_sheet_zoom_in->set_tooltip_text(TTRC("Zoom In"));
  2140. split_sheet_zoom_in->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_in));
  2141. split_sheet_zoom_hb->add_child(split_sheet_zoom_in);
  2142. split_sheet_zoom_fit = memnew(Button);
  2143. split_sheet_zoom_fit->set_theme_type_variation(SceneStringName(FlatButton));
  2144. split_sheet_zoom_fit->set_focus_mode(FOCUS_ACCESSIBILITY);
  2145. split_sheet_zoom_fit->set_tooltip_text(TTRC("Zoom to Fit"));
  2146. split_sheet_zoom_fit->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_sheet_zoom_fit));
  2147. split_sheet_zoom_hb->add_child(split_sheet_zoom_fit);
  2148. split_sheet_settings_vb = memnew(VBoxContainer);
  2149. split_sheet_settings_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2150. HBoxContainer *split_sheet_h_hb = memnew(HBoxContainer);
  2151. split_sheet_h_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2152. Label *split_sheet_h_label = memnew(Label(TTRC("Horizontal")));
  2153. split_sheet_h_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2154. split_sheet_h_hb->add_child(split_sheet_h_label);
  2155. split_sheet_h = memnew(SpinBox);
  2156. split_sheet_h->set_h_size_flags(SIZE_EXPAND_FILL);
  2157. split_sheet_h->set_min(1);
  2158. split_sheet_h->set_max(128);
  2159. split_sheet_h->set_step(1);
  2160. split_sheet_h->set_select_all_on_focus(true);
  2161. split_sheet_h->set_accessibility_name(TTRC("Horizontal"));
  2162. split_sheet_h_hb->add_child(split_sheet_h);
  2163. split_sheet_h->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  2164. split_sheet_settings_vb->add_child(split_sheet_h_hb);
  2165. HBoxContainer *split_sheet_v_hb = memnew(HBoxContainer);
  2166. split_sheet_v_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2167. Label *split_sheet_v_label = memnew(Label(TTRC("Vertical")));
  2168. split_sheet_v_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2169. split_sheet_v_hb->add_child(split_sheet_v_label);
  2170. split_sheet_v = memnew(SpinBox);
  2171. split_sheet_v->set_h_size_flags(SIZE_EXPAND_FILL);
  2172. split_sheet_v->set_min(1);
  2173. split_sheet_v->set_max(128);
  2174. split_sheet_v->set_step(1);
  2175. split_sheet_v->set_select_all_on_focus(true);
  2176. split_sheet_v->set_accessibility_name(TTRC("Vertical"));
  2177. split_sheet_v_hb->add_child(split_sheet_v);
  2178. split_sheet_v->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_FRAME_COUNT));
  2179. split_sheet_settings_vb->add_child(split_sheet_v_hb);
  2180. HBoxContainer *split_sheet_size_hb = memnew(HBoxContainer);
  2181. split_sheet_size_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2182. Label *split_sheet_size_label = memnew(Label(TTRC("Size")));
  2183. split_sheet_size_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2184. split_sheet_size_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  2185. split_sheet_size_hb->add_child(split_sheet_size_label);
  2186. VBoxContainer *split_sheet_size_vb = memnew(VBoxContainer);
  2187. split_sheet_size_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2188. split_sheet_size_x = memnew(SpinBox);
  2189. split_sheet_size_x->set_h_size_flags(SIZE_EXPAND_FILL);
  2190. split_sheet_size_x->set_min(1);
  2191. split_sheet_size_x->set_step(1);
  2192. split_sheet_size_x->set_suffix("px");
  2193. split_sheet_size_x->set_select_all_on_focus(true);
  2194. split_sheet_size_x->set_accessibility_name(TTRC("X Size"));
  2195. split_sheet_size_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  2196. split_sheet_size_vb->add_child(split_sheet_size_x);
  2197. split_sheet_size_y = memnew(SpinBox);
  2198. split_sheet_size_y->set_h_size_flags(SIZE_EXPAND_FILL);
  2199. split_sheet_size_y->set_min(1);
  2200. split_sheet_size_y->set_step(1);
  2201. split_sheet_size_y->set_suffix("px");
  2202. split_sheet_size_y->set_select_all_on_focus(true);
  2203. split_sheet_size_y->set_accessibility_name(TTRC("Y Size"));
  2204. split_sheet_size_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_SIZE));
  2205. split_sheet_size_vb->add_child(split_sheet_size_y);
  2206. split_sheet_size_hb->add_child(split_sheet_size_vb);
  2207. split_sheet_settings_vb->add_child(split_sheet_size_hb);
  2208. HBoxContainer *split_sheet_sep_hb = memnew(HBoxContainer);
  2209. split_sheet_sep_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2210. Label *split_sheet_sep_label = memnew(Label(TTRC("Separation")));
  2211. split_sheet_sep_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2212. split_sheet_sep_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  2213. split_sheet_sep_hb->add_child(split_sheet_sep_label);
  2214. VBoxContainer *split_sheet_sep_vb = memnew(VBoxContainer);
  2215. split_sheet_sep_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2216. split_sheet_sep_x = memnew(SpinBox);
  2217. split_sheet_sep_x->set_min(0);
  2218. split_sheet_sep_x->set_step(1);
  2219. split_sheet_sep_x->set_suffix("px");
  2220. split_sheet_sep_x->set_select_all_on_focus(true);
  2221. split_sheet_sep_x->set_accessibility_name(TTRC("X Separation"));
  2222. split_sheet_sep_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2223. split_sheet_sep_vb->add_child(split_sheet_sep_x);
  2224. split_sheet_sep_y = memnew(SpinBox);
  2225. split_sheet_sep_y->set_min(0);
  2226. split_sheet_sep_y->set_step(1);
  2227. split_sheet_sep_y->set_suffix("px");
  2228. split_sheet_sep_y->set_select_all_on_focus(true);
  2229. split_sheet_sep_y->set_accessibility_name(TTRC("Y Separation"));
  2230. split_sheet_sep_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2231. split_sheet_sep_vb->add_child(split_sheet_sep_y);
  2232. split_sheet_sep_hb->add_child(split_sheet_sep_vb);
  2233. split_sheet_settings_vb->add_child(split_sheet_sep_hb);
  2234. HBoxContainer *split_sheet_offset_hb = memnew(HBoxContainer);
  2235. split_sheet_offset_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  2236. Label *split_sheet_offset_label = memnew(Label(TTRC("Offset")));
  2237. split_sheet_offset_label->set_h_size_flags(SIZE_EXPAND_FILL);
  2238. split_sheet_offset_label->set_v_size_flags(SIZE_SHRINK_BEGIN);
  2239. split_sheet_offset_hb->add_child(split_sheet_offset_label);
  2240. VBoxContainer *split_sheet_offset_vb = memnew(VBoxContainer);
  2241. split_sheet_offset_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  2242. split_sheet_offset_x = memnew(SpinBox);
  2243. split_sheet_offset_x->set_min(0);
  2244. split_sheet_offset_x->set_step(1);
  2245. split_sheet_offset_x->set_suffix("px");
  2246. split_sheet_offset_x->set_select_all_on_focus(true);
  2247. split_sheet_offset_x->set_accessibility_name(TTRC("X Offset"));
  2248. split_sheet_offset_x->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2249. split_sheet_offset_vb->add_child(split_sheet_offset_x);
  2250. split_sheet_offset_y = memnew(SpinBox);
  2251. split_sheet_offset_y->set_min(0);
  2252. split_sheet_offset_y->set_step(1);
  2253. split_sheet_offset_y->set_suffix("px");
  2254. split_sheet_offset_y->set_select_all_on_focus(true);
  2255. split_sheet_offset_y->set_accessibility_name(TTRC("Y Offset"));
  2256. split_sheet_offset_y->connect(SceneStringName(value_changed), callable_mp(this, &SpriteFramesEditor::_sheet_spin_changed).bind(PARAM_USE_CURRENT));
  2257. split_sheet_offset_vb->add_child(split_sheet_offset_y);
  2258. split_sheet_offset_hb->add_child(split_sheet_offset_vb);
  2259. split_sheet_settings_vb->add_child(split_sheet_offset_hb);
  2260. Button *auto_slice = memnew(Button);
  2261. auto_slice->set_text(TTRC("Auto Slice"));
  2262. auto_slice->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_auto_slice_sprite_sheet));
  2263. split_sheet_settings_vb->add_child(auto_slice);
  2264. split_sheet_hb->add_child(split_sheet_settings_vb);
  2265. file_split_sheet = memnew(EditorFileDialog);
  2266. file_split_sheet->set_title(TTRC("Create Frames from Sprite Sheet"));
  2267. file_split_sheet->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  2268. add_child(file_split_sheet);
  2269. // Deferred so file dialog is hidden when sprite sheet dialog popups. Otherwise, after allowing
  2270. // sprite sheet dialog to be maximized, it would complain about already having exclusive child window.
  2271. file_split_sheet->connect("file_selected", callable_mp(this, &SpriteFramesEditor::_prepare_sprite_sheet), CONNECT_DEFERRED);
  2272. // Config scale.
  2273. scale_ratio = 1.2f;
  2274. thumbnail_default_size = 96 * MAX(1, EDSCALE);
  2275. thumbnail_zoom = MAX(1.0f, EDSCALE);
  2276. max_thumbnail_zoom = 8.0f * MAX(1.0f, EDSCALE);
  2277. min_thumbnail_zoom = 0.1f * MAX(1.0f, EDSCALE);
  2278. // Default the zoom to match the editor scale, but don't dezoom on editor scales below 100% to prevent pixel art from looking bad.
  2279. sheet_zoom = MAX(1.0f, EDSCALE);
  2280. max_sheet_zoom = 128.0f * MAX(1.0f, EDSCALE);
  2281. min_sheet_zoom = 0.01f * MAX(1.0f, EDSCALE);
  2282. _zoom_reset();
  2283. // Ensure the anim search box is wide enough by default.
  2284. // Not by setting its minimum size so it can still be shrunk if desired.
  2285. main_split->set_split_offset(56 * EDSCALE);
  2286. }
  2287. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  2288. Ref<SpriteFrames> s;
  2289. AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object);
  2290. if (animated_sprite) {
  2291. s = animated_sprite->get_sprite_frames();
  2292. } else {
  2293. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  2294. if (animated_sprite_3d) {
  2295. s = animated_sprite_3d->get_sprite_frames();
  2296. } else {
  2297. s = p_object;
  2298. }
  2299. }
  2300. frames_editor->edit(s);
  2301. }
  2302. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  2303. AnimatedSprite2D *animated_sprite_2d = Object::cast_to<AnimatedSprite2D>(p_object);
  2304. if (animated_sprite_2d && *animated_sprite_2d->get_sprite_frames()) {
  2305. return true;
  2306. }
  2307. AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object);
  2308. if (animated_sprite_3d && *animated_sprite_3d->get_sprite_frames()) {
  2309. return true;
  2310. }
  2311. SpriteFrames *frames = Object::cast_to<SpriteFrames>(p_object);
  2312. if (frames && (frames_editor->get_sprite_frames().is_null() || frames_editor->get_sprite_frames() == frames)) {
  2313. return true;
  2314. }
  2315. return false;
  2316. }
  2317. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  2318. if (p_visible) {
  2319. frames_editor->make_visible();
  2320. } else {
  2321. frames_editor->close();
  2322. }
  2323. }
  2324. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin() {
  2325. frames_editor = memnew(SpriteFramesEditor);
  2326. frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2327. EditorDockManager::get_singleton()->add_dock(frames_editor);
  2328. frames_editor->close();
  2329. }
  2330. Ref<ClipboardAnimation> ClipboardAnimation::from_sprite_frames(const Ref<SpriteFrames> &p_frames, const String &p_anim) {
  2331. Ref<ClipboardAnimation> clipboard_anim;
  2332. clipboard_anim.instantiate();
  2333. clipboard_anim->name = p_anim;
  2334. clipboard_anim->speed = p_frames->get_animation_speed(p_anim);
  2335. clipboard_anim->loop = p_frames->get_animation_loop(p_anim);
  2336. int frame_count = p_frames->get_frame_count(p_anim);
  2337. for (int i = 0; i < frame_count; ++i) {
  2338. ClipboardSpriteFrames::Frame frame;
  2339. frame.texture = p_frames->get_frame_texture(p_anim, i);
  2340. frame.duration = p_frames->get_frame_duration(p_anim, i);
  2341. clipboard_anim->frames.push_back(frame);
  2342. }
  2343. return clipboard_anim;
  2344. }