editor_audio_buses.cpp 52 KB

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