editor_audio_buses.cpp 52 KB

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