From fd9122a0ded67396f84baa92870d828a7bae57f1 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 26 Jul 2020 18:37:34 +0100 Subject: [PATCH] Use StringName for voxel names --- meshers/blocky/voxel.cpp | 1 + meshers/blocky/voxel.h | 5 ++--- meshers/blocky/voxel_library.cpp | 19 +++++++++++++++++++ meshers/blocky/voxel_library.h | 3 +++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/meshers/blocky/voxel.cpp b/meshers/blocky/voxel.cpp index 6efa804e..7a1c6c7f 100644 --- a/meshers/blocky/voxel.cpp +++ b/meshers/blocky/voxel.cpp @@ -449,6 +449,7 @@ void Voxel::_bind_methods() { ClassDB::bind_method(D_METHOD("set_collision_aabbs", "aabbs"), &Voxel::_b_set_collision_aabbs); ClassDB::bind_method(D_METHOD("get_collision_aabbs"), &Voxel::_b_get_collision_aabbs); + // TODO Update to StringName in Godot 4 ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_voxel_name", "get_voxel_name"); ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent"), "set_transparent", "is_transparent"); diff --git a/meshers/blocky/voxel.h b/meshers/blocky/voxel.h index 4e1b897a..d9d15d6e 100644 --- a/meshers/blocky/voxel.h +++ b/meshers/blocky/voxel.h @@ -30,7 +30,7 @@ public: // Properties Ref set_voxel_name(String name); - _FORCE_INLINE_ String get_voxel_name() const { return _name; } + _FORCE_INLINE_ StringName get_voxel_name() const { return _name; } Ref set_id(int id); _FORCE_INLINE_ int get_id() const { return _id; } @@ -98,7 +98,6 @@ private: void clear_geometry(); Ref set_cube_geometry(float sy = 1); - //Ref set_xquad_geometry(Vector2 atlas_pos); Array _b_get_collision_aabbs() const; void _b_set_collision_aabbs(Array array); @@ -108,7 +107,7 @@ private: // Identifiers int _id; - String _name; // TODO StringName? + StringName _name; // Properties int _material_id; diff --git a/meshers/blocky/voxel_library.cpp b/meshers/blocky/voxel_library.cpp index 8e758f80..2e5e6d45 100644 --- a/meshers/blocky/voxel_library.cpp +++ b/meshers/blocky/voxel_library.cpp @@ -30,6 +30,16 @@ void VoxelLibrary::set_voxel_count(unsigned int type_count) { _change_notify(); } +int VoxelLibrary::get_voxel_index_from_name(StringName name) const { + for (size_t i = 0; i < _voxel_types.size(); ++i) { + const Ref &v = _voxel_types[i]; + if (v->get_name() == name) { + return i; + } + } + return -1; +} + void VoxelLibrary::load_default() { set_voxel_count(2); create_voxel(0, "air")->set_transparent(true); @@ -382,6 +392,9 @@ void VoxelLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("set_voxel_count", "count"), &VoxelLibrary::set_voxel_count); ClassDB::bind_method(D_METHOD("get_voxel_count"), &VoxelLibrary::get_voxel_count); + ClassDB::bind_method(D_METHOD("get_voxel_index_from_name", "name"), &VoxelLibrary::get_voxel_index_from_name); + ClassDB::bind_method(D_METHOD("get_voxel_by_name", "name"), &VoxelLibrary::_b_get_voxel_by_name); + ClassDB::bind_method(D_METHOD("bake"), &VoxelLibrary::bake); ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_size"), "set_atlas_size", "get_atlas_size"); @@ -394,3 +407,9 @@ Ref VoxelLibrary::_b_get_voxel(unsigned int id) { ERR_FAIL_COND_V(id >= _voxel_types.size(), Ref()); return _voxel_types[id]; } + +Ref VoxelLibrary::_b_get_voxel_by_name(StringName name) { + int id = get_voxel_index_from_name(name); + ERR_FAIL_COND_V(id == -1, Ref()); + return _voxel_types[id]; +} diff --git a/meshers/blocky/voxel_library.h b/meshers/blocky/voxel_library.h index 75d951c6..c72f656d 100644 --- a/meshers/blocky/voxel_library.h +++ b/meshers/blocky/voxel_library.h @@ -41,6 +41,8 @@ public: bool get_side_pattern_occlusion(unsigned int pattern_a, unsigned int pattern_b) const; + int get_voxel_index_from_name(StringName name) const; + private: void generate_side_culling_matrix(); @@ -51,6 +53,7 @@ private: void _get_property_list(List *p_list) const; Ref _b_get_voxel(unsigned int id); + Ref _b_get_voxel_by_name(StringName name); private: std::vector > _voxel_types;