sprite_frames_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*************************************************************************/
  2. /* sprite_frames_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "sprite_frames_editor_plugin.h"
  30. #include "io/resource_loader.h"
  31. #include "globals.h"
  32. #include "tools/editor/editor_settings.h"
  33. void SpriteFramesEditor::_input_event(InputEvent p_event) {
  34. }
  35. void SpriteFramesEditor::_notification(int p_what) {
  36. if (p_what==NOTIFICATION_FIXED_PROCESS) {
  37. }
  38. if (p_what==NOTIFICATION_ENTER_TREE) {
  39. load->set_icon( get_icon("Folder","EditorIcons") );
  40. _delete->set_icon( get_icon("Del","EditorIcons") );
  41. }
  42. if (p_what==NOTIFICATION_READY) {
  43. // NodePath("/root")->connect("node_removed", this,"_node_removed",Vector<Variant>(),true);
  44. }
  45. if (p_what==NOTIFICATION_DRAW) {
  46. }
  47. }
  48. void SpriteFramesEditor::_file_load_request(const DVector<String>& p_path) {
  49. List< Ref<Texture> > resources;
  50. for(int i=0;i<p_path.size();i++) {
  51. Ref<Texture> resource;
  52. resource = ResourceLoader::load(p_path[i]);
  53. if (resource.is_null()) {
  54. dialog->set_text("ERROR: Couldn't load frame resource!");
  55. dialog->set_title("Error!");
  56. //dialog->get_cancel()->set_text("Close");
  57. dialog->get_ok()->set_text("Close");
  58. dialog->popup_centered_minsize();
  59. return; ///beh should show an error i guess
  60. }
  61. resources.push_back(resource);
  62. }
  63. if (resources.empty()) {
  64. print_line("added frames!");
  65. return;
  66. }
  67. undo_redo->create_action("Add Frame");
  68. int fc=frames->get_frame_count();
  69. for(List< Ref<Texture> >::Element *E=resources.front();E;E=E->next() ) {
  70. undo_redo->add_do_method(frames,"add_frame",E->get());
  71. undo_redo->add_undo_method(frames,"remove_frame",fc++);
  72. }
  73. undo_redo->add_do_method(this,"_update_library");
  74. undo_redo->add_undo_method(this,"_update_library");
  75. undo_redo->commit_action();
  76. print_line("added frames!");
  77. }
  78. void SpriteFramesEditor::_load_pressed() {
  79. loading_scene=false;
  80. file->clear_filters();
  81. List<String> extensions;
  82. ResourceLoader::get_recognized_extensions_for_type("Texture",&extensions);
  83. for(int i=0;i<extensions.size();i++)
  84. file->add_filter("*."+extensions[i]);
  85. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  86. file->popup_centered_ratio();
  87. }
  88. void SpriteFramesEditor::_item_edited() {
  89. #if 0
  90. if (!tree->get_selected())
  91. return;
  92. TreeItem *s = tree->get_selected();
  93. if (tree->get_selected_column()==0) {
  94. // renamed
  95. String old_name=s->get_metadata(0);
  96. String new_name=s->get_text(0);
  97. if (old_name==new_name)
  98. return;
  99. if (new_name=="" || new_name.find("\\")!=-1 || new_name.find("/")!=-1 || frames->has_resource(new_name)) {
  100. s->set_text(0,old_name);
  101. return;
  102. }
  103. RES samp = frames->get_resource(old_name);
  104. undo_redo->create_action("Rename Resource");
  105. undo_redo->add_do_method(frames,"remove_resource",old_name);
  106. undo_redo->add_do_method(frames,"add_resource",new_name,samp);
  107. undo_redo->add_undo_method(frames,"remove_resource",new_name);
  108. undo_redo->add_undo_method(frames,"add_resource",old_name,samp);
  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. #endif
  114. }
  115. void SpriteFramesEditor::_delete_confirm_pressed() {
  116. if (!tree->get_selected())
  117. return;
  118. sel-=1;
  119. if (sel<0 && frames->get_frame_count())
  120. sel=0;
  121. int to_remove = tree->get_selected()->get_metadata(0);
  122. sel=to_remove;
  123. Ref<Texture> r = frames->get_frame(to_remove);
  124. undo_redo->create_action("Delete Resource");
  125. undo_redo->add_do_method(frames,"remove_frame",to_remove);
  126. undo_redo->add_undo_method(frames,"add_frame",r,to_remove);
  127. undo_redo->add_do_method(this,"_update_library");
  128. undo_redo->add_undo_method(this,"_update_library");
  129. undo_redo->commit_action();
  130. }
  131. void SpriteFramesEditor::_paste_pressed() {
  132. Ref<Texture> r=EditorSettings::get_singleton()->get_resource_clipboard();
  133. if (!r.is_valid()) {
  134. dialog->set_text("Resource clipboard is empty or not a texture!");
  135. dialog->set_title("Error!");
  136. //dialog->get_cancel()->set_text("Close");
  137. dialog->get_ok()->set_text("Close");
  138. dialog->popup_centered_minsize();
  139. return; ///beh should show an error i guess
  140. }
  141. undo_redo->create_action("Paste Frame");
  142. undo_redo->add_do_method(frames,"add_frame",r);
  143. undo_redo->add_undo_method(frames,"remove_frame",frames->get_frame_count());
  144. undo_redo->add_do_method(this,"_update_library");
  145. undo_redo->add_undo_method(this,"_update_library");
  146. undo_redo->commit_action();
  147. }
  148. void SpriteFramesEditor::_empty_pressed() {
  149. int from=-1;
  150. if (tree->get_selected()) {
  151. from = tree->get_selected()->get_metadata(0);
  152. sel=from;
  153. } else {
  154. from=frames->get_frame_count();
  155. }
  156. Ref<Texture> r;
  157. undo_redo->create_action("Add Empty");
  158. undo_redo->add_do_method(frames,"add_frame",r,from);
  159. undo_redo->add_undo_method(frames,"remove_frame",from);
  160. undo_redo->add_do_method(this,"_update_library");
  161. undo_redo->add_undo_method(this,"_update_library");
  162. undo_redo->commit_action();
  163. }
  164. void SpriteFramesEditor::_empty2_pressed() {
  165. int from=-1;
  166. if (tree->get_selected()) {
  167. from = tree->get_selected()->get_metadata(0);
  168. sel=from;
  169. } else {
  170. from=frames->get_frame_count();
  171. }
  172. Ref<Texture> r;
  173. undo_redo->create_action("Add Empty");
  174. undo_redo->add_do_method(frames,"add_frame",r,from+1);
  175. undo_redo->add_undo_method(frames,"remove_frame",from+1);
  176. undo_redo->add_do_method(this,"_update_library");
  177. undo_redo->add_undo_method(this,"_update_library");
  178. undo_redo->commit_action();
  179. }
  180. void SpriteFramesEditor::_up_pressed() {
  181. if (!tree->get_selected())
  182. return;
  183. int to_move = tree->get_selected()->get_metadata(0);
  184. if (to_move<1)
  185. return;
  186. sel=to_move;
  187. sel-=1;
  188. Ref<Texture> r = frames->get_frame(to_move);
  189. undo_redo->create_action("Delete Resource");
  190. undo_redo->add_do_method(frames,"set_frame",to_move,frames->get_frame(to_move-1));
  191. undo_redo->add_do_method(frames,"set_frame",to_move-1,frames->get_frame(to_move));
  192. undo_redo->add_undo_method(frames,"set_frame",to_move,frames->get_frame(to_move));
  193. undo_redo->add_undo_method(frames,"set_frame",to_move-1,frames->get_frame(to_move-1));
  194. undo_redo->add_do_method(this,"_update_library");
  195. undo_redo->add_undo_method(this,"_update_library");
  196. undo_redo->commit_action();
  197. }
  198. void SpriteFramesEditor::_down_pressed() {
  199. if (!tree->get_selected())
  200. return;
  201. int to_move = tree->get_selected()->get_metadata(0);
  202. if (to_move<0 || to_move>=frames->get_frame_count()-1)
  203. return;
  204. sel=to_move;
  205. sel+=1;
  206. Ref<Texture> r = frames->get_frame(to_move);
  207. undo_redo->create_action("Delete Resource");
  208. undo_redo->add_do_method(frames,"set_frame",to_move,frames->get_frame(to_move+1));
  209. undo_redo->add_do_method(frames,"set_frame",to_move+1,frames->get_frame(to_move));
  210. undo_redo->add_undo_method(frames,"set_frame",to_move,frames->get_frame(to_move));
  211. undo_redo->add_undo_method(frames,"set_frame",to_move+1,frames->get_frame(to_move+1));
  212. undo_redo->add_do_method(this,"_update_library");
  213. undo_redo->add_undo_method(this,"_update_library");
  214. undo_redo->commit_action();
  215. }
  216. void SpriteFramesEditor::_delete_pressed() {
  217. if (!tree->get_selected())
  218. return;
  219. _delete_confirm_pressed(); //it has undo.. why bother with a dialog..
  220. /*
  221. dialog->set_title("Confirm...");
  222. dialog->set_text("Remove Resource '"+tree->get_selected()->get_text(0)+"' ?");
  223. //dialog->get_cancel()->set_text("Cancel");
  224. //dialog->get_ok()->show();
  225. dialog->get_ok()->set_text("Remove");
  226. dialog->popup_centered(Size2(300,60));*/
  227. }
  228. void SpriteFramesEditor::_update_library() {
  229. tree->clear();
  230. tree->set_hide_root(true);
  231. TreeItem *root = tree->create_item(NULL);
  232. if (sel>=frames->get_frame_count())
  233. sel=frames->get_frame_count()-1;
  234. else if (sel<0 && frames->get_frame_count())
  235. sel=0;
  236. for(int i=0;i<frames->get_frame_count();i++) {
  237. TreeItem *ti = tree->create_item(root);
  238. ti->set_cell_mode(0,TreeItem::CELL_MODE_STRING);
  239. ti->set_selectable(0,true);
  240. if (frames->get_frame(i).is_null()) {
  241. ti->set_text(0,"Frame "+itos(i)+" (empty)");
  242. } else {
  243. ti->set_text(0,"Frame "+itos(i)+" ("+frames->get_frame(i)->get_name()+")");
  244. ti->set_icon(0,frames->get_frame(i));
  245. }
  246. if (frames->get_frame(i).is_valid())
  247. ti->set_tooltip(0,frames->get_frame(i)->get_path());
  248. ti->set_metadata(0,i);
  249. ti->set_icon_max_width(0,96);
  250. if (sel==i)
  251. ti->select(0);
  252. }
  253. //player->add_resource("default",resource);
  254. }
  255. void SpriteFramesEditor::edit(SpriteFrames* p_frames) {
  256. frames=p_frames;
  257. if (p_frames) {
  258. _update_library();
  259. } else {
  260. hide();
  261. //set_fixed_process(false);
  262. }
  263. }
  264. void SpriteFramesEditor::_bind_methods() {
  265. ObjectTypeDB::bind_method(_MD("_input_event"),&SpriteFramesEditor::_input_event);
  266. ObjectTypeDB::bind_method(_MD("_load_pressed"),&SpriteFramesEditor::_load_pressed);
  267. ObjectTypeDB::bind_method(_MD("_empty_pressed"),&SpriteFramesEditor::_empty_pressed);
  268. ObjectTypeDB::bind_method(_MD("_empty2_pressed"),&SpriteFramesEditor::_empty2_pressed);
  269. ObjectTypeDB::bind_method(_MD("_item_edited"),&SpriteFramesEditor::_item_edited);
  270. ObjectTypeDB::bind_method(_MD("_delete_pressed"),&SpriteFramesEditor::_delete_pressed);
  271. ObjectTypeDB::bind_method(_MD("_paste_pressed"),&SpriteFramesEditor::_paste_pressed);
  272. ObjectTypeDB::bind_method(_MD("_delete_confirm_pressed"),&SpriteFramesEditor::_delete_confirm_pressed);
  273. ObjectTypeDB::bind_method(_MD("_file_load_request"),&SpriteFramesEditor::_file_load_request);
  274. ObjectTypeDB::bind_method(_MD("_update_library"),&SpriteFramesEditor::_update_library);
  275. ObjectTypeDB::bind_method(_MD("_up_pressed"),&SpriteFramesEditor::_up_pressed);
  276. ObjectTypeDB::bind_method(_MD("_down_pressed"),&SpriteFramesEditor::_down_pressed);
  277. }
  278. SpriteFramesEditor::SpriteFramesEditor() {
  279. //add_style_override("panel", get_stylebox("panel","Panel"));
  280. VBoxContainer *vbc = memnew( VBoxContainer );
  281. add_child(vbc);
  282. HBoxContainer *hbc = memnew( HBoxContainer );
  283. vbc->add_child(hbc);
  284. load = memnew( Button );
  285. load->set_tooltip("Load Resource");
  286. hbc->add_child(load);
  287. paste = memnew( Button );
  288. paste->set_text("Paste");
  289. hbc->add_child(paste);
  290. empty = memnew( Button );
  291. empty->set_text("Insert Empty (Before)");
  292. hbc->add_child(empty);
  293. empty2 = memnew( Button );
  294. empty2->set_text("Insert Empty (After)");
  295. hbc->add_child(empty2);
  296. move_up = memnew( Button );
  297. move_up->set_text("Up");
  298. hbc->add_child(move_up);
  299. move_down = memnew( Button );
  300. move_down->set_text("Down");
  301. hbc->add_child(move_down);
  302. _delete = memnew( Button );
  303. hbc->add_child(_delete);
  304. file = memnew( EditorFileDialog );
  305. add_child(file);
  306. tree = memnew( Tree );
  307. tree->set_columns(2);
  308. tree->set_column_min_width(0,3);
  309. tree->set_column_min_width(1,1);
  310. tree->set_column_expand(0,true);
  311. tree->set_column_expand(1,true);
  312. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  313. vbc->add_child(tree);
  314. dialog = memnew( AcceptDialog );
  315. add_child( dialog );
  316. load->connect("pressed", this,"_load_pressed");
  317. _delete->connect("pressed", this,"_delete_pressed");
  318. paste->connect("pressed", this,"_paste_pressed");
  319. empty->connect("pressed", this,"_empty_pressed");
  320. empty2->connect("pressed", this,"_empty2_pressed");
  321. move_up->connect("pressed", this,"_up_pressed");
  322. move_down->connect("pressed", this,"_down_pressed");
  323. file->connect("files_selected", this,"_file_load_request");
  324. //dialog->connect("confirmed", this,"_delete_confirm_pressed");
  325. tree->connect("item_edited", this,"_item_edited");
  326. loading_scene=false;
  327. sel=-1;
  328. }
  329. void SpriteFramesEditorPlugin::edit(Object *p_object) {
  330. frames_editor->set_undo_redo(&get_undo_redo());
  331. SpriteFrames * s = p_object->cast_to<SpriteFrames>();
  332. if (!s)
  333. return;
  334. frames_editor->edit(s);
  335. }
  336. bool SpriteFramesEditorPlugin::handles(Object *p_object) const {
  337. return p_object->is_type("SpriteFrames");
  338. }
  339. void SpriteFramesEditorPlugin::make_visible(bool p_visible) {
  340. if (p_visible) {
  341. button->show();
  342. editor->make_bottom_panel_item_visible(frames_editor);
  343. // frames_editor->set_process(true);
  344. } else {
  345. button->hide();
  346. if (frames_editor->is_visible())
  347. editor->hide_bottom_panel();
  348. // frames_editor->set_process(false);
  349. }
  350. }
  351. SpriteFramesEditorPlugin::SpriteFramesEditorPlugin(EditorNode *p_node) {
  352. editor=p_node;
  353. frames_editor = memnew( SpriteFramesEditor );
  354. frames_editor->set_custom_minimum_size(Size2(0,300));
  355. button=editor->add_bottom_panel_item("SpriteFrames",frames_editor);
  356. button->hide();
  357. }
  358. SpriteFramesEditorPlugin::~SpriteFramesEditorPlugin()
  359. {
  360. }