editor_audio_buses.cpp 51 KB

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