audio_stream_import_settings.cpp 24 KB

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