2022-07-20 23:00:58 +00:00
/**************************************************************************/
/* audio_stream_import_settings.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
# include "audio_stream_import_settings.h"
# include "editor/audio_stream_preview.h"
# include "editor/editor_file_system.h"
2023-08-13 00:33:39 +00:00
# include "editor/editor_string_names.h"
2024-01-15 12:14:55 +00:00
# include "editor/themes/editor_scale.h"
2022-11-11 19:12:48 +00:00
# include "scene/gui/check_box.h"
2022-07-20 23:00:58 +00:00
2023-10-06 07:00:36 +00:00
AudioStreamImportSettingsDialog * AudioStreamImportSettingsDialog : : singleton = nullptr ;
2022-07-20 23:00:58 +00:00
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _notification ( int p_what ) {
2022-07-20 23:00:58 +00:00
switch ( p_what ) {
case NOTIFICATION_READY : {
2023-10-06 07:00:36 +00:00
AudioStreamPreviewGenerator : : get_singleton ( ) - > connect ( " preview_updated " , callable_mp ( this , & AudioStreamImportSettingsDialog : : _preview_changed ) ) ;
2024-05-14 12:28:18 +00:00
connect ( SceneStringName ( confirmed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _reimport ) ) ;
2022-07-20 23:00:58 +00:00
} break ;
2023-10-19 16:05:19 +00:00
case NOTIFICATION_THEME_CHANGED : {
2023-08-13 00:33:39 +00:00
_play_button - > set_icon ( get_editor_theme_icon ( SNAME ( " MainPlay " ) ) ) ;
_stop_button - > set_icon ( get_editor_theme_icon ( SNAME ( " Stop " ) ) ) ;
2023-10-19 16:05:19 +00:00
2023-08-13 00:33:39 +00:00
_preview - > set_color ( get_theme_color ( SNAME ( " dark_color_2 " ) , EditorStringName ( Editor ) ) ) ;
color_rect - > set_color ( get_theme_color ( SNAME ( " dark_color_1 " ) , EditorStringName ( Editor ) ) ) ;
2023-10-19 16:05:19 +00:00
_current_label - > begin_bulk_theme_override ( ) ;
2024-05-14 13:57:29 +00:00
_current_label - > add_theme_font_override ( SceneStringName ( font ) , get_theme_font ( SNAME ( " status_source " ) , EditorStringName ( EditorFonts ) ) ) ;
_current_label - > add_theme_font_size_override ( SceneStringName ( font_size ) , get_theme_font_size ( SNAME ( " status_source_size " ) , EditorStringName ( EditorFonts ) ) ) ;
2023-10-19 16:05:19 +00:00
_current_label - > end_bulk_theme_override ( ) ;
_duration_label - > begin_bulk_theme_override ( ) ;
2024-05-14 13:57:29 +00:00
_duration_label - > add_theme_font_override ( SceneStringName ( font ) , get_theme_font ( SNAME ( " status_source " ) , EditorStringName ( EditorFonts ) ) ) ;
_duration_label - > add_theme_font_size_override ( SceneStringName ( font_size ) , get_theme_font_size ( SNAME ( " status_source_size " ) , EditorStringName ( EditorFonts ) ) ) ;
2023-10-19 16:05:19 +00:00
_duration_label - > end_bulk_theme_override ( ) ;
2023-08-13 00:33:39 +00:00
zoom_in - > set_icon ( get_editor_theme_icon ( SNAME ( " ZoomMore " ) ) ) ;
zoom_out - > set_icon ( get_editor_theme_icon ( SNAME ( " ZoomLess " ) ) ) ;
zoom_reset - > set_icon ( get_editor_theme_icon ( SNAME ( " ZoomReset " ) ) ) ;
2022-07-20 23:00:58 +00:00
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
_preview - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
} break ;
case NOTIFICATION_PROCESS : {
_current = _player - > get_playback_position ( ) ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
} break ;
case NOTIFICATION_VISIBILITY_CHANGED : {
if ( ! is_visible ( ) ) {
_stop ( ) ;
}
} break ;
}
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _draw_preview ( ) {
2022-07-20 23:00:58 +00:00
Rect2 rect = _preview - > get_rect ( ) ;
2022-09-29 09:53:28 +00:00
Size2 rect_size = rect . size ;
2023-05-30 14:49:58 +00:00
int width = rect_size . width ;
2022-07-20 23:00:58 +00:00
Ref < AudioStreamPreview > preview = AudioStreamPreviewGenerator : : get_singleton ( ) - > generate_preview ( stream ) ;
float preview_offset = zoom_bar - > get_value ( ) ;
float preview_len = zoom_bar - > get_page ( ) ;
2023-08-13 00:33:39 +00:00
Ref < Font > beat_font = get_theme_font ( SNAME ( " main " ) , EditorStringName ( EditorFonts ) ) ;
int main_size = get_theme_font_size ( SNAME ( " main_size " ) , EditorStringName ( EditorFonts ) ) ;
2023-05-12 04:50:58 +00:00
Vector < Vector2 > points ;
2023-05-30 14:49:58 +00:00
points . resize ( width * 2 ) ;
2023-08-13 00:33:39 +00:00
Color color_active = get_theme_color ( SNAME ( " contrast_color_2 " ) , EditorStringName ( Editor ) ) ;
2022-07-20 23:00:58 +00:00
Color color_inactive = color_active ;
color_inactive . a * = 0.5 ;
2023-05-12 04:50:58 +00:00
Vector < Color > colors ;
2023-05-30 14:49:58 +00:00
colors . resize ( width ) ;
2022-07-20 23:00:58 +00:00
float inactive_from = 1e20 ;
float beat_size = 0 ;
int last_beat = 0 ;
if ( stream - > get_bpm ( ) > 0 ) {
beat_size = 60 / float ( stream - > get_bpm ( ) ) ;
int y_ofs = beat_font - > get_height ( main_size ) + 4 * EDSCALE ;
rect . position . y + = y_ofs ;
rect . size . y - = y_ofs ;
if ( stream - > get_beat_count ( ) > 0 ) {
last_beat = stream - > get_beat_count ( ) ;
inactive_from = last_beat * beat_size ;
}
}
2023-05-30 14:49:58 +00:00
for ( int i = 0 ; i < width ; i + + ) {
2022-09-29 09:53:28 +00:00
float ofs = preview_offset + i * preview_len / rect_size . width ;
float ofs_n = preview_offset + ( i + 1 ) * preview_len / rect_size . width ;
2022-07-20 23:00:58 +00:00
float max = preview - > get_max ( ofs , ofs_n ) * 0.5 + 0.5 ;
float min = preview - > get_min ( ofs , ofs_n ) * 0.5 + 0.5 ;
int idx = i ;
2023-05-12 04:50:58 +00:00
points . write [ idx * 2 + 0 ] = Vector2 ( i + 1 , rect . position . y + min * rect . size . y ) ;
points . write [ idx * 2 + 1 ] = Vector2 ( i + 1 , rect . position . y + max * rect . size . y ) ;
2022-07-20 23:00:58 +00:00
2023-05-12 04:50:58 +00:00
colors . write [ idx ] = ofs > inactive_from ? color_inactive : color_active ;
2022-07-20 23:00:58 +00:00
}
2023-05-12 04:50:58 +00:00
if ( ! points . is_empty ( ) ) {
RS : : get_singleton ( ) - > canvas_item_add_multiline ( _preview - > get_canvas_item ( ) , points , colors ) ;
}
2022-07-20 23:00:58 +00:00
if ( beat_size ) {
Color beat_color = Color ( 1 , 1 , 1 , 1 ) ;
Color final_beat_color = beat_color ;
Color bar_color = beat_color ;
beat_color . a * = 0.4 ;
bar_color . a * = 0.6 ;
int prev_beat = 0 ; // Do not draw beat zero
Color color_bg = color_active ;
color_bg . a * = 0.2 ;
_preview - > draw_rect ( Rect2 ( 0 , 0 , rect . size . width , rect . position . y ) , color_bg ) ;
int bar_beats = stream - > get_bar_beats ( ) ;
int last_text_end_x = 0 ;
2023-05-30 14:49:58 +00:00
for ( int i = 0 ; i < width ; i + + ) {
2022-09-29 09:53:28 +00:00
float ofs = preview_offset + i * preview_len / rect_size . width ;
2022-07-20 23:00:58 +00:00
int beat = int ( ofs / beat_size ) ;
if ( beat ! = prev_beat ) {
String text = itos ( beat ) ;
int text_w = beat_font - > get_string_size ( text ) . width ;
if ( i - text_w / 2 > last_text_end_x + 2 * EDSCALE ) {
int x_ofs = i - text_w / 2 ;
_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 ) ;
last_text_end_x = i + text_w / 2 ;
}
if ( beat = = last_beat ) {
_preview - > draw_rect ( Rect2i ( i , rect . position . y , 2 , rect . size . height ) , final_beat_color ) ;
// Darken subsequent beats
beat_color . a * = 0.3 ;
color_active . a * = 0.3 ;
} else {
_preview - > draw_rect ( Rect2i ( i , rect . position . y , 1 , rect . size . height ) , ( beat % bar_beats ) = = 0 ? bar_color : beat_color ) ;
}
prev_beat = beat ;
}
}
}
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _preview_changed ( ObjectID p_which ) {
2022-07-20 23:00:58 +00:00
if ( stream . is_valid ( ) & & stream - > get_instance_id ( ) = = p_which ) {
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _preview_zoom_in ( ) {
2022-07-20 23:00:58 +00:00
if ( ! stream . is_valid ( ) ) {
return ;
}
float page_size = zoom_bar - > get_page ( ) ;
zoom_bar - > set_page ( page_size * 0.5 ) ;
zoom_bar - > set_value ( zoom_bar - > get_value ( ) + page_size * 0.25 ) ;
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _preview_zoom_out ( ) {
2022-07-20 23:00:58 +00:00
if ( ! stream . is_valid ( ) ) {
return ;
}
float page_size = zoom_bar - > get_page ( ) ;
zoom_bar - > set_page ( MIN ( zoom_bar - > get_max ( ) , page_size * 2.0 ) ) ;
zoom_bar - > set_value ( zoom_bar - > get_value ( ) - page_size * 0.5 ) ;
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _preview_zoom_reset ( ) {
2022-07-20 23:00:58 +00:00
if ( ! stream . is_valid ( ) ) {
return ;
}
zoom_bar - > set_max ( stream - > get_length ( ) ) ;
zoom_bar - > set_page ( zoom_bar - > get_max ( ) ) ;
zoom_bar - > set_value ( 0 ) ;
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _preview_zoom_offset_changed ( double ) {
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _audio_changed ( ) {
2022-07-20 23:00:58 +00:00
if ( ! is_visible ( ) ) {
return ;
}
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
color_rect - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _play ( ) {
2022-07-20 23:00:58 +00:00
if ( _player - > is_playing ( ) ) {
// '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'.
_pausing = true ;
_player - > stop ( ) ;
2023-08-13 00:33:39 +00:00
_play_button - > set_icon ( get_editor_theme_icon ( SNAME ( " MainPlay " ) ) ) ;
2022-07-20 23:00:58 +00:00
set_process ( false ) ;
} else {
_player - > play ( _current ) ;
2023-08-13 00:33:39 +00:00
_play_button - > set_icon ( get_editor_theme_icon ( SNAME ( " Pause " ) ) ) ;
2022-07-20 23:00:58 +00:00
set_process ( true ) ;
}
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _stop ( ) {
2022-07-20 23:00:58 +00:00
_player - > stop ( ) ;
2023-08-13 00:33:39 +00:00
_play_button - > set_icon ( get_editor_theme_icon ( SNAME ( " MainPlay " ) ) ) ;
2022-07-20 23:00:58 +00:00
_current = 0 ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
set_process ( false ) ;
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _on_finished ( ) {
2023-08-13 00:33:39 +00:00
_play_button - > set_icon ( get_editor_theme_icon ( SNAME ( " MainPlay " ) ) ) ;
2022-07-20 23:00:58 +00:00
if ( ! _pausing ) {
_current = 0 ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
} else {
_pausing = false ;
}
set_process ( false ) ;
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _draw_indicator ( ) {
2022-07-20 23:00:58 +00:00
if ( ! stream . is_valid ( ) ) {
return ;
}
Rect2 rect = _preview - > get_rect ( ) ;
2023-08-13 00:33:39 +00:00
Ref < Font > beat_font = get_theme_font ( SNAME ( " main " ) , EditorStringName ( EditorFonts ) ) ;
int main_size = get_theme_font_size ( SNAME ( " main_size " ) , EditorStringName ( EditorFonts ) ) ;
2022-07-20 23:00:58 +00:00
if ( stream - > get_bpm ( ) > 0 ) {
int y_ofs = beat_font - > get_height ( main_size ) + 4 * EDSCALE ;
rect . position . y + = y_ofs ;
rect . size . height - = y_ofs ;
}
2024-01-05 11:24:56 +00:00
_current_label - > set_text ( String : : num ( _current , 2 ) . pad_decimals ( 2 ) + " / " ) ;
2022-07-20 23:00:58 +00:00
float ofs_x = ( _current - zoom_bar - > get_value ( ) ) * rect . size . width / zoom_bar - > get_page ( ) ;
if ( ofs_x < 0 | | ofs_x > = rect . size . width ) {
return ;
}
2023-08-13 00:33:39 +00:00
const Color color = get_theme_color ( SNAME ( " accent_color " ) , EditorStringName ( Editor ) ) ;
2022-07-20 23:00:58 +00:00
_indicator - > draw_line ( Point2 ( ofs_x , rect . position . y ) , Point2 ( ofs_x , rect . position . y + rect . size . height ) , color , Math : : round ( 2 * EDSCALE ) ) ;
_indicator - > draw_texture (
2023-08-13 00:33:39 +00:00
get_editor_theme_icon ( SNAME ( " TimelineIndicator " ) ) ,
Point2 ( ofs_x - get_editor_theme_icon ( SNAME ( " TimelineIndicator " ) ) - > get_width ( ) * 0.5 , rect . position . y ) ,
2022-07-20 23:00:58 +00:00
color ) ;
if ( stream - > get_bpm ( ) > 0 & & _hovering_beat ! = - 1 ) {
// Draw hovered beat.
float preview_offset = zoom_bar - > get_value ( ) ;
float preview_len = zoom_bar - > get_page ( ) ;
float beat_size = 60 / float ( stream - > get_bpm ( ) ) ;
int prev_beat = 0 ;
for ( int i = 0 ; i < rect . size . width ; i + + ) {
float ofs = preview_offset + i * preview_len / rect . size . width ;
int beat = int ( ofs / beat_size ) ;
if ( beat ! = prev_beat ) {
String text = itos ( beat ) ;
int text_w = beat_font - > get_string_size ( text ) . width ;
2022-10-27 23:39:28 +00:00
if ( i - text_w / 2 > 2 * EDSCALE & & beat = = _hovering_beat ) {
2022-07-20 23:00:58 +00:00
int x_ofs = i - text_w / 2 ;
_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 ) ;
break ;
}
prev_beat = beat ;
}
}
}
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _on_indicator_mouse_exited ( ) {
2022-07-20 23:00:58 +00:00
_hovering_beat = - 1 ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _on_input_indicator ( Ref < InputEvent > p_event ) {
2022-07-20 23:00:58 +00:00
const Ref < InputEventMouseButton > mb = p_event ;
if ( mb . is_valid ( ) & & mb - > get_button_index ( ) = = MouseButton : : LEFT ) {
if ( stream - > get_bpm ( ) > 0 ) {
2023-08-13 00:33:39 +00:00
int main_size = get_theme_font_size ( SNAME ( " main_size " ) , EditorStringName ( EditorFonts ) ) ;
Ref < Font > beat_font = get_theme_font ( SNAME ( " main " ) , EditorStringName ( EditorFonts ) ) ;
2022-07-20 23:00:58 +00:00
int y_ofs = beat_font - > get_height ( main_size ) + 4 * EDSCALE ;
if ( ( ! _dragging & & mb - > get_position ( ) . y < y_ofs ) | | _beat_len_dragging ) {
if ( mb - > is_pressed ( ) ) {
_set_beat_len_to ( mb - > get_position ( ) . x ) ;
_beat_len_dragging = true ;
} else {
_beat_len_dragging = false ;
}
return ;
}
}
if ( mb - > is_pressed ( ) ) {
_seek_to ( mb - > get_position ( ) . x ) ;
}
_dragging = mb - > is_pressed ( ) ;
}
const Ref < InputEventMouseMotion > mm = p_event ;
if ( mm . is_valid ( ) ) {
if ( _dragging ) {
_seek_to ( mm - > get_position ( ) . x ) ;
}
if ( _beat_len_dragging ) {
_set_beat_len_to ( mm - > get_position ( ) . x ) ;
}
if ( stream - > get_bpm ( ) > 0 ) {
2023-08-13 00:33:39 +00:00
int main_size = get_theme_font_size ( SNAME ( " main_size " ) , EditorStringName ( EditorFonts ) ) ;
Ref < Font > beat_font = get_theme_font ( SNAME ( " main " ) , EditorStringName ( EditorFonts ) ) ;
2022-07-20 23:00:58 +00:00
int y_ofs = beat_font - > get_height ( main_size ) + 4 * EDSCALE ;
if ( mm - > get_position ( ) . y < y_ofs ) {
int new_hovering_beat = _get_beat_at_pos ( mm - > get_position ( ) . x ) ;
if ( new_hovering_beat ! = _hovering_beat ) {
_hovering_beat = new_hovering_beat ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
} else if ( _hovering_beat ! = - 1 ) {
_hovering_beat = - 1 ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
}
}
}
2023-10-06 07:00:36 +00:00
int AudioStreamImportSettingsDialog : : _get_beat_at_pos ( real_t p_x ) {
2022-07-20 23:00:58 +00:00
float ofs_sec = zoom_bar - > get_value ( ) + p_x * zoom_bar - > get_page ( ) / _preview - > get_size ( ) . width ;
ofs_sec = CLAMP ( ofs_sec , 0 , stream - > get_length ( ) ) ;
float beat_size = 60 / float ( stream - > get_bpm ( ) ) ;
int beat = int ( ofs_sec / beat_size + 0.5 ) ;
if ( beat * beat_size > stream - > get_length ( ) + 0.001 ) { // Stream may end few audio frames before but may still want to use full loop.
beat - - ;
}
return beat ;
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _set_beat_len_to ( real_t p_x ) {
2022-07-20 23:00:58 +00:00
int beat = _get_beat_at_pos ( p_x ) ;
if ( beat < 1 ) {
beat = 1 ; // Because 0 is disable.
}
updating_settings = true ;
beats_enabled - > set_pressed ( true ) ;
beats_edit - > set_value ( beat ) ;
updating_settings = false ;
_settings_changed ( ) ;
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _seek_to ( real_t p_x ) {
2022-07-20 23:00:58 +00:00
_current = zoom_bar - > get_value ( ) + p_x / _preview - > get_rect ( ) . size . x * zoom_bar - > get_page ( ) ;
_current = CLAMP ( _current , 0 , stream - > get_length ( ) ) ;
_player - > seek ( _current ) ;
2022-08-13 21:21:24 +00:00
_indicator - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : edit ( const String & p_path , const String & p_importer , const Ref < AudioStream > & p_stream ) {
2022-07-20 23:00:58 +00:00
if ( ! stream . is_null ( ) ) {
2023-10-06 07:00:36 +00:00
stream - > disconnect_changed ( callable_mp ( this , & AudioStreamImportSettingsDialog : : _audio_changed ) ) ;
2022-07-20 23:00:58 +00:00
}
importer = p_importer ;
path = p_path ;
stream = p_stream ;
_player - > set_stream ( stream ) ;
_current = 0 ;
String text = String : : num ( stream - > get_length ( ) , 2 ) . pad_decimals ( 2 ) + " s " ;
_duration_label - > set_text ( text ) ;
if ( ! stream . is_null ( ) ) {
2023-10-06 07:00:36 +00:00
stream - > connect_changed ( callable_mp ( this , & AudioStreamImportSettingsDialog : : _audio_changed ) ) ;
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
color_rect - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
} else {
hide ( ) ;
}
params . clear ( ) ;
if ( stream . is_valid ( ) ) {
Ref < ConfigFile > config_file ;
config_file . instantiate ( ) ;
Error err = config_file - > load ( p_path + " .import " ) ;
updating_settings = true ;
if ( err = = OK ) {
double bpm = config_file - > get_value ( " params " , " bpm " , 0 ) ;
int beats = config_file - > get_value ( " params " , " beat_count " , 0 ) ;
bpm_edit - > set_value ( bpm > 0 ? bpm : 120 ) ;
bpm_enabled - > set_pressed ( bpm > 0 ) ;
beats_edit - > set_value ( beats ) ;
beats_enabled - > set_pressed ( beats > 0 ) ;
loop - > set_pressed ( config_file - > get_value ( " params " , " loop " , false ) ) ;
loop_offset - > set_value ( config_file - > get_value ( " params " , " loop_offset " , 0 ) ) ;
bar_beats_edit - > set_value ( config_file - > get_value ( " params " , " bar_beats " , 4 ) ) ;
List < String > keys ;
config_file - > get_section_keys ( " params " , & keys ) ;
for ( const String & K : keys ) {
params [ K ] = config_file - > get_value ( " params " , K ) ;
}
} else {
bpm_edit - > set_value ( false ) ;
bpm_enabled - > set_pressed ( false ) ;
beats_edit - > set_value ( 0 ) ;
beats_enabled - > set_pressed ( false ) ;
bar_beats_edit - > set_value ( 4 ) ;
loop - > set_pressed ( false ) ;
loop_offset - > set_value ( 0 ) ;
}
_preview_zoom_reset ( ) ;
updating_settings = false ;
_settings_changed ( ) ;
set_title ( vformat ( TTR ( " Audio Stream Importer: %s " ) , p_path . get_file ( ) ) ) ;
popup_centered ( ) ;
}
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _settings_changed ( ) {
2022-07-20 23:00:58 +00:00
if ( updating_settings ) {
return ;
}
updating_settings = true ;
stream - > call ( " set_loop " , loop - > is_pressed ( ) ) ;
stream - > call ( " set_loop_offset " , loop_offset - > get_value ( ) ) ;
2022-12-20 17:44:35 +00:00
if ( loop - > is_pressed ( ) ) {
loop_offset - > set_editable ( true ) ;
} else {
loop_offset - > set_editable ( false ) ;
}
2022-07-20 23:00:58 +00:00
if ( bpm_enabled - > is_pressed ( ) ) {
stream - > call ( " set_bpm " , bpm_edit - > get_value ( ) ) ;
2022-12-20 17:44:35 +00:00
beats_enabled - > set_disabled ( false ) ;
beats_edit - > set_editable ( true ) ;
bar_beats_edit - > set_editable ( true ) ;
2022-07-20 23:00:58 +00:00
double bpm = bpm_edit - > get_value ( ) ;
if ( bpm > 0 ) {
float beat_size = 60 / float ( bpm ) ;
int beat_max = int ( ( stream - > get_length ( ) + 0.001 ) / beat_size ) ;
int current_beat = beats_edit - > get_value ( ) ;
beats_edit - > set_max ( beat_max ) ;
if ( current_beat > beat_max ) {
beats_edit - > set_value ( beat_max ) ;
stream - > call ( " set_beat_count " , beat_max ) ;
}
}
stream - > call ( " set_bar_beats " , bar_beats_edit - > get_value ( ) ) ;
} else {
stream - > call ( " set_bpm " , 0 ) ;
stream - > call ( " set_bar_beats " , 4 ) ;
2022-12-20 17:44:35 +00:00
beats_enabled - > set_disabled ( true ) ;
beats_edit - > set_editable ( false ) ;
bar_beats_edit - > set_editable ( false ) ;
2022-07-20 23:00:58 +00:00
}
if ( bpm_enabled - > is_pressed ( ) & & beats_enabled - > is_pressed ( ) ) {
stream - > call ( " set_beat_count " , beats_edit - > get_value ( ) ) ;
} else {
stream - > call ( " set_beat_count " , 0 ) ;
}
updating_settings = false ;
2022-08-13 21:21:24 +00:00
_preview - > queue_redraw ( ) ;
_indicator - > queue_redraw ( ) ;
color_rect - > queue_redraw ( ) ;
2022-07-20 23:00:58 +00:00
}
2023-10-06 07:00:36 +00:00
void AudioStreamImportSettingsDialog : : _reimport ( ) {
2022-07-20 23:00:58 +00:00
params [ " loop " ] = loop - > is_pressed ( ) ;
params [ " loop_offset " ] = loop_offset - > get_value ( ) ;
params [ " bpm " ] = bpm_enabled - > is_pressed ( ) ? double ( bpm_edit - > get_value ( ) ) : double ( 0 ) ;
params [ " beat_count " ] = ( bpm_enabled - > is_pressed ( ) & & beats_enabled - > is_pressed ( ) ) ? int ( beats_edit - > get_value ( ) ) : int ( 0 ) ;
params [ " bar_beats " ] = ( bpm_enabled - > is_pressed ( ) ) ? int ( bar_beats_edit - > get_value ( ) ) : int ( 4 ) ;
EditorFileSystem : : get_singleton ( ) - > reimport_file_with_custom_parameters ( path , importer , params ) ;
}
2023-10-06 07:00:36 +00:00
AudioStreamImportSettingsDialog : : AudioStreamImportSettingsDialog ( ) {
2022-07-20 23:00:58 +00:00
get_ok_button ( ) - > set_text ( TTR ( " Reimport " ) ) ;
get_cancel_button ( ) - > set_text ( TTR ( " Close " ) ) ;
VBoxContainer * main_vbox = memnew ( VBoxContainer ) ;
add_child ( main_vbox ) ;
HBoxContainer * loop_hb = memnew ( HBoxContainer ) ;
loop_hb - > add_theme_constant_override ( " separation " , 4 * EDSCALE ) ;
loop = memnew ( CheckBox ) ;
loop - > set_text ( TTR ( " Enable " ) ) ;
2022-08-25 10:42:17 +00:00
loop - > set_tooltip_text ( TTR ( " Enable looping. " ) ) ;
2023-10-06 07:00:36 +00:00
loop - > connect ( " toggled " , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-07-20 23:00:58 +00:00
loop_hb - > add_child ( loop ) ;
loop_hb - > add_spacer ( ) ;
loop_hb - > add_child ( memnew ( Label ( TTR ( " Offset: " ) ) ) ) ;
loop_offset = memnew ( SpinBox ) ;
loop_offset - > set_max ( 10000 ) ;
loop_offset - > set_step ( 0.001 ) ;
loop_offset - > set_suffix ( " sec " ) ;
2022-08-25 10:42:17 +00:00
loop_offset - > set_tooltip_text ( TTR ( " Loop offset (from beginning). Note that if BPM is set, this setting will be ignored. " ) ) ;
2024-05-14 09:42:00 +00:00
loop_offset - > connect ( SceneStringName ( value_changed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-07-20 23:00:58 +00:00
loop_hb - > add_child ( loop_offset ) ;
main_vbox - > add_margin_child ( TTR ( " Loop: " ) , loop_hb ) ;
HBoxContainer * interactive_hb = memnew ( HBoxContainer ) ;
interactive_hb - > add_theme_constant_override ( " separation " , 4 * EDSCALE ) ;
bpm_enabled = memnew ( CheckBox ) ;
bpm_enabled - > set_text ( ( TTR ( " BPM: " ) ) ) ;
2023-10-06 07:00:36 +00:00
bpm_enabled - > connect ( " toggled " , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-07-20 23:00:58 +00:00
interactive_hb - > add_child ( bpm_enabled ) ;
bpm_edit = memnew ( SpinBox ) ;
bpm_edit - > set_max ( 400 ) ;
bpm_edit - > set_step ( 0.01 ) ;
2022-08-25 10:42:17 +00:00
bpm_edit - > set_tooltip_text ( TTR ( " Configure the Beats Per Measure (tempo) used for the interactive streams. \n This is required in order to configure beat information. " ) ) ;
2024-05-14 09:42:00 +00:00
bpm_edit - > connect ( SceneStringName ( value_changed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-07-20 23:00:58 +00:00
interactive_hb - > add_child ( bpm_edit ) ;
interactive_hb - > add_spacer ( ) ;
beats_enabled = memnew ( CheckBox ) ;
2022-12-19 16:42:54 +00:00
beats_enabled - > set_text ( TTR ( " Beat Count: " ) ) ;
2023-10-06 07:00:36 +00:00
beats_enabled - > connect ( " toggled " , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-07-20 23:00:58 +00:00
interactive_hb - > add_child ( beats_enabled ) ;
beats_edit = memnew ( SpinBox ) ;
2022-08-25 10:42:17 +00:00
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. \n It is recommended to set this value (either manually or by clicking on a beat number in the preview) to ensure looping works properly. " ) ) ;
2022-07-20 23:00:58 +00:00
beats_edit - > set_max ( 99999 ) ;
2024-05-14 09:42:00 +00:00
beats_edit - > connect ( SceneStringName ( value_changed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-07-20 23:00:58 +00:00
interactive_hb - > add_child ( beats_edit ) ;
2022-12-20 17:44:35 +00:00
bar_beats_label = memnew ( Label ( TTR ( " Bar Beats: " ) ) ) ;
interactive_hb - > add_child ( bar_beats_label ) ;
bar_beats_edit = memnew ( SpinBox ) ;
bar_beats_edit - > set_tooltip_text ( TTR ( " Configure the Beats Per Bar. This used for music-aware transitions between AudioStreams. " ) ) ;
bar_beats_edit - > set_min ( 2 ) ;
bar_beats_edit - > set_max ( 32 ) ;
2024-05-14 09:42:00 +00:00
bar_beats_edit - > connect ( SceneStringName ( value_changed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _settings_changed ) . unbind ( 1 ) ) ;
2022-12-20 17:44:35 +00:00
interactive_hb - > add_child ( bar_beats_edit ) ;
interactive_hb - > add_spacer ( ) ;
2022-07-20 23:00:58 +00:00
main_vbox - > add_margin_child ( TTR ( " Music Playback: " ) , interactive_hb ) ;
color_rect = memnew ( ColorRect ) ;
main_vbox - > add_margin_child ( TTR ( " Preview: " ) , color_rect ) ;
color_rect - > set_custom_minimum_size ( Size2 ( 600 , 200 ) * EDSCALE ) ;
color_rect - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
_player = memnew ( AudioStreamPlayer ) ;
2024-05-13 14:56:03 +00:00
_player - > connect ( SceneStringName ( finished ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _on_finished ) ) ;
2022-07-20 23:00:58 +00:00
color_rect - > add_child ( _player ) ;
VBoxContainer * vbox = memnew ( VBoxContainer ) ;
vbox - > set_anchors_and_offsets_preset ( Control : : PRESET_FULL_RECT , Control : : PRESET_MODE_MINSIZE , 0 ) ;
color_rect - > add_child ( vbox ) ;
vbox - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
_preview = memnew ( ColorRect ) ;
_preview - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
2024-05-13 14:56:03 +00:00
_preview - > connect ( SceneStringName ( draw ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _draw_preview ) ) ;
2022-07-20 23:00:58 +00:00
_preview - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
vbox - > add_child ( _preview ) ;
HBoxContainer * zoom_hbox = memnew ( HBoxContainer ) ;
zoom_bar = memnew ( HScrollBar ) ;
zoom_in = memnew ( Button ) ;
zoom_in - > set_flat ( true ) ;
zoom_reset = memnew ( Button ) ;
zoom_reset - > set_flat ( true ) ;
zoom_out = memnew ( Button ) ;
zoom_out - > set_flat ( true ) ;
zoom_hbox - > add_child ( zoom_bar ) ;
zoom_bar - > set_h_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
zoom_bar - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
zoom_hbox - > add_child ( zoom_out ) ;
zoom_hbox - > add_child ( zoom_reset ) ;
zoom_hbox - > add_child ( zoom_in ) ;
2024-05-14 07:40:21 +00:00
zoom_in - > connect ( SceneStringName ( pressed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _preview_zoom_in ) ) ;
zoom_reset - > connect ( SceneStringName ( pressed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _preview_zoom_reset ) ) ;
zoom_out - > connect ( SceneStringName ( pressed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _preview_zoom_out ) ) ;
2024-05-14 09:42:00 +00:00
zoom_bar - > connect ( SceneStringName ( value_changed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _preview_zoom_offset_changed ) ) ;
2022-07-20 23:00:58 +00:00
vbox - > add_child ( zoom_hbox ) ;
_indicator = memnew ( Control ) ;
_indicator - > set_anchors_and_offsets_preset ( Control : : PRESET_FULL_RECT ) ;
2024-05-13 14:56:03 +00:00
_indicator - > connect ( SceneStringName ( draw ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _draw_indicator ) ) ;
_indicator - > connect ( SceneStringName ( gui_input ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _on_input_indicator ) ) ;
_indicator - > connect ( SceneStringName ( mouse_exited ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _on_indicator_mouse_exited ) ) ;
2022-07-20 23:00:58 +00:00
_preview - > add_child ( _indicator ) ;
HBoxContainer * hbox = memnew ( HBoxContainer ) ;
hbox - > add_theme_constant_override ( " separation " , 0 ) ;
vbox - > add_child ( hbox ) ;
_play_button = memnew ( Button ) ;
_play_button - > set_flat ( true ) ;
hbox - > add_child ( _play_button ) ;
_play_button - > set_focus_mode ( Control : : FOCUS_NONE ) ;
2024-05-14 07:40:21 +00:00
_play_button - > connect ( SceneStringName ( pressed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _play ) ) ;
2022-07-20 23:00:58 +00:00
_stop_button = memnew ( Button ) ;
_stop_button - > set_flat ( true ) ;
hbox - > add_child ( _stop_button ) ;
_stop_button - > set_focus_mode ( Control : : FOCUS_NONE ) ;
2024-05-14 07:40:21 +00:00
_stop_button - > connect ( SceneStringName ( pressed ) , callable_mp ( this , & AudioStreamImportSettingsDialog : : _stop ) ) ;
2022-07-20 23:00:58 +00:00
_current_label = memnew ( Label ) ;
_current_label - > set_horizontal_alignment ( HORIZONTAL_ALIGNMENT_RIGHT ) ;
_current_label - > set_h_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
_current_label - > set_modulate ( Color ( 1 , 1 , 1 , 0.5 ) ) ;
hbox - > add_child ( _current_label ) ;
_duration_label = memnew ( Label ) ;
hbox - > add_child ( _duration_label ) ;
singleton = this ;
}