sprite_frames_editor_plugin.cpp 85 KB

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