Path: blob/master/editor/plugins/plugin_config_dialog.cpp
9902 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"));100emit_signal(SNAME("plugin_ready"), scr.ptr(), active_edit->is_pressed() ? _to_absolute_plugin_path(_get_subfolder()) : "");101}102}103104void PluginConfigDialog::_on_canceled() {105_clear_fields();106}107108void PluginConfigDialog::_on_required_text_changed() {109if (name_edit->get_text().is_empty()) {110validation_panel->set_message(MSG_ID_PLUGIN, TTR("Plugin name cannot be blank."), EditorValidationPanel::MSG_ERROR);111}112if (subfolder_edit->is_visible()) {113if (!subfolder_edit->get_text().is_empty() && !subfolder_edit->get_text().is_valid_filename()) {114validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder name is not a valid folder name."), EditorValidationPanel::MSG_ERROR);115} else {116String path = "res://addons/" + _get_subfolder();117if (!_edit_mode && DirAccess::exists(path)) { // Only show this error if in "create" mode.118validation_panel->set_message(MSG_ID_SUBFOLDER, TTR("Subfolder cannot be one which already exists."), EditorValidationPanel::MSG_ERROR);119}120}121} else {122validation_panel->set_message(MSG_ID_SUBFOLDER, "", EditorValidationPanel::MSG_OK);123}124// Language and script validation.125int lang_idx = script_option_edit->get_selected();126ScriptLanguage *language = ScriptServer::get_language(lang_idx);127if (language == nullptr) {128return;129}130String ext = language->get_extension();131if ((!script_edit->get_text().get_extension().is_empty() && script_edit->get_text().get_extension() != ext) || script_edit->get_text().ends_with(".")) {132validation_panel->set_message(MSG_ID_SCRIPT, vformat(TTR("Script extension must match chosen language extension (.%s)."), ext), EditorValidationPanel::MSG_ERROR);133}134if (active_edit->is_visible()) {135if (language->get_name() == "C#") {136active_edit->set_pressed(false);137active_edit->set_disabled(true);138validation_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);139} else {140active_edit->set_disabled(false);141}142}143}144145String PluginConfigDialog::_get_subfolder() {146return subfolder_edit->get_text().is_empty() ? name_edit->get_text().replace_char(' ', '_').to_lower() : subfolder_edit->get_text();147}148149String PluginConfigDialog::_to_absolute_plugin_path(const String &p_plugin_name) {150return "res://addons/" + p_plugin_name + "/plugin.cfg";151}152153void PluginConfigDialog::_notification(int p_what) {154switch (p_what) {155case NOTIFICATION_VISIBILITY_CHANGED: {156if (is_visible()) {157name_edit->grab_focus();158}159} break;160161case NOTIFICATION_READY: {162connect(SceneStringName(confirmed), callable_mp(this, &PluginConfigDialog::_on_confirmed));163get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &PluginConfigDialog::_on_canceled));164} break;165}166}167168void PluginConfigDialog::config(const String &p_config_path) {169if (!p_config_path.is_empty()) {170Ref<ConfigFile> cf = memnew(ConfigFile);171Error err = cf->load(p_config_path);172ERR_FAIL_COND_MSG(err != OK, "Cannot load config file from path '" + p_config_path + "'.");173174name_edit->set_text(cf->get_value("plugin", "name", ""));175subfolder_edit->set_text(p_config_path.get_base_dir().get_file());176desc_edit->set_text(cf->get_value("plugin", "description", ""));177author_edit->set_text(cf->get_value("plugin", "author", ""));178version_edit->set_text(cf->get_value("plugin", "version", ""));179script_edit->set_text(cf->get_value("plugin", "script", ""));180181_edit_mode = true;182set_title(TTR("Edit a Plugin"));183} else {184_clear_fields();185_edit_mode = false;186set_title(TTR("Create a Plugin"));187}188189for (Control *control : plugin_edit_hidden_controls) {190control->set_visible(!_edit_mode);191}192193validation_panel->update();194195get_ok_button()->set_disabled(!_edit_mode);196set_ok_button_text(_edit_mode ? TTR("Update") : TTR("Create"));197}198199void PluginConfigDialog::_bind_methods() {200ADD_SIGNAL(MethodInfo("plugin_ready", PropertyInfo(Variant::STRING, "script_path", PROPERTY_HINT_NONE, ""), PropertyInfo(Variant::STRING, "activate_name")));201}202203PluginConfigDialog::PluginConfigDialog() {204get_ok_button()->set_disabled(true);205set_hide_on_ok(true);206207VBoxContainer *vbox = memnew(VBoxContainer);208vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);209vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);210add_child(vbox);211212GridContainer *grid = memnew(GridContainer);213grid->set_columns(2);214grid->set_v_size_flags(Control::SIZE_EXPAND_FILL);215vbox->add_child(grid);216217// Plugin Name218Label *name_lb = memnew(Label);219name_lb->set_text(TTR("Plugin Name:"));220name_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);221grid->add_child(name_lb);222223name_edit = memnew(LineEdit);224name_edit->set_placeholder("MyPlugin");225name_edit->set_tooltip_text(TTR("Required. This name will be displayed in the list of plugins."));226name_edit->set_accessibility_name(TTRC("Plugin Name:"));227name_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);228grid->add_child(name_edit);229230// Subfolder231Label *subfolder_lb = memnew(Label);232subfolder_lb->set_text(TTR("Subfolder:"));233subfolder_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);234grid->add_child(subfolder_lb);235plugin_edit_hidden_controls.push_back(subfolder_lb);236237subfolder_edit = memnew(LineEdit);238subfolder_edit->set_placeholder("\"my_plugin\" -> res://addons/my_plugin");239subfolder_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`."));240subfolder_edit->set_accessibility_name(TTRC("Subfolder:"));241subfolder_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);242grid->add_child(subfolder_edit);243plugin_edit_hidden_controls.push_back(subfolder_edit);244245// Description246Label *desc_lb = memnew(Label);247desc_lb->set_text(TTR("Description:"));248desc_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);249grid->add_child(desc_lb);250251desc_edit = memnew(TextEdit);252desc_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."));253desc_edit->set_accessibility_name(TTRC("Description:"));254desc_edit->set_custom_minimum_size(Size2(400, 80) * EDSCALE);255desc_edit->set_line_wrapping_mode(TextEdit::LineWrappingMode::LINE_WRAPPING_BOUNDARY);256desc_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);257desc_edit->set_v_size_flags(Control::SIZE_EXPAND_FILL);258grid->add_child(desc_edit);259260// Author261Label *author_lb = memnew(Label);262author_lb->set_text(TTR("Author:"));263author_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);264grid->add_child(author_lb);265266author_edit = memnew(LineEdit);267author_edit->set_placeholder("Godette");268author_edit->set_accessibility_name(TTRC("Author:"));269author_edit->set_tooltip_text(TTR("Optional. The author's username, full name, or organization name."));270author_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);271grid->add_child(author_edit);272273// Version274Label *version_lb = memnew(Label);275version_lb->set_text(TTR("Version:"));276version_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);277grid->add_child(version_lb);278279version_edit = memnew(LineEdit);280version_edit->set_tooltip_text(TTR("Optional. A human-readable version identifier used for informational purposes only."));281version_edit->set_placeholder("1.0");282version_edit->set_accessibility_name(TTRC("Version:"));283version_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);284grid->add_child(version_edit);285286// Language dropdown287Label *script_option_lb = memnew(Label);288script_option_lb->set_text(TTR("Language:"));289script_option_lb->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);290grid->add_child(script_option_lb);291292script_option_edit = memnew(OptionButton);293script_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."));294script_option_edit->set_accessibility_name(TTRC("Language:"));295int default_lang = 0;296for (int i = 0; i < ScriptServer::get_language_count(); i++) {297ScriptLanguage *lang = ScriptServer::get_language(i);298script_option_edit->add_item(lang->get_name());299if (lang->get_name() == "GDScript") {300default_lang = i;301}302}303script_option_edit->select(default_lang);304grid->add_child(script_option_edit);305306// Plugin Script Name307Label *script_name_label = memnew(Label);308script_name_label->set_text(TTR("Script Name:"));309script_name_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);310grid->add_child(script_name_label);311312script_edit = memnew(LineEdit);313script_edit->set_tooltip_text(TTR("Optional. The name of the script file. If left empty, will default to the subfolder name."));314script_edit->set_placeholder("\"plugin.gd\" -> res://addons/my_plugin/plugin.gd");315script_edit->set_accessibility_name(TTRC("Script Name:"));316script_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);317grid->add_child(script_edit);318319// Activate now checkbox320Label *active_label = memnew(Label);321active_label->set_text(TTR("Activate now?"));322active_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);323grid->add_child(active_label);324plugin_edit_hidden_controls.push_back(active_label);325326active_edit = memnew(CheckBox);327active_edit->set_pressed(true);328active_edit->set_accessibility_name(TTRC("Activate now?"));329grid->add_child(active_edit);330plugin_edit_hidden_controls.push_back(active_edit);331332Control *spacing = memnew(Control);333vbox->add_child(spacing);334spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));335336validation_panel = memnew(EditorValidationPanel);337vbox->add_child(validation_panel);338validation_panel->add_line(MSG_ID_PLUGIN, TTR("Plugin name is valid."));339validation_panel->add_line(MSG_ID_SCRIPT, TTR("Script extension is valid."));340validation_panel->add_line(MSG_ID_SUBFOLDER, TTR("Subfolder name is valid."));341validation_panel->add_line(MSG_ID_ACTIVE, "");342validation_panel->set_update_callback(callable_mp(this, &PluginConfigDialog::_on_required_text_changed));343validation_panel->set_accept_button(get_ok_button());344345script_option_edit->connect(SceneStringName(item_selected), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));346name_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));347subfolder_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));348script_edit->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));349}350351352