editor_plugin_settings.cpp 11 KB

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