gdnative_library_editor_plugin.cpp 16 KB

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