2016-05-01 22:20:27 +02:00
|
|
|
#include "voxel_library.h"
|
|
|
|
|
2017-08-15 02:24:52 +02:00
|
|
|
VoxelLibrary::VoxelLibrary() :
|
2019-04-24 02:29:47 +02:00
|
|
|
Resource(),
|
|
|
|
_atlas_size(1) {
|
2019-05-17 18:38:04 +02:00
|
|
|
|
|
|
|
_voxel_editor_count = 0;
|
|
|
|
_voxel_editor_page = 0;
|
2016-05-01 22:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelLibrary::~VoxelLibrary() {
|
2017-08-15 02:24:52 +02:00
|
|
|
// Handled with a WeakRef
|
|
|
|
// for (unsigned int i = 0; i < MAX_VOXEL_TYPES; ++i) {
|
|
|
|
// if (_voxel_types[i].is_valid()) {
|
|
|
|
// _voxel_types[i]->set_library(NULL);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelLibrary::get_voxel_count() const {
|
|
|
|
int count = 0;
|
2019-04-24 02:29:47 +02:00
|
|
|
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
|
|
|
|
if (_voxel_types[i].is_valid())
|
2017-08-15 02:24:52 +02:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelLibrary::load_default() {
|
|
|
|
create_voxel(0, "air")->set_transparent(true);
|
|
|
|
create_voxel(1, "solid")
|
|
|
|
->set_transparent(false)
|
|
|
|
->set_geometry_type(Voxel::GEOMETRY_CUBE);
|
|
|
|
}
|
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
int VoxelLibrary::get_voxel_editor_count() {
|
|
|
|
return _voxel_editor_count;
|
2017-08-15 02:24:52 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
void VoxelLibrary::set_voxel_editor_count(int value) {
|
|
|
|
_voxel_editor_count = value;
|
|
|
|
}
|
2017-08-15 02:24:52 +02:00
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
int VoxelLibrary::get_voxel_editor_page() {
|
|
|
|
return _voxel_editor_page;
|
|
|
|
}
|
2017-08-15 02:24:52 +02:00
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
void VoxelLibrary::set_voxel_editor_page(int value) {
|
|
|
|
if (value < 0 || value > (int)(_voxel_editor_count / ITEMS_PER_PAGE)) {
|
|
|
|
return;
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
2017-08-15 02:24:52 +02:00
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
_voxel_editor_page = value;
|
2017-08-15 02:24:52 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 22:20:27 +02:00
|
|
|
|
|
|
|
void VoxelLibrary::set_atlas_size(int s) {
|
2017-01-01 04:40:16 +01:00
|
|
|
ERR_FAIL_COND(s <= 0);
|
|
|
|
_atlas_size = s;
|
2016-05-01 22:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Voxel> VoxelLibrary::create_voxel(int id, String name) {
|
2017-01-01 04:40:16 +01:00
|
|
|
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<Voxel>());
|
2019-05-17 18:38:04 +02:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
Ref<Voxel> voxel(memnew(Voxel));
|
2019-05-17 18:38:04 +02:00
|
|
|
|
2017-08-15 02:24:52 +02:00
|
|
|
voxel->set_library(Ref<VoxelLibrary>(this));
|
2017-01-01 04:40:16 +01:00
|
|
|
voxel->set_id(id);
|
2017-08-15 02:24:52 +02:00
|
|
|
voxel->set_voxel_name(name);
|
2017-01-01 04:40:16 +01:00
|
|
|
_voxel_types[id] = voxel;
|
2019-05-17 18:38:04 +02:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
return voxel;
|
2016-05-01 22:20:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
void VoxelLibrary::add_voxel(Ref<Voxel> voxel) {
|
|
|
|
int index = get_voxel_count();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(index >= MAX_VOXEL_TYPES);
|
|
|
|
|
|
|
|
voxel->set_id(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelLibrary::set_voxel(int id, Ref<Voxel> voxel) {
|
|
|
|
ERR_FAIL_COND(id < 0 || id >= MAX_VOXEL_TYPES);
|
|
|
|
|
|
|
|
voxel->set_id(id);
|
|
|
|
|
|
|
|
_voxel_types[id] = voxel;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Voxel> VoxelLibrary::get_voxel(int id) {
|
2017-01-01 05:23:22 +01:00
|
|
|
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<Voxel>());
|
2019-05-17 18:38:04 +02:00
|
|
|
|
2017-01-01 05:23:22 +01:00
|
|
|
return _voxel_types[id];
|
|
|
|
}
|
|
|
|
|
2019-05-17 18:38:04 +02:00
|
|
|
void VoxelLibrary::remove_voxel(int id) {
|
|
|
|
ERR_FAIL_COND(id < 0 || id >= MAX_VOXEL_TYPES);
|
|
|
|
|
|
|
|
_voxel_types[id] = Ref<Voxel>(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelLibrary::_validate_property(PropertyInfo &property) const {
|
|
|
|
String prop = property.name;
|
|
|
|
if (prop.begins_with("Voxel_")) {
|
|
|
|
int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
|
|
|
|
if (frame >= _voxel_editor_count || frame < ITEMS_PER_PAGE * _voxel_editor_page || frame > ITEMS_PER_PAGE * (_voxel_editor_page + 1)) {
|
|
|
|
property.usage = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:20:27 +02:00
|
|
|
void VoxelLibrary::_bind_methods() {
|
|
|
|
|
2017-08-13 00:08:53 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("create_voxel", "id", "name"), &VoxelLibrary::create_voxel);
|
2017-01-01 05:23:22 +01:00
|
|
|
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_atlas_size", "square_size"), &VoxelLibrary::set_atlas_size);
|
2017-08-15 02:24:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_atlas_size"), &VoxelLibrary::get_atlas_size);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_size"), "set_atlas_size", "get_atlas_size");
|
2019-05-17 18:38:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_voxel_editor_count"), &VoxelLibrary::get_voxel_editor_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_voxel_editor_count", "value"), &VoxelLibrary::set_voxel_editor_count);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "voxel_editor_count", PROPERTY_HINT_RANGE, "0," + itos(MAX_VOXEL_TYPES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_voxel_editor_count", "get_voxel_editor_count");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_voxel_editor_page"), &VoxelLibrary::get_voxel_editor_page);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_voxel_editor_page", "value"), &VoxelLibrary::set_voxel_editor_page);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "voxel_editor_page", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_voxel_editor_page", "get_voxel_editor_page");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("has_voxel", "id"), &VoxelLibrary::has_voxel);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_voxel", "voxel"), &VoxelLibrary::add_voxel);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_voxel", "id"), &VoxelLibrary::set_voxel);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_voxel_count"), &VoxelLibrary::get_voxel_count);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_voxel", "id"), &VoxelLibrary::get_voxel);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_voxel", "id", "voxel"), &VoxelLibrary::set_voxel);
|
|
|
|
|
|
|
|
ADD_GROUP("Voxel", "Voxel");
|
|
|
|
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "Voxel_" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "Voxel", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_voxel", "get_voxel", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
BIND_CONSTANT(MAX_VOXEL_TYPES);
|
2016-05-01 22:20:27 +02:00
|
|
|
}
|