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