editor_audio_buses.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_node.h"
  32. #include "filesystem_dock.h"
  33. #include "io/resource_saver.h"
  34. #include "os/keyboard.h"
  35. #include "servers/audio_server.h"
  36. void EditorAudioBus::_notification(int p_what) {
  37. if (p_what == NOTIFICATION_READY) {
  38. for (int i = 0; i < cc; i++) {
  39. channel[i].vu_l->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  40. channel[i].vu_l->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  41. channel[i].vu_r->set_under_texture(get_icon("BusVuEmpty", "EditorIcons"));
  42. channel[i].vu_r->set_progress_texture(get_icon("BusVuFull", "EditorIcons"));
  43. channel[i].prev_active = true;
  44. }
  45. scale->set_texture(get_icon("BusVuDb", "EditorIcons"));
  46. disabled_vu = get_icon("BusVuFrozen", "EditorIcons");
  47. solo->set_icon(get_icon("AudioBusSolo", "EditorIcons"));
  48. mute->set_icon(get_icon("AudioBusMute", "EditorIcons"));
  49. bypass->set_icon(get_icon("AudioBusBypass", "EditorIcons"));
  50. bus_options->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons"));
  51. update_bus();
  52. set_process(true);
  53. }
  54. if (p_what == NOTIFICATION_DRAW) {
  55. if (has_focus()) {
  56. draw_style_box(get_stylebox("focus", "Button"), Rect2(Vector2(), get_size()));
  57. } else if (is_master) {
  58. draw_style_box(get_stylebox("disabled", "Button"), Rect2(Vector2(), get_size()));
  59. }
  60. }
  61. if (p_what == NOTIFICATION_PROCESS) {
  62. for (int i = 0; i < cc; i++) {
  63. float real_peak[2] = { -100, -100 };
  64. bool activity_found = false;
  65. if (AudioServer::get_singleton()->is_bus_channel_active(get_index(), i)) {
  66. activity_found = true;
  67. real_peak[0] = MAX(real_peak[0], AudioServer::get_singleton()->get_bus_peak_volume_left_db(get_index(), i));
  68. real_peak[1] = MAX(real_peak[1], AudioServer::get_singleton()->get_bus_peak_volume_right_db(get_index(), i));
  69. }
  70. if (real_peak[0] > channel[i].peak_l) {
  71. channel[i].peak_l = real_peak[0];
  72. } else {
  73. channel[i].peak_l -= get_process_delta_time() * 60.0;
  74. }
  75. if (real_peak[1] > channel[i].peak_r) {
  76. channel[i].peak_r = real_peak[1];
  77. } else {
  78. channel[i].peak_r -= get_process_delta_time() * 60.0;
  79. }
  80. channel[i].vu_l->set_value(channel[i].peak_l);
  81. channel[i].vu_r->set_value(channel[i].peak_r);
  82. if (activity_found != channel[i].prev_active) {
  83. if (activity_found) {
  84. channel[i].vu_l->set_over_texture(Ref<Texture>());
  85. channel[i].vu_r->set_over_texture(Ref<Texture>());
  86. } else {
  87. channel[i].vu_l->set_over_texture(disabled_vu);
  88. channel[i].vu_r->set_over_texture(disabled_vu);
  89. }
  90. channel[i].prev_active = activity_found;
  91. }
  92. }
  93. }
  94. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  95. for (int i = 0; i < 4; i++) {
  96. channel[i].peak_l = -100;
  97. channel[i].peak_r = -100;
  98. channel[i].prev_active = true;
  99. }
  100. set_process(is_visible_in_tree());
  101. }
  102. }
  103. void EditorAudioBus::update_send() {
  104. send->clear();
  105. if (is_master) {
  106. send->set_disabled(true);
  107. send->set_text(TTR("Speakers"));
  108. } else {
  109. send->set_disabled(false);
  110. StringName current_send = AudioServer::get_singleton()->get_bus_send(get_index());
  111. int current_send_index = 0; //by default to master
  112. for (int i = 0; i < get_index(); i++) {
  113. StringName send_name = AudioServer::get_singleton()->get_bus_name(i);
  114. send->add_item(send_name);
  115. if (send_name == current_send) {
  116. current_send_index = i;
  117. }
  118. }
  119. send->select(current_send_index);
  120. }
  121. }
  122. void EditorAudioBus::update_bus() {
  123. if (updating_bus)
  124. return;
  125. updating_bus = true;
  126. int index = get_index();
  127. slider->set_value(AudioServer::get_singleton()->get_bus_volume_db(index));
  128. track_name->set_text(AudioServer::get_singleton()->get_bus_name(index));
  129. if (is_master)
  130. track_name->set_editable(false);
  131. solo->set_pressed(AudioServer::get_singleton()->is_bus_solo(index));
  132. mute->set_pressed(AudioServer::get_singleton()->is_bus_mute(index));
  133. bypass->set_pressed(AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  134. // effects..
  135. effects->clear();
  136. TreeItem *root = effects->create_item();
  137. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  138. Ref<AudioEffect> afx = AudioServer::get_singleton()->get_bus_effect(index, i);
  139. TreeItem *fx = effects->create_item(root);
  140. fx->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  141. fx->set_editable(0, true);
  142. fx->set_checked(0, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  143. fx->set_text(0, afx->get_name());
  144. fx->set_metadata(0, i);
  145. }
  146. TreeItem *add = effects->create_item(root);
  147. add->set_cell_mode(0, TreeItem::CELL_MODE_CUSTOM);
  148. add->set_editable(0, true);
  149. add->set_selectable(0, false);
  150. add->set_text(0, TTR("Add Effect"));
  151. update_send();
  152. updating_bus = false;
  153. }
  154. void EditorAudioBus::_name_changed(const String &p_new_name) {
  155. if (p_new_name == AudioServer::get_singleton()->get_bus_name(get_index()))
  156. return;
  157. String attempt = p_new_name;
  158. int attempts = 1;
  159. while (true) {
  160. bool name_free = true;
  161. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  162. if (AudioServer::get_singleton()->get_bus_name(i) == attempt) {
  163. name_free = false;
  164. break;
  165. }
  166. }
  167. if (name_free) {
  168. break;
  169. }
  170. attempts++;
  171. attempt = p_new_name + " " + itos(attempts);
  172. }
  173. updating_bus = true;
  174. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  175. StringName current = AudioServer::get_singleton()->get_bus_name(get_index());
  176. ur->create_action(TTR("Rename Audio Bus"));
  177. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", get_index(), attempt);
  178. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", get_index(), current);
  179. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  180. if (AudioServer::get_singleton()->get_bus_send(i) == current) {
  181. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", i, attempt);
  182. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", i, current);
  183. }
  184. }
  185. ur->add_do_method(buses, "_update_bus", get_index());
  186. ur->add_undo_method(buses, "_update_bus", get_index());
  187. ur->add_do_method(buses, "_update_sends");
  188. ur->add_undo_method(buses, "_update_sends");
  189. ur->commit_action();
  190. updating_bus = false;
  191. track_name->release_focus();
  192. }
  193. void EditorAudioBus::_volume_db_changed(float p_db) {
  194. if (updating_bus)
  195. return;
  196. updating_bus = true;
  197. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  198. ur->create_action("Change Audio Bus Volume", UndoRedo::MERGE_ENDS);
  199. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), p_db);
  200. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", get_index(), AudioServer::get_singleton()->get_bus_volume_db(get_index()));
  201. ur->add_do_method(buses, "_update_bus", get_index());
  202. ur->add_undo_method(buses, "_update_bus", get_index());
  203. ur->commit_action();
  204. updating_bus = false;
  205. }
  206. void EditorAudioBus::_solo_toggled() {
  207. updating_bus = true;
  208. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  209. ur->create_action(TTR("Toggle Audio Bus Solo"));
  210. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), solo->is_pressed());
  211. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", get_index(), AudioServer::get_singleton()->is_bus_solo(get_index()));
  212. ur->add_do_method(buses, "_update_bus", get_index());
  213. ur->add_undo_method(buses, "_update_bus", get_index());
  214. ur->commit_action();
  215. updating_bus = false;
  216. }
  217. void EditorAudioBus::_mute_toggled() {
  218. updating_bus = true;
  219. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  220. ur->create_action(TTR("Toggle Audio Bus Mute"));
  221. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), mute->is_pressed());
  222. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", get_index(), AudioServer::get_singleton()->is_bus_mute(get_index()));
  223. ur->add_do_method(buses, "_update_bus", get_index());
  224. ur->add_undo_method(buses, "_update_bus", get_index());
  225. ur->commit_action();
  226. updating_bus = false;
  227. }
  228. void EditorAudioBus::_bypass_toggled() {
  229. updating_bus = true;
  230. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  231. ur->create_action(TTR("Toggle Audio Bus Bypass Effects"));
  232. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), bypass->is_pressed());
  233. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", get_index(), AudioServer::get_singleton()->is_bus_bypassing_effects(get_index()));
  234. ur->add_do_method(buses, "_update_bus", get_index());
  235. ur->add_undo_method(buses, "_update_bus", get_index());
  236. ur->commit_action();
  237. updating_bus = false;
  238. }
  239. void EditorAudioBus::_send_selected(int p_which) {
  240. updating_bus = true;
  241. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  242. ur->create_action("Select Audio Bus Send");
  243. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", get_index(), send->get_item_text(p_which));
  244. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", get_index(), AudioServer::get_singleton()->get_bus_send(get_index()));
  245. ur->add_do_method(buses, "_update_bus", get_index());
  246. ur->add_undo_method(buses, "_update_bus", get_index());
  247. ur->commit_action();
  248. updating_bus = false;
  249. }
  250. void EditorAudioBus::_effect_selected() {
  251. TreeItem *effect = effects->get_selected();
  252. if (!effect)
  253. return;
  254. updating_bus = true;
  255. if (effect->get_metadata(0) != Variant()) {
  256. int index = effect->get_metadata(0);
  257. Ref<AudioEffect> effect = AudioServer::get_singleton()->get_bus_effect(get_index(), index);
  258. if (effect.is_valid()) {
  259. EditorNode::get_singleton()->push_item(effect.ptr());
  260. }
  261. }
  262. updating_bus = false;
  263. }
  264. void EditorAudioBus::_effect_edited() {
  265. if (updating_bus)
  266. return;
  267. TreeItem *effect = effects->get_edited();
  268. if (!effect)
  269. return;
  270. if (effect->get_metadata(0) == Variant()) {
  271. Rect2 area = effects->get_item_rect(effect);
  272. effect_options->set_position(effects->get_global_position() + area.position + Vector2(0, area.size.y));
  273. effect_options->popup();
  274. //add effect
  275. } else {
  276. int index = effect->get_metadata(0);
  277. updating_bus = true;
  278. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  279. ur->create_action(TTR("Select Audio Bus Send"));
  280. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, effect->is_checked(0));
  281. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  282. ur->add_do_method(buses, "_update_bus", get_index());
  283. ur->add_undo_method(buses, "_update_bus", get_index());
  284. ur->commit_action();
  285. updating_bus = false;
  286. }
  287. }
  288. void EditorAudioBus::_effect_add(int p_which) {
  289. if (updating_bus)
  290. return;
  291. StringName name = effect_options->get_item_metadata(p_which);
  292. Object *fx = ClassDB::instance(name);
  293. ERR_FAIL_COND(!fx);
  294. AudioEffect *afx = Object::cast_to<AudioEffect>(fx);
  295. ERR_FAIL_COND(!afx);
  296. Ref<AudioEffect> afxr = Ref<AudioEffect>(afx);
  297. afxr->set_name(effect_options->get_item_text(p_which));
  298. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  299. ur->create_action(TTR("Add Audio Bus Effect"));
  300. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), afxr, -1);
  301. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect_count(get_index()));
  302. ur->add_do_method(buses, "_update_bus", get_index());
  303. ur->add_undo_method(buses, "_update_bus", get_index());
  304. ur->commit_action();
  305. }
  306. void EditorAudioBus::_gui_input(const Ref<InputEvent> &p_event) {
  307. Ref<InputEventKey> k = p_event;
  308. if (k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && !k->is_echo()) {
  309. accept_event();
  310. emit_signal("delete_request");
  311. }
  312. Ref<InputEventMouseButton> mb = p_event;
  313. if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) {
  314. Vector2 pos = Vector2(mb->get_position().x, mb->get_position().y);
  315. bus_popup->set_position(get_global_position() + pos);
  316. bus_popup->popup();
  317. }
  318. }
  319. void EditorAudioBus::_bus_popup_pressed(int p_option) {
  320. if (p_option == 2) {
  321. // Reset volume
  322. emit_signal("vol_reset_request");
  323. } else if (p_option == 1) {
  324. emit_signal("delete_request");
  325. } else if (p_option == 0) {
  326. //duplicate
  327. emit_signal("duplicate_request", get_index());
  328. }
  329. }
  330. Variant EditorAudioBus::get_drag_data(const Point2 &p_point) {
  331. if (get_index() == 0) {
  332. return Variant();
  333. }
  334. Control *c = memnew(Control);
  335. Panel *p = memnew(Panel);
  336. c->add_child(p);
  337. p->add_style_override("panel", get_stylebox("focus", "Button"));
  338. p->set_size(get_size());
  339. p->set_position(-p_point);
  340. set_drag_preview(c);
  341. Dictionary d;
  342. d["type"] = "move_audio_bus";
  343. d["index"] = get_index();
  344. emit_signal("drop_end_request");
  345. return d;
  346. }
  347. bool EditorAudioBus::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  348. if (get_index() == 0)
  349. return false;
  350. Dictionary d = p_data;
  351. if (d.has("type") && String(d["type"]) == "move_audio_bus") {
  352. return true;
  353. }
  354. return false;
  355. }
  356. void EditorAudioBus::drop_data(const Point2 &p_point, const Variant &p_data) {
  357. Dictionary d = p_data;
  358. emit_signal("dropped", d["index"], get_index());
  359. }
  360. Variant EditorAudioBus::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  361. print_line("drag fw");
  362. TreeItem *item = effects->get_item_at_position(p_point);
  363. if (!item) {
  364. print_line("no item");
  365. return Variant();
  366. }
  367. Variant md = item->get_metadata(0);
  368. if (md.get_type() == Variant::INT) {
  369. Dictionary fxd;
  370. fxd["type"] = "audio_bus_effect";
  371. fxd["bus"] = get_index();
  372. fxd["effect"] = md;
  373. Label *l = memnew(Label);
  374. l->set_text(item->get_text(0));
  375. effects->set_drag_preview(l);
  376. return fxd;
  377. }
  378. return Variant();
  379. }
  380. bool EditorAudioBus::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  381. Dictionary d = p_data;
  382. if (!d.has("type") || String(d["type"]) != "audio_bus_effect")
  383. return false;
  384. TreeItem *item = effects->get_item_at_position(p_point);
  385. if (!item)
  386. return false;
  387. effects->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  388. return true;
  389. }
  390. void EditorAudioBus::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  391. Dictionary d = p_data;
  392. TreeItem *item = effects->get_item_at_position(p_point);
  393. if (!item)
  394. return;
  395. int pos = effects->get_drop_section_at_position(p_point);
  396. Variant md = item->get_metadata(0);
  397. int paste_at;
  398. int bus = d["bus"];
  399. int effect = d["effect"];
  400. if (md.get_type() == Variant::INT) {
  401. paste_at = md;
  402. if (pos > 0)
  403. paste_at++;
  404. if (bus == get_index() && paste_at > effect) {
  405. paste_at--;
  406. }
  407. } else {
  408. paste_at = -1;
  409. }
  410. bool enabled = AudioServer::get_singleton()->is_bus_effect_enabled(bus, effect);
  411. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  412. ur->create_action(TTR("Move Bus Effect"));
  413. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", bus, effect);
  414. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(bus, effect), paste_at);
  415. if (paste_at == -1) {
  416. paste_at = AudioServer::get_singleton()->get_bus_effect_count(get_index());
  417. if (bus == get_index()) {
  418. paste_at--;
  419. }
  420. }
  421. if (!enabled) {
  422. ur->add_do_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), paste_at, false);
  423. }
  424. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), paste_at);
  425. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", bus, AudioServer::get_singleton()->get_bus_effect(bus, effect), effect);
  426. if (!enabled) {
  427. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", bus, effect, false);
  428. }
  429. ur->add_do_method(buses, "_update_bus", get_index());
  430. ur->add_undo_method(buses, "_update_bus", get_index());
  431. if (get_index() != bus) {
  432. ur->add_do_method(buses, "_update_bus", bus);
  433. ur->add_undo_method(buses, "_update_bus", bus);
  434. }
  435. ur->commit_action();
  436. }
  437. void EditorAudioBus::_delete_effect_pressed(int p_option) {
  438. TreeItem *item = effects->get_selected();
  439. if (!item)
  440. return;
  441. if (item->get_metadata(0).get_type() != Variant::INT)
  442. return;
  443. int index = item->get_metadata(0);
  444. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  445. ur->create_action(TTR("Delete Bus Effect"));
  446. ur->add_do_method(AudioServer::get_singleton(), "remove_bus_effect", get_index(), index);
  447. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", get_index(), AudioServer::get_singleton()->get_bus_effect(get_index(), index), index);
  448. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", get_index(), index, AudioServer::get_singleton()->is_bus_effect_enabled(get_index(), index));
  449. ur->add_do_method(buses, "_update_bus", get_index());
  450. ur->add_undo_method(buses, "_update_bus", get_index());
  451. ur->commit_action();
  452. }
  453. void EditorAudioBus::_effect_rmb(const Vector2 &p_pos) {
  454. TreeItem *item = effects->get_selected();
  455. if (!item)
  456. return;
  457. if (item->get_metadata(0).get_type() != Variant::INT)
  458. return;
  459. delete_effect_popup->set_position(get_global_mouse_position());
  460. delete_effect_popup->popup();
  461. }
  462. void EditorAudioBus::_bind_methods() {
  463. ClassDB::bind_method("update_bus", &EditorAudioBus::update_bus);
  464. ClassDB::bind_method("update_send", &EditorAudioBus::update_send);
  465. ClassDB::bind_method("_name_changed", &EditorAudioBus::_name_changed);
  466. ClassDB::bind_method("_volume_db_changed", &EditorAudioBus::_volume_db_changed);
  467. ClassDB::bind_method("_solo_toggled", &EditorAudioBus::_solo_toggled);
  468. ClassDB::bind_method("_mute_toggled", &EditorAudioBus::_mute_toggled);
  469. ClassDB::bind_method("_bypass_toggled", &EditorAudioBus::_bypass_toggled);
  470. ClassDB::bind_method("_name_focus_exit", &EditorAudioBus::_name_focus_exit);
  471. ClassDB::bind_method("_send_selected", &EditorAudioBus::_send_selected);
  472. ClassDB::bind_method("_effect_edited", &EditorAudioBus::_effect_edited);
  473. ClassDB::bind_method("_effect_selected", &EditorAudioBus::_effect_selected);
  474. ClassDB::bind_method("_effect_add", &EditorAudioBus::_effect_add);
  475. ClassDB::bind_method("_gui_input", &EditorAudioBus::_gui_input);
  476. ClassDB::bind_method("_bus_popup_pressed", &EditorAudioBus::_bus_popup_pressed);
  477. ClassDB::bind_method("get_drag_data_fw", &EditorAudioBus::get_drag_data_fw);
  478. ClassDB::bind_method("can_drop_data_fw", &EditorAudioBus::can_drop_data_fw);
  479. ClassDB::bind_method("drop_data_fw", &EditorAudioBus::drop_data_fw);
  480. ClassDB::bind_method("_delete_effect_pressed", &EditorAudioBus::_delete_effect_pressed);
  481. ClassDB::bind_method("_effect_rmb", &EditorAudioBus::_effect_rmb);
  482. ADD_SIGNAL(MethodInfo("duplicate_request"));
  483. ADD_SIGNAL(MethodInfo("delete_request"));
  484. ADD_SIGNAL(MethodInfo("vol_reset_request"));
  485. ADD_SIGNAL(MethodInfo("drop_end_request"));
  486. ADD_SIGNAL(MethodInfo("dropped"));
  487. }
  488. EditorAudioBus::EditorAudioBus(EditorAudioBuses *p_buses, bool p_is_master) {
  489. buses = p_buses;
  490. updating_bus = false;
  491. is_master = p_is_master;
  492. set_tooltip(TTR("Audio Bus, Drag and Drop to rearrange."));
  493. VBoxContainer *vb = memnew(VBoxContainer);
  494. add_child(vb);
  495. set_v_size_flags(SIZE_EXPAND_FILL);
  496. set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  497. track_name = memnew(LineEdit);
  498. track_name->connect("text_entered", this, "_name_changed");
  499. track_name->connect("focus_exited", this, "_name_focus_exit");
  500. vb->add_child(track_name);
  501. HBoxContainer *hbc = memnew(HBoxContainer);
  502. vb->add_child(hbc);
  503. solo = memnew(ToolButton);
  504. solo->set_toggle_mode(true);
  505. solo->set_tooltip(TTR("Solo"));
  506. solo->set_focus_mode(FOCUS_NONE);
  507. solo->connect("pressed", this, "_solo_toggled");
  508. hbc->add_child(solo);
  509. mute = memnew(ToolButton);
  510. mute->set_toggle_mode(true);
  511. mute->set_tooltip(TTR("Mute"));
  512. mute->set_focus_mode(FOCUS_NONE);
  513. mute->connect("pressed", this, "_mute_toggled");
  514. hbc->add_child(mute);
  515. bypass = memnew(ToolButton);
  516. bypass->set_toggle_mode(true);
  517. bypass->set_tooltip(TTR("Bypass"));
  518. bypass->set_focus_mode(FOCUS_NONE);
  519. bypass->connect("pressed", this, "_bypass_toggled");
  520. hbc->add_child(bypass);
  521. hbc->add_spacer();
  522. bus_options = memnew(MenuButton);
  523. bus_options->set_h_size_flags(SIZE_SHRINK_END);
  524. bus_options->set_anchor(MARGIN_RIGHT, 0.0);
  525. bus_options->set_tooltip(TTR("Bus options"));
  526. hbc->add_child(bus_options);
  527. Ref<StyleBoxEmpty> sbempty = memnew(StyleBoxEmpty);
  528. for (int i = 0; i < hbc->get_child_count(); i++) {
  529. Control *child = Object::cast_to<Control>(hbc->get_child(i));
  530. child->add_style_override("normal", sbempty);
  531. child->add_style_override("hover", sbempty);
  532. child->add_style_override("focus", sbempty);
  533. child->add_style_override("pressed", sbempty);
  534. }
  535. vb->add_child(memnew(HSeparator));
  536. HBoxContainer *hb = memnew(HBoxContainer);
  537. vb->add_child(hb);
  538. slider = memnew(VSlider);
  539. slider->set_min(-80);
  540. slider->set_max(24);
  541. slider->set_step(0.1);
  542. slider->connect("value_changed", this, "_volume_db_changed");
  543. hb->add_child(slider);
  544. cc = AudioServer::get_singleton()->get_channel_count();
  545. for (int i = 0; i < cc; i++) {
  546. channel[i].vu_l = memnew(TextureProgress);
  547. channel[i].vu_l->set_fill_mode(TextureProgress::FILL_BOTTOM_TO_TOP);
  548. hb->add_child(channel[i].vu_l);
  549. channel[i].vu_l->set_min(-80);
  550. channel[i].vu_l->set_max(24);
  551. channel[i].vu_l->set_step(0.1);
  552. channel[i].vu_r = memnew(TextureProgress);
  553. channel[i].vu_r->set_fill_mode(TextureProgress::FILL_BOTTOM_TO_TOP);
  554. hb->add_child(channel[i].vu_r);
  555. channel[i].vu_r->set_min(-80);
  556. channel[i].vu_r->set_max(24);
  557. channel[i].vu_r->set_step(0.1);
  558. }
  559. scale = memnew(TextureRect);
  560. hb->add_child(scale);
  561. effects = memnew(Tree);
  562. effects->set_hide_root(true);
  563. effects->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  564. effects->set_hide_folding(true);
  565. vb->add_child(effects);
  566. effects->connect("item_edited", this, "_effect_edited");
  567. effects->connect("cell_selected", this, "_effect_selected");
  568. effects->set_edit_checkbox_cell_only_when_checkbox_is_pressed(true);
  569. effects->set_drag_forwarding(this);
  570. effects->connect("item_rmb_selected", this, "_effect_rmb");
  571. effects->set_allow_rmb_select(true);
  572. effects->set_focus_mode(FOCUS_CLICK);
  573. effects->set_allow_reselect(true);
  574. send = memnew(OptionButton);
  575. send->set_clip_text(true);
  576. send->connect("item_selected", this, "_send_selected");
  577. vb->add_child(send);
  578. set_focus_mode(FOCUS_CLICK);
  579. effect_options = memnew(PopupMenu);
  580. effect_options->connect("index_pressed", this, "_effect_add");
  581. add_child(effect_options);
  582. List<StringName> effects;
  583. ClassDB::get_inheriters_from_class("AudioEffect", &effects);
  584. effects.sort_custom<StringName::AlphCompare>();
  585. for (List<StringName>::Element *E = effects.front(); E; E = E->next()) {
  586. if (!ClassDB::can_instance(E->get()))
  587. continue;
  588. Ref<Texture> icon;
  589. if (has_icon(E->get(), "EditorIcons")) {
  590. icon = get_icon(E->get(), "EditorIcons");
  591. }
  592. String name = E->get().operator String().replace("AudioEffect", "");
  593. effect_options->add_item(name);
  594. effect_options->set_item_metadata(effect_options->get_item_count() - 1, E->get());
  595. effect_options->set_item_icon(effect_options->get_item_count() - 1, icon);
  596. }
  597. bus_popup = bus_options->get_popup();
  598. bus_popup->add_item(TTR("Duplicate"));
  599. bus_popup->add_item(TTR("Delete"));
  600. bus_popup->set_item_disabled(1, is_master);
  601. bus_popup->add_item(TTR("Reset Volume"));
  602. bus_popup->connect("index_pressed", this, "_bus_popup_pressed");
  603. delete_effect_popup = memnew(PopupMenu);
  604. delete_effect_popup->add_item(TTR("Delete Effect"));
  605. add_child(delete_effect_popup);
  606. delete_effect_popup->connect("index_pressed", this, "_delete_effect_pressed");
  607. }
  608. bool EditorAudioBusDrop::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  609. Dictionary d = p_data;
  610. if (d.has("type") && String(d["type"]) == "move_audio_bus") {
  611. return true;
  612. }
  613. return false;
  614. }
  615. void EditorAudioBusDrop::drop_data(const Point2 &p_point, const Variant &p_data) {
  616. Dictionary d = p_data;
  617. emit_signal("dropped", d["index"], -1);
  618. }
  619. void EditorAudioBusDrop::_bind_methods() {
  620. ADD_SIGNAL(MethodInfo("dropped"));
  621. }
  622. EditorAudioBusDrop::EditorAudioBusDrop() {
  623. }
  624. void EditorAudioBuses::_update_buses() {
  625. while (bus_hb->get_child_count() > 0) {
  626. memdelete(bus_hb->get_child(0));
  627. }
  628. drop_end = NULL;
  629. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  630. bool is_master = i == 0 ? true : false;
  631. EditorAudioBus *audio_bus = memnew(EditorAudioBus(this, is_master));
  632. bus_hb->add_child(audio_bus);
  633. audio_bus->connect("delete_request", this, "_delete_bus", varray(audio_bus), CONNECT_DEFERRED);
  634. audio_bus->connect("duplicate_request", this, "_duplicate_bus", varray(), CONNECT_DEFERRED);
  635. audio_bus->connect("vol_reset_request", this, "_reset_bus_volume", varray(audio_bus), CONNECT_DEFERRED);
  636. audio_bus->connect("drop_end_request", this, "_request_drop_end");
  637. audio_bus->connect("dropped", this, "_drop_at_index", varray(), CONNECT_DEFERRED);
  638. }
  639. }
  640. EditorAudioBuses *EditorAudioBuses::register_editor() {
  641. EditorAudioBuses *audio_buses = memnew(EditorAudioBuses);
  642. EditorNode::get_singleton()->add_bottom_panel_item("Audio", audio_buses);
  643. return audio_buses;
  644. }
  645. void EditorAudioBuses::_notification(int p_what) {
  646. if (p_what == NOTIFICATION_READY) {
  647. _update_buses();
  648. }
  649. if (p_what == NOTIFICATION_DRAG_END) {
  650. if (drop_end) {
  651. drop_end->queue_delete();
  652. drop_end = NULL;
  653. }
  654. }
  655. if (p_what == NOTIFICATION_PROCESS) {
  656. //check if anything was edited
  657. bool edited = AudioServer::get_singleton()->is_edited();
  658. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  659. for (int j = 0; j < AudioServer::get_singleton()->get_bus_effect_count(i); j++) {
  660. Ref<AudioEffect> effect = AudioServer::get_singleton()->get_bus_effect(i, j);
  661. if (effect->is_edited()) {
  662. edited = true;
  663. effect->set_edited(false);
  664. }
  665. }
  666. }
  667. AudioServer::get_singleton()->set_edited(false);
  668. if (edited) {
  669. save_timer->start();
  670. }
  671. }
  672. }
  673. void EditorAudioBuses::_add_bus() {
  674. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  675. //need to simulate new name, so we can undi :(
  676. ur->create_action(TTR("Add Audio Bus"));
  677. ur->add_do_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count() + 1);
  678. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_count", AudioServer::get_singleton()->get_bus_count());
  679. ur->add_do_method(this, "_update_buses");
  680. ur->add_undo_method(this, "_update_buses");
  681. ur->commit_action();
  682. }
  683. void EditorAudioBuses::_update_bus(int p_index) {
  684. if (p_index >= bus_hb->get_child_count())
  685. return;
  686. bus_hb->get_child(p_index)->call("update_bus");
  687. }
  688. void EditorAudioBuses::_update_sends() {
  689. for (int i = 0; i < bus_hb->get_child_count(); i++) {
  690. bus_hb->get_child(i)->call("update_send");
  691. }
  692. }
  693. void EditorAudioBuses::_delete_bus(Object *p_which) {
  694. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  695. int index = bus->get_index();
  696. if (index == 0) {
  697. EditorNode::get_singleton()->show_warning(TTR("Master bus can't be deleted!"));
  698. return;
  699. }
  700. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  701. ur->create_action(TTR("Delete Audio Bus"));
  702. ur->add_do_method(AudioServer::get_singleton(), "remove_bus", index);
  703. ur->add_undo_method(AudioServer::get_singleton(), "add_bus", index);
  704. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_name", index, AudioServer::get_singleton()->get_bus_name(index));
  705. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  706. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_send", index, AudioServer::get_singleton()->get_bus_send(index));
  707. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_solo", index, AudioServer::get_singleton()->is_bus_solo(index));
  708. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_mute", index, AudioServer::get_singleton()->is_bus_mute(index));
  709. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_bypass_effects", index, AudioServer::get_singleton()->is_bus_bypassing_effects(index));
  710. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(index); i++) {
  711. ur->add_undo_method(AudioServer::get_singleton(), "add_bus_effect", index, AudioServer::get_singleton()->get_bus_effect(index, i));
  712. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_effect_enabled", index, i, AudioServer::get_singleton()->is_bus_effect_enabled(index, i));
  713. }
  714. ur->add_do_method(this, "_update_buses");
  715. ur->add_undo_method(this, "_update_buses");
  716. ur->commit_action();
  717. }
  718. void EditorAudioBuses::_duplicate_bus(int p_which) {
  719. int add_at_pos = p_which + 1;
  720. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  721. ur->create_action(TTR("Duplicate Audio Bus"));
  722. ur->add_do_method(AudioServer::get_singleton(), "add_bus", add_at_pos);
  723. ur->add_do_method(AudioServer::get_singleton(), "set_bus_name", add_at_pos, AudioServer::get_singleton()->get_bus_name(p_which) + " Copy");
  724. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", add_at_pos, AudioServer::get_singleton()->get_bus_volume_db(p_which));
  725. ur->add_do_method(AudioServer::get_singleton(), "set_bus_send", add_at_pos, AudioServer::get_singleton()->get_bus_send(p_which));
  726. ur->add_do_method(AudioServer::get_singleton(), "set_bus_solo", add_at_pos, AudioServer::get_singleton()->is_bus_solo(p_which));
  727. ur->add_do_method(AudioServer::get_singleton(), "set_bus_mute", add_at_pos, AudioServer::get_singleton()->is_bus_mute(p_which));
  728. ur->add_do_method(AudioServer::get_singleton(), "set_bus_bypass_effects", add_at_pos, AudioServer::get_singleton()->is_bus_bypassing_effects(p_which));
  729. for (int i = 0; i < AudioServer::get_singleton()->get_bus_effect_count(p_which); i++) {
  730. ur->add_do_method(AudioServer::get_singleton(), "add_bus_effect", add_at_pos, AudioServer::get_singleton()->get_bus_effect(p_which, i));
  731. 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));
  732. }
  733. ur->add_undo_method(AudioServer::get_singleton(), "remove_bus", add_at_pos);
  734. ur->add_do_method(this, "_update_buses");
  735. ur->add_undo_method(this, "_update_buses");
  736. ur->commit_action();
  737. }
  738. void EditorAudioBuses::_reset_bus_volume(Object *p_which) {
  739. EditorAudioBus *bus = Object::cast_to<EditorAudioBus>(p_which);
  740. int index = bus->get_index();
  741. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  742. ur->create_action(TTR("Reset Bus Volume"));
  743. ur->add_do_method(AudioServer::get_singleton(), "set_bus_volume_db", index, 0.f);
  744. ur->add_undo_method(AudioServer::get_singleton(), "set_bus_volume_db", index, AudioServer::get_singleton()->get_bus_volume_db(index));
  745. ur->add_do_method(this, "_update_buses");
  746. ur->add_undo_method(this, "_update_buses");
  747. ur->commit_action();
  748. }
  749. void EditorAudioBuses::_request_drop_end() {
  750. if (!drop_end && bus_hb->get_child_count()) {
  751. drop_end = memnew(EditorAudioBusDrop);
  752. bus_hb->add_child(drop_end);
  753. drop_end->set_custom_minimum_size(Object::cast_to<Control>(bus_hb->get_child(0))->get_size());
  754. drop_end->connect("dropped", this, "_drop_at_index", varray(), CONNECT_DEFERRED);
  755. }
  756. }
  757. void EditorAudioBuses::_drop_at_index(int p_bus, int p_index) {
  758. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  759. //need to simulate new name, so we can undi :(
  760. ur->create_action(TTR("Move Audio Bus"));
  761. ur->add_do_method(AudioServer::get_singleton(), "move_bus", p_bus, p_index);
  762. int final_pos;
  763. if (p_index == p_bus) {
  764. final_pos = p_bus;
  765. } else if (p_index == -1) {
  766. final_pos = AudioServer::get_singleton()->get_bus_count() - 1;
  767. } else if (p_index < p_bus) {
  768. final_pos = p_index;
  769. } else {
  770. final_pos = p_index - 1;
  771. }
  772. ur->add_undo_method(AudioServer::get_singleton(), "move_bus", final_pos, p_bus);
  773. ur->add_do_method(this, "_update_buses");
  774. ur->add_undo_method(this, "_update_buses");
  775. ur->commit_action();
  776. }
  777. void EditorAudioBuses::_server_save() {
  778. Ref<AudioBusLayout> state = AudioServer::get_singleton()->generate_bus_layout();
  779. ResourceSaver::save(edited_path, state);
  780. }
  781. void EditorAudioBuses::_select_layout() {
  782. EditorNode::get_singleton()->get_filesystem_dock()->select_file(edited_path);
  783. }
  784. void EditorAudioBuses::_save_as_layout() {
  785. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  786. file_dialog->set_title(TTR("Save Audio Bus Layout As.."));
  787. file_dialog->set_current_path(edited_path);
  788. file_dialog->popup_centered_ratio();
  789. new_layout = false;
  790. }
  791. void EditorAudioBuses::_new_layout() {
  792. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  793. file_dialog->set_title(TTR("Location for New Layout.."));
  794. file_dialog->set_current_path(edited_path);
  795. file_dialog->popup_centered_ratio();
  796. new_layout = true;
  797. }
  798. void EditorAudioBuses::_load_layout() {
  799. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  800. file_dialog->set_title(TTR("Open Audio Bus Layout"));
  801. file_dialog->set_current_path(edited_path);
  802. file_dialog->popup_centered_ratio();
  803. new_layout = false;
  804. }
  805. void EditorAudioBuses::_load_default_layout() {
  806. Ref<AudioBusLayout> state = ResourceLoader::load("res://default_bus_layout.tres");
  807. if (state.is_null()) {
  808. EditorNode::get_singleton()->show_warning(TTR("There is no 'res://default_bus_layout.tres' file."));
  809. return;
  810. }
  811. edited_path = "res://default_bus_layout.tres";
  812. file->set_text(edited_path.get_file());
  813. AudioServer::get_singleton()->set_bus_layout(state);
  814. _update_buses();
  815. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  816. call_deferred("_select_layout");
  817. }
  818. void EditorAudioBuses::_file_dialog_callback(const String &p_string) {
  819. if (file_dialog->get_mode() == EditorFileDialog::MODE_OPEN_FILE) {
  820. Ref<AudioBusLayout> state = ResourceLoader::load(p_string);
  821. if (state.is_null()) {
  822. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  823. return;
  824. }
  825. edited_path = p_string;
  826. file->set_text(p_string.get_file());
  827. AudioServer::get_singleton()->set_bus_layout(state);
  828. _update_buses();
  829. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  830. call_deferred("_select_layout");
  831. } else if (file_dialog->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  832. if (new_layout) {
  833. Ref<AudioBusLayout> empty_state;
  834. empty_state.instance();
  835. AudioServer::get_singleton()->set_bus_layout(empty_state);
  836. }
  837. Error err = ResourceSaver::save(p_string, AudioServer::get_singleton()->generate_bus_layout());
  838. if (err != OK) {
  839. EditorNode::get_singleton()->show_warning("Error saving file: " + p_string);
  840. return;
  841. }
  842. edited_path = p_string;
  843. file->set_text(p_string.get_file());
  844. _update_buses();
  845. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  846. call_deferred("_select_layout");
  847. }
  848. }
  849. void EditorAudioBuses::_bind_methods() {
  850. ClassDB::bind_method("_add_bus", &EditorAudioBuses::_add_bus);
  851. ClassDB::bind_method("_update_buses", &EditorAudioBuses::_update_buses);
  852. ClassDB::bind_method("_update_bus", &EditorAudioBuses::_update_bus);
  853. ClassDB::bind_method("_update_sends", &EditorAudioBuses::_update_sends);
  854. ClassDB::bind_method("_delete_bus", &EditorAudioBuses::_delete_bus);
  855. ClassDB::bind_method("_request_drop_end", &EditorAudioBuses::_request_drop_end);
  856. ClassDB::bind_method("_drop_at_index", &EditorAudioBuses::_drop_at_index);
  857. ClassDB::bind_method("_server_save", &EditorAudioBuses::_server_save);
  858. ClassDB::bind_method("_select_layout", &EditorAudioBuses::_select_layout);
  859. ClassDB::bind_method("_save_as_layout", &EditorAudioBuses::_save_as_layout);
  860. ClassDB::bind_method("_load_layout", &EditorAudioBuses::_load_layout);
  861. ClassDB::bind_method("_load_default_layout", &EditorAudioBuses::_load_default_layout);
  862. ClassDB::bind_method("_new_layout", &EditorAudioBuses::_new_layout);
  863. ClassDB::bind_method("_duplicate_bus", &EditorAudioBuses::_duplicate_bus);
  864. ClassDB::bind_method("_reset_bus_volume", &EditorAudioBuses::_reset_bus_volume);
  865. ClassDB::bind_method("_file_dialog_callback", &EditorAudioBuses::_file_dialog_callback);
  866. }
  867. EditorAudioBuses::EditorAudioBuses() {
  868. drop_end = NULL;
  869. top_hb = memnew(HBoxContainer);
  870. add_child(top_hb);
  871. file = memnew(ToolButton);
  872. file->set_text("default_bus_layout.tres");
  873. top_hb->add_child(file);
  874. file->connect("pressed", this, "_select_layout");
  875. add = memnew(Button);
  876. top_hb->add_child(add);
  877. add->set_text(TTR("Add Bus"));
  878. add->set_tooltip(TTR("Create a new Bus Layout."));
  879. add->connect("pressed", this, "_add_bus");
  880. top_hb->add_spacer();
  881. load = memnew(Button);
  882. load->set_text(TTR("Load"));
  883. load->set_tooltip(TTR("Load an existing Bus Layout."));
  884. top_hb->add_child(load);
  885. load->connect("pressed", this, "_load_layout");
  886. save_as = memnew(Button);
  887. save_as->set_text(TTR("Save As"));
  888. save_as->set_tooltip(TTR("Save this Bus Layout to a file."));
  889. top_hb->add_child(save_as);
  890. save_as->connect("pressed", this, "_save_as_layout");
  891. _default = memnew(Button);
  892. _default->set_text(TTR("Load Default"));
  893. _default->set_tooltip(TTR("Load the default Bus Layout."));
  894. top_hb->add_child(_default);
  895. _default->connect("pressed", this, "_load_default_layout");
  896. _new = memnew(Button);
  897. _new->set_text(TTR("Create"));
  898. _new->set_tooltip(TTR("Create a new Bus Layout."));
  899. top_hb->add_child(_new);
  900. _new->connect("pressed", this, "_new_layout");
  901. bus_scroll = memnew(ScrollContainer);
  902. bus_scroll->add_style_override("panel", memnew(StyleBoxEmpty));
  903. bus_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  904. bus_scroll->set_enable_h_scroll(true);
  905. bus_scroll->set_enable_v_scroll(false);
  906. add_child(bus_scroll);
  907. bus_hb = memnew(HBoxContainer);
  908. bus_scroll->add_child(bus_hb);
  909. save_timer = memnew(Timer);
  910. save_timer->set_wait_time(0.8);
  911. save_timer->set_one_shot(true);
  912. add_child(save_timer);
  913. save_timer->connect("timeout", this, "_server_save");
  914. set_v_size_flags(SIZE_EXPAND_FILL);
  915. edited_path = "res://default_bus_layout.tres";
  916. file_dialog = memnew(EditorFileDialog);
  917. List<String> ext;
  918. ResourceLoader::get_recognized_extensions_for_type("AudioBusLayout", &ext);
  919. for (List<String>::Element *E = ext.front(); E; E = E->next()) {
  920. file_dialog->add_filter("*." + E->get() + "; Audio Bus Layout");
  921. }
  922. add_child(file_dialog);
  923. file_dialog->connect("file_selected", this, "_file_dialog_callback");
  924. set_process(true);
  925. }
  926. void EditorAudioBuses::open_layout(const String &p_path) {
  927. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  928. Ref<AudioBusLayout> state = ResourceLoader::load(p_path);
  929. if (state.is_null()) {
  930. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not an audio bus layout."));
  931. return;
  932. }
  933. edited_path = p_path;
  934. file->set_text(p_path.get_file());
  935. AudioServer::get_singleton()->set_bus_layout(state);
  936. _update_buses();
  937. EditorNode::get_singleton()->get_undo_redo()->clear_history();
  938. call_deferred("_select_layout");
  939. }
  940. void AudioBusesEditorPlugin::edit(Object *p_node) {
  941. if (Object::cast_to<AudioBusLayout>(p_node)) {
  942. String path = Object::cast_to<AudioBusLayout>(p_node)->get_path();
  943. if (path.is_resource_file()) {
  944. audio_bus_editor->open_layout(path);
  945. }
  946. }
  947. }
  948. bool AudioBusesEditorPlugin::handles(Object *p_node) const {
  949. return (Object::cast_to<AudioBusLayout>(p_node) != NULL);
  950. }
  951. void AudioBusesEditorPlugin::make_visible(bool p_visible) {
  952. }
  953. AudioBusesEditorPlugin::AudioBusesEditorPlugin(EditorAudioBuses *p_node) {
  954. audio_bus_editor = p_node;
  955. }
  956. AudioBusesEditorPlugin::~AudioBusesEditorPlugin() {
  957. }