summaryrefslogtreecommitdiff
path: root/modules/gdnative/gd_native_library_editor.cpp
blob: 8aa931d6c972e0700690748b4ee7c934c994a5af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "gd_native_library_editor.h"
#include "gdnative.h"

void GDNativeLibraryEditor::_find_gdnative_singletons(EditorFileSystemDirectory *p_dir, const Set<String> &enabled_list) {


	// check children

	for (int i = 0; i < p_dir->get_file_count(); i++) {
		String file_type = p_dir->get_file_type(i);

		if (file_type != "GDNativeLibrary") {
			continue;
		}

		Ref<GDNativeLibrary> lib = ResourceLoader::load(p_dir->get_file_path(i));
		if (lib.is_valid() && lib->is_singleton_gdnative()) {
			String path = p_dir->get_file_path(i);
			TreeItem *ti = libraries->create_item(libraries->get_root());
			ti->set_text(0,path.get_file());
			ti->set_tooltip(0,path);
			ti->set_metadata(0,path);
			ti->set_cell_mode(1,TreeItem::CELL_MODE_RANGE);
			ti->set_text(1,"Disabled,Enabled");
			bool enabled = enabled_list.has(path)?true:false;

			ti->set_range(1,enabled?1:0);
			ti->set_custom_color(1,enabled?Color(0,1,0):Color(1,0,0));
		}
	}

	// check subdirectories
	for (int i = 0; i < p_dir->get_subdir_count(); i++) {
		_find_gdnative_singletons(p_dir->get_subdir(i),enabled_list);
	}


}

void GDNativeLibraryEditor::_update_libraries() {

	updating=true;
	libraries->clear();
	libraries->create_item(); //rppt

	Vector<String> enabled_paths;
	if (ProjectSettings::get_singleton()->has("gdnative/singletons")) {
		enabled_paths=ProjectSettings::get_singleton()->get("gdnative/singletons");
	}
	Set<String> enabled_list;
	for(int i=0;i<enabled_paths.size();i++) {
		enabled_list.insert(enabled_paths[i]);
	}

	EditorFileSystemDirectory *fs = EditorFileSystem::get_singleton()->get_filesystem();
	if (fs) {
		_find_gdnative_singletons(fs,enabled_list);
	}

	updating=false;

}

void GDNativeLibraryEditor::_item_edited() {
	if (updating)
		return;

	TreeItem *item = libraries->get_edited();
	if (!item)
		return;

	bool enabled = item->get_range(1);
	String path = item->get_metadata(0);

	Vector<String> enabled_paths;
	if (ProjectSettings::get_singleton()->has("gdnative/singletons")) {
		enabled_paths=ProjectSettings::get_singleton()->get("gdnative/singletons");
	}

	if (enabled) {
		if (enabled_paths.find(path)==-1) {
			enabled_paths.push_back(path);
		}
	} else {
		enabled_paths.erase(path);
	}

	if (enabled_paths.size()) {
		ProjectSettings::get_singleton()->set("gdnative/singletons",enabled_paths);
	} else {
		ProjectSettings::get_singleton()->set("gdnative/singletons",Variant());
	}
}

void GDNativeLibraryEditor::_notification(int p_what) {

	if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
		if (is_visible_in_tree()) {
			_update_libraries();
		}
	}
}

void GDNativeLibraryEditor::_bind_methods() {

	ClassDB::bind_method(D_METHOD("_item_edited"),&GDNativeLibraryEditor::_item_edited);
}

GDNativeLibraryEditor::GDNativeLibraryEditor()
{
	libraries = memnew( Tree );
	libraries->set_columns(2);
	libraries->set_column_titles_visible(true);
	libraries->set_column_title(0,TTR("Library"));
	libraries->set_column_title(1,TTR("Status"));
	libraries->set_hide_root(true);
	add_margin_child(TTR("Libraries: "),libraries,true);
	updating=false;
	libraries->connect("item_edited",this,"_item_edited");
}