editor_audio_buses.cpp 50 KB

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