|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | use godot::builtin::real_consts::FRAC_PI_3;
|
9 |
| -use godot::builtin::Vector2; |
| 9 | +use godot::builtin::{Array, PackedArray, Variant, Vector2}; |
10 | 10 | use godot::classes::notify::ObjectNotification;
|
11 |
| -use godot::classes::{ClassDb, IRefCounted, RefCounted}; |
| 11 | +use godot::classes::{mesh, ArrayMesh, ClassDb, IArrayMesh, IRefCounted, RefCounted}; |
12 | 12 | use godot::meta::ToGodot;
|
13 |
| -use godot::obj::{Base, Gd, InstanceId, NewAlloc, NewGd, WithBaseField}; |
| 13 | +use godot::obj::{Base, Gd, IndexEnum, InstanceId, NewAlloc, NewGd, WithBaseField}; |
14 | 14 | use godot::register::{godot_api, GodotClass};
|
15 | 15 | use godot::task::TaskHandle;
|
16 | 16 |
|
@@ -197,3 +197,69 @@ fn base_postinit_refcounted() -> TaskHandle {
|
197 | 197 | assert_eq!(obj.get_reference_count(), 2);
|
198 | 198 | next_frame(move || assert_eq!(obj.get_reference_count(), 1, "eventual dec-ref happens"))
|
199 | 199 | }
|
| 200 | + |
| 201 | +fn make_mesh_arrays() -> Array<Variant> { |
| 202 | + let mut arrays = Array::new(); |
| 203 | + arrays.resize(mesh::ArrayType::ENUMERATOR_COUNT, &Variant::nil()); |
| 204 | + let indices = PackedArray::<i32>::from([0, 1, 2]); |
| 205 | + let vertex = PackedArray::<Vector2>::from([ |
| 206 | + Vector2::new(0.0, 0.0), |
| 207 | + Vector2::new(1.0, 0.0), |
| 208 | + Vector2::new(0.0, 1.0), |
| 209 | + ]); |
| 210 | + arrays.set(mesh::ArrayType::INDEX.to_index(), &indices.to_variant()); |
| 211 | + arrays.set(mesh::ArrayType::VERTEX.to_index(), &vertex.to_variant()); |
| 212 | + arrays |
| 213 | +} |
| 214 | + |
| 215 | +#[derive(GodotClass)] |
| 216 | +#[class(base=ArrayMesh)] |
| 217 | +struct InitArrayMeshTest { |
| 218 | + base: Base<ArrayMesh>, |
| 219 | +} |
| 220 | + |
| 221 | +#[rustfmt::skip] |
| 222 | +#[godot_api] |
| 223 | +impl IArrayMesh for InitArrayMeshTest { |
| 224 | + fn init(base: Base<ArrayMesh>) -> Self { |
| 225 | + let mut sf = Self { base }; |
| 226 | + sf.base_mut() |
| 227 | + .add_surface_from_arrays(mesh::PrimitiveType::TRIANGLES, &make_mesh_arrays()); |
| 228 | + sf |
| 229 | + } |
| 230 | + |
| 231 | + fn on_notification(&mut self, what: ObjectNotification) { |
| 232 | + if what == ObjectNotification::PREDELETE { |
| 233 | + let arr = make_mesh_arrays(); |
| 234 | + self.base_mut().add_surface_from_arrays(mesh::PrimitiveType::TRIANGLES, &arr); |
| 235 | + |
| 236 | + assert_eq!(self.base().get_surface_count(), 2); |
| 237 | + assert_eq!(self.base().surface_get_arrays(0), arr); |
| 238 | + assert_eq!(self.base().surface_get_arrays(1), arr); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + fn get_surface_count(&self) -> i32 { unreachable!(); } |
| 243 | + fn surface_get_array_len(&self, _index: i32) -> i32 { unreachable!(); } |
| 244 | + fn surface_get_array_index_len(&self, _index: i32) -> i32 { unreachable!(); } |
| 245 | + fn surface_get_arrays(&self, _index: i32) -> Array<Variant> { unreachable!(); } |
| 246 | + fn surface_get_blend_shape_arrays(&self, _index: i32) -> Array<Array<Variant>> { unreachable!(); } |
| 247 | + fn surface_get_lods(&self, _index: i32) -> godot::builtin::Dictionary { unreachable!(); } |
| 248 | + fn surface_get_format(&self, _index: i32) -> u32 { unreachable!(); } |
| 249 | + fn surface_get_primitive_type(&self, _index: i32) -> u32 { unreachable!(); } |
| 250 | + #[cfg(feature = "codegen-full")] |
| 251 | + fn surface_set_material(&mut self, _index: i32, _material: Option<Gd<godot::classes::Material>>) { unreachable!(); } |
| 252 | + #[cfg(feature = "codegen-full")] |
| 253 | + fn surface_get_material(&self, _index: i32) -> Option<Gd<godot::classes::Material>> { unreachable!(); } |
| 254 | + fn get_blend_shape_count(&self) -> i32 { unreachable!(); } |
| 255 | + fn get_blend_shape_name(&self, _index: i32) -> godot::builtin::StringName { unreachable!(); } |
| 256 | + fn set_blend_shape_name(&mut self, _index: i32, _name: godot::builtin::StringName){ unreachable!(); } |
| 257 | + fn get_aabb(&self) -> godot::builtin::Aabb { unreachable!(); } |
| 258 | +} |
| 259 | + |
| 260 | +#[itest] |
| 261 | +fn base_mut_init_array_mesh() { |
| 262 | + let mesh = InitArrayMeshTest::new_gd(); |
| 263 | + assert_eq!(mesh.get_surface_count(), 1); |
| 264 | + assert_eq!(mesh.surface_get_arrays(0), make_mesh_arrays()); |
| 265 | +} |
0 commit comments