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-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 #include "cube_grid_theme_editor_plugin.h"
31 
32 #include "editor/editor_node.h"
33 #include "editor/editor_settings.h"
34 #include "main/main.h"
35 #include "scene/3d/mesh_instance.h"
36 #include "scene/3d/navigation_mesh.h"
37 #include "scene/3d/physics_body.h"
38 #include "scene/main/viewport.h"
39 #include "scene/resources/packed_scene.h"
40 
edit(const Ref<MeshLibrary> & p_theme)41 void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_theme) {
42 
43 	theme = p_theme;
44 	if (theme.is_valid())
45 		menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !theme->has_meta("_editor_source_scene"));
46 }
47 
_menu_confirm()48 void MeshLibraryEditor::_menu_confirm() {
49 
50 	switch (option) {
51 
52 		case MENU_OPTION_REMOVE_ITEM: {
53 
54 			theme->remove_item(to_erase);
55 		} break;
56 		case MENU_OPTION_UPDATE_FROM_SCENE: {
57 			String existing = theme->get_meta("_editor_source_scene");
58 			ERR_FAIL_COND(existing == "");
59 			_import_scene_cbk(existing);
60 
61 		} break;
62 		default: {};
63 	}
64 }
65 
_import_scene(Node * p_scene,Ref<MeshLibrary> p_library,bool p_merge)66 void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
67 
68 	if (!p_merge)
69 		p_library->clear();
70 
71 	for (int i = 0; i < p_scene->get_child_count(); i++) {
72 
73 		Node *child = p_scene->get_child(i);
74 
75 		if (!child->cast_to<MeshInstance>()) {
76 			if (child->get_child_count() > 0) {
77 				child = child->get_child(0);
78 				if (!child->cast_to<MeshInstance>()) {
79 					continue;
80 				}
81 
82 			} else
83 				continue;
84 		}
85 
86 		MeshInstance *mi = child->cast_to<MeshInstance>();
87 		Ref<Mesh> mesh = mi->get_mesh();
88 		if (mesh.is_null())
89 			continue;
90 
91 		int id = p_library->find_item_name(mi->get_name());
92 		if (id < 0) {
93 
94 			id = p_library->get_last_unused_item_id();
95 			p_library->create_item(id);
96 			p_library->set_item_name(id, mi->get_name());
97 		}
98 
99 		p_library->set_item_mesh(id, mesh);
100 
101 		Ref<Shape> collision;
102 
103 		for (int j = 0; j < mi->get_child_count(); j++) {
104 #if 1
105 			Node *child2 = mi->get_child(j);
106 			if (!child2->cast_to<StaticBody>())
107 				continue;
108 			StaticBody *sb = child2->cast_to<StaticBody>();
109 			if (sb->get_shape_count() == 0)
110 				continue;
111 			collision = sb->get_shape(0);
112 			if (!collision.is_null())
113 				break;
114 #endif
115 		}
116 
117 		if (!collision.is_null()) {
118 
119 			p_library->set_item_shape(id, collision);
120 		}
121 		Ref<NavigationMesh> navmesh;
122 		for (int j = 0; j < mi->get_child_count(); j++) {
123 			Node *child2 = mi->get_child(j);
124 			if (!child2->cast_to<NavigationMeshInstance>())
125 				continue;
126 			NavigationMeshInstance *sb = child2->cast_to<NavigationMeshInstance>();
127 			navmesh = sb->get_navigation_mesh();
128 			if (!navmesh.is_null())
129 				break;
130 		}
131 		if (!navmesh.is_null()) {
132 			p_library->set_item_navmesh(id, navmesh);
133 		}
134 	}
135 
136 	//generate previews!
137 
138 	if (1) {
139 		Vector<int> ids = p_library->get_item_list();
140 		RID vp = VS::get_singleton()->viewport_create();
141 		VS::ViewportRect vr;
142 		vr.x = 0;
143 		vr.y = 0;
144 		vr.width = EditorSettings::get_singleton()->get("grid_map/preview_size");
145 		vr.height = EditorSettings::get_singleton()->get("grid_map/preview_size");
146 		VS::get_singleton()->viewport_set_rect(vp, vr);
147 		VS::get_singleton()->viewport_set_as_render_target(vp, true);
148 		VS::get_singleton()->viewport_set_render_target_update_mode(vp, VS::RENDER_TARGET_UPDATE_ALWAYS);
149 		RID scen = VS::get_singleton()->scenario_create();
150 		VS::get_singleton()->viewport_set_scenario(vp, scen);
151 		RID cam = VS::get_singleton()->camera_create();
152 		VS::get_singleton()->camera_set_transform(cam, Transform());
153 		VS::get_singleton()->viewport_attach_camera(vp, cam);
154 		RID light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL);
155 		RID lightinst = VS::get_singleton()->instance_create2(light, scen);
156 		VS::get_singleton()->camera_set_orthogonal(cam, 1.0, 0.01, 1000.0);
157 
158 		EditorProgress ep("mlib", TTR("Creating Mesh Library"), ids.size());
159 
160 		for (int i = 0; i < ids.size(); i++) {
161 
162 			int id = ids[i];
163 			Ref<Mesh> mesh = p_library->get_item_mesh(id);
164 			if (!mesh.is_valid())
165 				continue;
166 			AABB aabb = mesh->get_aabb();
167 			print_line("aabb: " + aabb);
168 			Vector3 ofs = aabb.pos + aabb.size * 0.5;
169 			aabb.pos -= ofs;
170 			Transform xform;
171 			xform.basis = Matrix3().rotated(Vector3(0, 1, 0), Math_PI * 0.25);
172 			xform.basis = Matrix3().rotated(Vector3(1, 0, 0), -Math_PI * 0.25) * xform.basis;
173 			AABB rot_aabb = xform.xform(aabb);
174 			print_line("rot_aabb: " + rot_aabb);
175 			float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
176 			if (m == 0)
177 				continue;
178 			m = 1.0 / m;
179 			m *= 0.5;
180 			print_line("scale: " + rtos(m));
181 			xform.basis.scale(Vector3(m, m, m));
182 			xform.origin = -xform.basis.xform(ofs); //-ofs*m;
183 			xform.origin.z -= rot_aabb.size.z * 2;
184 			RID inst = VS::get_singleton()->instance_create2(mesh->get_rid(), scen);
185 			VS::get_singleton()->instance_set_transform(inst, xform);
186 			ep.step(TTR("Thumbnail.."), i);
187 			VS::get_singleton()->viewport_queue_screen_capture(vp);
188 			Main::iteration();
189 			Image img = VS::get_singleton()->viewport_get_screen_capture(vp);
190 			ERR_CONTINUE(img.empty());
191 			Ref<ImageTexture> it(memnew(ImageTexture));
192 			it->create_from_image(img);
193 			p_library->set_item_preview(id, it);
194 
195 			//					print_line("loaded image, size: "+rtos(m)+" dist: "+rtos(dist)+" empty?"+itos(img.empty())+" w: "+itos(it->get_width())+" h: "+itos(it->get_height()));
196 			VS::get_singleton()->free(inst);
197 		}
198 
199 		VS::get_singleton()->free(lightinst);
200 		VS::get_singleton()->free(light);
201 		VS::get_singleton()->free(vp);
202 		VS::get_singleton()->free(cam);
203 		VS::get_singleton()->free(scen);
204 	}
205 }
206 
_import_scene_cbk(const String & p_str)207 void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
208 
209 	print_line("Impot Callback!");
210 
211 	Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
212 	ERR_FAIL_COND(ps.is_null());
213 	Node *scene = ps->instance();
214 
215 	_import_scene(scene, theme, option == MENU_OPTION_UPDATE_FROM_SCENE);
216 
217 	memdelete(scene);
218 	theme->set_meta("_editor_source_scene", p_str);
219 	menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
220 }
221 
update_library_file(Node * p_base_scene,Ref<MeshLibrary> ml,bool p_merge)222 Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge) {
223 
224 	_import_scene(p_base_scene, ml, p_merge);
225 	return OK;
226 }
227 
_menu_cbk(int p_option)228 void MeshLibraryEditor::_menu_cbk(int p_option) {
229 
230 	option = p_option;
231 	switch (p_option) {
232 
233 		case MENU_OPTION_ADD_ITEM: {
234 
235 			theme->create_item(theme->get_last_unused_item_id());
236 		} break;
237 		case MENU_OPTION_REMOVE_ITEM: {
238 
239 			String p = editor->get_property_editor()->get_selected_path();
240 			if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/") >= 3) {
241 
242 				to_erase = p.get_slice("/", 3).to_int();
243 				cd->set_text(vformat(TTR("Remove item %d?"), to_erase));
244 				cd->popup_centered(Size2(300, 60));
245 			}
246 		} break;
247 		case MENU_OPTION_IMPORT_FROM_SCENE: {
248 
249 			file->popup_centered_ratio();
250 		} break;
251 		case MENU_OPTION_UPDATE_FROM_SCENE: {
252 
253 			cd->set_text("Update from existing scene?:\n" + String(theme->get_meta("_editor_source_scene")));
254 			cd->popup_centered(Size2(500, 60));
255 		} break;
256 	}
257 }
258 
_bind_methods()259 void MeshLibraryEditor::_bind_methods() {
260 
261 	ObjectTypeDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
262 	ObjectTypeDB::bind_method("_menu_confirm", &MeshLibraryEditor::_menu_confirm);
263 	ObjectTypeDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
264 }
265 
MeshLibraryEditor(EditorNode * p_editor)266 MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
267 
268 	file = memnew(EditorFileDialog);
269 	file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
270 	//not for now?
271 	List<String> extensions;
272 	ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
273 	file->clear_filters();
274 	file->set_title(TTR("Import Scene"));
275 	for (int i = 0; i < extensions.size(); i++) {
276 
277 		file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
278 	}
279 	add_child(file);
280 	file->connect("file_selected", this, "_import_scene_cbk");
281 
282 	Panel *panel = memnew(Panel);
283 	panel->set_area_as_parent_rect();
284 	add_child(panel);
285 	MenuButton *options = memnew(MenuButton);
286 	panel->add_child(options);
287 	options->set_pos(Point2(1, 1));
288 	options->set_text("Theme");
289 	options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
290 	options->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
291 	options->get_popup()->add_separator();
292 	options->get_popup()->add_item(TTR("Import from Scene"), MENU_OPTION_IMPORT_FROM_SCENE);
293 	options->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
294 	options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
295 	options->get_popup()->connect("item_pressed", this, "_menu_cbk");
296 	menu = options;
297 	editor = p_editor;
298 	cd = memnew(ConfirmationDialog);
299 	add_child(cd);
300 	cd->get_ok()->connect("pressed", this, "_menu_confirm");
301 }
302 
edit(Object * p_node)303 void MeshLibraryEditorPlugin::edit(Object *p_node) {
304 
305 	if (p_node && p_node->cast_to<MeshLibrary>()) {
306 		theme_editor->edit(p_node->cast_to<MeshLibrary>());
307 		theme_editor->show();
308 	} else
309 		theme_editor->hide();
310 }
311 
handles(Object * p_node) const312 bool MeshLibraryEditorPlugin::handles(Object *p_node) const {
313 
314 	return p_node->is_type("MeshLibrary");
315 }
316 
make_visible(bool p_visible)317 void MeshLibraryEditorPlugin::make_visible(bool p_visible) {
318 
319 	if (p_visible)
320 		theme_editor->show();
321 	else
322 		theme_editor->hide();
323 }
324 
MeshLibraryEditorPlugin(EditorNode * p_node)325 MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
326 
327 	EDITOR_DEF("grid_map/preview_size", 64);
328 	theme_editor = memnew(MeshLibraryEditor(p_node));
329 
330 	p_node->get_viewport()->add_child(theme_editor);
331 	theme_editor->set_area_as_parent_rect();
332 	theme_editor->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
333 	theme_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
334 	theme_editor->set_end(Point2(0, 22));
335 	theme_editor->hide();
336 }
337