editor_plugin_settings.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "core/os/main_loop.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_scale.h"
  38. #include "scene/gui/margin_container.h"
  39. #include "scene/gui/tree.h"
  40. void EditorPluginSettings::_notification(int p_what) {
  41. switch (p_what) {
  42. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  43. update_plugins();
  44. } break;
  45. case Node::NOTIFICATION_READY: {
  46. plugin_config_dialog->connect("plugin_ready", callable_mp(EditorNode::get_singleton(), &EditorNode::_on_plugin_ready));
  47. plugin_list->connect("button_clicked", callable_mp(this, &EditorPluginSettings::_cell_button_pressed));
  48. } break;
  49. }
  50. }
  51. void EditorPluginSettings::update_plugins() {
  52. plugin_list->clear();
  53. updating = true;
  54. TreeItem *root = plugin_list->create_item();
  55. Vector<String> plugins = _get_plugins("res://addons");
  56. plugins.sort();
  57. for (int i = 0; i < plugins.size(); i++) {
  58. Ref<ConfigFile> cf;
  59. cf.instantiate();
  60. const String path = plugins[i];
  61. Error err2 = cf->load(path);
  62. if (err2 != OK) {
  63. WARN_PRINT("Can't load plugin config: " + path);
  64. } else {
  65. bool key_missing = false;
  66. if (!cf->has_section_key("plugin", "name")) {
  67. WARN_PRINT("Plugin config misses \"plugin/name\" key: " + path);
  68. key_missing = true;
  69. }
  70. if (!cf->has_section_key("plugin", "author")) {
  71. WARN_PRINT("Plugin config misses \"plugin/author\" key: " + path);
  72. key_missing = true;
  73. }
  74. if (!cf->has_section_key("plugin", "version")) {
  75. WARN_PRINT("Plugin config misses \"plugin/version\" key: " + path);
  76. key_missing = true;
  77. }
  78. if (!cf->has_section_key("plugin", "description")) {
  79. WARN_PRINT("Plugin config misses \"plugin/description\" key: " + path);
  80. key_missing = true;
  81. }
  82. if (!cf->has_section_key("plugin", "script")) {
  83. WARN_PRINT("Plugin config misses \"plugin/script\" key: " + path);
  84. key_missing = true;
  85. }
  86. if (!key_missing) {
  87. String name = cf->get_value("plugin", "name");
  88. String author = cf->get_value("plugin", "author");
  89. String version = cf->get_value("plugin", "version");
  90. String description = cf->get_value("plugin", "description");
  91. String scr = cf->get_value("plugin", "script");
  92. const PackedInt32Array boundaries = TS->string_get_word_breaks(description, "", 80);
  93. String wrapped_description;
  94. for (int j = 0; j < boundaries.size(); j += 2) {
  95. const int start = boundaries[j];
  96. const int end = boundaries[j + 1];
  97. wrapped_description += "\n" + description.substr(start, end - start + 1).rstrip("\n");
  98. }
  99. TreeItem *item = plugin_list->create_item(root);
  100. item->set_text(0, name);
  101. item->set_tooltip_text(0, TTR("Name:") + " " + name + "\n" + TTR("Path:") + " " + path + "\n" + TTR("Main Script:") + " " + scr + "\n" + TTR("Description:") + " " + wrapped_description);
  102. item->set_metadata(0, path);
  103. item->set_text(1, version);
  104. item->set_metadata(1, scr);
  105. item->set_text(2, author);
  106. item->set_metadata(2, description);
  107. item->set_cell_mode(3, TreeItem::CELL_MODE_CHECK);
  108. item->set_text(3, TTR("Enable"));
  109. bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(path);
  110. item->set_checked(3, is_active);
  111. item->set_editable(3, true);
  112. item->add_button(4, get_editor_theme_icon(SNAME("Edit")), BUTTON_PLUGIN_EDIT, false, TTR("Edit Plugin"));
  113. }
  114. }
  115. }
  116. updating = false;
  117. }
  118. void EditorPluginSettings::_plugin_activity_changed() {
  119. if (updating) {
  120. return;
  121. }
  122. TreeItem *ti = plugin_list->get_edited();
  123. ERR_FAIL_NULL(ti);
  124. bool active = ti->is_checked(3);
  125. String name = ti->get_metadata(0);
  126. EditorNode::get_singleton()->set_addon_plugin_enabled(name, active, true);
  127. bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
  128. if (is_active != active) {
  129. updating = true;
  130. ti->set_checked(3, is_active);
  131. updating = false;
  132. }
  133. }
  134. void EditorPluginSettings::_create_clicked() {
  135. plugin_config_dialog->config("");
  136. plugin_config_dialog->popup_centered();
  137. }
  138. void EditorPluginSettings::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  139. if (p_button != MouseButton::LEFT) {
  140. return;
  141. }
  142. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  143. if (!item) {
  144. return;
  145. }
  146. if (p_id == BUTTON_PLUGIN_EDIT) {
  147. if (p_column == 4) {
  148. String dir = item->get_metadata(0);
  149. plugin_config_dialog->config(dir);
  150. plugin_config_dialog->popup_centered();
  151. }
  152. }
  153. }
  154. Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) {
  155. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  156. Error err = da->change_dir(p_dir);
  157. if (err != OK) {
  158. return Vector<String>();
  159. }
  160. Vector<String> plugins;
  161. da->list_dir_begin();
  162. for (String path = da->get_next(); !path.is_empty(); path = da->get_next()) {
  163. if (path[0] == '.' || !da->current_is_dir()) {
  164. continue;
  165. }
  166. const String full_path = p_dir.path_join(path);
  167. const String plugin_config = full_path.path_join("plugin.cfg");
  168. if (FileAccess::exists(plugin_config)) {
  169. plugins.push_back(plugin_config);
  170. } else {
  171. plugins.append_array(_get_plugins(full_path));
  172. }
  173. }
  174. da->list_dir_end();
  175. return plugins;
  176. }
  177. void EditorPluginSettings::_bind_methods() {
  178. }
  179. EditorPluginSettings::EditorPluginSettings() {
  180. ProjectSettings::get_singleton()->add_hidden_prefix("editor_plugins/");
  181. plugin_config_dialog = memnew(PluginConfigDialog);
  182. plugin_config_dialog->config("");
  183. add_child(plugin_config_dialog);
  184. HBoxContainer *title_hb = memnew(HBoxContainer);
  185. Label *l = memnew(Label(TTR("Installed Plugins:")));
  186. l->set_theme_type_variation("HeaderSmall");
  187. title_hb->add_child(l);
  188. title_hb->add_spacer();
  189. Button *create_plugin = memnew(Button(TTR("Create New Plugin")));
  190. create_plugin->connect("pressed", callable_mp(this, &EditorPluginSettings::_create_clicked));
  191. title_hb->add_child(create_plugin);
  192. add_child(title_hb);
  193. plugin_list = memnew(Tree);
  194. plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
  195. plugin_list->set_columns(5);
  196. plugin_list->set_column_titles_visible(true);
  197. plugin_list->set_column_title(0, TTR("Name"));
  198. plugin_list->set_column_title(1, TTR("Version"));
  199. plugin_list->set_column_title(2, TTR("Author"));
  200. plugin_list->set_column_title(3, TTR("Status"));
  201. plugin_list->set_column_title(4, TTR("Edit"));
  202. plugin_list->set_column_expand(0, true);
  203. plugin_list->set_column_clip_content(0, true);
  204. plugin_list->set_column_expand(1, false);
  205. plugin_list->set_column_clip_content(1, true);
  206. plugin_list->set_column_expand(2, false);
  207. plugin_list->set_column_clip_content(2, true);
  208. plugin_list->set_column_expand(3, false);
  209. plugin_list->set_column_clip_content(3, true);
  210. plugin_list->set_column_expand(4, false);
  211. plugin_list->set_column_clip_content(4, true);
  212. plugin_list->set_column_custom_minimum_width(1, 100 * EDSCALE);
  213. plugin_list->set_column_custom_minimum_width(2, 250 * EDSCALE);
  214. plugin_list->set_column_custom_minimum_width(3, 80 * EDSCALE);
  215. plugin_list->set_column_custom_minimum_width(4, 40 * EDSCALE);
  216. plugin_list->set_hide_root(true);
  217. plugin_list->connect("item_edited", callable_mp(this, &EditorPluginSettings::_plugin_activity_changed), CONNECT_DEFERRED);
  218. VBoxContainer *mc = memnew(VBoxContainer);
  219. mc->add_child(plugin_list);
  220. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  221. mc->set_h_size_flags(SIZE_EXPAND_FILL);
  222. add_child(mc);
  223. }