audio_stream_import_settings.cpp 24 KB

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