editor_audio_buses.cpp 43 KB

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