gdnative_library_singleton_editor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. void GDNativeLibrarySingletonEditor::_find_gdnative_singletons(EditorFileSystemDirectory *p_dir, const Set<String> &enabled_list) {
  34. // check children
  35. for (int i = 0; i < p_dir->get_file_count(); i++) {
  36. String file_type = p_dir->get_file_type(i);
  37. if (file_type != "GDNativeLibrary") {
  38. continue;
  39. }
  40. Ref<GDNativeLibrary> lib = ResourceLoader::load(p_dir->get_file_path(i));
  41. if (lib.is_valid() && lib->is_singleton()) {
  42. String path = p_dir->get_file_path(i);
  43. TreeItem *ti = libraries->create_item(libraries->get_root());
  44. ti->set_text(0, path.get_file());
  45. ti->set_tooltip(0, path);
  46. ti->set_metadata(0, path);
  47. ti->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  48. ti->set_text(1, "Disabled,Enabled");
  49. bool enabled = enabled_list.has(path) ? true : false;
  50. ti->set_range(1, enabled ? 1 : 0);
  51. ti->set_custom_color(1, enabled ? Color(0, 1, 0) : Color(1, 0, 0));
  52. }
  53. }
  54. // check subdirectories
  55. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  56. _find_gdnative_singletons(p_dir->get_subdir(i), enabled_list);
  57. }
  58. }
  59. void GDNativeLibrarySingletonEditor::_update_libraries() {
  60. updating = true;
  61. libraries->clear();
  62. libraries->create_item(); //rppt
  63. Vector<String> enabled_paths;
  64. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  65. enabled_paths = ProjectSettings::get_singleton()->get("gdnative/singletons");
  66. }
  67. Set<String> enabled_list;
  68. for (int i = 0; i < enabled_paths.size(); i++) {
  69. enabled_list.insert(enabled_paths[i]);
  70. }
  71. EditorFileSystemDirectory *fs = EditorFileSystem::get_singleton()->get_filesystem();
  72. if (fs) {
  73. _find_gdnative_singletons(fs, enabled_list);
  74. }
  75. updating = false;
  76. }
  77. void GDNativeLibrarySingletonEditor::_item_edited() {
  78. if (updating)
  79. return;
  80. TreeItem *item = libraries->get_edited();
  81. if (!item)
  82. return;
  83. bool enabled = item->get_range(1);
  84. String path = item->get_metadata(0);
  85. Vector<String> enabled_paths;
  86. if (ProjectSettings::get_singleton()->has_setting("gdnative/singletons")) {
  87. enabled_paths = ProjectSettings::get_singleton()->get("gdnative/singletons");
  88. }
  89. if (enabled) {
  90. if (enabled_paths.find(path) == -1) {
  91. enabled_paths.push_back(path);
  92. }
  93. } else {
  94. enabled_paths.erase(path);
  95. }
  96. if (enabled_paths.size()) {
  97. ProjectSettings::get_singleton()->set("gdnative/singletons", enabled_paths);
  98. } else {
  99. ProjectSettings::get_singleton()->set("gdnative/singletons", Variant());
  100. }
  101. }
  102. void GDNativeLibrarySingletonEditor::_notification(int p_what) {
  103. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  104. if (is_visible_in_tree()) {
  105. _update_libraries();
  106. }
  107. }
  108. }
  109. void GDNativeLibrarySingletonEditor::_bind_methods() {
  110. ClassDB::bind_method(D_METHOD("_item_edited"), &GDNativeLibrarySingletonEditor::_item_edited);
  111. }
  112. GDNativeLibrarySingletonEditor::GDNativeLibrarySingletonEditor() {
  113. libraries = memnew(Tree);
  114. libraries->set_columns(2);
  115. libraries->set_column_titles_visible(true);
  116. libraries->set_column_title(0, TTR("Library"));
  117. libraries->set_column_title(1, TTR("Status"));
  118. libraries->set_hide_root(true);
  119. add_margin_child(TTR("Libraries: "), libraries, true);
  120. updating = false;
  121. libraries->connect("item_edited", this, "_item_edited");
  122. }
  123. #endif // TOOLS_ENABLED