Path: blob/master/editor/plugins/plugin_config_dialog.cpp
20844 views
/**************************************************************************/1/* plugin_config_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "plugin_config_dialog.h"3132#include "core/io/config_file.h"33#include "core/io/dir_access.h"34#include "core/object/script_language.h"35#include "editor/editor_node.h"36#include "editor/file_system/editor_file_system.h"37#include "editor/gui/editor_validation_panel.h"38#include "editor/plugins/editor_plugin.h"39#include "editor/settings/project_settings_editor.h"40#include "editor/themes/editor_scale.h"41#include "scene/gui/grid_container.h"4243void PluginConfigDialog::_clear_fields() {44name_edit->set_text("");45subfolder_edit->set_text("");46desc_edit->set_text("");47author_edit->set_text("");48version_edit->set_text("");49script_edit->set_text("");50}5152void PluginConfigDialog::_on_confirmed() {53String path = "res://addons/" + _get_subfolder();5455if (!_edit_mode) {56Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);57if (d.is_null() || d->make_dir_recursive(path) != OK) {58return;59}60}61// Create the plugin.cfg file.62Ref<ConfigFile> cf = memnew(ConfigFile);63cf->load(path.path_join("plugin.cfg"));64cf->set_value("plugin", "name", name_edit->get_text());65cf->set_value("plugin", "description", desc_edit->get_text());66cf->set_value("plugin", "author", author_edit->get_text());67cf->set_value("plugin", "version", version_edit->get_text());68// Language-specific settings.69int lang_index = script_option_edit->get_selected();70_create_script_for_plugin(path, cf, lang_index);71// Save and inform the editor.72cf->save(path.path_join("plugin.cfg"));73EditorNode::get_singleton()->get_project_settings()->update_plugins();74EditorFileSystem::get_singleton()->scan();75_clear_fields();76}7778void PluginConfigDialog::_create_script_for_plugin(const String &p_plugin_path, Ref<ConfigFile> p_config_file, int p_script_lang_index) {79ScriptLanguage *language = ScriptServer::get_language(p_script_lang_index);80ERR_FAIL_COND(language == nullptr);81String ext = language->get_extension();82String script_name = script_edit->get_text().is_empty() ? _get_subfolder() : script_edit->get_text();83if (script_name.get_extension() != ext) {84script_name += "." + ext;85}86String script_path = p_plugin_path.path_join(script_name);87p_config_file->set_value("plugin", "script", script_name);88// If the requested script does not exist, create it.89if (!_edit_mode && !FileAccess::exists(script_path)) {90String class_name = script_name.get_basename();91String template_content = "";92Vector<ScriptLanguage::ScriptTemplate> templates = language->get_built_in_templates("EditorPlugin");93if (!templates.is_empty()) {94template_content = templates[0].content;95}96Ref<Script> scr = language->make_template(template_content, class_name, "EditorPlugin");97scr->set_path(script_path, true);98ResourceSaver::save(scr);99p_config_file->save(p_plugin_path.path_join("plugin.cfg"));100}101}102103void PluginConfigDialog::_on_canceled() {104_clear_fields();105}106107void PluginConfigDialog::_on_required_text_changed() {108if (name_edit->get_text().is_empty()) {109validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank."), EditorValidationPanel::MSG_ERROR);110}111if (subfolder_edit->is_visible()) {112if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {113validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name."), EditorValidationPanel::MSG_ERROR);114} else {115String path = "res://addons/" + _get_subfolder();116if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.117validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists."), EditorValidationPanel::MSG_ERROR);118}119}120} else {121validation_panel->set_message(MSG_ID_SUBFOLDER, "", EditorValidationPanel::MSG_OK);122}123// Language and script validation.124int lang_idx = script_option_edit->get_selected();125ScriptLanguage *language = ScriptServer::get_language(lang_idx);126if (language == nullptr) {127return;128}129String ext = language->get_extension();130if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {131validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)."), ext), EditorValidationPanel::MSG_ERROR);132}133if (language->get_name() == "GDScript") {134validation_panel->set_message(MSG_ID_ENABLE_WARNINGS, TTR("Consider enabling GDScript warnings for this plugin by adding an entry for it to the project setting Debug > GDScript > Warnings > Directory Rules."), EditorValidationPanel::MSG_INFO);135}136}137138String PluginConfigDialog::_get_subfolder() {139return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace_char(' ', '_').to_lower() : subfolder_edit->get_text();140}141142String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {143return "res://addons/" + p_plugin_name + "/plugin.cfg";144}145146void PluginConfigDialog::_notification(int p_what) {147switch (p_what) {148case NOTIFICATION_VISIBILITY_CHANGED: {149if (is_visible()) {150name_edit->grab_focus();151}152} break;153154case NOTIFICATION_READY: {155connect(SceneStringName(confirmed), callable_mp(this, &PluginConfigDialog::_on_confirmed));156get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &PluginConfigDialog::_on_canceled));157} break;158}159}160161void PluginConfigDialog::config(const String &p_config_path) {162if (!p_config_path.is_empty()) {163Ref<ConfigFile> cf = memnew(ConfigFile);164Error err = cf->load(p_config_path);165ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");166167name_edit->set_text(cf->get_value("plugin", "name", ""));168subfolder_edit->set_text(p_config_path.get_base_dir().get_file());169desc_edit->set_text(cf->get_value("plugin", "description", ""));170author_edit->set_text(cf->get_value("plugin", "author", ""));171version_edit->set_text(cf->get_value("plugin", "version", ""));172script_edit->set_text(cf->get_value("plugin", "script", ""));173174_edit_mode = true;175set_title(TTR("Edit a Plugin"));176} else {177_clear_fields();178_edit_mode = false;179set_title(TTR("Create a Plugin"));180}181182for (Control *control : plugin_edit_hidden_controls) {183control->set_visible(!_edit_mode);184}185186validation_panel->update();187188get_ok_button()->set_disabled(!_edit_mode);189set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));190}191192void PluginConfigDialog::_bind_methods() {193ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));194}195196PluginConfigDialog::PluginConfigDialog() {197get_ok_button()->set_disabled(true);198set_hide_on_ok(true);199200VBoxContainer *vbox = memnew(VBoxContainer);201vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);202vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);203add_child(vbox);204205GridContainer *grid = memnew(GridContainer);206grid->set_columns(2);207grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);208vbox->add_child(grid);209210// Plugin Name211Label *name_lb = memnew(Label);212name_lb->set_text(TTR("Plugin Name:"));213name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);214grid->add_child(name_lb);215216name_edit = memnew(LineEdit);217name_edit->set_placeholder("MyPlugin");218name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins."));219name_edit->set_accessibility_name(TTRC("Plugin Name:"));220name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);221grid->add_child(name_edit);222223// Subfolder224Label *subfolder_lb = memnew(Label);225subfolder_lb->set_text(TTR("Subfolder:"));226subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);227grid->add_child(subfolder_lb);228plugin_edit_hidden_controls.push_back(subfolder_lb);229230subfolder_edit = memnew(LineEdit);231subfolder_edit->set_placeholder(U"\"my_plugin\" → res://addons/my_plugin");232subfolder_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`."));233subfolder_edit->set_accessibility_name(TTRC("Subfolder:"));234subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);235grid->add_child(subfolder_edit);236plugin_edit_hidden_controls.push_back(subfolder_edit);237238// Description239Label *desc_lb = memnew(Label);240desc_lb->set_text(TTR("Description:"));241desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);242grid->add_child(desc_lb);243244desc_edit = memnew(TextEdit);245desc_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."));246desc_edit->set_accessibility_name(TTRC("Description:"));247desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);248desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);249desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);250desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);251grid->add_child(desc_edit);252253// Author254Label *author_lb = memnew(Label);255author_lb->set_text(TTR("Author:"));256author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);257grid->add_child(author_lb);258259author_edit = memnew(LineEdit);260author_edit->set_placeholder("Godette");261author_edit->set_accessibility_name(TTRC("Author:"));262author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name."));263author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);264grid->add_child(author_edit);265266// Version267Label *version_lb = memnew(Label);268version_lb->set_text(TTR("Version:"));269version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);270grid->add_child(version_lb);271272version_edit = memnew(LineEdit);273version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only."));274version_edit->set_placeholder("1.0");275version_edit->set_accessibility_name(TTRC("Version:"));276version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);277grid->add_child(version_edit);278279// Language dropdown280Label *script_option_lb = memnew(Label);281script_option_lb->set_text(TTR("Language:"));282script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);283grid->add_child(script_option_lb);284285script_option_edit = memnew(OptionButton);286script_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."));287script_option_edit->set_accessibility_name(TTRC("Language:"));288int default_lang = 0;289for (int i = 0; i < ScriptServer::get_language_count(); i++) {290ScriptLanguage *lang = ScriptServer::get_language(i);291script_option_edit->add_item(lang->get_name());292if (lang->get_name() == "GDScript") {293default_lang = i;294}295}296script_option_edit->select(default_lang);297grid->add_child(script_option_edit);298299// Plugin Script Name300Label *script_name_label = memnew(Label);301script_name_label->set_text(TTR("Script Name:"));302script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);303grid->add_child(script_name_label);304305script_edit = memnew(LineEdit);306script_edit->set_tooltip_text(TTR("Optional. The name of the script file. If left empty, will default to the subfolder name."));307script_edit->set_placeholder(U"\"plugin.gd\" → res://addons/my_plugin/plugin.gd");308script_edit->set_accessibility_name(TTRC("Script Name:"));309script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);310grid->add_child(script_edit);311312Control *spacing = memnew(Control);313vbox->add_child(spacing);314spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));315316validation_panel = memnew(EditorValidationPanel);317vbox->add_child(validation_panel);318validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid."));319validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid."));320validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid."));321validation_panel->add_line(MSG_ID_ACTIVE, "");322validation_panel->add_line(MSG_ID_ENABLE_WARNINGS, "");323validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed));324validation_panel->set_accept_button(get_ok_button());325326script_option_edit->connect(SceneStringName(item_selected), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));327name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));328subfolder_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));329script_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));330}331332333