gdnative_library_editor_plugin.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*************************************************************************/
  2. /* gdnative_library_editor_plugin.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. #ifdef TOOLS_ENABLED
  31. #include "gdnative_library_editor_plugin.h"
  32. #include "gdnative.h"
  33. #include "editor/editor_scale.h"
  34. void GDNativeLibraryEditor::edit(Ref<GDNativeLibrary> p_library) {
  35. library = p_library;
  36. Ref<ConfigFile> config = p_library->get_config_file();
  37. for (Map<String, NativePlatformConfig>::Element *E = platforms.front(); E; E = E->next()) {
  38. for (List<String>::Element *it = E->value().entries.front(); it; it = it->next()) {
  39. String target = E->key() + "." + it->get();
  40. TargetConfig ecfg;
  41. ecfg.library = config->get_value("entry", target, "");
  42. ecfg.dependencies = config->get_value("dependencies", target, Array());
  43. entry_configs[target] = ecfg;
  44. }
  45. }
  46. _update_tree();
  47. }
  48. void GDNativeLibraryEditor::_bind_methods() {
  49. }
  50. void GDNativeLibraryEditor::_update_tree() {
  51. tree->clear();
  52. TreeItem *root = tree->create_item();
  53. PopupMenu *filter_list = filter->get_popup();
  54. String text = "";
  55. for (int i = 0; i < filter_list->get_item_count(); i++) {
  56. if (!filter_list->is_item_checked(i)) {
  57. continue;
  58. }
  59. Map<String, NativePlatformConfig>::Element *E = platforms.find(filter_list->get_item_metadata(i));
  60. if (!text.is_empty()) {
  61. text += ", ";
  62. }
  63. text += E->get().name;
  64. TreeItem *platform = tree->create_item(root);
  65. platform->set_text(0, E->get().name);
  66. platform->set_metadata(0, E->get().library_extension);
  67. platform->set_custom_bg_color(0, get_theme_color("prop_category", "Editor"));
  68. platform->set_custom_bg_color(1, get_theme_color("prop_category", "Editor"));
  69. platform->set_custom_bg_color(2, get_theme_color("prop_category", "Editor"));
  70. platform->set_selectable(0, false);
  71. platform->set_expand_right(0, true);
  72. for (List<String>::Element *it = E->value().entries.front(); it; it = it->next()) {
  73. String target = E->key() + "." + it->get();
  74. TreeItem *bit = tree->create_item(platform);
  75. bit->set_text(0, it->get());
  76. bit->set_metadata(0, target);
  77. bit->set_selectable(0, false);
  78. bit->set_custom_bg_color(0, get_theme_color("prop_subsection", "Editor"));
  79. bit->add_button(1, get_theme_icon("Folder", "EditorIcons"), BUTTON_SELECT_LIBRARY, false, TTR("Select the dynamic library for this entry"));
  80. String file = entry_configs[target].library;
  81. if (!file.is_empty()) {
  82. bit->add_button(1, get_theme_icon("Clear", "EditorIcons"), BUTTON_CLEAR_LIBRARY, false, TTR("Clear"));
  83. }
  84. bit->set_text(1, file);
  85. bit->add_button(2, get_theme_icon("Folder", "EditorIcons"), BUTTON_SELECT_DEPENDENCES, false, TTR("Select dependencies of the library for this entry"));
  86. Array files = entry_configs[target].dependencies;
  87. if (files.size()) {
  88. bit->add_button(2, get_theme_icon("Clear", "EditorIcons"), BUTTON_CLEAR_DEPENDENCES, false, TTR("Clear"));
  89. }
  90. bit->set_text(2, Variant(files));
  91. bit->add_button(3, get_theme_icon("MoveUp", "EditorIcons"), BUTTON_MOVE_UP, false, TTR("Move Up"));
  92. bit->add_button(3, get_theme_icon("MoveDown", "EditorIcons"), BUTTON_MOVE_DOWN, false, TTR("Move Down"));
  93. bit->add_button(3, get_theme_icon("Remove", "EditorIcons"), BUTTON_ERASE_ENTRY, false, TTR("Remove current entry"));
  94. }
  95. TreeItem *new_arch = tree->create_item(platform);
  96. new_arch->set_text(0, TTR("Double click to create a new entry"));
  97. new_arch->set_text_align(0, TreeItem::ALIGN_CENTER);
  98. new_arch->set_custom_color(0, get_theme_color("accent_color", "Editor"));
  99. new_arch->set_expand_right(0, true);
  100. new_arch->set_metadata(1, E->key());
  101. platform->set_collapsed(collapsed_items.find(E->get().name) != nullptr);
  102. }
  103. filter->set_text(text);
  104. }
  105. void GDNativeLibraryEditor::_on_item_button(Object *item, int column, int id) {
  106. String target = Object::cast_to<TreeItem>(item)->get_metadata(0);
  107. String platform = target.substr(0, target.find("."));
  108. String entry = target.substr(platform.length() + 1, target.length());
  109. String section = (id == BUTTON_SELECT_DEPENDENCES || id == BUTTON_CLEAR_DEPENDENCES) ? "dependencies" : "entry";
  110. if (id == BUTTON_SELECT_LIBRARY || id == BUTTON_SELECT_DEPENDENCES) {
  111. TreeItem *treeItem = Object::cast_to<TreeItem>(item)->get_parent();
  112. EditorFileDialog::FileMode mode = EditorFileDialog::FILE_MODE_OPEN_FILE;
  113. if (id == BUTTON_SELECT_DEPENDENCES) {
  114. mode = EditorFileDialog::FILE_MODE_OPEN_FILES;
  115. } else if (treeItem->get_text(0) == "iOS") {
  116. mode = EditorFileDialog::FILE_MODE_OPEN_ANY;
  117. }
  118. file_dialog->set_meta("target", target);
  119. file_dialog->set_meta("section", section);
  120. file_dialog->clear_filters();
  121. String filter_string = treeItem->get_metadata(0);
  122. Vector<String> filters = filter_string.split(",", false, 0);
  123. for (int i = 0; i < filters.size(); i++) {
  124. file_dialog->add_filter(filters[i]);
  125. }
  126. file_dialog->set_file_mode(mode);
  127. file_dialog->popup_file_dialog();
  128. } else if (id == BUTTON_CLEAR_LIBRARY) {
  129. _set_target_value(section, target, "");
  130. } else if (id == BUTTON_CLEAR_DEPENDENCES) {
  131. _set_target_value(section, target, Array());
  132. } else if (id == BUTTON_ERASE_ENTRY) {
  133. _erase_entry(platform, entry);
  134. } else if (id == BUTTON_MOVE_UP || id == BUTTON_MOVE_DOWN) {
  135. _move_entry(platform, entry, id);
  136. }
  137. }
  138. void GDNativeLibraryEditor::_on_library_selected(const String &file) {
  139. _set_target_value(file_dialog->get_meta("section"), file_dialog->get_meta("target"), file);
  140. }
  141. void GDNativeLibraryEditor::_on_dependencies_selected(const PackedStringArray &files) {
  142. _set_target_value(file_dialog->get_meta("section"), file_dialog->get_meta("target"), files);
  143. }
  144. void GDNativeLibraryEditor::_on_filter_selected(int index) {
  145. PopupMenu *filter_list = filter->get_popup();
  146. filter_list->set_item_checked(index, !filter_list->is_item_checked(index));
  147. _update_tree();
  148. }
  149. void GDNativeLibraryEditor::_on_item_collapsed(Object *p_item) {
  150. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  151. String name = item->get_text(0);
  152. if (item->is_collapsed()) {
  153. collapsed_items.insert(name);
  154. } else if (Set<String>::Element *e = collapsed_items.find(name)) {
  155. collapsed_items.erase(e);
  156. }
  157. }
  158. void GDNativeLibraryEditor::_on_item_activated() {
  159. TreeItem *item = tree->get_selected();
  160. if (item && tree->get_selected_column() == 0 && item->get_metadata(0).get_type() == Variant::NIL) {
  161. new_architecture_dialog->set_meta("platform", item->get_metadata(1));
  162. new_architecture_dialog->popup_centered();
  163. }
  164. }
  165. void GDNativeLibraryEditor::_on_create_new_entry() {
  166. String platform = new_architecture_dialog->get_meta("platform");
  167. String entry = new_architecture_input->get_text().strip_edges();
  168. if (!entry.is_empty()) {
  169. platforms[platform].entries.push_back(entry);
  170. _update_tree();
  171. }
  172. }
  173. void GDNativeLibraryEditor::_set_target_value(const String &section, const String &target, Variant file) {
  174. if (section == "entry") {
  175. entry_configs[target].library = file;
  176. } else if (section == "dependencies") {
  177. entry_configs[target].dependencies = file;
  178. }
  179. _translate_to_config_file();
  180. _update_tree();
  181. }
  182. void GDNativeLibraryEditor::_erase_entry(const String &platform, const String &entry) {
  183. if (platforms.has(platform)) {
  184. if (List<String>::Element *E = platforms[platform].entries.find(entry)) {
  185. String target = platform + "." + entry;
  186. platforms[platform].entries.erase(E);
  187. _set_target_value("entry", target, "");
  188. _set_target_value("dependencies", target, Array());
  189. _translate_to_config_file();
  190. _update_tree();
  191. }
  192. }
  193. }
  194. void GDNativeLibraryEditor::_move_entry(const String &platform, const String &entry, int dir) {
  195. if (List<String>::Element *E = platforms[platform].entries.find(entry)) {
  196. if (E->prev() && dir == BUTTON_MOVE_UP) {
  197. platforms[platform].entries.insert_before(E->prev(), E->get());
  198. platforms[platform].entries.erase(E);
  199. } else if (E->next() && dir == BUTTON_MOVE_DOWN) {
  200. platforms[platform].entries.insert_after(E->next(), E->get());
  201. platforms[platform].entries.erase(E);
  202. }
  203. _translate_to_config_file();
  204. _update_tree();
  205. }
  206. }
  207. void GDNativeLibraryEditor::_translate_to_config_file() {
  208. if (!library.is_null()) {
  209. Ref<ConfigFile> config = library->get_config_file();
  210. config->erase_section("entry");
  211. config->erase_section("dependencies");
  212. for (Map<String, NativePlatformConfig>::Element *E = platforms.front(); E; E = E->next()) {
  213. for (List<String>::Element *it = E->value().entries.front(); it; it = it->next()) {
  214. String target = E->key() + "." + it->get();
  215. if (entry_configs[target].library.is_empty() && entry_configs[target].dependencies.is_empty()) {
  216. continue;
  217. }
  218. config->set_value("entry", target, entry_configs[target].library);
  219. config->set_value("dependencies", target, entry_configs[target].dependencies);
  220. }
  221. }
  222. library->notify_property_list_changed();
  223. }
  224. }
  225. GDNativeLibraryEditor::GDNativeLibraryEditor() {
  226. { // Define platforms
  227. NativePlatformConfig platform_windows;
  228. platform_windows.name = "Windows";
  229. platform_windows.entries.push_back("64");
  230. platform_windows.entries.push_back("32");
  231. platform_windows.library_extension = "*.dll";
  232. platforms["Windows"] = platform_windows;
  233. NativePlatformConfig platform_linux;
  234. platform_linux.name = "Linux/X11";
  235. platform_linux.entries.push_back("64");
  236. platform_linux.entries.push_back("32");
  237. platform_linux.library_extension = "*.so";
  238. platforms["X11"] = platform_linux;
  239. NativePlatformConfig platform_osx;
  240. platform_osx.name = "Mac OSX";
  241. platform_osx.entries.push_back("64");
  242. platform_osx.entries.push_back("32");
  243. platform_osx.library_extension = "*.dylib";
  244. platforms["OSX"] = platform_osx;
  245. NativePlatformConfig platform_haiku;
  246. platform_haiku.name = "Haiku";
  247. platform_haiku.entries.push_back("64");
  248. platform_haiku.entries.push_back("32");
  249. platform_haiku.library_extension = "*.so";
  250. platforms["Haiku"] = platform_haiku;
  251. NativePlatformConfig platform_uwp;
  252. platform_uwp.name = "UWP";
  253. platform_uwp.entries.push_back("arm");
  254. platform_uwp.entries.push_back("32");
  255. platform_uwp.entries.push_back("64");
  256. platform_uwp.library_extension = "*.dll";
  257. platforms["UWP"] = platform_uwp;
  258. NativePlatformConfig platform_android;
  259. platform_android.name = "Android";
  260. platform_android.entries.push_back("armeabi-v7a");
  261. platform_android.entries.push_back("arm64-v8a");
  262. platform_android.entries.push_back("x86");
  263. platform_android.entries.push_back("x86_64");
  264. platform_android.library_extension = "*.so";
  265. platforms["Android"] = platform_android;
  266. NativePlatformConfig platform_html5;
  267. platform_html5.name = "HTML5";
  268. platform_html5.entries.push_back("wasm32");
  269. platform_html5.library_extension = "*.wasm";
  270. platforms["HTML5"] = platform_html5;
  271. NativePlatformConfig platform_ios;
  272. platform_ios.name = "iOS";
  273. platform_ios.entries.push_back("armv7");
  274. platform_ios.entries.push_back("arm64");
  275. platform_ios.entries.push_back("x86_64");
  276. // iOS can use both Static and Dynamic libraries.
  277. // Frameworks is actually a folder with files.
  278. platform_ios.library_extension = "*.framework; Framework, *.xcframework; Binary Framework, *.a; Static Library, *.dylib; Dynamic Library";
  279. platforms["iOS"] = platform_ios;
  280. }
  281. VBoxContainer *container = memnew(VBoxContainer);
  282. add_child(container);
  283. container->set_anchors_and_offsets_preset(PRESET_WIDE);
  284. HBoxContainer *hbox = memnew(HBoxContainer);
  285. container->add_child(hbox);
  286. Label *label = memnew(Label);
  287. label->set_text(TTR("Platform:"));
  288. hbox->add_child(label);
  289. filter = memnew(MenuButton);
  290. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  291. filter->set_text_align(filter->ALIGN_LEFT);
  292. hbox->add_child(filter);
  293. PopupMenu *filter_list = filter->get_popup();
  294. filter_list->set_hide_on_checkable_item_selection(false);
  295. int idx = 0;
  296. for (Map<String, NativePlatformConfig>::Element *E = platforms.front(); E; E = E->next()) {
  297. filter_list->add_check_item(E->get().name, idx);
  298. filter_list->set_item_metadata(idx, E->key());
  299. filter_list->set_item_checked(idx, true);
  300. idx += 1;
  301. }
  302. filter_list->connect("index_pressed", callable_mp(this, &GDNativeLibraryEditor::_on_filter_selected));
  303. tree = memnew(Tree);
  304. container->add_child(tree);
  305. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  306. tree->set_hide_root(true);
  307. tree->set_column_titles_visible(true);
  308. tree->set_columns(4);
  309. tree->set_column_expand(0, false);
  310. tree->set_column_min_width(0, int(200 * EDSCALE));
  311. tree->set_column_title(0, TTR("Platform"));
  312. tree->set_column_title(1, TTR("Dynamic Library"));
  313. tree->set_column_title(2, TTR("Dependencies"));
  314. tree->set_column_expand(3, false);
  315. tree->set_column_min_width(3, int(110 * EDSCALE));
  316. tree->connect("button_pressed", callable_mp(this, &GDNativeLibraryEditor::_on_item_button));
  317. tree->connect("item_collapsed", callable_mp(this, &GDNativeLibraryEditor::_on_item_collapsed));
  318. tree->connect("item_activated", callable_mp(this, &GDNativeLibraryEditor::_on_item_activated));
  319. file_dialog = memnew(EditorFileDialog);
  320. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  321. //file_dialog->set_resizable(true);
  322. add_child(file_dialog);
  323. file_dialog->connect("file_selected", callable_mp(this, &GDNativeLibraryEditor::_on_library_selected));
  324. file_dialog->connect("dir_selected", callable_mp(this, &GDNativeLibraryEditor::_on_library_selected));
  325. file_dialog->connect("files_selected", callable_mp(this, &GDNativeLibraryEditor::_on_dependencies_selected));
  326. new_architecture_dialog = memnew(ConfirmationDialog);
  327. add_child(new_architecture_dialog);
  328. new_architecture_dialog->set_title(TTR("Add an architecture entry"));
  329. new_architecture_input = memnew(LineEdit);
  330. new_architecture_dialog->add_child(new_architecture_input);
  331. // new_architecture_dialog->set_custom_minimum_size(Vector2(300, 80) * EDSCALE);
  332. new_architecture_input->set_anchors_and_offsets_preset(PRESET_HCENTER_WIDE, PRESET_MODE_MINSIZE, 5 * EDSCALE);
  333. new_architecture_dialog->get_ok_button()->connect("pressed", callable_mp(this, &GDNativeLibraryEditor::_on_create_new_entry));
  334. }
  335. void GDNativeLibraryEditorPlugin::edit(Object *p_node) {
  336. Ref<GDNativeLibrary> new_library = Object::cast_to<GDNativeLibrary>(p_node);
  337. if (new_library.is_valid()) {
  338. library_editor->edit(new_library);
  339. }
  340. }
  341. bool GDNativeLibraryEditorPlugin::handles(Object *p_node) const {
  342. return p_node->is_class("GDNativeLibrary");
  343. }
  344. void GDNativeLibraryEditorPlugin::make_visible(bool p_visible) {
  345. if (p_visible) {
  346. button->show();
  347. EditorNode::get_singleton()->make_bottom_panel_item_visible(library_editor);
  348. } else {
  349. if (library_editor->is_visible_in_tree()) {
  350. EditorNode::get_singleton()->hide_bottom_panel();
  351. }
  352. button->hide();
  353. }
  354. }
  355. GDNativeLibraryEditorPlugin::GDNativeLibraryEditorPlugin(EditorNode *p_node) {
  356. library_editor = memnew(GDNativeLibraryEditor);
  357. library_editor->set_custom_minimum_size(Size2(0, 250 * EDSCALE));
  358. button = p_node->add_bottom_panel_item(TTR("GDNativeLibrary"), library_editor);
  359. button->hide();
  360. }
  361. #endif