Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/plugins/plugin_config_dialog.cpp
9902 views
1
/**************************************************************************/
2
/* plugin_config_dialog.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 "plugin_config_dialog.h"
32
33
#include "core/io/config_file.h"
34
#include "core/io/dir_access.h"
35
#include "core/object/script_language.h"
36
#include "editor/editor_node.h"
37
#include "editor/file_system/editor_file_system.h"
38
#include "editor/gui/editor_validation_panel.h"
39
#include "editor/plugins/editor_plugin.h"
40
#include "editor/settings/project_settings_editor.h"
41
#include "editor/themes/editor_scale.h"
42
#include "scene/gui/grid_container.h"
43
44
void PluginConfigDialog::_clear_fields() {
45
name_edit->set_text("");
46
subfolder_edit->set_text("");
47
desc_edit->set_text("");
48
author_edit->set_text("");
49
version_edit->set_text("");
50
script_edit->set_text("");
51
}
52
53
void PluginConfigDialog::_on_confirmed() {
54
String path = "res://addons/" + _get_subfolder();
55
56
if (!_edit_mode) {
57
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
58
if (d.is_null() || d->make_dir_recursive(path) != OK) {
59
return;
60
}
61
}
62
// Create the plugin.cfg file.
63
Ref<ConfigFile> cf = memnew(ConfigFile);
64
cf->load(path.path_join("plugin.cfg"));
65
cf->set_value("plugin", "name", name_edit->get_text());
66
cf->set_value("plugin", "description", desc_edit->get_text());
67
cf->set_value("plugin", "author", author_edit->get_text());
68
cf->set_value("plugin", "version", version_edit->get_text());
69
// Language-specific settings.
70
int lang_index = script_option_edit->get_selected();
71
_create_script_for_plugin(path, cf, lang_index);
72
// Save and inform the editor.
73
cf->save(path.path_join("plugin.cfg"));
74
EditorNode::get_singleton()->get_project_settings()->update_plugins();
75
EditorFileSystem::get_singleton()->scan();
76
_clear_fields();
77
}
78
79
void PluginConfigDialog::_create_script_for_plugin(const String &p_plugin_path, Ref<ConfigFile> p_config_file, int p_script_lang_index) {
80
ScriptLanguage *language = ScriptServer::get_language(p_script_lang_index);
81
ERR_FAIL_COND(language == nullptr);
82
String ext = language->get_extension();
83
String script_name = script_edit->get_text().is_empty() ? _get_subfolder() : script_edit->get_text();
84
if (script_name.get_extension() != ext) {
85
script_name += "." + ext;
86
}
87
String script_path = p_plugin_path.path_join(script_name);
88
p_config_file->set_value("plugin", "script", script_name);
89
// If the requested script does not exist, create it.
90
if (!_edit_mode && !FileAccess::exists(script_path)) {
91
String class_name = script_name.get_basename();
92
String template_content = "";
93
Vector<ScriptLanguage::ScriptTemplate> templates = language->get_built_in_templates("EditorPlugin");
94
if (!templates.is_empty()) {
95
template_content = templates[0].content;
96
}
97
Ref<Script> scr = language->make_template(template_content, class_name, "EditorPlugin");
98
scr->set_path(script_path, true);
99
ResourceSaver::save(scr);
100
p_config_file->save(p_plugin_path.path_join("plugin.cfg"));
101
emit_signal(SNAME("plugin_ready"), scr.ptr(), active_edit->is_pressed() ? _to_absolute_plugin_path(_get_subfolder()) : "");
102
}
103
}
104
105
void PluginConfigDialog::_on_canceled() {
106
_clear_fields();
107
}
108
109
void PluginConfigDialog::_on_required_text_changed() {
110
if (name_edit->get_text().is_empty()) {
111
validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank."), EditorValidationPanel::MSG_ERROR);
112
}
113
if (subfolder_edit->is_visible()) {
114
if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {
115
validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name."), EditorValidationPanel::MSG_ERROR);
116
} else {
117
String path = "res://addons/" + _get_subfolder();
118
if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.
119
validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists."), EditorValidationPanel::MSG_ERROR);
120
}
121
}
122
} else {
123
validation_panel->set_message(MSG_ID_SUBFOLDER, "", EditorValidationPanel::MSG_OK);
124
}
125
// Language and script validation.
126
int lang_idx = script_option_edit->get_selected();
127
ScriptLanguage *language = ScriptServer::get_language(lang_idx);
128
if (language == nullptr) {
129
return;
130
}
131
String ext = language->get_extension();
132
if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {
133
validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)."), ext), EditorValidationPanel::MSG_ERROR);
134
}
135
if (active_edit->is_visible()) {
136
if (language->get_name() == "C#") {
137
active_edit->set_pressed(false);
138
active_edit->set_disabled(true);
139
validation_panel->set_message(MSG_ID_ACTIVE, TTR("C# doesn't support activating the plugin on creation because the project must be built first."), EditorValidationPanel::MSG_WARNING);
140
} else {
141
active_edit->set_disabled(false);
142
}
143
}
144
}
145
146
String PluginConfigDialog::_get_subfolder() {
147
return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace_char(' ', '_').to_lower() : subfolder_edit->get_text();
148
}
149
150
String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
151
return "res://addons/" + p_plugin_name + "/plugin.cfg";
152
}
153
154
void PluginConfigDialog::_notification(int p_what) {
155
switch (p_what) {
156
case NOTIFICATION_VISIBILITY_CHANGED: {
157
if (is_visible()) {
158
name_edit->grab_focus();
159
}
160
} break;
161
162
case NOTIFICATION_READY: {
163
connect(SceneStringName(confirmed), callable_mp(this, &PluginConfigDialog::_on_confirmed));
164
get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &PluginConfigDialog::_on_canceled));
165
} break;
166
}
167
}
168
169
void PluginConfigDialog::config(const String &p_config_path) {
170
if (!p_config_path.is_empty()) {
171
Ref<ConfigFile> cf = memnew(ConfigFile);
172
Error err = cf->load(p_config_path);
173
ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
174
175
name_edit->set_text(cf->get_value("plugin", "name", ""));
176
subfolder_edit->set_text(p_config_path.get_base_dir().get_file());
177
desc_edit->set_text(cf->get_value("plugin", "description", ""));
178
author_edit->set_text(cf->get_value("plugin", "author", ""));
179
version_edit->set_text(cf->get_value("plugin", "version", ""));
180
script_edit->set_text(cf->get_value("plugin", "script", ""));
181
182
_edit_mode = true;
183
set_title(TTR("Edit a Plugin"));
184
} else {
185
_clear_fields();
186
_edit_mode = false;
187
set_title(TTR("Create a Plugin"));
188
}
189
190
for (Control *control : plugin_edit_hidden_controls) {
191
control->set_visible(!_edit_mode);
192
}
193
194
validation_panel->update();
195
196
get_ok_button()->set_disabled(!_edit_mode);
197
set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));
198
}
199
200
void PluginConfigDialog::_bind_methods() {
201
ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
202
}
203
204
PluginConfigDialog::PluginConfigDialog() {
205
get_ok_button()->set_disabled(true);
206
set_hide_on_ok(true);
207
208
VBoxContainer *vbox = memnew(VBoxContainer);
209
vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
210
vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
211
add_child(vbox);
212
213
GridContainer *grid = memnew(GridContainer);
214
grid->set_columns(2);
215
grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);
216
vbox->add_child(grid);
217
218
// Plugin Name
219
Label *name_lb = memnew(Label);
220
name_lb->set_text(TTR("Plugin Name:"));
221
name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
222
grid->add_child(name_lb);
223
224
name_edit = memnew(LineEdit);
225
name_edit->set_placeholder("MyPlugin");
226
name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins."));
227
name_edit->set_accessibility_name(TTRC("Plugin Name:"));
228
name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
229
grid->add_child(name_edit);
230
231
// Subfolder
232
Label *subfolder_lb = memnew(Label);
233
subfolder_lb->set_text(TTR("Subfolder:"));
234
subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
235
grid->add_child(subfolder_lb);
236
plugin_edit_hidden_controls.push_back(subfolder_lb);
237
238
subfolder_edit = memnew(LineEdit);
239
subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
240
subfolder_edit->set_tooltip_text(TTR("Optional. The folder name should generally use `snake_case` naming (avoid spaces and special characters).\nIf left empty, the folder will be named after the plugin name converted to `snake_case`."));
241
subfolder_edit->set_accessibility_name(TTRC("Subfolder:"));
242
subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
243
grid->add_child(subfolder_edit);
244
plugin_edit_hidden_controls.push_back(subfolder_edit);
245
246
// Description
247
Label *desc_lb = memnew(Label);
248
desc_lb->set_text(TTR("Description:"));
249
desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
250
grid->add_child(desc_lb);
251
252
desc_edit = memnew(TextEdit);
253
desc_edit->set_tooltip_text(TTR("Optional. This description should be kept relatively short (up to 5 lines).\nIt will display when hovering the plugin in the list of plugins."));
254
desc_edit->set_accessibility_name(TTRC("Description:"));
255
desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
256
desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);
257
desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
258
desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
259
grid->add_child(desc_edit);
260
261
// Author
262
Label *author_lb = memnew(Label);
263
author_lb->set_text(TTR("Author:"));
264
author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
265
grid->add_child(author_lb);
266
267
author_edit = memnew(LineEdit);
268
author_edit->set_placeholder("Godette");
269
author_edit->set_accessibility_name(TTRC("Author:"));
270
author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name."));
271
author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
272
grid->add_child(author_edit);
273
274
// Version
275
Label *version_lb = memnew(Label);
276
version_lb->set_text(TTR("Version:"));
277
version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
278
grid->add_child(version_lb);
279
280
version_edit = memnew(LineEdit);
281
version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only."));
282
version_edit->set_placeholder("1.0");
283
version_edit->set_accessibility_name(TTRC("Version:"));
284
version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
285
grid->add_child(version_edit);
286
287
// Language dropdown
288
Label *script_option_lb = memnew(Label);
289
script_option_lb->set_text(TTR("Language:"));
290
script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
291
grid->add_child(script_option_lb);
292
293
script_option_edit = memnew(OptionButton);
294
script_option_edit->set_tooltip_text(TTR("Required. The scripting language to use for the script.\nNote that a plugin may use several languages at once by adding more scripts to the plugin."));
295
script_option_edit->set_accessibility_name(TTRC("Language:"));
296
int default_lang = 0;
297
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
298
ScriptLanguage *lang = ScriptServer::get_language(i);
299
script_option_edit->add_item(lang->get_name());
300
if (lang->get_name() == "GDScript") {
301
default_lang = i;
302
}
303
}
304
script_option_edit->select(default_lang);
305
grid->add_child(script_option_edit);
306
307
// Plugin Script Name
308
Label *script_name_label = memnew(Label);
309
script_name_label->set_text(TTR("Script Name:"));
310
script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
311
grid->add_child(script_name_label);
312
313
script_edit = memnew(LineEdit);
314
script_edit->set_tooltip_text(TTR("Optional. The name of the script file. If left empty, will default to the subfolder name."));
315
script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
316
script_edit->set_accessibility_name(TTRC("Script Name:"));
317
script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
318
grid->add_child(script_edit);
319
320
// Activate now checkbox
321
Label *active_label = memnew(Label);
322
active_label->set_text(TTR("Activate now?"));
323
active_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
324
grid->add_child(active_label);
325
plugin_edit_hidden_controls.push_back(active_label);
326
327
active_edit = memnew(CheckBox);
328
active_edit->set_pressed(true);
329
active_edit->set_accessibility_name(TTRC("Activate now?"));
330
grid->add_child(active_edit);
331
plugin_edit_hidden_controls.push_back(active_edit);
332
333
Control *spacing = memnew(Control);
334
vbox->add_child(spacing);
335
spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
336
337
validation_panel = memnew(EditorValidationPanel);
338
vbox->add_child(validation_panel);
339
validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid."));
340
validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid."));
341
validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid."));
342
validation_panel->add_line(MSG_ID_ACTIVE, "");
343
validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
344
validation_panel->set_accept_button(get_ok_button());
345
346
script_option_edit->connect(SceneStringName(item_selected), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
347
name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
348
subfolder_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
349
script_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
350
}
351
352