editor_plugin_settings.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**************************************************************************/
  2. /* editor_plugin_settings.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. #include "editor_plugin_settings.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/config_file.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/gui/margin_container.h"
  39. #include "scene/gui/separator.h"
  40. #include "scene/gui/texture_rect.h"
  41. #include "scene/gui/tree.h"
  42. void EditorPluginSettings::_notification(int p_what) {
  43. switch (p_what) {
  44. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  45. update_plugins();
  46. } break;
  47. case NOTIFICATION_READY: {
  48. plugin_config_dialog->connect("plugin_ready", callable_mp(EditorNode::get_singleton(), &EditorNode::_on_plugin_ready));
  49. plugin_list->connect("button_clicked", callable_mp(this, &EditorPluginSettings::_cell_button_pressed));
  50. } break;
  51. case NOTIFICATION_TRANSLATION_CHANGED: {
  52. if (plugin_list->get_root()) {
  53. update_plugins();
  54. }
  55. } break;
  56. case NOTIFICATION_THEME_CHANGED: {
  57. if (Engine::get_singleton()->is_recovery_mode_hint()) {
  58. recovery_mode_icon->set_texture(get_editor_theme_icon(SNAME("NodeWarning")));
  59. }
  60. } break;
  61. }
  62. }
  63. void EditorPluginSettings::update_plugins() {
  64. plugin_list->clear();
  65. updating = true;
  66. TreeItem *root = plugin_list->create_item();
  67. Vector<String> plugins = _get_plugins("res://addons");
  68. plugins.sort();
  69. for (int i = 0; i < plugins.size(); i++) {
  70. Ref<ConfigFile> cfg;
  71. cfg.instantiate();
  72. const String &path = plugins[i];
  73. Error err = cfg->load(path);
  74. if (err != OK) {
  75. WARN_PRINT("Can't load plugin config at: " + path);
  76. } else {
  77. Vector<String> missing_keys;
  78. for (const String required_key : { "name", "author", "version", "description", "script" }) {
  79. if (!cfg->has_section_key("plugin", required_key)) {
  80. missing_keys.append("\"plugin/" + required_key + "\"");
  81. }
  82. }
  83. if (!missing_keys.is_empty()) {
  84. WARN_PRINT(vformat("Plugin config at \"%s\" is missing the following keys: %s", path, String(",").join(missing_keys)));
  85. } else {
  86. String name = cfg->get_value("plugin", "name");
  87. String author = cfg->get_value("plugin", "author");
  88. String version = cfg->get_value("plugin", "version");
  89. String description = cfg->get_value("plugin", "description");
  90. String scr = cfg->get_value("plugin", "script");
  91. bool is_enabled = EditorNode::get_singleton()->is_addon_plugin_enabled(path);
  92. Color disabled_color = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
  93. const PackedInt32Array boundaries = TS->string_get_word_breaks(description, "", 80);
  94. String wrapped_description;
  95. for (int j = 0; j < boundaries.size(); j += 2) {
  96. const int start = boundaries[j];
  97. const int end = boundaries[j + 1];
  98. wrapped_description += "\n" + description.substr(start, end - start + 1).rstrip("\n");
  99. }
  100. TreeItem *item = plugin_list->create_item(root);
  101. item->set_text(COLUMN_NAME, name);
  102. if (!is_enabled) {
  103. item->set_custom_color(COLUMN_NAME, disabled_color);
  104. }
  105. item->set_tooltip_text(COLUMN_NAME, vformat(TTR("Name: %s\nPath: %s\nMain Script: %s\n\n%s"), name, path, scr, wrapped_description));
  106. item->set_metadata(COLUMN_NAME, path);
  107. item->set_text(COLUMN_VERSION, version);
  108. item->set_custom_font(COLUMN_VERSION, get_theme_font("source", EditorStringName(EditorFonts)));
  109. item->set_metadata(COLUMN_VERSION, scr);
  110. item->set_text(COLUMN_AUTHOR, author);
  111. item->set_metadata(COLUMN_AUTHOR, description);
  112. item->set_cell_mode(COLUMN_STATUS, TreeItem::CELL_MODE_CHECK);
  113. item->set_text(COLUMN_STATUS, TTRC("On"));
  114. item->set_checked(COLUMN_STATUS, is_enabled);
  115. item->set_editable(COLUMN_STATUS, true);
  116. item->add_button(COLUMN_EDIT, get_editor_theme_icon(SNAME("Edit")), BUTTON_PLUGIN_EDIT, false, TTRC("Edit Plugin"));
  117. }
  118. }
  119. }
  120. updating = false;
  121. }
  122. void EditorPluginSettings::_plugin_activity_changed() {
  123. if (updating) {
  124. return;
  125. }
  126. TreeItem *ti = plugin_list->get_edited();
  127. ERR_FAIL_NULL(ti);
  128. bool checked = ti->is_checked(COLUMN_STATUS);
  129. String name = ti->get_metadata(COLUMN_NAME);
  130. EditorNode::get_singleton()->set_addon_plugin_enabled(name, checked, true);
  131. bool is_enabled = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
  132. if (is_enabled != checked) {
  133. updating = true;
  134. ti->set_checked(COLUMN_STATUS, is_enabled);
  135. updating = false;
  136. }
  137. if (is_enabled) {
  138. ti->clear_custom_color(COLUMN_NAME);
  139. } else {
  140. ti->set_custom_color(COLUMN_NAME, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  141. }
  142. }
  143. void EditorPluginSettings::_create_clicked() {
  144. plugin_config_dialog->config("");
  145. plugin_config_dialog->popup_centered();
  146. }
  147. void EditorPluginSettings::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  148. if (p_button != MouseButton::LEFT) {
  149. return;
  150. }
  151. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  152. if (!item) {
  153. return;
  154. }
  155. if (p_id == BUTTON_PLUGIN_EDIT) {
  156. if (p_column == COLUMN_EDIT) {
  157. String dir = item->get_metadata(COLUMN_NAME);
  158. plugin_config_dialog->config(dir);
  159. plugin_config_dialog->popup_centered();
  160. }
  161. }
  162. }
  163. Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) {
  164. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  165. Error err = da->change_dir(p_dir);
  166. if (err != OK) {
  167. return Vector<String>();
  168. }
  169. Vector<String> plugins;
  170. da->list_dir_begin();
  171. for (String path = da->get_next(); !path.is_empty(); path = da->get_next()) {
  172. if (path[0] == '.' || !da->current_is_dir()) {
  173. continue;
  174. }
  175. const String full_path = p_dir.path_join(path);
  176. const String plugin_config = full_path.path_join("plugin.cfg");
  177. if (FileAccess::exists(plugin_config)) {
  178. plugins.push_back(plugin_config);
  179. } else {
  180. plugins.append_array(_get_plugins(full_path));
  181. }
  182. }
  183. da->list_dir_end();
  184. return plugins;
  185. }
  186. EditorPluginSettings::EditorPluginSettings() {
  187. ProjectSettings::get_singleton()->add_hidden_prefix("editor_plugins/");
  188. plugin_config_dialog = memnew(PluginConfigDialog);
  189. plugin_config_dialog->config("");
  190. add_child(plugin_config_dialog);
  191. if (Engine::get_singleton()->is_recovery_mode_hint()) {
  192. HBoxContainer *c = memnew(HBoxContainer);
  193. add_child(c);
  194. recovery_mode_icon = memnew(TextureRect);
  195. recovery_mode_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  196. c->add_child(recovery_mode_icon);
  197. Label *recovery_mode_label = memnew(Label(TTRC("Recovery mode is enabled. Enabled plugins will not run while this mode is active.")));
  198. recovery_mode_label->set_theme_type_variation("HeaderSmall");
  199. recovery_mode_label->set_h_size_flags(SIZE_EXPAND_FILL);
  200. c->add_child(recovery_mode_label);
  201. HSeparator *sep = memnew(HSeparator);
  202. add_child(sep);
  203. }
  204. HBoxContainer *title_hb = memnew(HBoxContainer);
  205. Label *label = memnew(Label(TTRC("Installed Plugins:")));
  206. label->set_theme_type_variation("HeaderSmall");
  207. title_hb->add_child(label);
  208. title_hb->add_spacer();
  209. Button *create_plugin_button = memnew(Button(TTRC("Create New Plugin")));
  210. create_plugin_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPluginSettings::_create_clicked));
  211. title_hb->add_child(create_plugin_button);
  212. add_child(title_hb);
  213. plugin_list = memnew(Tree);
  214. plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
  215. plugin_list->set_theme_type_variation("TreeTable");
  216. plugin_list->set_hide_folding(true);
  217. plugin_list->set_columns(COLUMN_MAX);
  218. plugin_list->set_column_titles_visible(true);
  219. plugin_list->set_column_title(COLUMN_STATUS, TTRC("Enabled"));
  220. plugin_list->set_column_title(COLUMN_NAME, TTRC("Name"));
  221. plugin_list->set_column_title(COLUMN_VERSION, TTRC("Version"));
  222. plugin_list->set_column_title(COLUMN_AUTHOR, TTRC("Author"));
  223. plugin_list->set_column_title(COLUMN_EDIT, TTRC("Edit"));
  224. plugin_list->set_column_title_alignment(COLUMN_STATUS, HORIZONTAL_ALIGNMENT_LEFT);
  225. plugin_list->set_column_title_alignment(COLUMN_NAME, HORIZONTAL_ALIGNMENT_LEFT);
  226. plugin_list->set_column_title_alignment(COLUMN_VERSION, HORIZONTAL_ALIGNMENT_LEFT);
  227. plugin_list->set_column_title_alignment(COLUMN_AUTHOR, HORIZONTAL_ALIGNMENT_LEFT);
  228. plugin_list->set_column_title_alignment(COLUMN_EDIT, HORIZONTAL_ALIGNMENT_LEFT);
  229. plugin_list->set_column_expand(COLUMN_STATUS, false);
  230. plugin_list->set_column_expand(COLUMN_NAME, true);
  231. plugin_list->set_column_expand(COLUMN_VERSION, false);
  232. plugin_list->set_column_expand(COLUMN_AUTHOR, false);
  233. plugin_list->set_column_expand(COLUMN_EDIT, false);
  234. plugin_list->set_column_clip_content(COLUMN_STATUS, true);
  235. plugin_list->set_column_clip_content(COLUMN_NAME, true);
  236. plugin_list->set_column_clip_content(COLUMN_VERSION, true);
  237. plugin_list->set_column_clip_content(COLUMN_AUTHOR, true);
  238. plugin_list->set_column_clip_content(COLUMN_EDIT, true);
  239. plugin_list->set_column_custom_minimum_width(COLUMN_STATUS, 80 * EDSCALE);
  240. plugin_list->set_column_custom_minimum_width(COLUMN_VERSION, 100 * EDSCALE);
  241. plugin_list->set_column_custom_minimum_width(COLUMN_AUTHOR, 250 * EDSCALE);
  242. plugin_list->set_column_custom_minimum_width(COLUMN_EDIT, 40 * EDSCALE);
  243. plugin_list->set_hide_root(true);
  244. plugin_list->connect("item_edited", callable_mp(this, &EditorPluginSettings::_plugin_activity_changed), CONNECT_DEFERRED);
  245. VBoxContainer *mc = memnew(VBoxContainer);
  246. mc->add_child(plugin_list);
  247. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  248. mc->set_h_size_flags(SIZE_EXPAND_FILL);
  249. add_child(mc);
  250. }