gdnative_library_singleton_editor.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************/
  2. /* gdnative_library_singleton_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifdef TOOLS_ENABLED
  31. #include "gdnative_library_singleton_editor.h"
  32. #include "gdnative.h"
  33. #include "core/config/project_settings.h"
  34. #include "editor/editor_node.h"
  35. Set<String> GDNativeLibrarySingletonEditor::_find_singletons_recursive(EditorFileSystemDirectory *p_dir) {
  36. Set<String> file_paths;
  37. // check children
  38. for (int i = 0; i < p_dir->get_file_count(); i++) {
  39. String file_name = p_dir->get_file(i);
  40. String file_type = p_dir->get_file_type(i);
  41. if (file_type != "GDNativeLibrary") {
  42. continue;
  43. }
  44. Ref<GDNativeLibrary> lib = ResourceLoader::load(p_dir->get_file_path(i));
  45. if (lib.is_valid() && lib->is_singleton()) {
  46. file_paths.insert(p_dir->get_file_path(i));
  47. }
  48. }
  49. // check subdirectories
  50. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  51. Set<String> paths = _find_singletons_recursive(p_dir->get_subdir(i));
  52. for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
  53. file_paths.insert(E->get());
  54. }
  55. }
  56. return file_paths;
  57. }
  58. void GDNativeLibrarySingletonEditor::_discover_singletons() {
  59. EditorFileSystemDirectory *dir = EditorFileSystem::get_singleton()->get_filesystem();
  60. Set<String> file_paths = _find_singletons_recursive(dir);
  61. bool changed = false;
  62. Array current_files;
  63. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  64. current_files = ProjectSettings::get_singleton()->get("gdnative/singletons");
  65. }
  66. Array files;
  67. for (Set<String>::Element *E = file_paths.front(); E; E = E->next()) {
  68. if (!current_files.has(E->get())) {
  69. changed = true;
  70. }
  71. files.append(E->get());
  72. }
  73. // Check for removed files
  74. if (!changed) {
  75. // Removed singleton
  76. for (int j = 0; j < current_files.size(); j++) {
  77. if (!files.has(current_files[j])) {
  78. changed = true;
  79. break;
  80. }
  81. }
  82. }
  83. if (changed) {
  84. ProjectSettings::get_singleton()->set("gdnative/singletons", files);
  85. _update_libraries(); // So singleton options (i.e. disabled) updates too
  86. ProjectSettings::get_singleton()->save();
  87. }
  88. }
  89. void GDNativeLibrarySingletonEditor::_update_libraries() {
  90. updating = true;
  91. libraries->clear();
  92. libraries->create_item(); // root item
  93. Array singletons;
  94. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  95. singletons = ProjectSettings::get_singleton()->get("gdnative/singletons");
  96. }
  97. Array singletons_disabled;
  98. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons_disabled")) {
  99. singletons_disabled = ProjectSettings::get_singleton()->get("gdnative/singletons_disabled");
  100. }
  101. Array updated_disabled;
  102. for (int i = 0; i < singletons.size(); i++) {
  103. bool enabled = true;
  104. String path = singletons[i];
  105. if (singletons_disabled.has(path)) {
  106. enabled = false;
  107. updated_disabled.push_back(path);
  108. }
  109. TreeItem *ti = libraries->create_item(libraries->get_root());
  110. ti->set_text(0, path.get_file());
  111. ti->set_tooltip(0, path);
  112. ti->set_metadata(0, path);
  113. ti->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  114. ti->set_text(1, "Disabled,Enabled");
  115. ti->set_range(1, enabled ? 1 : 0);
  116. ti->set_custom_color(1, enabled ? Color(0, 1, 0) : Color(1, 0, 0));
  117. ti->set_editable(1, true);
  118. }
  119. // The singletons list changed, we must update the settings
  120. if (updated_disabled.size() != singletons_disabled.size()) {
  121. ProjectSettings::get_singleton()->set("gdnative/singletons_disabled", updated_disabled);
  122. }
  123. updating = false;
  124. }
  125. void GDNativeLibrarySingletonEditor::_item_edited() {
  126. if (updating) {
  127. return;
  128. }
  129. TreeItem *item = libraries->get_edited();
  130. if (!item) {
  131. return;
  132. }
  133. bool enabled = item->get_range(1);
  134. String path = item->get_metadata(0);
  135. Array disabled_paths;
  136. Array undo_paths;
  137. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons_disabled")) {
  138. disabled_paths = ProjectSettings::get_singleton()->get("gdnative/singletons_disabled");
  139. // Duplicate so redo works (not a reference)
  140. disabled_paths = disabled_paths.duplicate();
  141. // For undo, so we can reset the property.
  142. undo_paths = disabled_paths.duplicate();
  143. }
  144. if (enabled) {
  145. disabled_paths.erase(path);
  146. } else {
  147. if (disabled_paths.find(path) == -1) {
  148. disabled_paths.push_back(path);
  149. }
  150. }
  151. undo_redo->create_action(enabled ? TTR("Enabled GDNative Singleton") : TTR("Disabled GDNative Singleton"));
  152. undo_redo->add_do_property(ProjectSettings::get_singleton(), "gdnative/singletons_disabled", disabled_paths);
  153. undo_redo->add_do_method(this, "_update_libraries");
  154. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "gdnative/singletons_disabled", undo_paths);
  155. undo_redo->add_undo_method(this, "_update_libraries");
  156. undo_redo->commit_action();
  157. }
  158. void GDNativeLibrarySingletonEditor::_notification(int p_what) {
  159. switch (p_what) {
  160. case NOTIFICATION_VISIBILITY_CHANGED: {
  161. if (is_visible_in_tree()) {
  162. _update_libraries();
  163. }
  164. } break;
  165. }
  166. }
  167. void GDNativeLibrarySingletonEditor::_bind_methods() {
  168. ClassDB::bind_method(D_METHOD("_update_libraries"), &GDNativeLibrarySingletonEditor::_update_libraries);
  169. }
  170. GDNativeLibrarySingletonEditor::GDNativeLibrarySingletonEditor() {
  171. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  172. libraries = memnew(Tree);
  173. libraries->set_columns(2);
  174. libraries->set_column_titles_visible(true);
  175. libraries->set_column_title(0, TTR("Library"));
  176. libraries->set_column_title(1, TTR("Status"));
  177. libraries->set_hide_root(true);
  178. add_margin_child(TTR("Libraries: "), libraries, true);
  179. updating = false;
  180. libraries->connect("item_edited", callable_mp(this, &GDNativeLibrarySingletonEditor::_item_edited));
  181. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &GDNativeLibrarySingletonEditor::_discover_singletons));
  182. }
  183. #endif // TOOLS_ENABLED