Path: blob/master/editor/settings/gdextension/project_settings_gdextension.cpp
46047 views
/**************************************************************************/1/* project_settings_gdextension.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 "project_settings_gdextension.h"3132#include "core/config/project_settings.h"33#include "core/extension/gdextension_manager.h"34#include "core/io/config_file.h"35#include "core/object/callable_mp.h"36#include "core/os/os.h"37#include "editor/themes/editor_scale.h"38#include "scene/gui/label.h"39#include "scene/gui/tree.h"4041class GDExtensionPluginCreatorBase;4243void ProjectSettingsGDExtension::_notification(int p_what) {44switch (p_what) {45case NOTIFICATION_VISIBILITY_CHANGED:46case NOTIFICATION_WM_WINDOW_FOCUS_IN: {47if (is_visible_in_tree()) {48_update_extension_tree();49}50} break;51case NOTIFICATION_READY: {52extension_list->connect("button_clicked", callable_mp(this, &ProjectSettingsGDExtension::_cell_button_pressed));53extension_list->connect("item_activated", callable_mp(this, &ProjectSettingsGDExtension::_on_item_activated));54} break;55}56}5758void ProjectSettingsGDExtension::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {59if (p_button != MouseButton::LEFT) {60return;61}62TreeItem *item = Object::cast_to<TreeItem>(p_item);63if (item == nullptr) {64return;65}66if (p_id == COLUMN_RELOAD) {67const String path = item->get_metadata(COLUMN_PATH);68GDExtensionManager::get_singleton()->reload_extension(path);69_update_extension_tree();70}71}7273void ProjectSettingsGDExtension::_on_item_activated() {74TreeItem *item = extension_list->get_selected();75if (item == nullptr) {76return;77}78const String global_path = ProjectSettings::get_singleton()->globalize_path(item->get_metadata(COLUMN_PATH));79OS::get_singleton()->shell_open(global_path);80}8182void ProjectSettingsGDExtension::_update_extension_tree() {83extension_list->clear();84TreeItem *root = extension_list->create_item();85GDExtensionManager *gdext_man = GDExtensionManager::get_singleton();86const Vector<String> extensions = gdext_man->get_loaded_extensions();87for (const String &extension_path : extensions) {88const Ref<GDExtension> gdextension = gdext_man->get_extension(extension_path);89ERR_CONTINUE(gdextension.is_null());90TreeItem *item = extension_list->create_item(root);91item->set_text(COLUMN_PATH, extension_path);92Ref<ConfigFile> gdext_config;93gdext_config.instantiate();94Error err = gdext_config->load(extension_path);95ERR_CONTINUE(err != OK);96item->set_text(COLUMN_MIN_VERSION, gdext_config->get_value("configuration", "compatibility_minimum", ""));97item->set_text(COLUMN_MAX_VERSION, gdext_config->get_value("configuration", "compatibility_maximum", ""));98if (gdextension->is_reloadable()) {99item->add_button(COLUMN_RELOAD, get_editor_theme_icon(SNAME("Reload")), COLUMN_RELOAD, false, TTRC("Reload Extension"));100}101item->set_metadata(COLUMN_PATH, extension_path);102}103}104105ProjectSettingsGDExtension::ProjectSettingsGDExtension() {106// Create the title label.107HBoxContainer *title_hb = memnew(HBoxContainer);108Label *label = memnew(Label(TTRC("Installed GDExtensions:")));109label->set_theme_type_variation("HeaderSmall");110title_hb->add_child(label);111title_hb->add_spacer();112add_child(title_hb);113// Create the tree.114extension_list = memnew(Tree);115extension_list->set_v_size_flags(SIZE_EXPAND_FILL);116extension_list->set_hide_root(true);117extension_list->set_theme_type_variation("TreeTable");118extension_list->set_hide_folding(true);119// Configure tree columns.120extension_list->set_columns(COLUMN_MAX);121extension_list->set_column_titles_visible(true);122extension_list->set_column_title(COLUMN_PATH, TTRC("Path"));123extension_list->set_column_title(COLUMN_MIN_VERSION, TTRC("Min Version"));124extension_list->set_column_title(COLUMN_MAX_VERSION, TTRC("Max Version"));125extension_list->set_column_title(COLUMN_RELOAD, TTRC("Reload"));126extension_list->set_column_title_alignment(COLUMN_PATH, HORIZONTAL_ALIGNMENT_LEFT);127extension_list->set_column_title_alignment(COLUMN_MIN_VERSION, HORIZONTAL_ALIGNMENT_LEFT);128extension_list->set_column_title_alignment(COLUMN_MAX_VERSION, HORIZONTAL_ALIGNMENT_LEFT);129extension_list->set_column_title_alignment(COLUMN_RELOAD, HORIZONTAL_ALIGNMENT_LEFT);130extension_list->set_column_expand(COLUMN_PATH, true);131extension_list->set_column_expand(COLUMN_MIN_VERSION, false);132extension_list->set_column_expand(COLUMN_MAX_VERSION, false);133extension_list->set_column_expand(COLUMN_RELOAD, false);134extension_list->set_column_clip_content(COLUMN_PATH, true);135extension_list->set_column_clip_content(COLUMN_MIN_VERSION, true);136extension_list->set_column_clip_content(COLUMN_MAX_VERSION, true);137extension_list->set_column_clip_content(COLUMN_RELOAD, true);138extension_list->set_column_custom_minimum_width(COLUMN_PATH, 300 * EDSCALE);139extension_list->set_column_custom_minimum_width(COLUMN_MIN_VERSION, 100 * EDSCALE);140extension_list->set_column_custom_minimum_width(COLUMN_MAX_VERSION, 100 * EDSCALE);141extension_list->set_column_custom_minimum_width(COLUMN_RELOAD, 40 * EDSCALE);142add_child(extension_list);143}144145146