audio_stream_import_settings.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. int width = rect_size.width;
  73. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  74. float preview_offset = zoom_bar->get_value();
  75. float preview_len = zoom_bar->get_page();
  76. Ref<Font> beat_font = get_theme_font(SNAME("main"), SNAME("EditorFonts"));
  77. int main_size = get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts"));
  78. Vector<Vector2> points;
  79. points.resize(width * 2);
  80. Color color_active = get_theme_color(SNAME("contrast_color_2"), SNAME("Editor"));
  81. Color color_inactive = color_active;
  82. color_inactive.a *= 0.5;
  83. Vector<Color> colors;
  84. colors.resize(width);
  85. float inactive_from = 1e20;
  86. float beat_size = 0;
  87. int last_beat = 0;
  88. if (stream->get_bpm() > 0) {
  89. beat_size = 60 / float(stream->get_bpm());
  90. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  91. rect.position.y += y_ofs;
  92. rect.size.y -= y_ofs;
  93. if (stream->get_beat_count() > 0) {
  94. last_beat = stream->get_beat_count();
  95. inactive_from = last_beat * beat_size;
  96. }
  97. }
  98. for (int i = 0; i < width; i++) {
  99. float ofs = preview_offset + i * preview_len / rect_size.width;
  100. float ofs_n = preview_offset + (i + 1) * preview_len / rect_size.width;
  101. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  102. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  103. int idx = i;
  104. points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  105. points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  106. colors.write[idx] = ofs > inactive_from ? color_inactive : color_active;
  107. }
  108. if (!points.is_empty()) {
  109. RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), points, colors);
  110. }
  111. if (beat_size) {
  112. Color beat_color = Color(1, 1, 1, 1);
  113. Color final_beat_color = beat_color;
  114. Color bar_color = beat_color;
  115. beat_color.a *= 0.4;
  116. bar_color.a *= 0.6;
  117. int prev_beat = 0; // Do not draw beat zero
  118. Color color_bg = color_active;
  119. color_bg.a *= 0.2;
  120. _preview->draw_rect(Rect2(0, 0, rect.size.width, rect.position.y), color_bg);
  121. int bar_beats = stream->get_bar_beats();
  122. int last_text_end_x = 0;
  123. for (int i = 0; i < width; i++) {
  124. float ofs = preview_offset + i * preview_len / rect_size.width;
  125. int beat = int(ofs / beat_size);
  126. if (beat != prev_beat) {
  127. String text = itos(beat);
  128. int text_w = beat_font->get_string_size(text).width;
  129. if (i - text_w / 2 > last_text_end_x + 2 * EDSCALE) {
  130. int x_ofs = i - text_w / 2;
  131. _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);
  132. last_text_end_x = i + text_w / 2;
  133. }
  134. if (beat == last_beat) {
  135. _preview->draw_rect(Rect2i(i, rect.position.y, 2, rect.size.height), final_beat_color);
  136. // Darken subsequent beats
  137. beat_color.a *= 0.3;
  138. color_active.a *= 0.3;
  139. } else {
  140. _preview->draw_rect(Rect2i(i, rect.position.y, 1, rect.size.height), (beat % bar_beats) == 0 ? bar_color : beat_color);
  141. }
  142. prev_beat = beat;
  143. }
  144. }
  145. }
  146. }
  147. void AudioStreamImportSettings::_preview_changed(ObjectID p_which) {
  148. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  149. _preview->queue_redraw();
  150. }
  151. }
  152. void AudioStreamImportSettings::_preview_zoom_in() {
  153. if (!stream.is_valid()) {
  154. return;
  155. }
  156. float page_size = zoom_bar->get_page();
  157. zoom_bar->set_page(page_size * 0.5);
  158. zoom_bar->set_value(zoom_bar->get_value() + page_size * 0.25);
  159. _preview->queue_redraw();
  160. _indicator->queue_redraw();
  161. }
  162. void AudioStreamImportSettings::_preview_zoom_out() {
  163. if (!stream.is_valid()) {
  164. return;
  165. }
  166. float page_size = zoom_bar->get_page();
  167. zoom_bar->set_page(MIN(zoom_bar->get_max(), page_size * 2.0));
  168. zoom_bar->set_value(zoom_bar->get_value() - page_size * 0.5);
  169. _preview->queue_redraw();
  170. _indicator->queue_redraw();
  171. }
  172. void AudioStreamImportSettings::_preview_zoom_reset() {
  173. if (!stream.is_valid()) {
  174. return;
  175. }
  176. zoom_bar->set_max(stream->get_length());
  177. zoom_bar->set_page(zoom_bar->get_max());
  178. zoom_bar->set_value(0);
  179. _preview->queue_redraw();
  180. _indicator->queue_redraw();
  181. }
  182. void AudioStreamImportSettings::_preview_zoom_offset_changed(double) {
  183. _preview->queue_redraw();
  184. _indicator->queue_redraw();
  185. }
  186. void AudioStreamImportSettings::_audio_changed() {
  187. if (!is_visible()) {
  188. return;
  189. }
  190. _preview->queue_redraw();
  191. _indicator->queue_redraw();
  192. color_rect->queue_redraw();
  193. }
  194. void AudioStreamImportSettings::_play() {
  195. if (_player->is_playing()) {
  196. // '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'.
  197. _pausing = true;
  198. _player->stop();
  199. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  200. set_process(false);
  201. } else {
  202. _player->play(_current);
  203. _play_button->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons")));
  204. set_process(true);
  205. }
  206. }
  207. void AudioStreamImportSettings::_stop() {
  208. _player->stop();
  209. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  210. _current = 0;
  211. _indicator->queue_redraw();
  212. set_process(false);
  213. }
  214. void AudioStreamImportSettings::_on_finished() {
  215. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  216. if (!_pausing) {
  217. _current = 0;
  218. _indicator->queue_redraw();
  219. } else {
  220. _pausing = false;
  221. }
  222. set_process(false);
  223. }
  224. void AudioStreamImportSettings::_draw_indicator() {
  225. if (!stream.is_valid()) {
  226. return;
  227. }
  228. Rect2 rect = _preview->get_rect();
  229. Ref<Font> beat_font = get_theme_font(SNAME("main"), SNAME("EditorFonts"));
  230. int main_size = get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts"));
  231. if (stream->get_bpm() > 0) {
  232. int y_ofs = beat_font->get_height(main_size) + 4 * EDSCALE;
  233. rect.position.y += y_ofs;
  234. rect.size.height -= y_ofs;
  235. }
  236. float ofs_x = (_current - zoom_bar->get_value()) * rect.size.width / zoom_bar->get_page();
  237. if (ofs_x < 0 || ofs_x >= rect.size.width) {
  238. return;
  239. }
  240. const Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  241. _indicator->draw_line(Point2(ofs_x, rect.position.y), Point2(ofs_x, rect.position.y + rect.size.height), color, Math::round(2 * EDSCALE));
  242. _indicator->draw_texture(
  243. get_theme_icon(SNAME("TimelineIndicator"), SNAME("EditorIcons")),
  244. Point2(ofs_x - get_theme_icon(SNAME("TimelineIndicator"), SNAME("EditorIcons"))->get_width() * 0.5, rect.position.y),
  245. color);
  246. if (stream->get_bpm() > 0 && _hovering_beat != -1) {
  247. // Draw hovered beat.
  248. float preview_offset = zoom_bar->get_value();
  249. float preview_len = zoom_bar->get_page();
  250. float beat_size = 60 / float(stream->get_bpm());
  251. int prev_beat = 0;
  252. for (int i = 0; i < rect.size.width; i++) {
  253. float ofs = preview_offset + i * preview_len / rect.size.width;
  254. int beat = int(ofs / beat_size);
  255. if (beat != prev_beat) {
  256. String text = itos(beat);
  257. int text_w = beat_font->get_string_size(text).width;
  258. if (i - text_w / 2 > 2 * EDSCALE && beat == _hovering_beat) {
  259. int x_ofs = i - text_w / 2;
  260. _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);
  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 (loop->is_pressed()) {
  411. loop_offset->set_editable(true);
  412. } else {
  413. loop_offset->set_editable(false);
  414. }
  415. if (bpm_enabled->is_pressed()) {
  416. stream->call("set_bpm", bpm_edit->get_value());
  417. beats_enabled->set_disabled(false);
  418. beats_edit->set_editable(true);
  419. bar_beats_edit->set_editable(true);
  420. double bpm = bpm_edit->get_value();
  421. if (bpm > 0) {
  422. float beat_size = 60 / float(bpm);
  423. int beat_max = int((stream->get_length() + 0.001) / beat_size);
  424. int current_beat = beats_edit->get_value();
  425. beats_edit->set_max(beat_max);
  426. if (current_beat > beat_max) {
  427. beats_edit->set_value(beat_max);
  428. stream->call("set_beat_count", beat_max);
  429. }
  430. }
  431. stream->call("set_bar_beats", bar_beats_edit->get_value());
  432. } else {
  433. stream->call("set_bpm", 0);
  434. stream->call("set_bar_beats", 4);
  435. beats_enabled->set_disabled(true);
  436. beats_edit->set_editable(false);
  437. bar_beats_edit->set_editable(false);
  438. }
  439. if (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) {
  440. stream->call("set_beat_count", beats_edit->get_value());
  441. } else {
  442. stream->call("set_beat_count", 0);
  443. }
  444. updating_settings = false;
  445. _preview->queue_redraw();
  446. _indicator->queue_redraw();
  447. color_rect->queue_redraw();
  448. }
  449. void AudioStreamImportSettings::_reimport() {
  450. params["loop"] = loop->is_pressed();
  451. params["loop_offset"] = loop_offset->get_value();
  452. params["bpm"] = bpm_enabled->is_pressed() ? double(bpm_edit->get_value()) : double(0);
  453. params["beat_count"] = (bpm_enabled->is_pressed() && beats_enabled->is_pressed()) ? int(beats_edit->get_value()) : int(0);
  454. params["bar_beats"] = (bpm_enabled->is_pressed()) ? int(bar_beats_edit->get_value()) : int(4);
  455. EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(path, importer, params);
  456. }
  457. AudioStreamImportSettings::AudioStreamImportSettings() {
  458. get_ok_button()->set_text(TTR("Reimport"));
  459. get_cancel_button()->set_text(TTR("Close"));
  460. VBoxContainer *main_vbox = memnew(VBoxContainer);
  461. add_child(main_vbox);
  462. HBoxContainer *loop_hb = memnew(HBoxContainer);
  463. loop_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  464. loop = memnew(CheckBox);
  465. loop->set_text(TTR("Enable"));
  466. loop->set_tooltip_text(TTR("Enable looping."));
  467. loop->connect("toggled", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  468. loop_hb->add_child(loop);
  469. loop_hb->add_spacer();
  470. loop_hb->add_child(memnew(Label(TTR("Offset:"))));
  471. loop_offset = memnew(SpinBox);
  472. loop_offset->set_max(10000);
  473. loop_offset->set_step(0.001);
  474. loop_offset->set_suffix("sec");
  475. loop_offset->set_tooltip_text(TTR("Loop offset (from beginning). Note that if BPM is set, this setting will be ignored."));
  476. loop_offset->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  477. loop_hb->add_child(loop_offset);
  478. main_vbox->add_margin_child(TTR("Loop:"), loop_hb);
  479. HBoxContainer *interactive_hb = memnew(HBoxContainer);
  480. interactive_hb->add_theme_constant_override("separation", 4 * EDSCALE);
  481. bpm_enabled = memnew(CheckBox);
  482. bpm_enabled->set_text((TTR("BPM:")));
  483. bpm_enabled->connect("toggled", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  484. interactive_hb->add_child(bpm_enabled);
  485. bpm_edit = memnew(SpinBox);
  486. bpm_edit->set_max(400);
  487. bpm_edit->set_step(0.01);
  488. 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."));
  489. bpm_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  490. interactive_hb->add_child(bpm_edit);
  491. interactive_hb->add_spacer();
  492. beats_enabled = memnew(CheckBox);
  493. beats_enabled->set_text(TTR("Beat Count:"));
  494. beats_enabled->connect("toggled", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  495. interactive_hb->add_child(beats_enabled);
  496. beats_edit = memnew(SpinBox);
  497. 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."));
  498. beats_edit->set_max(99999);
  499. beats_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  500. interactive_hb->add_child(beats_edit);
  501. bar_beats_label = memnew(Label(TTR("Bar Beats:")));
  502. interactive_hb->add_child(bar_beats_label);
  503. bar_beats_edit = memnew(SpinBox);
  504. bar_beats_edit->set_tooltip_text(TTR("Configure the Beats Per Bar. This used for music-aware transitions between AudioStreams."));
  505. bar_beats_edit->set_min(2);
  506. bar_beats_edit->set_max(32);
  507. bar_beats_edit->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_settings_changed).unbind(1));
  508. interactive_hb->add_child(bar_beats_edit);
  509. interactive_hb->add_spacer();
  510. main_vbox->add_margin_child(TTR("Music Playback:"), interactive_hb);
  511. color_rect = memnew(ColorRect);
  512. main_vbox->add_margin_child(TTR("Preview:"), color_rect);
  513. color_rect->set_custom_minimum_size(Size2(600, 200) * EDSCALE);
  514. color_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  515. _player = memnew(AudioStreamPlayer);
  516. _player->connect("finished", callable_mp(this, &AudioStreamImportSettings::_on_finished));
  517. color_rect->add_child(_player);
  518. VBoxContainer *vbox = memnew(VBoxContainer);
  519. vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  520. color_rect->add_child(vbox);
  521. vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  522. _preview = memnew(ColorRect);
  523. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  524. _preview->connect("draw", callable_mp(this, &AudioStreamImportSettings::_draw_preview));
  525. _preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  526. vbox->add_child(_preview);
  527. HBoxContainer *zoom_hbox = memnew(HBoxContainer);
  528. zoom_bar = memnew(HScrollBar);
  529. zoom_in = memnew(Button);
  530. zoom_in->set_flat(true);
  531. zoom_reset = memnew(Button);
  532. zoom_reset->set_flat(true);
  533. zoom_out = memnew(Button);
  534. zoom_out->set_flat(true);
  535. zoom_hbox->add_child(zoom_bar);
  536. zoom_bar->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  537. zoom_bar->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  538. zoom_hbox->add_child(zoom_out);
  539. zoom_hbox->add_child(zoom_reset);
  540. zoom_hbox->add_child(zoom_in);
  541. zoom_in->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_in));
  542. zoom_reset->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_reset));
  543. zoom_out->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_out));
  544. zoom_bar->connect("value_changed", callable_mp(this, &AudioStreamImportSettings::_preview_zoom_offset_changed));
  545. vbox->add_child(zoom_hbox);
  546. _indicator = memnew(Control);
  547. _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  548. _indicator->connect("draw", callable_mp(this, &AudioStreamImportSettings::_draw_indicator));
  549. _indicator->connect("gui_input", callable_mp(this, &AudioStreamImportSettings::_on_input_indicator));
  550. _indicator->connect("mouse_exited", callable_mp(this, &AudioStreamImportSettings::_on_indicator_mouse_exited));
  551. _preview->add_child(_indicator);
  552. HBoxContainer *hbox = memnew(HBoxContainer);
  553. hbox->add_theme_constant_override("separation", 0);
  554. vbox->add_child(hbox);
  555. _play_button = memnew(Button);
  556. _play_button->set_flat(true);
  557. hbox->add_child(_play_button);
  558. _play_button->set_focus_mode(Control::FOCUS_NONE);
  559. _play_button->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_play));
  560. _stop_button = memnew(Button);
  561. _stop_button->set_flat(true);
  562. hbox->add_child(_stop_button);
  563. _stop_button->set_focus_mode(Control::FOCUS_NONE);
  564. _stop_button->connect("pressed", callable_mp(this, &AudioStreamImportSettings::_stop));
  565. _current_label = memnew(Label);
  566. _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  567. _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  568. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  569. hbox->add_child(_current_label);
  570. _duration_label = memnew(Label);
  571. hbox->add_child(_duration_label);
  572. singleton = this;
  573. }