audio_stream_import_settings.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /**************************************************************************/
  2. /* audio_stream_import_settings.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 "audio_stream_import_settings.h"
  31. #include "editor/audio_stream_preview.h"
  32. #include "editor/editor_file_system.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/check_box.h"
  36. AudioStreamImportSettingsDialog *AudioStreamImportSettingsDialog::singleton = nullptr;
  37. void AudioStreamImportSettingsDialog::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_READY: {
  40. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AudioStreamImportSettingsDialog::_preview_changed));
  41. connect("confirmed", callable_mp(this, &AudioStreamImportSettingsDialog::_reimport));
  42. } break;
  43. case NOTIFICATION_THEME_CHANGED: {
  44. _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
  45. _stop_button->set_icon(get_editor_theme_icon(SNAME("Stop")));
  46. _preview->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));
  47. color_rect->set_color(get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor)));
  48. _current_label->begin_bulk_theme_override();
  49. _current_label->add_theme_font_override("font", get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
  50. _current_label->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
  51. _current_label->end_bulk_theme_override();
  52. _duration_label->begin_bulk_theme_override();
  53. _duration_label->add_theme_font_override("font", get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts)));
  54. _duration_label->add_theme_font_size_override("font_size", get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts)));
  55. _duration_label->end_bulk_theme_override();
  56. zoom_in->set_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  57. zoom_out->set_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  58. zoom_reset->set_icon(get_editor_theme_icon(SNAME("ZoomReset")));
  59. _indicator->queue_redraw();
  60. _preview->queue_redraw();
  61. } break;
  62. case NOTIFICATION_PROCESS: {
  63. _current = _player->get_playback_position();
  64. _indicator->queue_redraw();
  65. } break;
  66. case NOTIFICATION_VISIBILITY_CHANGED: {
  67. if (!is_visible()) {
  68. _stop();
  69. }
  70. } break;
  71. }
  72. }
  73. void AudioStreamImportSettingsDialog::_draw_preview() {
  74. Rect2 rect = _preview->get_rect();
  75. Size2 rect_size = rect.size;
  76. int width = rect_size.width;
  77. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  78. float preview_offset = zoom_bar->get_value();
  79. float preview_len = zoom_bar->get_page();
  80. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  81. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  82. Vector<Vector2> points;
  83. points.resize(width * 2);
  84. Color color_active = get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor));
  85. Color color_inactive = color_active;
  86. color_inactive.a *= 0.5;
  87. Vector<Color> colors;
  88. colors.resize(width);
  89. float inactive_from = 1e20;
  90. float beat_size = 0;
  91. int last_beat = 0;
  92. if (stream->get_bpm() > 0) {
  93. beat_size = 60 / float(stream->get_bpm());
  94. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  95. rect.position.y += y_ofs;
  96. rect.size.y -= y_ofs;
  97. if (stream->get_beat_count() > 0) {
  98. last_beat = stream->get_beat_count();
  99. inactive_from = last_beat * beat_size;
  100. }
  101. }
  102. for (int i = 0; i < width; i++) {
  103. float ofs = preview_offset + i * preview_len / rect_size.width;
  104. float ofs_n = preview_offset + (i + 1) * preview_len / rect_size.width;
  105. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  106. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  107. int idx = i;
  108. points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  109. points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  110. colors.write[idx] = ofs > inactive_from ? color_inactive : color_active;
  111. }
  112. if (!points.is_empty()) {
  113. RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), points, colors);
  114. }
  115. if (beat_size) {
  116. Color beat_color = Color(1, 1, 1, 1);
  117. Color final_beat_color = beat_color;
  118. Color bar_color = beat_color;
  119. beat_color.a *= 0.4;
  120. bar_color.a *= 0.6;
  121. int prev_beat = 0; // Do not draw beat zero
  122. Color color_bg = color_active;
  123. color_bg.a *= 0.2;
  124. _preview->draw_rect(Rect2(0, 0, rect.size.width, rect.position.y), color_bg);
  125. int bar_beats = stream->get_bar_beats();
  126. int last_text_end_x = 0;
  127. for (int i = 0; i < width; i++) {
  128. float ofs = preview_offset + i * preview_len / rect_size.width;
  129. int beat = int(ofs / beat_size);
  130. if (beat != prev_beat) {
  131. String text = itos(beat);
  132. int text_w = beat_font->get_string_size(text).width;
  133. if (i - text_w / 2 > last_text_end_x + 2 * EDSCALE) {
  134. int x_ofs = i - text_w / 2;
  135. _preview->draw_string(beat_font, Point2(x_ofs, 2 * EDSCALE + beat_font->get_ascent(main_size)), text, HORIZONTAL_ALIGNMENT_LEFT, rect.size.width - x_ofs, Font::DEFAULT_FONT_SIZE, color_active);
  136. last_text_end_x = i + text_w / 2;
  137. }
  138. if (beat == last_beat) {
  139. _preview->draw_rect(Rect2i(i, rect.position.y, 2, rect.size.height), final_beat_color);
  140. // Darken subsequent beats
  141. beat_color.a *= 0.3;
  142. color_active.a *= 0.3;
  143. } else {
  144. _preview->draw_rect(Rect2i(i, rect.position.y, 1, rect.size.height), (beat % bar_beats) == 0 ? bar_color : beat_color);
  145. }
  146. prev_beat = beat;
  147. }
  148. }
  149. }
  150. }
  151. void AudioStreamImportSettingsDialog::_preview_changed(ObjectID p_which) {
  152. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  153. _preview->queue_redraw();
  154. }
  155. }
  156. void AudioStreamImportSettingsDialog::_preview_zoom_in() {
  157. if (!stream.is_valid()) {
  158. return;
  159. }
  160. float page_size = zoom_bar->get_page();
  161. zoom_bar->set_page(page_size * 0.5);
  162. zoom_bar->set_value(zoom_bar->get_value() + page_size * 0.25);
  163. _preview->queue_redraw();
  164. _indicator->queue_redraw();
  165. }
  166. void AudioStreamImportSettingsDialog::_preview_zoom_out() {
  167. if (!stream.is_valid()) {
  168. return;
  169. }
  170. float page_size = zoom_bar->get_page();
  171. zoom_bar->set_page(MIN(zoom_bar->get_max(), page_size * 2.0));
  172. zoom_bar->set_value(zoom_bar->get_value() - page_size * 0.5);
  173. _preview->queue_redraw();
  174. _indicator->queue_redraw();
  175. }
  176. void AudioStreamImportSettingsDialog::_preview_zoom_reset() {
  177. if (!stream.is_valid()) {
  178. return;
  179. }
  180. zoom_bar->set_max(stream->get_length());
  181. zoom_bar->set_page(zoom_bar->get_max());
  182. zoom_bar->set_value(0);
  183. _preview->queue_redraw();
  184. _indicator->queue_redraw();
  185. }
  186. void AudioStreamImportSettingsDialog::_preview_zoom_offset_changed(double) {
  187. _preview->queue_redraw();
  188. _indicator->queue_redraw();
  189. }
  190. void AudioStreamImportSettingsDialog::_audio_changed() {
  191. if (!is_visible()) {
  192. return;
  193. }
  194. _preview->queue_redraw();
  195. _indicator->queue_redraw();
  196. color_rect->queue_redraw();
  197. }
  198. void AudioStreamImportSettingsDialog::_play() {
  199. if (_player->is_playing()) {
  200. // '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'.
  201. _pausing = true;
  202. _player->stop();
  203. _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
  204. set_process(false);
  205. } else {
  206. _player->play(_current);
  207. _play_button->set_icon(get_editor_theme_icon(SNAME("Pause")));
  208. set_process(true);
  209. }
  210. }
  211. void AudioStreamImportSettingsDialog::_stop() {
  212. _player->stop();
  213. _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
  214. _current = 0;
  215. _indicator->queue_redraw();
  216. set_process(false);
  217. }
  218. void AudioStreamImportSettingsDialog::_on_finished() {
  219. _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
  220. if (!_pausing) {
  221. _current = 0;
  222. _indicator->queue_redraw();
  223. } else {
  224. _pausing = false;
  225. }
  226. set_process(false);
  227. }
  228. void AudioStreamImportSettingsDialog::_draw_indicator() {
  229. if (!stream.is_valid()) {
  230. return;
  231. }
  232. Rect2 rect = _preview->get_rect();
  233. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  234. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  235. if (stream->get_bpm() > 0) {
  236. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  237. rect.position.y += y_ofs;
  238. rect.size.height -= y_ofs;
  239. }
  240. _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
  241. float ofs_x = (_current - zoom_bar->get_value()) * rect.size.width / zoom_bar->get_page();
  242. if (ofs_x < 0 || ofs_x >= rect.size.width) {
  243. return;
  244. }
  245. const Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  246. _indicator->draw_line(Point2(ofs_x, rect.position.y), Point2(ofs_x, rect.position.y + rect.size.height), color, Math::round(2 * EDSCALE));
  247. _indicator->draw_texture(
  248. get_editor_theme_icon(SNAME("TimelineIndicator")),
  249. Point2(ofs_x - get_editor_theme_icon(SNAME("TimelineIndicator"))->get_width() * 0.5, rect.position.y),
  250. color);
  251. if (stream->get_bpm() > 0 && _hovering_beat != -1) {
  252. // Draw hovered beat.
  253. float preview_offset = zoom_bar->get_value();
  254. float preview_len = zoom_bar->get_page();
  255. float beat_size = 60 / float(stream->get_bpm());
  256. int prev_beat = 0;
  257. for (int i = 0; i < rect.size.width; i++) {
  258. float ofs = preview_offset + i * preview_len / rect.size.width;
  259. int beat = int(ofs / beat_size);
  260. if (beat != prev_beat) {
  261. String text = itos(beat);
  262. int text_w = beat_font->get_string_size(text).width;
  263. if (i - text_w / 2 > 2 * EDSCALE && beat == _hovering_beat) {
  264. int x_ofs = i - text_w / 2;
  265. _indicator->draw_string(beat_font, Point2(x_ofs, 2 * EDSCALE + beat_font->get_ascent(main_size)), text, HORIZONTAL_ALIGNMENT_LEFT, rect.size.width - x_ofs, Font::DEFAULT_FONT_SIZE, color);
  266. break;
  267. }
  268. prev_beat = beat;
  269. }
  270. }
  271. }
  272. }
  273. void AudioStreamImportSettingsDialog::_on_indicator_mouse_exited() {
  274. _hovering_beat = -1;
  275. _indicator->queue_redraw();
  276. }
  277. void AudioStreamImportSettingsDialog::_on_input_indicator(Ref<InputEvent> p_event) {
  278. const Ref<InputEventMouseButton> mb = p_event;
  279. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  280. if (stream->get_bpm() > 0) {
  281. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  282. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  283. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  284. if ((!_dragging && mb->get_position().y < y_ofs) || _beat_len_dragging) {
  285. if (mb->is_pressed()) {
  286. _set_beat_len_to(mb->get_position().x);
  287. _beat_len_dragging = true;
  288. } else {
  289. _beat_len_dragging = false;
  290. }
  291. return;
  292. }
  293. }
  294. if (mb->is_pressed()) {
  295. _seek_to(mb->get_position().x);
  296. }
  297. _dragging = mb->is_pressed();
  298. }
  299. const Ref<InputEventMouseMotion> mm = p_event;
  300. if (mm.is_valid()) {
  301. if (_dragging) {
  302. _seek_to(mm->get_position().x);
  303. }
  304. if (_beat_len_dragging) {
  305. _set_beat_len_to(mm->get_position().x);
  306. }
  307. if (stream->get_bpm() > 0) {
  308. int main_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  309. Ref<Font> beat_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  310. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  311. if (mm->get_position().y < y_ofs) {
  312. int new_hovering_beat = _get_beat_at_pos(mm->get_position().x);
  313. if (new_hovering_beat != _hovering_beat) {
  314. _hovering_beat = new_hovering_beat;
  315. _indicator->queue_redraw();
  316. }
  317. } else if (_hovering_beat != -1) {
  318. _hovering_beat = -1;
  319. _indicator->queue_redraw();
  320. }
  321. }
  322. }
  323. }
  324. int AudioStreamImportSettingsDialog::_get_beat_at_pos(real_t p_x) {
  325. float ofs_sec = zoom_bar->get_value() + p_x * zoom_bar->get_page() / _preview->get_size().width;
  326. ofs_sec = CLAMP(ofs_sec, 0, stream->get_length());
  327. float beat_size = 60 / float(stream->get_bpm());
  328. int beat = int(ofs_sec / beat_size + 0.5);
  329. if (beat * beat_size > stream->get_length() + 0.001) { // Stream may end few audio frames before but may still want to use full loop.
  330. beat--;
  331. }
  332. return beat;
  333. }
  334. void AudioStreamImportSettingsDialog::_set_beat_len_to(real_t p_x) {
  335. int beat = _get_beat_at_pos(p_x);
  336. if (beat < 1) {
  337. beat = 1; // Because 0 is disable.
  338. }
  339. updating_settings = true;
  340. beats_enabled->set_pressed(true);
  341. beats_edit->set_value(beat);
  342. updating_settings = false;
  343. _settings_changed();
  344. }
  345. void AudioStreamImportSettingsDialog::_seek_to(real_t p_x) {
  346. _current = zoom_bar->get_value() + p_x / _preview->get_rect().size.x * zoom_bar->get_page();
  347. _current = CLAMP(_current, 0, stream->get_length());
  348. _player->seek(_current);
  349. _indicator->queue_redraw();
  350. }
  351. void AudioStreamImportSettingsDialog::edit(const String &p_path, const String &p_importer, const Ref<AudioStream> &p_stream) {
  352. if (!stream.is_null()) {
  353. stream->disconnect_changed(callable_mp(this, &AudioStreamImportSettingsDialog::_audio_changed));
  354. }
  355. importer = p_importer;
  356. path = p_path;
  357. stream = p_stream;
  358. _player->set_stream(stream);
  359. _current = 0;
  360. String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
  361. _duration_label->set_text(text);
  362. if (!stream.is_null()) {
  363. stream->connect_changed(callable_mp(this, &AudioStreamImportSettingsDialog::_audio_changed));
  364. _preview->queue_redraw();
  365. _indicator->queue_redraw();
  366. color_rect->queue_redraw();
  367. } else {
  368. hide();
  369. }
  370. params.clear();
  371. if (stream.is_valid()) {
  372. Ref<ConfigFile> config_file;
  373. config_file.instantiate();
  374. Error err = config_file->load(p_path + ".import");
  375. updating_settings = true;
  376. if (err == OK) {
  377. double bpm = config_file->get_value("params", "bpm", 0);
  378. int beats = config_file->get_value("params", "beat_count", 0);
  379. bpm_edit->set_value(bpm > 0 ? bpm : 120);
  380. bpm_enabled->set_pressed(bpm > 0);
  381. beats_edit->set_value(beats);
  382. beats_enabled->set_pressed(beats > 0);
  383. loop->set_pressed(config_file->get_value("params", "loop", false));
  384. loop_offset->set_value(config_file->get_value("params", "loop_offset", 0));
  385. bar_beats_edit->set_value(config_file->get_value("params", "bar_beats", 4));
  386. List<String> keys;
  387. config_file->get_section_keys("params", &keys);
  388. for (const String &K : keys) {
  389. params[K] = config_file->get_value("params", K);
  390. }
  391. } else {
  392. bpm_edit->set_value(false);
  393. bpm_enabled->set_pressed(false);
  394. beats_edit->set_value(0);
  395. beats_enabled->set_pressed(false);
  396. bar_beats_edit->set_value(4);
  397. loop->set_pressed(false);
  398. loop_offset->set_value(0);
  399. }
  400. _preview_zoom_reset();
  401. updating_settings = false;
  402. _settings_changed();
  403. set_title(vformat(TTR("Audio Stream Importer: %s"), p_path.get_file()));
  404. popup_centered();
  405. }
  406. }
  407. void AudioStreamImportSettingsDialog::_settings_changed() {
  408. if (updating_settings) {
  409. return;
  410. }
  411. updating_settings = true;
  412. stream->call("set_loop", loop->is_pressed());
  413. stream->call("set_loop_offset", loop_offset->get_value());
  414. if (loop->is_pressed()) {
  415. loop_offset->set_editable(true);
  416. } else {
  417. loop_offset->set_editable(false);
  418. }
  419. if (bpm_enabled->is_pressed()) {
  420. stream->call("set_bpm", bpm_edit->get_value());
  421. beats_enabled->set_disabled(false);
  422. beats_edit->set_editable(true);
  423. bar_beats_edit->set_editable(true);
  424. double bpm = bpm_edit->get_value();
  425. if (bpm > 0) {
  426. float beat_size = 60 / float(bpm);
  427. int beat_max = int((stream->get_length() + 0.001) / beat_size);
  428. int current_beat = beats_edit->get_value();
  429. beats_edit->set_max(beat_max);
  430. if (current_beat > beat_max) {
  431. beats_edit->set_value(beat_max);
  432. stream->call("set_beat_count", beat_max);
  433. }
  434. }
  435. stream->call("set_bar_beats", bar_beats_edit->get_value());
  436. } else {
  437. stream->call("set_bpm", 0);
  438. stream->call("set_bar_beats", 4);
  439. beats_enabled->set_disabled(true);
  440. beats_edit->set_editable(false);
  441. bar_beats_edit->set_editable(false);
  442. }
  443. if (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) {
  444. stream->call("set_beat_count", beats_edit->get_value());
  445. } else {
  446. stream->call("set_beat_count", 0);
  447. }
  448. updating_settings = false;
  449. _preview->queue_redraw();
  450. _indicator->queue_redraw();
  451. color_rect->queue_redraw();
  452. }
  453. void AudioStreamImportSettingsDialog::_reimport() {
  454. params["loop"] = loop->is_pressed();
  455. params["loop_offset"] = loop_offset->get_value();
  456. params["bpm"] = bpm_enabled->is_pressed() ? double(bpm_edit->get_value()) : double(0);
  457. params["beat_count"] = (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) ? int(beats_edit->get_value()) : int(0);
  458. params["bar_beats"] = (bpm_enabled->is_pressed()) ? int(bar_beats_edit->get_value()) : int(4);
  459. EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(path, importer, params);
  460. }
  461. AudioStreamImportSettingsDialog::AudioStreamImportSettingsDialog() {
  462. get_ok_button()->set_text(TTR("Reimport"));
  463. get_cancel_button()->set_text(TTR("Close"));
  464. VBoxContainer *main_vbox = memnew(VBoxContainer);
  465. add_child(main_vbox);
  466. HBoxContainer *loop_hb = memnew(HBoxContainer);
  467. loop_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  468. loop = memnew(CheckBox);
  469. loop->set_text(TTR("Enable"));
  470. loop->set_tooltip_text(TTR("Enable looping."));
  471. loop->connect("toggled", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  472. loop_hb->add_child(loop);
  473. loop_hb->add_spacer();
  474. loop_hb->add_child(memnew(Label(TTR("Offset:"))));
  475. loop_offset = memnew(SpinBox);
  476. loop_offset->set_max(10000);
  477. loop_offset->set_step(0.001);
  478. loop_offset->set_suffix("sec");
  479. loop_offset->set_tooltip_text(TTR("Loop offset (from beginning). Note that if BPM is set, this setting will be ignored."));
  480. loop_offset->connect("value_changed", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  481. loop_hb->add_child(loop_offset);
  482. main_vbox->add_margin_child(TTR("Loop:"), loop_hb);
  483. HBoxContainer *interactive_hb = memnew(HBoxContainer);
  484. interactive_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  485. bpm_enabled = memnew(CheckBox);
  486. bpm_enabled->set_text((TTR("BPM:")));
  487. bpm_enabled->connect("toggled", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  488. interactive_hb->add_child(bpm_enabled);
  489. bpm_edit = memnew(SpinBox);
  490. bpm_edit->set_max(400);
  491. bpm_edit->set_step(0.01);
  492. bpm_edit->set_tooltip_text(TTR("Configure the Beats Per Measure (tempo) used for the interactive streams.\nThis is required in order to configure beat information."));
  493. bpm_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  494. interactive_hb->add_child(bpm_edit);
  495. interactive_hb->add_spacer();
  496. beats_enabled = memnew(CheckBox);
  497. beats_enabled->set_text(TTR("Beat Count:"));
  498. beats_enabled->connect("toggled", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  499. interactive_hb->add_child(beats_enabled);
  500. beats_edit = memnew(SpinBox);
  501. beats_edit->set_tooltip_text(TTR("Configure the amount of Beats used for music-aware looping. If zero, it will be autodetected from the length.\nIt is recommended to set this value (either manually or by clicking on a beat number in the preview) to ensure looping works properly."));
  502. beats_edit->set_max(99999);
  503. beats_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  504. interactive_hb->add_child(beats_edit);
  505. bar_beats_label = memnew(Label(TTR("Bar Beats:")));
  506. interactive_hb->add_child(bar_beats_label);
  507. bar_beats_edit = memnew(SpinBox);
  508. bar_beats_edit->set_tooltip_text(TTR("Configure the Beats Per Bar. This used for music-aware transitions between AudioStreams."));
  509. bar_beats_edit->set_min(2);
  510. bar_beats_edit->set_max(32);
  511. bar_beats_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettingsDialog::_settings_changed).unbind(1));
  512. interactive_hb->add_child(bar_beats_edit);
  513. interactive_hb->add_spacer();
  514. main_vbox->add_margin_child(TTR("Music Playback:"), interactive_hb);
  515. color_rect = memnew(ColorRect);
  516. main_vbox->add_margin_child(TTR("Preview:"), color_rect);
  517. color_rect->set_custom_minimum_size(Size2(600, 200) * EDSCALE);
  518. color_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  519. _player = memnew(AudioStreamPlayer);
  520. _player->connect(SceneStringName(finished), callable_mp(this, &AudioStreamImportSettingsDialog::_on_finished));
  521. color_rect->add_child(_player);
  522. VBoxContainer *vbox = memnew(VBoxContainer);
  523. vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  524. color_rect->add_child(vbox);
  525. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  526. _preview = memnew(ColorRect);
  527. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  528. _preview->connect(SceneStringName(draw), callable_mp(this, &AudioStreamImportSettingsDialog::_draw_preview));
  529. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  530. vbox->add_child(_preview);
  531. HBoxContainer *zoom_hbox = memnew(HBoxContainer);
  532. zoom_bar = memnew(HScrollBar);
  533. zoom_in = memnew(Button);
  534. zoom_in->set_flat(true);
  535. zoom_reset = memnew(Button);
  536. zoom_reset->set_flat(true);
  537. zoom_out = memnew(Button);
  538. zoom_out->set_flat(true);
  539. zoom_hbox->add_child(zoom_bar);
  540. zoom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  541. zoom_bar->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  542. zoom_hbox->add_child(zoom_out);
  543. zoom_hbox->add_child(zoom_reset);
  544. zoom_hbox->add_child(zoom_in);
  545. zoom_in->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_in));
  546. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_reset));
  547. zoom_out->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_out));
  548. zoom_bar->connect("value_changed", callable_mp(this, &AudioStreamImportSettingsDialog::_preview_zoom_offset_changed));
  549. vbox->add_child(zoom_hbox);
  550. _indicator = memnew(Control);
  551. _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  552. _indicator->connect(SceneStringName(draw), callable_mp(this, &AudioStreamImportSettingsDialog::_draw_indicator));
  553. _indicator->connect(SceneStringName(gui_input), callable_mp(this, &AudioStreamImportSettingsDialog::_on_input_indicator));
  554. _indicator->connect(SceneStringName(mouse_exited), callable_mp(this, &AudioStreamImportSettingsDialog::_on_indicator_mouse_exited));
  555. _preview->add_child(_indicator);
  556. HBoxContainer *hbox = memnew(HBoxContainer);
  557. hbox->add_theme_constant_override("separation", 0);
  558. vbox->add_child(hbox);
  559. _play_button = memnew(Button);
  560. _play_button->set_flat(true);
  561. hbox->add_child(_play_button);
  562. _play_button->set_focus_mode(Control::FOCUS_NONE);
  563. _play_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_play));
  564. _stop_button = memnew(Button);
  565. _stop_button->set_flat(true);
  566. hbox->add_child(_stop_button);
  567. _stop_button->set_focus_mode(Control::FOCUS_NONE);
  568. _stop_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamImportSettingsDialog::_stop));
  569. _current_label = memnew(Label);
  570. _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  571. _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  572. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  573. hbox->add_child(_current_label);
  574. _duration_label = memnew(Label);
  575. hbox->add_child(_duration_label);
  576. singleton = this;
  577. }