0.2.0-dev Download ZIP
144 lines ·7.0 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* project_settings_gdextension.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "project_settings_gdextension.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/extension/gdextension_manager.h" |
| 35 | #include "core/io/config_file.h" |
| 36 | #include "core/object/callable_mp.h" |
| 37 | #include "core/os/os.h" |
| 38 | #include "editor/themes/editor_scale.h" |
| 39 | #include "scene/gui/label.h" |
| 40 | #include "scene/gui/tree.h" |
| 41 | |
| 42 | class GDExtensionPluginCreatorBase; |
| 43 | |
| 44 | void ProjectSettingsGDExtension::_notification(int p_what) { |
| 45 | switch (p_what) { |
| 46 | case NOTIFICATION_VISIBILITY_CHANGED: |
| 47 | case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 48 | if (is_visible_in_tree()) { |
| 49 | _update_extension_tree(); |
| 50 | } |
| 51 | } break; |
| 52 | case NOTIFICATION_READY: { |
| 53 | extension_list->connect("button_clicked", callable_mp(this, &ProjectSettingsGDExtension::_cell_button_pressed)); |
| 54 | extension_list->connect("item_activated", callable_mp(this, &ProjectSettingsGDExtension::_on_item_activated)); |
| 55 | } break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void ProjectSettingsGDExtension::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) { |
| 60 | if (p_button != MouseButton::LEFT) { |
| 61 | return; |
| 62 | } |
| 63 | TreeItem *item = Object::cast_to<TreeItem>(p_item); |
| 64 | if (item == nullptr) { |
| 65 | return; |
| 66 | } |
| 67 | if (p_id == COLUMN_RELOAD) { |
| 68 | const String path = item->get_metadata(COLUMN_PATH); |
| 69 | GDExtensionManager::get_singleton()->reload_extension(path); |
| 70 | _update_extension_tree(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void ProjectSettingsGDExtension::_on_item_activated() { |
| 75 | TreeItem *item = extension_list->get_selected(); |
| 76 | if (item == nullptr) { |
| 77 | return; |
| 78 | } |
| 79 | const String global_path = ProjectSettings::get_singleton()->globalize_path(item->get_metadata(COLUMN_PATH)); |
| 80 | OS::get_singleton()->shell_open(global_path); |
| 81 | } |
| 82 | |
| 83 | void ProjectSettingsGDExtension::_update_extension_tree() { |
| 84 | extension_list->clear(); |
| 85 | TreeItem *root = extension_list->create_item(); |
| 86 | GDExtensionManager *gdext_man = GDExtensionManager::get_singleton(); |
| 87 | const Vector<String> extensions = gdext_man->get_loaded_extensions(); |
| 88 | for (const String &extension_path : extensions) { |
| 89 | const Ref<GDExtension> gdextension = gdext_man->get_extension(extension_path); |
| 90 | ERR_CONTINUE(gdextension.is_null()); |
| 91 | TreeItem *item = extension_list->create_item(root); |
| 92 | item->set_text(COLUMN_PATH, extension_path); |
| 93 | Ref<ConfigFile> gdext_config; |
| 94 | gdext_config.instantiate(); |
| 95 | Error err = gdext_config->load(extension_path); |
| 96 | ERR_CONTINUE(err != OK); |
| 97 | item->set_text(COLUMN_MIN_VERSION, gdext_config->get_value("configuration", "compatibility_minimum", "")); |
| 98 | item->set_text(COLUMN_MAX_VERSION, gdext_config->get_value("configuration", "compatibility_maximum", "")); |
| 99 | if (gdextension->is_reloadable()) { |
| 100 | item->add_button(COLUMN_RELOAD, get_editor_theme_icon(SNAME("Reload")), COLUMN_RELOAD, false, TTRC("Reload Extension")); |
| 101 | } |
| 102 | item->set_metadata(COLUMN_PATH, extension_path); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | ProjectSettingsGDExtension::ProjectSettingsGDExtension() { |
| 107 | // Create the title label. |
| 108 | HBoxContainer *title_hb = memnew(HBoxContainer); |
| 109 | Label *label = memnew(Label(TTRC("Installed GDExtensions:"))); |
| 110 | label->set_theme_type_variation("HeaderSmall"); |
| 111 | title_hb->add_child(label); |
| 112 | title_hb->add_spacer(); |
| 113 | add_child(title_hb); |
| 114 | // Create the tree. |
| 115 | extension_list = memnew(Tree); |
| 116 | extension_list->set_v_size_flags(SIZE_EXPAND_FILL); |
| 117 | extension_list->set_hide_root(true); |
| 118 | extension_list->set_theme_type_variation("TreeTable"); |
| 119 | extension_list->set_hide_folding(true); |
| 120 | // Configure tree columns. |
| 121 | extension_list->set_columns(COLUMN_MAX); |
| 122 | extension_list->set_column_titles_visible(true); |
| 123 | extension_list->set_column_title(COLUMN_PATH, TTRC("Path")); |
| 124 | extension_list->set_column_title(COLUMN_MIN_VERSION, TTRC("Min Version")); |
| 125 | extension_list->set_column_title(COLUMN_MAX_VERSION, TTRC("Max Version")); |
| 126 | extension_list->set_column_title(COLUMN_RELOAD, TTRC("Reload")); |
| 127 | extension_list->set_column_title_alignment(COLUMN_PATH, HORIZONTAL_ALIGNMENT_LEFT); |
| 128 | extension_list->set_column_title_alignment(COLUMN_MIN_VERSION, HORIZONTAL_ALIGNMENT_LEFT); |
| 129 | extension_list->set_column_title_alignment(COLUMN_MAX_VERSION, HORIZONTAL_ALIGNMENT_LEFT); |
| 130 | extension_list->set_column_title_alignment(COLUMN_RELOAD, HORIZONTAL_ALIGNMENT_LEFT); |
| 131 | extension_list->set_column_expand(COLUMN_PATH, true); |
| 132 | extension_list->set_column_expand(COLUMN_MIN_VERSION, false); |
| 133 | extension_list->set_column_expand(COLUMN_MAX_VERSION, false); |
| 134 | extension_list->set_column_expand(COLUMN_RELOAD, false); |
| 135 | extension_list->set_column_clip_content(COLUMN_PATH, true); |
| 136 | extension_list->set_column_clip_content(COLUMN_MIN_VERSION, true); |
| 137 | extension_list->set_column_clip_content(COLUMN_MAX_VERSION, true); |
| 138 | extension_list->set_column_clip_content(COLUMN_RELOAD, true); |
| 139 | extension_list->set_column_custom_minimum_width(COLUMN_PATH, 300 * EDSCALE); |
| 140 | extension_list->set_column_custom_minimum_width(COLUMN_MIN_VERSION, 100 * EDSCALE); |
| 141 | extension_list->set_column_custom_minimum_width(COLUMN_MAX_VERSION, 100 * EDSCALE); |
| 142 | extension_list->set_column_custom_minimum_width(COLUMN_RELOAD, 40 * EDSCALE); |
| 143 | add_child(extension_list); |
| 144 | } |