editor_audio_buses.cpp 55 KB

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