gdnative_library_editor_plugin.cpp 16 KB

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