2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* mesh_library.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
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "mesh_library.h"
|
2019-09-24 14:05:49 +00:00
|
|
|
#include "core/engine.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (name.begins_with("item/")) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = name.get_slicec('/', 1).to_int();
|
|
|
|
String what = name.get_slicec('/', 2);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!item_map.has(idx))
|
|
|
|
create_item(idx);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (what == "name")
|
|
|
|
set_item_name(idx, p_value);
|
|
|
|
else if (what == "mesh")
|
|
|
|
set_item_mesh(idx, p_value);
|
2017-08-27 19:00:59 +00:00
|
|
|
else if (what == "shape") {
|
|
|
|
Vector<ShapeData> shapes;
|
|
|
|
ShapeData sd;
|
|
|
|
sd.shape = p_value;
|
|
|
|
shapes.push_back(sd);
|
|
|
|
set_item_shapes(idx, shapes);
|
|
|
|
} else if (what == "shapes") {
|
|
|
|
_set_item_shapes(idx, p_value);
|
|
|
|
} else if (what == "preview")
|
2017-03-05 15:44:50 +00:00
|
|
|
set_item_preview(idx, p_value);
|
|
|
|
else if (what == "navmesh")
|
|
|
|
set_item_navmesh(idx, p_value);
|
2019-02-26 13:38:23 +00:00
|
|
|
else if (what == "navmesh_transform")
|
|
|
|
set_item_navmesh_transform(idx, p_value);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
|
|
|
|
String name = p_name;
|
|
|
|
int idx = name.get_slicec('/', 1).to_int();
|
|
|
|
ERR_FAIL_COND_V(!item_map.has(idx), false);
|
|
|
|
String what = name.get_slicec('/', 2);
|
|
|
|
|
|
|
|
if (what == "name")
|
|
|
|
r_ret = get_item_name(idx);
|
|
|
|
else if (what == "mesh")
|
|
|
|
r_ret = get_item_mesh(idx);
|
2017-08-27 19:00:59 +00:00
|
|
|
else if (what == "shapes")
|
|
|
|
r_ret = _get_item_shapes(idx);
|
2017-03-05 15:44:50 +00:00
|
|
|
else if (what == "navmesh")
|
|
|
|
r_ret = get_item_navmesh(idx);
|
2019-02-26 13:38:23 +00:00
|
|
|
else if (what == "navmesh_transform")
|
|
|
|
r_ret = get_item_navmesh_transform(idx);
|
2017-03-05 15:44:50 +00:00
|
|
|
else if (what == "preview")
|
|
|
|
r_ret = get_item_preview(idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, Item>::Element *E = item_map.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String name = "item/" + itos(E->key()) + "/";
|
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, name + "name"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, name + "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
|
2019-02-26 13:38:23 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::TRANSFORM, name + "mesh_transform"));
|
2017-08-27 19:00:59 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::ARRAY, name + "shapes"));
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, name + "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
|
2019-02-26 13:38:23 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::TRANSFORM, name + "navmesh_transform"));
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, name + "preview", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_HELPER));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibrary::create_item(int p_item) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_item < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(item_map.has(p_item));
|
2017-03-05 15:44:50 +00:00
|
|
|
item_map[p_item] = Item();
|
2014-02-10 01:10:30 +00:00
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshLibrary::set_item_name(int p_item, const String &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2017-03-05 15:44:50 +00:00
|
|
|
item_map[p_item].name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
2019-02-26 13:38:23 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2017-03-05 15:44:50 +00:00
|
|
|
item_map[p_item].mesh = p_mesh;
|
2014-02-10 01:10:30 +00:00
|
|
|
notify_change_to_owners();
|
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-08-27 19:00:59 +00:00
|
|
|
void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2017-08-27 19:00:59 +00:00
|
|
|
item_map[p_item].shapes = p_shapes;
|
2014-02-10 01:10:30 +00:00
|
|
|
_change_notify();
|
|
|
|
notify_change_to_owners();
|
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshLibrary::set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navmesh) {
|
2016-04-20 18:19:05 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2017-03-05 15:44:50 +00:00
|
|
|
item_map[p_item].navmesh = p_navmesh;
|
2016-04-20 18:19:05 +00:00
|
|
|
_change_notify();
|
|
|
|
notify_change_to_owners();
|
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2019-02-26 13:38:23 +00:00
|
|
|
void MeshLibrary::set_item_navmesh_transform(int p_item, const Transform &p_transform) {
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2019-02-26 13:38:23 +00:00
|
|
|
item_map[p_item].navmesh_transform = p_transform;
|
|
|
|
notify_change_to_owners();
|
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshLibrary::set_item_preview(int p_item, const Ref<Texture> &p_preview) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2017-03-05 15:44:50 +00:00
|
|
|
item_map[p_item].preview = p_preview;
|
2014-02-10 01:10:30 +00:00
|
|
|
emit_changed();
|
|
|
|
_change_notify();
|
|
|
|
}
|
2019-02-26 13:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String MeshLibrary::get_item_name(int p_item) const {
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
return item_map[p_item].name;
|
|
|
|
}
|
2019-02-26 13:38:23 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
return item_map[p_item].mesh;
|
|
|
|
}
|
|
|
|
|
2017-08-27 19:00:59 +00:00
|
|
|
Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2017-08-27 19:00:59 +00:00
|
|
|
return item_map[p_item].shapes;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 18:19:05 +00:00
|
|
|
Ref<NavigationMesh> MeshLibrary::get_item_navmesh(int p_item) const {
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2016-04-20 18:19:05 +00:00
|
|
|
return item_map[p_item].navmesh;
|
|
|
|
}
|
|
|
|
|
2019-02-26 13:38:23 +00:00
|
|
|
Transform MeshLibrary::get_item_navmesh_transform(int p_item) const {
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2019-02-26 13:38:23 +00:00
|
|
|
return item_map[p_item].navmesh_transform;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Texture> MeshLibrary::get_item_preview(int p_item) const {
|
|
|
|
|
2019-09-24 14:05:49 +00:00
|
|
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
ERR_PRINT("MeshLibrary item previews are only generated in an editor context, which means they aren't available in a running project.");
|
|
|
|
return Ref<Texture>();
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
return item_map[p_item].preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MeshLibrary::has_item(int p_item) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return item_map.has(p_item);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
void MeshLibrary::remove_item(int p_item) {
|
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
item_map.erase(p_item);
|
|
|
|
notify_change_to_owners();
|
|
|
|
_change_notify();
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshLibrary::clear() {
|
|
|
|
|
|
|
|
item_map.clear();
|
|
|
|
notify_change_to_owners();
|
|
|
|
_change_notify();
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<int> MeshLibrary::get_item_list() const {
|
|
|
|
|
|
|
|
Vector<int> ret;
|
|
|
|
ret.resize(item_map.size());
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 0;
|
|
|
|
for (Map<int, Item>::Element *E = item_map.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
ret.write[idx++] = E->key();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:53:17 +00:00
|
|
|
int MeshLibrary::find_item_by_name(const String &p_name) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, Item>::Element *E = item_map.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (E->get().name == p_name)
|
2014-02-10 01:10:30 +00:00
|
|
|
return E->key();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeshLibrary::get_last_unused_item_id() const {
|
|
|
|
|
|
|
|
if (!item_map.size())
|
|
|
|
return 0;
|
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
return item_map.back()->key() + 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 19:00:59 +00:00
|
|
|
void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(p_shapes.size() & 1);
|
|
|
|
Vector<ShapeData> shapes;
|
|
|
|
for (int i = 0; i < p_shapes.size(); i += 2) {
|
|
|
|
ShapeData sd;
|
|
|
|
sd.shape = p_shapes[i + 0];
|
|
|
|
sd.local_transform = p_shapes[i + 1];
|
|
|
|
|
|
|
|
if (sd.shape.is_valid()) {
|
|
|
|
shapes.push_back(sd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_item_shapes(p_item, shapes);
|
|
|
|
}
|
|
|
|
|
|
|
|
Array MeshLibrary::_get_item_shapes(int p_item) const {
|
|
|
|
|
|
|
|
Vector<ShapeData> shapes = get_item_shapes(p_item);
|
|
|
|
Array ret;
|
|
|
|
for (int i = 0; i < shapes.size(); i++) {
|
|
|
|
ret.push_back(shapes[i].shape);
|
|
|
|
ret.push_back(shapes[i].local_transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void MeshLibrary::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
|
2017-08-09 11:19:41 +00:00
|
|
|
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);
|
2019-02-26 13:38:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_navmesh_transform", "id", "navmesh"), &MeshLibrary::set_item_navmesh_transform);
|
2017-08-27 19:00:59 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
|
2017-08-28 03:03:34 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
|
2017-08-09 11:19:41 +00:00
|
|
|
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);
|
2019-02-26 13:38:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_navmesh_transform", "id"), &MeshLibrary::get_item_navmesh_transform);
|
2017-08-27 19:00:59 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
|
2017-08-28 03:03:34 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
|
2017-09-03 17:53:17 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeshLibrary::MeshLibrary() {
|
|
|
|
}
|
|
|
|
MeshLibrary::~MeshLibrary() {
|
|
|
|
}
|