editor_audio_buses.cpp 51 KB

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