diff --git a/core/class_db.cpp b/core/class_db.cpp index 0d6c72afa8f..4b0e1b31f03 100644 --- a/core/class_db.cpp +++ b/core/class_db.cpp @@ -864,7 +864,7 @@ void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, cons MethodBind *mb_get = NULL; if (p_getter) { - MethodBind *mb_get = get_method(p_class, p_getter); + mb_get = get_method(p_class, p_getter); #ifdef DEBUG_METHODS_ENABLED if (!mb_get) { diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index 06e4bb5aa6f..b5d881ad5cc 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -32,10 +32,121 @@ #include "editor/editor_node.h" #include "editor/editor_settings.h" #include "editor_resource_preview.h" +#include "main/main.h" #include "plugins/canvas_item_editor_plugin.h" #include "plugins/spatial_editor_plugin.h" #include "scene/3d/camera.h" #include "scene/gui/popup_menu.h" +#include "servers/visual_server.h" +Array EditorInterface::_make_mesh_previews(const Array &p_meshes, int p_preview_size) { + + Vector > meshes; + + for (int i = 0; i < p_meshes.size(); i++) { + meshes.push_back(p_meshes[i]); + } + + Vector > textures = make_mesh_previews(meshes, p_preview_size); + Array ret; + for (int i = 0; i < textures.size(); i++) { + ret.push_back(textures[i]); + } + + return ret; +} + +Vector > EditorInterface::make_mesh_previews(const Vector > &p_meshes, int p_preview_size) { + + int size = p_preview_size; + + RID scenario = VS::get_singleton()->scenario_create(); + + RID viewport = VS::get_singleton()->viewport_create(); + VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ALWAYS); + VS::get_singleton()->viewport_set_vflip(viewport, true); + VS::get_singleton()->viewport_set_scenario(viewport, scenario); + VS::get_singleton()->viewport_set_size(viewport, size, size); + VS::get_singleton()->viewport_set_transparent_background(viewport, true); + VS::get_singleton()->viewport_set_active(viewport, true); + RID viewport_texture = VS::get_singleton()->viewport_get_texture(viewport); + + RID camera = VS::get_singleton()->camera_create(); + VS::get_singleton()->viewport_attach_camera(viewport, camera); + VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3))); + //VS::get_singleton()->camera_set_perspective(camera,45,0.1,10); + VS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0); + + RID light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); + RID light_instance = VS::get_singleton()->instance_create2(light, scenario); + VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0))); + + RID light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); + VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); + //VS::get_singleton()->light_set_color(light2, VS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0)); + RID light_instance2 = VS::get_singleton()->instance_create2(light2, scenario); + + VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))); + + //sphere = VS::get_singleton()->mesh_create(); + RID mesh_instance = VS::get_singleton()->instance_create(); + VS::get_singleton()->instance_set_scenario(mesh_instance, scenario); + + EditorProgress ep("mlib", TTR("Creating Mesh Previews"), p_meshes.size()); + + Vector > textures; + + for (int i = 0; i < p_meshes.size(); i++) { + + Ref mesh = p_meshes[i]; + if (!mesh.is_valid()) { + textures.push_back(Ref()); + continue; + } + Rect3 aabb = mesh->get_aabb(); + print_line("aabb: " + aabb); + Vector3 ofs = aabb.position + aabb.size * 0.5; + aabb.position -= ofs; + Transform xform; + xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.25); + xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.25) * xform.basis; + Rect3 rot_aabb = xform.xform(aabb); + print_line("rot_aabb: " + rot_aabb); + float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5; + if (m == 0) + continue; + m = 1.0 / m; + m *= 0.5; + print_line("scale: " + rtos(m)); + xform.basis.scale(Vector3(m, m, m)); + xform.origin = -xform.basis.xform(ofs); //-ofs*m; + xform.origin.z -= rot_aabb.size.z * 2; + RID inst = VS::get_singleton()->instance_create2(mesh->get_rid(), scenario); + VS::get_singleton()->instance_set_transform(inst, xform); + ep.step(TTR("Thumbnail.."), i); + Main::iteration(); + Main::iteration(); + Ref img = VS::get_singleton()->texture_get_data(viewport_texture); + ERR_CONTINUE(!img.is_valid() || img->empty()); + Ref it(memnew(ImageTexture)); + it->create_from_image(img); + + //print_line("loaded image, size: "+rtos(m)+" dist: "+rtos(dist)+" empty?"+itos(img.empty())+" w: "+itos(it->get_width())+" h: "+itos(it->get_height())); + VS::get_singleton()->free(inst); + + textures.push_back(it); + } + + VS::get_singleton()->free(mesh_instance); + VS::get_singleton()->free(viewport); + VS::get_singleton()->free(light); + VS::get_singleton()->free(light_instance); + VS::get_singleton()->free(light2); + VS::get_singleton()->free(light_instance2); + VS::get_singleton()->free(camera); + VS::get_singleton()->free(scenario); + + return textures; +} Control *EditorInterface::get_editor_viewport() { @@ -145,6 +256,7 @@ void EditorInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer); ClassDB::bind_method(D_METHOD("get_resource_filesystem"), &EditorInterface::get_resource_file_system); ClassDB::bind_method(D_METHOD("get_editor_viewport"), &EditorInterface::get_editor_viewport); + ClassDB::bind_method(D_METHOD("make_mesh_previews"), &EditorInterface::_make_mesh_previews); ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene); ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true)); diff --git a/editor/editor_plugin.h b/editor/editor_plugin.h index ce75cfd27ae..99328f8149f 100644 --- a/editor/editor_plugin.h +++ b/editor/editor_plugin.h @@ -61,6 +61,8 @@ protected: static void _bind_methods(); static EditorInterface *singleton; + Array _make_mesh_previews(const Array &p_meshes, int p_preview_size); + public: static EditorInterface *get_singleton() { return singleton; } @@ -86,6 +88,8 @@ public: Error save_scene(); void save_scene_as(const String &p_scene, bool p_with_preview = true); + Vector > make_mesh_previews(const Vector > &p_meshes, int p_preview_size); + EditorInterface(); }; diff --git a/editor/plugins/cube_grid_theme_editor_plugin.cpp b/editor/plugins/cube_grid_theme_editor_plugin.cpp index 36c0b44e384..08b38c2ca27 100644 --- a/editor/plugins/cube_grid_theme_editor_plugin.cpp +++ b/editor/plugins/cube_grid_theme_editor_plugin.cpp @@ -150,91 +150,17 @@ void MeshLibraryEditor::_import_scene(Node *p_scene, Ref p_library, //generate previews! if (1) { + + Vector > meshes; Vector ids = p_library->get_item_list(); - RID vp = VS::get_singleton()->viewport_create(); - int size = EditorSettings::get_singleton()->get("editors/grid_map/preview_size"); - - RID scenario = VS::get_singleton()->scenario_create(); - - RID viewport = VS::get_singleton()->viewport_create(); - VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ALWAYS); - VS::get_singleton()->viewport_set_vflip(viewport, true); - VS::get_singleton()->viewport_set_scenario(viewport, scenario); - VS::get_singleton()->viewport_set_size(viewport, size, size); - VS::get_singleton()->viewport_set_transparent_background(viewport, true); - VS::get_singleton()->viewport_set_active(viewport, true); - RID viewport_texture = VS::get_singleton()->viewport_get_texture(viewport); - - RID camera = VS::get_singleton()->camera_create(); - VS::get_singleton()->viewport_attach_camera(viewport, camera); - VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3))); - //VS::get_singleton()->camera_set_perspective(camera,45,0.1,10); - VS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0); - - RID light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); - RID light_instance = VS::get_singleton()->instance_create2(light, scenario); - VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0))); - - RID light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); - VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); - //VS::get_singleton()->light_set_color(light2, VS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0)); - RID light_instance2 = VS::get_singleton()->instance_create2(light2, scenario); - - VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))); - - //sphere = VS::get_singleton()->mesh_create(); - RID mesh_instance = VS::get_singleton()->instance_create(); - VS::get_singleton()->instance_set_scenario(mesh_instance, scenario); - - EditorProgress ep("mlib", TTR("Creating Mesh Library"), ids.size()); - for (int i = 0; i < ids.size(); i++) { - - int id = ids[i]; - Ref mesh = p_library->get_item_mesh(id); - if (!mesh.is_valid()) - continue; - Rect3 aabb = mesh->get_aabb(); - print_line("aabb: " + aabb); - Vector3 ofs = aabb.position + aabb.size * 0.5; - aabb.position -= ofs; - Transform xform; - xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.25); - xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.25) * xform.basis; - Rect3 rot_aabb = xform.xform(aabb); - print_line("rot_aabb: " + rot_aabb); - float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5; - if (m == 0) - continue; - m = 1.0 / m; - m *= 0.5; - print_line("scale: " + rtos(m)); - xform.basis.scale(Vector3(m, m, m)); - xform.origin = -xform.basis.xform(ofs); //-ofs*m; - xform.origin.z -= rot_aabb.size.z * 2; - RID inst = VS::get_singleton()->instance_create2(mesh->get_rid(), scenario); - VS::get_singleton()->instance_set_transform(inst, xform); - ep.step(TTR("Thumbnail.."), i); - Main::iteration(); - Main::iteration(); - Ref img = VS::get_singleton()->texture_get_data(viewport_texture); - ERR_CONTINUE(!img.is_valid() || img->empty()); - Ref it(memnew(ImageTexture)); - it->create_from_image(img); - p_library->set_item_preview(id, it); - - //print_line("loaded image, size: "+rtos(m)+" dist: "+rtos(dist)+" empty?"+itos(img.empty())+" w: "+itos(it->get_width())+" h: "+itos(it->get_height())); - VS::get_singleton()->free(inst); + meshes.push_back(p_library->get_item_mesh(ids[i])); } - VS::get_singleton()->free(mesh_instance); - VS::get_singleton()->free(viewport); - VS::get_singleton()->free(light); - VS::get_singleton()->free(light_instance); - VS::get_singleton()->free(light2); - VS::get_singleton()->free(light_instance2); - VS::get_singleton()->free(camera); - VS::get_singleton()->free(scenario); + Vector > textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, EditorSettings::get_singleton()->get("editors/grid_map/preview_size")); + for (int i = 0; i < ids.size(); i++) { + p_library->set_item_preview(ids[i], textures[i]); + } } } diff --git a/modules/gdscript/gd_editor.cpp b/modules/gdscript/gd_editor.cpp index 02fab6d17f2..b10694ddfd4 100644 --- a/modules/gdscript/gd_editor.cpp +++ b/modules/gdscript/gd_editor.cpp @@ -577,6 +577,7 @@ static bool _guess_expression_type(GDCompletionContext &context, const GDParser: const GDParser::OperatorNode *op = static_cast(p_node); if (op->op == GDParser::OperatorNode::OP_CALL) { + if (op->arguments[0]->type == GDParser::Node::TYPE_TYPE) { const GDParser::TypeNode *tn = static_cast(op->arguments[0]); @@ -589,21 +590,45 @@ static bool _guess_expression_type(GDCompletionContext &context, const GDParser: } else if (op->arguments.size() > 1 && op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) { + StringName id = static_cast(op->arguments[1])->name; + + if (op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER && String(id) == "new") { + + //shortcut + StringName identifier = static_cast(op->arguments[0])->name; + + if (ClassDB::class_exists(identifier)) { + r_type.type = Variant::OBJECT; + r_type.value = Variant(); + r_type.obj_type = identifier; + return true; + } + } + GDCompletionIdentifier base; if (!_guess_expression_type(context, op->arguments[0], p_line, base)) return false; - StringName id = static_cast(op->arguments[1])->name; - if (base.type == Variant::OBJECT) { if (id.operator String() == "new" && base.value.get_type() == Variant::OBJECT) { + Object *obj = base.value; - if (GDNativeClass *gdnc = Object::cast_to(obj)) { + if (obj && Object::cast_to(obj)) { + GDNativeClass *gdnc = Object::cast_to(obj); r_type.type = Variant::OBJECT; r_type.value = Variant(); r_type.obj_type = gdnc->get_name(); return true; + } else { + + if (base.obj_type != StringName()) { + + r_type.type = Variant::OBJECT; + r_type.value = Variant(); + r_type.obj_type = base.obj_type; + return true; + } } } diff --git a/scene/resources/mesh_library.cpp b/scene/resources/mesh_library.cpp index 6abbcfb90bd..4e1ffd2ab3c 100644 --- a/scene/resources/mesh_library.cpp +++ b/scene/resources/mesh_library.cpp @@ -268,10 +268,12 @@ void MeshLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh); ClassDB::bind_method(D_METHOD("set_item_navmesh", "id", "navmesh"), &MeshLibrary::set_item_navmesh); ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes); + ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview); ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name); ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh); ClassDB::bind_method(D_METHOD("get_item_navmesh", "id"), &MeshLibrary::get_item_navmesh); ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes); + ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview); ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item); ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear); ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);