2016-06-18 20:46:12 +08: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-05 07:50:27 +08:00
|
|
|
|
2016-02-28 10:10:44 +08:00
|
|
|
#include "editor_plugin_settings.h"
|
2017-01-16 15:04:19 +08:00
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/io/config_file.h"
|
2023-04-13 03:02:28 +08:00
|
|
|
#include "core/io/dir_access.h"
|
2021-06-11 20:51:48 +08:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/main_loop.h"
|
2022-02-12 09:46:22 +08:00
|
|
|
#include "editor/editor_node.h"
|
|
|
|
#include "editor/editor_scale.h"
|
2017-02-21 11:05:15 +08:00
|
|
|
#include "scene/gui/margin_container.h"
|
2022-08-01 02:14:15 +08:00
|
|
|
#include "scene/gui/tree.h"
|
2016-02-28 10:10:44 +08:00
|
|
|
|
|
|
|
void EditorPluginSettings::_notification(int p_what) {
|
2022-02-16 07:52:32 +08:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
|
|
|
|
update_plugins();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Node::NOTIFICATION_READY: {
|
2020-12-06 23:09:18 +08:00
|
|
|
plugin_config_dialog->connect("plugin_ready", callable_mp(EditorNode::get_singleton(), &EditorNode::_on_plugin_ready));
|
2021-09-18 15:33:18 +08:00
|
|
|
plugin_list->connect("button_clicked", callable_mp(this, &EditorPluginSettings::_cell_button_pressed));
|
2022-02-16 07:52:32 +08:00
|
|
|
} break;
|
2016-02-28 10:10:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPluginSettings::update_plugins() {
|
|
|
|
plugin_list->clear();
|
|
|
|
updating = true;
|
|
|
|
TreeItem *root = plugin_list->create_item();
|
|
|
|
|
2020-11-21 09:30:02 +08:00
|
|
|
Vector<String> plugins = _get_plugins("res://addons");
|
2016-02-28 10:10:44 +08:00
|
|
|
plugins.sort();
|
|
|
|
|
|
|
|
for (int i = 0; i < plugins.size(); i++) {
|
|
|
|
Ref<ConfigFile> cf;
|
2021-06-18 06:03:09 +08:00
|
|
|
cf.instantiate();
|
2020-11-21 09:30:02 +08:00
|
|
|
const String path = plugins[i];
|
2016-02-28 10:10:44 +08:00
|
|
|
|
2019-02-13 04:10:08 +08:00
|
|
|
Error err2 = cf->load(path);
|
2016-02-28 10:10:44 +08:00
|
|
|
|
2019-02-13 04:10:08 +08:00
|
|
|
if (err2 != OK) {
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Can't load plugin config: " + path);
|
2016-02-28 10:10:44 +08:00
|
|
|
} else {
|
2019-08-31 02:56:31 +08:00
|
|
|
bool key_missing = false;
|
2016-02-28 10:10:44 +08:00
|
|
|
|
2019-08-31 02:56:31 +08:00
|
|
|
if (!cf->has_section_key("plugin", "name")) {
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Plugin config misses \"plugin/name\" key: " + path);
|
2019-08-31 02:56:31 +08:00
|
|
|
key_missing = true;
|
|
|
|
}
|
|
|
|
if (!cf->has_section_key("plugin", "author")) {
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Plugin config misses \"plugin/author\" key: " + path);
|
2019-08-31 02:56:31 +08:00
|
|
|
key_missing = true;
|
|
|
|
}
|
|
|
|
if (!cf->has_section_key("plugin", "version")) {
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Plugin config misses \"plugin/version\" key: " + path);
|
2019-08-31 02:56:31 +08:00
|
|
|
key_missing = true;
|
|
|
|
}
|
|
|
|
if (!cf->has_section_key("plugin", "description")) {
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Plugin config misses \"plugin/description\" key: " + path);
|
2019-08-31 02:56:31 +08:00
|
|
|
key_missing = true;
|
|
|
|
}
|
|
|
|
if (!cf->has_section_key("plugin", "script")) {
|
2019-11-07 16:44:15 +08:00
|
|
|
WARN_PRINT("Plugin config misses \"plugin/script\" key: " + path);
|
2019-08-31 02:56:31 +08:00
|
|
|
key_missing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key_missing) {
|
|
|
|
String name = cf->get_value("plugin", "name");
|
|
|
|
String author = cf->get_value("plugin", "author");
|
|
|
|
String version = cf->get_value("plugin", "version");
|
|
|
|
String description = cf->get_value("plugin", "description");
|
2022-09-29 17:53:28 +08:00
|
|
|
String scr = cf->get_value("plugin", "script");
|
2019-08-31 02:56:31 +08:00
|
|
|
|
2023-07-06 14:45:07 +08: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-31 02:56:31 +08:00
|
|
|
TreeItem *item = plugin_list->create_item(root);
|
|
|
|
item->set_text(0, name);
|
2023-07-06 14:45:07 +08:00
|
|
|
item->set_tooltip_text(0, TTR("Name:") + " " + name + "\n" + TTR("Path:") + " " + path + "\n" + TTR("Main Script:") + " " + scr + "\n" + TTR("Description:") + " " + wrapped_description);
|
2020-11-21 09:30:02 +08:00
|
|
|
item->set_metadata(0, path);
|
2019-08-31 02:56:31 +08:00
|
|
|
item->set_text(1, version);
|
2022-09-29 17:53:28 +08:00
|
|
|
item->set_metadata(1, scr);
|
2019-08-31 02:56:31 +08:00
|
|
|
item->set_text(2, author);
|
|
|
|
item->set_metadata(2, description);
|
2020-02-14 00:56:02 +08:00
|
|
|
item->set_cell_mode(3, TreeItem::CELL_MODE_CHECK);
|
|
|
|
item->set_text(3, TTR("Enable"));
|
2020-11-21 09:30:02 +08:00
|
|
|
bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(path);
|
2020-02-14 00:56:02 +08:00
|
|
|
item->set_checked(3, is_active);
|
2019-08-31 02:56:31 +08:00
|
|
|
item->set_editable(3, true);
|
2023-08-13 08:33:39 +08:00
|
|
|
item->add_button(4, get_editor_theme_icon(SNAME("Edit")), BUTTON_PLUGIN_EDIT, false, TTR("Edit Plugin"));
|
2016-02-28 10:10:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorPluginSettings::_plugin_activity_changed() {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (updating) {
|
2016-02-28 10:10:44 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-02-28 10:10:44 +08:00
|
|
|
|
|
|
|
TreeItem *ti = plugin_list->get_edited();
|
2023-09-09 23:24:40 +08:00
|
|
|
ERR_FAIL_NULL(ti);
|
2020-02-14 00:56:02 +08:00
|
|
|
bool active = ti->is_checked(3);
|
2016-02-28 10:10:44 +08:00
|
|
|
String name = ti->get_metadata(0);
|
|
|
|
|
2018-06-11 08:59:53 +08:00
|
|
|
EditorNode::get_singleton()->set_addon_plugin_enabled(name, active, true);
|
2016-02-28 10:10:44 +08:00
|
|
|
|
|
|
|
bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
|
|
|
|
|
|
|
|
if (is_active != active) {
|
|
|
|
updating = true;
|
2020-02-14 00:56:02 +08:00
|
|
|
ti->set_checked(3, is_active);
|
2016-02-28 10:10:44 +08:00
|
|
|
updating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 23:34:22 +08:00
|
|
|
void EditorPluginSettings::_create_clicked() {
|
|
|
|
plugin_config_dialog->config("");
|
|
|
|
plugin_config_dialog->popup_centered();
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:33:18 +08: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 23:34:22 +08:00
|
|
|
TreeItem *item = Object::cast_to<TreeItem>(p_item);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!item) {
|
2018-07-19 23:34:22 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-07-19 23:34:22 +08:00
|
|
|
if (p_id == BUTTON_PLUGIN_EDIT) {
|
|
|
|
if (p_column == 4) {
|
|
|
|
String dir = item->get_metadata(0);
|
2020-11-21 09:30:02 +08:00
|
|
|
plugin_config_dialog->config(dir);
|
2018-07-19 23:34:22 +08:00
|
|
|
plugin_config_dialog->popup_centered();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 09:30:02 +08:00
|
|
|
Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) {
|
2022-03-23 17:08:58 +08:00
|
|
|
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
2020-11-21 09:30:02 +08:00
|
|
|
Error err = da->change_dir(p_dir);
|
|
|
|
if (err != OK) {
|
|
|
|
return Vector<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> plugins;
|
|
|
|
da->list_dir_begin();
|
2021-12-09 17:42:46 +08:00
|
|
|
for (String path = da->get_next(); !path.is_empty(); path = da->get_next()) {
|
2020-11-21 09:30:02 +08:00
|
|
|
if (path[0] == '.' || !da->current_is_dir()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:34:01 +08:00
|
|
|
const String full_path = p_dir.path_join(path);
|
|
|
|
const String plugin_config = full_path.path_join("plugin.cfg");
|
2020-11-21 09:30:02 +08: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 10:10:44 +08:00
|
|
|
void EditorPluginSettings::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorPluginSettings::EditorPluginSettings() {
|
2022-06-08 23:52:19 +08:00
|
|
|
ProjectSettings::get_singleton()->add_hidden_prefix("editor_plugins/");
|
|
|
|
|
2018-07-19 23:34:22 +08:00
|
|
|
plugin_config_dialog = memnew(PluginConfigDialog);
|
|
|
|
plugin_config_dialog->config("");
|
|
|
|
add_child(plugin_config_dialog);
|
|
|
|
|
2016-02-28 10:10:44 +08:00
|
|
|
HBoxContainer *title_hb = memnew(HBoxContainer);
|
2021-07-08 21:29:15 +08:00
|
|
|
Label *l = memnew(Label(TTR("Installed Plugins:")));
|
|
|
|
l->set_theme_type_variation("HeaderSmall");
|
|
|
|
title_hb->add_child(l);
|
2016-02-28 10:10:44 +08:00
|
|
|
title_hb->add_spacer();
|
2022-05-08 05:19:55 +08:00
|
|
|
Button *create_plugin = memnew(Button(TTR("Create New Plugin")));
|
2020-02-22 01:28:45 +08:00
|
|
|
create_plugin->connect("pressed", callable_mp(this, &EditorPluginSettings::_create_clicked));
|
2018-07-19 23:34:22 +08:00
|
|
|
title_hb->add_child(create_plugin);
|
2016-02-28 10:10:44 +08:00
|
|
|
add_child(title_hb);
|
|
|
|
|
|
|
|
plugin_list = memnew(Tree);
|
|
|
|
plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
|
2018-07-19 23:34:22 +08:00
|
|
|
plugin_list->set_columns(5);
|
2016-02-28 10:10:44 +08:00
|
|
|
plugin_list->set_column_titles_visible(true);
|
2021-07-29 21:51:07 +08:00
|
|
|
plugin_list->set_column_title(0, TTR("Name"));
|
|
|
|
plugin_list->set_column_title(1, TTR("Version"));
|
|
|
|
plugin_list->set_column_title(2, TTR("Author"));
|
|
|
|
plugin_list->set_column_title(3, TTR("Status"));
|
|
|
|
plugin_list->set_column_title(4, TTR("Edit"));
|
2016-02-28 10:10:44 +08:00
|
|
|
plugin_list->set_column_expand(0, true);
|
2021-07-04 11:13:28 +08:00
|
|
|
plugin_list->set_column_clip_content(0, true);
|
2016-02-28 10:10:44 +08:00
|
|
|
plugin_list->set_column_expand(1, false);
|
2021-07-04 11:13:28 +08:00
|
|
|
plugin_list->set_column_clip_content(1, true);
|
2016-02-28 10:10:44 +08:00
|
|
|
plugin_list->set_column_expand(2, false);
|
2021-07-04 11:13:28 +08:00
|
|
|
plugin_list->set_column_clip_content(2, true);
|
2016-02-28 10:10:44 +08:00
|
|
|
plugin_list->set_column_expand(3, false);
|
2021-07-04 11:13:28 +08:00
|
|
|
plugin_list->set_column_clip_content(3, true);
|
2018-07-19 23:34:22 +08:00
|
|
|
plugin_list->set_column_expand(4, false);
|
2021-07-04 11:13:28 +08:00
|
|
|
plugin_list->set_column_clip_content(4, true);
|
2021-06-28 21:40:56 +08:00
|
|
|
plugin_list->set_column_custom_minimum_width(1, 100 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(2, 250 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(3, 80 * EDSCALE);
|
|
|
|
plugin_list->set_column_custom_minimum_width(4, 40 * EDSCALE);
|
2016-02-28 10:10:44 +08:00
|
|
|
plugin_list->set_hide_root(true);
|
2023-09-06 11:11:07 +08:00
|
|
|
plugin_list->connect("item_edited", callable_mp(this, &EditorPluginSettings::_plugin_activity_changed), CONNECT_DEFERRED);
|
2016-02-28 10:10:44 +08:00
|
|
|
|
2017-08-08 10:55:24 +08:00
|
|
|
VBoxContainer *mc = memnew(VBoxContainer);
|
2016-02-28 10:10:44 +08:00
|
|
|
mc->add_child(plugin_list);
|
|
|
|
mc->set_v_size_flags(SIZE_EXPAND_FILL);
|
2017-08-08 10:55:24 +08:00
|
|
|
mc->set_h_size_flags(SIZE_EXPAND_FILL);
|
2016-02-28 10:10:44 +08:00
|
|
|
|
|
|
|
add_child(mc);
|
|
|
|
}
|