1#[cfg(feature = "serialize")]
5use serde::{
6 Deserialize,
7 Serialize,
8};
9
10use crate::types::{
11 Model,
12 Palette,
13 Size,
14 Version,
15 Voxel,
16};
17
18pub type VoxData = VoxModels<Model>;
20
21impl VoxModelBuffer for Model {
22 fn new(size: Size) -> Self {
23 Model {
24 size,
25 voxels: vec![],
26 }
27 }
28
29 fn set_voxel(&mut self, voxel: Voxel, _palette: &Palette) {
30 self.voxels.push(voxel);
31 }
32}
33
34pub trait VoxBuffer {
49 fn set_version(&mut self, _version: Version) {}
54
55 fn set_num_models(&mut self, _num_models: usize) {}
57
58 fn set_model_size(&mut self, _model_size: Size) {}
61
62 fn set_voxel(&mut self, voxel: Voxel);
64
65 fn set_palette(&mut self, palette: Palette);
68}
69
70pub trait VoxModelBuffer {
72 fn new(size: Size) -> Self;
73 fn set_voxel(&mut self, voxel: Voxel, palette: &Palette);
74}
75
76#[derive(Debug)]
79#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
80pub struct VoxModels<V> {
81 pub version: Version,
82 pub models: Vec<V>,
83 pub palette: Palette,
84}
85
86impl<V> Default for VoxModels<V> {
87 fn default() -> Self {
88 Self {
89 version: Version::default(),
90 models: vec![],
91 palette: Palette::default(),
92 }
93 }
94}
95
96impl<V: VoxModelBuffer> VoxBuffer for VoxModels<V> {
97 fn set_version(&mut self, version: Version) {
98 self.version = version;
99 }
100
101 fn set_num_models(&mut self, num_models: usize) {
102 self.models.reserve_exact(num_models);
103 }
104
105 fn set_model_size(&mut self, model_size: Size) {
106 self.models.push(V::new(model_size));
107 }
108
109 fn set_voxel(&mut self, voxel: Voxel) {
110 let model = self.models.last_mut().expect("model");
111 model.set_voxel(voxel, &self.palette);
112 }
113
114 fn set_palette(&mut self, palette: Palette) {
115 self.palette = palette;
116 }
117}