2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* cube_grid_theme_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "cube_grid_theme_editor_plugin.h"
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "editor/editor_node.h"
|
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#include "main/main.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/3d/mesh_instance.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/3d/navigation_mesh.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/3d/physics_body.h"
|
|
|
|
#include "scene/main/viewport.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_theme) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
theme = p_theme;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (theme.is_valid())
|
2017-08-15 14:41:17 +00:00
|
|
|
menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !theme->has_meta("_editor_source_scene"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibraryEditor::_menu_confirm() {
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
switch (option) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
case MENU_OPTION_REMOVE_ITEM: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
theme->remove_item(to_erase);
|
|
|
|
} break;
|
|
|
|
case MENU_OPTION_UPDATE_FROM_SCENE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
String existing = theme->get_meta("_editor_source_scene");
|
2017-08-15 14:41:17 +00:00
|
|
|
ERR_FAIL_COND(existing == "");
|
2014-02-10 01:10:30 +00:00
|
|
|
_import_scene_cbk(existing);
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
} break;
|
|
|
|
default: {};
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
|
|
|
|
|
|
|
|
if (!p_merge)
|
|
|
|
p_library->clear();
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
for (int i = 0; i < p_scene->get_child_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Node *child = p_scene->get_child(i);
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
if (!Object::cast_to<MeshInstance>(child)) {
|
2017-08-15 14:41:17 +00:00
|
|
|
if (child->get_child_count() > 0) {
|
|
|
|
child = child->get_child(0);
|
2017-08-24 20:58:51 +00:00
|
|
|
if (!Object::cast_to<MeshInstance>(child)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
MeshInstance *mi = Object::cast_to<MeshInstance>(child);
|
2017-08-15 14:41:17 +00:00
|
|
|
Ref<Mesh> mesh = mi->get_mesh();
|
2014-02-10 01:10:30 +00:00
|
|
|
if (mesh.is_null())
|
2017-08-15 14:41:17 +00:00
|
|
|
continue;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-03 17:53:17 +00:00
|
|
|
int id = p_library->find_item_by_name(mi->get_name());
|
2017-08-15 14:41:17 +00:00
|
|
|
if (id < 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
id = p_library->get_last_unused_item_id();
|
2014-02-10 01:10:30 +00:00
|
|
|
p_library->create_item(id);
|
2017-08-15 14:41:17 +00:00
|
|
|
p_library->set_item_name(id, mi->get_name());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
p_library->set_item_mesh(id, mesh);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-27 19:00:59 +00:00
|
|
|
Vector<MeshLibrary::ShapeData> collisions;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
for (int j = 0; j < mi->get_child_count(); j++) {
|
2016-04-20 18:19:05 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Node *child2 = mi->get_child(j);
|
2017-08-24 20:58:51 +00:00
|
|
|
if (!Object::cast_to<StaticBody>(child2))
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2017-08-15 14:41:17 +00:00
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
StaticBody *sb = Object::cast_to<StaticBody>(child2);
|
2017-08-15 14:41:17 +00:00
|
|
|
List<uint32_t> shapes;
|
|
|
|
sb->get_shape_owners(&shapes);
|
|
|
|
|
|
|
|
for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
|
2017-09-04 10:48:14 +00:00
|
|
|
if (sb->is_shape_owner_disabled(E->get()))
|
|
|
|
continue;
|
2017-08-15 14:41:17 +00:00
|
|
|
|
|
|
|
//Transform shape_transform = sb->shape_owner_get_transform(E->get());
|
|
|
|
|
|
|
|
//shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
|
|
|
|
|
|
|
|
for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
|
|
|
|
|
2017-08-27 19:00:59 +00:00
|
|
|
Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
|
2017-09-04 10:48:14 +00:00
|
|
|
if (!collision.is_valid())
|
2017-08-27 19:00:59 +00:00
|
|
|
continue;
|
|
|
|
MeshLibrary::ShapeData shape_data;
|
|
|
|
shape_data.shape = collision;
|
|
|
|
shape_data.local_transform = sb->shape_owner_get_transform(E->get());
|
|
|
|
collisions.push_back(shape_data);
|
2017-08-15 14:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 19:00:59 +00:00
|
|
|
p_library->set_item_shapes(id, collisions);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-04-20 18:19:05 +00:00
|
|
|
Ref<NavigationMesh> navmesh;
|
2017-08-15 14:41:17 +00:00
|
|
|
for (int j = 0; j < mi->get_child_count(); j++) {
|
|
|
|
Node *child2 = mi->get_child(j);
|
2017-08-24 20:58:51 +00:00
|
|
|
if (!Object::cast_to<NavigationMeshInstance>(child2))
|
2017-08-15 14:41:17 +00:00
|
|
|
continue;
|
2017-08-24 20:58:51 +00:00
|
|
|
NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
|
2017-08-15 14:41:17 +00:00
|
|
|
navmesh = sb->get_navigation_mesh();
|
|
|
|
if (!navmesh.is_null())
|
|
|
|
break;
|
2016-04-20 18:19:05 +00:00
|
|
|
}
|
2017-08-15 14:41:17 +00:00
|
|
|
if (!navmesh.is_null()) {
|
2016-04-20 18:19:05 +00:00
|
|
|
p_library->set_item_navmesh(id, navmesh);
|
|
|
|
}
|
2017-08-15 14:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//generate previews!
|
|
|
|
|
|
|
|
if (1) {
|
|
|
|
|
2017-08-28 03:03:34 +00:00
|
|
|
Vector<Ref<Mesh> > meshes;
|
|
|
|
Vector<int> ids = p_library->get_item_list();
|
2017-08-15 14:41:17 +00:00
|
|
|
for (int i = 0; i < ids.size(); i++) {
|
2017-08-28 03:03:34 +00:00
|
|
|
meshes.push_back(p_library->get_item_mesh(ids[i]));
|
2017-08-15 14:41:17 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-28 03:03:34 +00:00
|
|
|
Vector<Ref<Texture> > 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]);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-05-04 13:28:37 +00:00
|
|
|
print_line("Impot Callback!");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(ps.is_null());
|
|
|
|
Node *scene = ps->instance();
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
_import_scene(scene, theme, option == MENU_OPTION_UPDATE_FROM_SCENE);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
memdelete(scene);
|
2017-08-15 14:41:17 +00:00
|
|
|
theme->set_meta("_editor_source_scene", p_str);
|
|
|
|
menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
_import_scene(p_base_scene, ml, p_merge);
|
2014-02-10 01:10:30 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibraryEditor::_menu_cbk(int p_option) {
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
option = p_option;
|
|
|
|
switch (p_option) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
case MENU_OPTION_ADD_ITEM: {
|
|
|
|
|
|
|
|
theme->create_item(theme->get_last_unused_item_id());
|
|
|
|
} break;
|
|
|
|
case MENU_OPTION_REMOVE_ITEM: {
|
|
|
|
|
|
|
|
String p = editor->get_property_editor()->get_selected_path();
|
2017-08-15 14:41:17 +00:00
|
|
|
if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/") >= 3) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
to_erase = p.get_slice("/", 3).to_int();
|
|
|
|
cd->set_text(vformat(TTR("Remove item %d?"), to_erase));
|
|
|
|
cd->popup_centered(Size2(300, 60));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case MENU_OPTION_IMPORT_FROM_SCENE: {
|
|
|
|
|
|
|
|
file->popup_centered_ratio();
|
|
|
|
} break;
|
2017-08-15 14:41:17 +00:00
|
|
|
case MENU_OPTION_UPDATE_FROM_SCENE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
cd->set_text("Update from existing scene?:\n" + String(theme->get_meta("_editor_source_scene")));
|
|
|
|
cd->popup_centered(Size2(500, 60));
|
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibraryEditor::_bind_methods() {
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
ClassDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
|
|
|
|
ClassDB::bind_method("_menu_confirm", &MeshLibraryEditor::_menu_confirm);
|
|
|
|
ClassDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
file = memnew(EditorFileDialog);
|
2015-06-06 12:44:38 +00:00
|
|
|
file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
|
2014-02-10 01:10:30 +00:00
|
|
|
//not for now?
|
|
|
|
List<String> extensions;
|
2017-08-15 14:41:17 +00:00
|
|
|
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
|
2014-02-10 01:10:30 +00:00
|
|
|
file->clear_filters();
|
2016-05-04 01:25:37 +00:00
|
|
|
file->set_title(TTR("Import Scene"));
|
2017-08-15 14:41:17 +00:00
|
|
|
for (int i = 0; i < extensions.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
add_child(file);
|
2017-08-15 14:41:17 +00:00
|
|
|
file->connect("file_selected", this, "_import_scene_cbk");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
Panel *panel = memnew(Panel);
|
2014-02-10 01:10:30 +00:00
|
|
|
panel->set_area_as_parent_rect();
|
|
|
|
add_child(panel);
|
2017-08-15 14:41:17 +00:00
|
|
|
MenuButton *options = memnew(MenuButton);
|
2014-02-10 01:10:30 +00:00
|
|
|
panel->add_child(options);
|
2017-08-15 14:41:17 +00:00
|
|
|
options->set_position(Point2(1, 1));
|
2014-02-10 01:10:30 +00:00
|
|
|
options->set_text("Theme");
|
2017-08-15 14:41:17 +00:00
|
|
|
options->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
|
|
|
|
options->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
|
2014-02-10 01:10:30 +00:00
|
|
|
options->get_popup()->add_separator();
|
2017-08-15 14:41:17 +00:00
|
|
|
options->get_popup()->add_item(TTR("Import from Scene"), MENU_OPTION_IMPORT_FROM_SCENE);
|
|
|
|
options->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
|
|
|
|
options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
|
|
|
|
options->get_popup()->connect("id_pressed", this, "_menu_cbk");
|
|
|
|
menu = options;
|
|
|
|
editor = p_editor;
|
2014-02-10 01:10:30 +00:00
|
|
|
cd = memnew(ConfirmationDialog);
|
|
|
|
add_child(cd);
|
2017-08-15 14:41:17 +00:00
|
|
|
cd->get_ok()->connect("pressed", this, "_menu_confirm");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibraryEditorPlugin::edit(Object *p_node) {
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
if (Object::cast_to<MeshLibrary>(p_node)) {
|
|
|
|
theme_editor->edit(Object::cast_to<MeshLibrary>(p_node));
|
2014-02-10 01:10:30 +00:00
|
|
|
theme_editor->show();
|
|
|
|
} else
|
|
|
|
theme_editor->hide();
|
|
|
|
}
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
bool MeshLibraryEditorPlugin::handles(Object *p_node) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
return p_node->is_class("MeshLibrary");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
void MeshLibraryEditorPlugin::make_visible(bool p_visible) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_visible)
|
|
|
|
theme_editor->show();
|
|
|
|
else
|
|
|
|
theme_editor->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
|
|
|
|
|
2017-08-15 14:41:17 +00:00
|
|
|
EDITOR_DEF("editors/grid_map/preview_size", 64);
|
|
|
|
theme_editor = memnew(MeshLibraryEditor(p_node));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
p_node->get_viewport()->add_child(theme_editor);
|
|
|
|
theme_editor->set_area_as_parent_rect();
|
2017-08-15 14:41:17 +00:00
|
|
|
theme_editor->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_BEGIN);
|
|
|
|
theme_editor->set_end(Point2(0, 22));
|
2016-03-08 23:00:52 +00:00
|
|
|
theme_editor->hide();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|