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