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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*************************************************************************/
/* plugin_config_dialog.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "plugin_config_dialog.h"
#include "core/io/config_file.h"
#include "core/io/dir_access.h"
#include "editor/editor_node.h"
#include "editor/editor_plugin.h"
#include "editor/editor_scale.h"
#include "editor/project_settings_editor.h"
#include "scene/gui/grid_container.h"
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_GDSCRIPT_ENABLED
#include "modules/gdscript/gdscript.h"
#endif
void PluginConfigDialog::_clear_fields() {
name_edit->set_text("");
subfolder_edit->set_text("");
desc_edit->set_text("");
author_edit->set_text("");
version_edit->set_text("");
script_edit->set_text("");
}
void PluginConfigDialog::_on_confirmed() {
String path = "res://addons/" + subfolder_edit->get_text();
if (!_edit_mode) {
DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (!d || d->make_dir_recursive(path) != OK) {
return;
}
}
Ref<ConfigFile> cf = memnew(ConfigFile);
cf->set_value("plugin", "name", name_edit->get_text());
cf->set_value("plugin", "description", desc_edit->get_text());
cf->set_value("plugin", "author", author_edit->get_text());
cf->set_value("plugin", "version", version_edit->get_text());
cf->set_value("plugin", "script", script_edit->get_text());
cf->save(path.plus_file("plugin.cfg"));
if (!_edit_mode) {
int lang_idx = script_option_edit->get_selected();
String lang_name = ScriptServer::get_language(lang_idx)->get_name();
Ref<Script> script;
// TODO Use script templates. Right now, this code won't add the 'tool' annotation to other languages.
// TODO Better support script languages with named classes (has_named_classes).
// FIXME: It's hacky to have hardcoded access to the GDScript module here.
// The editor code should not have to know what languages are enabled.
#ifdef MODULE_GDSCRIPT_ENABLED
if (lang_name == GDScriptLanguage::get_singleton()->get_name()) {
// Hard-coded GDScript template to keep usability until we use script templates.
Ref<Script> gdscript = memnew(GDScript);
gdscript->set_source_code(
"@tool\n"
"extends EditorPlugin\n"
"\n"
"\n"
"func _enter_tree()%VOID_RETURN%:\n"
"%TS%pass\n"
"\n"
"\n"
"func _exit_tree()%VOID_RETURN%:\n"
"%TS%pass\n");
GDScriptLanguage::get_singleton()->make_template("", "", gdscript);
String script_path = path.plus_file(script_edit->get_text());
gdscript->set_path(script_path);
ResourceSaver::save(script_path, gdscript);
script = gdscript;
} else {
#endif
String script_path = path.plus_file(script_edit->get_text());
String class_name = script_path.get_file().get_basename();
script = ScriptServer::get_language(lang_idx)->get_template(class_name, "EditorPlugin");
script->set_path(script_path);
ResourceSaver::save(script_path, script);
#ifdef MODULE_GDSCRIPT_ENABLED
}
#endif
emit_signal("plugin_ready", script.operator->(), active_edit->is_pressed() ? _to_absolute_plugin_path(subfolder_edit->get_text()) : "");
} else {
EditorNode::get_singleton()->get_project_settings()->update_plugins();
}
_clear_fields();
}
void PluginConfigDialog::_on_cancelled() {
_clear_fields();
}
void PluginConfigDialog::_on_required_text_changed(const String &) {
int lang_idx = script_option_edit->get_selected();
String ext = ScriptServer::get_language(lang_idx)->get_extension();
Ref<Texture2D> valid_icon = get_theme_icon("StatusSuccess", "EditorIcons");
Ref<Texture2D> invalid_icon = get_theme_icon("StatusWarning", "EditorIcons");
// Set variables to assume all is valid
bool is_valid = true;
name_validation->set_texture(valid_icon);
subfolder_validation->set_texture(valid_icon);
script_validation->set_texture(valid_icon);
name_validation->set_tooltip("");
subfolder_validation->set_tooltip("");
script_validation->set_tooltip("");
// Change valid status to invalid depending on conditions.
Vector<String> errors;
if (name_edit->get_text().is_empty()) {
is_valid = false;
name_validation->set_texture(invalid_icon);
name_validation->set_tooltip(TTR("Plugin name cannot not be blank."));
}
if (script_edit->get_text().get_extension() != ext) {
is_valid = false;
script_validation->set_texture(invalid_icon);
script_validation->set_tooltip(vformat(TTR("Script extension must match chosen langauge extension (.%s)."), ext));
}
if (script_edit->get_text().get_basename().is_empty()) {
is_valid = false;
script_validation->set_texture(invalid_icon);
script_validation->set_tooltip(TTR("Script name cannot not be blank."));
}
if (subfolder_edit->get_text().is_empty()) {
is_valid = false;
subfolder_validation->set_texture(invalid_icon);
subfolder_validation->set_tooltip(TTR("Subfolder cannot be blank."));
} else {
DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
String path = "res://addons/" + subfolder_edit->get_text();
if (dir->dir_exists(path) && !_edit_mode) { // Only show this error if in "create" mode.
is_valid = false;
subfolder_validation->set_texture(invalid_icon);
subfolder_validation->set_tooltip(TTR("Subfolder cannot be one which already exists."));
}
}
get_ok_button()->set_disabled(!is_valid);
}
String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {
return "res://addons/" + p_plugin_name + "/plugin.cfg";
}
void PluginConfigDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_visible()) {
name_edit->grab_focus();
}
} break;
case NOTIFICATION_READY: {
connect("confirmed", callable_mp(this, &PluginConfigDialog::_on_confirmed));
get_cancel_button()->connect("pressed", callable_mp(this, &PluginConfigDialog::_on_cancelled));
} break;
}
}
void PluginConfigDialog::config(const String &p_config_path) {
if (p_config_path.length()) {
Ref<ConfigFile> cf = memnew(ConfigFile);
Error err = cf->load(p_config_path);
ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");
name_edit->set_text(cf->get_value("plugin", "name", ""));
subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file());
desc_edit->set_text(cf->get_value("plugin", "description", ""));
author_edit->set_text(cf->get_value("plugin", "author", ""));
version_edit->set_text(cf->get_value("plugin", "version", ""));
script_edit->set_text(cf->get_value("plugin", "script", ""));
_edit_mode = true;
active_edit->hide();
Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 2))->hide();
subfolder_edit->hide();
Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 2))->hide();
set_title(TTR("Edit a Plugin"));
} else {
_clear_fields();
_edit_mode = false;
active_edit->show();
Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 2))->show();
subfolder_edit->show();
Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 2))->show();
set_title(TTR("Create a Plugin"));
}
// Simulate text changing so the errors populate.
_on_required_text_changed("");
get_ok_button()->set_disabled(!_edit_mode);
get_ok_button()->set_text(_edit_mode ? TTR("Update") : TTR("Create"));
}
void PluginConfigDialog::_bind_methods() {
ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));
}
PluginConfigDialog::PluginConfigDialog() {
get_ok_button()->set_disabled(true);
set_hide_on_ok(true);
VBoxContainer *vbox = memnew(VBoxContainer);
vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
add_child(vbox);
GridContainer *grid = memnew(GridContainer);
grid->set_columns(3);
vbox->add_child(grid);
// Plugin Name
Label *name_lb = memnew(Label);
name_lb->set_text(TTR("Plugin Name:"));
grid->add_child(name_lb);
name_validation = memnew(TextureRect);
name_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
grid->add_child(name_validation);
name_edit = memnew(LineEdit);
name_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
name_edit->set_placeholder("MyPlugin");
grid->add_child(name_edit);
// Subfolder
Label *subfolder_lb = memnew(Label);
subfolder_lb->set_text(TTR("Subfolder:"));
grid->add_child(subfolder_lb);
subfolder_validation = memnew(TextureRect);
subfolder_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
grid->add_child(subfolder_validation);
subfolder_edit = memnew(LineEdit);
subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");
subfolder_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
grid->add_child(subfolder_edit);
// Description
Label *desc_lb = memnew(Label);
desc_lb->set_text(TTR("Description:"));
grid->add_child(desc_lb);
Control *desc_spacer = memnew(Control);
grid->add_child(desc_spacer);
desc_edit = memnew(TextEdit);
desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);
desc_edit->set_wrap_enabled(true);
grid->add_child(desc_edit);
// Author
Label *author_lb = memnew(Label);
author_lb->set_text(TTR("Author:"));
grid->add_child(author_lb);
Control *author_spacer = memnew(Control);
grid->add_child(author_spacer);
author_edit = memnew(LineEdit);
author_edit->set_placeholder("Godette");
grid->add_child(author_edit);
// Version
Label *version_lb = memnew(Label);
version_lb->set_text(TTR("Version:"));
grid->add_child(version_lb);
Control *version_spacer = memnew(Control);
grid->add_child(version_spacer);
version_edit = memnew(LineEdit);
version_edit->set_placeholder("1.0");
grid->add_child(version_edit);
// Language dropdown
Label *script_option_lb = memnew(Label);
script_option_lb->set_text(TTR("Language:"));
grid->add_child(script_option_lb);
Control *script_opt_spacer = memnew(Control);
grid->add_child(script_opt_spacer);
script_option_edit = memnew(OptionButton);
int default_lang = 0;
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
ScriptLanguage *lang = ScriptServer::get_language(i);
script_option_edit->add_item(lang->get_name());
#ifdef MODULE_GDSCRIPT_ENABLED
if (lang == GDScriptLanguage::get_singleton()) {
default_lang = i;
}
#endif
}
script_option_edit->select(default_lang);
grid->add_child(script_option_edit);
// Plugin Script Name
Label *script_lb = memnew(Label);
script_lb->set_text(TTR("Script Name:"));
grid->add_child(script_lb);
script_validation = memnew(TextureRect);
script_validation->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
grid->add_child(script_validation);
script_edit = memnew(LineEdit);
script_edit->connect("text_changed", callable_mp(this, &PluginConfigDialog::_on_required_text_changed));
script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");
grid->add_child(script_edit);
// Activate now checkbox
// TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first.
Label *active_lb = memnew(Label);
active_lb->set_text(TTR("Activate now?"));
grid->add_child(active_lb);
Control *active_spacer = memnew(Control);
grid->add_child(active_spacer);
active_edit = memnew(CheckBox);
active_edit->set_pressed(true);
grid->add_child(active_edit);
}
PluginConfigDialog::~PluginConfigDialog() {
}
|