editor_audio_buses.cpp 55 KB

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