editor_audio_buses.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_READY: {
  61. for (int i = 0; i < CHANNELS_MAX; i++) {
  62. channel[i].vu_l->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  63. channel[i].vu_l->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  64. channel[i].vu_r->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  65. channel[i].vu_r->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  66. channel[i].prev_active = true;
  67. }
  68. disabled_vu = get_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_icon("AudioBusSolo", "EditorIcons"));
  73. solo->add_color_override("icon_color_pressed", solo_color);
  74. mute->set_icon(get_icon("AudioBusMute", "EditorIcons"));
  75. mute->add_color_override("icon_color_pressed", mute_color);
  76. bypass->set_icon(get_icon("AudioBusBypass", "EditorIcons"));
  77. bypass->add_color_override("icon_color_pressed", bypass_color);
  78. bus_options->set_icon(get_icon("GuiTabMenuHl", "EditorIcons"));
  79. update_bus();
  80. set_process(true);
  81. } break;
  82. case NOTIFICATION_DRAW: {
  83. if (is_master) {
  84. draw_style_box(get_stylebox("disabled", "Button"), Rect2(Vector2(), get_size()));
  85. } else if (has_focus()) {
  86. draw_style_box(get_stylebox("focus", "Button"), Rect2(Vector2(), get_size()));
  87. } else {
  88. draw_style_box(get_stylebox("panel", "TabContainer"), Rect2(Vector2(), get_size()));
  89. }
  90. if (get_index() != 0 && hovering_drop) {
  91. Color accent = get_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<Texture>());
  124. channel[i].vu_r->set_over_texture(Ref<Texture>());
  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_icon("BusVuEmpty", "EditorIcons"));
  144. channel[i].vu_l->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  145. channel[i].vu_r->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  146. channel[i].vu_r->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  147. channel[i].prev_active = true;
  148. }
  149. disabled_vu = get_icon("BusVuFrozen", "EditorIcons");
  150. solo->set_icon(get_icon("AudioBusSolo", "EditorIcons"));
  151. mute->set_icon(get_icon("AudioBusMute", "EditorIcons"));
  152. bypass->set_icon(get_icon("AudioBusBypass", "EditorIcons"));
  153. bus_options->set_icon(get_icon("GuiTabMenuHl", "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_scancode() == 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::_unhandled_key_input(Ref<InputEvent> p_event) {
  452. Ref<InputEventKey> k = p_event;
  453. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_scancode() == KEY_DELETE) {
  454. TreeItem *current_effect = effects->get_selected();
  455. if (current_effect && current_effect->get_metadata(0).get_type() == Variant::INT) {
  456. _delete_effect_pressed(0);
  457. accept_event();
  458. }
  459. }
  460. }
  461. void EditorAudioBus::_bus_popup_pressed(int p_option) {
  462. if (p_option == 2) {
  463. // Reset volume
  464. emit_signal("vol_reset_request");
  465. } else if (p_option == 1) {
  466. emit_signal("delete_request");
  467. } else if (p_option == 0) {
  468. //duplicate
  469. emit_signal("duplicate_request", get_index());
  470. }
  471. }
  472. Variant EditorAudioBus::get_drag_data(const Point2 &p_point) {
  473. if (get_index() == 0) {
  474. return Variant();
  475. }
  476. Control *c = memnew(Control);
  477. Panel *p = memnew(Panel);
  478. c->add_child(p);
  479. p->set_modulate(Color(1, 1, 1, 0.7));
  480. p->add_style_override("panel", get_stylebox("focus", "Button"));
  481. p->set_size(get_size());
  482. p->set_position(-p_point);
  483. set_drag_preview(c);
  484. Dictionary d;
  485. d["type"] = "move_audio_bus";
  486. d["index"] = get_index();
  487. if (get_index() < AudioServer::get_singleton()->get_bus_count() - 1) {
  488. emit_signal("drop_end_request");
  489. }
  490. return d;
  491. }
  492. bool EditorAudioBus::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  493. if (get_index() == 0) {
  494. return false;
  495. }
  496. Dictionary d = p_data;
  497. if (d.has("type") && String(d["type"]) == "move_audio_bus" && (int)d["index"] != get_index()) {
  498. hovering_drop = true;
  499. return true;
  500. }
  501. return false;
  502. }
  503. void EditorAudioBus::drop_data(const Point2 &p_point, const Variant &p_data) {
  504. Dictionary d = p_data;
  505. emit_signal("dropped", d["index"], get_index());
  506. }
  507. Variant EditorAudioBus::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  508. TreeItem *item = effects->get_item_at_position(p_point);
  509. if (!item) {
  510. return Variant();
  511. }
  512. Variant md = item->get_metadata(0);
  513. if (md.get_type() == Variant::INT) {
  514. Dictionary fxd;
  515. fxd["type"] = "audio_bus_effect";
  516. fxd["bus"] = get_index();
  517. fxd["effect"] = md;
  518. Label *l = memnew(Label);
  519. l->set_text(item->get_text(0));
  520. effects->set_drag_preview(l);
  521. return fxd;
  522. }
  523. return Variant();
  524. }
  525. bool EditorAudioBus::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  526. Dictionary d = p_data;
  527. if (!d.has("type") || String(d["type"]) != "audio_bus_effect")
  528. return false;
  529. TreeItem *item = effects->get_item_at_position(p_point);
  530. if (!item)
  531. return false;
  532. effects->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  533. return true;
  534. }
  535. void EditorAudioBus::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  536. Dictionary d = p_data;
  537. TreeItem *item = effects->get_item_at_position(p_point);
  538. if (!item)
  539. return;
  540. int pos = effects->get_drop_section_at_position(p_point);
  541. Variant md = item->get_metadata(0);
  542. int paste_at;
  543. int bus = d["bus"];
  544. int effect = d["effect"];
  545. if (md.get_type() == Variant::INT) {
  546. paste_at = md;
  547. if (pos > 0)
  548. paste_at++;
  549. if (bus == get_index() && paste_at > effect) {
  550. paste_at--;
  551. }
  552. } else {
  553. paste_at = -1;
  554. }
  555. bool enabled = AudioServer::get_singleton()->is_bus_effect_enabled(bus, effect);
  556. UndoRedo *ur = EditorNode::get_undo_redo();
  557. ur->create_action(TTR("Move Bus Effect"));
  558. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", bus, effect);
  559. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(bus, effect), paste_at);
  560. if (paste_at == -1) {
  561. paste_at = AudioServer::get_singleton()->get_bus_effect_count(get_index());
  562. if (bus == get_index()) {
  563. paste_at--;
  564. }
  565. }
  566. if (!enabled) {
  567. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), paste_at, false);
  568. }
  569. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), paste_at);
  570. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", bus, AudioServer::get_singleton()->get_bus_effect(bus, effect), effect);
  571. if (!enabled) {
  572. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", bus, effect, false);
  573. }
  574. ur->add_do_method(buses, "_update_bus", get_index());
  575. ur->add_undo_method(buses, "_update_bus", get_index());
  576. if (get_index() != bus) {
  577. ur->add_do_method(buses, "_update_bus", bus);
  578. ur->add_undo_method(buses, "_update_bus", bus);
  579. }
  580. ur->commit_action();
  581. }
  582. void EditorAudioBus::_delete_effect_pressed(int p_option) {
  583. TreeItem *item = effects->get_selected();
  584. if (!item)
  585. return;
  586. if (item->get_metadata(0).get_type() != Variant::INT)
  587. return;
  588. int index = item->get_metadata(0);
  589. UndoRedo *ur = EditorNode::get_undo_redo();
  590. ur->create_action(TTR("Delete Bus Effect"));
  591. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), index);
  592. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(get_index(), index), index);
  593. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  594. ur->add_do_method(buses, "_update_bus", get_index());
  595. ur->add_undo_method(buses, "_update_bus", get_index());
  596. ur->commit_action();
  597. }
  598. void EditorAudioBus::_effect_rmb(const Vector2 &p_pos) {
  599. TreeItem *item = effects->get_selected();
  600. if (!item)
  601. return;
  602. if (item->get_metadata(0).get_type() != Variant::INT)
  603. return;
  604. delete_effect_popup->set_position(get_global_mouse_position());
  605. delete_effect_popup->popup();
  606. }
  607. void EditorAudioBus::_bind_methods() {
  608. ClassDB::bind_method("update_bus", &EditorAudioBus::update_bus);
  609. ClassDB::bind_method("update_send", &EditorAudioBus::update_send);
  610. ClassDB::bind_method("_name_changed", &EditorAudioBus::_name_changed);
  611. ClassDB::bind_method("_volume_changed", &EditorAudioBus::_volume_changed);
  612. ClassDB::bind_method("_show_value", &EditorAudioBus::_show_value);
  613. ClassDB::bind_method("_hide_value_preview", &EditorAudioBus::_hide_value_preview);
  614. ClassDB::bind_method("_solo_toggled", &EditorAudioBus::_solo_toggled);
  615. ClassDB::bind_method("_mute_toggled", &EditorAudioBus::_mute_toggled);
  616. ClassDB::bind_method("_bypass_toggled", &EditorAudioBus::_bypass_toggled);
  617. ClassDB::bind_method("_name_focus_exit", &EditorAudioBus::_name_focus_exit);
  618. ClassDB::bind_method("_send_selected", &EditorAudioBus::_send_selected);
  619. ClassDB::bind_method("_effect_edited", &EditorAudioBus::_effect_edited);
  620. ClassDB::bind_method("_effect_selected", &EditorAudioBus::_effect_selected);
  621. ClassDB::bind_method("_effect_add", &EditorAudioBus::_effect_add);
  622. ClassDB::bind_method("_gui_input", &EditorAudioBus::_gui_input);
  623. ClassDB::bind_method("_unhandled_key_input", &EditorAudioBus::_unhandled_key_input);
  624. ClassDB::bind_method("_bus_popup_pressed", &EditorAudioBus::_bus_popup_pressed);
  625. ClassDB::bind_method("get_drag_data_fw", &EditorAudioBus::get_drag_data_fw);
  626. ClassDB::bind_method("can_drop_data_fw", &EditorAudioBus::can_drop_data_fw);
  627. ClassDB::bind_method("drop_data_fw", &EditorAudioBus::drop_data_fw);
  628. ClassDB::bind_method("_delete_effect_pressed", &EditorAudioBus::_delete_effect_pressed);
  629. ClassDB::bind_method("_effect_rmb", &EditorAudioBus::_effect_rmb);
  630. ADD_SIGNAL(MethodInfo("duplicate_request"));
  631. ADD_SIGNAL(MethodInfo("delete_request"));
  632. ADD_SIGNAL(MethodInfo("vol_reset_request"));
  633. ADD_SIGNAL(MethodInfo("drop_end_request"));
  634. ADD_SIGNAL(MethodInfo("dropped"));
  635. }
  636. EditorAudioBus::EditorAudioBus(EditorAudioBuses *p_buses, bool p_is_master) {
  637. buses = p_buses;
  638. updating_bus = false;
  639. is_master = p_is_master;
  640. hovering_drop = false;
  641. set_tooltip(TTR("Drag & drop to rearrange."));
  642. VBoxContainer *vb = memnew(VBoxContainer);
  643. add_child(vb);
  644. set_v_size_flags(SIZE_EXPAND_FILL);
  645. set_process_unhandled_key_input(true);
  646. track_name = memnew(LineEdit);
  647. track_name->connect("text_entered", this, "_name_changed");
  648. track_name->connect("focus_exited", this, "_name_focus_exit");
  649. vb->add_child(track_name);
  650. HBoxContainer *hbc = memnew(HBoxContainer);
  651. vb->add_child(hbc);
  652. solo = memnew(ToolButton);
  653. solo->set_toggle_mode(true);
  654. solo->set_tooltip(TTR("Solo"));
  655. solo->set_focus_mode(FOCUS_NONE);
  656. solo->connect("pressed", this, "_solo_toggled");
  657. hbc->add_child(solo);
  658. mute = memnew(ToolButton);
  659. mute->set_toggle_mode(true);
  660. mute->set_tooltip(TTR("Mute"));
  661. mute->set_focus_mode(FOCUS_NONE);
  662. mute->connect("pressed", this, "_mute_toggled");
  663. hbc->add_child(mute);
  664. bypass = memnew(ToolButton);
  665. bypass->set_toggle_mode(true);
  666. bypass->set_tooltip(TTR("Bypass"));
  667. bypass->set_focus_mode(FOCUS_NONE);
  668. bypass->connect("pressed", this, "_bypass_toggled");
  669. hbc->add_child(bypass);
  670. hbc->add_spacer();
  671. bus_options = memnew(MenuButton);
  672. bus_options->set_h_size_flags(SIZE_SHRINK_END);
  673. bus_options->set_anchor(MARGIN_RIGHT, 0.0);
  674. bus_options->set_tooltip(TTR("Bus options"));
  675. hbc->add_child(bus_options);
  676. Ref<StyleBoxEmpty> sbempty = memnew(StyleBoxEmpty);
  677. for (int i = 0; i < hbc->get_child_count(); i++) {
  678. Control *child = Object::cast_to<Control>(hbc->get_child(i));
  679. child->add_style_override("normal", sbempty);
  680. child->add_style_override("hover", sbempty);
  681. child->add_style_override("focus", sbempty);
  682. child->add_style_override("pressed", sbempty);
  683. }
  684. HSeparator *separator = memnew(HSeparator);
  685. separator->set_mouse_filter(MOUSE_FILTER_PASS);
  686. vb->add_child(separator);
  687. HBoxContainer *hb = memnew(HBoxContainer);
  688. vb->add_child(hb);
  689. slider = memnew(VSlider);
  690. slider->set_min(0.0);
  691. slider->set_max(1.0);
  692. slider->set_step(0.0001);
  693. slider->set_clip_contents(false);
  694. audio_value_preview_box = memnew(Panel);
  695. HBoxContainer *audioprev_hbc = memnew(HBoxContainer);
  696. audioprev_hbc->set_v_size_flags(SIZE_EXPAND_FILL);
  697. audioprev_hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  698. audioprev_hbc->set_mouse_filter(MOUSE_FILTER_PASS);
  699. audio_value_preview_box->add_child(audioprev_hbc);
  700. audio_value_preview_label = memnew(Label);
  701. audio_value_preview_label->set_v_size_flags(SIZE_EXPAND_FILL);
  702. audio_value_preview_label->set_h_size_flags(SIZE_EXPAND_FILL);
  703. audio_value_preview_label->set_mouse_filter(MOUSE_FILTER_PASS);
  704. audioprev_hbc->add_child(audio_value_preview_label);
  705. slider->add_child(audio_value_preview_box);
  706. audio_value_preview_box->set_as_toplevel(true);
  707. Ref<StyleBoxFlat> panel_style = memnew(StyleBoxFlat);
  708. panel_style->set_bg_color(Color(0.0f, 0.0f, 0.0f, 0.8f));
  709. audio_value_preview_box->add_style_override("panel", panel_style);
  710. audio_value_preview_box->set_mouse_filter(MOUSE_FILTER_PASS);
  711. audio_value_preview_box->hide();
  712. preview_timer = memnew(Timer);
  713. preview_timer->set_wait_time(0.8f);
  714. preview_timer->set_one_shot(true);
  715. add_child(preview_timer);
  716. slider->connect("value_changed", this, "_volume_changed");
  717. slider->connect("value_changed", this, "_show_value");
  718. preview_timer->connect("timeout", this, "_hide_value_preview");
  719. hb->add_child(slider);
  720. cc = 0;
  721. for (int i = 0; i < CHANNELS_MAX; i++) {
  722. channel[i].vu_l = memnew(TextureProgress);
  723. channel[i].vu_l->set_fill_mode(TextureProgress::FILL_BOTTOM_TO_TOP);
  724. hb->add_child(channel[i].vu_l);
  725. channel[i].vu_l->set_min(-80);
  726. channel[i].vu_l->set_max(24);
  727. channel[i].vu_l->set_step(0.1);
  728. channel[i].vu_r = memnew(TextureProgress);
  729. channel[i].vu_r->set_fill_mode(TextureProgress::FILL_BOTTOM_TO_TOP);
  730. hb->add_child(channel[i].vu_r);
  731. channel[i].vu_r->set_min(-80);
  732. channel[i].vu_r->set_max(24);
  733. channel[i].vu_r->set_step(0.1);
  734. channel[i].peak_l = 0.0f;
  735. channel[i].peak_r = 0.0f;
  736. }
  737. EditorAudioMeterNotches *scale = memnew(EditorAudioMeterNotches);
  738. for (float db = 6.0f; db >= -80.0f; db -= 6.0f) {
  739. bool renderNotch = (db >= -6.0f || db == -24.0f || db == -72.0f);
  740. scale->add_notch(_scaled_db_to_normalized_volume(db), db, renderNotch);
  741. }
  742. scale->set_mouse_filter(MOUSE_FILTER_PASS);
  743. hb->add_child(scale);
  744. effects = memnew(Tree);
  745. effects->set_hide_root(true);
  746. effects->set_custom_minimum_size(Size2(0, 80) * EDSCALE);
  747. effects->set_hide_folding(true);
  748. effects->set_v_size_flags(SIZE_EXPAND_FILL);
  749. vb->add_child(effects);
  750. effects->connect("item_edited", this, "_effect_edited");
  751. effects->connect("cell_selected", this, "_effect_selected");
  752. effects->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  753. effects->set_drag_forwarding(this);
  754. effects->connect("item_rmb_selected", this, "_effect_rmb");
  755. effects->set_allow_rmb_select(true);
  756. effects->set_focus_mode(FOCUS_CLICK);
  757. effects->set_allow_reselect(true);
  758. send = memnew(OptionButton);
  759. send->set_clip_text(true);
  760. send->connect("item_selected", this, "_send_selected");
  761. vb->add_child(send);
  762. set_focus_mode(FOCUS_CLICK);
  763. effect_options = memnew(PopupMenu);
  764. effect_options->connect("index_pressed", this, "_effect_add");
  765. add_child(effect_options);
  766. List<StringName> effects;
  767. ClassDB::get_inheriters_from_class("AudioEffect", &effects);
  768. effects.sort_custom<StringName::AlphCompare>();
  769. for (List<StringName>::Element *E = effects.front(); E; E = E->next()) {
  770. if (!ClassDB::can_instance(E->get()))
  771. continue;
  772. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(E->get());
  773. String name = E->get().operator String().replace("AudioEffect", "");
  774. effect_options->add_item(name);
  775. effect_options->set_item_metadata(effect_options->get_item_count() - 1, E->get());
  776. effect_options->set_item_icon(effect_options->get_item_count() - 1, icon);
  777. }
  778. bus_popup = bus_options->get_popup();
  779. bus_popup->add_item(TTR("Duplicate"));
  780. bus_popup->add_item(TTR("Delete"));
  781. bus_popup->set_item_disabled(1, is_master);
  782. bus_popup->add_item(TTR("Reset Volume"));
  783. bus_popup->connect("index_pressed", this, "_bus_popup_pressed");
  784. delete_effect_popup = memnew(PopupMenu);
  785. delete_effect_popup->add_item(TTR("Delete Effect"));
  786. add_child(delete_effect_popup);
  787. delete_effect_popup->connect("index_pressed", this, "_delete_effect_pressed");
  788. }
  789. void EditorAudioBusDrop::_notification(int p_what) {
  790. switch (p_what) {
  791. case NOTIFICATION_DRAW: {
  792. draw_style_box(get_stylebox("normal", "Button"), Rect2(Vector2(), get_size()));
  793. if (hovering_drop) {
  794. Color accent = get_color("accent_color", "Editor");
  795. accent.a *= 0.7;
  796. draw_rect(Rect2(Point2(), get_size()), accent, false);
  797. }
  798. } break;
  799. case NOTIFICATION_MOUSE_ENTER: {
  800. if (!hovering_drop) {
  801. hovering_drop = true;
  802. update();
  803. }
  804. } break;
  805. case NOTIFICATION_MOUSE_EXIT:
  806. case NOTIFICATION_DRAG_END: {
  807. if (hovering_drop) {
  808. hovering_drop = false;
  809. update();
  810. }
  811. } break;
  812. }
  813. }
  814. bool EditorAudioBusDrop::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  815. Dictionary d = p_data;
  816. return (d.has("type") && String(d["type"]) == "move_audio_bus");
  817. }
  818. void EditorAudioBusDrop::drop_data(const Point2 &p_point, const Variant &p_data) {
  819. Dictionary d = p_data;
  820. emit_signal("dropped", d["index"], AudioServer::get_singleton()->get_bus_count());
  821. }
  822. void EditorAudioBusDrop::_bind_methods() {
  823. ADD_SIGNAL(MethodInfo("dropped"));
  824. }
  825. EditorAudioBusDrop::EditorAudioBusDrop() {
  826. hovering_drop = false;
  827. }
  828. void EditorAudioBuses::_update_buses() {
  829. while (bus_hb->get_child_count() > 0) {
  830. memdelete(bus_hb->get_child(0));
  831. }
  832. drop_end = NULL;
  833. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  834. bool is_master = (i == 0);
  835. EditorAudioBus *audio_bus = memnew(EditorAudioBus(this, is_master));
  836. bus_hb->add_child(audio_bus);
  837. audio_bus->connect("delete_request", this, "_delete_bus", varray(audio_bus), CONNECT_DEFERRED);
  838. audio_bus->connect("duplicate_request", this, "_duplicate_bus", varray(), CONNECT_DEFERRED);
  839. audio_bus->connect("vol_reset_request", this, "_reset_bus_volume", varray(audio_bus), CONNECT_DEFERRED);
  840. audio_bus->connect("drop_end_request", this, "_request_drop_end");
  841. audio_bus->connect("dropped", this, "_drop_at_index", varray(), CONNECT_DEFERRED);
  842. }
  843. }
  844. EditorAudioBuses *EditorAudioBuses::register_editor() {
  845. EditorAudioBuses *audio_buses = memnew(EditorAudioBuses);
  846. EditorNode::get_singleton()->add_bottom_panel_item(TTR("Audio"), audio_buses);
  847. return audio_buses;
  848. }
  849. void EditorAudioBuses::_notification(int p_what) {
  850. switch (p_what) {
  851. case NOTIFICATION_ENTER_TREE:
  852. case NOTIFICATION_THEME_CHANGED: {
  853. bus_scroll->add_style_override("bg", get_stylebox("bg", "Tree"));
  854. } break;
  855. case NOTIFICATION_READY: {
  856. _update_buses();
  857. } break;
  858. case NOTIFICATION_DRAG_END: {
  859. if (drop_end) {
  860. drop_end->queue_delete();
  861. drop_end = NULL;
  862. }
  863. } break;
  864. case NOTIFICATION_PROCESS: {
  865. // Check if anything was edited.
  866. bool edited = AudioServer::get_singleton()->is_edited();
  867. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  868. for (int j = 0; j < AudioServer::get_singleton()->get_bus_effect_count(i); j++) {
  869. Ref<AudioEffect> effect = AudioServer::get_singleton()->get_bus_effect(i, j);
  870. if (effect->is_edited()) {
  871. edited = true;
  872. effect->set_edited(false);
  873. }
  874. }
  875. }
  876. AudioServer::get_singleton()->set_edited(false);
  877. if (edited) {
  878. save_timer->start();
  879. }
  880. } break;
  881. }
  882. }
  883. void EditorAudioBuses::_add_bus() {
  884. UndoRedo *ur = EditorNode::get_undo_redo();
  885. ur->create_action(TTR("Add Audio Bus"));
  886. ur->add_do_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count() + 1);
  887. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count());
  888. ur->add_do_method(this, "_update_buses");
  889. ur->add_undo_method(this, "_update_buses");
  890. ur->commit_action();
  891. }
  892. void EditorAudioBuses::_update_bus(int p_index) {
  893. if (p_index >= bus_hb->get_child_count())
  894. return;
  895. bus_hb->get_child(p_index)->call("update_bus");
  896. }
  897. void EditorAudioBuses::_update_sends() {
  898. for (int i = 0; i < bus_hb->get_child_count(); i++) {
  899. bus_hb->get_child(i)->call("update_send");
  900. }
  901. }
  902. void EditorAudioBuses::_delete_bus(Object *p_which) {
  903. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  904. int index = bus->get_index();
  905. if (index == 0) {
  906. EditorNode::get_singleton()->show_warning(TTR("Master bus can't be deleted!"));
  907. return;
  908. }
  909. UndoRedo *ur = EditorNode::get_undo_redo();
  910. ur->create_action(TTR("Delete Audio Bus"));
  911. ur->add_do_method(AudioServer::get_singleton(), "remove_bus", index);
  912. ur->add_undo_method(AudioServer::get_singleton(), "add_bus", index);
  913. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", index, AudioServer::get_singleton()->get_bus_name(index));
  914. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  915. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", index, AudioServer::get_singleton()->get_bus_send(index));
  916. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", index, AudioServer::get_singleton()->is_bus_solo(index));
  917. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", index, AudioServer::get_singleton()->is_bus_mute(index));
  918. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", index, AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  919. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  920. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", index, AudioServer::get_singleton()->get_bus_effect(index, i));
  921. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", index, i, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  922. }
  923. ur->add_do_method(this, "_update_buses");
  924. ur->add_undo_method(this, "_update_buses");
  925. ur->commit_action();
  926. }
  927. void EditorAudioBuses::_duplicate_bus(int p_which) {
  928. int add_at_pos = p_which + 1;
  929. UndoRedo *ur = EditorNode::get_undo_redo();
  930. ur->create_action(TTR("Duplicate Audio Bus"));
  931. ur->add_do_method(AudioServer::get_singleton(), "add_bus", add_at_pos);
  932. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", add_at_pos, AudioServer::get_singleton()->get_bus_name(p_which) + " Copy");
  933. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", add_at_pos, AudioServer::get_singleton()->get_bus_volume_db(p_which));
  934. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", add_at_pos, AudioServer::get_singleton()->get_bus_send(p_which));
  935. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", add_at_pos, AudioServer::get_singleton()->is_bus_solo(p_which));
  936. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", add_at_pos, AudioServer::get_singleton()->is_bus_mute(p_which));
  937. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", add_at_pos, AudioServer::get_singleton()->is_bus_bypassing_effects(p_which));
  938. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(p_which); i++) {
  939. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", add_at_pos, AudioServer::get_singleton()->get_bus_effect(p_which, i));
  940. 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));
  941. }
  942. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus", add_at_pos);
  943. ur->add_do_method(this, "_update_buses");
  944. ur->add_undo_method(this, "_update_buses");
  945. ur->commit_action();
  946. }
  947. void EditorAudioBuses::_reset_bus_volume(Object *p_which) {
  948. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  949. int index = bus->get_index();
  950. UndoRedo *ur = EditorNode::get_undo_redo();
  951. ur->create_action(TTR("Reset Bus Volume"));
  952. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", index, 0.f);
  953. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  954. ur->add_do_method(this, "_update_buses");
  955. ur->add_undo_method(this, "_update_buses");
  956. ur->commit_action();
  957. }
  958. void EditorAudioBuses::_request_drop_end() {
  959. if (!drop_end && bus_hb->get_child_count()) {
  960. drop_end = memnew(EditorAudioBusDrop);
  961. bus_hb->add_child(drop_end);
  962. drop_end->set_custom_minimum_size(Object::cast_to<Control>(bus_hb->get_child(0))->get_size());
  963. drop_end->connect("dropped", this, "_drop_at_index", varray(), CONNECT_DEFERRED);
  964. }
  965. }
  966. void EditorAudioBuses::_drop_at_index(int p_bus, int p_index) {
  967. UndoRedo *ur = EditorNode::get_undo_redo();
  968. ur->create_action(TTR("Move Audio Bus"));
  969. ur->add_do_method(AudioServer::get_singleton(), "move_bus", p_bus, p_index);
  970. int real_bus = p_index > p_bus ? p_bus : p_bus + 1;
  971. int real_index = p_index > p_bus ? p_index - 1 : p_index;
  972. ur->add_undo_method(AudioServer::get_singleton(), "move_bus", real_index, real_bus);
  973. ur->add_do_method(this, "_update_buses");
  974. ur->add_undo_method(this, "_update_buses");
  975. ur->commit_action();
  976. }
  977. void EditorAudioBuses::_server_save() {
  978. Ref<AudioBusLayout> state = AudioServer::get_singleton()->generate_bus_layout();
  979. ResourceSaver::save(edited_path, state);
  980. }
  981. void EditorAudioBuses::_select_layout() {
  982. EditorNode::get_singleton()->get_filesystem_dock()->select_file(edited_path);
  983. }
  984. void EditorAudioBuses::_save_as_layout() {
  985. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  986. file_dialog->set_title(TTR("Save Audio Bus Layout As..."));
  987. file_dialog->set_current_path(edited_path);
  988. file_dialog->popup_centered_ratio();
  989. new_layout = false;
  990. }
  991. void EditorAudioBuses::_new_layout() {
  992. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  993. file_dialog->set_title(TTR("Location for New Layout..."));
  994. file_dialog->set_current_path(edited_path);
  995. file_dialog->popup_centered_ratio();
  996. new_layout = true;
  997. }
  998. void EditorAudioBuses::_load_layout() {
  999. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1000. file_dialog->set_title(TTR("Open Audio Bus Layout"));
  1001. file_dialog->set_current_path(edited_path);
  1002. file_dialog->popup_centered_ratio();
  1003. new_layout = false;
  1004. }
  1005. void EditorAudioBuses::_load_default_layout() {
  1006. String layout_path = ProjectSettings::get_singleton()->get("audio/default_bus_layout");
  1007. Ref<AudioBusLayout> state = ResourceLoader::load(layout_path, "", true);
  1008. if (state.is_null()) {
  1009. EditorNode::get_singleton()->show_warning(vformat(TTR("There is no '%s' file."), layout_path));
  1010. return;
  1011. }
  1012. edited_path = layout_path;
  1013. file->set_text(String(TTR("Layout")) + ": " + layout_path.get_file());
  1014. AudioServer::get_singleton()->set_bus_layout(state);
  1015. _update_buses();
  1016. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  1017. call_deferred("_select_layout");
  1018. }
  1019. void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
  1020. if (file_dialog->get_mode() == EditorFileDialog::MODE_OPEN_FILE) {
  1021. Ref<AudioBusLayout> state = ResourceLoader::load(p_string, "", true);
  1022. if (state.is_null()) {
  1023. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  1024. return;
  1025. }
  1026. edited_path = p_string;
  1027. file->set_text(String(TTR("Layout")) + ": " + p_string.get_file());
  1028. AudioServer::get_singleton()->set_bus_layout(state);
  1029. _update_buses();
  1030. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  1031. call_deferred("_select_layout");
  1032. } else if (file_dialog->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  1033. if (new_layout) {
  1034. Ref<AudioBusLayout> empty_state;
  1035. empty_state.instance();
  1036. AudioServer::get_singleton()->set_bus_layout(empty_state);
  1037. }
  1038. Error err = ResourceSaver::save(p_string, AudioServer::get_singleton()->generate_bus_layout());
  1039. if (err != OK) {
  1040. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file: %s"), p_string));
  1041. return;
  1042. }
  1043. edited_path = p_string;
  1044. file->set_text(String(TTR("Layout")) + ": " + p_string.get_file());
  1045. _update_buses();
  1046. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  1047. call_deferred("_select_layout");
  1048. }
  1049. }
  1050. void EditorAudioBuses::_bind_methods() {
  1051. ClassDB::bind_method("_add_bus", &EditorAudioBuses::_add_bus);
  1052. ClassDB::bind_method("_update_buses", &EditorAudioBuses::_update_buses);
  1053. ClassDB::bind_method("_update_bus", &EditorAudioBuses::_update_bus);
  1054. ClassDB::bind_method("_update_sends", &EditorAudioBuses::_update_sends);
  1055. ClassDB::bind_method("_delete_bus", &EditorAudioBuses::_delete_bus);
  1056. ClassDB::bind_method("_request_drop_end", &EditorAudioBuses::_request_drop_end);
  1057. ClassDB::bind_method("_drop_at_index", &EditorAudioBuses::_drop_at_index);
  1058. ClassDB::bind_method("_server_save", &EditorAudioBuses::_server_save);
  1059. ClassDB::bind_method("_select_layout", &EditorAudioBuses::_select_layout);
  1060. ClassDB::bind_method("_save_as_layout", &EditorAudioBuses::_save_as_layout);
  1061. ClassDB::bind_method("_load_layout", &EditorAudioBuses::_load_layout);
  1062. ClassDB::bind_method("_load_default_layout", &EditorAudioBuses::_load_default_layout);
  1063. ClassDB::bind_method("_new_layout", &EditorAudioBuses::_new_layout);
  1064. ClassDB::bind_method("_duplicate_bus", &EditorAudioBuses::_duplicate_bus);
  1065. ClassDB::bind_method("_reset_bus_volume", &EditorAudioBuses::_reset_bus_volume);
  1066. ClassDB::bind_method("_file_dialog_callback", &EditorAudioBuses::_file_dialog_callback);
  1067. }
  1068. EditorAudioBuses::EditorAudioBuses() {
  1069. drop_end = NULL;
  1070. top_hb = memnew(HBoxContainer);
  1071. add_child(top_hb);
  1072. file = memnew(Label);
  1073. String layout_path = ProjectSettings::get_singleton()->get("audio/default_bus_layout");
  1074. file->set_text(String(TTR("Layout")) + ": " + layout_path.get_file());
  1075. file->set_clip_text(true);
  1076. file->set_h_size_flags(SIZE_EXPAND_FILL);
  1077. top_hb->add_child(file);
  1078. add = memnew(Button);
  1079. top_hb->add_child(add);
  1080. add->set_text(TTR("Add Bus"));
  1081. add->set_tooltip(TTR("Add a new Audio Bus to this layout."));
  1082. add->connect("pressed", this, "_add_bus");
  1083. VSeparator *separator = memnew(VSeparator);
  1084. top_hb->add_child(separator);
  1085. load = memnew(Button);
  1086. load->set_text(TTR("Load"));
  1087. load->set_tooltip(TTR("Load an existing Bus Layout."));
  1088. top_hb->add_child(load);
  1089. load->connect("pressed", this, "_load_layout");
  1090. save_as = memnew(Button);
  1091. save_as->set_text(TTR("Save As"));
  1092. save_as->set_tooltip(TTR("Save this Bus Layout to a file."));
  1093. top_hb->add_child(save_as);
  1094. save_as->connect("pressed", this, "_save_as_layout");
  1095. _default = memnew(Button);
  1096. _default->set_text(TTR("Load Default"));
  1097. _default->set_tooltip(TTR("Load the default Bus Layout."));
  1098. top_hb->add_child(_default);
  1099. _default->connect("pressed", this, "_load_default_layout");
  1100. _new = memnew(Button);
  1101. _new->set_text(TTR("Create"));
  1102. _new->set_tooltip(TTR("Create a new Bus Layout."));
  1103. top_hb->add_child(_new);
  1104. _new->connect("pressed", this, "_new_layout");
  1105. bus_scroll = memnew(ScrollContainer);
  1106. bus_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  1107. bus_scroll->set_enable_h_scroll(true);
  1108. bus_scroll->set_enable_v_scroll(false);
  1109. add_child(bus_scroll);
  1110. bus_hb = memnew(HBoxContainer);
  1111. bus_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  1112. bus_scroll->add_child(bus_hb);
  1113. save_timer = memnew(Timer);
  1114. save_timer->set_wait_time(0.8);
  1115. save_timer->set_one_shot(true);
  1116. add_child(save_timer);
  1117. save_timer->connect("timeout", this, "_server_save");
  1118. set_v_size_flags(SIZE_EXPAND_FILL);
  1119. edited_path = ProjectSettings::get_singleton()->get("audio/default_bus_layout");
  1120. file_dialog = memnew(EditorFileDialog);
  1121. List<String> ext;
  1122. ResourceLoader::get_recognized_extensions_for_type("AudioBusLayout", &ext);
  1123. for (List<String>::Element *E = ext.front(); E; E = E->next()) {
  1124. file_dialog->add_filter("*." + E->get() + "; Audio Bus Layout");
  1125. }
  1126. add_child(file_dialog);
  1127. file_dialog->connect("file_selected", this, "_file_dialog_callback");
  1128. set_process(true);
  1129. }
  1130. void EditorAudioBuses::open_layout(const String &p_path) {
  1131. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1132. Ref<AudioBusLayout> state = ResourceLoader::load(p_path, "", true);
  1133. if (state.is_null()) {
  1134. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  1135. return;
  1136. }
  1137. edited_path = p_path;
  1138. file->set_text(p_path.get_file());
  1139. AudioServer::get_singleton()->set_bus_layout(state);
  1140. _update_buses();
  1141. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  1142. call_deferred("_select_layout");
  1143. }
  1144. void AudioBusesEditorPlugin::edit(Object *p_node) {
  1145. if (Object::cast_to<AudioBusLayout>(p_node)) {
  1146. String path = Object::cast_to<AudioBusLayout>(p_node)->get_path();
  1147. if (path.is_resource_file()) {
  1148. audio_bus_editor->open_layout(path);
  1149. }
  1150. }
  1151. }
  1152. bool AudioBusesEditorPlugin::handles(Object *p_node) const {
  1153. return (Object::cast_to<AudioBusLayout>(p_node) != NULL);
  1154. }
  1155. void AudioBusesEditorPlugin::make_visible(bool p_visible) {
  1156. }
  1157. AudioBusesEditorPlugin::AudioBusesEditorPlugin(EditorAudioBuses *p_node) {
  1158. audio_bus_editor = p_node;
  1159. }
  1160. AudioBusesEditorPlugin::~AudioBusesEditorPlugin() {
  1161. }
  1162. void EditorAudioMeterNotches::add_notch(float p_normalized_offset, float p_db_value, bool p_render_value) {
  1163. notches.push_back(AudioNotch(p_normalized_offset, p_db_value, p_render_value));
  1164. }
  1165. Size2 EditorAudioMeterNotches::get_minimum_size() const {
  1166. Ref<Font> font = get_font("font", "Label");
  1167. float font_height = font->get_height();
  1168. float width = 0;
  1169. float height = top_padding + btm_padding;
  1170. for (int i = 0; i < notches.size(); i++) {
  1171. if (notches[i].render_db_value) {
  1172. width = MAX(width, font->get_string_size(String::num(Math::abs(notches[i].db_value)) + "dB").x);
  1173. height += font_height;
  1174. }
  1175. }
  1176. width += line_length + label_space;
  1177. return Size2(width, height);
  1178. }
  1179. void EditorAudioMeterNotches::_bind_methods() {
  1180. ClassDB::bind_method("add_notch", &EditorAudioMeterNotches::add_notch);
  1181. ClassDB::bind_method("_draw_audio_notches", &EditorAudioMeterNotches::_draw_audio_notches);
  1182. }
  1183. void EditorAudioMeterNotches::_notification(int p_what) {
  1184. switch (p_what) {
  1185. case NOTIFICATION_THEME_CHANGED: {
  1186. notch_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(1, 1, 1) : Color(0, 0, 0);
  1187. } break;
  1188. case NOTIFICATION_DRAW: {
  1189. _draw_audio_notches();
  1190. } break;
  1191. }
  1192. }
  1193. void EditorAudioMeterNotches::_draw_audio_notches() {
  1194. Ref<Font> font = get_font("font", "Label");
  1195. float font_height = font->get_height();
  1196. for (int i = 0; i < notches.size(); i++) {
  1197. AudioNotch n = notches[i];
  1198. draw_line(Vector2(0, (1.0f - n.relative_position) * (get_size().y - btm_padding - top_padding) + top_padding),
  1199. Vector2(line_length, (1.0f - n.relative_position) * (get_size().y - btm_padding - top_padding) + top_padding),
  1200. notch_color,
  1201. 1);
  1202. if (n.render_db_value) {
  1203. draw_string(font,
  1204. Vector2(line_length + label_space,
  1205. (1.0f - n.relative_position) * (get_size().y - btm_padding - top_padding) + (font_height / 4) + top_padding),
  1206. String::num(Math::abs(n.db_value)) + "dB",
  1207. notch_color);
  1208. }
  1209. }
  1210. }
  1211. EditorAudioMeterNotches::EditorAudioMeterNotches() :
  1212. line_length(5.0f),
  1213. label_space(2.0f),
  1214. btm_padding(9.0f),
  1215. top_padding(5.0f) {
  1216. notch_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(1, 1, 1) : Color(0, 0, 0);
  1217. }