sprite_frames_editor_plugin.cpp 60 KB

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