editor_plugin_settings.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* editor_plugin_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/io/config_file.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/main_loop.h"
  34. #include "core/project_settings.h"
  35. #include "editor_node.h"
  36. #include "editor_scale.h"
  37. #include "scene/gui/margin_container.h"
  38. void EditorPluginSettings::_notification(int p_what) {
  39. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  40. update_plugins();
  41. } else if (p_what == Node::NOTIFICATION_READY) {
  42. plugin_config_dialog->connect("plugin_ready", EditorNode::get_singleton(), "_on_plugin_ready");
  43. plugin_list->connect("button_pressed", this, "_cell_button_pressed");
  44. }
  45. }
  46. void EditorPluginSettings::update_plugins() {
  47. plugin_list->clear();
  48. updating = true;
  49. TreeItem *root = plugin_list->create_item();
  50. Vector<String> plugins = _get_plugins("res://addons");
  51. plugins.sort();
  52. for (int i = 0; i < plugins.size(); i++) {
  53. Ref<ConfigFile> cf;
  54. cf.instance();
  55. const String path = plugins[i];
  56. Error err2 = cf->load(path);
  57. if (err2 != OK) {
  58. WARN_PRINTS("Can't load plugin config: " + path);
  59. } else {
  60. bool key_missing = false;
  61. if (!cf->has_section_key("plugin", "name")) {
  62. WARN_PRINTS("Plugin config misses \"plugin/name\" key: " + path);
  63. key_missing = true;
  64. }
  65. if (!cf->has_section_key("plugin", "author")) {
  66. WARN_PRINTS("Plugin config misses \"plugin/author\" key: " + path);
  67. key_missing = true;
  68. }
  69. if (!cf->has_section_key("plugin", "version")) {
  70. WARN_PRINTS("Plugin config misses \"plugin/version\" key: " + path);
  71. key_missing = true;
  72. }
  73. if (!cf->has_section_key("plugin", "description")) {
  74. WARN_PRINTS("Plugin config misses \"plugin/description\" key: " + path);
  75. key_missing = true;
  76. }
  77. if (!cf->has_section_key("plugin", "script")) {
  78. WARN_PRINTS("Plugin config misses \"plugin/script\" key: " + path);
  79. key_missing = true;
  80. }
  81. if (!key_missing) {
  82. String name = cf->get_value("plugin", "name");
  83. String author = cf->get_value("plugin", "author");
  84. String version = cf->get_value("plugin", "version");
  85. String description = cf->get_value("plugin", "description");
  86. String script = cf->get_value("plugin", "script");
  87. TreeItem *item = plugin_list->create_item(root);
  88. item->set_text(0, name);
  89. item->set_tooltip(0, TTR("Name:") + " " + name + "\n" + TTR("Path:") + " " + path + "\n" + TTR("Main Script:") + " " + script + "\n" + TTR("Description:") + " " + description);
  90. item->set_metadata(0, path);
  91. item->set_text(1, version);
  92. item->set_metadata(1, script);
  93. item->set_text(2, author);
  94. item->set_metadata(2, description);
  95. item->set_cell_mode(3, TreeItem::CELL_MODE_CHECK);
  96. item->set_text(3, TTR("Enable"));
  97. bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(path);
  98. item->set_checked(3, is_active);
  99. item->set_editable(3, true);
  100. item->add_button(4, get_icon("Edit", "EditorIcons"), BUTTON_PLUGIN_EDIT, false, TTR("Edit Plugin"));
  101. }
  102. }
  103. }
  104. updating = false;
  105. }
  106. void EditorPluginSettings::_plugin_activity_changed() {
  107. if (updating)
  108. return;
  109. TreeItem *ti = plugin_list->get_edited();
  110. ERR_FAIL_COND(!ti);
  111. bool active = ti->is_checked(3);
  112. String name = ti->get_metadata(0);
  113. EditorNode::get_singleton()->set_addon_plugin_enabled(name, active, true);
  114. bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
  115. if (is_active != active) {
  116. updating = true;
  117. ti->set_checked(3, is_active);
  118. updating = false;
  119. }
  120. }
  121. void EditorPluginSettings::_create_clicked() {
  122. plugin_config_dialog->config("");
  123. plugin_config_dialog->popup_centered();
  124. }
  125. void EditorPluginSettings::_cell_button_pressed(Object *p_item, int p_column, int p_id) {
  126. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  127. if (!item)
  128. return;
  129. if (p_id == BUTTON_PLUGIN_EDIT) {
  130. if (p_column == 4) {
  131. String dir = item->get_metadata(0);
  132. plugin_config_dialog->config(dir);
  133. plugin_config_dialog->popup_centered();
  134. }
  135. }
  136. }
  137. Vector<String> EditorPluginSettings::_get_plugins(const String &p_dir) {
  138. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  139. Error err = da->change_dir(p_dir);
  140. if (err != OK) {
  141. return Vector<String>();
  142. }
  143. Vector<String> plugins;
  144. da->list_dir_begin();
  145. for (String path = da->get_next(); path != String(); path = da->get_next()) {
  146. if (path[0] == '.' || !da->current_is_dir()) {
  147. continue;
  148. }
  149. const String full_path = p_dir.plus_file(path);
  150. const String plugin_config = full_path.plus_file("plugin.cfg");
  151. if (FileAccess::exists(plugin_config)) {
  152. plugins.push_back(plugin_config);
  153. } else {
  154. plugins.append_array(_get_plugins(full_path));
  155. }
  156. }
  157. da->list_dir_end();
  158. return plugins;
  159. }
  160. void EditorPluginSettings::_bind_methods() {
  161. ClassDB::bind_method("update_plugins", &EditorPluginSettings::update_plugins);
  162. ClassDB::bind_method("_create_clicked", &EditorPluginSettings::_create_clicked);
  163. ClassDB::bind_method("_plugin_activity_changed", &EditorPluginSettings::_plugin_activity_changed);
  164. ClassDB::bind_method("_cell_button_pressed", &EditorPluginSettings::_cell_button_pressed);
  165. }
  166. EditorPluginSettings::EditorPluginSettings() {
  167. plugin_config_dialog = memnew(PluginConfigDialog);
  168. plugin_config_dialog->config("");
  169. add_child(plugin_config_dialog);
  170. HBoxContainer *title_hb = memnew(HBoxContainer);
  171. title_hb->add_child(memnew(Label(TTR("Installed Plugins:"))));
  172. title_hb->add_spacer();
  173. create_plugin = memnew(Button(TTR("Create")));
  174. create_plugin->connect("pressed", this, "_create_clicked");
  175. title_hb->add_child(create_plugin);
  176. update_list = memnew(Button(TTR("Update")));
  177. update_list->connect("pressed", this, "update_plugins");
  178. title_hb->add_child(update_list);
  179. add_child(title_hb);
  180. plugin_list = memnew(Tree);
  181. plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
  182. plugin_list->set_columns(5);
  183. plugin_list->set_column_titles_visible(true);
  184. plugin_list->set_column_title(0, TTR("Name:"));
  185. plugin_list->set_column_title(1, TTR("Version:"));
  186. plugin_list->set_column_title(2, TTR("Author:"));
  187. plugin_list->set_column_title(3, TTR("Status:"));
  188. plugin_list->set_column_title(4, TTR("Edit:"));
  189. plugin_list->set_column_expand(0, true);
  190. plugin_list->set_column_expand(1, false);
  191. plugin_list->set_column_expand(2, false);
  192. plugin_list->set_column_expand(3, false);
  193. plugin_list->set_column_expand(4, false);
  194. plugin_list->set_column_min_width(1, 100 * EDSCALE);
  195. plugin_list->set_column_min_width(2, 250 * EDSCALE);
  196. plugin_list->set_column_min_width(3, 80 * EDSCALE);
  197. plugin_list->set_column_min_width(4, 40 * EDSCALE);
  198. plugin_list->set_hide_root(true);
  199. plugin_list->connect("item_edited", this, "_plugin_activity_changed");
  200. VBoxContainer *mc = memnew(VBoxContainer);
  201. mc->add_child(plugin_list);
  202. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  203. mc->set_h_size_flags(SIZE_EXPAND_FILL);
  204. add_child(mc);
  205. updating = false;
  206. }