sprite_frames_editor_plugin.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/io/resource_loader.h"
  32. #include "core/project_settings.h"
  33. #include "editor/editor_settings.h"
  34. #include "scene/3d/sprite_3d.h"
  35. void SpriteFramesEditor::_gui_input(Ref<InputEvent> p_event) {
  36. }
  37. void SpriteFramesEditor::_notification(int p_what) {
  38. if (p_what == NOTIFICATION_ENTER_TREE) {
  39. load->set_icon(get_icon("Load", "EditorIcons"));
  40. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  41. paste->set_icon(get_icon("ActionPaste", "EditorIcons"));
  42. empty->set_icon(get_icon("InsertBefore", "EditorIcons"));
  43. empty2->set_icon(get_icon("InsertAfter", "EditorIcons"));
  44. move_up->set_icon(get_icon("MoveLeft", "EditorIcons"));
  45. move_down->set_icon(get_icon("MoveRight", "EditorIcons"));
  46. _delete->set_icon(get_icon("Remove", "EditorIcons"));
  47. new_anim->set_icon(get_icon("New", "EditorIcons"));
  48. remove_anim->set_icon(get_icon("Remove", "EditorIcons"));
  49. } else if (p_what == NOTIFICATION_READY) {
  50. add_constant_override("autohide", 1); // Fixes the dragger always showing up.
  51. }
  52. }
  53. void SpriteFramesEditor::_file_load_request(const PoolVector<String> &p_path, int p_at_pos) {
  54. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  55. List<Ref<Texture> > resources;
  56. for (int i = 0; i < p_path.size(); i++) {
  57. Ref<Texture> resource;
  58. resource = ResourceLoader::load(p_path[i]);
  59. if (resource.is_null()) {
  60. dialog->set_text(TTR("ERROR: Couldn't load frame resource!"));
  61. dialog->set_title(TTR("Error!"));
  62. //dialog->get_cancel()->set_text("Close");
  63. dialog->get_ok()->set_text(TTR("Close"));
  64. dialog->popup_centered_minsize();
  65. return; ///beh should show an error i guess
  66. }
  67. resources.push_back(resource);
  68. }
  69. if (resources.empty()) {
  70. return;
  71. }
  72. undo_redo->create_action(TTR("Add Frame"));
  73. int fc = frames->get_frame_count(edited_anim);
  74. int count = 0;
  75. for (List<Ref<Texture> >::Element *E = resources.front(); E; E = E->next()) {
  76. undo_redo->add_do_method(frames, "add_frame", edited_anim, E->get(), p_at_pos == -1 ? -1 : p_at_pos + count);
  77. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos);
  78. count++;
  79. }
  80. undo_redo->add_do_method(this, "_update_library");
  81. undo_redo->add_undo_method(this, "_update_library");
  82. undo_redo->commit_action();
  83. }
  84. void SpriteFramesEditor::_load_pressed() {
  85. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  86. loading_scene = false;
  87. file->clear_filters();
  88. List<String> extensions;
  89. ResourceLoader::get_recognized_extensions_for_type("Texture", &extensions);
  90. for (int i = 0; i < extensions.size(); i++)
  91. file->add_filter("*." + extensions[i]);
  92. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  93. file->popup_centered_ratio();
  94. }
  95. void SpriteFramesEditor::_paste_pressed() {
  96. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  97. Ref<Texture> r = EditorSettings::get_singleton()->get_resource_clipboard();
  98. if (!r.is_valid()) {
  99. dialog->set_text(TTR("Resource clipboard is empty or not a texture!"));
  100. dialog->set_title(TTR("Error!"));
  101. //dialog->get_cancel()->set_text("Close");
  102. dialog->get_ok()->set_text(TTR("Close"));
  103. dialog->popup_centered_minsize();
  104. return; ///beh should show an error i guess
  105. }
  106. undo_redo->create_action(TTR("Paste Frame"));
  107. undo_redo->add_do_method(frames, "add_frame", edited_anim, r);
  108. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, frames->get_frame_count(edited_anim));
  109. undo_redo->add_do_method(this, "_update_library");
  110. undo_redo->add_undo_method(this, "_update_library");
  111. undo_redo->commit_action();
  112. }
  113. void SpriteFramesEditor::_copy_pressed() {
  114. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  115. if (tree->get_current() < 0)
  116. return;
  117. Ref<Texture> r = frames->get_frame(edited_anim, tree->get_current());
  118. if (!r.is_valid()) {
  119. return;
  120. }
  121. EditorSettings::get_singleton()->set_resource_clipboard(r);
  122. }
  123. void SpriteFramesEditor::_empty_pressed() {
  124. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  125. int from = -1;
  126. if (tree->get_current() >= 0) {
  127. from = tree->get_current();
  128. sel = from;
  129. } else {
  130. from = frames->get_frame_count(edited_anim);
  131. }
  132. Ref<Texture> r;
  133. undo_redo->create_action(TTR("Add Empty"));
  134. undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from);
  135. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from);
  136. undo_redo->add_do_method(this, "_update_library");
  137. undo_redo->add_undo_method(this, "_update_library");
  138. undo_redo->commit_action();
  139. }
  140. void SpriteFramesEditor::_empty2_pressed() {
  141. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  142. int from = -1;
  143. if (tree->get_current() >= 0) {
  144. from = tree->get_current();
  145. sel = from;
  146. } else {
  147. from = frames->get_frame_count(edited_anim);
  148. }
  149. Ref<Texture> r;
  150. undo_redo->create_action(TTR("Add Empty"));
  151. undo_redo->add_do_method(frames, "add_frame", edited_anim, r, from + 1);
  152. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, from + 1);
  153. undo_redo->add_do_method(this, "_update_library");
  154. undo_redo->add_undo_method(this, "_update_library");
  155. undo_redo->commit_action();
  156. }
  157. void SpriteFramesEditor::_up_pressed() {
  158. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  159. if (tree->get_current() < 0)
  160. return;
  161. int to_move = tree->get_current();
  162. if (to_move < 1)
  163. return;
  164. sel = to_move;
  165. sel -= 1;
  166. Ref<Texture> r = frames->get_frame(edited_anim, to_move);
  167. undo_redo->create_action(TTR("Delete Resource"));
  168. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move - 1));
  169. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move));
  170. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
  171. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move - 1, frames->get_frame(edited_anim, to_move - 1));
  172. undo_redo->add_do_method(this, "_update_library");
  173. undo_redo->add_undo_method(this, "_update_library");
  174. undo_redo->commit_action();
  175. }
  176. void SpriteFramesEditor::_down_pressed() {
  177. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  178. if (tree->get_current() < 0)
  179. return;
  180. int to_move = tree->get_current();
  181. if (to_move < 0 || to_move >= frames->get_frame_count(edited_anim) - 1)
  182. return;
  183. sel = to_move;
  184. sel += 1;
  185. Ref<Texture> r = frames->get_frame(edited_anim, to_move);
  186. undo_redo->create_action(TTR("Delete Resource"));
  187. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move + 1));
  188. undo_redo->add_do_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move));
  189. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move, frames->get_frame(edited_anim, to_move));
  190. undo_redo->add_undo_method(frames, "set_frame", edited_anim, to_move + 1, frames->get_frame(edited_anim, to_move + 1));
  191. undo_redo->add_do_method(this, "_update_library");
  192. undo_redo->add_undo_method(this, "_update_library");
  193. undo_redo->commit_action();
  194. }
  195. void SpriteFramesEditor::_delete_pressed() {
  196. ERR_FAIL_COND(!frames->has_animation(edited_anim));
  197. if (tree->get_current() < 0)
  198. return;
  199. int to_delete = tree->get_current();
  200. if (to_delete < 0 || to_delete >= frames->get_frame_count(edited_anim)) {
  201. return;
  202. }
  203. undo_redo->create_action(TTR("Delete Resource"));
  204. undo_redo->add_do_method(frames, "remove_frame", edited_anim, to_delete);
  205. undo_redo->add_undo_method(frames, "add_frame", edited_anim, frames->get_frame(edited_anim, to_delete), to_delete);
  206. undo_redo->add_do_method(this, "_update_library");
  207. undo_redo->add_undo_method(this, "_update_library");
  208. undo_redo->commit_action();
  209. }
  210. void SpriteFramesEditor::_animation_select() {
  211. if (updating)
  212. return;
  213. TreeItem *selected = animations->get_selected();
  214. ERR_FAIL_COND(!selected);
  215. edited_anim = selected->get_text(0);
  216. _update_library(true);
  217. }
  218. static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) {
  219. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  220. if (!edited)
  221. return;
  222. if (p_node != edited && p_node->get_owner() != edited)
  223. return;
  224. {
  225. AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);
  226. if (as && as->get_sprite_frames() == p_sfames) {
  227. r_nodes->push_back(p_node);
  228. }
  229. }
  230. {
  231. AnimatedSprite3D *as = Object::cast_to<AnimatedSprite3D>(p_node);
  232. if (as && as->get_sprite_frames() == p_sfames) {
  233. r_nodes->push_back(p_node);
  234. }
  235. }
  236. for (int i = 0; i < p_node->get_child_count(); i++) {
  237. _find_anim_sprites(p_node->get_child(i), r_nodes, p_sfames);
  238. }
  239. }
  240. void SpriteFramesEditor::_animation_name_edited() {
  241. if (updating)
  242. return;
  243. if (!frames->has_animation(edited_anim))
  244. return;
  245. TreeItem *edited = animations->get_edited();
  246. if (!edited)
  247. return;
  248. String new_name = edited->get_text(0);
  249. if (new_name == String(edited_anim))
  250. return;
  251. new_name = new_name.replace("/", "_").replace(",", " ");
  252. String name = new_name;
  253. int counter = 0;
  254. while (frames->has_animation(name)) {
  255. counter++;
  256. name = new_name + " " + itos(counter);
  257. }
  258. List<Node *> nodes;
  259. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  260. undo_redo->create_action(TTR("Rename Animation"));
  261. undo_redo->add_do_method(frames, "rename_animation", edited_anim, name);
  262. undo_redo->add_undo_method(frames, "rename_animation", name, edited_anim);
  263. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  264. String current = E->get()->call("get_animation");
  265. if (current != edited_anim)
  266. continue;
  267. undo_redo->add_do_method(E->get(), "set_animation", name);
  268. undo_redo->add_undo_method(E->get(), "set_animation", edited_anim);
  269. }
  270. undo_redo->add_do_method(this, "_update_library");
  271. undo_redo->add_undo_method(this, "_update_library");
  272. edited_anim = new_name;
  273. undo_redo->commit_action();
  274. }
  275. void SpriteFramesEditor::_animation_add() {
  276. String name = "New Anim";
  277. int counter = 0;
  278. while (frames->has_animation(name)) {
  279. counter++;
  280. name = "New Anim " + itos(counter);
  281. }
  282. List<Node *> nodes;
  283. _find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
  284. undo_redo->create_action(TTR("Add Animation"));
  285. undo_redo->add_do_method(frames, "add_animation", name);
  286. undo_redo->add_undo_method(frames, "remove_animation", name);
  287. undo_redo->add_do_method(this, "_update_library");
  288. undo_redo->add_undo_method(this, "_update_library");
  289. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  290. String current = E->get()->call("get_animation");
  291. if (frames->has_animation(current))
  292. continue;
  293. undo_redo->add_do_method(E->get(), "set_animation", name);
  294. undo_redo->add_undo_method(E->get(), "set_animation", current);
  295. }
  296. edited_anim = name;
  297. undo_redo->commit_action();
  298. animations->grab_focus();
  299. }
  300. void SpriteFramesEditor::_animation_remove() {
  301. if (updating)
  302. return;
  303. if (!frames->has_animation(edited_anim))
  304. return;
  305. undo_redo->create_action(TTR("Remove Animation"));
  306. undo_redo->add_do_method(frames, "remove_animation", edited_anim);
  307. undo_redo->add_undo_method(frames, "add_animation", edited_anim);
  308. undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  309. undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  310. int fc = frames->get_frame_count(edited_anim);
  311. for (int i = 0; i < fc; i++) {
  312. Ref<Texture> frame = frames->get_frame(edited_anim, i);
  313. undo_redo->add_undo_method(frames, "add_frame", edited_anim, frame);
  314. }
  315. undo_redo->add_do_method(this, "_update_library");
  316. undo_redo->add_undo_method(this, "_update_library");
  317. undo_redo->commit_action();
  318. }
  319. void SpriteFramesEditor::_animation_loop_changed() {
  320. if (updating)
  321. return;
  322. undo_redo->create_action(TTR("Change Animation Loop"));
  323. undo_redo->add_do_method(frames, "set_animation_loop", edited_anim, anim_loop->is_pressed());
  324. undo_redo->add_undo_method(frames, "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
  325. undo_redo->add_do_method(this, "_update_library", true);
  326. undo_redo->add_undo_method(this, "_update_library", true);
  327. undo_redo->commit_action();
  328. }
  329. void SpriteFramesEditor::_animation_fps_changed(double p_value) {
  330. if (updating)
  331. return;
  332. undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS);
  333. undo_redo->add_do_method(frames, "set_animation_speed", edited_anim, p_value);
  334. undo_redo->add_undo_method(frames, "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
  335. undo_redo->add_do_method(this, "_update_library", true);
  336. undo_redo->add_undo_method(this, "_update_library", true);
  337. undo_redo->commit_action();
  338. }
  339. void SpriteFramesEditor::_update_library(bool p_skip_selector) {
  340. updating = true;
  341. if (!p_skip_selector) {
  342. animations->clear();
  343. TreeItem *anim_root = animations->create_item();
  344. List<StringName> anim_names;
  345. frames->get_animation_list(&anim_names);
  346. anim_names.sort_custom<StringName::AlphCompare>();
  347. for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) {
  348. String name = E->get();
  349. TreeItem *it = animations->create_item(anim_root);
  350. it->set_metadata(0, name);
  351. it->set_text(0, name);
  352. it->set_editable(0, true);
  353. if (E->get() == edited_anim) {
  354. it->select(0);
  355. }
  356. }
  357. }
  358. tree->clear();
  359. if (!frames->has_animation(edited_anim)) {
  360. updating = false;
  361. return;
  362. }
  363. if (sel >= frames->get_frame_count(edited_anim))
  364. sel = frames->get_frame_count(edited_anim) - 1;
  365. else if (sel < 0 && frames->get_frame_count(edited_anim))
  366. sel = 0;
  367. for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
  368. String name;
  369. Ref<Texture> icon;
  370. if (frames->get_frame(edited_anim, i).is_null()) {
  371. name = itos(i) + ": " + TTR("(empty)");
  372. } else {
  373. name = itos(i) + ": " + frames->get_frame(edited_anim, i)->get_name();
  374. icon = frames->get_frame(edited_anim, i);
  375. }
  376. tree->add_item(name, icon);
  377. if (frames->get_frame(edited_anim, i).is_valid())
  378. tree->set_item_tooltip(tree->get_item_count() - 1, frames->get_frame(edited_anim, i)->get_path());
  379. if (sel == i)
  380. tree->select(tree->get_item_count() - 1);
  381. }
  382. anim_speed->set_value(frames->get_animation_speed(edited_anim));
  383. anim_loop->set_pressed(frames->get_animation_loop(edited_anim));
  384. updating = false;
  385. //player->add_resource("default",resource);
  386. }
  387. void SpriteFramesEditor::edit(SpriteFrames *p_frames) {
  388. if (frames == p_frames)
  389. return;
  390. frames = p_frames;
  391. if (p_frames) {
  392. if (!p_frames->has_animation(edited_anim)) {
  393. List<StringName> anim_names;
  394. frames->get_animation_list(&anim_names);
  395. anim_names.sort_custom<StringName::AlphCompare>();
  396. if (anim_names.size()) {
  397. edited_anim = anim_names.front()->get();
  398. } else {
  399. edited_anim = StringName();
  400. }
  401. }
  402. _update_library();
  403. } else {
  404. hide();
  405. }
  406. }
  407. Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  408. if (!frames->has_animation(edited_anim))
  409. return false;
  410. int idx = tree->get_item_at_position(p_point, true);
  411. if (idx < 0 || idx >= frames->get_frame_count(edited_anim))
  412. return Variant();
  413. RES frame = frames->get_frame(edited_anim, idx);
  414. if (frame.is_null())
  415. return Variant();
  416. return EditorNode::get_singleton()->drag_resource(frame, p_from);
  417. }
  418. bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  419. Dictionary d = p_data;
  420. if (!d.has("type"))
  421. return false;
  422. if (d.has("from") && (Object *)(d["from"]) == tree)
  423. return false;
  424. if (String(d["type"]) == "resource" && d.has("resource")) {
  425. RES r = d["resource"];
  426. Ref<Texture> texture = r;
  427. if (texture.is_valid()) {
  428. return true;
  429. }
  430. }
  431. if (String(d["type"]) == "files") {
  432. Vector<String> files = d["files"];
  433. if (files.size() == 0)
  434. return false;
  435. for (int i = 0; i < files.size(); i++) {
  436. String file = files[i];
  437. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  438. if (!ClassDB::is_parent_class(ftype, "Texture")) {
  439. return false;
  440. }
  441. }
  442. return true;
  443. }
  444. return false;
  445. }
  446. void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  447. if (!can_drop_data_fw(p_point, p_data, p_from))
  448. return;
  449. Dictionary d = p_data;
  450. if (!d.has("type"))
  451. return;
  452. int at_pos = tree->get_item_at_position(p_point, true);
  453. if (String(d["type"]) == "resource" && d.has("resource")) {
  454. RES r = d["resource"];
  455. Ref<Texture> texture = r;
  456. if (texture.is_valid()) {
  457. undo_redo->create_action(TTR("Add Frame"));
  458. undo_redo->add_do_method(frames, "add_frame", edited_anim, texture, at_pos == -1 ? -1 : at_pos);
  459. undo_redo->add_undo_method(frames, "remove_frame", edited_anim, at_pos == -1 ? frames->get_frame_count(edited_anim) : at_pos);
  460. undo_redo->add_do_method(this, "_update_library");
  461. undo_redo->add_undo_method(this, "_update_library");
  462. undo_redo->commit_action();
  463. }
  464. }
  465. if (String(d["type"]) == "files") {
  466. PoolVector<String> files = d["files"];
  467. _file_load_request(files, at_pos);
  468. }
  469. }
  470. void SpriteFramesEditor::_bind_methods() {
  471. ClassDB::bind_method(D_METHOD("_gui_input"), &SpriteFramesEditor::_gui_input);
  472. ClassDB::bind_method(D_METHOD("_load_pressed"), &SpriteFramesEditor::_load_pressed);
  473. ClassDB::bind_method(D_METHOD("_empty_pressed"), &SpriteFramesEditor::_empty_pressed);
  474. ClassDB::bind_method(D_METHOD("_empty2_pressed"), &SpriteFramesEditor::_empty2_pressed);
  475. ClassDB::bind_method(D_METHOD("_delete_pressed"), &SpriteFramesEditor::_delete_pressed);
  476. ClassDB::bind_method(D_METHOD("_copy_pressed"), &SpriteFramesEditor::_copy_pressed);
  477. ClassDB::bind_method(D_METHOD("_paste_pressed"), &SpriteFramesEditor::_paste_pressed);
  478. ClassDB::bind_method(D_METHOD("_file_load_request", "files", "at_position"), &SpriteFramesEditor::_file_load_request, DEFVAL(-1));
  479. ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false));
  480. ClassDB::bind_method(D_METHOD("_up_pressed"), &SpriteFramesEditor::_up_pressed);
  481. ClassDB::bind_method(D_METHOD("_down_pressed"), &SpriteFramesEditor::_down_pressed);
  482. ClassDB::bind_method(D_METHOD("_animation_select"), &SpriteFramesEditor::_animation_select);
  483. ClassDB::bind_method(D_METHOD("_animation_name_edited"), &SpriteFramesEditor::_animation_name_edited);
  484. ClassDB::bind_method(D_METHOD("_animation_add"), &SpriteFramesEditor::_animation_add);
  485. ClassDB::bind_method(D_METHOD("_animation_remove"), &SpriteFramesEditor::_animation_remove);
  486. ClassDB::bind_method(D_METHOD("_animation_loop_changed"), &SpriteFramesEditor::_animation_loop_changed);
  487. ClassDB::bind_method(D_METHOD("_animation_fps_changed"), &SpriteFramesEditor::_animation_fps_changed);
  488. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SpriteFramesEditor::get_drag_data_fw);
  489. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpriteFramesEditor::can_drop_data_fw);
  490. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpriteFramesEditor::drop_data_fw);
  491. }
  492. SpriteFramesEditor::SpriteFramesEditor() {
  493. VBoxContainer *vbc_animlist = memnew(VBoxContainer);
  494. add_child(vbc_animlist);
  495. vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
  496. VBoxContainer *sub_vb = memnew(VBoxContainer);
  497. vbc_animlist->add_margin_child(TTR("Animations:"), sub_vb, true);
  498. sub_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  499. HBoxContainer *hbc_animlist = memnew(HBoxContainer);
  500. sub_vb->add_child(hbc_animlist);
  501. new_anim = memnew(ToolButton);
  502. new_anim->set_tooltip(TTR("New Animation"));
  503. hbc_animlist->add_child(new_anim);
  504. new_anim->set_h_size_flags(SIZE_EXPAND_FILL);
  505. new_anim->connect("pressed", this, "_animation_add");
  506. remove_anim = memnew(ToolButton);
  507. remove_anim->set_tooltip(TTR("Remove Animation"));
  508. hbc_animlist->add_child(remove_anim);
  509. remove_anim->connect("pressed", this, "_animation_remove");
  510. animations = memnew(Tree);
  511. sub_vb->add_child(animations);
  512. animations->set_v_size_flags(SIZE_EXPAND_FILL);
  513. animations->set_hide_root(true);
  514. animations->connect("cell_selected", this, "_animation_select");
  515. animations->connect("item_edited", this, "_animation_name_edited");
  516. animations->set_allow_reselect(true);
  517. anim_speed = memnew(SpinBox);
  518. vbc_animlist->add_margin_child(TTR("Speed (FPS):"), anim_speed);
  519. anim_speed->set_min(0);
  520. anim_speed->set_max(100);
  521. anim_speed->set_step(0.01);
  522. anim_speed->connect("value_changed", this, "_animation_fps_changed");
  523. anim_loop = memnew(CheckButton);
  524. anim_loop->set_text(TTR("Loop"));
  525. vbc_animlist->add_child(anim_loop);
  526. anim_loop->connect("pressed", this, "_animation_loop_changed");
  527. VBoxContainer *vbc = memnew(VBoxContainer);
  528. add_child(vbc);
  529. vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  530. sub_vb = memnew(VBoxContainer);
  531. vbc->add_margin_child(TTR("Animation Frames:"), sub_vb, true);
  532. HBoxContainer *hbc = memnew(HBoxContainer);
  533. sub_vb->add_child(hbc);
  534. load = memnew(ToolButton);
  535. load->set_tooltip(TTR("Load Resource"));
  536. hbc->add_child(load);
  537. copy = memnew(ToolButton);
  538. copy->set_tooltip(TTR("Copy"));
  539. hbc->add_child(copy);
  540. paste = memnew(ToolButton);
  541. paste->set_tooltip(TTR("Paste"));
  542. hbc->add_child(paste);
  543. empty = memnew(ToolButton);
  544. empty->set_tooltip(TTR("Insert Empty (Before)"));
  545. hbc->add_child(empty);
  546. empty2 = memnew(ToolButton);
  547. empty2->set_tooltip(TTR("Insert Empty (After)"));
  548. hbc->add_child(empty2);
  549. hbc->add_spacer(false);
  550. move_up = memnew(ToolButton);
  551. move_up->set_tooltip(TTR("Move (Before)"));
  552. hbc->add_child(move_up);
  553. move_down = memnew(ToolButton);
  554. move_down->set_tooltip(TTR("Move (After)"));
  555. hbc->add_child(move_down);
  556. _delete = memnew(ToolButton);
  557. _delete->set_tooltip(TTR("Delete"));
  558. hbc->add_child(_delete);
  559. file = memnew(EditorFileDialog);
  560. add_child(file);
  561. tree = memnew(ItemList);
  562. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  563. tree->set_icon_mode(ItemList::ICON_MODE_TOP);
  564. int thumbnail_size = 96;
  565. tree->set_max_columns(0);
  566. tree->set_icon_mode(ItemList::ICON_MODE_TOP);
  567. tree->set_fixed_column_width(thumbnail_size * 3 / 2);
  568. tree->set_max_text_lines(2);
  569. tree->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  570. tree->set_drag_forwarding(this);
  571. sub_vb->add_child(tree);
  572. dialog = memnew(AcceptDialog);
  573. add_child(dialog);
  574. load->connect("pressed", this, "_load_pressed");
  575. _delete->connect("pressed", this, "_delete_pressed");
  576. copy->connect("pressed", this, "_copy_pressed");
  577. paste->connect("pressed", this, "_paste_pressed");
  578. empty->connect("pressed", this, "_empty_pressed");
  579. empty2->connect("pressed", this, "_empty2_pressed");
  580. move_up->connect("pressed", this, "_up_pressed");
  581. move_down->connect("pressed", this, "_down_pressed");
  582. file->connect("files_selected", this, "_file_load_request");
  583. loading_scene = false;
  584. sel = -1;
  585. updating = false;
  586. edited_anim = "default";
  587. }
  588. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  589. frames_editor->set_undo_redo(&get_undo_redo());
  590. SpriteFrames *s;
  591. AnimatedSprite *animated_sprite = Object::cast_to<AnimatedSprite>(p_object);
  592. if (animated_sprite) {
  593. s = *animated_sprite->get_sprite_frames();
  594. } else {
  595. s = Object::cast_to<SpriteFrames>(p_object);
  596. }
  597. frames_editor->edit(s);
  598. }
  599. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  600. AnimatedSprite *animated_sprite = Object::cast_to<AnimatedSprite>(p_object);
  601. if (animated_sprite && *animated_sprite->get_sprite_frames()) {
  602. return true;
  603. } else {
  604. return p_object->is_class("SpriteFrames");
  605. }
  606. }
  607. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  608. if (p_visible) {
  609. button->show();
  610. editor->make_bottom_panel_item_visible(frames_editor);
  611. } else {
  612. button->hide();
  613. if (frames_editor->is_visible_in_tree())
  614. editor->hide_bottom_panel();
  615. }
  616. }
  617. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin(EditorNode *p_node) {
  618. editor = p_node;
  619. frames_editor = memnew(SpriteFramesEditor);
  620. frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  621. button = editor->add_bottom_panel_item(TTR("SpriteFrames"), frames_editor);
  622. button->hide();
  623. }
  624. SpriteFramesEditorPlugin::~SpriteFramesEditorPlugin() {
  625. }