1 /*************************************************************************/
2 /*  mesh_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 
31 #include "mesh_library_editor_plugin.h"
32 
33 #include "editor/editor_node.h"
34 #include "editor/editor_settings.h"
35 #include "main/main.h"
36 #include "scene/3d/mesh_instance.h"
37 #include "scene/3d/navigation_mesh.h"
38 #include "scene/3d/physics_body.h"
39 #include "scene/main/viewport.h"
40 #include "scene/resources/packed_scene.h"
41 #include "spatial_editor_plugin.h"
42 
edit(const Ref<MeshLibrary> & p_mesh_library)43 void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_mesh_library) {
44 
45 	mesh_library = p_mesh_library;
46 	if (mesh_library.is_valid())
47 		menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !mesh_library->has_meta("_editor_source_scene"));
48 }
49 
_menu_confirm()50 void MeshLibraryEditor::_menu_confirm() {
51 
52 	switch (option) {
53 
54 		case MENU_OPTION_REMOVE_ITEM: {
55 
56 			mesh_library->remove_item(to_erase);
57 		} break;
58 		case MENU_OPTION_UPDATE_FROM_SCENE: {
59 			String existing = mesh_library->get_meta("_editor_source_scene");
60 			ERR_FAIL_COND(existing == "");
61 			_import_scene_cbk(existing);
62 
63 		} break;
64 		default: {
65 		};
66 	}
67 }
68 
_import_scene(Node * p_scene,Ref<MeshLibrary> p_library,bool p_merge)69 void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
70 
71 	if (!p_merge)
72 		p_library->clear();
73 
74 	Map<int, MeshInstance *> mesh_instances;
75 
76 	for (int i = 0; i < p_scene->get_child_count(); i++) {
77 
78 		Node *child = p_scene->get_child(i);
79 
80 		if (!Object::cast_to<MeshInstance>(child)) {
81 			if (child->get_child_count() > 0) {
82 				child = child->get_child(0);
83 				if (!Object::cast_to<MeshInstance>(child)) {
84 					continue;
85 				}
86 
87 			} else
88 				continue;
89 		}
90 
91 		MeshInstance *mi = Object::cast_to<MeshInstance>(child);
92 		Ref<Mesh> mesh = mi->get_mesh();
93 		if (mesh.is_null())
94 			continue;
95 
96 		mesh = mesh->duplicate();
97 		for (int j = 0; j < mesh->get_surface_count(); ++j) {
98 			Ref<Material> mat = mi->get_surface_material(j);
99 
100 			if (mat.is_valid()) {
101 				mesh->surface_set_material(j, mat);
102 			}
103 		}
104 
105 		int id = p_library->find_item_by_name(mi->get_name());
106 		if (id < 0) {
107 
108 			id = p_library->get_last_unused_item_id();
109 			p_library->create_item(id);
110 			p_library->set_item_name(id, mi->get_name());
111 		}
112 
113 		p_library->set_item_mesh(id, mesh);
114 		mesh_instances[id] = mi;
115 
116 		Vector<MeshLibrary::ShapeData> collisions;
117 
118 		for (int j = 0; j < mi->get_child_count(); j++) {
119 
120 			Node *child2 = mi->get_child(j);
121 			if (!Object::cast_to<StaticBody>(child2))
122 				continue;
123 
124 			StaticBody *sb = Object::cast_to<StaticBody>(child2);
125 			List<uint32_t> shapes;
126 			sb->get_shape_owners(&shapes);
127 
128 			for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
129 				if (sb->is_shape_owner_disabled(E->get()))
130 					continue;
131 
132 				//Transform shape_transform = sb->shape_owner_get_transform(E->get());
133 
134 				//shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
135 
136 				for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
137 
138 					Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
139 					if (!collision.is_valid())
140 						continue;
141 					MeshLibrary::ShapeData shape_data;
142 					shape_data.shape = collision;
143 					shape_data.local_transform = sb->get_transform() * sb->shape_owner_get_transform(E->get());
144 					collisions.push_back(shape_data);
145 				}
146 			}
147 		}
148 
149 		p_library->set_item_shapes(id, collisions);
150 
151 		Ref<NavigationMesh> navmesh;
152 		Transform navmesh_transform;
153 		for (int j = 0; j < mi->get_child_count(); j++) {
154 			Node *child2 = mi->get_child(j);
155 			if (!Object::cast_to<NavigationMeshInstance>(child2))
156 				continue;
157 			NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
158 			navmesh = sb->get_navigation_mesh();
159 			navmesh_transform = sb->get_transform();
160 			if (!navmesh.is_null())
161 				break;
162 		}
163 		if (!navmesh.is_null()) {
164 			p_library->set_item_navmesh(id, navmesh);
165 			p_library->set_item_navmesh_transform(id, navmesh_transform);
166 		}
167 	}
168 
169 	//generate previews!
170 
171 	if (1) {
172 
173 		Vector<Ref<Mesh> > meshes;
174 		Vector<Transform> transforms;
175 		Vector<int> ids = p_library->get_item_list();
176 		for (int i = 0; i < ids.size(); i++) {
177 
178 			if (mesh_instances.find(ids[i])) {
179 
180 				meshes.push_back(p_library->get_item_mesh(ids[i]));
181 				transforms.push_back(mesh_instances[ids[i]]->get_transform());
182 			}
183 		}
184 
185 		Vector<Ref<Texture> > textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, &transforms, EditorSettings::get_singleton()->get("editors/grid_map/preview_size"));
186 		int j = 0;
187 		for (int i = 0; i < ids.size(); i++) {
188 
189 			if (mesh_instances.find(ids[i])) {
190 
191 				p_library->set_item_preview(ids[i], textures[j]);
192 				j++;
193 			}
194 		}
195 	}
196 }
197 
_import_scene_cbk(const String & p_str)198 void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
199 
200 	Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
201 	ERR_FAIL_COND(ps.is_null());
202 	Node *scene = ps->instance();
203 
204 	ERR_FAIL_COND_MSG(!scene, "Cannot create an instance from PackedScene '" + p_str + "'.");
205 
206 	_import_scene(scene, mesh_library, option == MENU_OPTION_UPDATE_FROM_SCENE);
207 
208 	memdelete(scene);
209 	mesh_library->set_meta("_editor_source_scene", p_str);
210 	menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
211 }
212 
update_library_file(Node * p_base_scene,Ref<MeshLibrary> ml,bool p_merge)213 Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge) {
214 
215 	_import_scene(p_base_scene, ml, p_merge);
216 	return OK;
217 }
218 
_menu_cbk(int p_option)219 void MeshLibraryEditor::_menu_cbk(int p_option) {
220 
221 	option = p_option;
222 	switch (p_option) {
223 
224 		case MENU_OPTION_ADD_ITEM: {
225 
226 			mesh_library->create_item(mesh_library->get_last_unused_item_id());
227 		} break;
228 		case MENU_OPTION_REMOVE_ITEM: {
229 
230 			String p = editor->get_inspector()->get_selected_path();
231 			if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/") >= 3) {
232 
233 				to_erase = p.get_slice("/", 3).to_int();
234 				cd->set_text(vformat(TTR("Remove item %d?"), to_erase));
235 				cd->popup_centered(Size2(300, 60));
236 			}
237 		} break;
238 		case MENU_OPTION_IMPORT_FROM_SCENE: {
239 
240 			file->popup_centered_ratio();
241 		} break;
242 		case MENU_OPTION_UPDATE_FROM_SCENE: {
243 
244 			cd->set_text(vformat(TTR("Update from existing scene?:\n%s"), String(mesh_library->get_meta("_editor_source_scene"))));
245 			cd->popup_centered(Size2(500, 60));
246 		} break;
247 	}
248 }
249 
_bind_methods()250 void MeshLibraryEditor::_bind_methods() {
251 
252 	ClassDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
253 	ClassDB::bind_method("_menu_confirm", &MeshLibraryEditor::_menu_confirm);
254 	ClassDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
255 }
256 
MeshLibraryEditor(EditorNode * p_editor)257 MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
258 
259 	file = memnew(EditorFileDialog);
260 	file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
261 	//not for now?
262 	List<String> extensions;
263 	ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
264 	file->clear_filters();
265 	file->set_title(TTR("Import Scene"));
266 	for (int i = 0; i < extensions.size(); i++) {
267 
268 		file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
269 	}
270 	add_child(file);
271 	file->connect("file_selected", this, "_import_scene_cbk");
272 
273 	menu = memnew(MenuButton);
274 	SpatialEditor::get_singleton()->add_control_to_menu_panel(menu);
275 	menu->set_position(Point2(1, 1));
276 	menu->set_text(TTR("Mesh Library"));
277 	menu->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MeshLibrary", "EditorIcons"));
278 	menu->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
279 	menu->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
280 	menu->get_popup()->add_separator();
281 	menu->get_popup()->add_item(TTR("Import from Scene"), MENU_OPTION_IMPORT_FROM_SCENE);
282 	menu->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
283 	menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
284 	menu->get_popup()->connect("id_pressed", this, "_menu_cbk");
285 	menu->hide();
286 
287 	editor = p_editor;
288 	cd = memnew(ConfirmationDialog);
289 	add_child(cd);
290 	cd->get_ok()->connect("pressed", this, "_menu_confirm");
291 }
292 
edit(Object * p_node)293 void MeshLibraryEditorPlugin::edit(Object *p_node) {
294 
295 	if (Object::cast_to<MeshLibrary>(p_node)) {
296 		mesh_library_editor->edit(Object::cast_to<MeshLibrary>(p_node));
297 		mesh_library_editor->show();
298 	} else
299 		mesh_library_editor->hide();
300 }
301 
handles(Object * p_node) const302 bool MeshLibraryEditorPlugin::handles(Object *p_node) const {
303 
304 	return p_node->is_class("MeshLibrary");
305 }
306 
make_visible(bool p_visible)307 void MeshLibraryEditorPlugin::make_visible(bool p_visible) {
308 
309 	if (p_visible) {
310 		mesh_library_editor->show();
311 		mesh_library_editor->get_menu_button()->show();
312 	} else {
313 		mesh_library_editor->hide();
314 		mesh_library_editor->get_menu_button()->hide();
315 	}
316 }
317 
MeshLibraryEditorPlugin(EditorNode * p_node)318 MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
319 
320 	EDITOR_DEF("editors/grid_map/preview_size", 64);
321 	mesh_library_editor = memnew(MeshLibraryEditor(p_node));
322 
323 	p_node->get_viewport()->add_child(mesh_library_editor);
324 	mesh_library_editor->set_anchors_and_margins_preset(Control::PRESET_TOP_WIDE);
325 	mesh_library_editor->set_end(Point2(0, 22));
326 	mesh_library_editor->hide();
327 }
328