@ -1672,6 +1672,8 @@ void TileSetAtlasSourceEditor::_menu_option(int p_option) {
_update_tile_id_label ( ) ;
} break ;
case ADVANCED_AUTO_CREATE_TILES : {
atlases_to_auto_create_tiles . clear ( ) ;
atlases_to_auto_create_tiles . append ( tile_set_atlas_source ) ;
_auto_create_tiles ( ) ;
} break ;
case ADVANCED_AUTO_REMOVE_TILES : {
@ -2096,6 +2098,8 @@ void TileSetAtlasSourceEditor::_tile_proxy_object_changed(String p_what) {
void TileSetAtlasSourceEditor : : _atlas_source_proxy_object_changed ( String p_what ) {
if ( p_what = = " texture " & & ! atlas_source_proxy_object - > get ( " texture " ) . is_null ( ) ) {
atlases_to_auto_create_tiles . clear ( ) ;
atlases_to_auto_create_tiles . append ( tile_set_atlas_source ) ;
confirm_auto_create_tiles - > popup_centered ( ) ;
} else if ( p_what = = " id " ) {
emit_signal ( SNAME ( " source_id_changed " ) , atlas_source_proxy_object - > get_id ( ) ) ;
@ -2242,54 +2246,61 @@ void TileSetAtlasSourceEditor::edit(Ref<TileSet> p_tile_set, TileSetAtlasSource
_update_current_tile_data_editor ( ) ;
}
void TileSetAtlasSourceEditor : : init_ source( ) {
void TileSetAtlasSourceEditor : : init_ new_atlases( const Vector < Ref < TileSetAtlasSource > > & p_atlases ) {
tool_setup_atlas_source_button - > set_pressed ( true ) ;
atlases_to_auto_create_tiles = p_atlases ;
confirm_auto_create_tiles - > popup_centered ( ) ;
}
void TileSetAtlasSourceEditor : : _auto_create_tiles ( ) {
if ( ! tile_set_atlas_source ) {
return ;
}
for ( Ref < TileSetAtlasSource > & atlas_source : atlases_to_auto_create_tiles ) {
if ( atlas_source . is_valid ( ) ) {
Ref < Texture2D > texture = atlas_source - > get_texture ( ) ;
if ( texture . is_valid ( ) ) {
Vector2i margins = atlas_source - > get_margins ( ) ;
Vector2i separation = atlas_source - > get_separation ( ) ;
Vector2i texture_region_size = atlas_source - > get_texture_region_size ( ) ;
Size2i grid_size = atlas_source - > get_atlas_grid_size ( ) ;
EditorUndoRedoManager * undo_redo = EditorUndoRedoManager : : get_singleton ( ) ;
undo_redo - > create_action ( TTR ( " Create tiles in non-transparent texture regions " ) ) ;
for ( int y = 0 ; y < grid_size . y ; y + + ) {
for ( int x = 0 ; x < grid_size . x ; x + + ) {
// Check if we have a tile at the coord.
Vector2i coords = Vector2i ( x , y ) ;
if ( atlas_source - > get_tile_at_coords ( coords ) = = TileSetSource : : INVALID_ATLAS_COORDS ) {
// Check if the texture is empty at the given coords.
Rect2i region = Rect2i ( margins + ( coords * ( texture_region_size + separation ) ) , texture_region_size ) ;
bool is_opaque = false ;
for ( int region_x = region . get_position ( ) . x ; region_x < region . get_end ( ) . x ; region_x + + ) {
for ( int region_y = region . get_position ( ) . y ; region_y < region . get_end ( ) . y ; region_y + + ) {
if ( texture - > is_pixel_opaque ( region_x , region_y ) ) {
is_opaque = true ;
break ;
}
}
if ( is_opaque ) {
break ;
}
}
Ref < Texture2D > texture = tile_set_atlas_source - > get_texture ( ) ;
if ( texture . is_valid ( ) ) {
Vector2i margins = tile_set_atlas_source - > get_margins ( ) ;
Vector2i separation = tile_set_atlas_source - > get_separation ( ) ;
Vector2i texture_region_size = tile_set_atlas_source - > get_texture_region_size ( ) ;
Size2i grid_size = tile_set_atlas_source - > get_atlas_grid_size ( ) ;
EditorUndoRedoManager * undo_redo = EditorUndoRedoManager : : get_singleton ( ) ;
undo_redo - > create_action ( TTR ( " Create tiles in non-transparent texture regions " ) ) ;
for ( int y = 0 ; y < grid_size . y ; y + + ) {
for ( int x = 0 ; x < grid_size . x ; x + + ) {
// Check if we have a tile at the coord
Vector2i coords = Vector2i ( x , y ) ;
if ( tile_set_atlas_source - > get_tile_at_coords ( coords ) = = TileSetSource : : INVALID_ATLAS_COORDS ) {
// Check if the texture is empty at the given coords.
Rect2i region = Rect2i ( margins + ( coords * ( texture_region_size + separation ) ) , texture_region_size ) ;
bool is_opaque = false ;
for ( int region_x = region . get_position ( ) . x ; region_x < region . get_end ( ) . x ; region_x + + ) {
for ( int region_y = region . get_position ( ) . y ; region_y < region . get_end ( ) . y ; region_y + + ) {
if ( texture - > is_pixel_opaque ( region_x , region_y ) ) {
is_opaque = true ;
break ;
// If we do have opaque pixels, create a tile.
if ( is_opaque ) {
undo_redo - > add_do_method ( * atlas_source , " create_tile " , coords ) ;
undo_redo - > add_undo_method ( * atlas_source , " remove_tile " , coords ) ;
}
}
if ( is_opaque ) {
break ;
}
}
// If we do have opaque pixels, create a tile.
if ( is_opaque ) {
undo_redo - > add_do_method ( tile_set_atlas_source , " create_tile " , coords ) ;
undo_redo - > add_undo_method ( tile_set_atlas_source , " remove_tile " , coords ) ;
}
}
undo_redo - > commit_action ( ) ;
}
}
undo_redo - > commit_action ( ) ;
}
_cancel_auto_create_tiles ( ) ;
}
void TileSetAtlasSourceEditor : : _cancel_auto_create_tiles ( ) {
atlases_to_auto_create_tiles . clear ( ) ;
}
void TileSetAtlasSourceEditor : : _auto_remove_tiles ( ) {
@ -2644,6 +2655,7 @@ TileSetAtlasSourceEditor::TileSetAtlasSourceEditor() {
confirm_auto_create_tiles - > set_ok_button_text ( TTR ( " Yes " ) ) ;
confirm_auto_create_tiles - > add_cancel_button ( ) - > set_text ( TTR ( " No " ) ) ;
confirm_auto_create_tiles - > connect ( " confirmed " , callable_mp ( this , & TileSetAtlasSourceEditor : : _auto_create_tiles ) ) ;
confirm_auto_create_tiles - > connect ( " canceled " , callable_mp ( this , & TileSetAtlasSourceEditor : : _cancel_auto_create_tiles ) ) ;
add_child ( confirm_auto_create_tiles ) ;
// Inspector plugin.