Ran into an example of binding variant arrays with a specific type, so now the voxel surface array is bound like that.

This commit is contained in:
Relintai 2019-09-06 04:31:57 +02:00
parent b32a028209
commit 34e0252066
2 changed files with 44 additions and 91 deletions

View File

@ -5,77 +5,70 @@ VoxelmanLibrary::VoxelmanLibrary() {
_atlas_rows = 8;
_atlas_columns = 8;
_voxel_types_count = 0;
_voxel_types_page = 0;
_is_textured = true;
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
if (_voxel_types[i] != NULL) {
_voxel_types[i]->set_library(Ref<VoxelmanLibrary>(this));
}
}
}
VoxelmanLibrary::~VoxelmanLibrary() {
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
if (_voxel_types[i].is_valid()) {
_voxel_types[i]->set_library(Ref<VoxelmanLibrary>(NULL));
_voxel_types[i].unref();
for (int i = 0; i < _voxel_surfaces.size(); ++i) {
if (_voxel_surfaces[i].is_valid()) {
_voxel_surfaces.get(i)->set_library(Ref<VoxelmanLibrary>(NULL));
_voxel_surfaces.get(i).unref();
}
}
_voxel_surfaces.clear();
_material.unref();
_prop_material.unref();
}
Ref<VoxelSurface> VoxelmanLibrary::get_voxel_surface(int index) const {
ERR_FAIL_INDEX_V(index, _voxel_types_count, Ref<VoxelSurface>(NULL));
ERR_FAIL_INDEX_V(index, _voxel_surfaces.size(), Ref<VoxelSurface>(NULL));
return _voxel_types[index];
return _voxel_surfaces[index];
}
void VoxelmanLibrary::set_voxel_surface(int index, Ref<VoxelSurface> value) {
ERR_FAIL_COND(index < 0 || index > _voxel_types_count);
ERR_FAIL_COND(index < 0);
if (_voxel_types[index].is_valid()) {
_voxel_types[index]->set_library(Ref<VoxelmanLibrary>(NULL));
if (_voxel_surfaces.size() < index) {
_voxel_surfaces.resize(index + 1);
}
if (_voxel_surfaces[index].is_valid()) {
_voxel_surfaces.get(index)->set_library(Ref<VoxelmanLibrary>(NULL));
}
if (value.is_valid()) {
value->set_library(Ref<VoxelmanLibrary>(this));
_voxel_types[index] = Ref<VoxelSurface>(value);
_voxel_surfaces.set(index, Ref<VoxelSurface>(value));
}
}
Vector<Variant> VoxelmanLibrary::get_voxel_surfaces() {
Vector<Variant> r;
for (int i = 0; i < _voxel_surfaces.size(); i++) {
r.push_back(_voxel_surfaces[i].get_ref_ptr());
}
return r;
}
void VoxelmanLibrary::set_voxel_surfaces(const Vector<Variant> &effects) {
_voxel_surfaces.clear();
for (int i = 0; i < effects.size(); i++) {
Ref<VoxelSurface> surface = Ref<VoxelSurface>(effects[i]);
surface->set_library(this);
_voxel_surfaces.push_back(surface);
}
}
int VoxelmanLibrary::get_voxel_types_count() {
return _voxel_types_count;
}
void VoxelmanLibrary::set_voxel_types_count(int value) {
_voxel_types_count = value;
}
int VoxelmanLibrary::get_voxel_types_page() {
return _voxel_types_page;
}
void VoxelmanLibrary::set_voxel_types_page(int value) {
if (value < 0 || value > (int)(_voxel_types_count / ITEMS_PER_PAGE)) {
return;
}
_voxel_types_page = value;
return _voxel_surfaces.size();
}
int VoxelmanLibrary::get_voxel_count() const {
int count = 0;
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
if (_voxel_types[i].is_valid())
++count;
}
return count;
return _voxel_surfaces.size();
}
void VoxelmanLibrary::set_atlas_columns(int s) {
@ -96,29 +89,7 @@ void VoxelmanLibrary::set_is_textured(bool value) {
_is_textured = value;
}
Ref<VoxelSurface> VoxelmanLibrary::create_voxel(int id, String name) {
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<VoxelSurface>());
Ref<VoxelSurface> voxel(memnew(VoxelSurface));
voxel->set_library(Ref<VoxelmanLibrary>(this));
voxel->set_id(id);
voxel->set_voxel_name(name);
_voxel_types[id] = voxel;
return voxel;
}
void VoxelmanLibrary::_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_types_count || frame < ITEMS_PER_PAGE * _voxel_types_page || frame > ITEMS_PER_PAGE * (_voxel_types_page + 1)) {
property.usage = 0;
}
}
}
void VoxelmanLibrary::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_voxel", "id", "name"), &VoxelmanLibrary::create_voxel);
ClassDB::bind_method(D_METHOD("get_surface", "id"), &VoxelmanLibrary::get_surface);
ClassDB::bind_method(D_METHOD("get_atlas_columns"), &VoxelmanLibrary::get_atlas_columns);
@ -142,19 +113,12 @@ void VoxelmanLibrary::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop_material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_prop_material", "get_prop_material");
ClassDB::bind_method(D_METHOD("get_voxel_types_count"), &VoxelmanLibrary::get_voxel_types_count);
ClassDB::bind_method(D_METHOD("set_voxel_types_count", "value"), &VoxelmanLibrary::set_voxel_types_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "voxel_types_count", PROPERTY_HINT_RANGE, "0," + itos(MAX_VOXEL_TYPES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_voxel_types_count", "get_voxel_types_count");
ClassDB::bind_method(D_METHOD("get_voxel_types_page"), &VoxelmanLibrary::get_voxel_types_page);
ClassDB::bind_method(D_METHOD("set_voxel_types_page", "value"), &VoxelmanLibrary::set_voxel_types_page);
ADD_PROPERTY(PropertyInfo(Variant::INT, "voxel_types_page", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_voxel_types_page", "get_voxel_types_page");
ClassDB::bind_method(D_METHOD("get_voxel_surface", "index"), &VoxelmanLibrary::get_voxel_surface);
ClassDB::bind_method(D_METHOD("set_voxel_surface", "index", "surface"), &VoxelmanLibrary::set_voxel_surface);
for (int i = 0; i < MAX_VOXEL_TYPES; ++i) {
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "Voxel_" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "VoxelSurface", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_voxel_surface", "get_voxel_surface", i);
}
ClassDB::bind_method(D_METHOD("get_voxel_surfaces"), &VoxelmanLibrary::get_voxel_surfaces);
ClassDB::bind_method(D_METHOD("set_voxel_surfaces"), &VoxelmanLibrary::set_voxel_surfaces);
BIND_CONSTANT(MAX_VOXEL_TYPES);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "voxel_surfaces", (PropertyHint)(PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE), "17/17:VoxelSurface", PROPERTY_USAGE_DEFAULT, "VoxelSurface"), "set_voxel_surfaces", "get_voxel_surfaces");
}

View File

@ -32,35 +32,24 @@ public:
Ref<Material> get_prop_material() const { return _prop_material; }
void set_prop_material(Ref<Material> mat) { _prop_material = mat; }
Ref<VoxelSurface> create_voxel(int id, String name);
int get_voxel_count() const;
Ref<VoxelSurface> get_voxel_surface(int index) const;
void set_voxel_surface(int index, Ref<VoxelSurface> value);
Vector<Variant> get_voxel_surfaces();
void set_voxel_surfaces(const Vector<Variant> &effects);
int get_voxel_types_count();
void set_voxel_types_count(int value);
int get_voxel_types_page();
void set_voxel_types_page(int value);
_FORCE_INLINE_ bool has_voxel(int id) const { return _voxel_types[id].is_valid(); }
_FORCE_INLINE_ Ref<VoxelSurface> get_surface(int id) const { return _voxel_types[id]; }
_FORCE_INLINE_ bool has_voxel(int id) const { return _voxel_surfaces[id].is_valid(); }
_FORCE_INLINE_ Ref<VoxelSurface> get_surface(int id) const { return _voxel_surfaces[id]; }
protected:
static void _bind_methods();
void _validate_property(PropertyInfo &property) const;
private:
enum {
MAX_VOXEL_TYPES = 256,
ITEMS_PER_PAGE = 10,
};
int _voxel_types_count;
int _voxel_types_page;
Ref<VoxelSurface> _voxel_types[MAX_VOXEL_TYPES];
Vector<Ref<VoxelSurface> > _voxel_surfaces;
Ref<Material> _material;
Ref<Material> _prop_material;