editor_audio_buses.cpp 51 KB

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