cube_grid_theme_editor_plugin.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*************************************************************************/
  2. /* cube_grid_theme_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "cube_grid_theme_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "main/main.h"
  34. #include "scene/3d/mesh_instance.h"
  35. #include "scene/3d/navigation_mesh.h"
  36. #include "scene/3d/physics_body.h"
  37. #include "scene/main/viewport.h"
  38. #include "scene/resources/packed_scene.h"
  39. void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_theme) {
  40. theme = p_theme;
  41. if (theme.is_valid())
  42. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !theme->has_meta("_editor_source_scene"));
  43. }
  44. void MeshLibraryEditor::_menu_confirm() {
  45. switch (option) {
  46. case MENU_OPTION_REMOVE_ITEM: {
  47. theme->remove_item(to_erase);
  48. } break;
  49. case MENU_OPTION_UPDATE_FROM_SCENE: {
  50. String existing = theme->get_meta("_editor_source_scene");
  51. ERR_FAIL_COND(existing == "");
  52. _import_scene_cbk(existing);
  53. } break;
  54. default: {};
  55. }
  56. }
  57. void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
  58. if (!p_merge)
  59. p_library->clear();
  60. for (int i = 0; i < p_scene->get_child_count(); i++) {
  61. Node *child = p_scene->get_child(i);
  62. if (!Object::cast_to<MeshInstance>(child)) {
  63. if (child->get_child_count() > 0) {
  64. child = child->get_child(0);
  65. if (!Object::cast_to<MeshInstance>(child)) {
  66. continue;
  67. }
  68. } else
  69. continue;
  70. }
  71. MeshInstance *mi = Object::cast_to<MeshInstance>(child);
  72. Ref<Mesh> mesh = mi->get_mesh();
  73. if (mesh.is_null())
  74. continue;
  75. int id = p_library->find_item_by_name(mi->get_name());
  76. if (id < 0) {
  77. id = p_library->get_last_unused_item_id();
  78. p_library->create_item(id);
  79. p_library->set_item_name(id, mi->get_name());
  80. }
  81. p_library->set_item_mesh(id, mesh);
  82. Vector<MeshLibrary::ShapeData> collisions;
  83. for (int j = 0; j < mi->get_child_count(); j++) {
  84. Node *child2 = mi->get_child(j);
  85. if (!Object::cast_to<StaticBody>(child2))
  86. continue;
  87. StaticBody *sb = Object::cast_to<StaticBody>(child2);
  88. List<uint32_t> shapes;
  89. sb->get_shape_owners(&shapes);
  90. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  91. if (sb->is_shape_owner_disabled(E->get()))
  92. continue;
  93. //Transform shape_transform = sb->shape_owner_get_transform(E->get());
  94. //shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
  95. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  96. Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
  97. if (!collision.is_valid())
  98. continue;
  99. MeshLibrary::ShapeData shape_data;
  100. shape_data.shape = collision;
  101. shape_data.local_transform = sb->shape_owner_get_transform(E->get());
  102. collisions.push_back(shape_data);
  103. }
  104. }
  105. }
  106. p_library->set_item_shapes(id, collisions);
  107. Ref<NavigationMesh> navmesh;
  108. for (int j = 0; j < mi->get_child_count(); j++) {
  109. Node *child2 = mi->get_child(j);
  110. if (!Object::cast_to<NavigationMeshInstance>(child2))
  111. continue;
  112. NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
  113. navmesh = sb->get_navigation_mesh();
  114. if (!navmesh.is_null())
  115. break;
  116. }
  117. if (!navmesh.is_null()) {
  118. p_library->set_item_navmesh(id, navmesh);
  119. }
  120. }
  121. //generate previews!
  122. if (1) {
  123. Vector<Ref<Mesh> > meshes;
  124. Vector<int> ids = p_library->get_item_list();
  125. for (int i = 0; i < ids.size(); i++) {
  126. meshes.push_back(p_library->get_item_mesh(ids[i]));
  127. }
  128. Vector<Ref<Texture> > textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, EditorSettings::get_singleton()->get("editors/grid_map/preview_size"));
  129. for (int i = 0; i < ids.size(); i++) {
  130. p_library->set_item_preview(ids[i], textures[i]);
  131. }
  132. }
  133. }
  134. void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
  135. print_line("Impot Callback!");
  136. Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
  137. ERR_FAIL_COND(ps.is_null());
  138. Node *scene = ps->instance();
  139. _import_scene(scene, theme, option == MENU_OPTION_UPDATE_FROM_SCENE);
  140. memdelete(scene);
  141. theme->set_meta("_editor_source_scene", p_str);
  142. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
  143. }
  144. Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge) {
  145. _import_scene(p_base_scene, ml, p_merge);
  146. return OK;
  147. }
  148. void MeshLibraryEditor::_menu_cbk(int p_option) {
  149. option = p_option;
  150. switch (p_option) {
  151. case MENU_OPTION_ADD_ITEM: {
  152. theme->create_item(theme->get_last_unused_item_id());
  153. } break;
  154. case MENU_OPTION_REMOVE_ITEM: {
  155. String p = editor->get_property_editor()->get_selected_path();
  156. if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/") >= 3) {
  157. to_erase = p.get_slice("/", 3).to_int();
  158. cd->set_text(vformat(TTR("Remove item %d?"), to_erase));
  159. cd->popup_centered(Size2(300, 60));
  160. }
  161. } break;
  162. case MENU_OPTION_IMPORT_FROM_SCENE: {
  163. file->popup_centered_ratio();
  164. } break;
  165. case MENU_OPTION_UPDATE_FROM_SCENE: {
  166. cd->set_text("Update from existing scene?:\n" + String(theme->get_meta("_editor_source_scene")));
  167. cd->popup_centered(Size2(500, 60));
  168. } break;
  169. }
  170. }
  171. void MeshLibraryEditor::_bind_methods() {
  172. ClassDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
  173. ClassDB::bind_method("_menu_confirm", &MeshLibraryEditor::_menu_confirm);
  174. ClassDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
  175. }
  176. MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
  177. file = memnew(EditorFileDialog);
  178. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  179. //not for now?
  180. List<String> extensions;
  181. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  182. file->clear_filters();
  183. file->set_title(TTR("Import Scene"));
  184. for (int i = 0; i < extensions.size(); i++) {
  185. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  186. }
  187. add_child(file);
  188. file->connect("file_selected", this, "_import_scene_cbk");
  189. Panel *panel = memnew(Panel);
  190. panel->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  191. add_child(panel);
  192. MenuButton *options = memnew(MenuButton);
  193. panel->add_child(options);
  194. options->set_position(Point2(1, 1));
  195. options->set_text("Theme");
  196. options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
  197. options->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
  198. options->get_popup()->add_separator();
  199. options->get_popup()->add_item(TTR("Import from Scene"), MENU_OPTION_IMPORT_FROM_SCENE);
  200. options->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
  201. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
  202. options->get_popup()->connect("id_pressed", this, "_menu_cbk");
  203. menu = options;
  204. editor = p_editor;
  205. cd = memnew(ConfirmationDialog);
  206. add_child(cd);
  207. cd->get_ok()->connect("pressed", this, "_menu_confirm");
  208. }
  209. void MeshLibraryEditorPlugin::edit(Object *p_node) {
  210. if (Object::cast_to<MeshLibrary>(p_node)) {
  211. theme_editor->edit(Object::cast_to<MeshLibrary>(p_node));
  212. theme_editor->show();
  213. } else
  214. theme_editor->hide();
  215. }
  216. bool MeshLibraryEditorPlugin::handles(Object *p_node) const {
  217. return p_node->is_class("MeshLibrary");
  218. }
  219. void MeshLibraryEditorPlugin::make_visible(bool p_visible) {
  220. if (p_visible)
  221. theme_editor->show();
  222. else
  223. theme_editor->hide();
  224. }
  225. MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
  226. EDITOR_DEF("editors/grid_map/preview_size", 64);
  227. theme_editor = memnew(MeshLibraryEditor(p_node));
  228. p_node->get_viewport()->add_child(theme_editor);
  229. theme_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  230. theme_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
  231. theme_editor->set_end(Point2(0, 22));
  232. theme_editor->hide();
  233. }