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/editor_plugin.h" |
38 | #include "editor/editor_scale.h" |
39 | #include "editor/gui/editor_validation_panel.h" |
40 | #include "editor/project_settings_editor.h" |
41 | #include "scene/gui/grid_container.h" |
42 | |
43 | void PluginConfigDialog::_clear_fields() { |
44 | name_edit->set_text("" ); |
45 | subfolder_edit->set_text("" ); |
46 | desc_edit->set_text("" ); |
47 | author_edit->set_text("" ); |
48 | version_edit->set_text("" ); |
49 | script_edit->set_text("" ); |
50 | } |
51 | |
52 | void PluginConfigDialog::_on_confirmed() { |
53 | String path = "res://addons/" + _get_subfolder(); |
54 | |
55 | if (!_edit_mode) { |
56 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
57 | if (d.is_null() || d->make_dir_recursive(path) != OK) { |
58 | return; |
59 | } |
60 | } |
61 | |
62 | int lang_idx = script_option_edit->get_selected(); |
63 | String ext = ScriptServer::get_language(lang_idx)->get_extension(); |
64 | String script_name = script_edit->get_text().is_empty() ? _get_subfolder() : script_edit->get_text(); |
65 | if (script_name.get_extension().is_empty()) { |
66 | script_name += "." + ext; |
67 | } |
68 | String script_path = path.path_join(script_name); |
69 | |
70 | Ref<ConfigFile> cf = memnew(ConfigFile); |
71 | cf->set_value("plugin" , "name" , name_edit->get_text()); |
72 | cf->set_value("plugin" , "description" , desc_edit->get_text()); |
73 | cf->set_value("plugin" , "author" , author_edit->get_text()); |
74 | cf->set_value("plugin" , "version" , version_edit->get_text()); |
75 | cf->set_value("plugin" , "script" , script_name); |
76 | |
77 | cf->save(path.path_join("plugin.cfg" )); |
78 | |
79 | if (!_edit_mode) { |
80 | String class_name = script_name.get_basename(); |
81 | String template_content = "" ; |
82 | Vector<ScriptLanguage::ScriptTemplate> templates = ScriptServer::get_language(lang_idx)->get_built_in_templates("EditorPlugin" ); |
83 | if (!templates.is_empty()) { |
84 | template_content = templates[0].content; |
85 | } |
86 | Ref<Script> scr = ScriptServer::get_language(lang_idx)->make_template(template_content, class_name, "EditorPlugin" ); |
87 | scr->set_path(script_path, true); |
88 | ResourceSaver::save(scr); |
89 | |
90 | emit_signal(SNAME("plugin_ready" ), scr.ptr(), active_edit->is_pressed() ? _to_absolute_plugin_path(_get_subfolder()) : "" ); |
91 | } else { |
92 | EditorNode::get_singleton()->get_project_settings()->update_plugins(); |
93 | } |
94 | _clear_fields(); |
95 | } |
96 | |
97 | void PluginConfigDialog::_on_canceled() { |
98 | _clear_fields(); |
99 | } |
100 | |
101 | void PluginConfigDialog::_on_required_text_changed() { |
102 | int lang_idx = script_option_edit->get_selected(); |
103 | String ext = ScriptServer::get_language(lang_idx)->get_extension(); |
104 | |
105 | if (name_edit->get_text().is_empty()) { |
106 | validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank." ), EditorValidationPanel::MSG_ERROR); |
107 | } |
108 | if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with("." )) { |
109 | validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)." ), ext), EditorValidationPanel::MSG_ERROR); |
110 | } |
111 | if (subfolder_edit->is_visible()) { |
112 | if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) { |
113 | validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name." ), EditorValidationPanel::MSG_ERROR); |
114 | } else { |
115 | String path = "res://addons/" + _get_subfolder(); |
116 | if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode. |
117 | validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists." ), EditorValidationPanel::MSG_ERROR); |
118 | } |
119 | } |
120 | } else { |
121 | validation_panel->set_message(MSG_ID_SUBFOLDER, "" , EditorValidationPanel::MSG_OK); |
122 | } |
123 | } |
124 | |
125 | String PluginConfigDialog::_get_subfolder() { |
126 | return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace(" " , "_" ).to_lower() : subfolder_edit->get_text(); |
127 | } |
128 | |
129 | String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) { |
130 | return "res://addons/" + p_plugin_name + "/plugin.cfg" ; |
131 | } |
132 | |
133 | void PluginConfigDialog::_notification(int p_what) { |
134 | switch (p_what) { |
135 | case NOTIFICATION_VISIBILITY_CHANGED: { |
136 | if (is_visible()) { |
137 | name_edit->grab_focus(); |
138 | } |
139 | } break; |
140 | |
141 | case NOTIFICATION_READY: { |
142 | connect("confirmed" , callable_mp(this, &PluginConfigDialog::_on_confirmed)); |
143 | get_cancel_button()->connect("pressed" , callable_mp(this, &PluginConfigDialog::_on_canceled)); |
144 | } break; |
145 | } |
146 | } |
147 | |
148 | void PluginConfigDialog::config(const String &p_config_path) { |
149 | if (p_config_path.length()) { |
150 | Ref<ConfigFile> cf = memnew(ConfigFile); |
151 | Error err = cf->load(p_config_path); |
152 | ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'." ); |
153 | |
154 | name_edit->set_text(cf->get_value("plugin" , "name" , "" )); |
155 | subfolder_edit->set_text(p_config_path.get_base_dir().get_basename().get_file()); |
156 | desc_edit->set_text(cf->get_value("plugin" , "description" , "" )); |
157 | author_edit->set_text(cf->get_value("plugin" , "author" , "" )); |
158 | version_edit->set_text(cf->get_value("plugin" , "version" , "" )); |
159 | script_edit->set_text(cf->get_value("plugin" , "script" , "" )); |
160 | |
161 | _edit_mode = true; |
162 | active_edit->hide(); |
163 | Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->hide(); |
164 | subfolder_edit->hide(); |
165 | Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->hide(); |
166 | set_title(TTR("Edit a Plugin" )); |
167 | } else { |
168 | _clear_fields(); |
169 | _edit_mode = false; |
170 | active_edit->show(); |
171 | Object::cast_to<Label>(active_edit->get_parent()->get_child(active_edit->get_index() - 1))->show(); |
172 | subfolder_edit->show(); |
173 | Object::cast_to<Label>(subfolder_edit->get_parent()->get_child(subfolder_edit->get_index() - 1))->show(); |
174 | set_title(TTR("Create a Plugin" )); |
175 | } |
176 | validation_panel->update(); |
177 | |
178 | get_ok_button()->set_disabled(!_edit_mode); |
179 | set_ok_button_text(_edit_mode ? TTR("Update" ) : TTR("Create" )); |
180 | } |
181 | |
182 | void PluginConfigDialog::_bind_methods() { |
183 | ADD_SIGNAL(MethodInfo("plugin_ready" , PropertyInfo(Variant::STRING, "script_path" , PROPERTY_HINT_NONE, "" ), PropertyInfo(Variant::STRING, "activate_name" ))); |
184 | } |
185 | |
186 | PluginConfigDialog::PluginConfigDialog() { |
187 | get_ok_button()->set_disabled(true); |
188 | set_hide_on_ok(true); |
189 | |
190 | VBoxContainer *vbox = memnew(VBoxContainer); |
191 | vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
192 | vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
193 | add_child(vbox); |
194 | |
195 | GridContainer *grid = memnew(GridContainer); |
196 | grid->set_columns(2); |
197 | grid->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
198 | vbox->add_child(grid); |
199 | |
200 | // Plugin Name |
201 | Label *name_lb = memnew(Label); |
202 | name_lb->set_text(TTR("Plugin Name:" )); |
203 | name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
204 | grid->add_child(name_lb); |
205 | |
206 | name_edit = memnew(LineEdit); |
207 | name_edit->set_placeholder("MyPlugin" ); |
208 | name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins." )); |
209 | name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
210 | grid->add_child(name_edit); |
211 | |
212 | // Subfolder |
213 | Label *subfolder_lb = memnew(Label); |
214 | subfolder_lb->set_text(TTR("Subfolder:" )); |
215 | subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
216 | grid->add_child(subfolder_lb); |
217 | |
218 | subfolder_edit = memnew(LineEdit); |
219 | subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin" ); |
220 | 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`." )); |
221 | subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
222 | grid->add_child(subfolder_edit); |
223 | |
224 | // Description |
225 | Label *desc_lb = memnew(Label); |
226 | desc_lb->set_text(TTR("Description:" )); |
227 | desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
228 | grid->add_child(desc_lb); |
229 | |
230 | desc_edit = memnew(TextEdit); |
231 | 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." )); |
232 | desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE); |
233 | desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY); |
234 | desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
235 | desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
236 | grid->add_child(desc_edit); |
237 | |
238 | // Author |
239 | Label *author_lb = memnew(Label); |
240 | author_lb->set_text(TTR("Author:" )); |
241 | author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
242 | grid->add_child(author_lb); |
243 | |
244 | author_edit = memnew(LineEdit); |
245 | author_edit->set_placeholder("Godette" ); |
246 | author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name." )); |
247 | author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
248 | grid->add_child(author_edit); |
249 | |
250 | // Version |
251 | Label *version_lb = memnew(Label); |
252 | version_lb->set_text(TTR("Version:" )); |
253 | version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
254 | grid->add_child(version_lb); |
255 | |
256 | version_edit = memnew(LineEdit); |
257 | version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only." )); |
258 | version_edit->set_placeholder("1.0" ); |
259 | version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
260 | grid->add_child(version_edit); |
261 | |
262 | // Language dropdown |
263 | Label *script_option_lb = memnew(Label); |
264 | script_option_lb->set_text(TTR("Language:" )); |
265 | script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
266 | grid->add_child(script_option_lb); |
267 | |
268 | script_option_edit = memnew(OptionButton); |
269 | 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." )); |
270 | int default_lang = 0; |
271 | for (int i = 0; i < ScriptServer::get_language_count(); i++) { |
272 | ScriptLanguage *lang = ScriptServer::get_language(i); |
273 | script_option_edit->add_item(lang->get_name()); |
274 | if (lang->get_name() == "GDScript" ) { |
275 | default_lang = i; |
276 | } |
277 | } |
278 | script_option_edit->select(default_lang); |
279 | grid->add_child(script_option_edit); |
280 | |
281 | // Plugin Script Name |
282 | Label *script_lb = memnew(Label); |
283 | script_lb->set_text(TTR("Script Name:" )); |
284 | script_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
285 | grid->add_child(script_lb); |
286 | |
287 | script_edit = memnew(LineEdit); |
288 | script_edit->set_tooltip_text(TTR("Optional. The path to the script (relative to the add-on folder). If left empty, will default to \"plugin.gd\"." )); |
289 | script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd" ); |
290 | script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
291 | grid->add_child(script_edit); |
292 | |
293 | // Activate now checkbox |
294 | // TODO Make this option work better with languages like C#. Right now, it does not work because the C# project must be compiled first. |
295 | Label *active_lb = memnew(Label); |
296 | active_lb->set_text(TTR("Activate now?" )); |
297 | active_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
298 | grid->add_child(active_lb); |
299 | |
300 | active_edit = memnew(CheckBox); |
301 | active_edit->set_pressed(true); |
302 | grid->add_child(active_edit); |
303 | |
304 | Control *spacing = memnew(Control); |
305 | vbox->add_child(spacing); |
306 | spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE)); |
307 | |
308 | validation_panel = memnew(EditorValidationPanel); |
309 | vbox->add_child(validation_panel); |
310 | validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid." )); |
311 | validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid." )); |
312 | validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid." )); |
313 | validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed)); |
314 | validation_panel->set_accept_button(get_ok_button()); |
315 | |
316 | script_option_edit->connect("item_selected" , callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1)); |
317 | name_edit->connect("text_changed" , callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1)); |
318 | subfolder_edit->connect("text_changed" , callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1)); |
319 | script_edit->connect("text_changed" , callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1)); |
320 | } |
321 | |
322 | PluginConfigDialog::~PluginConfigDialog() { |
323 | } |
324 | |