浏览代码

Merge pull request #5506 from vnen/enhance-quickopen-search

Improve Quick Open search
Rémi Verschelde 9 年之前
父节点
当前提交
f866a79388
共有 2 个文件被更改,包括 60 次插入17 次删除
  1. 58 16
      tools/editor/quick_open.cpp
  2. 2 1
      tools/editor/quick_open.h

+ 58 - 16
tools/editor/quick_open.cpp

@@ -109,16 +109,16 @@ void EditorQuickOpen::_sbox_input(const InputEvent& p_ie) {
 
 }
 
-void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd) {
+void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector< Pair< String, Ref<Texture> > > &list) {
 
 	if (!add_directories) {
 		for(int i=0;i<efsd->get_subdir_count();i++) {
 
-			_parse_fs(efsd->get_subdir(i));
+			_parse_fs(efsd->get_subdir(i), list);
 		}
 	}
 
-	TreeItem *root = search_options->get_root();
+	String search_text = search_box->get_text();
 
 	if (add_directories) {
 		String path = efsd->get_path();
@@ -126,11 +126,26 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd) {
 			path+="/";
 		if (path!="res://") {
 			path=path.substr(6,path.length());
-			if (search_box->get_text().is_subsequence_ofi(path)) {
-				TreeItem *ti = search_options->create_item(root);
-				ti->set_text(0,path);
-				Ref<Texture> icon = get_icon("folder","FileDialog");
-				ti->set_icon(0,icon);
+			if (search_text.is_subsequence_ofi(path)) {
+				Pair< String, Ref<Texture> > pair;
+				pair.first = path;
+				pair.second = get_icon("folder", "FileDialog");
+				if (list.size() > 0) {
+
+					float this_sim = search_text.to_lower().similarity(path.to_lower());
+					float other_sim = search_text.to_lower().similarity(list[0].first.to_lower());
+					int pos = 1;
+
+					while (pos < list.size() && this_sim < other_sim) {
+						other_sim = search_text.to_lower().similarity(list[pos++].first.to_lower());
+					}
+
+					pos = this_sim > other_sim ? pos - 1 : pos;
+					list.insert(pos, pair);
+
+				} else {
+					list.push_back(pair);
+				}
 			}
 		}
 	}
@@ -138,12 +153,29 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd) {
 
 		String file = efsd->get_file_path(i);
 		file=file.substr(6,file.length());
-		if (ObjectTypeDB::is_type(efsd->get_file_type(i),base_type) && (search_box->get_text().is_subsequence_ofi(file))) {
 
-			TreeItem *ti = search_options->create_item(root);
-			ti->set_text(0,file);
-			Ref<Texture> icon = get_icon( (has_icon(efsd->get_file_type(i),ei)?efsd->get_file_type(i):ot),ei);
-			ti->set_icon(0,icon);
+		if (ObjectTypeDB::is_type(efsd->get_file_type(i),base_type) && (search_text.is_subsequence_ofi(file))) {
+			Pair< String, Ref<Texture> > pair;
+			pair.first = file;
+			pair.second = get_icon((has_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei);
+
+			if (list.size() > 0) {
+
+				float this_sim = search_text.to_lower().similarity(file.to_lower());
+				float other_sim = search_text.to_lower().similarity(list[0].first.to_lower());
+				int pos = 1;
+
+				while (pos < list.size() && this_sim < other_sim) {
+					other_sim = search_text.to_lower().similarity(list[pos++].first.to_lower());
+				}
+
+				pos = this_sim > other_sim ? pos - 1 : pos;
+				list.insert(pos, pair);
+
+			} else {
+
+				list.push_back(pair);
+			}
 		}
 	}
 
@@ -151,7 +183,7 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd) {
 	if (add_directories) {
 		for(int i=0;i<efsd->get_subdir_count();i++) {
 
-			_parse_fs(efsd->get_subdir(i));
+			_parse_fs(efsd->get_subdir(i), list);
 		}
 	}
 
@@ -159,10 +191,20 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd) {
 
 void EditorQuickOpen::_update_search() {
 
-
 	search_options->clear();
 	TreeItem *root = search_options->create_item();
-	_parse_fs(EditorFileSystem::get_singleton()->get_filesystem());
+	EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem();
+	Vector< Pair< String, Ref<Texture> > > list;
+
+	_parse_fs(efsd, list);
+
+	//String best_match = list[0];
+
+	for (int i = 0; i < list.size(); i++) {
+		TreeItem *ti = search_options->create_item(root);
+		ti->set_text(0, list[i].first);
+		ti->set_icon(0, list[i].second);
+	}
 
 	if (root->get_children()) {
 		TreeItem *ti = root->get_children();

+ 2 - 1
tools/editor/quick_open.h

@@ -32,6 +32,7 @@
 #include "scene/gui/dialogs.h"
 #include "scene/gui/tree.h"
 #include "editor_file_system.h"
+#include "pair.h"
 class EditorQuickOpen : public ConfirmationDialog {
 
 	OBJ_TYPE(EditorQuickOpen,ConfirmationDialog )
@@ -47,7 +48,7 @@ class EditorQuickOpen : public ConfirmationDialog {
 	void _update_search();
 
 	void _sbox_input(const InputEvent& p_ie);
-	void _parse_fs(EditorFileSystemDirectory *efsd);
+	void _parse_fs(EditorFileSystemDirectory *efsd, Vector< Pair< String,Ref <Texture> > > &list);
 
 
 	void _confirmed();