editor_plugin_settings.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_node.h"
  32. #include "io/config_file.h"
  33. #include "os/file_access.h"
  34. #include "os/main_loop.h"
  35. #include "project_settings.h"
  36. #include "scene/gui/margin_container.h"
  37. void EditorPluginSettings::_notification(int p_what) {
  38. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
  39. update_plugins();
  40. }
  41. }
  42. void EditorPluginSettings::update_plugins() {
  43. plugin_list->clear();
  44. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  45. Error err = da->change_dir("res://addons");
  46. if (err != OK) {
  47. memdelete(da);
  48. return;
  49. }
  50. updating = true;
  51. TreeItem *root = plugin_list->create_item();
  52. da->list_dir_begin();
  53. String d = da->get_next();
  54. Vector<String> plugins;
  55. while (d != String()) {
  56. bool dir = da->current_is_dir();
  57. String path = "res://addons/" + d + "/plugin.cfg";
  58. if (dir && FileAccess::exists(path)) {
  59. plugins.push_back(d);
  60. }
  61. d = da->get_next();
  62. }
  63. da->list_dir_end();
  64. memdelete(da);
  65. plugins.sort();
  66. Vector<String> active_plugins = ProjectSettings::get_singleton()->get("editor_plugins/enabled");
  67. for (int i = 0; i < plugins.size(); i++) {
  68. Ref<ConfigFile> cf;
  69. cf.instance();
  70. String path = "res://addons/" + plugins[i] + "/plugin.cfg";
  71. Error err = cf->load(path);
  72. if (err != OK) {
  73. WARN_PRINTS("Can't load plugin config: " + path);
  74. } else if (!cf->has_section_key("plugin", "name")) {
  75. WARN_PRINTS("Plugin misses plugin/name: " + path);
  76. } else if (!cf->has_section_key("plugin", "author")) {
  77. WARN_PRINTS("Plugin misses plugin/author: " + path);
  78. } else if (!cf->has_section_key("plugin", "version")) {
  79. WARN_PRINTS("Plugin misses plugin/version: " + path);
  80. } else if (!cf->has_section_key("plugin", "description")) {
  81. WARN_PRINTS("Plugin misses plugin/description: " + path);
  82. } else if (!cf->has_section_key("plugin", "script")) {
  83. WARN_PRINTS("Plugin misses plugin/script: " + path);
  84. } else {
  85. String d = plugins[i];
  86. String name = cf->get_value("plugin", "name");
  87. String author = cf->get_value("plugin", "author");
  88. String version = cf->get_value("plugin", "version");
  89. String description = cf->get_value("plugin", "description");
  90. String script = cf->get_value("plugin", "script");
  91. TreeItem *item = plugin_list->create_item(root);
  92. item->set_text(0, name);
  93. item->set_tooltip(0, "Name: " + name + "\nPath: " + path + "\nMain Script: " + script);
  94. item->set_metadata(0, d);
  95. item->set_text(1, version);
  96. item->set_metadata(1, script);
  97. item->set_text(2, author);
  98. item->set_metadata(2, description);
  99. item->set_cell_mode(3, TreeItem::CELL_MODE_RANGE);
  100. item->set_range_config(3, 0, 1, 1);
  101. item->set_text(3, "Inactive,Active");
  102. item->set_editable(3, true);
  103. if (EditorNode::get_singleton()->is_addon_plugin_enabled(d)) {
  104. item->set_custom_color(3, get_color("success_color", "Editor"));
  105. item->set_range(3, 1);
  106. } else {
  107. item->set_custom_color(3, get_color("disabled_font_color", "Editor"));
  108. item->set_range(3, 0);
  109. }
  110. }
  111. }
  112. updating = false;
  113. }
  114. void EditorPluginSettings::_plugin_activity_changed() {
  115. if (updating)
  116. return;
  117. TreeItem *ti = plugin_list->get_edited();
  118. ERR_FAIL_COND(!ti);
  119. bool active = ti->get_range(3);
  120. String name = ti->get_metadata(0);
  121. EditorNode::get_singleton()->set_addon_plugin_enabled(name, active);
  122. bool is_active = EditorNode::get_singleton()->is_addon_plugin_enabled(name);
  123. if (is_active != active) {
  124. updating = true;
  125. ti->set_range(3, is_active ? 1 : 0);
  126. updating = false;
  127. }
  128. if (is_active)
  129. ti->set_custom_color(3, get_color("success_color", "Editor"));
  130. else
  131. ti->set_custom_color(3, get_color("disabled_font_color", "Editor"));
  132. }
  133. void EditorPluginSettings::_bind_methods() {
  134. ClassDB::bind_method("update_plugins", &EditorPluginSettings::update_plugins);
  135. ClassDB::bind_method("_plugin_activity_changed", &EditorPluginSettings::_plugin_activity_changed);
  136. }
  137. EditorPluginSettings::EditorPluginSettings() {
  138. HBoxContainer *title_hb = memnew(HBoxContainer);
  139. title_hb->add_child(memnew(Label(TTR("Installed Plugins:"))));
  140. title_hb->add_spacer();
  141. update_list = memnew(Button(TTR("Update")));
  142. update_list->connect("pressed", this, "update_plugins");
  143. title_hb->add_child(update_list);
  144. add_child(title_hb);
  145. plugin_list = memnew(Tree);
  146. plugin_list->set_v_size_flags(SIZE_EXPAND_FILL);
  147. plugin_list->set_columns(4);
  148. plugin_list->set_column_titles_visible(true);
  149. plugin_list->set_column_title(0, TTR("Name:"));
  150. plugin_list->set_column_title(1, TTR("Version:"));
  151. plugin_list->set_column_title(2, TTR("Author:"));
  152. plugin_list->set_column_title(3, TTR("Status:"));
  153. plugin_list->set_column_expand(0, true);
  154. plugin_list->set_column_expand(1, false);
  155. plugin_list->set_column_expand(2, false);
  156. plugin_list->set_column_expand(3, false);
  157. plugin_list->set_column_min_width(1, 100 * EDSCALE);
  158. plugin_list->set_column_min_width(2, 250 * EDSCALE);
  159. plugin_list->set_column_min_width(3, 80 * EDSCALE);
  160. plugin_list->set_hide_root(true);
  161. plugin_list->connect("item_edited", this, "_plugin_activity_changed");
  162. VBoxContainer *mc = memnew(VBoxContainer);
  163. mc->add_child(plugin_list);
  164. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  165. mc->set_h_size_flags(SIZE_EXPAND_FILL);
  166. add_child(mc);
  167. updating = false;
  168. }