sample_library_editor_plugin.cpp 15 KB

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