sprite_frames_editor_plugin.cpp 55 KB

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