2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* editor_plugin_settings.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2016-02-28 02:10:44 +00:00
|
|
|
#include "editor_plugin_settings.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/config_file.h"
|
2023-04-12 19:02:28 +00:00
|
|
|
#include "core/io/dir_access.h"
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/main_loop.h"
|
2022-02-12 01:46:22 +00:00
|
|
|
#include "editor/editor_node.h"
|
2024-02-13 23:18:23 +00:00
|
|
|
#include "editor/editor_string_names.h"
|
2024-01-15 12:14:55 +00:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/gui/margin_container.h"
|
2022-07-31 18:14:15 +00:00
|
|
|
#include "scene/gui/tree.h"
|
2016-02-28 02:10:44 +00:00
|
|
|
|
|
|
|
void EditorPluginSettings::_notification(int p_what) {
|
2022-02-15 23:52:32 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
|
|
|
|
update_plugins();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Node::NOTIFICATION_READY: {
|
2020-12-06 15:09:18 +00:00
|
|
|
plugin_config_dialog->connect("plugin_ready", callable_mp(EditorNode::get_singleton(), &EditorNode::_on_plugin_ready));
|
2021-09-18 07:33:18 +00:00
|
|
|
plugin_list->connect("button_clicked", callable_mp(this, &EditorPluginSettings::_cell_button_pressed));
|
2022-02-15 23:52:32 +00:00
|
|
|
} break;
|
2016-02-28 02:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPluginSettings::update_plugins() {
|
|
|
|
plugin_list->clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = true;
|
2016-02-28 02:10:44 +00:00
|
|
|
TreeItem *root = plugin_list->create_item();
|
|
|
|
|
2020-11-21 01:30:02 +00:00
|
|
|
Vector<String> plugins = _get_plugins("res://addons");
|
2016-02-28 02:10:44 +00:00
|
|
|
plugins.sort();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < plugins.size(); i++) {
|
2024-02-13 23:18:23 +00:00
|
|
|
Ref<ConfigFile> cfg;
|
|
|
|
cfg.instantiate();
|
2023-11-18 22:40:56 +00:00
|
|
|
const String &path = plugins[i];
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2024-02-13 23:18:23 +00:00
|
|
|
Error err = cfg->load(path);
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2024-02-13 23:18:23 +00:00
|
|
|
if (err != OK) {
|
|
|
|
WARN_PRINT("Can't load plugin config at: " + path);
|
2016-02-28 02:10:44 +00:00
|
|
|
} else {
|
2024-02-13 23:18:23 +00:00
|
|
|
Vector<String> missing_keys;
|
|
|
|
for (const String required_key : { "name", "author", "version", "description", "script" }) {
|
|
|
|
if (!cfg->has_section_key("plugin", required_key)) {
|
|
|
|
missing_keys.append("\"plugin/" + required_key + "\"");
|
|
|
|
}
|
2019-08-30 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:18:23 +00:00
|
|
|
if (!missing_keys.is_empty()) {
|
|
|
|
WARN_PRINT(vformat("Plugin config at \"%s\" is missing the following keys: %s", path, String(",").join(missing_keys)));
|
|
|
|
} else {
|
|
|
|
String name = cfg->get_value("plugin", "name");
|
|
|
|
String author = cfg->get_value("plugin", "author");
|
|
|
|
String version = cfg->get_value("plugin", "version");
|
|
|
|
String description = cfg->get_value("plugin", "description");
|
|
|
|
String scr = cfg->get_value("plugin", "script");
|
|
|
|
|
|
|
|
bool is_enabled = EditorNode::get_singleton()->is_addon_plugin_enabled(path);
|
|
|
|
Color disabled_color = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
|
2019-08-30 18:56:31 +00:00
|
|
|
|
2023-07-06 06:45:07 +00:00
|
|
|
const PackedInt32Array boundaries = TS->string_get_word_breaks(description, "", 80);
|
|
|
|
String wrapped_description;
|
|
|
|
|
|
|
|
for (int j = 0; j < boundaries.size(); j += 2) {
|
|
|
|
const int start = boundaries[j];
|
|
|
|
const int end = boundaries[j + 1];
|
|
|
|
wrapped_description += "\n" + description.substr(start, end - start + 1).rstrip("\n");
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:56:31 +00:00
|
|
|
TreeItem *item = plugin_list->create_item(root);
|
2024-02-13 23:18:23 +00:00
|
|
|
item->set_text(COLUMN_NAME, name);
|
|
|
|
if (!is_enabled) {
|
|
|
|
item->set_custom_color(COLUMN_NAME, disabled_color);
|
|
|
|
}
|
|
|
|
item->set_tooltip_text(COLUMN_NAME, vformat(TTR("Name: %s\nPath: %s\nMain Script: %s\n\n%s"), name, path, scr, wrapped_description));
|
|
|
|
item->set_metadata(COLUMN_NAME, path);
|
|
|
|
item->set_text(COLUMN_VERSION, version);
|
|
|
|
item->set_custom_font(COLUMN_VERSION, get_theme_font("source", EditorStringName(EditorFonts)));
|
|
|
|
item->set_metadata(COLUMN_VERSION, scr);
|
|
|
|
item->set_text(COLUMN_AUTHOR, author);
|
|
|
|
item->set_metadata(COLUMN_AUTHOR, description);
|
|
|
|
item->set_cell_mode(COLUMN_STATUS, TreeItem::CELL_MODE_CHECK);
|
|
|
|
item->set_text(COLUMN_STATUS, TTR("On"));
|
|
|
|
item->set_checked(COLUMN_STATUS, is_enabled);
|
|
|
|
item->set_editable(COLUMN_STATUS, true);
|
|
|
|
item->add_button(COLUMN_EDIT, get_editor_theme_icon(SNAME("Edit")), BUTTON_PLUGIN_EDIT, false, TTR("Edit Plugin"));
|
2016-02-28 02:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = false;
|
2016-02-28 02:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPluginSettings::_plugin_activity_changed() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (updating) {
|
2016-02-28 02:10:44 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TreeItem *ti = plugin_list->get_edited();
|
2023-09-09 15:24:40 +00:00
|
|
|
ERR_FAIL_NULL(ti);
|
2024-02-13 23:18:23 +00:00
|
|
|
bool checked = ti->is_checked(COLUMN_STATUS);
|
|
|
|
String name = ti->get_metadata(COLUMN_NAME);
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2024-02-13 23:18:23 +00:00
|
|
|
EditorNode::get_singleton()->set_addon_plugin_enabled(name, checked, true);
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2024-02-13 23:18:23 +00:00
|
|
|
bool is_enabled = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2024-02-13 23:18:23 +00:00
|
|
|
if (is_enabled != checked) {
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = true;
|
2024-02-13 23:18:23 +00:00
|
|
|
ti->set_checked(COLUMN_STATUS, is_enabled);
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = false;
|
2016-02-28 02:10:44 +00:00
|
|
|
}
|
2024-02-25 16:05:12 +00:00
|
|
|
if (is_enabled) {
|
|
|
|
ti->clear_custom_color(COLUMN_NAME);
|
|
|
|
} else {
|
|
|
|
ti->set_custom_color(COLUMN_NAME, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
|
|
|
|
}
|
2016-02-28 02:10:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 15:34:22 +00:00
|
|
|
void EditorPluginSettings::_create_clicked() {
|
|
|
|
plugin_config_dialog->config("");
|
|
|
|
plugin_config_dialog->popup_centered();
|
|
|
|
}
|
|
|
|
|
2021-09-18 07:33:18 +00:00
|
|
|
void EditorPluginSettings::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
|
|
|
|
if (p_button != MouseButton::LEFT) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-19 15:34:22 +00:00
|
|
|
TreeItem *item = Object::cast_to<TreeItem>(p_item);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!item) {
|
2018-07-19 15:34:22 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-07-19 15:34:22 +00:00
|
|
|
if (p_id == BUTTON_PLUGIN_EDIT) {
|
2024-02-13 23:18:23 +00:00
|
|
|
if (p_column == COLUMN_EDIT) {
|
|
|
|
String dir = item->get_metadata(COLUMN_NAME);
|
2020-11-21 01:30:02 +00:00
|
|
|
plugin_config_dialog->config(dir);
|
2018-07-19 15:34:22 +00:00
|
|
|
plugin_config_dialog->popup_centered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 01:30:02 +00:00
|
|
|
Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) {
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
2020-11-21 01:30:02 +00:00
|
|
|
Error err = da->change_dir(p_dir);
|
|
|
|
if (err != OK) {
|
|
|
|
return Vector<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> plugins;
|
|
|
|
da->list_dir_begin();
|
2021-12-09 09:42:46 +00:00
|
|
|
for (String path = da->get_next(); !path.is_empty(); path = da->get_next()) {
|
2020-11-21 01:30:02 +00:00
|
|
|
if (path[0] == '.' || !da->current_is_dir()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-30 00:34:01 +00:00
|
|
|
const String full_path = p_dir.path_join(path);
|
|
|
|
const String plugin_config = full_path.path_join("plugin.cfg");
|
2020-11-21 01:30:02 +00:00
|
|
|
if (FileAccess::exists(plugin_config)) {
|
|
|
|
plugins.push_back(plugin_config);
|
|
|
|
} else {
|
|
|
|
plugins.append_array(_get_plugins(full_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
da->list_dir_end();
|
|
|
|
return plugins;
|
|
|
|
}
|
|
|
|
|
2016-02-28 02:10:44 +00:00
|
|
|
void EditorPluginSettings::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorPluginSettings::EditorPluginSettings() {
|
2022-06-08 15:52:19 +00:00
|
|
|
ProjectSettings::get_singleton()->add_hidden_prefix("editor_plugins/");
|
|
|
|
|
2018-07-19 15:34:22 +00:00
|
|
|
plugin_config_dialog = memnew(PluginConfigDialog);
|
|
|
|
plugin_config_dialog->config("");
|
|
|
|
add_child(plugin_config_dialog);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
HBoxContainer *title_hb = memnew(HBoxContainer);
|
2024-02-13 23:18:23 +00:00
|
|
|
Label *label = memnew(Label(TTR("Installed Plugins:")));
|
|
|
|
label->set_theme_type_variation("HeaderSmall");
|
|
|
|
title_hb->add_child(label);
|
2016-02-28 02:10:44 +00:00
|
|
|
title_hb->add_spacer();
|
2024-02-13 23:18:23 +00:00
|
|
|
Button *create_plugin_button = memnew(Button(TTR("Create New Plugin")));
|
|
|
|
create_plugin_button->connect("pressed", callable_mp(this, &EditorPluginSettings::_create_clicked));
|
|
|
|
title_hb->add_child(create_plugin_button);
|
2016-02-28 02:10:44 +00:00
|
|
|
add_child(title_hb);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
plugin_list = memnew(Tree);
|
2016-02-28 02:10:44 +00:00
|
|
|
plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
|
2024-02-13 23:18:23 +00:00
|
|
|
plugin_list->set_columns(COLUMN_MAX);
|
2016-02-28 02:10:44 +00:00
|
|
|
plugin_list->set_column_titles_visible(true);
|
2024-02-13 23:18:23 +00:00
|
|
|
plugin_list->set_column_title(COLUMN_STATUS, TTR("Enabled"));
|
|
|
|
plugin_list->set_column_title(COLUMN_NAME, TTR("Name"));
|
|
|
|
plugin_list->set_column_title(COLUMN_VERSION, TTR("Version"));
|
|
|
|
plugin_list->set_column_title(COLUMN_AUTHOR, TTR("Author"));
|
|
|
|
plugin_list->set_column_title(COLUMN_EDIT, TTR("Edit"));
|
|
|
|
plugin_list->set_column_title_alignment(COLUMN_STATUS, HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
plugin_list->set_column_title_alignment(COLUMN_NAME, HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
plugin_list->set_column_title_alignment(COLUMN_VERSION, HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
plugin_list->set_column_title_alignment(COLUMN_AUTHOR, HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
plugin_list->set_column_title_alignment(COLUMN_EDIT, HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
plugin_list->set_column_expand(COLUMN_PADDING_LEFT, false);
|
|
|
|
plugin_list->set_column_expand(COLUMN_STATUS, false);
|
|
|
|
plugin_list->set_column_expand(COLUMN_NAME, true);
|
|
|
|
plugin_list->set_column_expand(COLUMN_VERSION, false);
|
|
|
|
plugin_list->set_column_expand(COLUMN_AUTHOR, false);
|
|
|
|
plugin_list->set_column_expand(COLUMN_EDIT, false);
|
|
|
|
plugin_list->set_column_expand(COLUMN_PADDING_RIGHT, false);
|
|
|
|
plugin_list->set_column_clip_content(COLUMN_STATUS, true);
|
|
|
|
plugin_list->set_column_clip_content(COLUMN_NAME, true);
|
|
|
|
plugin_list->set_column_clip_content(COLUMN_VERSION, true);
|
|
|
|
plugin_list->set_column_clip_content(COLUMN_AUTHOR, true);
|
|
|
|
plugin_list->set_column_clip_content(COLUMN_EDIT, true);
|
|
|
|
plugin_list->set_column_custom_minimum_width(COLUMN_PADDING_LEFT, 10 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(COLUMN_STATUS, 80 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(COLUMN_VERSION, 100 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(COLUMN_AUTHOR, 250 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(COLUMN_EDIT, 40 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(COLUMN_PADDING_RIGHT, 10 * EDSCALE);
|
2016-02-28 02:10:44 +00:00
|
|
|
plugin_list->set_hide_root(true);
|
2023-09-06 03:11:07 +00:00
|
|
|
plugin_list->connect("item_edited", callable_mp(this, &EditorPluginSettings::_plugin_activity_changed), CONNECT_DEFERRED);
|
2016-02-28 02:10:44 +00:00
|
|
|
|
2017-08-08 02:55:24 +00:00
|
|
|
VBoxContainer *mc = memnew(VBoxContainer);
|
2016-02-28 02:10:44 +00:00
|
|
|
mc->add_child(plugin_list);
|
|
|
|
mc->set_v_size_flags(SIZE_EXPAND_FILL);
|
2017-08-08 02:55:24 +00:00
|
|
|
mc->set_h_size_flags(SIZE_EXPAND_FILL);
|
2016-02-28 02:10:44 +00:00
|
|
|
|
|
|
|
add_child(mc);
|
|
|
|
}
|