2017-03-05 14:47:28 +00:00
/**************************************************************************/
/* import_dock.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. */
/**************************************************************************/
2018-01-04 23:50:27 +00:00
2017-02-01 12:45:45 +00:00
# include "import_dock.h"
2022-02-12 01:46:22 +00:00
# include "core/config/project_settings.h"
# include "editor/editor_node.h"
# include "editor/editor_resource_preview.h"
2022-07-31 18:14:15 +00:00
# include "editor/editor_settings.h"
2023-08-13 00:33:39 +00:00
# include "editor/editor_string_names.h"
2023-06-30 23:29:46 +00:00
# include "editor/editor_undo_redo_manager.h"
2024-01-15 12:14:55 +00:00
# include "editor/themes/editor_scale.h"
2022-11-22 23:14:08 +00:00
# include "editor/themes/editor_theme_manager.h"
2017-02-01 12:45:45 +00:00
class ImportDockParameters : public Object {
2019-03-19 18:35:57 +00:00
GDCLASS ( ImportDockParameters , Object ) ;
2017-02-01 12:45:45 +00:00
public :
2022-05-13 13:04:37 +00:00
HashMap < StringName , Variant > values ;
2017-02-01 12:45:45 +00:00
List < PropertyInfo > properties ;
Ref < ResourceImporter > importer ;
Vector < String > paths ;
2022-05-19 15:00:06 +00:00
HashSet < StringName > checked ;
2024-02-05 10:10:37 +00:00
bool checking = false ;
bool skip = false ;
2021-11-14 17:02:38 +00:00
String base_options_path ;
2017-02-01 12:45:45 +00:00
bool _set ( const StringName & p_name , const Variant & p_value ) {
if ( values . has ( p_name ) ) {
values [ p_name ] = p_value ;
2018-10-30 16:11:54 +00:00
if ( checking ) {
checked . insert ( p_name ) ;
2021-02-10 20:18:45 +00:00
notify_property_list_changed ( ) ;
2018-10-30 16:11:54 +00:00
}
2017-02-01 12:45:45 +00:00
return true ;
}
return false ;
}
bool _get ( const StringName & p_name , Variant & r_ret ) const {
if ( values . has ( p_name ) ) {
r_ret = values [ p_name ] ;
return true ;
}
return false ;
}
void _get_property_list ( List < PropertyInfo > * p_list ) const {
2021-07-16 03:45:57 +00:00
for ( const PropertyInfo & E : properties ) {
2021-11-14 17:02:38 +00:00
if ( ! importer - > get_option_visibility ( base_options_path , E . name , values ) ) {
2017-02-01 12:45:45 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2021-07-16 03:45:57 +00:00
PropertyInfo pi = E ;
2018-10-30 16:11:54 +00:00
if ( checking ) {
pi . usage | = PROPERTY_USAGE_CHECKABLE ;
2021-07-16 03:45:57 +00:00
if ( checked . has ( E . name ) ) {
2018-10-30 16:11:54 +00:00
pi . usage | = PROPERTY_USAGE_CHECKED ;
}
}
p_list - > push_back ( pi ) ;
2017-02-01 12:45:45 +00:00
}
}
void update ( ) {
2021-02-10 20:18:45 +00:00
notify_property_list_changed ( ) ;
2017-02-01 12:45:45 +00:00
}
} ;
2021-11-17 20:08:55 +00:00
ImportDock * ImportDock : : singleton = nullptr ;
2017-02-01 12:45:45 +00:00
void ImportDock : : set_edit_path ( const String & p_path ) {
Ref < ConfigFile > config ;
2021-06-17 22:03:09 +00:00
config . instantiate ( ) ;
2017-02-01 12:45:45 +00:00
Error err = config - > load ( p_path + " .import " ) ;
if ( err ! = OK ) {
clear ( ) ;
return ;
}
2021-03-22 19:41:47 +00:00
String importer_name = config - > get_value ( " remap " , " importer " ) ;
2024-02-05 10:10:37 +00:00
if ( importer_name = = " keep " ) {
params - > importer . unref ( ) ;
params - > skip = false ;
} else if ( importer_name = = " skip " ) {
params - > importer . unref ( ) ;
params - > skip = true ;
} else {
params - > importer = ResourceFormatImporter : : get_singleton ( ) - > get_importer_by_name ( importer_name ) ;
params - > skip = false ;
}
2017-02-01 12:45:45 +00:00
2020-02-28 03:03:55 +00:00
params - > paths . clear ( ) ;
params - > paths . push_back ( p_path ) ;
2021-11-14 17:02:38 +00:00
params - > base_options_path = p_path ;
2020-02-28 03:03:55 +00:00
2021-11-14 17:02:38 +00:00
_update_options ( p_path , config ) ;
2017-02-01 12:45:45 +00:00
2020-03-17 06:33:00 +00:00
List < Ref < ResourceImporter > > importers ;
2017-02-01 12:45:45 +00:00
ResourceFormatImporter : : get_singleton ( ) - > get_importers_for_extension ( p_path . get_extension ( ) , & importers ) ;
2020-03-17 06:33:00 +00:00
List < Pair < String , String > > importer_names ;
2017-02-01 12:45:45 +00:00
2021-07-26 15:50:35 +00:00
for ( const Ref < ResourceImporter > & E : importers ) {
2021-07-16 03:45:57 +00:00
importer_names . push_back ( Pair < String , String > ( E - > get_visible_name ( ) , E - > get_importer_name ( ) ) ) ;
2017-02-01 12:45:45 +00:00
}
2020-03-17 06:33:00 +00:00
importer_names . sort_custom < PairSort < String , String > > ( ) ;
2017-02-01 12:45:45 +00:00
import_as - > clear ( ) ;
2021-07-24 13:46:25 +00:00
for ( const Pair < String , String > & E : importer_names ) {
2021-07-16 03:45:57 +00:00
import_as - > add_item ( E . first ) ;
2022-03-12 00:06:45 +00:00
import_as - > set_item_metadata ( - 1 , E . second ) ;
2021-07-16 03:45:57 +00:00
if ( E . second = = importer_name ) {
2017-02-01 12:45:45 +00:00
import_as - > select ( import_as - > get_item_count ( ) - 1 ) ;
}
}
2021-09-22 05:10:09 +00:00
_add_keep_import_option ( importer_name ) ;
2021-03-22 19:41:47 +00:00
2017-11-13 19:31:00 +00:00
import - > set_disabled ( false ) ;
2021-10-09 10:29:53 +00:00
_set_dirty ( false ) ;
2017-11-13 19:31:00 +00:00
import_as - > set_disabled ( false ) ;
2019-09-04 05:20:57 +00:00
preset - > set_disabled ( false ) ;
2021-12-25 15:31:36 +00:00
content - > show ( ) ;
select_a_resource - > hide ( ) ;
2017-11-13 19:31:00 +00:00
imported - > set_text ( p_path . get_file ( ) ) ;
}
2021-09-22 05:10:09 +00:00
void ImportDock : : _add_keep_import_option ( const String & p_importer_name ) {
import_as - > add_separator ( ) ;
2024-02-05 10:10:37 +00:00
import_as - > add_item ( TTR ( " Keep File (exported as is) " ) ) ;
2022-03-12 00:06:45 +00:00
import_as - > set_item_metadata ( - 1 , " keep " ) ;
2024-02-05 10:10:37 +00:00
import_as - > add_item ( TTR ( " Skip File (not exported) " ) ) ;
import_as - > set_item_metadata ( - 1 , " skip " ) ;
2021-09-22 05:10:09 +00:00
if ( p_importer_name = = " keep " ) {
2024-02-05 10:10:37 +00:00
import_as - > select ( import_as - > get_item_count ( ) - 2 ) ;
} else if ( p_importer_name = = " skip " ) {
2021-09-22 05:10:09 +00:00
import_as - > select ( import_as - > get_item_count ( ) - 1 ) ;
}
}
2021-11-14 17:02:38 +00:00
void ImportDock : : _update_options ( const String & p_path , const Ref < ConfigFile > & p_config ) {
2021-05-13 20:34:34 +00:00
// Set the importer class to fetch the correct class in the XML class reference.
// This allows tooltips to display when hovering properties.
if ( params - > importer ! = nullptr ) {
2024-02-05 10:10:37 +00:00
// Null check to avoid crashing if the "Keep File (exported as is)" mode is selected.
2021-05-13 20:34:34 +00:00
import_opts - > set_object_class ( params - > importer - > get_class_name ( ) ) ;
}
2017-11-13 19:31:00 +00:00
List < ResourceImporter : : ImportOption > options ;
2021-03-22 19:41:47 +00:00
if ( params - > importer . is_valid ( ) ) {
2021-11-14 17:02:38 +00:00
params - > importer - > get_import_options ( p_path , & options ) ;
2021-03-22 19:41:47 +00:00
}
2017-11-13 19:31:00 +00:00
params - > properties . clear ( ) ;
params - > values . clear ( ) ;
2020-02-28 03:03:55 +00:00
params - > checking = params - > paths . size ( ) > 1 ;
2018-10-30 16:11:54 +00:00
params - > checked . clear ( ) ;
2021-11-14 17:02:38 +00:00
params - > base_options_path = p_path ;
2017-11-13 19:31:00 +00:00
2021-07-24 13:46:25 +00:00
for ( const ResourceImporter : : ImportOption & E : options ) {
2021-07-16 03:45:57 +00:00
params - > properties . push_back ( E . option ) ;
if ( p_config . is_valid ( ) & & p_config - > has_section_key ( " params " , E . option . name ) ) {
params - > values [ E . option . name ] = p_config - > get_value ( " params " , E . option . name ) ;
2017-11-13 19:31:00 +00:00
} else {
2021-07-16 03:45:57 +00:00
params - > values [ E . option . name ] = E . default_value ;
2017-11-13 19:31:00 +00:00
}
}
params - > update ( ) ;
2020-02-14 01:58:19 +00:00
_update_preset_menu ( ) ;
2021-03-19 12:57:52 +00:00
2024-04-23 20:35:49 +00:00
bool was_imported = p_config . is_valid ( ) & & p_config - > get_value ( " remap " , " importer " ) ! = " skip " & & p_config - > get_value ( " remap " , " importer " ) ! = " keep " ;
if ( was_imported & & params - > importer . is_valid ( ) & & params - > paths . size ( ) = = 1 & & params - > importer - > has_advanced_options ( ) ) {
2021-03-19 12:57:52 +00:00
advanced - > show ( ) ;
advanced_spacer - > show ( ) ;
} else {
advanced - > hide ( ) ;
advanced_spacer - > hide ( ) ;
}
2017-02-01 12:45:45 +00:00
}
void ImportDock : : set_edit_multiple_paths ( const Vector < String > & p_paths ) {
clear ( ) ;
2019-11-22 07:35:03 +00:00
// Use the value that is repeated the most.
2022-05-13 13:04:37 +00:00
HashMap < String , Dictionary > value_frequency ;
2022-05-19 15:00:06 +00:00
HashSet < String > extensions ;
2017-02-01 12:45:45 +00:00
for ( int i = 0 ; i < p_paths . size ( ) ; i + + ) {
Ref < ConfigFile > config ;
2021-06-17 22:03:09 +00:00
config . instantiate ( ) ;
2021-11-14 17:02:38 +00:00
extensions . insert ( p_paths [ i ] . get_extension ( ) ) ;
2017-02-01 12:45:45 +00:00
Error err = config - > load ( p_paths [ i ] + " .import " ) ;
ERR_CONTINUE ( err ! = OK ) ;
if ( i = = 0 ) {
2024-02-05 10:10:37 +00:00
String importer_name = config - > get_value ( " remap " , " importer " ) ;
if ( importer_name = = " keep " ) {
params - > importer . unref ( ) ;
params - > skip = false ;
} else if ( importer_name = = " skip " ) {
params - > importer . unref ( ) ;
params - > skip = true ;
} else {
params - > importer = ResourceFormatImporter : : get_singleton ( ) - > get_importer_by_name ( importer_name ) ;
params - > skip = false ;
}
2017-02-01 12:45:45 +00:00
if ( params - > importer . is_null ( ) ) {
clear ( ) ;
return ;
}
}
2021-03-26 02:05:25 +00:00
if ( ! config - > has_section ( " params " ) ) {
continue ;
}
2017-02-01 12:45:45 +00:00
List < String > keys ;
config - > get_section_keys ( " params " , & keys ) ;
2021-07-24 13:46:25 +00:00
for ( const String & E : keys ) {
2021-07-16 03:45:57 +00:00
if ( ! value_frequency . has ( E ) ) {
value_frequency [ E ] = Dictionary ( ) ;
2017-02-01 12:45:45 +00:00
}
2021-07-16 03:45:57 +00:00
Variant value = config - > get_value ( " params " , E ) ;
2017-02-01 12:45:45 +00:00
2021-07-16 03:45:57 +00:00
if ( value_frequency [ E ] . has ( value ) ) {
value_frequency [ E ] [ value ] = int ( value_frequency [ E ] [ value ] ) + 1 ;
2017-02-01 12:45:45 +00:00
} else {
2021-07-16 03:45:57 +00:00
value_frequency [ E ] [ value ] = 1 ;
2017-02-01 12:45:45 +00:00
}
}
}
ERR_FAIL_COND ( params - > importer . is_null ( ) ) ;
2021-11-14 17:02:38 +00:00
String base_path ;
if ( extensions . size ( ) = = 1 & & p_paths . size ( ) > 0 ) {
base_path = p_paths [ 0 ] ;
}
2017-02-01 12:45:45 +00:00
List < ResourceImporter : : ImportOption > options ;
2021-11-14 17:02:38 +00:00
params - > importer - > get_import_options ( base_path , & options ) ;
2017-02-01 12:45:45 +00:00
params - > properties . clear ( ) ;
params - > values . clear ( ) ;
2018-10-30 16:11:54 +00:00
params - > checking = true ;
params - > checked . clear ( ) ;
2021-11-14 17:02:38 +00:00
params - > base_options_path = base_path ;
2017-02-01 12:45:45 +00:00
2021-07-24 13:46:25 +00:00
for ( const ResourceImporter : : ImportOption & E : options ) {
2021-07-16 03:45:57 +00:00
params - > properties . push_back ( E . option ) ;
2017-02-01 12:45:45 +00:00
2021-07-16 03:45:57 +00:00
if ( value_frequency . has ( E . option . name ) ) {
Dictionary d = value_frequency [ E . option . name ] ;
2017-02-01 12:45:45 +00:00
int freq = 0 ;
List < Variant > v ;
d . get_key_list ( & v ) ;
Variant value ;
2021-07-24 13:46:25 +00:00
for ( const Variant & F : v ) {
2021-07-16 03:45:57 +00:00
int f = d [ F ] ;
2017-02-01 12:45:45 +00:00
if ( f > freq ) {
2021-07-16 03:45:57 +00:00
value = F ;
2017-02-01 12:45:45 +00:00
}
}
2021-07-16 03:45:57 +00:00
params - > values [ E . option . name ] = value ;
2017-02-01 12:45:45 +00:00
} else {
2021-07-16 03:45:57 +00:00
params - > values [ E . option . name ] = E . default_value ;
2017-02-01 12:45:45 +00:00
}
}
params - > update ( ) ;
2020-03-17 06:33:00 +00:00
List < Ref < ResourceImporter > > importers ;
2017-02-01 12:45:45 +00:00
ResourceFormatImporter : : get_singleton ( ) - > get_importers_for_extension ( p_paths [ 0 ] . get_extension ( ) , & importers ) ;
2020-03-17 06:33:00 +00:00
List < Pair < String , String > > importer_names ;
2017-02-01 12:45:45 +00:00
2021-07-26 15:50:35 +00:00
for ( const Ref < ResourceImporter > & E : importers ) {
2021-07-16 03:45:57 +00:00
importer_names . push_back ( Pair < String , String > ( E - > get_visible_name ( ) , E - > get_importer_name ( ) ) ) ;
2017-02-01 12:45:45 +00:00
}
2020-03-17 06:33:00 +00:00
importer_names . sort_custom < PairSort < String , String > > ( ) ;
2017-02-01 12:45:45 +00:00
import_as - > clear ( ) ;
2021-07-24 13:46:25 +00:00
for ( const Pair < String , String > & E : importer_names ) {
2021-07-16 03:45:57 +00:00
import_as - > add_item ( E . first ) ;
2022-03-12 00:06:45 +00:00
import_as - > set_item_metadata ( - 1 , E . second ) ;
2021-07-16 03:45:57 +00:00
if ( E . second = = params - > importer - > get_importer_name ( ) ) {
2017-02-01 12:45:45 +00:00
import_as - > select ( import_as - > get_item_count ( ) - 1 ) ;
}
}
2021-09-22 05:10:09 +00:00
_add_keep_import_option ( params - > importer - > get_importer_name ( ) ) ;
2020-02-14 01:58:19 +00:00
_update_preset_menu ( ) ;
params - > paths = p_paths ;
import - > set_disabled ( false ) ;
2021-10-09 10:29:53 +00:00
_set_dirty ( false ) ;
2020-02-14 01:58:19 +00:00
import_as - > set_disabled ( false ) ;
preset - > set_disabled ( false ) ;
2022-01-06 17:32:24 +00:00
content - > show ( ) ;
select_a_resource - > hide ( ) ;
2020-02-14 01:58:19 +00:00
2020-02-19 08:58:34 +00:00
imported - > set_text ( vformat ( TTR ( " %d Files " ) , p_paths . size ( ) ) ) ;
2021-03-19 12:57:52 +00:00
if ( params - > paths . size ( ) = = 1 & & params - > importer - > has_advanced_options ( ) ) {
advanced - > show ( ) ;
advanced_spacer - > show ( ) ;
} else {
advanced - > hide ( ) ;
advanced_spacer - > hide ( ) ;
}
2020-02-14 01:58:19 +00:00
}
2023-03-20 07:47:41 +00:00
void ImportDock : : reimport_resources ( const Vector < String > & p_paths ) {
switch ( p_paths . size ( ) ) {
case 0 :
ERR_FAIL_MSG ( " You need to select files to reimport them. " ) ;
case 1 :
set_edit_path ( p_paths [ 0 ] ) ;
break ;
default :
set_edit_multiple_paths ( p_paths ) ;
break ;
}
_reimport_attempt ( ) ;
}
2020-02-14 01:58:19 +00:00
void ImportDock : : _update_preset_menu ( ) {
2017-02-01 12:45:45 +00:00
preset - > get_popup ( ) - > clear ( ) ;
2021-03-22 19:41:47 +00:00
if ( params - > importer . is_null ( ) ) {
preset - > get_popup ( ) - > add_item ( TTR ( " Default " ) ) ;
preset - > hide ( ) ;
return ;
}
preset - > show ( ) ;
2017-02-01 12:45:45 +00:00
if ( params - > importer - > get_preset_count ( ) = = 0 ) {
preset - > get_popup ( ) - > add_item ( TTR ( " Default " ) ) ;
} else {
for ( int i = 0 ; i < params - > importer - > get_preset_count ( ) ; i + + ) {
preset - > get_popup ( ) - > add_item ( params - > importer - > get_preset_name ( i ) ) ;
}
}
2020-02-14 01:58:19 +00:00
preset - > get_popup ( ) - > add_separator ( ) ;
preset - > get_popup ( ) - > add_item ( vformat ( TTR ( " Set as Default for '%s' " ) , params - > importer - > get_visible_name ( ) ) , ITEM_SET_AS_DEFAULT ) ;
if ( ProjectSettings : : get_singleton ( ) - > has_setting ( " importer_defaults/ " + params - > importer - > get_importer_name ( ) ) ) {
preset - > get_popup ( ) - > add_item ( TTR ( " Load Default " ) , ITEM_LOAD_DEFAULT ) ;
preset - > get_popup ( ) - > add_separator ( ) ;
preset - > get_popup ( ) - > add_item ( vformat ( TTR ( " Clear Default for '%s' " ) , params - > importer - > get_visible_name ( ) ) , ITEM_CLEAR_DEFAULT ) ;
}
2017-02-01 12:45:45 +00:00
}
2017-11-10 20:53:01 +00:00
void ImportDock : : _importer_selected ( int i_idx ) {
String name = import_as - > get_selected_metadata ( ) ;
2021-03-22 19:41:47 +00:00
if ( name = = " keep " ) {
params - > importer . unref ( ) ;
2024-02-05 10:10:37 +00:00
params - > skip = false ;
_update_options ( params - > base_options_path , Ref < ConfigFile > ( ) ) ;
} else if ( name = = " skip " ) {
params - > importer . unref ( ) ;
params - > skip = true ;
2021-11-14 17:02:38 +00:00
_update_options ( params - > base_options_path , Ref < ConfigFile > ( ) ) ;
2021-03-22 19:41:47 +00:00
} else {
Ref < ResourceImporter > importer = ResourceFormatImporter : : get_singleton ( ) - > get_importer_by_name ( name ) ;
ERR_FAIL_COND ( importer . is_null ( ) ) ;
2017-11-10 20:53:01 +00:00
2021-03-22 19:41:47 +00:00
params - > importer = importer ;
2024-02-05 10:10:37 +00:00
params - > skip = false ;
2021-03-22 19:41:47 +00:00
Ref < ConfigFile > config ;
if ( params - > paths . size ( ) ) {
2021-11-14 17:02:38 +00:00
String path = params - > paths [ 0 ] ;
2021-06-17 22:03:09 +00:00
config . instantiate ( ) ;
2021-11-14 17:02:38 +00:00
Error err = config - > load ( path + " .import " ) ;
2021-03-22 19:41:47 +00:00
if ( err ! = OK ) {
config . unref ( ) ;
}
2017-11-13 19:31:00 +00:00
}
2021-11-14 17:02:38 +00:00
_update_options ( params - > base_options_path , config ) ;
2017-11-13 19:31:00 +00:00
}
2017-11-10 20:53:01 +00:00
}
2017-02-01 12:45:45 +00:00
void ImportDock : : _preset_selected ( int p_idx ) {
2017-08-31 14:55:54 +00:00
int item_id = preset - > get_popup ( ) - > get_item_id ( p_idx ) ;
2017-02-01 12:45:45 +00:00
2017-08-31 14:55:54 +00:00
switch ( item_id ) {
case ITEM_SET_AS_DEFAULT : {
2017-07-23 21:48:05 +00:00
Dictionary d ;
2017-02-01 12:45:45 +00:00
2021-07-16 03:45:57 +00:00
for ( const PropertyInfo & E : params - > properties ) {
d [ E . name ] = params - > values [ E . name ] ;
2017-07-23 21:48:05 +00:00
}
2017-02-01 12:45:45 +00:00
2017-07-23 21:48:05 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " importer_defaults/ " + params - > importer - > get_importer_name ( ) , d ) ;
ProjectSettings : : get_singleton ( ) - > save ( ) ;
2020-02-14 01:58:19 +00:00
_update_preset_menu ( ) ;
2017-07-23 21:48:05 +00:00
} break ;
case ITEM_LOAD_DEFAULT : {
2017-10-05 18:34:34 +00:00
ERR_FAIL_COND ( ! ProjectSettings : : get_singleton ( ) - > has_setting ( " importer_defaults/ " + params - > importer - > get_importer_name ( ) ) ) ;
2017-07-23 21:48:05 +00:00
2022-10-18 14:43:37 +00:00
Dictionary d = GLOBAL_GET ( " importer_defaults/ " + params - > importer - > get_importer_name ( ) ) ;
2017-07-23 21:48:05 +00:00
List < Variant > v ;
d . get_key_list ( & v ) ;
2020-02-14 01:58:19 +00:00
if ( params - > checking ) {
params - > checked . clear ( ) ;
}
2021-07-24 13:46:25 +00:00
for ( const Variant & E : v ) {
2021-07-16 03:45:57 +00:00
params - > values [ E ] = d [ E ] ;
2020-02-14 01:58:19 +00:00
if ( params - > checking ) {
2021-07-16 03:45:57 +00:00
params - > checked . insert ( E ) ;
2020-02-14 01:58:19 +00:00
}
2017-07-23 21:48:05 +00:00
}
params - > update ( ) ;
} break ;
case ITEM_CLEAR_DEFAULT : {
ProjectSettings : : get_singleton ( ) - > set ( " importer_defaults/ " + params - > importer - > get_importer_name ( ) , Variant ( ) ) ;
ProjectSettings : : get_singleton ( ) - > save ( ) ;
2020-02-14 01:58:19 +00:00
_update_preset_menu ( ) ;
2017-07-23 21:48:05 +00:00
} break ;
default : {
List < ResourceImporter : : ImportOption > options ;
2021-11-14 17:02:38 +00:00
params - > importer - > get_import_options ( params - > base_options_path , & options , p_idx ) ;
2017-07-23 21:48:05 +00:00
2020-02-14 01:58:19 +00:00
if ( params - > checking ) {
params - > checked . clear ( ) ;
}
2021-07-24 13:46:25 +00:00
for ( const ResourceImporter : : ImportOption & E : options ) {
2021-07-16 03:45:57 +00:00
params - > values [ E . option . name ] = E . default_value ;
2020-02-14 01:58:19 +00:00
if ( params - > checking ) {
2021-07-16 03:45:57 +00:00
params - > checked . insert ( E . option . name ) ;
2020-02-14 01:58:19 +00:00
}
2017-07-23 21:48:05 +00:00
}
params - > update ( ) ;
} break ;
}
2017-02-01 12:45:45 +00:00
}
void ImportDock : : clear ( ) {
imported - > set_text ( " " ) ;
import - > set_disabled ( true ) ;
import_as - > clear ( ) ;
import_as - > set_disabled ( true ) ;
2019-09-04 05:20:57 +00:00
preset - > set_disabled ( true ) ;
2017-02-01 12:45:45 +00:00
params - > values . clear ( ) ;
params - > properties . clear ( ) ;
params - > update ( ) ;
preset - > get_popup ( ) - > clear ( ) ;
2021-12-25 15:31:36 +00:00
content - > hide ( ) ;
select_a_resource - > show ( ) ;
2017-02-01 12:45:45 +00:00
}
2019-01-25 20:23:56 +00:00
static bool _find_owners ( EditorFileSystemDirectory * efsd , const String & p_path ) {
2020-05-14 14:41:43 +00:00
if ( ! efsd ) {
2019-01-25 20:23:56 +00:00
return false ;
2020-05-14 14:41:43 +00:00
}
2019-01-25 20:23:56 +00:00
for ( int i = 0 ; i < efsd - > get_subdir_count ( ) ; i + + ) {
if ( _find_owners ( efsd - > get_subdir ( i ) , p_path ) ) {
return true ;
}
}
for ( int i = 0 ; i < efsd - > get_file_count ( ) ; i + + ) {
Vector < String > deps = efsd - > get_file_deps ( i ) ;
2022-02-01 18:19:13 +00:00
if ( deps . has ( p_path ) ) {
2019-01-25 20:23:56 +00:00
return true ;
2020-05-14 14:41:43 +00:00
}
2019-01-25 20:23:56 +00:00
}
return false ;
}
2020-05-14 12:29:06 +00:00
2024-04-23 20:35:49 +00:00
void ImportDock : : _reimport_pressed ( ) {
_reimport_attempt ( ) ;
if ( params - > importer . is_valid ( ) & & params - > paths . size ( ) = = 1 & & params - > importer - > has_advanced_options ( ) ) {
advanced - > show ( ) ;
advanced_spacer - > show ( ) ;
} else {
advanced - > hide ( ) ;
advanced_spacer - > hide ( ) ;
}
}
2019-01-25 20:23:56 +00:00
void ImportDock : : _reimport_attempt ( ) {
bool used_in_resources = false ;
2021-03-22 19:41:47 +00:00
String importer_name ;
if ( params - > importer . is_valid ( ) ) {
importer_name = params - > importer - > get_importer_name ( ) ;
} else {
2024-02-05 10:10:37 +00:00
if ( params - > skip ) {
importer_name = " skip " ;
} else {
importer_name = " keep " ;
}
2021-03-22 19:41:47 +00:00
}
2019-01-25 20:23:56 +00:00
for ( int i = 0 ; i < params - > paths . size ( ) ; i + + ) {
Ref < ConfigFile > config ;
2021-06-17 22:03:09 +00:00
config . instantiate ( ) ;
2019-01-25 20:23:56 +00:00
Error err = config - > load ( params - > paths [ i ] + " .import " ) ;
ERR_CONTINUE ( err ! = OK ) ;
String imported_with = config - > get_value ( " remap " , " importer " ) ;
2024-04-23 20:35:49 +00:00
if ( imported_with ! = importer_name & & imported_with ! = " keep " & & imported_with ! = " skip " ) {
2023-09-28 16:10:18 +00:00
Ref < Resource > resource = ResourceLoader : : load ( params - > paths [ i ] ) ;
if ( resource . is_valid ( ) ) {
need_cleanup . push_back ( params - > paths [ i ] ) ;
if ( _find_owners ( EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) , params - > paths [ i ] ) ) {
used_in_resources = true ;
}
2019-01-25 20:23:56 +00:00
}
}
}
2023-06-30 23:29:46 +00:00
if ( ! need_cleanup . is_empty ( ) | | used_in_resources ) {
cleanup_warning - > set_visible ( ! need_cleanup . is_empty ( ) ) ;
2019-01-25 20:23:56 +00:00
label_warning - > set_visible ( used_in_resources ) ;
2020-03-06 17:00:16 +00:00
reimport_confirm - > popup_centered ( ) ;
2019-01-25 20:23:56 +00:00
return ;
}
_reimport ( ) ;
}
2023-06-30 23:29:46 +00:00
void ImportDock : : _reimport_and_cleanup ( ) {
HashMap < String , Ref < Resource > > old_resources ;
for ( const String & path : need_cleanup ) {
Ref < Resource > res = ResourceLoader : : load ( path ) ;
res - > set_path ( " " ) ;
res - > set_meta ( SNAME ( " _skip_save_ " ) , true ) ;
old_resources [ path ] = res ;
}
EditorResourcePreview : : get_singleton ( ) - > stop ( ) ; // Don't try to re-create previews after import.
2019-01-25 20:23:56 +00:00
_reimport ( ) ;
2023-06-30 23:29:46 +00:00
if ( need_cleanup . is_empty ( ) ) {
return ;
}
// After changing resource type we need to make sure that all old instances are unloaded or replaced.
EditorNode : : get_singleton ( ) - > push_item ( nullptr ) ;
EditorUndoRedoManager : : get_singleton ( ) - > clear_history ( ) ;
List < Ref < Resource > > external_resources ;
ResourceCache : : get_cached_resources ( & external_resources ) ;
for ( const String & path : need_cleanup ) {
Ref < Resource > old_res = old_resources [ path ] ;
2024-04-23 20:35:49 +00:00
Ref < Resource > new_res ;
if ( params - > importer . is_valid ( ) ) {
new_res = ResourceLoader : : load ( path ) ;
}
2023-06-30 23:29:46 +00:00
2023-08-02 05:03:28 +00:00
for ( int i = 0 ; i < EditorNode : : get_editor_data ( ) . get_edited_scene_count ( ) ; i + + ) {
Node * edited_scene_root = EditorNode : : get_editor_data ( ) . get_edited_scene_root ( i ) ;
if ( likely ( edited_scene_root ) ) {
_replace_resource_in_object ( edited_scene_root , old_res , new_res ) ;
}
2023-06-30 23:29:46 +00:00
}
for ( Ref < Resource > res : external_resources ) {
_replace_resource_in_object ( res . ptr ( ) , old_res , new_res ) ;
}
}
need_cleanup . clear ( ) ;
2019-01-25 20:23:56 +00:00
}
2021-03-19 12:57:52 +00:00
void ImportDock : : _advanced_options ( ) {
if ( params - > paths . size ( ) = = 1 & & params - > importer . is_valid ( ) ) {
params - > importer - > show_advanced_options ( params - > paths [ 0 ] ) ;
}
}
2024-02-05 10:10:37 +00:00
2017-02-01 12:45:45 +00:00
void ImportDock : : _reimport ( ) {
for ( int i = 0 ; i < params - > paths . size ( ) ; i + + ) {
Ref < ConfigFile > config ;
2021-06-17 22:03:09 +00:00
config . instantiate ( ) ;
2017-02-01 12:45:45 +00:00
Error err = config - > load ( params - > paths [ i ] + " .import " ) ;
ERR_CONTINUE ( err ! = OK ) ;
2021-03-22 19:41:47 +00:00
if ( params - > importer . is_valid ( ) ) {
String importer_name = params - > importer - > get_importer_name ( ) ;
2019-04-19 18:54:33 +00:00
2021-03-22 19:41:47 +00:00
if ( params - > checking & & config - > get_value ( " remap " , " importer " ) = = params - > importer - > get_importer_name ( ) ) {
//update only what is edited (checkboxes) if the importer is the same
2021-07-24 13:46:25 +00:00
for ( const PropertyInfo & E : params - > properties ) {
2021-07-16 03:45:57 +00:00
if ( params - > checked . has ( E . name ) ) {
config - > set_value ( " params " , E . name , params - > values [ E . name ] ) ;
2021-03-22 19:41:47 +00:00
}
}
} else {
//override entirely
config - > set_value ( " remap " , " importer " , importer_name ) ;
if ( config - > has_section ( " params " ) ) {
config - > erase_section ( " params " ) ;
}
2021-07-24 13:46:25 +00:00
for ( const PropertyInfo & E : params - > properties ) {
2021-07-16 03:45:57 +00:00
config - > set_value ( " params " , E . name , params - > values [ E . name ] ) ;
2018-10-30 16:11:54 +00:00
}
}
2017-02-01 12:45:45 +00:00
2021-03-22 19:41:47 +00:00
//handle group file
Ref < ResourceImporter > importer = ResourceFormatImporter : : get_singleton ( ) - > get_importer_by_name ( importer_name ) ;
ERR_CONTINUE ( ! importer . is_valid ( ) ) ;
String group_file_property = importer - > get_option_group_file ( ) ;
2021-12-09 09:42:46 +00:00
if ( ! group_file_property . is_empty ( ) ) {
2021-03-22 19:41:47 +00:00
//can import from a group (as in, atlas)
ERR_CONTINUE ( ! params - > values . has ( group_file_property ) ) ;
String group_file = params - > values [ group_file_property ] ;
config - > set_value ( " remap " , " group_file " , group_file ) ;
} else {
config - > set_value ( " remap " , " group_file " , Variant ( ) ) ; //clear group file if unused
2018-10-30 16:11:54 +00:00
}
2017-02-01 12:45:45 +00:00
2019-04-19 18:54:33 +00:00
} else {
2021-03-22 19:41:47 +00:00
//set to no import
config - > clear ( ) ;
2024-02-05 10:10:37 +00:00
if ( params - > skip ) {
config - > set_value ( " remap " , " importer " , " skip " ) ;
} else {
config - > set_value ( " remap " , " importer " , " keep " ) ;
}
2019-04-19 18:54:33 +00:00
}
2017-02-01 12:45:45 +00:00
config - > save ( params - > paths [ i ] + " .import " ) ;
}
EditorFileSystem : : get_singleton ( ) - > reimport_files ( params - > paths ) ;
2021-07-17 21:22:52 +00:00
EditorFileSystem : : get_singleton ( ) - > emit_signal ( SNAME ( " filesystem_changed " ) ) ; //it changed, so force emitting the signal
2021-10-09 10:29:53 +00:00
_set_dirty ( false ) ;
2017-02-01 12:45:45 +00:00
}
2023-06-30 23:29:46 +00:00
void ImportDock : : _replace_resource_in_object ( Object * p_object , const Ref < Resource > & old_resource , const Ref < Resource > & new_resource ) {
ERR_FAIL_NULL ( p_object ) ;
List < PropertyInfo > props ;
p_object - > get_property_list ( & props ) ;
for ( const PropertyInfo & p : props ) {
if ( p . type ! = Variant : : OBJECT | | p . hint ! = PROPERTY_HINT_RESOURCE_TYPE ) {
continue ;
}
Ref < Resource > res = p_object - > get ( p . name ) ;
if ( res . is_null ( ) ) {
continue ;
}
if ( res = = old_resource ) {
p_object - > set ( p . name , new_resource ) ;
} else {
_replace_resource_in_object ( res . ptr ( ) , old_resource , new_resource ) ;
}
}
Node * n = Object : : cast_to < Node > ( p_object ) ;
if ( n ) {
for ( int i = 0 ; i < n - > get_child_count ( ) ; i + + ) {
_replace_resource_in_object ( n - > get_child ( i ) , old_resource , new_resource ) ;
}
}
}
2017-08-31 14:58:43 +00:00
void ImportDock : : _notification ( int p_what ) {
switch ( p_what ) {
case EditorSettings : : NOTIFICATION_EDITOR_SETTINGS_CHANGED : {
2022-11-22 23:14:08 +00:00
if ( EditorThemeManager : : is_generated_theme_outdated ( ) ) {
imported - > add_theme_style_override ( " normal " , get_theme_stylebox ( SNAME ( " normal " ) , SNAME ( " LineEdit " ) ) ) ;
}
2017-08-31 14:58:43 +00:00
} break ;
2017-11-10 10:16:58 +00:00
case NOTIFICATION_ENTER_TREE : {
import_opts - > edit ( params ) ;
2023-08-13 00:33:39 +00:00
label_warning - > add_theme_color_override ( " font_color " , get_theme_color ( SNAME ( " warning_color " ) , EditorStringName ( Editor ) ) ) ;
2017-11-10 10:16:58 +00:00
} break ;
2017-08-31 14:58:43 +00:00
}
}
2018-10-30 16:11:54 +00:00
2021-10-09 10:29:53 +00:00
void ImportDock : : _property_edited ( const StringName & p_prop ) {
_set_dirty ( true ) ;
}
void ImportDock : : _set_dirty ( bool p_dirty ) {
if ( p_dirty ) {
// Add a dirty marker to notify the user that they should reimport the selected resource to see changes.
import - > set_text ( TTR ( " Reimport " ) + " (*) " ) ;
2023-08-13 00:33:39 +00:00
import - > add_theme_color_override ( " font_color " , get_theme_color ( SNAME ( " warning_color " ) , EditorStringName ( Editor ) ) ) ;
2022-08-25 10:42:17 +00:00
import - > set_tooltip_text ( TTR ( " You have pending changes that haven't been applied yet. Click Reimport to apply changes made to the import options. \n Selecting another resource in the FileSystem dock without clicking Reimport first will discard changes made in the Import dock. " ) ) ;
2021-10-09 10:29:53 +00:00
} else {
// Remove the dirty marker on the Reimport button.
import - > set_text ( TTR ( " Reimport " ) ) ;
2022-02-08 09:14:58 +00:00
import - > remove_theme_color_override ( " font_color " ) ;
2022-08-25 10:42:17 +00:00
import - > set_tooltip_text ( " " ) ;
2021-10-09 10:29:53 +00:00
}
}
2018-10-30 16:11:54 +00:00
void ImportDock : : _property_toggled ( const StringName & p_prop , bool p_checked ) {
if ( p_checked ) {
params - > checked . insert ( p_prop ) ;
} else {
params - > checked . erase ( p_prop ) ;
}
}
2020-05-14 12:29:06 +00:00
2017-02-01 12:45:45 +00:00
void ImportDock : : _bind_methods ( ) {
2017-02-13 11:47:24 +00:00
ClassDB : : bind_method ( D_METHOD ( " _reimport " ) , & ImportDock : : _reimport ) ;
2017-02-01 12:45:45 +00:00
}
2017-07-23 14:44:42 +00:00
void ImportDock : : initialize_import_options ( ) const {
ERR_FAIL_COND ( ! import_opts | | ! params ) ;
import_opts - > edit ( params ) ;
}
2017-02-01 12:45:45 +00:00
ImportDock : : ImportDock ( ) {
2021-11-17 20:08:55 +00:00
singleton = this ;
2017-11-26 01:59:31 +00:00
set_name ( " Import " ) ;
2021-12-25 15:31:36 +00:00
content = memnew ( VBoxContainer ) ;
content - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
add_child ( content ) ;
content - > hide ( ) ;
2017-08-04 18:16:04 +00:00
imported = memnew ( Label ) ;
2023-09-13 11:14:07 +00:00
imported - > add_theme_style_override ( " normal " , EditorNode : : get_singleton ( ) - > get_editor_theme ( ) - > get_stylebox ( SNAME ( " normal " ) , SNAME ( " LineEdit " ) ) ) ;
2018-05-22 07:56:16 +00:00
imported - > set_clip_text ( true ) ;
2021-12-25 15:31:36 +00:00
content - > add_child ( imported ) ;
2017-02-01 12:45:45 +00:00
HBoxContainer * hb = memnew ( HBoxContainer ) ;
2021-12-25 15:31:36 +00:00
content - > add_margin_child ( TTR ( " Import As: " ) , hb ) ;
2017-02-01 12:45:45 +00:00
import_as = memnew ( OptionButton ) ;
2019-09-04 05:20:57 +00:00
import_as - > set_disabled ( true ) ;
2022-08-29 12:28:01 +00:00
import_as - > set_fit_to_longest_item ( false ) ;
import_as - > set_text_overrun_behavior ( TextServer : : OVERRUN_TRIM_ELLIPSIS ) ;
import_as - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
2020-02-21 17:28:45 +00:00
import_as - > connect ( " item_selected " , callable_mp ( this , & ImportDock : : _importer_selected ) ) ;
2017-02-01 12:45:45 +00:00
hb - > add_child ( import_as ) ;
import_as - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
preset = memnew ( MenuButton ) ;
2019-09-04 05:20:57 +00:00
preset - > set_text ( TTR ( " Preset " ) ) ;
preset - > set_disabled ( true ) ;
2020-02-21 17:28:45 +00:00
preset - > get_popup ( ) - > connect ( " index_pressed " , callable_mp ( this , & ImportDock : : _preset_selected ) ) ;
2017-02-01 12:45:45 +00:00
hb - > add_child ( preset ) ;
2018-08-18 13:07:23 +00:00
import_opts = memnew ( EditorInspector ) ;
2021-12-25 15:31:36 +00:00
content - > add_child ( import_opts ) ;
2017-02-01 12:45:45 +00:00
import_opts - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2021-10-09 10:29:53 +00:00
import_opts - > connect ( " property_edited " , callable_mp ( this , & ImportDock : : _property_edited ) ) ;
2020-02-21 17:28:45 +00:00
import_opts - > connect ( " property_toggled " , callable_mp ( this , & ImportDock : : _property_toggled ) ) ;
2021-05-13 20:34:34 +00:00
// Make it possible to display tooltips stored in the XML class reference.
// The object name is set when the importer changes in `_update_options()`.
import_opts - > set_use_doc_hints ( true ) ;
2017-02-01 12:45:45 +00:00
hb = memnew ( HBoxContainer ) ;
2021-12-25 15:31:36 +00:00
content - > add_child ( hb ) ;
2017-02-01 12:45:45 +00:00
import = memnew ( Button ) ;
import - > set_text ( TTR ( " Reimport " ) ) ;
2019-09-04 05:20:57 +00:00
import - > set_disabled ( true ) ;
2024-04-23 20:35:49 +00:00
import - > connect ( " pressed " , callable_mp ( this , & ImportDock : : _reimport_pressed ) ) ;
2021-03-19 12:57:52 +00:00
if ( ! DisplayServer : : get_singleton ( ) - > get_swap_cancel_ok ( ) ) {
advanced_spacer = hb - > add_spacer ( ) ;
advanced = memnew ( Button ) ;
advanced - > set_text ( TTR ( " Advanced... " ) ) ;
hb - > add_child ( advanced ) ;
}
2017-02-01 12:45:45 +00:00
hb - > add_spacer ( ) ;
hb - > add_child ( import ) ;
hb - > add_spacer ( ) ;
2021-03-19 12:57:52 +00:00
if ( DisplayServer : : get_singleton ( ) - > get_swap_cancel_ok ( ) ) {
advanced = memnew ( Button ) ;
advanced - > set_text ( TTR ( " Advanced... " ) ) ;
hb - > add_child ( advanced ) ;
advanced_spacer = hb - > add_spacer ( ) ;
}
advanced - > hide ( ) ;
advanced_spacer - > hide ( ) ;
advanced - > connect ( " pressed " , callable_mp ( this , & ImportDock : : _advanced_options ) ) ;
2019-01-25 20:23:56 +00:00
reimport_confirm = memnew ( ConfirmationDialog ) ;
2021-12-25 15:31:36 +00:00
content - > add_child ( reimport_confirm ) ;
2023-06-30 23:29:46 +00:00
reimport_confirm - > connect ( " confirmed " , callable_mp ( this , & ImportDock : : _reimport_and_cleanup ) ) ;
2019-01-25 20:23:56 +00:00
VBoxContainer * vbc_confirm = memnew ( VBoxContainer ( ) ) ;
2023-06-30 23:29:46 +00:00
cleanup_warning = memnew ( Label ( TTR ( " The imported resource is currently loaded. All instances will be replaced and undo history will be cleared. " ) ) ) ;
vbc_confirm - > add_child ( cleanup_warning ) ;
label_warning = memnew ( Label ( TTR ( " WARNING: Assets exist that use this resource. They may stop loading properly after changing type. " ) ) ) ;
2019-01-25 20:23:56 +00:00
vbc_confirm - > add_child ( label_warning ) ;
reimport_confirm - > add_child ( vbc_confirm ) ;
2017-02-01 12:45:45 +00:00
params = memnew ( ImportDockParameters ) ;
2021-12-25 15:31:36 +00:00
select_a_resource = memnew ( Label ) ;
select_a_resource - > set_text ( TTR ( " Select a resource file in the filesystem or in the inspector to adjust import settings. " ) ) ;
2022-06-15 08:01:45 +00:00
select_a_resource - > set_autowrap_mode ( TextServer : : AUTOWRAP_WORD ) ;
2021-12-25 15:31:36 +00:00
select_a_resource - > set_custom_minimum_size ( Size2 ( 100 * EDSCALE , 0 ) ) ;
select_a_resource - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
select_a_resource - > set_horizontal_alignment ( HORIZONTAL_ALIGNMENT_CENTER ) ;
select_a_resource - > set_vertical_alignment ( VERTICAL_ALIGNMENT_CENTER ) ;
add_child ( select_a_resource ) ;
2017-02-01 12:45:45 +00:00
}
ImportDock : : ~ ImportDock ( ) {
2021-11-17 20:08:55 +00:00
singleton = nullptr ;
2017-02-01 12:45:45 +00:00
memdelete ( params ) ;
}