editor_audio_buses.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /**************************************************************************/
  2. /* editor_audio_buses.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/filesystem_dock.h"
  40. #include "editor/gui/editor_file_dialog.h"
  41. #include "scene/gui/separator.h"
  42. #include "scene/resources/font.h"
  43. #include "servers/audio_server.h"
  44. void EditorAudioBus::_update_visible_channels() {
  45. int i = 0;
  46. for (; i < cc; i++) {
  47. if (!channel[i].vu_l->is_visible()) {
  48. channel[i].vu_l->show();
  49. }
  50. if (!channel[i].vu_r->is_visible()) {
  51. channel[i].vu_r->show();
  52. }
  53. }
  54. for (; i < CHANNELS_MAX; i++) {
  55. if (channel[i].vu_l->is_visible()) {
  56. channel[i].vu_l->hide();
  57. }
  58. if (channel[i].vu_r->is_visible()) {
  59. channel[i].vu_r->hide();
  60. }
  61. }
  62. }
  63. void EditorAudioBus::_notification(int p_what) {
  64. switch (p_what) {
  65. case NOTIFICATION_ENTER_TREE:
  66. case NOTIFICATION_THEME_CHANGED: {
  67. Ref<Texture2D> active_bus_texture = get_theme_icon(SNAME("BusVuActive"), SNAME("EditorIcons"));
  68. for (int i = 0; i < CHANNELS_MAX; i++) {
  69. channel[i].vu_l->set_under_texture(active_bus_texture);
  70. channel[i].vu_l->set_tint_under(Color(0.75, 0.75, 0.75));
  71. channel[i].vu_l->set_progress_texture(active_bus_texture);
  72. channel[i].vu_r->set_under_texture(active_bus_texture);
  73. channel[i].vu_r->set_tint_under(Color(0.75, 0.75, 0.75));
  74. channel[i].vu_r->set_progress_texture(active_bus_texture);
  75. channel[i].prev_active = true;
  76. }
  77. disabled_vu = get_theme_icon(SNAME("BusVuFrozen"), SNAME("EditorIcons"));
  78. Color solo_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(1.0, 0.89, 0.22) : Color(1.0, 0.92, 0.44);
  79. Color mute_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(1.0, 0.16, 0.16) : Color(1.0, 0.44, 0.44);
  80. Color bypass_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(0.13, 0.8, 1.0) : Color(0.44, 0.87, 1.0);
  81. solo->set_icon(get_theme_icon(SNAME("AudioBusSolo"), SNAME("EditorIcons")));
  82. solo->add_theme_color_override("icon_pressed_color", solo_color);
  83. mute->set_icon(get_theme_icon(SNAME("AudioBusMute"), SNAME("EditorIcons")));
  84. mute->add_theme_color_override("icon_pressed_color", mute_color);
  85. bypass->set_icon(get_theme_icon(SNAME("AudioBusBypass"), SNAME("EditorIcons")));
  86. bypass->add_theme_color_override("icon_pressed_color", bypass_color);
  87. bus_options->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  88. audio_value_preview_label->add_theme_color_override("font_color", get_theme_color(SNAME("font_color"), SNAME("TooltipLabel")));
  89. audio_value_preview_label->add_theme_color_override("font_shadow_color", get_theme_color(SNAME("font_shadow_color"), SNAME("TooltipLabel")));
  90. audio_value_preview_box->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("TooltipPanel")));
  91. for (int i = 0; i < effect_options->get_item_count(); i++) {
  92. String class_name = effect_options->get_item_metadata(i);
  93. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(class_name);
  94. effect_options->set_item_icon(i, icon);
  95. }
  96. } break;
  97. case NOTIFICATION_READY: {
  98. update_bus();
  99. set_process(true);
  100. } break;
  101. case NOTIFICATION_DRAW: {
  102. if (is_master) {
  103. draw_style_box(get_theme_stylebox(SNAME("disabled"), SNAME("Button")), Rect2(Vector2(), get_size()));
  104. } else if (has_focus()) {
  105. draw_style_box(get_theme_stylebox(SNAME("focus"), SNAME("Button")), Rect2(Vector2(), get_size()));
  106. } else {
  107. draw_style_box(get_theme_stylebox(SNAME("panel"), SNAME("TabContainer")), Rect2(Vector2(), get_size()));
  108. }
  109. if (get_index() != 0 && hovering_drop) {
  110. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  111. accent.a *= 0.7;
  112. draw_rect(Rect2(Point2(), get_size()), accent, false);
  113. }
  114. } break;
  115. case NOTIFICATION_PROCESS: {
  116. if (cc != AudioServer::get_singleton()->get_bus_channels(get_index())) {
  117. cc = AudioServer::get_singleton()->get_bus_channels(get_index());
  118. _update_visible_channels();
  119. }
  120. for (int i = 0; i < cc; i++) {
  121. float real_peak[2] = { -100, -100 };
  122. bool activity_found = false;
  123. if (AudioServer::get_singleton()->is_bus_channel_active(get_index(), i)) {
  124. activity_found = true;
  125. real_peak[0] = MAX(real_peak[0], AudioServer::get_singleton()->get_bus_peak_volume_left_db(get_index(), i));
  126. real_peak[1] = MAX(real_peak[1], AudioServer::get_singleton()->get_bus_peak_volume_right_db(get_index(), i));
  127. }
  128. if (real_peak[0] > channel[i].peak_l) {
  129. channel[i].peak_l = real_peak[0];
  130. } else {
  131. channel[i].peak_l -= get_process_delta_time() * 60.0;
  132. }
  133. if (real_peak[1] > channel[i].peak_r) {
  134. channel[i].peak_r = real_peak[1];
  135. } else {
  136. channel[i].peak_r -= get_process_delta_time() * 60.0;
  137. }
  138. channel[i].vu_l->set_value(channel[i].peak_l);
  139. channel[i].vu_r->set_value(channel[i].peak_r);
  140. if (activity_found != channel[i].prev_active) {
  141. if (activity_found) {
  142. channel[i].vu_l->set_over_texture(Ref<Texture2D>());
  143. channel[i].vu_r->set_over_texture(Ref<Texture2D>());
  144. } else {
  145. channel[i].vu_l->set_over_texture(disabled_vu);
  146. channel[i].vu_r->set_over_texture(disabled_vu);
  147. }
  148. channel[i].prev_active = activity_found;
  149. }
  150. }
  151. } break;
  152. case NOTIFICATION_VISIBILITY_CHANGED: {
  153. for (int i = 0; i < CHANNELS_MAX; i++) {
  154. channel[i].peak_l = -100;
  155. channel[i].peak_r = -100;
  156. channel[i].prev_active = true;
  157. }
  158. set_process(is_visible_in_tree());
  159. } break;
  160. case NOTIFICATION_MOUSE_EXIT:
  161. case NOTIFICATION_DRAG_END: {
  162. if (hovering_drop) {
  163. hovering_drop = false;
  164. queue_redraw();
  165. }
  166. } break;
  167. }
  168. }
  169. void EditorAudioBus::update_send() {
  170. send->clear();
  171. if (is_master) {
  172. send->set_disabled(true);
  173. send->set_text(TTR("Speakers"));
  174. } else {
  175. send->set_disabled(false);
  176. StringName current_send = AudioServer::get_singleton()->get_bus_send(get_index());
  177. int current_send_index = 0; //by default to master
  178. for (int i = 0; i < get_index(); i++) {
  179. StringName send_name = AudioServer::get_singleton()->get_bus_name(i);
  180. send->add_item(send_name);
  181. if (send_name == current_send) {
  182. current_send_index = i;
  183. }
  184. }
  185. send->select(current_send_index);
  186. }
  187. }
  188. void EditorAudioBus::update_bus() {
  189. if (updating_bus) {
  190. return;
  191. }
  192. updating_bus = true;
  193. int index = get_index();
  194. float db_value = AudioServer::get_singleton()->get_bus_volume_db(index);
  195. slider->set_value(_scaled_db_to_normalized_volume(db_value));
  196. track_name->set_text(AudioServer::get_singleton()->get_bus_name(index));
  197. if (is_master) {
  198. track_name->set_editable(false);
  199. }
  200. solo->set_pressed(AudioServer::get_singleton()->is_bus_solo(index));
  201. mute->set_pressed(AudioServer::get_singleton()->is_bus_mute(index));
  202. bypass->set_pressed(AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  203. // effects..
  204. effects->clear();
  205. TreeItem *root = effects->create_item();
  206. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  207. Ref<AudioEffect> afx = AudioServer::get_singleton()->get_bus_effect(index, i);
  208. TreeItem *fx = effects->create_item(root);
  209. fx->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  210. fx->set_editable(0, true);
  211. fx->set_checked(0, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  212. fx->set_text(0, afx->get_name());
  213. fx->set_metadata(0, i);
  214. }
  215. TreeItem *add = effects->create_item(root);
  216. add->set_cell_mode(0, TreeItem::CELL_MODE_CUSTOM);
  217. add->set_editable(0, true);
  218. add->set_selectable(0, false);
  219. add->set_text(0, TTR("Add Effect"));
  220. update_send();
  221. updating_bus = false;
  222. }
  223. void EditorAudioBus::_name_changed(const String &p_new_name) {
  224. if (updating_bus) {
  225. return;
  226. }
  227. updating_bus = true;
  228. track_name->release_focus();
  229. if (p_new_name == AudioServer::get_singleton()->get_bus_name(get_index())) {
  230. updating_bus = false;
  231. return;
  232. }
  233. String attempt = p_new_name;
  234. int attempts = 1;
  235. while (true) {
  236. bool name_free = true;
  237. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  238. if (AudioServer::get_singleton()->get_bus_name(i) == attempt) {
  239. name_free = false;
  240. break;
  241. }
  242. }
  243. if (name_free) {
  244. break;
  245. }
  246. attempts++;
  247. attempt = p_new_name + " " + itos(attempts);
  248. }
  249. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  250. StringName current = AudioServer::get_singleton()->get_bus_name(get_index());
  251. ur->create_action(TTR("Rename Audio Bus"));
  252. ur->add_do_method(buses, "_set_renaming_buses", true);
  253. ur->add_undo_method(buses, "_set_renaming_buses", true);
  254. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", get_index(), attempt);
  255. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", get_index(), current);
  256. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  257. if (AudioServer::get_singleton()->get_bus_send(i) == current) {
  258. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", i, attempt);
  259. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", i, current);
  260. }
  261. }
  262. ur->add_do_method(buses, "_update_bus", get_index());
  263. ur->add_undo_method(buses, "_update_bus", get_index());
  264. ur->add_do_method(buses, "_update_sends");
  265. ur->add_undo_method(buses, "_update_sends");
  266. ur->add_do_method(buses, "_set_renaming_buses", false);
  267. ur->add_undo_method(buses, "_set_renaming_buses", false);
  268. ur->commit_action();
  269. updating_bus = false;
  270. }
  271. void EditorAudioBus::_volume_changed(float p_normalized) {
  272. if (updating_bus) {
  273. return;
  274. }
  275. updating_bus = true;
  276. const float p_db = this->_normalized_volume_to_scaled_db(p_normalized);
  277. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  278. // Snap the value when holding Ctrl for easier editing.
  279. // To do so, it needs to be converted back to normalized volume (as the slider uses that unit).
  280. slider->set_value(_scaled_db_to_normalized_volume(Math::round(p_db)));
  281. }
  282. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  283. ur->create_action(TTR("Change Audio Bus Volume"), UndoRedo::MERGE_ENDS);
  284. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), p_db);
  285. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), AudioServer::get_singleton()->get_bus_volume_db(get_index()));
  286. ur->add_do_method(buses, "_update_bus", get_index());
  287. ur->add_undo_method(buses, "_update_bus", get_index());
  288. ur->commit_action();
  289. updating_bus = false;
  290. }
  291. float EditorAudioBus::_normalized_volume_to_scaled_db(float normalized) {
  292. /* There are three different formulas for the conversion from normalized
  293. * values to relative decibal values.
  294. * One formula is an exponential graph which intends to counteract
  295. * the logarithmic nature of human hearing. This is an approximation
  296. * of the behavior of a 'logarithmic potentiometer' found on most
  297. * musical instruments and also emulated in popular software.
  298. * The other two equations are hand-tuned linear tapers that intend to
  299. * try to ease the exponential equation in areas where it makes sense.*/
  300. if (normalized > 0.6f) {
  301. return 22.22f * normalized - 16.2f;
  302. } else if (normalized < 0.05f) {
  303. return 830.72 * normalized - 80.0f;
  304. } else {
  305. return 45.0f * Math::pow(normalized - 1.0, 3);
  306. }
  307. }
  308. float EditorAudioBus::_scaled_db_to_normalized_volume(float db) {
  309. /* Inversion of equations found in _normalized_volume_to_scaled_db.
  310. * IMPORTANT: If one function changes, the other much change to reflect it. */
  311. if (db > -2.88) {
  312. return (db + 16.2f) / 22.22f;
  313. } else if (db < -38.602f) {
  314. return (db + 80.00f) / 830.72f;
  315. } else {
  316. if (db < 0.0) {
  317. /* To accommodate for NaN on negative numbers for root, we will mirror the
  318. * results of the positive db range in order to get the desired numerical
  319. * value on the negative side. */
  320. float positive_x = Math::pow(Math::abs(db) / 45.0f, 1.0f / 3.0f) + 1.0f;
  321. Vector2 translation = Vector2(1.0f, 0.0f) - Vector2(positive_x, Math::abs(db));
  322. Vector2 reflected_position = Vector2(1.0, 0.0f) + translation;
  323. return reflected_position.x;
  324. } else {
  325. return Math::pow(db / 45.0f, 1.0f / 3.0f) + 1.0f;
  326. }
  327. }
  328. }
  329. void EditorAudioBus::_show_value(float slider_value) {
  330. float db;
  331. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  332. // Display the correct (snapped) value when holding Ctrl
  333. db = Math::round(_normalized_volume_to_scaled_db(slider_value));
  334. } else {
  335. db = _normalized_volume_to_scaled_db(slider_value);
  336. }
  337. String text;
  338. if (Math::is_zero_approx(Math::snapped(db, 0.1))) {
  339. // Prevent displaying `-0.0 dB` and show ` 0.0 dB` instead.
  340. // The leading space makes the text visually line up with its positive/negative counterparts.
  341. text = " 0.0 dB";
  342. } else {
  343. // Show an explicit `+` sign if positive.
  344. text = vformat("%+.1f dB", db);
  345. }
  346. // Also set the preview text as a standard Control tooltip.
  347. // This way, it can be seen when the slider is merely hovered (instead of dragged).
  348. slider->set_tooltip_text(text);
  349. audio_value_preview_label->set_text(text);
  350. const Vector2 slider_size = slider->get_size();
  351. const Vector2 slider_position = slider->get_global_position();
  352. const float vert_padding = 10.0f;
  353. const Vector2 box_position = Vector2(slider_size.x, (slider_size.y - vert_padding) * (1.0f - slider->get_value()) - vert_padding);
  354. audio_value_preview_box->set_position(slider_position + box_position);
  355. audio_value_preview_box->set_size(audio_value_preview_label->get_size());
  356. if (slider->has_focus() && !audio_value_preview_box->is_visible()) {
  357. audio_value_preview_box->show();
  358. }
  359. preview_timer->start();
  360. }
  361. void EditorAudioBus::_hide_value_preview() {
  362. audio_value_preview_box->hide();
  363. }
  364. void EditorAudioBus::_solo_toggled() {
  365. updating_bus = true;
  366. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  367. ur->create_action(TTR("Toggle Audio Bus Solo"));
  368. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), solo->is_pressed());
  369. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), AudioServer::get_singleton()->is_bus_solo(get_index()));
  370. ur->add_do_method(buses, "_update_bus", get_index());
  371. ur->add_undo_method(buses, "_update_bus", get_index());
  372. ur->commit_action();
  373. updating_bus = false;
  374. }
  375. void EditorAudioBus::_mute_toggled() {
  376. updating_bus = true;
  377. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  378. ur->create_action(TTR("Toggle Audio Bus Mute"));
  379. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), mute->is_pressed());
  380. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), AudioServer::get_singleton()->is_bus_mute(get_index()));
  381. ur->add_do_method(buses, "_update_bus", get_index());
  382. ur->add_undo_method(buses, "_update_bus", get_index());
  383. ur->commit_action();
  384. updating_bus = false;
  385. }
  386. void EditorAudioBus::_bypass_toggled() {
  387. updating_bus = true;
  388. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  389. ur->create_action(TTR("Toggle Audio Bus Bypass Effects"));
  390. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), bypass->is_pressed());
  391. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), AudioServer::get_singleton()->is_bus_bypassing_effects(get_index()));
  392. ur->add_do_method(buses, "_update_bus", get_index());
  393. ur->add_undo_method(buses, "_update_bus", get_index());
  394. ur->commit_action();
  395. updating_bus = false;
  396. }
  397. void EditorAudioBus::_send_selected(int p_which) {
  398. updating_bus = true;
  399. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  400. ur->create_action(TTR("Select Audio Bus Send"));
  401. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", get_index(), send->get_item_text(p_which));
  402. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", get_index(), AudioServer::get_singleton()->get_bus_send(get_index()));
  403. ur->add_do_method(buses, "_update_bus", get_index());
  404. ur->add_undo_method(buses, "_update_bus", get_index());
  405. ur->commit_action();
  406. updating_bus = false;
  407. }
  408. void EditorAudioBus::_effect_selected() {
  409. TreeItem *effect = effects->get_selected();
  410. if (!effect) {
  411. return;
  412. }
  413. updating_bus = true;
  414. if (effect->get_metadata(0) != Variant()) {
  415. int index = effect->get_metadata(0);
  416. Ref<AudioEffect> effect2 = AudioServer::get_singleton()->get_bus_effect(get_index(), index);
  417. if (effect2.is_valid()) {
  418. EditorNode::get_singleton()->push_item(effect2.ptr());
  419. }
  420. }
  421. updating_bus = false;
  422. }
  423. void EditorAudioBus::_effect_edited() {
  424. if (updating_bus) {
  425. return;
  426. }
  427. TreeItem *effect = effects->get_edited();
  428. if (!effect) {
  429. return;
  430. }
  431. if (effect->get_metadata(0) == Variant()) {
  432. Rect2 area = effects->get_item_rect(effect);
  433. effect_options->set_position(effects->get_screen_position() + area.position + Vector2(0, area.size.y));
  434. effect_options->reset_size();
  435. effect_options->popup();
  436. //add effect
  437. } else {
  438. int index = effect->get_metadata(0);
  439. updating_bus = true;
  440. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  441. ur->create_action(TTR("Select Audio Bus Send"));
  442. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, effect->is_checked(0));
  443. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  444. ur->add_do_method(buses, "_update_bus", get_index());
  445. ur->add_undo_method(buses, "_update_bus", get_index());
  446. ur->commit_action();
  447. updating_bus = false;
  448. }
  449. }
  450. void EditorAudioBus::_effect_add(int p_which) {
  451. if (updating_bus) {
  452. return;
  453. }
  454. StringName name = effect_options->get_item_metadata(p_which);
  455. Object *fx = ClassDB::instantiate(name);
  456. ERR_FAIL_COND(!fx);
  457. AudioEffect *afx = Object::cast_to<AudioEffect>(fx);
  458. ERR_FAIL_COND(!afx);
  459. Ref<AudioEffect> afxr = Ref<AudioEffect>(afx);
  460. afxr->set_name(effect_options->get_item_text(p_which));
  461. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  462. ur->create_action(TTR("Add Audio Bus Effect"));
  463. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), afxr, -1);
  464. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect_count(get_index()));
  465. ur->add_do_method(buses, "_update_bus", get_index());
  466. ur->add_undo_method(buses, "_update_bus", get_index());
  467. ur->commit_action();
  468. }
  469. void EditorAudioBus::gui_input(const Ref<InputEvent> &p_event) {
  470. ERR_FAIL_COND(p_event.is_null());
  471. Ref<InputEventMouseButton> mb = p_event;
  472. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  473. bus_popup->set_position(get_screen_position() + mb->get_position());
  474. bus_popup->reset_size();
  475. bus_popup->popup();
  476. }
  477. }
  478. void EditorAudioBus::_effects_gui_input(Ref<InputEvent> p_event) {
  479. Ref<InputEventKey> k = p_event;
  480. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == Key::KEY_DELETE) {
  481. TreeItem *current_effect = effects->get_selected();
  482. if (current_effect && current_effect->get_metadata(0).get_type() == Variant::INT) {
  483. _delete_effect_pressed(0);
  484. accept_event();
  485. }
  486. }
  487. }
  488. void EditorAudioBus::_bus_popup_pressed(int p_option) {
  489. if (p_option == 2) {
  490. // Reset volume
  491. emit_signal(SNAME("vol_reset_request"));
  492. } else if (p_option == 1) {
  493. emit_signal(SNAME("delete_request"));
  494. } else if (p_option == 0) {
  495. //duplicate
  496. emit_signal(SNAME("duplicate_request"), get_index());
  497. }
  498. }
  499. Variant EditorAudioBus::get_drag_data(const Point2 &p_point) {
  500. if (get_index() == 0) {
  501. return Variant();
  502. }
  503. Control *c = memnew(Control);
  504. Panel *p = memnew(Panel);
  505. c->add_child(p);
  506. p->set_modulate(Color(1, 1, 1, 0.7));
  507. p->add_theme_style_override("panel", get_theme_stylebox(SNAME("focus"), SNAME("Button")));
  508. p->set_size(get_size());
  509. p->set_position(-p_point);
  510. set_drag_preview(c);
  511. Dictionary d;
  512. d["type"] = "move_audio_bus";
  513. d["index"] = get_index();
  514. if (get_index() < AudioServer::get_singleton()->get_bus_count() - 1) {
  515. emit_signal(SNAME("drop_end_request"));
  516. }
  517. return d;
  518. }
  519. bool EditorAudioBus::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  520. if (get_index() == 0) {
  521. return false;
  522. }
  523. Dictionary d = p_data;
  524. if (d.has("type") && String(d["type"]) == "move_audio_bus" && (int)d["index"] != get_index()) {
  525. hovering_drop = true;
  526. return true;
  527. }
  528. return false;
  529. }
  530. void EditorAudioBus::drop_data(const Point2 &p_point, const Variant &p_data) {
  531. Dictionary d = p_data;
  532. emit_signal(SNAME("dropped"), d["index"], get_index());
  533. }
  534. Variant EditorAudioBus::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  535. TreeItem *item = effects->get_item_at_position(p_point);
  536. if (!item) {
  537. return Variant();
  538. }
  539. Variant md = item->get_metadata(0);
  540. if (md.get_type() == Variant::INT) {
  541. Dictionary fxd;
  542. fxd["type"] = "audio_bus_effect";
  543. fxd["bus"] = get_index();
  544. fxd["effect"] = md;
  545. Label *l = memnew(Label);
  546. l->set_text(item->get_text(0));
  547. effects->set_drag_preview(l);
  548. return fxd;
  549. }
  550. return Variant();
  551. }
  552. bool EditorAudioBus::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  553. Dictionary d = p_data;
  554. if (!d.has("type") || String(d["type"]) != "audio_bus_effect") {
  555. return false;
  556. }
  557. TreeItem *item = effects->get_item_at_position(p_point);
  558. if (!item) {
  559. return false;
  560. }
  561. effects->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  562. return true;
  563. }
  564. void EditorAudioBus::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  565. Dictionary d = p_data;
  566. TreeItem *item = effects->get_item_at_position(p_point);
  567. if (!item) {
  568. return;
  569. }
  570. int pos = effects->get_drop_section_at_position(p_point);
  571. Variant md = item->get_metadata(0);
  572. int paste_at;
  573. int bus = d["bus"];
  574. int effect = d["effect"];
  575. if (md.get_type() == Variant::INT) {
  576. paste_at = md;
  577. if (pos > 0) {
  578. paste_at++;
  579. }
  580. if (bus == get_index() && paste_at > effect) {
  581. paste_at--;
  582. }
  583. } else {
  584. paste_at = -1;
  585. }
  586. bool enabled = AudioServer::get_singleton()->is_bus_effect_enabled(bus, effect);
  587. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  588. ur->create_action(TTR("Move Bus Effect"));
  589. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", bus, effect);
  590. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(bus, effect), paste_at);
  591. if (paste_at == -1) {
  592. paste_at = AudioServer::get_singleton()->get_bus_effect_count(get_index());
  593. if (bus == get_index()) {
  594. paste_at--;
  595. }
  596. }
  597. if (!enabled) {
  598. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), paste_at, false);
  599. }
  600. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), paste_at);
  601. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", bus, AudioServer::get_singleton()->get_bus_effect(bus, effect), effect);
  602. if (!enabled) {
  603. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", bus, effect, false);
  604. }
  605. ur->add_do_method(buses, "_update_bus", get_index());
  606. ur->add_undo_method(buses, "_update_bus", get_index());
  607. if (get_index() != bus) {
  608. ur->add_do_method(buses, "_update_bus", bus);
  609. ur->add_undo_method(buses, "_update_bus", bus);
  610. }
  611. ur->commit_action();
  612. }
  613. void EditorAudioBus::_delete_effect_pressed(int p_option) {
  614. TreeItem *item = effects->get_selected();
  615. if (!item) {
  616. return;
  617. }
  618. if (item->get_metadata(0).get_type() != Variant::INT) {
  619. return;
  620. }
  621. int index = item->get_metadata(0);
  622. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  623. ur->create_action(TTR("Delete Bus Effect"));
  624. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), index);
  625. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(get_index(), index), index);
  626. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  627. ur->add_do_method(buses, "_update_bus", get_index());
  628. ur->add_undo_method(buses, "_update_bus", get_index());
  629. ur->commit_action();
  630. }
  631. void EditorAudioBus::_effect_rmb(const Vector2 &p_pos, MouseButton p_button) {
  632. if (p_button != MouseButton::RIGHT) {
  633. return;
  634. }
  635. TreeItem *item = effects->get_selected();
  636. if (!item) {
  637. return;
  638. }
  639. if (item->get_metadata(0).get_type() != Variant::INT) {
  640. return;
  641. }
  642. delete_effect_popup->set_position(get_screen_position() + get_local_mouse_position());
  643. delete_effect_popup->reset_size();
  644. delete_effect_popup->popup();
  645. }
  646. void EditorAudioBus::_bind_methods() {
  647. ClassDB::bind_method("update_bus", &EditorAudioBus::update_bus);
  648. ClassDB::bind_method("update_send", &EditorAudioBus::update_send);
  649. ADD_SIGNAL(MethodInfo("duplicate_request"));
  650. ADD_SIGNAL(MethodInfo("delete_request"));
  651. ADD_SIGNAL(MethodInfo("vol_reset_request"));
  652. ADD_SIGNAL(MethodInfo("drop_end_request"));
  653. ADD_SIGNAL(MethodInfo("dropped"));
  654. }
  655. EditorAudioBus::EditorAudioBus(EditorAudioBuses *p_buses, bool p_is_master) {
  656. buses = p_buses;
  657. is_master = p_is_master;
  658. set_tooltip_text(TTR("Drag & drop to rearrange."));
  659. VBoxContainer *vb = memnew(VBoxContainer);
  660. add_child(vb);
  661. set_v_size_flags(SIZE_EXPAND_FILL);
  662. track_name = memnew(LineEdit);
  663. track_name->connect("text_submitted", callable_mp(this, &EditorAudioBus::_name_changed));
  664. track_name->connect("focus_exited", callable_mp(this, &EditorAudioBus::_name_focus_exit));
  665. vb->add_child(track_name);
  666. HBoxContainer *hbc = memnew(HBoxContainer);
  667. vb->add_child(hbc);
  668. solo = memnew(Button);
  669. solo->set_flat(true);
  670. solo->set_toggle_mode(true);
  671. solo->set_tooltip_text(TTR("Solo"));
  672. solo->set_focus_mode(FOCUS_NONE);
  673. solo->connect("pressed", callable_mp(this, &EditorAudioBus::_solo_toggled));
  674. hbc->add_child(solo);
  675. mute = memnew(Button);
  676. mute->set_flat(true);
  677. mute->set_toggle_mode(true);
  678. mute->set_tooltip_text(TTR("Mute"));
  679. mute->set_focus_mode(FOCUS_NONE);
  680. mute->connect("pressed", callable_mp(this, &EditorAudioBus::_mute_toggled));
  681. hbc->add_child(mute);
  682. bypass = memnew(Button);
  683. bypass->set_flat(true);
  684. bypass->set_toggle_mode(true);
  685. bypass->set_tooltip_text(TTR("Bypass"));
  686. bypass->set_focus_mode(FOCUS_NONE);
  687. bypass->connect("pressed", callable_mp(this, &EditorAudioBus::_bypass_toggled));
  688. hbc->add_child(bypass);
  689. hbc->add_spacer();
  690. Ref<StyleBoxEmpty> sbempty = memnew(StyleBoxEmpty);
  691. for (int i = 0; i < hbc->get_child_count(); i++) {
  692. Control *child = Object::cast_to<Control>(hbc->get_child(i));
  693. child->add_theme_style_override("normal", sbempty);
  694. child->add_theme_style_override("hover", sbempty);
  695. child->add_theme_style_override("focus", sbempty);
  696. child->add_theme_style_override("pressed", sbempty);
  697. }
  698. HSeparator *separator = memnew(HSeparator);
  699. separator->set_mouse_filter(MOUSE_FILTER_PASS);
  700. vb->add_child(separator);
  701. HBoxContainer *hb = memnew(HBoxContainer);
  702. vb->add_child(hb);
  703. slider = memnew(VSlider);
  704. slider->set_min(0.0);
  705. slider->set_max(1.0);
  706. slider->set_step(0.0001);
  707. slider->set_clip_contents(false);
  708. audio_value_preview_box = memnew(Panel);
  709. slider->add_child(audio_value_preview_box);
  710. audio_value_preview_box->set_as_top_level(true);
  711. audio_value_preview_box->set_mouse_filter(MOUSE_FILTER_PASS);
  712. audio_value_preview_box->hide();
  713. HBoxContainer *audioprev_hbc = memnew(HBoxContainer);
  714. audioprev_hbc->set_v_size_flags(SIZE_EXPAND_FILL);
  715. audioprev_hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  716. audio_value_preview_box->add_child(audioprev_hbc);
  717. audio_value_preview_label = memnew(Label);
  718. audio_value_preview_label->set_v_size_flags(SIZE_EXPAND_FILL);
  719. audio_value_preview_label->set_h_size_flags(SIZE_EXPAND_FILL);
  720. audio_value_preview_label->set_mouse_filter(MOUSE_FILTER_PASS);
  721. audioprev_hbc->add_child(audio_value_preview_label);
  722. preview_timer = memnew(Timer);
  723. preview_timer->set_wait_time(0.8f);
  724. preview_timer->set_one_shot(true);
  725. add_child(preview_timer);
  726. slider->connect("value_changed", callable_mp(this, &EditorAudioBus::_volume_changed));
  727. slider->connect("value_changed", callable_mp(this, &EditorAudioBus::_show_value));
  728. preview_timer->connect("timeout", callable_mp(this, &EditorAudioBus::_hide_value_preview));
  729. hb->add_child(slider);
  730. cc = 0;
  731. for (int i = 0; i < CHANNELS_MAX; i++) {
  732. channel[i].vu_l = memnew(TextureProgressBar);
  733. channel[i].vu_l->set_fill_mode(TextureProgressBar::FILL_BOTTOM_TO_TOP);
  734. hb->add_child(channel[i].vu_l);
  735. channel[i].vu_l->set_min(-80);
  736. channel[i].vu_l->set_max(24);
  737. channel[i].vu_l->set_step(0.1);
  738. channel[i].vu_r = memnew(TextureProgressBar);
  739. channel[i].vu_r->set_fill_mode(TextureProgressBar::FILL_BOTTOM_TO_TOP);
  740. hb->add_child(channel[i].vu_r);
  741. channel[i].vu_r->set_min(-80);
  742. channel[i].vu_r->set_max(24);
  743. channel[i].vu_r->set_step(0.1);
  744. channel[i].peak_l = 0.0f;
  745. channel[i].peak_r = 0.0f;
  746. }
  747. EditorAudioMeterNotches *scale = memnew(EditorAudioMeterNotches);
  748. for (float db = 6.0f; db >= -80.0f; db -= 6.0f) {
  749. bool renderNotch = (db >= -6.0f || db == -24.0f || db == -72.0f);
  750. scale->add_notch(_scaled_db_to_normalized_volume(db), db, renderNotch);
  751. }
  752. scale->set_mouse_filter(MOUSE_FILTER_PASS);
  753. hb->add_child(scale);
  754. effects = memnew(Tree);
  755. effects->set_hide_root(true);
  756. effects->set_custom_minimum_size(Size2(0, 80) * EDSCALE);
  757. effects->set_hide_folding(true);
  758. effects->set_v_size_flags(SIZE_EXPAND_FILL);
  759. vb->add_child(effects);
  760. effects->connect("item_edited", callable_mp(this, &EditorAudioBus::_effect_edited));
  761. effects->connect("cell_selected", callable_mp(this, &EditorAudioBus::_effect_selected));
  762. effects->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  763. SET_DRAG_FORWARDING_GCD(effects, EditorAudioBus);
  764. effects->connect("item_mouse_selected", callable_mp(this, &EditorAudioBus::_effect_rmb));
  765. effects->set_allow_rmb_select(true);
  766. effects->set_focus_mode(FOCUS_CLICK);
  767. effects->set_allow_reselect(true);
  768. effects->connect("gui_input", callable_mp(this, &EditorAudioBus::_effects_gui_input));
  769. send = memnew(OptionButton);
  770. send->set_clip_text(true);
  771. send->connect("item_selected", callable_mp(this, &EditorAudioBus::_send_selected));
  772. vb->add_child(send);
  773. set_focus_mode(FOCUS_CLICK);
  774. effect_options = memnew(PopupMenu);
  775. effect_options->connect("index_pressed", callable_mp(this, &EditorAudioBus::_effect_add));
  776. add_child(effect_options);
  777. List<StringName> effect_list;
  778. ClassDB::get_inheriters_from_class("AudioEffect", &effect_list);
  779. effect_list.sort_custom<StringName::AlphCompare>();
  780. for (const StringName &E : effect_list) {
  781. if (!ClassDB::can_instantiate(E) || ClassDB::is_virtual(E)) {
  782. continue;
  783. }
  784. String name = E.operator String().replace("AudioEffect", "");
  785. effect_options->add_item(name);
  786. effect_options->set_item_metadata(-1, E);
  787. }
  788. bus_options = memnew(MenuButton);
  789. bus_options->set_shortcut_context(this);
  790. bus_options->set_h_size_flags(SIZE_SHRINK_END);
  791. bus_options->set_anchor(SIDE_RIGHT, 0.0);
  792. bus_options->set_tooltip_text(TTR("Bus Options"));
  793. hbc->add_child(bus_options);
  794. bus_popup = bus_options->get_popup();
  795. bus_popup->add_shortcut(ED_SHORTCUT("audio_bus_editor/duplicate_selected_bus", TTR("Duplicate Bus"), KeyModifierMask::CMD_OR_CTRL | Key::D));
  796. bus_popup->add_shortcut(ED_SHORTCUT("audio_bus_editor/delete_selected_bus", TTR("Delete Bus"), Key::KEY_DELETE));
  797. bus_popup->set_item_disabled(1, is_master);
  798. bus_popup->add_item(TTR("Reset Volume"));
  799. bus_popup->connect("index_pressed", callable_mp(this, &EditorAudioBus::_bus_popup_pressed));
  800. delete_effect_popup = memnew(PopupMenu);
  801. delete_effect_popup->add_item(TTR("Delete Effect"));
  802. add_child(delete_effect_popup);
  803. delete_effect_popup->connect("index_pressed", callable_mp(this, &EditorAudioBus::_delete_effect_pressed));
  804. }
  805. void EditorAudioBusDrop::_notification(int p_what) {
  806. switch (p_what) {
  807. case NOTIFICATION_DRAW: {
  808. draw_style_box(get_theme_stylebox(SNAME("normal"), SNAME("Button")), Rect2(Vector2(), get_size()));
  809. if (hovering_drop) {
  810. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  811. accent.a *= 0.7;
  812. draw_rect(Rect2(Point2(), get_size()), accent, false);
  813. }
  814. } break;
  815. case NOTIFICATION_MOUSE_ENTER: {
  816. if (!hovering_drop) {
  817. hovering_drop = true;
  818. queue_redraw();
  819. }
  820. } break;
  821. case NOTIFICATION_MOUSE_EXIT:
  822. case NOTIFICATION_DRAG_END: {
  823. if (hovering_drop) {
  824. hovering_drop = false;
  825. queue_redraw();
  826. }
  827. } break;
  828. }
  829. }
  830. bool EditorAudioBusDrop::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  831. Dictionary d = p_data;
  832. return (d.has("type") && String(d["type"]) == "move_audio_bus");
  833. }
  834. void EditorAudioBusDrop::drop_data(const Point2 &p_point, const Variant &p_data) {
  835. Dictionary d = p_data;
  836. emit_signal(SNAME("dropped"), d["index"], AudioServer::get_singleton()->get_bus_count());
  837. }
  838. void EditorAudioBusDrop::_bind_methods() {
  839. ADD_SIGNAL(MethodInfo("dropped"));
  840. }
  841. EditorAudioBusDrop::EditorAudioBusDrop() {
  842. }
  843. void EditorAudioBuses::_set_renaming_buses(bool p_renaming) {
  844. renaming_buses = p_renaming;
  845. }
  846. void EditorAudioBuses::_update_buses() {
  847. if (renaming_buses) {
  848. // This case will be handled more gracefully, no need to trigger a full rebuild.
  849. // This is possibly a mistake in the AudioServer, which fires bus_layout_changed
  850. // on a rename. This may not be intended, but no way to tell at the moment.
  851. return;
  852. }
  853. for (int i = bus_hb->get_child_count() - 1; i >= 0; i--) {
  854. EditorAudioBus *audio_bus = Object::cast_to<EditorAudioBus>(bus_hb->get_child(i));
  855. if (audio_bus) {
  856. bus_hb->remove_child(audio_bus);
  857. audio_bus->queue_free();
  858. }
  859. }
  860. if (drop_end) {
  861. bus_hb->remove_child(drop_end);
  862. drop_end->queue_free();
  863. drop_end = nullptr;
  864. }
  865. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  866. bool is_master = (i == 0);
  867. EditorAudioBus *audio_bus = memnew(EditorAudioBus(this, is_master));
  868. bus_hb->add_child(audio_bus);
  869. audio_bus->connect("delete_request", callable_mp(this, &EditorAudioBuses::_delete_bus).bind(audio_bus), CONNECT_DEFERRED);
  870. audio_bus->connect("duplicate_request", callable_mp(this, &EditorAudioBuses::_duplicate_bus), CONNECT_DEFERRED);
  871. audio_bus->connect("vol_reset_request", callable_mp(this, &EditorAudioBuses::_reset_bus_volume).bind(audio_bus), CONNECT_DEFERRED);
  872. audio_bus->connect("drop_end_request", callable_mp(this, &EditorAudioBuses::_request_drop_end));
  873. audio_bus->connect("dropped", callable_mp(this, &EditorAudioBuses::_drop_at_index), CONNECT_DEFERRED);
  874. }
  875. }
  876. EditorAudioBuses *EditorAudioBuses::register_editor() {
  877. EditorAudioBuses *audio_buses = memnew(EditorAudioBuses);
  878. EditorNode::get_singleton()->add_bottom_panel_item(TTR("Audio"), audio_buses);
  879. return audio_buses;
  880. }
  881. void EditorAudioBuses::_notification(int p_what) {
  882. switch (p_what) {
  883. case NOTIFICATION_ENTER_TREE:
  884. case NOTIFICATION_THEME_CHANGED: {
  885. bus_scroll->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  886. } break;
  887. case NOTIFICATION_READY: {
  888. _update_buses();
  889. } break;
  890. case NOTIFICATION_DRAG_END: {
  891. if (drop_end) {
  892. bus_hb->remove_child(drop_end);
  893. drop_end->queue_free();
  894. drop_end = nullptr;
  895. }
  896. } break;
  897. case NOTIFICATION_PROCESS: {
  898. // Check if anything was edited.
  899. bool edited = AudioServer::get_singleton()->is_edited();
  900. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  901. for (int j = 0; j < AudioServer::get_singleton()->get_bus_effect_count(i); j++) {
  902. Ref<AudioEffect> effect = AudioServer::get_singleton()->get_bus_effect(i, j);
  903. if (effect->is_edited()) {
  904. edited = true;
  905. effect->set_edited(false);
  906. }
  907. }
  908. }
  909. AudioServer::get_singleton()->set_edited(false);
  910. if (edited) {
  911. save_timer->start();
  912. }
  913. } break;
  914. }
  915. }
  916. void EditorAudioBuses::_add_bus() {
  917. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  918. ur->create_action(TTR("Add Audio Bus"));
  919. ur->add_do_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count() + 1);
  920. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count());
  921. ur->add_do_method(this, "_update_buses");
  922. ur->add_undo_method(this, "_update_buses");
  923. ur->commit_action();
  924. }
  925. void EditorAudioBuses::_update_bus(int p_index) {
  926. if (p_index >= bus_hb->get_child_count()) {
  927. return;
  928. }
  929. bus_hb->get_child(p_index)->call("update_bus");
  930. }
  931. void EditorAudioBuses::_update_sends() {
  932. for (int i = 0; i < bus_hb->get_child_count(); i++) {
  933. bus_hb->get_child(i)->call("update_send");
  934. }
  935. }
  936. void EditorAudioBuses::_delete_bus(Object *p_which) {
  937. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  938. int index = bus->get_index();
  939. if (index == 0) {
  940. EditorNode::get_singleton()->show_warning(TTR("Master bus can't be deleted!"));
  941. return;
  942. }
  943. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  944. ur->create_action(TTR("Delete Audio Bus"));
  945. ur->add_do_method(AudioServer::get_singleton(), "remove_bus", index);
  946. ur->add_undo_method(AudioServer::get_singleton(), "add_bus", index);
  947. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", index, AudioServer::get_singleton()->get_bus_name(index));
  948. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  949. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", index, AudioServer::get_singleton()->get_bus_send(index));
  950. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", index, AudioServer::get_singleton()->is_bus_solo(index));
  951. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", index, AudioServer::get_singleton()->is_bus_mute(index));
  952. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", index, AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  953. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  954. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", index, AudioServer::get_singleton()->get_bus_effect(index, i));
  955. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", index, i, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  956. }
  957. ur->add_do_method(this, "_update_buses");
  958. ur->add_undo_method(this, "_update_buses");
  959. ur->commit_action();
  960. }
  961. void EditorAudioBuses::_duplicate_bus(int p_which) {
  962. int add_at_pos = p_which + 1;
  963. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  964. ur->create_action(TTR("Duplicate Audio Bus"));
  965. ur->add_do_method(AudioServer::get_singleton(), "add_bus", add_at_pos);
  966. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", add_at_pos, AudioServer::get_singleton()->get_bus_name(p_which) + " Copy");
  967. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", add_at_pos, AudioServer::get_singleton()->get_bus_volume_db(p_which));
  968. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", add_at_pos, AudioServer::get_singleton()->get_bus_send(p_which));
  969. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", add_at_pos, AudioServer::get_singleton()->is_bus_solo(p_which));
  970. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", add_at_pos, AudioServer::get_singleton()->is_bus_mute(p_which));
  971. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", add_at_pos, AudioServer::get_singleton()->is_bus_bypassing_effects(p_which));
  972. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(p_which); i++) {
  973. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", add_at_pos, AudioServer::get_singleton()->get_bus_effect(p_which, i));
  974. 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));
  975. }
  976. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus", add_at_pos);
  977. ur->add_do_method(this, "_update_buses");
  978. ur->add_undo_method(this, "_update_buses");
  979. ur->commit_action();
  980. }
  981. void EditorAudioBuses::_reset_bus_volume(Object *p_which) {
  982. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  983. int index = bus->get_index();
  984. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  985. ur->create_action(TTR("Reset Bus Volume"));
  986. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", index, 0.f);
  987. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  988. ur->add_do_method(this, "_update_buses");
  989. ur->add_undo_method(this, "_update_buses");
  990. ur->commit_action();
  991. }
  992. void EditorAudioBuses::_request_drop_end() {
  993. if (!drop_end && bus_hb->get_child_count()) {
  994. drop_end = memnew(EditorAudioBusDrop);
  995. bus_hb->add_child(drop_end);
  996. drop_end->set_custom_minimum_size(Object::cast_to<Control>(bus_hb->get_child(0))->get_size());
  997. drop_end->connect("dropped", callable_mp(this, &EditorAudioBuses::_drop_at_index), CONNECT_DEFERRED);
  998. }
  999. }
  1000. void EditorAudioBuses::_drop_at_index(int p_bus, int p_index) {
  1001. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1002. ur->create_action(TTR("Move Audio Bus"));
  1003. ur->add_do_method(AudioServer::get_singleton(), "move_bus", p_bus, p_index);
  1004. int real_bus = p_index > p_bus ? p_bus : p_bus + 1;
  1005. int real_index = p_index > p_bus ? p_index - 1 : p_index;
  1006. ur->add_undo_method(AudioServer::get_singleton(), "move_bus", real_index, real_bus);
  1007. ur->add_do_method(this, "_update_buses");
  1008. ur->add_undo_method(this, "_update_buses");
  1009. ur->commit_action();
  1010. }
  1011. void EditorAudioBuses::_server_save() {
  1012. Ref<AudioBusLayout> state = AudioServer::get_singleton()->generate_bus_layout();
  1013. ResourceSaver::save(state, edited_path);
  1014. }
  1015. void EditorAudioBuses::_select_layout() {
  1016. FileSystemDock::get_singleton()->select_file(edited_path);
  1017. }
  1018. void EditorAudioBuses::_save_as_layout() {
  1019. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1020. file_dialog->set_title(TTR("Save Audio Bus Layout As..."));
  1021. file_dialog->set_current_path(edited_path);
  1022. file_dialog->popup_file_dialog();
  1023. new_layout = false;
  1024. }
  1025. void EditorAudioBuses::_new_layout() {
  1026. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1027. file_dialog->set_title(TTR("Location for New Layout..."));
  1028. file_dialog->set_current_path(edited_path);
  1029. file_dialog->popup_file_dialog();
  1030. new_layout = true;
  1031. }
  1032. void EditorAudioBuses::_load_layout() {
  1033. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1034. file_dialog->set_title(TTR("Open Audio Bus Layout"));
  1035. file_dialog->set_current_path(edited_path);
  1036. file_dialog->popup_file_dialog();
  1037. new_layout = false;
  1038. }
  1039. void EditorAudioBuses::_load_default_layout() {
  1040. String layout_path = GLOBAL_GET("audio/buses/default_bus_layout");
  1041. Ref<AudioBusLayout> state = ResourceLoader::load(layout_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
  1042. if (state.is_null()) {
  1043. EditorNode::get_singleton()->show_warning(vformat(TTR("There is no '%s' file."), layout_path));
  1044. return;
  1045. }
  1046. edited_path = layout_path;
  1047. file->set_text(String(TTR("Layout:")) + " " + layout_path.get_file());
  1048. AudioServer::get_singleton()->set_bus_layout(state);
  1049. _update_buses();
  1050. EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
  1051. call_deferred(SNAME("_select_layout"));
  1052. }
  1053. void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
  1054. if (file_dialog->get_file_mode() == EditorFileDialog::FILE_MODE_OPEN_FILE) {
  1055. Ref<AudioBusLayout> state = ResourceLoader::load(p_string, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
  1056. if (state.is_null()) {
  1057. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  1058. return;
  1059. }
  1060. edited_path = p_string;
  1061. file->set_text(String(TTR("Layout:")) + " " + p_string.get_file());
  1062. AudioServer::get_singleton()->set_bus_layout(state);
  1063. _update_buses();
  1064. EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
  1065. call_deferred(SNAME("_select_layout"));
  1066. } else if (file_dialog->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
  1067. if (new_layout) {
  1068. Ref<AudioBusLayout> empty_state;
  1069. empty_state.instantiate();
  1070. AudioServer::get_singleton()->set_bus_layout(empty_state);
  1071. }
  1072. Error err = ResourceSaver::save(AudioServer::get_singleton()->generate_bus_layout(), p_string);
  1073. if (err != OK) {
  1074. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file: %s"), p_string));
  1075. return;
  1076. }
  1077. edited_path = p_string;
  1078. file->set_text(String(TTR("Layout:")) + " " + p_string.get_file());
  1079. _update_buses();
  1080. EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
  1081. call_deferred(SNAME("_select_layout"));
  1082. }
  1083. }
  1084. void EditorAudioBuses::_bind_methods() {
  1085. ClassDB::bind_method("_set_renaming_buses", &EditorAudioBuses::_set_renaming_buses);
  1086. ClassDB::bind_method("_update_buses", &EditorAudioBuses::_update_buses);
  1087. ClassDB::bind_method("_update_bus", &EditorAudioBuses::_update_bus);
  1088. ClassDB::bind_method("_update_sends", &EditorAudioBuses::_update_sends);
  1089. ClassDB::bind_method("_select_layout", &EditorAudioBuses::_select_layout);
  1090. }
  1091. EditorAudioBuses::EditorAudioBuses() {
  1092. top_hb = memnew(HBoxContainer);
  1093. add_child(top_hb);
  1094. file = memnew(Label);
  1095. String layout_path = GLOBAL_GET("audio/buses/default_bus_layout");
  1096. file->set_text(String(TTR("Layout:")) + " " + layout_path.get_file());
  1097. file->set_clip_text(true);
  1098. file->set_h_size_flags(SIZE_EXPAND_FILL);
  1099. top_hb->add_child(file);
  1100. add = memnew(Button);
  1101. top_hb->add_child(add);
  1102. add->set_text(TTR("Add Bus"));
  1103. add->set_tooltip_text(TTR("Add a new Audio Bus to this layout."));
  1104. add->connect("pressed", callable_mp(this, &EditorAudioBuses::_add_bus));
  1105. VSeparator *separator = memnew(VSeparator);
  1106. top_hb->add_child(separator);
  1107. load = memnew(Button);
  1108. load->set_text(TTR("Load"));
  1109. load->set_tooltip_text(TTR("Load an existing Bus Layout."));
  1110. top_hb->add_child(load);
  1111. load->connect("pressed", callable_mp(this, &EditorAudioBuses::_load_layout));
  1112. save_as = memnew(Button);
  1113. save_as->set_text(TTR("Save As"));
  1114. save_as->set_tooltip_text(TTR("Save this Bus Layout to a file."));
  1115. top_hb->add_child(save_as);
  1116. save_as->connect("pressed", callable_mp(this, &EditorAudioBuses::_save_as_layout));
  1117. _default = memnew(Button);
  1118. _default->set_text(TTR("Load Default"));
  1119. _default->set_tooltip_text(TTR("Load the default Bus Layout."));
  1120. top_hb->add_child(_default);
  1121. _default->connect("pressed", callable_mp(this, &EditorAudioBuses::_load_default_layout));
  1122. _new = memnew(Button);
  1123. _new->set_text(TTR("Create"));
  1124. _new->set_tooltip_text(TTR("Create a new Bus Layout."));
  1125. top_hb->add_child(_new);
  1126. _new->connect("pressed", callable_mp(this, &EditorAudioBuses::_new_layout));
  1127. bus_scroll = memnew(ScrollContainer);
  1128. bus_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1129. bus_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1130. add_child(bus_scroll);
  1131. bus_hb = memnew(HBoxContainer);
  1132. bus_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1133. bus_scroll->add_child(bus_hb);
  1134. save_timer = memnew(Timer);
  1135. save_timer->set_wait_time(0.8);
  1136. save_timer->set_one_shot(true);
  1137. add_child(save_timer);
  1138. save_timer->connect("timeout", callable_mp(this, &EditorAudioBuses::_server_save));
  1139. set_v_size_flags(SIZE_EXPAND_FILL);
  1140. edited_path = GLOBAL_GET("audio/buses/default_bus_layout");
  1141. file_dialog = memnew(EditorFileDialog);
  1142. List<String> ext;
  1143. ResourceLoader::get_recognized_extensions_for_type("AudioBusLayout", &ext);
  1144. for (const String &E : ext) {
  1145. file_dialog->add_filter("*." + E, TTR("Audio Bus Layout"));
  1146. }
  1147. add_child(file_dialog);
  1148. file_dialog->connect("file_selected", callable_mp(this, &EditorAudioBuses::_file_dialog_callback));
  1149. AudioServer::get_singleton()->connect("bus_layout_changed", callable_mp(this, &EditorAudioBuses::_update_buses));
  1150. set_process(true);
  1151. }
  1152. void EditorAudioBuses::open_layout(const String &p_path) {
  1153. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1154. Ref<AudioBusLayout> state = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
  1155. if (state.is_null()) {
  1156. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  1157. return;
  1158. }
  1159. edited_path = p_path;
  1160. file->set_text(p_path.get_file());
  1161. AudioServer::get_singleton()->set_bus_layout(state);
  1162. _update_buses();
  1163. EditorUndoRedoManager::get_singleton()->clear_history(true, EditorUndoRedoManager::GLOBAL_HISTORY);
  1164. call_deferred(SNAME("_select_layout"));
  1165. }
  1166. void AudioBusesEditorPlugin::edit(Object *p_node) {
  1167. if (Object::cast_to<AudioBusLayout>(p_node)) {
  1168. String path = Object::cast_to<AudioBusLayout>(p_node)->get_path();
  1169. if (path.is_resource_file()) {
  1170. audio_bus_editor->open_layout(path);
  1171. }
  1172. }
  1173. }
  1174. bool AudioBusesEditorPlugin::handles(Object *p_node) const {
  1175. return (Object::cast_to<AudioBusLayout>(p_node) != nullptr);
  1176. }
  1177. void AudioBusesEditorPlugin::make_visible(bool p_visible) {
  1178. }
  1179. AudioBusesEditorPlugin::AudioBusesEditorPlugin(EditorAudioBuses *p_node) {
  1180. audio_bus_editor = p_node;
  1181. }
  1182. AudioBusesEditorPlugin::~AudioBusesEditorPlugin() {
  1183. }
  1184. void EditorAudioMeterNotches::add_notch(float p_normalized_offset, float p_db_value, bool p_render_value) {
  1185. notches.push_back(AudioNotch(p_normalized_offset, p_db_value, p_render_value));
  1186. }
  1187. Size2 EditorAudioMeterNotches::get_minimum_size() const {
  1188. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  1189. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  1190. float font_height = font->get_height(font_size);
  1191. float width = 0;
  1192. float height = top_padding + btm_padding;
  1193. for (int i = 0; i < notches.size(); i++) {
  1194. if (notches[i].render_db_value) {
  1195. width = MAX(width, font->get_string_size(String::num(Math::abs(notches[i].db_value)) + "dB", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x);
  1196. height += font_height;
  1197. }
  1198. }
  1199. width += line_length + label_space;
  1200. return Size2(width, height);
  1201. }
  1202. void EditorAudioMeterNotches::_update_theme_item_cache() {
  1203. Control::_update_theme_item_cache();
  1204. theme_cache.notch_color = get_theme_color(SNAME("font_color"), SNAME("Editor"));
  1205. theme_cache.font = get_theme_font(SNAME("font"), SNAME("Label"));
  1206. theme_cache.font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  1207. }
  1208. void EditorAudioMeterNotches::_bind_methods() {
  1209. ClassDB::bind_method("add_notch", &EditorAudioMeterNotches::add_notch);
  1210. ClassDB::bind_method("_draw_audio_notches", &EditorAudioMeterNotches::_draw_audio_notches);
  1211. }
  1212. void EditorAudioMeterNotches::_notification(int p_what) {
  1213. switch (p_what) {
  1214. case NOTIFICATION_DRAW: {
  1215. _draw_audio_notches();
  1216. } break;
  1217. }
  1218. }
  1219. void EditorAudioMeterNotches::_draw_audio_notches() {
  1220. float font_height = theme_cache.font->get_height(theme_cache.font_size);
  1221. for (int i = 0; i < notches.size(); i++) {
  1222. AudioNotch n = notches[i];
  1223. draw_line(Vector2(0, (1.0f - n.relative_position) * (get_size().y - btm_padding - top_padding) + top_padding),
  1224. Vector2(line_length * EDSCALE, (1.0f - n.relative_position) * (get_size().y - btm_padding - top_padding) + top_padding),
  1225. theme_cache.notch_color,
  1226. Math::round(EDSCALE));
  1227. if (n.render_db_value) {
  1228. draw_string(theme_cache.font,
  1229. Vector2((line_length + label_space) * EDSCALE,
  1230. (1.0f - n.relative_position) * (get_size().y - btm_padding - top_padding) + (font_height / 4) + top_padding),
  1231. String::num(Math::abs(n.db_value)) + "dB",
  1232. HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size,
  1233. theme_cache.notch_color);
  1234. }
  1235. }
  1236. }