editor_audio_buses.cpp 52 KB

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