editor_audio_buses.cpp 51 KB

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