2023-01-05 12:25:55 +00:00
/**************************************************************************/
/* dependency_editor.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
2015-08-23 23:15:56 +00:00
# include "dependency_editor.h"
2017-01-16 07:04:19 +00:00
2022-02-12 01:46:22 +00:00
# include "core/config/project_settings.h"
2021-06-11 12:51:48 +00:00
# include "core/io/file_access.h"
2018-09-11 16:13:45 +00:00
# include "core/io/resource_loader.h"
2022-02-12 01:46:22 +00:00
# include "editor/editor_file_system.h"
# include "editor/editor_node.h"
2022-07-31 18:14:15 +00:00
# include "editor/editor_settings.h"
2023-04-07 16:59:49 +00:00
# include "editor/gui/editor_file_dialog.h"
2024-01-15 12:14:55 +00:00
# include "editor/themes/editor_scale.h"
2015-08-23 23:15:56 +00:00
# include "scene/gui/margin_container.h"
2017-03-05 15:44:50 +00:00
void DependencyEditor : : _searched ( const String & p_path ) {
2022-05-13 13:04:37 +00:00
HashMap < String , String > dep_rename ;
2017-03-05 15:44:50 +00:00
dep_rename [ replacing ] = p_path ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
ResourceLoader : : rename_dependencies ( editing , dep_rename ) ;
2015-08-23 23:15:56 +00:00
_update_list ( ) ;
_update_file ( ) ;
}
2021-09-18 07:33:18 +00:00
void DependencyEditor : : _load_pressed ( Object * p_item , int p_cell , int p_button , MouseButton p_mouse_button ) {
if ( p_mouse_button ! = MouseButton : : LEFT ) {
return ;
}
2017-08-24 20:58:51 +00:00
TreeItem * ti = Object : : cast_to < TreeItem > ( p_item ) ;
2015-08-23 23:15:56 +00:00
replacing = ti - > get_text ( 1 ) ;
2017-03-05 15:44:50 +00:00
search - > set_title ( TTR ( " Search Replacement For: " ) + " " + replacing . get_file ( ) ) ;
2015-08-23 23:15:56 +00:00
2022-07-23 20:29:50 +00:00
// Set directory to closest existing directory.
search - > set_current_dir ( replacing . get_base_dir ( ) ) ;
2015-08-23 23:15:56 +00:00
search - > clear_filters ( ) ;
List < String > ext ;
2017-03-05 15:44:50 +00:00
ResourceLoader : : get_recognized_extensions_for_type ( ti - > get_metadata ( 0 ) , & ext ) ;
2021-07-24 13:46:25 +00:00
for ( const String & E : ext ) {
2024-03-26 15:51:30 +00:00
search - > add_filter ( " *. " + E ) ;
2015-08-23 23:15:56 +00:00
}
2020-07-11 16:45:19 +00:00
search - > popup_file_dialog ( ) ;
2015-08-23 23:15:56 +00:00
}
2022-05-13 13:04:37 +00:00
void DependencyEditor : : _fix_and_find ( EditorFileSystemDirectory * efsd , HashMap < String , HashMap < String , String > > & candidates ) {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_subdir_count ( ) ; i + + ) {
_fix_and_find ( efsd - > get_subdir ( i ) , candidates ) ;
2015-08-23 23:15:56 +00:00
}
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_file_count ( ) ; i + + ) {
2015-08-23 23:15:56 +00:00
String file = efsd - > get_file ( i ) ;
2020-05-14 14:41:43 +00:00
if ( ! candidates . has ( file ) ) {
2015-08-23 23:15:56 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
String path = efsd - > get_file_path ( i ) ;
2021-08-09 20:13:42 +00:00
for ( KeyValue < String , String > & E : candidates [ file ] ) {
2021-12-09 09:42:46 +00:00
if ( E . value . is_empty ( ) ) {
2021-08-09 20:13:42 +00:00
E . value = path ;
2015-08-23 23:15:56 +00:00
continue ;
}
//must match the best, using subdirs
2021-08-09 20:13:42 +00:00
String existing = E . value . replace_first ( " res:// " , " " ) ;
2017-03-05 15:44:50 +00:00
String current = path . replace_first ( " res:// " , " " ) ;
2021-08-09 20:13:42 +00:00
String lost = E . key . replace_first ( " res:// " , " " ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
Vector < String > existingv = existing . split ( " / " ) ;
2021-03-14 07:21:32 +00:00
existingv . reverse ( ) ;
2017-03-05 15:44:50 +00:00
Vector < String > currentv = current . split ( " / " ) ;
2021-03-14 07:21:32 +00:00
currentv . reverse ( ) ;
2017-03-05 15:44:50 +00:00
Vector < String > lostv = lost . split ( " / " ) ;
2021-03-14 07:21:32 +00:00
lostv . reverse ( ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
int existing_score = 0 ;
int current_score = 0 ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < lostv . size ( ) ; j + + ) {
if ( j < existingv . size ( ) & & lostv [ j ] = = existingv [ j ] ) {
2015-08-23 23:15:56 +00:00
existing_score + + ;
}
2017-03-05 15:44:50 +00:00
if ( j < currentv . size ( ) & & lostv [ j ] = = currentv [ j ] ) {
2015-08-23 23:15:56 +00:00
current_score + + ;
}
}
if ( current_score > existing_score ) {
//if it was the same, could track distance to new path but..
2021-08-09 20:13:42 +00:00
E . value = path ; //replace by more accurate
2015-08-23 23:15:56 +00:00
}
}
}
}
2017-03-05 15:44:50 +00:00
void DependencyEditor : : _fix_all ( ) {
2020-05-14 14:41:43 +00:00
if ( ! EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) ) {
2015-08-23 23:15:56 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
2022-05-13 13:04:37 +00:00
HashMap < String , HashMap < String , String > > candidates ;
2015-08-23 23:15:56 +00:00
2021-07-24 13:46:25 +00:00
for ( const String & E : missing ) {
2021-07-16 03:45:57 +00:00
String base = E . get_file ( ) ;
2015-08-23 23:15:56 +00:00
if ( ! candidates . has ( base ) ) {
2022-05-13 13:04:37 +00:00
candidates [ base ] = HashMap < String , String > ( ) ;
2015-08-23 23:15:56 +00:00
}
2021-07-16 03:45:57 +00:00
candidates [ base ] [ E ] = " " ;
2015-08-23 23:15:56 +00:00
}
2017-03-05 15:44:50 +00:00
_fix_and_find ( EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) , candidates ) ;
2015-08-23 23:15:56 +00:00
2022-05-13 13:04:37 +00:00
HashMap < String , String > remaps ;
2015-08-23 23:15:56 +00:00
2022-05-13 13:04:37 +00:00
for ( KeyValue < String , HashMap < String , String > > & E : candidates ) {
2021-08-09 20:13:42 +00:00
for ( const KeyValue < String , String > & F : E . value ) {
2021-12-09 09:42:46 +00:00
if ( ! F . value . is_empty ( ) ) {
2021-08-09 20:13:42 +00:00
remaps [ F . key ] = F . value ;
2015-08-23 23:15:56 +00:00
}
}
}
if ( remaps . size ( ) ) {
2017-03-05 15:44:50 +00:00
ResourceLoader : : rename_dependencies ( editing , remaps ) ;
2015-08-23 23:15:56 +00:00
_update_list ( ) ;
_update_file ( ) ;
}
}
void DependencyEditor : : _update_file ( ) {
EditorFileSystem : : get_singleton ( ) - > update_file ( editing ) ;
}
void DependencyEditor : : _update_list ( ) {
List < String > deps ;
2017-03-05 15:44:50 +00:00
ResourceLoader : : get_dependencies ( editing , & deps , true ) ;
2015-08-23 23:15:56 +00:00
tree - > clear ( ) ;
missing . clear ( ) ;
TreeItem * root = tree - > create_item ( ) ;
2021-07-17 21:22:52 +00:00
Ref < Texture2D > folder = tree - > get_theme_icon ( SNAME ( " folder " ) , SNAME ( " FileDialog " ) ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
bool broken = false ;
2015-08-23 23:15:56 +00:00
2021-07-24 13:46:25 +00:00
for ( const String & n : deps ) {
2015-08-23 23:15:56 +00:00
TreeItem * item = tree - > create_item ( root ) ;
String path ;
String type ;
2022-02-03 16:03:38 +00:00
if ( n . contains ( " :: " ) ) {
2017-03-05 15:44:50 +00:00
path = n . get_slice ( " :: " , 0 ) ;
type = n . get_slice ( " :: " , 1 ) ;
2015-08-23 23:15:56 +00:00
} else {
2017-03-05 15:44:50 +00:00
path = n ;
type = " Resource " ;
2015-08-23 23:15:56 +00:00
}
2021-07-23 19:01:18 +00:00
ResourceUID : : ID uid = ResourceUID : : get_singleton ( ) - > text_to_id ( path ) ;
if ( uid ! = ResourceUID : : INVALID_ID ) {
2023-02-11 22:47:43 +00:00
// Dependency is in uid format, obtain proper path.
if ( ResourceUID : : get_singleton ( ) - > has_id ( uid ) ) {
path = ResourceUID : : get_singleton ( ) - > get_id_path ( uid ) ;
} else if ( n . get_slice_count ( " :: " ) > = 3 ) {
// If uid can't be found, try to use fallback path.
path = n . get_slice ( " :: " , 2 ) ;
} else {
ERR_PRINT ( " Invalid dependency UID and fallback path. " ) ;
continue ;
}
2021-07-23 19:01:18 +00:00
}
2015-08-23 23:15:56 +00:00
String name = path . get_file ( ) ;
2019-06-11 18:43:37 +00:00
Ref < Texture2D > icon = EditorNode : : get_singleton ( ) - > get_class_icon ( type ) ;
2017-03-05 15:44:50 +00:00
item - > set_text ( 0 , name ) ;
item - > set_icon ( 0 , icon ) ;
item - > set_metadata ( 0 , type ) ;
item - > set_text ( 1 , path ) ;
2015-08-23 23:15:56 +00:00
if ( ! FileAccess : : exists ( path ) ) {
2017-03-05 15:44:50 +00:00
item - > set_custom_color ( 1 , Color ( 1 , 0.4 , 0.3 ) ) ;
2015-08-23 23:15:56 +00:00
missing . push_back ( path ) ;
2017-03-05 15:44:50 +00:00
broken = true ;
2015-08-23 23:15:56 +00:00
}
2017-03-05 15:44:50 +00:00
item - > add_button ( 1 , folder , 0 ) ;
2015-08-23 23:15:56 +00:00
}
fixdeps - > set_disabled ( ! broken ) ;
}
2017-03-05 15:44:50 +00:00
void DependencyEditor : : edit ( const String & p_path ) {
editing = p_path ;
set_title ( TTR ( " Dependencies For: " ) + " " + p_path . get_file ( ) ) ;
2015-08-23 23:15:56 +00:00
_update_list ( ) ;
2020-07-11 16:45:19 +00:00
popup_centered_ratio ( 0.4 ) ;
2015-08-23 23:15:56 +00:00
if ( EditorNode : : get_singleton ( ) - > is_scene_open ( p_path ) ) {
2019-03-25 00:54:29 +00:00
EditorNode : : get_singleton ( ) - > show_warning ( vformat ( TTR ( " Scene '%s' is currently being edited. \n Changes will only take effect when reloaded. " ) , p_path . get_file ( ) ) ) ;
2015-08-23 23:15:56 +00:00
} else if ( ResourceCache : : has ( p_path ) ) {
2019-03-25 00:54:29 +00:00
EditorNode : : get_singleton ( ) - > show_warning ( vformat ( TTR ( " Resource '%s' is in use. \n Changes will only take effect when reloaded. " ) , p_path . get_file ( ) ) ) ;
2015-08-23 23:15:56 +00:00
}
}
void DependencyEditor : : _bind_methods ( ) {
}
DependencyEditor : : DependencyEditor ( ) {
2017-03-05 15:44:50 +00:00
VBoxContainer * vb = memnew ( VBoxContainer ) ;
2016-05-04 01:25:37 +00:00
vb - > set_name ( TTR ( " Dependencies " ) ) ;
2015-08-23 23:15:56 +00:00
add_child ( vb ) ;
2017-01-10 04:49:55 +00:00
2017-03-05 15:44:50 +00:00
tree = memnew ( Tree ) ;
2024-03-17 08:28:18 +00:00
tree - > set_auto_translate_mode ( AUTO_TRANSLATE_MODE_DISABLED ) ;
2015-08-23 23:15:56 +00:00
tree - > set_columns ( 2 ) ;
tree - > set_column_titles_visible ( true ) ;
2017-03-05 15:44:50 +00:00
tree - > set_column_title ( 0 , TTR ( " Resource " ) ) ;
2021-07-04 03:13:28 +00:00
tree - > set_column_clip_content ( 0 , true ) ;
tree - > set_column_expand_ratio ( 0 , 2 ) ;
2017-03-05 15:44:50 +00:00
tree - > set_column_title ( 1 , TTR ( " Path " ) ) ;
2021-07-04 03:13:28 +00:00
tree - > set_column_clip_content ( 1 , true ) ;
tree - > set_column_expand_ratio ( 1 , 1 ) ;
2015-08-23 23:15:56 +00:00
tree - > set_hide_root ( true ) ;
2021-09-18 07:33:18 +00:00
tree - > connect ( " button_clicked " , callable_mp ( this , & DependencyEditor : : _load_pressed ) ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
HBoxContainer * hbc = memnew ( HBoxContainer ) ;
Label * label = memnew ( Label ( TTR ( " Dependencies: " ) ) ) ;
2021-07-08 13:29:15 +00:00
label - > set_theme_type_variation ( " HeaderSmall " ) ;
2015-08-23 23:15:56 +00:00
hbc - > add_child ( label ) ;
hbc - > add_spacer ( ) ;
2017-03-05 15:44:50 +00:00
fixdeps = memnew ( Button ( TTR ( " Fix Broken " ) ) ) ;
2015-08-23 23:15:56 +00:00
hbc - > add_child ( fixdeps ) ;
2020-02-21 17:28:45 +00:00
fixdeps - > connect ( " pressed " , callable_mp ( this , & DependencyEditor : : _fix_all ) ) ;
2015-08-23 23:15:56 +00:00
vb - > add_child ( hbc ) ;
2017-03-05 15:44:50 +00:00
MarginContainer * mc = memnew ( MarginContainer ) ;
2020-03-06 17:00:16 +00:00
mc - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
2015-08-23 23:15:56 +00:00
mc - > add_child ( tree ) ;
vb - > add_child ( mc ) ;
2016-05-04 01:25:37 +00:00
set_title ( TTR ( " Dependency Editor " ) ) ;
2017-03-05 15:44:50 +00:00
search = memnew ( EditorFileDialog ) ;
2020-02-21 17:28:45 +00:00
search - > connect ( " file_selected " , callable_mp ( this , & DependencyEditor : : _searched ) ) ;
2020-03-06 17:00:16 +00:00
search - > set_file_mode ( EditorFileDialog : : FILE_MODE_OPEN_FILE ) ;
2016-05-04 01:25:37 +00:00
search - > set_title ( TTR ( " Search Replacement Resource: " ) ) ;
2015-08-23 23:15:56 +00:00
add_child ( search ) ;
}
/////////////////////////////////////
2021-11-25 23:53:27 +00:00
void DependencyEditorOwners : : _list_rmb_clicked ( int p_item , const Vector2 & p_pos , MouseButton p_mouse_button_index ) {
if ( p_mouse_button_index ! = MouseButton : : RIGHT ) {
return ;
}
2017-11-15 04:05:49 +00:00
file_options - > clear ( ) ;
2021-11-20 08:04:57 +00:00
file_options - > reset_size ( ) ;
2017-11-15 04:05:49 +00:00
if ( p_item > = 0 ) {
2022-11-15 20:53:04 +00:00
PackedInt32Array selected_items = owners - > get_selected_items ( ) ;
bool only_scenes_selected = true ;
for ( int i = 0 ; i < selected_items . size ( ) ; i + + ) {
int item_idx = selected_items [ i ] ;
if ( ResourceLoader : : get_resource_type ( owners - > get_item_text ( item_idx ) ) ! = " PackedScene " ) {
only_scenes_selected = false ;
break ;
}
}
if ( only_scenes_selected ) {
2023-08-13 00:33:39 +00:00
file_options - > add_icon_item ( get_editor_theme_icon ( SNAME ( " Load " ) ) , TTRN ( " Open Scene " , " Open Scenes " , selected_items . size ( ) ) , FILE_OPEN ) ;
2022-11-15 20:53:04 +00:00
} else if ( selected_items . size ( ) = = 1 ) {
2023-08-13 00:33:39 +00:00
file_options - > add_icon_item ( get_editor_theme_icon ( SNAME ( " Load " ) ) , TTR ( " Open " ) , FILE_OPEN ) ;
2022-09-25 20:20:16 +00:00
} else {
2022-11-15 20:53:04 +00:00
return ;
2022-09-25 20:20:16 +00:00
}
2017-11-15 04:05:49 +00:00
}
2021-08-31 15:43:35 +00:00
file_options - > set_position ( owners - > get_screen_position ( ) + p_pos ) ;
file_options - > reset_size ( ) ;
2017-11-15 04:05:49 +00:00
file_options - > popup ( ) ;
}
void DependencyEditorOwners : : _select_file ( int p_idx ) {
String fpath = owners - > get_item_text ( p_idx ) ;
if ( ResourceLoader : : get_resource_type ( fpath ) = = " PackedScene " ) {
2022-01-27 09:36:51 +00:00
EditorNode : : get_singleton ( ) - > open_request ( fpath ) ;
2022-11-15 20:53:04 +00:00
} else {
EditorNode : : get_singleton ( ) - > load_resource ( fpath ) ;
2017-11-15 04:05:49 +00:00
}
2022-11-15 20:53:04 +00:00
hide ( ) ;
emit_signal ( SNAME ( " confirmed " ) ) ;
}
void DependencyEditorOwners : : _empty_clicked ( const Vector2 & p_pos , MouseButton p_mouse_button_index ) {
if ( p_mouse_button_index ! = MouseButton : : LEFT ) {
return ;
}
owners - > deselect_all ( ) ;
2017-11-15 04:05:49 +00:00
}
void DependencyEditorOwners : : _file_option ( int p_option ) {
switch ( p_option ) {
case FILE_OPEN : {
2022-09-25 20:20:16 +00:00
PackedInt32Array selected_items = owners - > get_selected_items ( ) ;
for ( int i = 0 ; i < selected_items . size ( ) ; i + + ) {
int item_idx = selected_items [ i ] ;
if ( item_idx < 0 | | item_idx > = owners - > get_item_count ( ) ) {
break ;
}
_select_file ( item_idx ) ;
2020-05-14 14:41:43 +00:00
}
2017-11-15 04:05:49 +00:00
} break ;
}
}
void DependencyEditorOwners : : _bind_methods ( ) {
}
2015-08-23 23:15:56 +00:00
void DependencyEditorOwners : : _fill_owners ( EditorFileSystemDirectory * efsd ) {
2020-05-14 14:41:43 +00:00
if ( ! efsd ) {
2015-08-23 23:15:56 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_subdir_count ( ) ; i + + ) {
2015-08-23 23:15:56 +00:00
_fill_owners ( efsd - > get_subdir ( i ) ) ;
}
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_file_count ( ) ; i + + ) {
2015-08-23 23:15:56 +00:00
Vector < String > deps = efsd - > get_file_deps ( i ) ;
2017-03-05 15:44:50 +00:00
bool found = false ;
for ( int j = 0 ; j < deps . size ( ) ; j + + ) {
if ( deps [ j ] = = editing ) {
found = true ;
2015-08-23 23:15:56 +00:00
break ;
}
}
2020-05-14 14:41:43 +00:00
if ( ! found ) {
2015-08-23 23:15:56 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
2019-06-11 18:43:37 +00:00
Ref < Texture2D > icon = EditorNode : : get_singleton ( ) - > get_class_icon ( efsd - > get_file_type ( i ) ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
owners - > add_item ( efsd - > get_file_path ( i ) , icon ) ;
2015-08-23 23:15:56 +00:00
}
}
2017-03-05 15:44:50 +00:00
void DependencyEditorOwners : : show ( const String & p_path ) {
editing = p_path ;
2015-08-23 23:15:56 +00:00
owners - > clear ( ) ;
_fill_owners ( EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) ) ;
2020-07-11 16:45:19 +00:00
popup_centered_ratio ( 0.3 ) ;
2015-08-23 23:15:56 +00:00
2022-06-02 23:56:21 +00:00
set_title ( vformat ( TTR ( " Owners of: %s (Total: %d) " ) , p_path . get_file ( ) , owners - > get_item_count ( ) ) ) ;
2015-08-23 23:15:56 +00:00
}
2022-01-27 09:36:51 +00:00
DependencyEditorOwners : : DependencyEditorOwners ( ) {
2017-11-15 04:05:49 +00:00
file_options = memnew ( PopupMenu ) ;
add_child ( file_options ) ;
2020-02-21 17:28:45 +00:00
file_options - > connect ( " id_pressed " , callable_mp ( this , & DependencyEditorOwners : : _file_option ) ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
owners = memnew ( ItemList ) ;
2024-01-23 21:29:45 +00:00
owners - > set_auto_translate_mode ( AUTO_TRANSLATE_MODE_DISABLED ) ;
2022-09-25 20:20:16 +00:00
owners - > set_select_mode ( ItemList : : SELECT_MULTI ) ;
2021-11-25 23:53:27 +00:00
owners - > connect ( " item_clicked " , callable_mp ( this , & DependencyEditorOwners : : _list_rmb_clicked ) ) ;
2020-02-21 17:28:45 +00:00
owners - > connect ( " item_activated " , callable_mp ( this , & DependencyEditorOwners : : _select_file ) ) ;
2022-11-15 20:53:04 +00:00
owners - > connect ( " empty_clicked " , callable_mp ( this , & DependencyEditorOwners : : _empty_clicked ) ) ;
2017-11-15 04:05:49 +00:00
owners - > set_allow_rmb_select ( true ) ;
2015-08-23 23:15:56 +00:00
add_child ( owners ) ;
}
///////////////////////
2017-10-01 22:24:49 +00:00
void DependencyRemoveDialog : : _find_files_in_removed_folder ( EditorFileSystemDirectory * efsd , const String & p_folder ) {
2020-05-14 14:41:43 +00:00
if ( ! efsd ) {
2017-10-01 22:24:49 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2017-10-01 22:24:49 +00:00
for ( int i = 0 ; i < efsd - > get_subdir_count ( ) ; + + i ) {
_find_files_in_removed_folder ( efsd - > get_subdir ( i ) , p_folder ) ;
}
for ( int i = 0 ; i < efsd - > get_file_count ( ) ; i + + ) {
String file = efsd - > get_file_path ( i ) ;
ERR_FAIL_COND ( all_remove_files . has ( file ) ) ; //We are deleting a directory which is contained in a directory we are deleting...
all_remove_files [ file ] = p_folder ; //Point the file to the ancestor directory we are deleting so we know what to parent it under in the tree.
}
}
2015-08-23 23:15:56 +00:00
2017-10-01 22:24:49 +00:00
void DependencyRemoveDialog : : _find_all_removed_dependencies ( EditorFileSystemDirectory * efsd , Vector < RemovedDependency > & p_removed ) {
2020-05-14 14:41:43 +00:00
if ( ! efsd ) {
2015-08-23 23:15:56 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_subdir_count ( ) ; i + + ) {
2017-10-01 22:24:49 +00:00
_find_all_removed_dependencies ( efsd - > get_subdir ( i ) , p_removed ) ;
2015-08-23 23:15:56 +00:00
}
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_file_count ( ) ; i + + ) {
2017-10-01 22:24:49 +00:00
const String path = efsd - > get_file_path ( i ) ;
2015-08-23 23:15:56 +00:00
2017-10-01 22:24:49 +00:00
//It doesn't matter if a file we are about to delete will have some of its dependencies removed too
2020-05-14 14:41:43 +00:00
if ( all_remove_files . has ( path ) ) {
2015-08-23 23:15:56 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
2017-10-01 22:24:49 +00:00
Vector < String > all_deps = efsd - > get_file_deps ( i ) ;
for ( int j = 0 ; j < all_deps . size ( ) ; + + j ) {
if ( all_remove_files . has ( all_deps [ j ] ) ) {
RemovedDependency dep ;
dep . file = path ;
dep . file_type = efsd - > get_file_type ( i ) ;
dep . dependency = all_deps [ j ] ;
dep . dependency_folder = all_remove_files [ all_deps [ j ] ] ;
p_removed . push_back ( dep ) ;
}
2015-08-23 23:15:56 +00:00
}
2017-10-01 22:24:49 +00:00
}
}
2015-08-23 23:15:56 +00:00
2022-01-04 17:52:32 +00:00
void DependencyRemoveDialog : : _find_localization_remaps_of_removed_files ( Vector < RemovedDependency > & p_removed ) {
for ( KeyValue < String , String > & files : all_remove_files ) {
const String & path = files . key ;
// Look for dependencies in the translation remaps.
if ( ProjectSettings : : get_singleton ( ) - > has_setting ( " internationalization/locale/translation_remaps " ) ) {
2022-10-18 14:43:37 +00:00
Dictionary remaps = GLOBAL_GET ( " internationalization/locale/translation_remaps " ) ;
2022-01-04 17:52:32 +00:00
if ( remaps . has ( path ) ) {
RemovedDependency dep ;
dep . file = TTR ( " Localization remap " ) ;
dep . file_type = " " ;
dep . dependency = path ;
dep . dependency_folder = files . value ;
p_removed . push_back ( dep ) ;
}
Array remap_keys = remaps . keys ( ) ;
for ( int j = 0 ; j < remap_keys . size ( ) ; j + + ) {
PackedStringArray remapped_files = remaps [ remap_keys [ j ] ] ;
for ( int k = 0 ; k < remapped_files . size ( ) ; k + + ) {
int splitter_pos = remapped_files [ k ] . rfind ( " : " ) ;
String res_path = remapped_files [ k ] . substr ( 0 , splitter_pos ) ;
if ( res_path = = path ) {
String locale_name = remapped_files [ k ] . substr ( splitter_pos + 1 ) ;
RemovedDependency dep ;
dep . file = vformat ( TTR ( " Localization remap for path '%s' and locale '%s'. " ) , remap_keys [ j ] , locale_name ) ;
dep . file_type = " " ;
dep . dependency = path ;
dep . dependency_folder = files . value ;
p_removed . push_back ( dep ) ;
}
}
}
}
}
}
2017-10-01 22:24:49 +00:00
void DependencyRemoveDialog : : _build_removed_dependency_tree ( const Vector < RemovedDependency > & p_removed ) {
owners - > clear ( ) ;
owners - > create_item ( ) ; // root
2015-08-23 23:15:56 +00:00
2022-05-13 13:04:37 +00:00
HashMap < String , TreeItem * > tree_items ;
2017-10-01 22:24:49 +00:00
for ( int i = 0 ; i < p_removed . size ( ) ; i + + ) {
RemovedDependency rd = p_removed [ i ] ;
//Ensure that the dependency is already in the tree
if ( ! tree_items . has ( rd . dependency ) ) {
if ( rd . dependency_folder . length ( ) > 0 ) {
//Ensure the ancestor folder is already in the tree
if ( ! tree_items . has ( rd . dependency_folder ) ) {
TreeItem * folder_item = owners - > create_item ( owners - > get_root ( ) ) ;
folder_item - > set_text ( 0 , rd . dependency_folder ) ;
2023-08-13 00:33:39 +00:00
folder_item - > set_icon ( 0 , owners - > get_editor_theme_icon ( SNAME ( " Folder " ) ) ) ;
2017-10-01 22:24:49 +00:00
tree_items [ rd . dependency_folder ] = folder_item ;
}
TreeItem * dependency_item = owners - > create_item ( tree_items [ rd . dependency_folder ] ) ;
dependency_item - > set_text ( 0 , rd . dependency ) ;
2023-08-13 00:33:39 +00:00
dependency_item - > set_icon ( 0 , owners - > get_editor_theme_icon ( SNAME ( " Warning " ) ) ) ;
2017-10-01 22:24:49 +00:00
tree_items [ rd . dependency ] = dependency_item ;
} else {
TreeItem * dependency_item = owners - > create_item ( owners - > get_root ( ) ) ;
dependency_item - > set_text ( 0 , rd . dependency ) ;
2023-08-13 00:33:39 +00:00
dependency_item - > set_icon ( 0 , owners - > get_editor_theme_icon ( SNAME ( " Warning " ) ) ) ;
2017-10-01 22:24:49 +00:00
tree_items [ rd . dependency ] = dependency_item ;
2015-08-23 23:15:56 +00:00
}
}
2017-10-01 22:24:49 +00:00
//List this file under this dependency
2019-06-11 18:43:37 +00:00
Ref < Texture2D > icon = EditorNode : : get_singleton ( ) - > get_class_icon ( rd . file_type ) ;
2017-10-01 22:24:49 +00:00
TreeItem * file_item = owners - > create_item ( tree_items [ rd . dependency ] ) ;
file_item - > set_text ( 0 , rd . file ) ;
file_item - > set_icon ( 0 , icon ) ;
2015-08-23 23:15:56 +00:00
}
}
2017-10-01 22:24:49 +00:00
void DependencyRemoveDialog : : show ( const Vector < String > & p_folders , const Vector < String > & p_files ) {
all_remove_files . clear ( ) ;
2018-02-26 07:59:49 +00:00
dirs_to_delete . clear ( ) ;
files_to_delete . clear ( ) ;
2015-08-23 23:15:56 +00:00
owners - > clear ( ) ;
2017-10-01 22:24:49 +00:00
for ( int i = 0 ; i < p_folders . size ( ) ; + + i ) {
String folder = p_folders [ i ] . ends_with ( " / " ) ? p_folders [ i ] : ( p_folders [ i ] + " / " ) ;
_find_files_in_removed_folder ( EditorFileSystem : : get_singleton ( ) - > get_filesystem_path ( folder ) , folder ) ;
2018-02-26 07:59:49 +00:00
dirs_to_delete . push_back ( folder ) ;
2017-10-01 22:24:49 +00:00
}
for ( int i = 0 ; i < p_files . size ( ) ; + + i ) {
all_remove_files [ p_files [ i ] ] = String ( ) ;
2018-02-26 07:59:49 +00:00
files_to_delete . push_back ( p_files [ i ] ) ;
2015-08-23 23:15:56 +00:00
}
2017-10-01 22:24:49 +00:00
Vector < RemovedDependency > removed_deps ;
_find_all_removed_dependencies ( EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) , removed_deps ) ;
2022-01-04 17:52:32 +00:00
_find_localization_remaps_of_removed_files ( removed_deps ) ;
2017-10-01 22:24:49 +00:00
removed_deps . sort ( ) ;
2020-12-15 12:04:21 +00:00
if ( removed_deps . is_empty ( ) ) {
2015-08-23 23:15:56 +00:00
owners - > hide ( ) ;
2021-08-02 10:03:18 +00:00
text - > set_text ( TTR ( " Remove the selected files from the project? (Cannot be undone.) \n Depending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently. " ) ) ;
2021-11-20 08:04:57 +00:00
reset_size ( ) ;
2019-06-15 07:03:28 +00:00
popup_centered ( ) ;
2017-10-01 22:24:49 +00:00
} else {
_build_removed_dependency_tree ( removed_deps ) ;
owners - > show ( ) ;
2021-08-02 10:03:18 +00:00
text - > set_text ( TTR ( " The files being removed are required by other resources in order for them to work. \n Remove them anyway? (Cannot be undone.) \n Depending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently. " ) ) ;
2019-06-15 07:03:28 +00:00
popup_centered ( Size2 ( 500 , 350 ) ) ;
2015-08-23 23:15:56 +00:00
}
2019-02-09 13:36:16 +00:00
EditorFileSystem : : get_singleton ( ) - > scan_changes ( ) ;
2015-08-23 23:15:56 +00:00
}
void DependencyRemoveDialog : : ok_pressed ( ) {
2023-01-23 09:01:28 +00:00
for ( const KeyValue < String , String > & E : all_remove_files ) {
String file = E . key ;
if ( ResourceCache : : has ( file ) ) {
Ref < Resource > res = ResourceCache : : get_ref ( file ) ;
emit_signal ( SNAME ( " resource_removed " ) , res ) ;
2018-05-11 19:37:08 +00:00
res - > set_path ( " " ) ;
}
2023-01-23 09:01:28 +00:00
}
2018-10-31 02:54:20 +00:00
2023-01-23 09:01:28 +00:00
for ( int i = 0 ; i < files_to_delete . size ( ) ; + + i ) {
2019-04-05 15:19:25 +00:00
// If the file we are deleting for e.g. the main scene, default environment,
// or audio bus layout, we must clear its definition in Project Settings.
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " application/config/icon " ) ) ) {
2019-03-16 21:05:24 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " application/config/icon " , " " ) ;
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " application/run/main_scene " ) ) ) {
2018-10-31 02:54:20 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " application/run/main_scene " , " " ) ;
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " application/boot_splash/image " ) ) ) {
2019-03-16 21:05:24 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " application/boot_splash/image " , " " ) ;
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " rendering/environment/defaults/default_environment " ) ) ) {
2021-02-17 16:44:49 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " rendering/environment/defaults/default_environment " , " " ) ;
2019-02-20 09:40:41 +00:00
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " display/mouse_cursor/custom_image " ) ) ) {
2019-03-16 21:05:24 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " display/mouse_cursor/custom_image " , " " ) ;
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " gui/theme/custom " ) ) ) {
2019-03-16 21:05:24 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " gui/theme/custom " , " " ) ;
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " gui/theme/custom_font " ) ) ) {
2019-03-16 21:05:24 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " gui/theme/custom_font " , " " ) ;
}
2022-10-18 14:43:37 +00:00
if ( files_to_delete [ i ] = = String ( GLOBAL_GET ( " audio/buses/default_bus_layout " ) ) ) {
2021-02-17 16:44:49 +00:00
ProjectSettings : : get_singleton ( ) - > set ( " audio/buses/default_bus_layout " , " " ) ;
2019-04-05 15:19:25 +00:00
}
2019-02-20 09:40:41 +00:00
2018-05-11 19:37:08 +00:00
String path = OS : : get_singleton ( ) - > get_resource_dir ( ) + files_to_delete [ i ] . replace_first ( " res:// " , " / " ) ;
2018-08-24 07:35:07 +00:00
print_verbose ( " Moving to trash: " + path ) ;
2018-05-11 19:37:08 +00:00
Error err = OS : : get_singleton ( ) - > move_to_trash ( path ) ;
if ( err ! = OK ) {
EditorNode : : get_singleton ( ) - > add_io_error ( TTR ( " Cannot remove: " ) + " \n " + files_to_delete [ i ] + " \n " ) ;
2018-11-13 19:17:33 +00:00
} else {
2021-07-17 21:22:52 +00:00
emit_signal ( SNAME ( " file_removed " ) , files_to_delete [ i ] ) ;
2018-05-11 19:37:08 +00:00
}
}
2018-02-26 07:59:49 +00:00
if ( dirs_to_delete . size ( ) = = 0 ) {
2018-09-18 12:02:59 +00:00
// If we only deleted files we should only need to tell the file system about the files we touched.
2020-05-14 14:41:43 +00:00
for ( int i = 0 ; i < files_to_delete . size ( ) ; + + i ) {
2018-02-26 07:59:49 +00:00
EditorFileSystem : : get_singleton ( ) - > update_file ( files_to_delete [ i ] ) ;
2020-05-14 14:41:43 +00:00
}
2018-02-26 07:59:49 +00:00
} else {
for ( int i = 0 ; i < dirs_to_delete . size ( ) ; + + i ) {
String path = OS : : get_singleton ( ) - > get_resource_dir ( ) + dirs_to_delete [ i ] . replace_first ( " res:// " , " / " ) ;
2018-08-24 07:35:07 +00:00
print_verbose ( " Moving to trash: " + path ) ;
2018-02-26 07:59:49 +00:00
Error err = OS : : get_singleton ( ) - > move_to_trash ( path ) ;
if ( err ! = OK ) {
EditorNode : : get_singleton ( ) - > add_io_error ( TTR ( " Cannot remove: " ) + " \n " + dirs_to_delete [ i ] + " \n " ) ;
2018-11-13 19:17:33 +00:00
} else {
2021-07-17 21:22:52 +00:00
emit_signal ( SNAME ( " folder_removed " ) , dirs_to_delete [ i ] ) ;
2018-02-26 07:59:49 +00:00
}
2017-10-01 22:24:49 +00:00
}
2017-09-25 13:15:11 +00:00
2018-09-18 12:02:59 +00:00
EditorFileSystem : : get_singleton ( ) - > scan_changes ( ) ;
}
2018-02-26 07:59:49 +00:00
2018-09-18 12:02:59 +00:00
// If some files/dirs would be deleted, favorite dirs need to be updated
Vector < String > previous_favorites = EditorSettings : : get_singleton ( ) - > get_favorites ( ) ;
Vector < String > new_favorites ;
2018-02-26 07:59:49 +00:00
2018-09-18 12:02:59 +00:00
for ( int i = 0 ; i < previous_favorites . size ( ) ; + + i ) {
if ( previous_favorites [ i ] . ends_with ( " / " ) ) {
2020-05-14 14:41:43 +00:00
if ( dirs_to_delete . find ( previous_favorites [ i ] ) < 0 ) {
2018-09-18 12:02:59 +00:00
new_favorites . push_back ( previous_favorites [ i ] ) ;
2020-05-14 14:41:43 +00:00
}
2018-09-18 12:02:59 +00:00
} else {
2020-05-14 14:41:43 +00:00
if ( files_to_delete . find ( previous_favorites [ i ] ) < 0 ) {
2018-09-18 12:02:59 +00:00
new_favorites . push_back ( previous_favorites [ i ] ) ;
2020-05-14 14:41:43 +00:00
}
2018-02-26 07:59:49 +00:00
}
2018-09-18 12:02:59 +00:00
}
2018-02-26 07:59:49 +00:00
2018-09-18 12:02:59 +00:00
if ( new_favorites . size ( ) < previous_favorites . size ( ) ) {
EditorSettings : : get_singleton ( ) - > set_favorites ( new_favorites ) ;
2015-08-23 23:15:56 +00:00
}
}
2018-11-13 19:17:33 +00:00
void DependencyRemoveDialog : : _bind_methods ( ) {
2023-01-23 09:01:28 +00:00
ADD_SIGNAL ( MethodInfo ( " resource_removed " , PropertyInfo ( Variant : : OBJECT , " obj " ) ) ) ;
2018-11-13 19:17:33 +00:00
ADD_SIGNAL ( MethodInfo ( " file_removed " , PropertyInfo ( Variant : : STRING , " file " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " folder_removed " , PropertyInfo ( Variant : : STRING , " folder " ) ) ) ;
}
2015-08-23 23:15:56 +00:00
DependencyRemoveDialog : : DependencyRemoveDialog ( ) {
2022-07-08 00:31:19 +00:00
set_ok_button_text ( TTR ( " Remove " ) ) ;
2019-06-15 07:03:28 +00:00
2017-03-05 15:44:50 +00:00
VBoxContainer * vb = memnew ( VBoxContainer ) ;
2015-08-23 23:15:56 +00:00
add_child ( vb ) ;
2017-01-10 04:49:55 +00:00
2017-03-05 15:44:50 +00:00
text = memnew ( Label ) ;
2015-08-23 23:15:56 +00:00
vb - > add_child ( text ) ;
2017-03-05 15:44:50 +00:00
owners = memnew ( Tree ) ;
2024-03-17 08:28:18 +00:00
owners - > set_auto_translate_mode ( AUTO_TRANSLATE_MODE_DISABLED ) ;
2015-08-23 23:15:56 +00:00
owners - > set_hide_root ( true ) ;
vb - > add_child ( owners ) ;
2020-03-06 17:00:16 +00:00
owners - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
2015-08-23 23:15:56 +00:00
}
//////////////
2018-11-02 03:44:40 +00:00
void DependencyErrorDialog : : show ( Mode p_mode , const String & p_for_file , const Vector < String > & report ) {
mode = p_mode ;
2017-03-05 15:44:50 +00:00
for_file = p_for_file ;
set_title ( TTR ( " Error loading: " ) + " " + p_for_file . get_file ( ) ) ;
2015-08-23 23:15:56 +00:00
files - > clear ( ) ;
2020-04-01 23:20:12 +00:00
TreeItem * root = files - > create_item ( nullptr ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < report . size ( ) ; i + + ) {
2015-08-23 23:15:56 +00:00
String dep ;
2017-03-05 15:44:50 +00:00
String type = " Object " ;
dep = report [ i ] . get_slice ( " :: " , 0 ) ;
2020-05-14 14:41:43 +00:00
if ( report [ i ] . get_slice_count ( " :: " ) > 0 ) {
2017-03-05 15:44:50 +00:00
type = report [ i ] . get_slice ( " :: " , 1 ) ;
2020-05-14 14:41:43 +00:00
}
2015-08-23 23:15:56 +00:00
2019-06-11 18:43:37 +00:00
Ref < Texture2D > icon = EditorNode : : get_singleton ( ) - > get_class_icon ( type ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
TreeItem * ti = files - > create_item ( root ) ;
ti - > set_text ( 0 , dep ) ;
ti - > set_icon ( 0 , icon ) ;
2015-08-23 23:15:56 +00:00
}
2019-06-15 07:03:28 +00:00
popup_centered ( ) ;
2015-08-23 23:15:56 +00:00
}
void DependencyErrorDialog : : ok_pressed ( ) {
2018-11-02 03:44:40 +00:00
switch ( mode ) {
case MODE_SCENE :
EditorNode : : get_singleton ( ) - > load_scene ( for_file , true ) ;
break ;
case MODE_RESOURCE :
EditorNode : : get_singleton ( ) - > load_resource ( for_file , true ) ;
break ;
}
2015-08-23 23:15:56 +00:00
}
2017-03-05 15:44:50 +00:00
void DependencyErrorDialog : : custom_action ( const String & ) {
2015-08-23 23:15:56 +00:00
EditorNode : : get_singleton ( ) - > fix_dependencies ( for_file ) ;
}
DependencyErrorDialog : : DependencyErrorDialog ( ) {
2017-03-05 15:44:50 +00:00
VBoxContainer * vb = memnew ( VBoxContainer ) ;
2015-08-23 23:15:56 +00:00
add_child ( vb ) ;
2017-01-10 04:49:55 +00:00
2017-03-05 15:44:50 +00:00
files = memnew ( Tree ) ;
2024-03-17 08:28:18 +00:00
files - > set_auto_translate_mode ( AUTO_TRANSLATE_MODE_DISABLED ) ;
2015-08-23 23:15:56 +00:00
files - > set_hide_root ( true ) ;
2018-11-02 03:44:40 +00:00
vb - > add_margin_child ( TTR ( " Load failed due to missing dependencies: " ) , files , true ) ;
2020-03-06 17:00:16 +00:00
files - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
2019-06-15 07:03:28 +00:00
2020-03-06 17:00:16 +00:00
set_min_size ( Size2 ( 500 , 220 ) * EDSCALE ) ;
2022-07-08 00:31:19 +00:00
set_ok_button_text ( TTR ( " Open Anyway " ) ) ;
set_cancel_button_text ( TTR ( " Close " ) ) ;
2015-08-23 23:15:56 +00:00
2017-03-05 15:44:50 +00:00
text = memnew ( Label ) ;
2015-08-23 23:15:56 +00:00
vb - > add_child ( text ) ;
2016-05-04 01:25:37 +00:00
text - > set_text ( TTR ( " Which action should be taken? " ) ) ;
2015-08-23 23:15:56 +00:00
2020-11-24 09:12:55 +00:00
mode = Mode : : MODE_RESOURCE ;
2017-03-05 15:44:50 +00:00
fdep = add_button ( TTR ( " Fix Dependencies " ) , true , " fixdeps " ) ;
2015-08-23 23:15:56 +00:00
2016-05-04 01:25:37 +00:00
set_title ( TTR ( " Errors loading! " ) ) ;
2015-08-23 23:15:56 +00:00
}
2015-11-29 04:08:31 +00:00
//////////////////////////////////////////////////////////////////////
void OrphanResourcesDialog : : ok_pressed ( ) {
paths . clear ( ) ;
2017-03-05 15:44:50 +00:00
_find_to_delete ( files - > get_root ( ) , paths ) ;
2020-12-15 12:04:21 +00:00
if ( paths . is_empty ( ) ) {
2015-11-29 04:08:31 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2015-11-29 04:08:31 +00:00
2017-03-05 15:44:50 +00:00
delete_confirm - > set_text ( vformat ( TTR ( " Permanently delete %d item(s)? (No undo!) " ) , paths . size ( ) ) ) ;
2020-03-06 17:00:16 +00:00
delete_confirm - > popup_centered ( ) ;
2015-11-29 04:08:31 +00:00
}
2017-03-05 15:44:50 +00:00
bool OrphanResourcesDialog : : _fill_owners ( EditorFileSystemDirectory * efsd , HashMap < String , int > & refs , TreeItem * p_parent ) {
2020-05-14 14:41:43 +00:00
if ( ! efsd ) {
2015-11-29 04:08:31 +00:00
return false ;
2020-05-14 14:41:43 +00:00
}
2015-11-29 04:08:31 +00:00
2018-01-18 20:37:17 +00:00
bool has_children = false ;
2015-11-29 04:08:31 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_subdir_count ( ) ; i + + ) {
2020-04-01 23:20:12 +00:00
TreeItem * dir_item = nullptr ;
2015-11-29 04:08:31 +00:00
if ( p_parent ) {
dir_item = files - > create_item ( p_parent ) ;
2017-03-05 15:44:50 +00:00
dir_item - > set_text ( 0 , efsd - > get_subdir ( i ) - > get_name ( ) ) ;
2021-07-17 21:22:52 +00:00
dir_item - > set_icon ( 0 , files - > get_theme_icon ( SNAME ( " folder " ) , SNAME ( " FileDialog " ) ) ) ;
2015-11-29 04:08:31 +00:00
}
2017-03-05 15:44:50 +00:00
bool children = _fill_owners ( efsd - > get_subdir ( i ) , refs , dir_item ) ;
2015-11-29 04:08:31 +00:00
if ( p_parent ) {
if ( ! children ) {
memdelete ( dir_item ) ;
} else {
2018-01-18 20:37:17 +00:00
has_children = true ;
2015-11-29 04:08:31 +00:00
}
}
}
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < efsd - > get_file_count ( ) ; i + + ) {
2015-11-29 04:08:31 +00:00
if ( ! p_parent ) {
Vector < String > deps = efsd - > get_file_deps ( i ) ;
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < deps . size ( ) ; j + + ) {
2015-11-29 04:08:31 +00:00
if ( ! refs . has ( deps [ j ] ) ) {
2017-03-05 15:44:50 +00:00
refs [ deps [ j ] ] = 1 ;
2015-11-29 04:08:31 +00:00
}
}
} else {
String path = efsd - > get_file_path ( i ) ;
if ( ! refs . has ( path ) ) {
2017-03-05 15:44:50 +00:00
TreeItem * ti = files - > create_item ( p_parent ) ;
ti - > set_cell_mode ( 0 , TreeItem : : CELL_MODE_CHECK ) ;
ti - > set_text ( 0 , efsd - > get_file ( i ) ) ;
ti - > set_editable ( 0 , true ) ;
2015-11-29 04:08:31 +00:00
2017-03-05 15:44:50 +00:00
String type = efsd - > get_file_type ( i ) ;
2015-11-29 04:08:31 +00:00
2019-06-11 18:43:37 +00:00
Ref < Texture2D > icon = EditorNode : : get_singleton ( ) - > get_class_icon ( type ) ;
2017-03-05 15:44:50 +00:00
ti - > set_icon ( 0 , icon ) ;
2015-11-29 04:08:31 +00:00
int ds = efsd - > get_file_deps ( i ) . size ( ) ;
2017-03-05 15:44:50 +00:00
ti - > set_text ( 1 , itos ( ds ) ) ;
2015-11-29 04:08:31 +00:00
if ( ds ) {
2023-08-13 00:33:39 +00:00
ti - > add_button ( 1 , files - > get_editor_theme_icon ( SNAME ( " GuiVisibilityVisible " ) ) , - 1 , false , TTR ( " Show Dependencies " ) ) ;
2015-11-29 04:08:31 +00:00
}
2017-03-05 15:44:50 +00:00
ti - > set_metadata ( 0 , path ) ;
2018-01-18 20:37:17 +00:00
has_children = true ;
2015-11-29 04:08:31 +00:00
}
}
}
2018-01-18 20:37:17 +00:00
return has_children ;
2015-11-29 04:08:31 +00:00
}
void OrphanResourcesDialog : : refresh ( ) {
2017-03-05 15:44:50 +00:00
HashMap < String , int > refs ;
2020-04-01 23:20:12 +00:00
_fill_owners ( EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) , refs , nullptr ) ;
2015-11-29 04:08:31 +00:00
files - > clear ( ) ;
2017-03-05 15:44:50 +00:00
TreeItem * root = files - > create_item ( ) ;
_fill_owners ( EditorFileSystem : : get_singleton ( ) - > get_filesystem ( ) , refs , root ) ;
2015-11-29 04:08:31 +00:00
}
2017-03-05 15:44:50 +00:00
void OrphanResourcesDialog : : show ( ) {
2015-11-29 04:08:31 +00:00
refresh ( ) ;
2020-07-11 16:45:19 +00:00
popup_centered_ratio ( 0.4 ) ;
2015-11-29 04:08:31 +00:00
}
2022-09-29 09:53:28 +00:00
void OrphanResourcesDialog : : _find_to_delete ( TreeItem * p_item , List < String > & r_paths ) {
2017-03-05 15:44:50 +00:00
while ( p_item ) {
if ( p_item - > get_cell_mode ( 0 ) = = TreeItem : : CELL_MODE_CHECK & & p_item - > is_checked ( 0 ) ) {
2022-09-29 09:53:28 +00:00
r_paths . push_back ( p_item - > get_metadata ( 0 ) ) ;
2015-11-29 04:08:31 +00:00
}
2021-03-07 20:07:30 +00:00
if ( p_item - > get_first_child ( ) ) {
2022-09-29 09:53:28 +00:00
_find_to_delete ( p_item - > get_first_child ( ) , r_paths ) ;
2015-11-29 04:08:31 +00:00
}
2017-03-05 15:44:50 +00:00
p_item = p_item - > get_next ( ) ;
2015-11-29 04:08:31 +00:00
}
}
void OrphanResourcesDialog : : _delete_confirm ( ) {
2022-03-23 09:08:58 +00:00
Ref < DirAccess > da = DirAccess : : create ( DirAccess : : ACCESS_RESOURCES ) ;
2021-07-24 13:46:25 +00:00
for ( const String & E : paths ) {
2021-07-16 03:45:57 +00:00
da - > remove ( E ) ;
EditorFileSystem : : get_singleton ( ) - > update_file ( E ) ;
2015-11-29 04:08:31 +00:00
}
refresh ( ) ;
}
2021-09-18 07:33:18 +00:00
void OrphanResourcesDialog : : _button_pressed ( Object * p_item , int p_column , int p_id , MouseButton p_button ) {
if ( p_button ! = MouseButton : : LEFT ) {
return ;
}
2017-08-24 20:58:51 +00:00
TreeItem * ti = Object : : cast_to < TreeItem > ( p_item ) ;
2015-11-29 04:08:31 +00:00
String path = ti - > get_metadata ( 0 ) ;
dep_edit - > edit ( path ) ;
}
void OrphanResourcesDialog : : _bind_methods ( ) {
}
2017-03-05 15:44:50 +00:00
OrphanResourcesDialog : : OrphanResourcesDialog ( ) {
2019-06-15 07:03:28 +00:00
set_title ( TTR ( " Orphan Resource Explorer " ) ) ;
delete_confirm = memnew ( ConfirmationDialog ) ;
2022-07-08 00:31:19 +00:00
set_ok_button_text ( TTR ( " Delete " ) ) ;
2019-06-15 07:03:28 +00:00
add_child ( delete_confirm ) ;
dep_edit = memnew ( DependencyEditor ) ;
add_child ( dep_edit ) ;
2020-02-21 17:28:45 +00:00
delete_confirm - > connect ( " confirmed " , callable_mp ( this , & OrphanResourcesDialog : : _delete_confirm ) ) ;
2019-06-15 07:03:28 +00:00
set_hide_on_ok ( false ) ;
2017-03-05 15:44:50 +00:00
VBoxContainer * vbc = memnew ( VBoxContainer ) ;
2015-11-29 04:08:31 +00:00
add_child ( vbc ) ;
2017-01-10 04:49:55 +00:00
2017-03-05 15:44:50 +00:00
files = memnew ( Tree ) ;
2024-03-17 08:28:18 +00:00
files - > set_auto_translate_mode ( AUTO_TRANSLATE_MODE_DISABLED ) ;
2015-11-29 04:08:31 +00:00
files - > set_columns ( 2 ) ;
files - > set_column_titles_visible ( true ) ;
2021-07-04 03:13:28 +00:00
files - > set_column_custom_minimum_width ( 1 , 100 * EDSCALE ) ;
2017-03-05 15:44:50 +00:00
files - > set_column_expand ( 0 , true ) ;
2021-07-04 03:13:28 +00:00
files - > set_column_clip_content ( 0 , true ) ;
2017-03-05 15:44:50 +00:00
files - > set_column_expand ( 1 , false ) ;
2021-07-04 03:13:28 +00:00
files - > set_column_clip_content ( 1 , true ) ;
2017-03-05 15:44:50 +00:00
files - > set_column_title ( 0 , TTR ( " Resource " ) ) ;
files - > set_column_title ( 1 , TTR ( " Owns " ) ) ;
2015-11-29 04:08:31 +00:00
files - > set_hide_root ( true ) ;
2017-03-05 15:44:50 +00:00
vbc - > add_margin_child ( TTR ( " Resources Without Explicit Ownership: " ) , files , true ) ;
2021-09-18 07:33:18 +00:00
files - > connect ( " button_clicked " , callable_mp ( this , & OrphanResourcesDialog : : _button_pressed ) ) ;
2015-11-29 04:08:31 +00:00
}