Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/plugins/editor_plugin_settings.cpp
9896 views
1
/**************************************************************************/
2
/* editor_plugin_settings.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "editor_plugin_settings.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/io/config_file.h"
35
#include "core/io/dir_access.h"
36
#include "core/io/file_access.h"
37
#include "editor/editor_node.h"
38
#include "editor/editor_string_names.h"
39
#include "editor/themes/editor_scale.h"
40
#include "scene/gui/margin_container.h"
41
#include "scene/gui/separator.h"
42
#include "scene/gui/texture_rect.h"
43
#include "scene/gui/tree.h"
44
45
void EditorPluginSettings::_notification(int p_what) {
46
switch (p_what) {
47
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
48
update_plugins();
49
} break;
50
51
case NOTIFICATION_READY: {
52
plugin_config_dialog->connect("plugin_ready", callable_mp(EditorNode::get_singleton(), &EditorNode::_on_plugin_ready));
53
plugin_list->connect("button_clicked", callable_mp(this, &EditorPluginSettings::_cell_button_pressed));
54
} break;
55
56
case NOTIFICATION_TRANSLATION_CHANGED: {
57
if (plugin_list->get_root()) {
58
update_plugins();
59
}
60
} break;
61
62
case NOTIFICATION_THEME_CHANGED: {
63
if (Engine::get_singleton()->is_recovery_mode_hint()) {
64
recovery_mode_icon->set_texture(get_editor_theme_icon(SNAME("NodeWarning")));
65
}
66
} break;
67
}
68
}
69
70
void EditorPluginSettings::update_plugins() {
71
plugin_list->clear();
72
updating = true;
73
TreeItem *root = plugin_list->create_item();
74
75
Vector<String> plugins = _get_plugins("res://addons");
76
plugins.sort();
77
78
for (int i = 0; i < plugins.size(); i++) {
79
Ref<ConfigFile> cfg;
80
cfg.instantiate();
81
const String &path = plugins[i];
82
83
Error err = cfg->load(path);
84
85
if (err != OK) {
86
WARN_PRINT("Can't load plugin config at: " + path);
87
} else {
88
Vector<String> missing_keys;
89
for (const String required_key : { "name", "author", "version", "description", "script" }) {
90
if (!cfg->has_section_key("plugin", required_key)) {
91
missing_keys.append("\"plugin/" + required_key + "\"");
92
}
93
}
94
95
if (!missing_keys.is_empty()) {
96
WARN_PRINT(vformat("Plugin config at \"%s\" is missing the following keys: %s", path, String(",").join(missing_keys)));
97
} else {
98
String name = cfg->get_value("plugin", "name");
99
String author = cfg->get_value("plugin", "author");
100
String version = cfg->get_value("plugin", "version");
101
String description = cfg->get_value("plugin", "description");
102
String scr = cfg->get_value("plugin", "script");
103
104
bool is_enabled = EditorNode::get_singleton()->is_addon_plugin_enabled(path);
105
Color disabled_color = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
106
107
const PackedInt32Array boundaries = TS->string_get_word_breaks(description, "", 80);
108
String wrapped_description;
109
110
for (int j = 0; j < boundaries.size(); j += 2) {
111
const int start = boundaries[j];
112
const int end = boundaries[j + 1];
113
wrapped_description += "\n" + description.substr(start, end - start + 1).rstrip("\n");
114
}
115
116
TreeItem *item = plugin_list->create_item(root);
117
item->set_text(COLUMN_NAME, name);
118
if (!is_enabled) {
119
item->set_custom_color(COLUMN_NAME, disabled_color);
120
}
121
item->set_tooltip_text(COLUMN_NAME, vformat(TTR("Name: %s\nPath: %s\nMain Script: %s\n\n%s"), name, path, scr, wrapped_description));
122
item->set_metadata(COLUMN_NAME, path);
123
item->set_text(COLUMN_VERSION, version);
124
item->set_custom_font(COLUMN_VERSION, get_theme_font("source", EditorStringName(EditorFonts)));
125
item->set_metadata(COLUMN_VERSION, scr);
126
item->set_text(COLUMN_AUTHOR, author);
127
item->set_metadata(COLUMN_AUTHOR, description);
128
item->set_cell_mode(COLUMN_STATUS, TreeItem::CELL_MODE_CHECK);
129
item->set_text(COLUMN_STATUS, TTRC("On"));
130
item->set_checked(COLUMN_STATUS, is_enabled);
131
item->set_editable(COLUMN_STATUS, true);
132
item->add_button(COLUMN_EDIT, get_editor_theme_icon(SNAME("Edit")), BUTTON_PLUGIN_EDIT, false, TTRC("Edit Plugin"));
133
}
134
}
135
}
136
137
updating = false;
138
}
139
140
void EditorPluginSettings::_plugin_activity_changed() {
141
if (updating) {
142
return;
143
}
144
145
TreeItem *ti = plugin_list->get_edited();
146
ERR_FAIL_NULL(ti);
147
bool checked = ti->is_checked(COLUMN_STATUS);
148
String name = ti->get_metadata(COLUMN_NAME);
149
150
EditorNode::get_singleton()->set_addon_plugin_enabled(name, checked, true);
151
152
bool is_enabled = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
153
154
if (is_enabled != checked) {
155
updating = true;
156
ti->set_checked(COLUMN_STATUS, is_enabled);
157
updating = false;
158
}
159
if (is_enabled) {
160
ti->clear_custom_color(COLUMN_NAME);
161
} else {
162
ti->set_custom_color(COLUMN_NAME, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
163
}
164
}
165
166
void EditorPluginSettings::_create_clicked() {
167
plugin_config_dialog->config("");
168
plugin_config_dialog->popup_centered();
169
}
170
171
void EditorPluginSettings::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
172
if (p_button != MouseButton::LEFT) {
173
return;
174
}
175
TreeItem *item = Object::cast_to<TreeItem>(p_item);
176
if (!item) {
177
return;
178
}
179
if (p_id == BUTTON_PLUGIN_EDIT) {
180
if (p_column == COLUMN_EDIT) {
181
String dir = item->get_metadata(COLUMN_NAME);
182
plugin_config_dialog->config(dir);
183
plugin_config_dialog->popup_centered();
184
}
185
}
186
}
187
188
Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) {
189
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
190
Error err = da->change_dir(p_dir);
191
if (err != OK) {
192
return Vector<String>();
193
}
194
195
Vector<String> plugins;
196
da->list_dir_begin();
197
for (String path = da->get_next(); !path.is_empty(); path = da->get_next()) {
198
if (path[0] == '.' || !da->current_is_dir()) {
199
continue;
200
}
201
202
const String full_path = p_dir.path_join(path);
203
const String plugin_config = full_path.path_join("plugin.cfg");
204
if (FileAccess::exists(plugin_config)) {
205
plugins.push_back(plugin_config);
206
} else {
207
plugins.append_array(_get_plugins(full_path));
208
}
209
}
210
211
da->list_dir_end();
212
return plugins;
213
}
214
215
EditorPluginSettings::EditorPluginSettings() {
216
ProjectSettings::get_singleton()->add_hidden_prefix("editor_plugins/");
217
218
plugin_config_dialog = memnew(PluginConfigDialog);
219
plugin_config_dialog->config("");
220
add_child(plugin_config_dialog);
221
222
if (Engine::get_singleton()->is_recovery_mode_hint()) {
223
HBoxContainer *c = memnew(HBoxContainer);
224
add_child(c);
225
226
recovery_mode_icon = memnew(TextureRect);
227
recovery_mode_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
228
c->add_child(recovery_mode_icon);
229
230
Label *recovery_mode_label = memnew(Label(TTRC("Recovery mode is enabled. Enabled plugins will not run while this mode is active.")));
231
recovery_mode_label->set_theme_type_variation("HeaderSmall");
232
recovery_mode_label->set_h_size_flags(SIZE_EXPAND_FILL);
233
c->add_child(recovery_mode_label);
234
235
HSeparator *sep = memnew(HSeparator);
236
add_child(sep);
237
}
238
239
HBoxContainer *title_hb = memnew(HBoxContainer);
240
Label *label = memnew(Label(TTRC("Installed Plugins:")));
241
label->set_theme_type_variation("HeaderSmall");
242
title_hb->add_child(label);
243
title_hb->add_spacer();
244
Button *create_plugin_button = memnew(Button(TTRC("Create New Plugin")));
245
create_plugin_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPluginSettings::_create_clicked));
246
title_hb->add_child(create_plugin_button);
247
add_child(title_hb);
248
249
plugin_list = memnew(Tree);
250
plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
251
plugin_list->set_columns(COLUMN_MAX);
252
plugin_list->set_column_titles_visible(true);
253
plugin_list->set_column_title(COLUMN_STATUS, TTRC("Enabled"));
254
plugin_list->set_column_title(COLUMN_NAME, TTRC("Name"));
255
plugin_list->set_column_title(COLUMN_VERSION, TTRC("Version"));
256
plugin_list->set_column_title(COLUMN_AUTHOR, TTRC("Author"));
257
plugin_list->set_column_title(COLUMN_EDIT, TTRC("Edit"));
258
plugin_list->set_column_title_alignment(COLUMN_STATUS, HORIZONTAL_ALIGNMENT_LEFT);
259
plugin_list->set_column_title_alignment(COLUMN_NAME, HORIZONTAL_ALIGNMENT_LEFT);
260
plugin_list->set_column_title_alignment(COLUMN_VERSION, HORIZONTAL_ALIGNMENT_LEFT);
261
plugin_list->set_column_title_alignment(COLUMN_AUTHOR, HORIZONTAL_ALIGNMENT_LEFT);
262
plugin_list->set_column_title_alignment(COLUMN_EDIT, HORIZONTAL_ALIGNMENT_LEFT);
263
plugin_list->set_column_expand(COLUMN_PADDING_LEFT, false);
264
plugin_list->set_column_expand(COLUMN_STATUS, false);
265
plugin_list->set_column_expand(COLUMN_NAME, true);
266
plugin_list->set_column_expand(COLUMN_VERSION, false);
267
plugin_list->set_column_expand(COLUMN_AUTHOR, false);
268
plugin_list->set_column_expand(COLUMN_EDIT, false);
269
plugin_list->set_column_expand(COLUMN_PADDING_RIGHT, false);
270
plugin_list->set_column_clip_content(COLUMN_STATUS, true);
271
plugin_list->set_column_clip_content(COLUMN_NAME, true);
272
plugin_list->set_column_clip_content(COLUMN_VERSION, true);
273
plugin_list->set_column_clip_content(COLUMN_AUTHOR, true);
274
plugin_list->set_column_clip_content(COLUMN_EDIT, true);
275
plugin_list->set_column_custom_minimum_width(COLUMN_PADDING_LEFT, 10 * EDSCALE);
276
plugin_list->set_column_custom_minimum_width(COLUMN_STATUS, 80 * EDSCALE);
277
plugin_list->set_column_custom_minimum_width(COLUMN_VERSION, 100 * EDSCALE);
278
plugin_list->set_column_custom_minimum_width(COLUMN_AUTHOR, 250 * EDSCALE);
279
plugin_list->set_column_custom_minimum_width(COLUMN_EDIT, 40 * EDSCALE);
280
plugin_list->set_column_custom_minimum_width(COLUMN_PADDING_RIGHT, 10 * EDSCALE);
281
plugin_list->set_hide_root(true);
282
plugin_list->connect("item_edited", callable_mp(this, &EditorPluginSettings::_plugin_activity_changed), CONNECT_DEFERRED);
283
284
VBoxContainer *mc = memnew(VBoxContainer);
285
mc->add_child(plugin_list);
286
mc->set_v_size_flags(SIZE_EXPAND_FILL);
287
mc->set_h_size_flags(SIZE_EXPAND_FILL);
288
289
add_child(mc);
290
}
291
292