summaryrefslogtreecommitdiff
path: root/editor/plugins/tiles/tiles_editor_plugin.cpp
diff options
context:
space:
mode:
authorkobewi <kobewi4e@gmail.com>2021-11-10 02:27:37 +0100
committerkobewi <kobewi4e@gmail.com>2022-01-26 13:58:32 +0100
commite793331cd71f0fd147d22f588a6afa170e17f4a9 (patch)
tree5646a9b81727edda181f45d952d3bff78836a37b /editor/plugins/tiles/tiles_editor_plugin.cpp
parent672363f295495478ef7d03ea00878b8ebfbdb430 (diff)
Allow sorting tileset sources
Diffstat (limited to 'editor/plugins/tiles/tiles_editor_plugin.cpp')
-rw-r--r--editor/plugins/tiles/tiles_editor_plugin.cpp99
1 files changed, 97 insertions, 2 deletions
diff --git a/editor/plugins/tiles/tiles_editor_plugin.cpp b/editor/plugins/tiles/tiles_editor_plugin.cpp
index cdde22f5bc..1aafab4713 100644
--- a/editor/plugins/tiles/tiles_editor_plugin.cpp
+++ b/editor/plugins/tiles/tiles_editor_plugin.cpp
@@ -218,15 +218,29 @@ void TilesEditorPlugin::set_sources_lists_current(int p_current) {
atlas_sources_lists_current = p_current;
}
-void TilesEditorPlugin::synchronize_sources_list(Object *p_current) {
- ItemList *item_list = Object::cast_to<ItemList>(p_current);
+void TilesEditorPlugin::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {
+ ItemList *item_list = Object::cast_to<ItemList>(p_current_list);
+ MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button);
ERR_FAIL_COND(!item_list);
+ ERR_FAIL_COND(!sorting_button);
+
+ if (sorting_button->is_visible_in_tree()) {
+ for (int i = 0; i != SOURCE_SORT_MAX; i++) {
+ sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort));
+ }
+ }
if (item_list->is_visible_in_tree()) {
if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) {
item_list->deselect_all();
} else {
+ // Make sure the selection is not overwritten after sorting.
+ int atlas_sources_lists_current_mem = atlas_sources_lists_current;
+ item_list->emit_signal(SNAME("sort_request"));
+ atlas_sources_lists_current = atlas_sources_lists_current_mem;
+
item_list->set_current(atlas_sources_lists_current);
+ item_list->ensure_current_is_visible();
item_list->emit_signal(SNAME("item_selected"), atlas_sources_lists_current);
}
}
@@ -246,6 +260,87 @@ void TilesEditorPlugin::synchronize_atlas_view(Object *p_current) {
}
}
+void TilesEditorPlugin::set_sorting_option(int p_option) {
+ source_sort = p_option;
+}
+
+List<int> TilesEditorPlugin::get_sorted_sources(const Ref<TileSet> tile_set) const {
+ SourceNameComparator::tile_set = tile_set;
+ List<int> source_ids;
+
+ for (int i = 0; i < tile_set->get_source_count(); i++) {
+ source_ids.push_back(tile_set->get_source_id(i));
+ }
+
+ switch (source_sort) {
+ case SOURCE_SORT_ID_REVERSE:
+ // Already sorted.
+ source_ids.reverse();
+ break;
+ case SOURCE_SORT_NAME:
+ source_ids.sort_custom<SourceNameComparator>();
+ break;
+ case SOURCE_SORT_NAME_REVERSE:
+ source_ids.sort_custom<SourceNameComparator>();
+ source_ids.reverse();
+ break;
+ default: // SOURCE_SORT_ID
+ break;
+ }
+
+ SourceNameComparator::tile_set.unref();
+ return source_ids;
+}
+
+Ref<TileSet> TilesEditorPlugin::SourceNameComparator::tile_set;
+
+bool TilesEditorPlugin::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {
+ String name_a;
+ String name_b;
+
+ {
+ TileSetSource *source = *tile_set->get_source(p_a);
+
+ if (!source->get_name().is_empty()) {
+ name_a = source->get_name();
+ }
+
+ TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
+ if (atlas_source) {
+ Ref<Texture2D> texture = atlas_source->get_texture();
+ if (name_a.is_empty() && texture.is_valid()) {
+ name_a = texture->get_path().get_file();
+ }
+ }
+
+ if (name_a.is_empty()) {
+ name_a = itos(p_a);
+ }
+ }
+
+ {
+ TileSetSource *source = *tile_set->get_source(p_b);
+
+ if (!source->get_name().is_empty()) {
+ name_b = source->get_name();
+ }
+
+ TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
+ if (atlas_source) {
+ Ref<Texture2D> texture = atlas_source->get_texture();
+ if (name_b.is_empty() && texture.is_valid()) {
+ name_b = texture->get_path().get_file();
+ }
+ }
+
+ if (name_b.is_empty()) {
+ name_b = itos(p_b);
+ }
+ }
+
+ return NaturalNoCaseComparator()(name_a, name_b);
+}
+
void TilesEditorPlugin::edit(Object *p_object) {
// Disconnect to changes.
TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));