diff options
Diffstat (limited to 'editor/editor_plugin.cpp')
-rw-r--r-- | editor/editor_plugin.cpp | 326 |
1 files changed, 236 insertions, 90 deletions
diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index f8ed18bba4..86acfcc50e 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -3,7 +3,7 @@ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ -/* http://www.godotengine.org */ +/* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ @@ -32,55 +32,133 @@ #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) { -void EditorPlugin::add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture> &p_icon) { + Vector<Ref<Mesh> > meshes; - EditorNode::get_editor_data().add_custom_type(p_type, p_base, p_script, p_icon); -} + for (int i = 0; i < p_meshes.size(); i++) { + meshes.push_back(p_meshes[i]); + } -void EditorPlugin::remove_custom_type(const String &p_type) { + Vector<Ref<Texture> > textures = make_mesh_previews(meshes, p_preview_size); + Array ret; + for (int i = 0; i < textures.size(); i++) { + ret.push_back(textures[i]); + } - EditorNode::get_editor_data().remove_custom_type(p_type); + return ret; } -ToolButton *EditorPlugin::add_control_to_bottom_panel(Control *p_control, const String &p_title) { +Vector<Ref<Texture> > EditorInterface::make_mesh_previews(const Vector<Ref<Mesh> > &p_meshes, int p_preview_size) { - return EditorNode::get_singleton()->add_bottom_panel_item(p_title, p_control); -} + int size = p_preview_size; -void EditorPlugin::add_control_to_dock(DockSlot p_slot, Control *p_control) { + RID scenario = VS::get_singleton()->scenario_create(); - ERR_FAIL_NULL(p_control); - EditorNode::get_singleton()->add_control_to_dock(EditorNode::DockSlot(p_slot), p_control); -} + 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); -void EditorPlugin::remove_control_from_docks(Control *p_control) { + 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); - ERR_FAIL_NULL(p_control); - EditorNode::get_singleton()->remove_control_from_dock(p_control); -} + 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))); -void EditorPlugin::remove_control_from_bottom_panel(Control *p_control) { + 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); - ERR_FAIL_NULL(p_control); - EditorNode::get_singleton()->remove_bottom_panel_item(p_control); + 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<Ref<Texture> > textures; + + for (int i = 0; i < p_meshes.size(); i++) { + + Ref<Mesh> mesh = p_meshes[i]; + if (!mesh.is_valid()) { + textures.push_back(Ref<Texture>()); + 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<Image> img = VS::get_singleton()->texture_get_data(viewport_texture); + ERR_CONTINUE(!img.is_valid() || img->empty()); + Ref<ImageTexture> 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 *EditorPlugin::get_editor_viewport() { +Control *EditorInterface::get_editor_viewport() { return EditorNode::get_singleton()->get_viewport(); } -void EditorPlugin::edit_resource(const Ref<Resource> &p_resource) { +void EditorInterface::edit_resource(const Ref<Resource> &p_resource) { EditorNode::get_singleton()->edit_resource(p_resource); } -void EditorPlugin::open_scene_from_path(const String &scene_path) { +void EditorInterface::open_scene_from_path(const String &scene_path) { if (EditorNode::get_singleton()->is_changing_scene()) { return; @@ -89,7 +167,7 @@ void EditorPlugin::open_scene_from_path(const String &scene_path) { EditorNode::get_singleton()->open_request(scene_path); } -void EditorPlugin::reload_scene_from_path(const String &scene_path) { +void EditorInterface::reload_scene_from_path(const String &scene_path) { if (EditorNode::get_singleton()->is_changing_scene()) { return; @@ -98,6 +176,130 @@ void EditorPlugin::reload_scene_from_path(const String &scene_path) { EditorNode::get_singleton()->reload_scene(scene_path); } +Node *EditorInterface::get_edited_scene_root() { + return EditorNode::get_singleton()->get_edited_scene(); +} + +Array EditorInterface::get_open_scenes() const { + + Array ret; + Vector<EditorData::EditedScene> scenes = EditorNode::get_singleton()->get_editor_data().get_edited_scenes(); + + int scns_amount = scenes.size(); + for (int idx_scn = 0; idx_scn < scns_amount; idx_scn++) { + if (scenes[idx_scn].root == NULL) + continue; + ret.push_back(scenes[idx_scn].root->get_filename()); + } + return ret; +} + +ScriptEditor *EditorInterface::get_script_editor() { + return ScriptEditor::get_singleton(); +} + +void EditorInterface::inspect_object(Object *p_obj, const String &p_for_property) { + + EditorNode::get_singleton()->push_item(p_obj, p_for_property); +} + +EditorFileSystem *EditorInterface::get_resource_file_system() { + return EditorFileSystem::get_singleton(); +} + +EditorSelection *EditorInterface::get_selection() { + return EditorNode::get_singleton()->get_editor_selection(); +} + +EditorSettings *EditorInterface::get_editor_settings() { + return EditorSettings::get_singleton(); +} + +EditorResourcePreview *EditorInterface::get_resource_previewer() { + return EditorResourcePreview::get_singleton(); +} + +Control *EditorInterface::get_base_control() { + + return EditorNode::get_singleton()->get_gui_base(); +} + +Error EditorInterface::save_scene() { + if (!get_edited_scene_root()) + return ERR_CANT_CREATE; + if (get_edited_scene_root()->get_filename() == String()) + return ERR_CANT_CREATE; + + save_scene_as(get_edited_scene_root()->get_filename()); + return OK; +} + +void EditorInterface::save_scene_as(const String &p_scene, bool p_with_preview) { + + EditorNode::get_singleton()->save_scene_to_path(p_scene, p_with_preview); +} + +EditorInterface *EditorInterface::singleton = NULL; + +void EditorInterface::_bind_methods() { + + ClassDB::bind_method(D_METHOD("inspect_object", "object", "for_property"), &EditorInterface::inspect_object, DEFVAL(String())); + ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection); + ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings); + ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor); + ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control); + ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorInterface::edit_resource); + ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath"), &EditorInterface::open_scene_from_path); + ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorInterface::reload_scene_from_path); + ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorInterface::get_open_scenes); + ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root); + 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)); +} + +EditorInterface::EditorInterface() { + singleton = this; +} + +/////////////////////////////////////////// +void EditorPlugin::add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture> &p_icon) { + + EditorNode::get_editor_data().add_custom_type(p_type, p_base, p_script, p_icon); +} + +void EditorPlugin::remove_custom_type(const String &p_type) { + + EditorNode::get_editor_data().remove_custom_type(p_type); +} + +ToolButton *EditorPlugin::add_control_to_bottom_panel(Control *p_control, const String &p_title) { + + return EditorNode::get_singleton()->add_bottom_panel_item(p_title, p_control); +} + +void EditorPlugin::add_control_to_dock(DockSlot p_slot, Control *p_control) { + + ERR_FAIL_NULL(p_control); + EditorNode::get_singleton()->add_control_to_dock(EditorNode::DockSlot(p_slot), p_control); +} + +void EditorPlugin::remove_control_from_docks(Control *p_control) { + + ERR_FAIL_NULL(p_control); + EditorNode::get_singleton()->remove_control_from_dock(p_control); +} + +void EditorPlugin::remove_control_from_bottom_panel(Control *p_control) { + + ERR_FAIL_NULL(p_control); + EditorNode::get_singleton()->remove_bottom_panel_item(p_control); +} + void EditorPlugin::add_control_to_container(CustomControlContainer p_location, Control *p_control) { switch (p_location) { @@ -155,7 +357,7 @@ void EditorPlugin::add_tool_menu_item(const String &p_name, Object *p_handler, c void EditorPlugin::add_tool_submenu_item(const String &p_name, Object *p_submenu) { ERR_FAIL_NULL(p_submenu); - PopupMenu *submenu = p_submenu->cast_to<PopupMenu>(); + PopupMenu *submenu = Object::cast_to<PopupMenu>(p_submenu); ERR_FAIL_NULL(submenu); //EditorNode::get_singleton()->add_tool_submenu_item(p_name, submenu); } @@ -171,28 +373,6 @@ void EditorPlugin::set_input_event_forwarding_always_enabled() { always_input_forwarding_list->add_plugin(this); } -Node *EditorPlugin::get_edited_scene_root() { - return EditorNode::get_singleton()->get_edited_scene(); -} - -Array EditorPlugin::get_open_scenes() const { - - Array ret; - Vector<EditorData::EditedScene> scenes = EditorNode::get_singleton()->get_editor_data().get_edited_scenes(); - - int scns_amount = scenes.size(); - for (int idx_scn = 0; idx_scn < scns_amount; idx_scn++) { - if (scenes[idx_scn].root == NULL) - continue; - ret.push_back(scenes[idx_scn].root->get_filename()); - } - return ret; -} - -ScriptEditor *EditorPlugin::get_script_editor() { - return ScriptEditor::get_singleton(); -} - void EditorPlugin::notify_scene_changed(const Node *scn_root) { if (scn_root == NULL) return; emit_signal("scene_changed", scn_root); @@ -369,23 +549,6 @@ void EditorPlugin::queue_save_layout() const { EditorNode::get_singleton()->save_layout(); } -EditorSelection *EditorPlugin::get_selection() { - return EditorNode::get_singleton()->get_editor_selection(); -} - -EditorSettings *EditorPlugin::get_editor_settings() { - return EditorSettings::get_singleton(); -} - -EditorResourcePreview *EditorPlugin::get_resource_previewer() { - return EditorResourcePreview::get_singleton(); -} - -Control *EditorPlugin::get_base_control() { - - return EditorNode::get_singleton()->get_gui_base(); -} - void EditorPlugin::make_bottom_panel_item_visible(Control *p_item) { EditorNode::get_singleton()->make_bottom_panel_item_visible(p_item); @@ -396,13 +559,8 @@ void EditorPlugin::hide_bottom_panel() { EditorNode::get_singleton()->hide_bottom_panel(); } -void EditorPlugin::inspect_object(Object *p_obj, const String &p_for_property) { - - EditorNode::get_singleton()->push_item(p_obj, p_for_property); -} - -EditorFileSystem *EditorPlugin::get_resource_file_system() { - return EditorFileSystem::get_singleton(); +EditorInterface *EditorPlugin::get_editor_interface() { + return EditorInterface::get_singleton(); } void EditorPlugin::_bind_methods() { @@ -417,36 +575,24 @@ void EditorPlugin::_bind_methods() { //ClassDB::bind_method(D_METHOD("remove_tool_menu_item", "name"),&EditorPlugin::remove_tool_menu_item); ClassDB::bind_method(D_METHOD("add_custom_type", "type", "base", "script", "icon"), &EditorPlugin::add_custom_type); ClassDB::bind_method(D_METHOD("remove_custom_type", "type"), &EditorPlugin::remove_custom_type); - ClassDB::bind_method(D_METHOD("get_editor_viewport"), &EditorPlugin::get_editor_viewport); - ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorPlugin::get_resource_previewer); - ClassDB::bind_method(D_METHOD("get_resource_filesystem"), &EditorPlugin::get_resource_file_system); - - ClassDB::bind_method(D_METHOD("inspect_object", "object", "for_property"), &EditorPlugin::inspect_object, DEFVAL(String())); ClassDB::bind_method(D_METHOD("update_canvas"), &EditorPlugin::update_canvas); ClassDB::bind_method(D_METHOD("make_bottom_panel_item_visible", "item"), &EditorPlugin::make_bottom_panel_item_visible); ClassDB::bind_method(D_METHOD("hide_bottom_panel"), &EditorPlugin::hide_bottom_panel); - ClassDB::bind_method(D_METHOD("get_base_control"), &EditorPlugin::get_base_control); ClassDB::bind_method(D_METHOD("get_undo_redo"), &EditorPlugin::_get_undo_redo); - ClassDB::bind_method(D_METHOD("get_selection"), &EditorPlugin::get_selection); - ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorPlugin::get_editor_settings); - ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorPlugin::get_script_editor); ClassDB::bind_method(D_METHOD("queue_save_layout"), &EditorPlugin::queue_save_layout); - ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorPlugin::edit_resource); - ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath"), &EditorPlugin::open_scene_from_path); - ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorPlugin::reload_scene_from_path); ClassDB::bind_method(D_METHOD("add_import_plugin", "importer"), &EditorPlugin::add_import_plugin); ClassDB::bind_method(D_METHOD("remove_import_plugin", "importer"), &EditorPlugin::remove_import_plugin); ClassDB::bind_method(D_METHOD("set_input_event_forwarding_always_enabled"), &EditorPlugin::set_input_event_forwarding_always_enabled); - ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorPlugin::get_open_scenes); - ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorPlugin::get_edited_scene_root); + + ClassDB::bind_method(D_METHOD("get_editor_interface"), &EditorPlugin::get_editor_interface); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_canvas_gui_input", PropertyInfo(Variant::TRANSFORM2D, "canvas_xform"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); - ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_draw_over_canvas", PropertyInfo(Variant::TRANSFORM2D, "canvas_xform"), PropertyInfo(Variant::OBJECT, "canvas:Control"))); + ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_draw_over_canvas", PropertyInfo(Variant::TRANSFORM2D, "canvas_xform"), PropertyInfo(Variant::OBJECT, "canvas", PROPERTY_HINT_RESOURCE_TYPE, "Control"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_spatial_gui_input", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); - MethodInfo gizmo = MethodInfo(Variant::OBJECT, "create_spatial_gizmo", PropertyInfo(Variant::OBJECT, "for_spatial:Spatial")); + MethodInfo gizmo = MethodInfo(Variant::OBJECT, "create_spatial_gizmo", PropertyInfo(Variant::OBJECT, "for_spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")); gizmo.return_val.hint = PROPERTY_HINT_RESOURCE_TYPE; gizmo.return_val.hint_string = "EditorSpatialGizmo"; ClassDB::add_virtual_method(get_class_static(), gizmo); @@ -464,9 +610,9 @@ void EditorPlugin::_bind_methods() { ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo("get_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile"))); - ADD_SIGNAL(MethodInfo("scene_changed", PropertyInfo(Variant::OBJECT, "scene_root:Node"))); - ADD_SIGNAL(MethodInfo("scene_closed", PropertyInfo(Variant::STRING, "filepath:String"))); - ADD_SIGNAL(MethodInfo("main_screen_changed", PropertyInfo(Variant::STRING, "screen_name:String"))); + ADD_SIGNAL(MethodInfo("scene_changed", PropertyInfo(Variant::OBJECT, "scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node"))); + ADD_SIGNAL(MethodInfo("scene_closed", PropertyInfo(Variant::STRING, "filepath"))); + ADD_SIGNAL(MethodInfo("main_screen_changed", PropertyInfo(Variant::STRING, "screen_name"))); BIND_ENUM_CONSTANT(CONTAINER_TOOLBAR); BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_MENU); |