Skip to content

Commit

Permalink
Merge pull request #79285 from KoBeWi/instant_atlas
Browse files Browse the repository at this point in the history
Streamline creating tile atlas sources
  • Loading branch information
YuriSizov committed Jul 12, 2023
2 parents 19f3303 + bdb7e62 commit 91fefcb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions editor/plugins/tiles/tile_set_atlas_source_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,7 @@ void TileSetAtlasSourceEditor::edit(Ref<TileSet> p_tile_set, TileSetAtlasSource
}

void TileSetAtlasSourceEditor::init_source() {
tool_setup_atlas_source_button->set_pressed(true);
confirm_auto_create_tiles->popup_centered();
}

Expand Down
51 changes: 38 additions & 13 deletions editor/plugins/tiles/tile_set_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/gui/editor_file_dialog.h"

#include "scene/gui/box_container.h"
#include "scene/gui/control.h"
Expand Down Expand Up @@ -223,6 +224,30 @@ void TileSetEditor::_update_sources_list(int force_selected_id) {
TilesEditorPlugin::get_singleton()->set_sources_lists_current(sources_list->get_current());
}

void TileSetEditor::_texture_file_selected(const String &p_path) {
Ref<Texture2D> texture = ResourceLoader::load(p_path);
if (texture.is_null()) {
EditorNode::get_singleton()->show_warning(TTR("Invalid texture selected."));
return;
}

int source_id = tile_set->get_next_source_id();

Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);
atlas_source->set_texture(texture);

// Add a new source.
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Add atlas source"));
undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);
undo_redo->add_do_method(*atlas_source, "set_texture_region_size", tile_set->get_tile_size());
undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
undo_redo->commit_action();

_update_sources_list(source_id);
tile_set_atlas_source_editor->init_source();
}

void TileSetEditor::_source_selected(int p_source_index) {
ERR_FAIL_COND(!tile_set.is_valid());

Expand Down Expand Up @@ -278,19 +303,19 @@ void TileSetEditor::_source_add_id_pressed(int p_id_pressed) {

switch (p_id_pressed) {
case 0: {
int source_id = tile_set->get_next_source_id();

Ref<TileSetAtlasSource> atlas_source = memnew(TileSetAtlasSource);

// Add a new source.
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Add atlas source"));
undo_redo->add_do_method(*tile_set, "add_source", atlas_source, source_id);
undo_redo->add_do_method(*atlas_source, "set_texture_region_size", tile_set->get_tile_size());
undo_redo->add_undo_method(*tile_set, "remove_source", source_id);
undo_redo->commit_action();

_update_sources_list(source_id);
if (!texture_file_dialog) {
texture_file_dialog = memnew(EditorFileDialog);
add_child(texture_file_dialog);
texture_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
texture_file_dialog->connect("file_selected", callable_mp(this, &TileSetEditor::_texture_file_selected));

List<String> extensions;
ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions);
for (const String &E : extensions) {
texture_file_dialog->add_filter("*." + E, E.to_upper());
}
}
texture_file_dialog->popup_file_dialog();
} break;
case 1: {
int source_id = tile_set->get_next_source_id();
Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/tiles/tile_set_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "tile_set_atlas_source_editor.h"
#include "tile_set_scenes_collection_source_editor.h"

class EditorFileDialog;

class TileSetEditor : public VBoxContainer {
GDCLASS(TileSetEditor, VBoxContainer);

Expand Down Expand Up @@ -72,12 +74,14 @@ class TileSetEditor : public VBoxContainer {
MenuButton *sources_advanced_menu_button = nullptr;
ItemList *sources_list = nullptr;
Ref<Texture2D> missing_texture_texture;
void _texture_file_selected(const String &p_path);
void _source_selected(int p_source_index);
void _source_delete_pressed();
void _source_add_id_pressed(int p_id_pressed);
void _sources_advanced_menu_id_pressed(int p_id_pressed);
void _set_source_sort(int p_sort);

EditorFileDialog *texture_file_dialog = nullptr;
AtlasMergingDialog *atlas_merging_dialog = nullptr;
TileProxiesManagerDialog *tile_proxies_manager_dialog = nullptr;

Expand Down

0 comments on commit 91fefcb

Please sign in to comment.