sample_library_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*************************************************************************/
  2. /* sample_library_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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 "sample_library_editor_plugin.h"
  30. #include "io/resource_loader.h"
  31. #include "globals.h"
  32. #include "tools/editor/editor_settings.h"
  33. #include "scene/main/viewport.h"
  34. #include "sample_editor_plugin.h"
  35. void SampleLibraryEditor::_gui_input(InputEvent p_event) {
  36. }
  37. void SampleLibraryEditor::_notification(int p_what) {
  38. if (p_what==NOTIFICATION_PROCESS) {
  39. if (is_playing && !player->is_active()) {
  40. TreeItem *tl=last_sample_playing->cast_to<TreeItem>();
  41. tl->set_button(0,0,get_icon("Play","EditorIcons"));
  42. is_playing = false;
  43. set_process(false);
  44. }
  45. }
  46. if (p_what==NOTIFICATION_ENTER_TREE) {
  47. load->set_icon( get_icon("Folder","EditorIcons") );
  48. load->set_tooltip(TTR("Open Sample File(s)"));
  49. }
  50. if (p_what==NOTIFICATION_READY) {
  51. // NodePath("/root")->connect("node_removed", this,"_node_removed",Vector<Variant>(),true);
  52. }
  53. if (p_what==NOTIFICATION_DRAW) {
  54. }
  55. }
  56. void SampleLibraryEditor::_file_load_request(const PoolVector<String>& p_path) {
  57. for(int i=0;i<p_path.size();i++) {
  58. String path = p_path[i];
  59. Ref<Sample> sample = ResourceLoader::load(path,"Sample");
  60. if (sample.is_null()) {
  61. dialog->set_text(TTR("ERROR: Couldn't load sample!"));
  62. dialog->set_title(TTR("Error!"));
  63. //dialog->get_cancel()->set_text("Close");
  64. dialog->get_ok()->set_text(TTR("Close"));
  65. dialog->popup_centered_minsize();
  66. return; ///beh should show an error i guess
  67. }
  68. String basename = path.get_file().basename();
  69. String name=basename;
  70. int counter=0;
  71. while(sample_library->has_sample(name)) {
  72. counter++;
  73. name=basename+"_"+itos(counter);
  74. }
  75. undo_redo->create_action(TTR("Add Sample"));
  76. undo_redo->add_do_method(sample_library.operator->(),"add_sample",name,sample);
  77. undo_redo->add_undo_method(sample_library.operator->(),"remove_sample",name);
  78. undo_redo->add_do_method(this,"_update_library");
  79. undo_redo->add_undo_method(this,"_update_library");
  80. undo_redo->commit_action();
  81. }
  82. }
  83. void SampleLibraryEditor::_load_pressed() {
  84. file->popup_centered_ratio();
  85. }
  86. void SampleLibraryEditor::_button_pressed(Object *p_item,int p_column, int p_id) {
  87. TreeItem *ti=p_item->cast_to<TreeItem>();
  88. String name = ti->get_text(0);
  89. if (p_column==0) { // Play/Stop
  90. String btn_type;
  91. if(!is_playing) {
  92. is_playing = true;
  93. btn_type = TTR("Stop");
  94. player->play(name,true);
  95. last_sample_playing = p_item;
  96. set_process(true);
  97. } else {
  98. player->stop_all();
  99. if(last_sample_playing != p_item){
  100. TreeItem *tl=last_sample_playing->cast_to<TreeItem>();
  101. tl->set_button(p_column,0,get_icon("Play","EditorIcons"));
  102. btn_type = TTR("Stop");
  103. player->play(name,true);
  104. last_sample_playing = p_item;
  105. } else {
  106. btn_type = TTR("Play");
  107. is_playing = false;
  108. }
  109. }
  110. ti->set_button(p_column,0,get_icon(btn_type,"EditorIcons"));
  111. } else if (p_column==1) { // Edit
  112. get_tree()->get_root()->get_child(0)->call("_resource_selected",sample_library->get_sample(name));
  113. } else if (p_column==5) { // Delete
  114. ti->select(0);
  115. _delete_pressed();
  116. }
  117. }
  118. void SampleLibraryEditor::_item_edited() {
  119. if (!tree->get_selected())
  120. return;
  121. TreeItem *s = tree->get_selected();
  122. if (tree->get_selected_column()==0) { // Name
  123. // renamed
  124. String old_name=s->get_metadata(0);
  125. String new_name=s->get_text(0);
  126. if (old_name==new_name)
  127. return;
  128. if (new_name=="" || new_name.find("\\")!=-1 || new_name.find("/")!=-1 || sample_library->has_sample(new_name)) {
  129. s->set_text(0,old_name);
  130. return;
  131. }
  132. Ref<Sample> samp = sample_library->get_sample(old_name);
  133. undo_redo->create_action(TTR("Rename Sample"));
  134. undo_redo->add_do_method(sample_library.operator->(),"remove_sample",old_name);
  135. undo_redo->add_do_method(sample_library.operator->(),"add_sample",new_name,samp);
  136. undo_redo->add_undo_method(sample_library.operator->(),"remove_sample",new_name);
  137. undo_redo->add_undo_method(sample_library.operator->(),"add_sample",old_name,samp);
  138. undo_redo->add_do_method(this,"_update_library");
  139. undo_redo->add_undo_method(this,"_update_library");
  140. undo_redo->commit_action();
  141. } else if (tree->get_selected_column()==3) { // Volume dB
  142. StringName n = s->get_text(0);
  143. sample_library->sample_set_volume_db(n,s->get_range(3));
  144. } else if (tree->get_selected_column()==4) { // Pitch scale
  145. StringName n = s->get_text(0);
  146. sample_library->sample_set_pitch_scale(n,s->get_range(4));
  147. }
  148. }
  149. void SampleLibraryEditor::_delete_pressed() {
  150. if (!tree->get_selected())
  151. return;
  152. String to_remove = tree->get_selected()->get_text(0);
  153. undo_redo->create_action(TTR("Delete Sample"));
  154. undo_redo->add_do_method(sample_library.operator->(),"remove_sample",to_remove);
  155. undo_redo->add_undo_method(sample_library.operator->(),"add_sample",to_remove,sample_library->get_sample(to_remove));
  156. undo_redo->add_do_method(this,"_update_library");
  157. undo_redo->add_undo_method(this,"_update_library");
  158. undo_redo->commit_action();
  159. }
  160. void SampleLibraryEditor::_update_library() {
  161. player->stop_all();
  162. tree->clear();
  163. tree->set_hide_root(true);
  164. TreeItem *root = tree->create_item(NULL);
  165. List<StringName> names;
  166. sample_library->get_sample_list(&names);
  167. names.sort_custom<StringName::AlphCompare>();
  168. for(List<StringName>::Element *E=names.front();E;E=E->next()) {
  169. TreeItem *ti = tree->create_item(root);
  170. // Name + Play/Stop
  171. ti->set_cell_mode(0,TreeItem::CELL_MODE_STRING);
  172. ti->set_editable(0,true);
  173. ti->set_selectable(0,true);
  174. ti->set_text(0,E->get());
  175. ti->set_metadata(0,E->get());
  176. ti->add_button(0,get_icon("Play","EditorIcons"));
  177. Ref<Sample> smp = sample_library->get_sample(E->get());
  178. // Preview/edit
  179. Ref<ImageTexture> preview( memnew( ImageTexture ));
  180. preview->create(128,16,Image::FORMAT_RGB8);
  181. SampleEditor::generate_preview_texture(smp,preview);
  182. ti->set_cell_mode(1,TreeItem::CELL_MODE_ICON);
  183. ti->set_selectable(1,false);
  184. ti->set_editable(1,false);
  185. ti->set_icon(1,preview);
  186. ti->add_button(1,get_icon("Edit","EditorIcons"));
  187. // Format
  188. ti->set_cell_mode(2,TreeItem::CELL_MODE_STRING);
  189. ti->set_editable(2,false);
  190. ti->set_selectable(2,false);
  191. ti->set_text(2,String()+(smp->get_format()==Sample::FORMAT_PCM16?TTR("16 Bits")+", ":(smp->get_format()==Sample::FORMAT_PCM8?TTR("8 Bits")+", ":"IMA-ADPCM,"))+(smp->is_stereo()?TTR("Stereo"):TTR("Mono")));
  192. // Volume dB
  193. ti->set_cell_mode(3,TreeItem::CELL_MODE_RANGE);
  194. ti->set_range_config(3,-60,24,0.01);
  195. ti->set_selectable(3,true);
  196. ti->set_editable(3,true);
  197. ti->set_range(3,sample_library->sample_get_volume_db(E->get()));
  198. // Pitch scale
  199. ti->set_cell_mode(4,TreeItem::CELL_MODE_RANGE);
  200. ti->set_range_config(4,0.01,100,0.01);
  201. ti->set_selectable(4,true);
  202. ti->set_editable(4,true);
  203. ti->set_range(4,sample_library->sample_get_pitch_scale(E->get()));
  204. // Delete
  205. ti->set_cell_mode(5,TreeItem::CELL_MODE_STRING);
  206. ti->add_button(5,get_icon("Remove","EditorIcons"));
  207. }
  208. //player->add_sample("default",sample);
  209. }
  210. void SampleLibraryEditor::edit(Ref<SampleLibrary> p_sample_library) {
  211. sample_library=p_sample_library;
  212. if (!sample_library.is_null()) {
  213. player->set_sample_library(sample_library);
  214. _update_library();
  215. } else {
  216. hide();
  217. }
  218. }
  219. Variant SampleLibraryEditor::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  220. TreeItem*ti =tree->get_item_at_pos(p_point);
  221. if (!ti)
  222. return Variant();
  223. String name = ti->get_metadata(0);
  224. RES res = sample_library->get_sample(name);
  225. if (!res.is_valid())
  226. return Variant();
  227. return EditorNode::get_singleton()->drag_resource(res,p_from);
  228. }
  229. bool SampleLibraryEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const {
  230. Dictionary d = p_data;
  231. if (!d.has("type"))
  232. return false;
  233. if (d.has("from") && (Object*)(d["from"])==tree)
  234. return false;
  235. if (String(d["type"])=="resource" && d.has("resource")) {
  236. RES r=d["resource"];
  237. Ref<Sample> sample = r;
  238. if (sample.is_valid()) {
  239. return true;
  240. }
  241. }
  242. if (String(d["type"])=="files") {
  243. Vector<String> files = d["files"];
  244. if (files.size()==0)
  245. return false;
  246. for(int i=0;i<files.size();i++) {
  247. String file = files[0];
  248. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  249. if (ftype!="Sample") {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. return false;
  256. }
  257. void SampleLibraryEditor::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) {
  258. if (!can_drop_data_fw(p_point,p_data,p_from))
  259. return;
  260. Dictionary d = p_data;
  261. if (!d.has("type"))
  262. return;
  263. if (String(d["type"])=="resource" && d.has("resource")) {
  264. RES r=d["resource"];
  265. Ref<Sample> sample = r;
  266. if (sample.is_valid()) {
  267. String basename;
  268. if (sample->get_name()!="") {
  269. basename=sample->get_name();
  270. } else if (sample->get_path().is_resource_file()) {
  271. basename = sample->get_path().basename();
  272. } else {
  273. basename="Sample";
  274. }
  275. String name=basename;
  276. int counter=0;
  277. while(sample_library->has_sample(name)) {
  278. counter++;
  279. name=basename+"_"+itos(counter);
  280. }
  281. undo_redo->create_action(TTR("Add Sample"));
  282. undo_redo->add_do_method(sample_library.operator->(),"add_sample",name,sample);
  283. undo_redo->add_undo_method(sample_library.operator->(),"remove_sample",name);
  284. undo_redo->add_do_method(this,"_update_library");
  285. undo_redo->add_undo_method(this,"_update_library");
  286. undo_redo->commit_action();
  287. }
  288. }
  289. if (String(d["type"])=="files") {
  290. PoolVector<String> files = d["files"];
  291. _file_load_request(files);
  292. }
  293. }
  294. void SampleLibraryEditor::_bind_methods() {
  295. ClassDB::bind_method(_MD("_gui_input"),&SampleLibraryEditor::_gui_input);
  296. ClassDB::bind_method(_MD("_load_pressed"),&SampleLibraryEditor::_load_pressed);
  297. ClassDB::bind_method(_MD("_item_edited"),&SampleLibraryEditor::_item_edited);
  298. ClassDB::bind_method(_MD("_delete_pressed"),&SampleLibraryEditor::_delete_pressed);
  299. ClassDB::bind_method(_MD("_file_load_request"),&SampleLibraryEditor::_file_load_request);
  300. ClassDB::bind_method(_MD("_update_library"),&SampleLibraryEditor::_update_library);
  301. ClassDB::bind_method(_MD("_button_pressed"),&SampleLibraryEditor::_button_pressed);
  302. ClassDB::bind_method(_MD("get_drag_data_fw"), &SampleLibraryEditor::get_drag_data_fw);
  303. ClassDB::bind_method(_MD("can_drop_data_fw"), &SampleLibraryEditor::can_drop_data_fw);
  304. ClassDB::bind_method(_MD("drop_data_fw"), &SampleLibraryEditor::drop_data_fw);
  305. }
  306. SampleLibraryEditor::SampleLibraryEditor() {
  307. player = memnew(SamplePlayer);
  308. add_child(player);
  309. add_style_override("panel", get_stylebox("panel","Panel"));
  310. load = memnew( Button );
  311. load->set_pos(Point2( 5, 5 ));
  312. load->set_size( Size2(1,1 ) );
  313. add_child(load);
  314. file = memnew( EditorFileDialog );
  315. add_child(file);
  316. List<String> extensions;
  317. ResourceLoader::get_recognized_extensions_for_type("Sample",&extensions);
  318. for(int i=0;i<extensions.size();i++)
  319. file->add_filter("*."+extensions[i]);
  320. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  321. tree = memnew( Tree );
  322. tree->set_columns(6);
  323. add_child(tree);
  324. tree->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5);
  325. tree->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,5);
  326. tree->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,30);
  327. tree->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,5);
  328. tree->set_column_titles_visible(true);
  329. tree->set_column_title(0,TTR("Name"));
  330. tree->set_column_title(1,TTR("Preview"));
  331. tree->set_column_title(2,TTR("Format"));
  332. tree->set_column_title(3,"dB");
  333. tree->set_column_title(4,TTR("Pitch"));
  334. tree->set_column_title(5,"");
  335. tree->set_column_min_width(1,150);
  336. tree->set_column_min_width(2,100);
  337. tree->set_column_min_width(3,50);
  338. tree->set_column_min_width(4,50);
  339. tree->set_column_min_width(5,32);
  340. tree->set_column_expand(1,false);
  341. tree->set_column_expand(2,false);
  342. tree->set_column_expand(3,false);
  343. tree->set_column_expand(4,false);
  344. tree->set_column_expand(5,false);
  345. tree->set_drag_forwarding(this);
  346. dialog = memnew( ConfirmationDialog );
  347. add_child( dialog );
  348. tree->connect("button_pressed",this,"_button_pressed");
  349. load->connect("pressed", this,"_load_pressed");
  350. file->connect("files_selected", this,"_file_load_request");
  351. tree->connect("item_edited", this,"_item_edited");
  352. is_playing = false;
  353. }
  354. void SampleLibraryEditorPlugin::edit(Object *p_object) {
  355. sample_library_editor->set_undo_redo(&get_undo_redo());
  356. SampleLibrary * s = p_object->cast_to<SampleLibrary>();
  357. if (!s)
  358. return;
  359. sample_library_editor->edit(Ref<SampleLibrary>(s));
  360. }
  361. bool SampleLibraryEditorPlugin::handles(Object *p_object) const {
  362. return p_object->is_class("SampleLibrary");
  363. }
  364. void SampleLibraryEditorPlugin::make_visible(bool p_visible) {
  365. if (p_visible) {
  366. //sample_library_editor->show();
  367. button->show();
  368. editor->make_bottom_panel_item_visible(sample_library_editor);
  369. // sample_library_editor->set_process(true);
  370. } else {
  371. if (sample_library_editor->is_visible())
  372. editor->hide_bottom_panel();
  373. button->hide();
  374. // sample_library_editor->set_process(false);
  375. }
  376. }
  377. SampleLibraryEditorPlugin::SampleLibraryEditorPlugin(EditorNode *p_node) {
  378. editor=p_node;
  379. sample_library_editor = memnew( SampleLibraryEditor );
  380. //editor->get_viewport()->add_child(sample_library_editor);
  381. sample_library_editor->set_custom_minimum_size(Size2(0,250));
  382. button=p_node->add_bottom_panel_item("SampleLibrary",sample_library_editor);
  383. button->hide();
  384. //sample_library_editor->set_area_as_parent_rect();
  385. // sample_library_editor->set_anchor( MARGIN_TOP, Control::ANCHOR_END);
  386. // sample_library_editor->set_margin( MARGIN_TOP, 120 );
  387. //sample_library_editor->hide();
  388. }
  389. SampleLibraryEditorPlugin::~SampleLibraryEditorPlugin()
  390. {
  391. }