ソースを参照

-orphan resource explorer dialog + tools menu

Juan Linietsky 9 年 前
コミット
46c5fda91d

+ 181 - 0
tools/editor/dependency_editor.cpp

@@ -510,3 +510,184 @@ DependencyErrorDialog::DependencyErrorDialog() {
 	set_title("Errors loading!");
 
 }
+
+//////////////////////////////////////////////////////////////////////
+
+
+
+void OrphanResourcesDialog::ok_pressed() {
+
+	paths.clear();
+
+	_find_to_delete(files->get_root(),paths);
+	if (paths.empty())
+		return;
+
+	delete_confirm->set_text("Permanently Delete "+itos(paths.size())+" Item(s) ? (No Undo!!)");
+	delete_confirm->popup_centered_minsize();
+}
+
+bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd,HashMap<String,int>& refs,TreeItem* p_parent){
+
+
+	if (!efsd)
+		return false;
+
+	bool has_childs=false;
+
+	for(int i=0;i<efsd->get_subdir_count();i++) {
+
+		TreeItem *dir_item=NULL;
+		if (p_parent) {
+			dir_item = files->create_item(p_parent);
+			dir_item->set_text(0,efsd->get_subdir(i)->get_name());
+			dir_item->set_icon(0,get_icon("folder","FileDialog"));
+
+		}
+		bool children = _fill_owners(efsd->get_subdir(i),refs,dir_item);
+
+		if (p_parent) {
+			if (!children) {
+				memdelete(dir_item);
+			} else {
+				has_childs=true;
+			}
+		}
+
+	}
+
+
+	for(int i=0;i<efsd->get_file_count();i++) {
+
+		if (!p_parent) {
+			Vector<String> deps = efsd->get_file_deps(i);
+			//print_line(":::"+efsd->get_file_path(i));
+			for(int j=0;j<deps.size();j++) {
+
+				if (!refs.has(deps[j])) {
+					refs[deps[j]]=1;
+				}
+			}
+		} else {
+
+			String path = efsd->get_file_path(i);
+			if (!refs.has(path)) {
+				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);
+
+				String type=efsd->get_file_type(i);
+
+				Ref<Texture> icon;
+				if (has_icon(type,"EditorIcons")) {
+					icon=get_icon(type,"EditorIcons");
+				} else {
+					icon=get_icon("Object","EditorIcons");
+				}
+				ti->set_icon(0,icon);
+				int ds = efsd->get_file_deps(i).size();
+				ti->set_text(1,itos(ds));
+				if (ds) {
+					ti->add_button(1,get_icon("Visible","EditorIcons"));
+				}
+				ti->set_metadata(0,path);
+				has_childs=true;
+			}
+		}
+
+	}
+
+	return has_childs;
+}
+
+
+void OrphanResourcesDialog::refresh() {
+	HashMap<String,int> refs;
+	_fill_owners(EditorFileSystem::get_singleton()->get_filesystem(),refs,NULL);
+	files->clear();
+	TreeItem *root=files->create_item();
+	_fill_owners(EditorFileSystem::get_singleton()->get_filesystem(),refs,root);
+}
+
+
+void OrphanResourcesDialog::show(){
+
+	refresh();
+	popup_centered_ratio();
+}
+
+
+void OrphanResourcesDialog::_find_to_delete(TreeItem* p_item,List<String>& paths) {
+
+	while(p_item) {
+
+		if (p_item->get_cell_mode(0)==TreeItem::CELL_MODE_CHECK && p_item->is_checked(0)) {
+			paths.push_back(p_item->get_metadata(0));
+		}
+
+		if (p_item->get_children()) {
+			_find_to_delete(p_item->get_children(),paths);
+		}
+
+		p_item=p_item->get_next();
+	}
+
+
+}
+
+void OrphanResourcesDialog::_delete_confirm() {
+
+	DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
+	for (List<String>::Element *E=paths.front();E;E=E->next()) {
+
+		da->remove(E->get());
+		EditorFileSystem::get_singleton()->update_file(E->get());
+	}
+	memdelete(da);
+	refresh();
+}
+
+void OrphanResourcesDialog::_button_pressed(Object *p_item,int p_column, int p_id) {
+
+	TreeItem *ti=p_item->cast_to<TreeItem>();
+
+	String path = ti->get_metadata(0);
+	dep_edit->edit(path);
+
+}
+
+void OrphanResourcesDialog::_bind_methods() {
+
+	ObjectTypeDB::bind_method(_MD("_delete_confirm"),&OrphanResourcesDialog::_delete_confirm);
+	ObjectTypeDB::bind_method(_MD("_button_pressed"),&OrphanResourcesDialog::_button_pressed);
+
+}
+
+OrphanResourcesDialog::OrphanResourcesDialog(){
+
+	VBoxContainer *vbc = memnew( VBoxContainer );
+	add_child(vbc);
+	set_child_rect(vbc);
+	files = memnew( Tree );
+	files->set_columns(2);
+	files->set_column_titles_visible(true);
+	files->set_column_min_width(1,100);
+	files->set_column_expand(0,true);
+	files->set_column_expand(1,false);
+	files->set_column_title(0,"Resource");
+	files->set_column_title(1,"Owns");
+	files->set_hide_root(true);
+	vbc->add_margin_child("Resources Without Explicit Ownership:",files,true);
+	set_title("Orphan Resource Explorer");
+	delete_confirm = memnew( ConfirmationDialog );
+	delete_confirm->set_text("Delete selected files?");
+	get_ok()->set_text("Delete");
+	add_child(delete_confirm);
+	dep_edit = memnew( DependencyEditor );
+	add_child(dep_edit);
+	files->connect("button_pressed",this,"_button_pressed");
+	delete_confirm->connect("confirmed",this,"_delete_confirm");
+	set_hide_on_ok(false);
+
+}

+ 25 - 0
tools/editor/dependency_editor.h

@@ -91,4 +91,29 @@ public:
 	DependencyErrorDialog();
 };
 
+
+
+class OrphanResourcesDialog : public ConfirmationDialog {
+	OBJ_TYPE(OrphanResourcesDialog,ConfirmationDialog);
+
+	DependencyEditor *dep_edit;
+	Tree *files;
+	ConfirmationDialog *delete_confirm;
+	void ok_pressed();
+
+	bool _fill_owners(EditorFileSystemDirectory *efsd, HashMap<String,int>& refs, TreeItem *p_parent);
+
+	List<String> paths;
+	void _find_to_delete(TreeItem* p_item,List<String>& paths);
+	void _delete_confirm();
+	void _button_pressed(Object *p_item,int p_column, int p_id);
+
+	void refresh();
+	static void _bind_methods();
+public:
+
+	void show();
+	OrphanResourcesDialog();
+};
+
 #endif // DEPENDENCY_EDITOR_H

+ 17 - 1
tools/editor/editor_node.cpp

@@ -2388,6 +2388,10 @@ void EditorNode::_menu_option_confirm(int p_option,bool p_confirmed) {
 				log->add_message("REDO: "+action);
 
 		} break;
+		case TOOLS_ORPHAN_RESOURCES: {
+
+			orphan_resources->show();
+		} break;
 
 		case EDIT_REVERT: {
 
@@ -5048,6 +5052,17 @@ EditorNode::EditorNode() {
 	p=import_menu->get_popup();
 	p->connect("item_pressed",this,"_menu_option");
 
+	tool_menu = memnew( MenuButton );
+	tool_menu->set_tooltip("Miscelaneous project or scene wide tools.");
+	tool_menu->set_text("Tools");
+
+	//tool_menu->set_icon(gui_base->get_icon("Save","EditorIcons"));
+	left_menu_hb->add_child( tool_menu );
+
+	p=tool_menu->get_popup();
+	p->connect("item_pressed",this,"_menu_option");
+	p->add_item("Orphan Resource Explorer",TOOLS_ORPHAN_RESOURCES);
+
 	export_button = memnew( ToolButton );
 	export_button->set_tooltip("Export the project to many platforms.");
 	export_button->set_text("Export");
@@ -5476,7 +5491,8 @@ EditorNode::EditorNode() {
 
 
 
-
+	orphan_resources = memnew( OrphanResourcesDialog );
+	gui_base->add_child(orphan_resources);
 
 
 

+ 3 - 0
tools/editor/editor_node.h

@@ -133,6 +133,7 @@ class EditorNode : public Node {
 		EDIT_UNDO,
 		EDIT_REDO,
 		EDIT_REVERT,
+		TOOLS_ORPHAN_RESOURCES,
 		RESOURCE_NEW,
 		RESOURCE_LOAD,
 		RESOURCE_SAVE,
@@ -237,6 +238,7 @@ class EditorNode : public Node {
 	Control *viewport;
 	MenuButton *file_menu;
 	MenuButton *import_menu;
+	MenuButton *tool_menu;
 	ToolButton *export_button;
 	ToolButton *prev_scene;
 	MenuButton *object_menu;
@@ -333,6 +335,7 @@ class EditorNode : public Node {
 
 	DependencyErrorDialog *dependency_error;
 	DependencyEditor *dependency_fixer;
+	OrphanResourcesDialog *orphan_resources;
 
 	TabContainer *dock_slot[DOCK_SLOT_MAX];
 	Rect2 dock_select_rect[DOCK_SLOT_MAX];