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