editor_audio_buses.cpp 58 KB

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