sprite_frames_editor_plugin.cpp 97 KB

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