editor_audio_buses.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /*************************************************************************/
  2. /* editor_audio_buses.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 "editor_audio_buses.h"
  31. #include "core/io/resource_saver.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor_node.h"
  34. #include "filesystem_dock.h"
  35. #include "servers/audio_server.h"
  36. void EditorAudioBus::_update_visible_channels() {
  37. int i = 0;
  38. for (; i < cc; i++) {
  39. if (!channel[i].vu_l->is_visible()) {
  40. channel[i].vu_l->show();
  41. }
  42. if (!channel[i].vu_r->is_visible()) {
  43. channel[i].vu_r->show();
  44. }
  45. }
  46. for (; i < CHANNELS_MAX; i++) {
  47. if (channel[i].vu_l->is_visible()) {
  48. channel[i].vu_l->hide();
  49. }
  50. if (channel[i].vu_r->is_visible()) {
  51. channel[i].vu_r->hide();
  52. }
  53. }
  54. }
  55. void EditorAudioBus::_notification(int p_what) {
  56. if (p_what == NOTIFICATION_READY) {
  57. for (int i = 0; i < CHANNELS_MAX; i++) {
  58. channel[i].vu_l->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  59. channel[i].vu_l->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  60. channel[i].vu_r->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  61. channel[i].vu_r->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  62. channel[i].prev_active = true;
  63. }
  64. scale->set_texture(get_icon("BusVuDb", "EditorIcons"));
  65. disabled_vu = get_icon("BusVuFrozen", "EditorIcons");
  66. solo->set_icon(get_icon("AudioBusSolo", "EditorIcons"));
  67. mute->set_icon(get_icon("AudioBusMute", "EditorIcons"));
  68. bypass->set_icon(get_icon("AudioBusBypass", "EditorIcons"));
  69. bus_options->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons"));
  70. update_bus();
  71. set_process(true);
  72. }
  73. if (p_what == NOTIFICATION_DRAW) {
  74. if (has_focus()) {
  75. draw_style_box(get_stylebox("focus", "Button"), Rect2(Vector2(), get_size()));
  76. } else if (is_master) {
  77. draw_style_box(get_stylebox("disabled", "Button"), Rect2(Vector2(), get_size()));
  78. }
  79. }
  80. if (p_what == NOTIFICATION_PROCESS) {
  81. if (cc != AudioServer::get_singleton()->get_bus_channels(get_index())) {
  82. cc = AudioServer::get_singleton()->get_bus_channels(get_index());
  83. _update_visible_channels();
  84. }
  85. for (int i = 0; i < cc; i++) {
  86. float real_peak[2] = { -100, -100 };
  87. bool activity_found = false;
  88. if (AudioServer::get_singleton()->is_bus_channel_active(get_index(), i)) {
  89. activity_found = true;
  90. real_peak[0] = MAX(real_peak[0], AudioServer::get_singleton()->get_bus_peak_volume_left_db(get_index(), i));
  91. real_peak[1] = MAX(real_peak[1], AudioServer::get_singleton()->get_bus_peak_volume_right_db(get_index(), i));
  92. }
  93. if (real_peak[0] > channel[i].peak_l) {
  94. channel[i].peak_l = real_peak[0];
  95. } else {
  96. channel[i].peak_l -= get_process_delta_time() * 60.0;
  97. }
  98. if (real_peak[1] > channel[i].peak_r) {
  99. channel[i].peak_r = real_peak[1];
  100. } else {
  101. channel[i].peak_r -= get_process_delta_time() * 60.0;
  102. }
  103. channel[i].vu_l->set_value(channel[i].peak_l);
  104. channel[i].vu_r->set_value(channel[i].peak_r);
  105. if (activity_found != channel[i].prev_active) {
  106. if (activity_found) {
  107. channel[i].vu_l->set_over_texture(Ref<Texture>());
  108. channel[i].vu_r->set_over_texture(Ref<Texture>());
  109. } else {
  110. channel[i].vu_l->set_over_texture(disabled_vu);
  111. channel[i].vu_r->set_over_texture(disabled_vu);
  112. }
  113. channel[i].prev_active = activity_found;
  114. }
  115. }
  116. }
  117. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  118. for (int i = 0; i < CHANNELS_MAX; i++) {
  119. channel[i].peak_l = -100;
  120. channel[i].peak_r = -100;
  121. channel[i].prev_active = true;
  122. }
  123. set_process(is_visible_in_tree());
  124. }
  125. if (p_what == NOTIFICATION_THEME_CHANGED) {
  126. for (int i = 0; i < CHANNELS_MAX; i++) {
  127. channel[i].vu_l->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  128. channel[i].vu_l->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  129. channel[i].vu_r->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  130. channel[i].vu_r->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  131. channel[i].prev_active = true;
  132. }
  133. scale->set_texture(get_icon("BusVuDb", "EditorIcons"));
  134. disabled_vu = get_icon("BusVuFrozen", "EditorIcons");
  135. solo->set_icon(get_icon("AudioBusSolo", "EditorIcons"));
  136. mute->set_icon(get_icon("AudioBusMute", "EditorIcons"));
  137. bypass->set_icon(get_icon("AudioBusBypass", "EditorIcons"));
  138. bus_options->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons"));
  139. }
  140. }
  141. void EditorAudioBus::update_send() {
  142. send->clear();
  143. if (is_master) {
  144. send->set_disabled(true);
  145. send->set_text(TTR("Speakers"));
  146. } else {
  147. send->set_disabled(false);
  148. StringName current_send = AudioServer::get_singleton()->get_bus_send(get_index());
  149. int current_send_index = 0; //by default to master
  150. for (int i = 0; i < get_index(); i++) {
  151. StringName send_name = AudioServer::get_singleton()->get_bus_name(i);
  152. send->add_item(send_name);
  153. if (send_name == current_send) {
  154. current_send_index = i;
  155. }
  156. }
  157. send->select(current_send_index);
  158. }
  159. }
  160. void EditorAudioBus::update_bus() {
  161. if (updating_bus)
  162. return;
  163. updating_bus = true;
  164. int index = get_index();
  165. slider->set_value(AudioServer::get_singleton()->get_bus_volume_db(index));
  166. track_name->set_text(AudioServer::get_singleton()->get_bus_name(index));
  167. if (is_master)
  168. track_name->set_editable(false);
  169. solo->set_pressed(AudioServer::get_singleton()->is_bus_solo(index));
  170. mute->set_pressed(AudioServer::get_singleton()->is_bus_mute(index));
  171. bypass->set_pressed(AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  172. // effects..
  173. effects->clear();
  174. TreeItem *root = effects->create_item();
  175. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  176. Ref<AudioEffect> afx = AudioServer::get_singleton()->get_bus_effect(index, i);
  177. TreeItem *fx = effects->create_item(root);
  178. fx->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  179. fx->set_editable(0, true);
  180. fx->set_checked(0, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  181. fx->set_text(0, afx->get_name());
  182. fx->set_metadata(0, i);
  183. }
  184. TreeItem *add = effects->create_item(root);
  185. add->set_cell_mode(0, TreeItem::CELL_MODE_CUSTOM);
  186. add->set_editable(0, true);
  187. add->set_selectable(0, false);
  188. add->set_text(0, TTR("Add Effect"));
  189. update_send();
  190. updating_bus = false;
  191. }
  192. void EditorAudioBus::_name_changed(const String &p_new_name) {
  193. if (p_new_name == AudioServer::get_singleton()->get_bus_name(get_index()))
  194. return;
  195. String attempt = p_new_name;
  196. int attempts = 1;
  197. while (true) {
  198. bool name_free = true;
  199. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  200. if (AudioServer::get_singleton()->get_bus_name(i) == attempt) {
  201. name_free = false;
  202. break;
  203. }
  204. }
  205. if (name_free) {
  206. break;
  207. }
  208. attempts++;
  209. attempt = p_new_name + " " + itos(attempts);
  210. }
  211. updating_bus = true;
  212. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  213. StringName current = AudioServer::get_singleton()->get_bus_name(get_index());
  214. ur->create_action(TTR("Rename Audio Bus"));
  215. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", get_index(), attempt);
  216. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", get_index(), current);
  217. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  218. if (AudioServer::get_singleton()->get_bus_send(i) == current) {
  219. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", i, attempt);
  220. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", i, current);
  221. }
  222. }
  223. ur->add_do_method(buses, "_update_bus", get_index());
  224. ur->add_undo_method(buses, "_update_bus", get_index());
  225. ur->add_do_method(buses, "_update_sends");
  226. ur->add_undo_method(buses, "_update_sends");
  227. ur->commit_action();
  228. updating_bus = false;
  229. track_name->release_focus();
  230. }
  231. void EditorAudioBus::_volume_db_changed(float p_db) {
  232. if (updating_bus)
  233. return;
  234. updating_bus = true;
  235. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  236. ur->create_action(TTR("Change Audio Bus Volume"), UndoRedo::MERGE_ENDS);
  237. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), p_db);
  238. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), AudioServer::get_singleton()->get_bus_volume_db(get_index()));
  239. ur->add_do_method(buses, "_update_bus", get_index());
  240. ur->add_undo_method(buses, "_update_bus", get_index());
  241. ur->commit_action();
  242. updating_bus = false;
  243. }
  244. void EditorAudioBus::_solo_toggled() {
  245. updating_bus = true;
  246. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  247. ur->create_action(TTR("Toggle Audio Bus Solo"));
  248. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), solo->is_pressed());
  249. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), AudioServer::get_singleton()->is_bus_solo(get_index()));
  250. ur->add_do_method(buses, "_update_bus", get_index());
  251. ur->add_undo_method(buses, "_update_bus", get_index());
  252. ur->commit_action();
  253. updating_bus = false;
  254. }
  255. void EditorAudioBus::_mute_toggled() {
  256. updating_bus = true;
  257. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  258. ur->create_action(TTR("Toggle Audio Bus Mute"));
  259. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), mute->is_pressed());
  260. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), AudioServer::get_singleton()->is_bus_mute(get_index()));
  261. ur->add_do_method(buses, "_update_bus", get_index());
  262. ur->add_undo_method(buses, "_update_bus", get_index());
  263. ur->commit_action();
  264. updating_bus = false;
  265. }
  266. void EditorAudioBus::_bypass_toggled() {
  267. updating_bus = true;
  268. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  269. ur->create_action(TTR("Toggle Audio Bus Bypass Effects"));
  270. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), bypass->is_pressed());
  271. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), AudioServer::get_singleton()->is_bus_bypassing_effects(get_index()));
  272. ur->add_do_method(buses, "_update_bus", get_index());
  273. ur->add_undo_method(buses, "_update_bus", get_index());
  274. ur->commit_action();
  275. updating_bus = false;
  276. }
  277. void EditorAudioBus::_send_selected(int p_which) {
  278. updating_bus = true;
  279. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  280. ur->create_action("Select Audio Bus Send");
  281. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", get_index(), send->get_item_text(p_which));
  282. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", get_index(), AudioServer::get_singleton()->get_bus_send(get_index()));
  283. ur->add_do_method(buses, "_update_bus", get_index());
  284. ur->add_undo_method(buses, "_update_bus", get_index());
  285. ur->commit_action();
  286. updating_bus = false;
  287. }
  288. void EditorAudioBus::_effect_selected() {
  289. TreeItem *effect = effects->get_selected();
  290. if (!effect)
  291. return;
  292. updating_bus = true;
  293. if (effect->get_metadata(0) != Variant()) {
  294. int index = effect->get_metadata(0);
  295. Ref<AudioEffect> effect = AudioServer::get_singleton()->get_bus_effect(get_index(), index);
  296. if (effect.is_valid()) {
  297. EditorNode::get_singleton()->push_item(effect.ptr());
  298. }
  299. }
  300. updating_bus = false;
  301. }
  302. void EditorAudioBus::_effect_edited() {
  303. if (updating_bus)
  304. return;
  305. TreeItem *effect = effects->get_edited();
  306. if (!effect)
  307. return;
  308. if (effect->get_metadata(0) == Variant()) {
  309. Rect2 area = effects->get_item_rect(effect);
  310. effect_options->set_position(effects->get_global_position() + area.position + Vector2(0, area.size.y));
  311. effect_options->popup();
  312. //add effect
  313. } else {
  314. int index = effect->get_metadata(0);
  315. updating_bus = true;
  316. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  317. ur->create_action(TTR("Select Audio Bus Send"));
  318. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, effect->is_checked(0));
  319. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  320. ur->add_do_method(buses, "_update_bus", get_index());
  321. ur->add_undo_method(buses, "_update_bus", get_index());
  322. ur->commit_action();
  323. updating_bus = false;
  324. }
  325. }
  326. void EditorAudioBus::_effect_add(int p_which) {
  327. if (updating_bus)
  328. return;
  329. StringName name = effect_options->get_item_metadata(p_which);
  330. Object *fx = ClassDB::instance(name);
  331. ERR_FAIL_COND(!fx);
  332. AudioEffect *afx = Object::cast_to<AudioEffect>(fx);
  333. ERR_FAIL_COND(!afx);
  334. Ref<AudioEffect> afxr = Ref<AudioEffect>(afx);
  335. afxr->set_name(effect_options->get_item_text(p_which));
  336. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  337. ur->create_action(TTR("Add Audio Bus Effect"));
  338. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), afxr, -1);
  339. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect_count(get_index()));
  340. ur->add_do_method(buses, "_update_bus", get_index());
  341. ur->add_undo_method(buses, "_update_bus", get_index());
  342. ur->commit_action();
  343. }
  344. void EditorAudioBus::_gui_input(const Ref<InputEvent> &p_event) {
  345. Ref<InputEventKey> k = p_event;
  346. if (k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && !k->is_echo()) {
  347. accept_event();
  348. emit_signal("delete_request");
  349. }
  350. Ref<InputEventMouseButton> mb = p_event;
  351. if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) {
  352. Vector2 pos = Vector2(mb->get_position().x, mb->get_position().y);
  353. bus_popup->set_position(get_global_position() + pos);
  354. bus_popup->popup();
  355. }
  356. }
  357. void EditorAudioBus::_bus_popup_pressed(int p_option) {
  358. if (p_option == 2) {
  359. // Reset volume
  360. emit_signal("vol_reset_request");
  361. } else if (p_option == 1) {
  362. emit_signal("delete_request");
  363. } else if (p_option == 0) {
  364. //duplicate
  365. emit_signal("duplicate_request", get_index());
  366. }
  367. }
  368. Variant EditorAudioBus::get_drag_data(const Point2 &p_point) {
  369. if (get_index() == 0) {
  370. return Variant();
  371. }
  372. Control *c = memnew(Control);
  373. Panel *p = memnew(Panel);
  374. c->add_child(p);
  375. p->add_style_override("panel", get_stylebox("focus", "Button"));
  376. p->set_size(get_size());
  377. p->set_position(-p_point);
  378. set_drag_preview(c);
  379. Dictionary d;
  380. d["type"] = "move_audio_bus";
  381. d["index"] = get_index();
  382. emit_signal("drop_end_request");
  383. return d;
  384. }
  385. bool EditorAudioBus::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  386. if (get_index() == 0)
  387. return false;
  388. Dictionary d = p_data;
  389. if (d.has("type") && String(d["type"]) == "move_audio_bus") {
  390. return true;
  391. }
  392. return false;
  393. }
  394. void EditorAudioBus::drop_data(const Point2 &p_point, const Variant &p_data) {
  395. Dictionary d = p_data;
  396. emit_signal("dropped", d["index"], get_index());
  397. }
  398. Variant EditorAudioBus::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  399. TreeItem *item = effects->get_item_at_position(p_point);
  400. if (!item) {
  401. return Variant();
  402. }
  403. Variant md = item->get_metadata(0);
  404. if (md.get_type() == Variant::INT) {
  405. Dictionary fxd;
  406. fxd["type"] = "audio_bus_effect";
  407. fxd["bus"] = get_index();
  408. fxd["effect"] = md;
  409. Label *l = memnew(Label);
  410. l->set_text(item->get_text(0));
  411. effects->set_drag_preview(l);
  412. return fxd;
  413. }
  414. return Variant();
  415. }
  416. bool EditorAudioBus::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  417. Dictionary d = p_data;
  418. if (!d.has("type") || String(d["type"]) != "audio_bus_effect")
  419. return false;
  420. TreeItem *item = effects->get_item_at_position(p_point);
  421. if (!item)
  422. return false;
  423. effects->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  424. return true;
  425. }
  426. void EditorAudioBus::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  427. Dictionary d = p_data;
  428. TreeItem *item = effects->get_item_at_position(p_point);
  429. if (!item)
  430. return;
  431. int pos = effects->get_drop_section_at_position(p_point);
  432. Variant md = item->get_metadata(0);
  433. int paste_at;
  434. int bus = d["bus"];
  435. int effect = d["effect"];
  436. if (md.get_type() == Variant::INT) {
  437. paste_at = md;
  438. if (pos > 0)
  439. paste_at++;
  440. if (bus == get_index() && paste_at > effect) {
  441. paste_at--;
  442. }
  443. } else {
  444. paste_at = -1;
  445. }
  446. bool enabled = AudioServer::get_singleton()->is_bus_effect_enabled(bus, effect);
  447. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  448. ur->create_action(TTR("Move Bus Effect"));
  449. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", bus, effect);
  450. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(bus, effect), paste_at);
  451. if (paste_at == -1) {
  452. paste_at = AudioServer::get_singleton()->get_bus_effect_count(get_index());
  453. if (bus == get_index()) {
  454. paste_at--;
  455. }
  456. }
  457. if (!enabled) {
  458. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), paste_at, false);
  459. }
  460. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), paste_at);
  461. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", bus, AudioServer::get_singleton()->get_bus_effect(bus, effect), effect);
  462. if (!enabled) {
  463. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", bus, effect, false);
  464. }
  465. ur->add_do_method(buses, "_update_bus", get_index());
  466. ur->add_undo_method(buses, "_update_bus", get_index());
  467. if (get_index() != bus) {
  468. ur->add_do_method(buses, "_update_bus", bus);
  469. ur->add_undo_method(buses, "_update_bus", bus);
  470. }
  471. ur->commit_action();
  472. }
  473. void EditorAudioBus::_delete_effect_pressed(int p_option) {
  474. TreeItem *item = effects->get_selected();
  475. if (!item)
  476. return;
  477. if (item->get_metadata(0).get_type() != Variant::INT)
  478. return;
  479. int index = item->get_metadata(0);
  480. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  481. ur->create_action(TTR("Delete Bus Effect"));
  482. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), index);
  483. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(get_index(), index), index);
  484. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  485. ur->add_do_method(buses, "_update_bus", get_index());
  486. ur->add_undo_method(buses, "_update_bus", get_index());
  487. ur->commit_action();
  488. }
  489. void EditorAudioBus::_effect_rmb(const Vector2 &p_pos) {
  490. TreeItem *item = effects->get_selected();
  491. if (!item)
  492. return;
  493. if (item->get_metadata(0).get_type() != Variant::INT)
  494. return;
  495. delete_effect_popup->set_position(get_global_mouse_position());
  496. delete_effect_popup->popup();
  497. }
  498. void EditorAudioBus::_bind_methods() {
  499. ClassDB::bind_method("update_bus", &EditorAudioBus::update_bus);
  500. ClassDB::bind_method("update_send", &EditorAudioBus::update_send);
  501. ClassDB::bind_method("_name_changed", &EditorAudioBus::_name_changed);
  502. ClassDB::bind_method("_volume_db_changed", &EditorAudioBus::_volume_db_changed);
  503. ClassDB::bind_method("_solo_toggled", &EditorAudioBus::_solo_toggled);
  504. ClassDB::bind_method("_mute_toggled", &EditorAudioBus::_mute_toggled);
  505. ClassDB::bind_method("_bypass_toggled", &EditorAudioBus::_bypass_toggled);
  506. ClassDB::bind_method("_name_focus_exit", &EditorAudioBus::_name_focus_exit);
  507. ClassDB::bind_method("_send_selected", &EditorAudioBus::_send_selected);
  508. ClassDB::bind_method("_effect_edited", &EditorAudioBus::_effect_edited);
  509. ClassDB::bind_method("_effect_selected", &EditorAudioBus::_effect_selected);
  510. ClassDB::bind_method("_effect_add", &EditorAudioBus::_effect_add);
  511. ClassDB::bind_method("_gui_input", &EditorAudioBus::_gui_input);
  512. ClassDB::bind_method("_bus_popup_pressed", &EditorAudioBus::_bus_popup_pressed);
  513. ClassDB::bind_method("get_drag_data_fw", &EditorAudioBus::get_drag_data_fw);
  514. ClassDB::bind_method("can_drop_data_fw", &EditorAudioBus::can_drop_data_fw);
  515. ClassDB::bind_method("drop_data_fw", &EditorAudioBus::drop_data_fw);
  516. ClassDB::bind_method("_delete_effect_pressed", &EditorAudioBus::_delete_effect_pressed);
  517. ClassDB::bind_method("_effect_rmb", &EditorAudioBus::_effect_rmb);
  518. ADD_SIGNAL(MethodInfo("duplicate_request"));
  519. ADD_SIGNAL(MethodInfo("delete_request"));
  520. ADD_SIGNAL(MethodInfo("vol_reset_request"));
  521. ADD_SIGNAL(MethodInfo("drop_end_request"));
  522. ADD_SIGNAL(MethodInfo("dropped"));
  523. }
  524. EditorAudioBus::EditorAudioBus(EditorAudioBuses *p_buses, bool p_is_master) {
  525. buses = p_buses;
  526. updating_bus = false;
  527. is_master = p_is_master;
  528. set_tooltip(TTR("Audio Bus, Drag and Drop to rearrange."));
  529. VBoxContainer *vb = memnew(VBoxContainer);
  530. add_child(vb);
  531. set_v_size_flags(SIZE_EXPAND_FILL);
  532. set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  533. track_name = memnew(LineEdit);
  534. track_name->connect("text_entered", this, "_name_changed");
  535. track_name->connect("focus_exited", this, "_name_focus_exit");
  536. vb->add_child(track_name);
  537. HBoxContainer *hbc = memnew(HBoxContainer);
  538. vb->add_child(hbc);
  539. solo = memnew(ToolButton);
  540. solo->set_toggle_mode(true);
  541. solo->set_tooltip(TTR("Solo"));
  542. solo->set_focus_mode(FOCUS_NONE);
  543. solo->connect("pressed", this, "_solo_toggled");
  544. hbc->add_child(solo);
  545. mute = memnew(ToolButton);
  546. mute->set_toggle_mode(true);
  547. mute->set_tooltip(TTR("Mute"));
  548. mute->set_focus_mode(FOCUS_NONE);
  549. mute->connect("pressed", this, "_mute_toggled");
  550. hbc->add_child(mute);
  551. bypass = memnew(ToolButton);
  552. bypass->set_toggle_mode(true);
  553. bypass->set_tooltip(TTR("Bypass"));
  554. bypass->set_focus_mode(FOCUS_NONE);
  555. bypass->connect("pressed", this, "_bypass_toggled");
  556. hbc->add_child(bypass);
  557. hbc->add_spacer();
  558. bus_options = memnew(MenuButton);
  559. bus_options->set_h_size_flags(SIZE_SHRINK_END);
  560. bus_options->set_anchor(MARGIN_RIGHT, 0.0);
  561. bus_options->set_tooltip(TTR("Bus options"));
  562. hbc->add_child(bus_options);
  563. Ref<StyleBoxEmpty> sbempty = memnew(StyleBoxEmpty);
  564. for (int i = 0; i < hbc->get_child_count(); i++) {
  565. Control *child = Object::cast_to<Control>(hbc->get_child(i));
  566. child->add_style_override("normal", sbempty);
  567. child->add_style_override("hover", sbempty);
  568. child->add_style_override("focus", sbempty);
  569. child->add_style_override("pressed", sbempty);
  570. }
  571. vb->add_child(memnew(HSeparator));
  572. HBoxContainer *hb = memnew(HBoxContainer);
  573. vb->add_child(hb);
  574. slider = memnew(VSlider);
  575. slider->set_min(-80);
  576. slider->set_max(24);
  577. slider->set_step(0.1);
  578. slider->connect("value_changed", this, "_volume_db_changed");
  579. hb->add_child(slider);
  580. cc = 0;
  581. for (int i = 0; i < CHANNELS_MAX; i++) {
  582. channel[i].vu_l = memnew(TextureProgress);
  583. channel[i].vu_l->set_fill_mode(TextureProgress::FILL_BOTTOM_TO_TOP);
  584. hb->add_child(channel[i].vu_l);
  585. channel[i].vu_l->set_min(-80);
  586. channel[i].vu_l->set_max(24);
  587. channel[i].vu_l->set_step(0.1);
  588. channel[i].vu_r = memnew(TextureProgress);
  589. channel[i].vu_r->set_fill_mode(TextureProgress::FILL_BOTTOM_TO_TOP);
  590. hb->add_child(channel[i].vu_r);
  591. channel[i].vu_r->set_min(-80);
  592. channel[i].vu_r->set_max(24);
  593. channel[i].vu_r->set_step(0.1);
  594. channel[i].peak_l = 0.0f;
  595. channel[i].peak_r = 0.0f;
  596. }
  597. scale = memnew(TextureRect);
  598. hb->add_child(scale);
  599. effects = memnew(Tree);
  600. effects->set_hide_root(true);
  601. effects->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  602. effects->set_hide_folding(true);
  603. effects->set_v_size_flags(SIZE_EXPAND_FILL);
  604. vb->add_child(effects);
  605. effects->connect("item_edited", this, "_effect_edited");
  606. effects->connect("cell_selected", this, "_effect_selected");
  607. effects->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  608. effects->set_drag_forwarding(this);
  609. effects->connect("item_rmb_selected", this, "_effect_rmb");
  610. effects->set_allow_rmb_select(true);
  611. effects->set_focus_mode(FOCUS_CLICK);
  612. effects->set_allow_reselect(true);
  613. send = memnew(OptionButton);
  614. send->set_clip_text(true);
  615. send->connect("item_selected", this, "_send_selected");
  616. vb->add_child(send);
  617. set_focus_mode(FOCUS_CLICK);
  618. effect_options = memnew(PopupMenu);
  619. effect_options->connect("index_pressed", this, "_effect_add");
  620. add_child(effect_options);
  621. List<StringName> effects;
  622. ClassDB::get_inheriters_from_class("AudioEffect", &effects);
  623. effects.sort_custom<StringName::AlphCompare>();
  624. for (List<StringName>::Element *E = effects.front(); E; E = E->next()) {
  625. if (!ClassDB::can_instance(E->get()))
  626. continue;
  627. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(E->get());
  628. String name = E->get().operator String().replace("AudioEffect", "");
  629. effect_options->add_item(name);
  630. effect_options->set_item_metadata(effect_options->get_item_count() - 1, E->get());
  631. effect_options->set_item_icon(effect_options->get_item_count() - 1, icon);
  632. }
  633. bus_popup = bus_options->get_popup();
  634. bus_popup->add_item(TTR("Duplicate"));
  635. bus_popup->add_item(TTR("Delete"));
  636. bus_popup->set_item_disabled(1, is_master);
  637. bus_popup->add_item(TTR("Reset Volume"));
  638. bus_popup->connect("index_pressed", this, "_bus_popup_pressed");
  639. delete_effect_popup = memnew(PopupMenu);
  640. delete_effect_popup->add_item(TTR("Delete Effect"));
  641. add_child(delete_effect_popup);
  642. delete_effect_popup->connect("index_pressed", this, "_delete_effect_pressed");
  643. }
  644. bool EditorAudioBusDrop::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  645. Dictionary d = p_data;
  646. if (d.has("type") && String(d["type"]) == "move_audio_bus") {
  647. return true;
  648. }
  649. return false;
  650. }
  651. void EditorAudioBusDrop::drop_data(const Point2 &p_point, const Variant &p_data) {
  652. Dictionary d = p_data;
  653. emit_signal("dropped", d["index"], -1);
  654. }
  655. void EditorAudioBusDrop::_bind_methods() {
  656. ADD_SIGNAL(MethodInfo("dropped"));
  657. }
  658. EditorAudioBusDrop::EditorAudioBusDrop() {
  659. }
  660. void EditorAudioBuses::_update_buses() {
  661. while (bus_hb->get_child_count() > 0) {
  662. memdelete(bus_hb->get_child(0));
  663. }
  664. drop_end = NULL;
  665. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  666. bool is_master = i == 0 ? true : false;
  667. EditorAudioBus *audio_bus = memnew(EditorAudioBus(this, is_master));
  668. bus_hb->add_child(audio_bus);
  669. audio_bus->connect("delete_request", this, "_delete_bus", varray(audio_bus), CONNECT_DEFERRED);
  670. audio_bus->connect("duplicate_request", this, "_duplicate_bus", varray(), CONNECT_DEFERRED);
  671. audio_bus->connect("vol_reset_request", this, "_reset_bus_volume", varray(audio_bus), CONNECT_DEFERRED);
  672. audio_bus->connect("drop_end_request", this, "_request_drop_end");
  673. audio_bus->connect("dropped", this, "_drop_at_index", varray(), CONNECT_DEFERRED);
  674. }
  675. }
  676. EditorAudioBuses *EditorAudioBuses::register_editor() {
  677. EditorAudioBuses *audio_buses = memnew(EditorAudioBuses);
  678. EditorNode::get_singleton()->add_bottom_panel_item(TTR("Audio"), audio_buses);
  679. return audio_buses;
  680. }
  681. void EditorAudioBuses::_notification(int p_what) {
  682. if (p_what == NOTIFICATION_READY) {
  683. _update_buses();
  684. }
  685. if (p_what == NOTIFICATION_DRAG_END) {
  686. if (drop_end) {
  687. drop_end->queue_delete();
  688. drop_end = NULL;
  689. }
  690. }
  691. if (p_what == NOTIFICATION_PROCESS) {
  692. //check if anything was edited
  693. bool edited = AudioServer::get_singleton()->is_edited();
  694. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  695. for (int j = 0; j < AudioServer::get_singleton()->get_bus_effect_count(i); j++) {
  696. Ref<AudioEffect> effect = AudioServer::get_singleton()->get_bus_effect(i, j);
  697. if (effect->is_edited()) {
  698. edited = true;
  699. effect->set_edited(false);
  700. }
  701. }
  702. }
  703. AudioServer::get_singleton()->set_edited(false);
  704. if (edited) {
  705. save_timer->start();
  706. }
  707. }
  708. }
  709. void EditorAudioBuses::_add_bus() {
  710. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  711. //need to simulate new name, so we can undi :(
  712. ur->create_action(TTR("Add Audio Bus"));
  713. ur->add_do_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count() + 1);
  714. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count());
  715. ur->add_do_method(this, "_update_buses");
  716. ur->add_undo_method(this, "_update_buses");
  717. ur->commit_action();
  718. }
  719. void EditorAudioBuses::_update_bus(int p_index) {
  720. if (p_index >= bus_hb->get_child_count())
  721. return;
  722. bus_hb->get_child(p_index)->call("update_bus");
  723. }
  724. void EditorAudioBuses::_update_sends() {
  725. for (int i = 0; i < bus_hb->get_child_count(); i++) {
  726. bus_hb->get_child(i)->call("update_send");
  727. }
  728. }
  729. void EditorAudioBuses::_delete_bus(Object *p_which) {
  730. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  731. int index = bus->get_index();
  732. if (index == 0) {
  733. EditorNode::get_singleton()->show_warning(TTR("Master bus can't be deleted!"));
  734. return;
  735. }
  736. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  737. ur->create_action(TTR("Delete Audio Bus"));
  738. ur->add_do_method(AudioServer::get_singleton(), "remove_bus", index);
  739. ur->add_undo_method(AudioServer::get_singleton(), "add_bus", index);
  740. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", index, AudioServer::get_singleton()->get_bus_name(index));
  741. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  742. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", index, AudioServer::get_singleton()->get_bus_send(index));
  743. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", index, AudioServer::get_singleton()->is_bus_solo(index));
  744. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", index, AudioServer::get_singleton()->is_bus_mute(index));
  745. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", index, AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  746. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  747. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", index, AudioServer::get_singleton()->get_bus_effect(index, i));
  748. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", index, i, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  749. }
  750. ur->add_do_method(this, "_update_buses");
  751. ur->add_undo_method(this, "_update_buses");
  752. ur->commit_action();
  753. }
  754. void EditorAudioBuses::_duplicate_bus(int p_which) {
  755. int add_at_pos = p_which + 1;
  756. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  757. ur->create_action(TTR("Duplicate Audio Bus"));
  758. ur->add_do_method(AudioServer::get_singleton(), "add_bus", add_at_pos);
  759. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", add_at_pos, AudioServer::get_singleton()->get_bus_name(p_which) + " Copy");
  760. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", add_at_pos, AudioServer::get_singleton()->get_bus_volume_db(p_which));
  761. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", add_at_pos, AudioServer::get_singleton()->get_bus_send(p_which));
  762. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", add_at_pos, AudioServer::get_singleton()->is_bus_solo(p_which));
  763. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", add_at_pos, AudioServer::get_singleton()->is_bus_mute(p_which));
  764. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", add_at_pos, AudioServer::get_singleton()->is_bus_bypassing_effects(p_which));
  765. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(p_which); i++) {
  766. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", add_at_pos, AudioServer::get_singleton()->get_bus_effect(p_which, i));
  767. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", add_at_pos, i, AudioServer::get_singleton()->is_bus_effect_enabled(p_which, i));
  768. }
  769. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus", add_at_pos);
  770. ur->add_do_method(this, "_update_buses");
  771. ur->add_undo_method(this, "_update_buses");
  772. ur->commit_action();
  773. }
  774. void EditorAudioBuses::_reset_bus_volume(Object *p_which) {
  775. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  776. int index = bus->get_index();
  777. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  778. ur->create_action(TTR("Reset Bus Volume"));
  779. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", index, 0.f);
  780. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  781. ur->add_do_method(this, "_update_buses");
  782. ur->add_undo_method(this, "_update_buses");
  783. ur->commit_action();
  784. }
  785. void EditorAudioBuses::_request_drop_end() {
  786. if (!drop_end && bus_hb->get_child_count()) {
  787. drop_end = memnew(EditorAudioBusDrop);
  788. bus_hb->add_child(drop_end);
  789. drop_end->set_custom_minimum_size(Object::cast_to<Control>(bus_hb->get_child(0))->get_size());
  790. drop_end->connect("dropped", this, "_drop_at_index", varray(), CONNECT_DEFERRED);
  791. }
  792. }
  793. void EditorAudioBuses::_drop_at_index(int p_bus, int p_index) {
  794. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  795. //need to simulate new name, so we can undi :(
  796. ur->create_action(TTR("Move Audio Bus"));
  797. ur->add_do_method(AudioServer::get_singleton(), "move_bus", p_bus, p_index);
  798. int final_pos;
  799. if (p_index == p_bus) {
  800. final_pos = p_bus;
  801. } else if (p_index == -1) {
  802. final_pos = AudioServer::get_singleton()->get_bus_count() - 1;
  803. } else if (p_index < p_bus) {
  804. final_pos = p_index;
  805. } else {
  806. final_pos = p_index - 1;
  807. }
  808. ur->add_undo_method(AudioServer::get_singleton(), "move_bus", final_pos, p_bus);
  809. ur->add_do_method(this, "_update_buses");
  810. ur->add_undo_method(this, "_update_buses");
  811. ur->commit_action();
  812. }
  813. void EditorAudioBuses::_server_save() {
  814. Ref<AudioBusLayout> state = AudioServer::get_singleton()->generate_bus_layout();
  815. ResourceSaver::save(edited_path, state);
  816. }
  817. void EditorAudioBuses::_select_layout() {
  818. EditorNode::get_singleton()->get_filesystem_dock()->select_file(edited_path);
  819. }
  820. void EditorAudioBuses::_save_as_layout() {
  821. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  822. file_dialog->set_title(TTR("Save Audio Bus Layout As..."));
  823. file_dialog->set_current_path(edited_path);
  824. file_dialog->popup_centered_ratio();
  825. new_layout = false;
  826. }
  827. void EditorAudioBuses::_new_layout() {
  828. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  829. file_dialog->set_title(TTR("Location for New Layout..."));
  830. file_dialog->set_current_path(edited_path);
  831. file_dialog->popup_centered_ratio();
  832. new_layout = true;
  833. }
  834. void EditorAudioBuses::_load_layout() {
  835. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  836. file_dialog->set_title(TTR("Open Audio Bus Layout"));
  837. file_dialog->set_current_path(edited_path);
  838. file_dialog->popup_centered_ratio();
  839. new_layout = false;
  840. }
  841. void EditorAudioBuses::_load_default_layout() {
  842. Ref<AudioBusLayout> state = ResourceLoader::load("res://default_bus_layout.tres");
  843. if (state.is_null()) {
  844. EditorNode::get_singleton()->show_warning(TTR("There is no 'res://default_bus_layout.tres' file."));
  845. return;
  846. }
  847. edited_path = "res://default_bus_layout.tres";
  848. file->set_text(edited_path.get_file());
  849. AudioServer::get_singleton()->set_bus_layout(state);
  850. _update_buses();
  851. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  852. call_deferred("_select_layout");
  853. }
  854. void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
  855. if (file_dialog->get_mode() == EditorFileDialog::MODE_OPEN_FILE) {
  856. Ref<AudioBusLayout> state = ResourceLoader::load(p_string);
  857. if (state.is_null()) {
  858. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  859. return;
  860. }
  861. edited_path = p_string;
  862. file->set_text(p_string.get_file());
  863. AudioServer::get_singleton()->set_bus_layout(state);
  864. _update_buses();
  865. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  866. call_deferred("_select_layout");
  867. } else if (file_dialog->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  868. if (new_layout) {
  869. Ref<AudioBusLayout> empty_state;
  870. empty_state.instance();
  871. AudioServer::get_singleton()->set_bus_layout(empty_state);
  872. }
  873. Error err = ResourceSaver::save(p_string, AudioServer::get_singleton()->generate_bus_layout());
  874. if (err != OK) {
  875. EditorNode::get_singleton()->show_warning("Error saving file: " + p_string);
  876. return;
  877. }
  878. edited_path = p_string;
  879. file->set_text(p_string.get_file());
  880. _update_buses();
  881. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  882. call_deferred("_select_layout");
  883. }
  884. }
  885. void EditorAudioBuses::_bind_methods() {
  886. ClassDB::bind_method("_add_bus", &EditorAudioBuses::_add_bus);
  887. ClassDB::bind_method("_update_buses", &EditorAudioBuses::_update_buses);
  888. ClassDB::bind_method("_update_bus", &EditorAudioBuses::_update_bus);
  889. ClassDB::bind_method("_update_sends", &EditorAudioBuses::_update_sends);
  890. ClassDB::bind_method("_delete_bus", &EditorAudioBuses::_delete_bus);
  891. ClassDB::bind_method("_request_drop_end", &EditorAudioBuses::_request_drop_end);
  892. ClassDB::bind_method("_drop_at_index", &EditorAudioBuses::_drop_at_index);
  893. ClassDB::bind_method("_server_save", &EditorAudioBuses::_server_save);
  894. ClassDB::bind_method("_select_layout", &EditorAudioBuses::_select_layout);
  895. ClassDB::bind_method("_save_as_layout", &EditorAudioBuses::_save_as_layout);
  896. ClassDB::bind_method("_load_layout", &EditorAudioBuses::_load_layout);
  897. ClassDB::bind_method("_load_default_layout", &EditorAudioBuses::_load_default_layout);
  898. ClassDB::bind_method("_new_layout", &EditorAudioBuses::_new_layout);
  899. ClassDB::bind_method("_duplicate_bus", &EditorAudioBuses::_duplicate_bus);
  900. ClassDB::bind_method("_reset_bus_volume", &EditorAudioBuses::_reset_bus_volume);
  901. ClassDB::bind_method("_file_dialog_callback", &EditorAudioBuses::_file_dialog_callback);
  902. }
  903. EditorAudioBuses::EditorAudioBuses() {
  904. drop_end = NULL;
  905. top_hb = memnew(HBoxContainer);
  906. add_child(top_hb);
  907. file = memnew(ToolButton);
  908. file->set_text("default_bus_layout.tres");
  909. top_hb->add_child(file);
  910. file->connect("pressed", this, "_select_layout");
  911. add = memnew(Button);
  912. top_hb->add_child(add);
  913. add->set_text(TTR("Add Bus"));
  914. add->set_tooltip(TTR("Create a new Bus Layout."));
  915. add->connect("pressed", this, "_add_bus");
  916. top_hb->add_spacer();
  917. load = memnew(Button);
  918. load->set_text(TTR("Load"));
  919. load->set_tooltip(TTR("Load an existing Bus Layout."));
  920. top_hb->add_child(load);
  921. load->connect("pressed", this, "_load_layout");
  922. save_as = memnew(Button);
  923. save_as->set_text(TTR("Save As"));
  924. save_as->set_tooltip(TTR("Save this Bus Layout to a file."));
  925. top_hb->add_child(save_as);
  926. save_as->connect("pressed", this, "_save_as_layout");
  927. _default = memnew(Button);
  928. _default->set_text(TTR("Load Default"));
  929. _default->set_tooltip(TTR("Load the default Bus Layout."));
  930. top_hb->add_child(_default);
  931. _default->connect("pressed", this, "_load_default_layout");
  932. _new = memnew(Button);
  933. _new->set_text(TTR("Create"));
  934. _new->set_tooltip(TTR("Create a new Bus Layout."));
  935. top_hb->add_child(_new);
  936. _new->connect("pressed", this, "_new_layout");
  937. bus_scroll = memnew(ScrollContainer);
  938. bus_scroll->add_style_override("panel", memnew(StyleBoxEmpty));
  939. bus_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  940. bus_scroll->set_enable_h_scroll(true);
  941. bus_scroll->set_enable_v_scroll(false);
  942. add_child(bus_scroll);
  943. bus_hb = memnew(HBoxContainer);
  944. bus_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  945. bus_scroll->add_child(bus_hb);
  946. save_timer = memnew(Timer);
  947. save_timer->set_wait_time(0.8);
  948. save_timer->set_one_shot(true);
  949. add_child(save_timer);
  950. save_timer->connect("timeout", this, "_server_save");
  951. set_v_size_flags(SIZE_EXPAND_FILL);
  952. edited_path = "res://default_bus_layout.tres";
  953. file_dialog = memnew(EditorFileDialog);
  954. List<String> ext;
  955. ResourceLoader::get_recognized_extensions_for_type("AudioBusLayout", &ext);
  956. for (List<String>::Element *E = ext.front(); E; E = E->next()) {
  957. file_dialog->add_filter("*." + E->get() + "; Audio Bus Layout");
  958. }
  959. add_child(file_dialog);
  960. file_dialog->connect("file_selected", this, "_file_dialog_callback");
  961. set_process(true);
  962. }
  963. void EditorAudioBuses::open_layout(const String &p_path) {
  964. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  965. Ref<AudioBusLayout> state = ResourceLoader::load(p_path);
  966. if (state.is_null()) {
  967. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  968. return;
  969. }
  970. edited_path = p_path;
  971. file->set_text(p_path.get_file());
  972. AudioServer::get_singleton()->set_bus_layout(state);
  973. _update_buses();
  974. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  975. call_deferred("_select_layout");
  976. }
  977. void AudioBusesEditorPlugin::edit(Object *p_node) {
  978. if (Object::cast_to<AudioBusLayout>(p_node)) {
  979. String path = Object::cast_to<AudioBusLayout>(p_node)->get_path();
  980. if (path.is_resource_file()) {
  981. audio_bus_editor->open_layout(path);
  982. }
  983. }
  984. }
  985. bool AudioBusesEditorPlugin::handles(Object *p_node) const {
  986. return (Object::cast_to<AudioBusLayout>(p_node) != NULL);
  987. }
  988. void AudioBusesEditorPlugin::make_visible(bool p_visible) {
  989. }
  990. AudioBusesEditorPlugin::AudioBusesEditorPlugin(EditorAudioBuses *p_node) {
  991. audio_bus_editor = p_node;
  992. }
  993. AudioBusesEditorPlugin::~AudioBusesEditorPlugin() {
  994. }