File size: 5,931 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::AlphaBlending;
use crate::uuid::NodeId;
use dyn_any::StaticType;
use glam::DAffine2;
use std::hash::Hash;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Instances<T> {
	#[serde(alias = "instances")]
	instance: Vec<T>,
	#[serde(default = "one_daffine2_default")]
	transform: Vec<DAffine2>,
	#[serde(default = "one_alpha_blending_default")]
	alpha_blending: Vec<AlphaBlending>,
	#[serde(default = "one_source_node_id_default")]
	source_node_id: Vec<Option<NodeId>>,
}

impl<T> Instances<T> {
	pub fn new(instance: T) -> Self {
		Self {
			instance: vec![instance],
			transform: vec![DAffine2::IDENTITY],
			alpha_blending: vec![AlphaBlending::default()],
			source_node_id: vec![None],
		}
	}

	pub fn push(&mut self, instance: Instance<T>) {
		self.instance.push(instance.instance);
		self.transform.push(instance.transform);
		self.alpha_blending.push(instance.alpha_blending);
		self.source_node_id.push(instance.source_node_id);
	}

	pub fn extend(&mut self, instances: Instances<T>) {
		self.instance.extend(instances.instance);
		self.transform.extend(instances.transform);
		self.alpha_blending.extend(instances.alpha_blending);
		self.source_node_id.extend(instances.source_node_id);
	}

	pub fn instance_iter(self) -> impl DoubleEndedIterator<Item = Instance<T>> {
		self.instance
			.into_iter()
			.zip(self.transform)
			.zip(self.alpha_blending)
			.zip(self.source_node_id)
			.map(|(((instance, transform), alpha_blending), source_node_id)| Instance {
				instance,
				transform,
				alpha_blending,
				source_node_id,
			})
	}

	pub fn instance_ref_iter(&self) -> impl DoubleEndedIterator<Item = InstanceRef<'_, T>> + Clone {
		self.instance
			.iter()
			.zip(self.transform.iter())
			.zip(self.alpha_blending.iter())
			.zip(self.source_node_id.iter())
			.map(|(((instance, transform), alpha_blending), source_node_id)| InstanceRef {
				instance,
				transform,
				alpha_blending,
				source_node_id,
			})
	}

	pub fn instance_mut_iter(&mut self) -> impl DoubleEndedIterator<Item = InstanceMut<'_, T>> {
		self.instance
			.iter_mut()
			.zip(self.transform.iter_mut())
			.zip(self.alpha_blending.iter_mut())
			.zip(self.source_node_id.iter_mut())
			.map(|(((instance, transform), alpha_blending), source_node_id)| InstanceMut {
				instance,
				transform,
				alpha_blending,
				source_node_id,
			})
	}

	pub fn get(&self, index: usize) -> Option<InstanceRef<'_, T>> {
		if index >= self.instance.len() {
			return None;
		}

		Some(InstanceRef {
			instance: &self.instance[index],
			transform: &self.transform[index],
			alpha_blending: &self.alpha_blending[index],
			source_node_id: &self.source_node_id[index],
		})
	}

	pub fn get_mut(&mut self, index: usize) -> Option<InstanceMut<'_, T>> {
		if index >= self.instance.len() {
			return None;
		}

		Some(InstanceMut {
			instance: &mut self.instance[index],
			transform: &mut self.transform[index],
			alpha_blending: &mut self.alpha_blending[index],
			source_node_id: &mut self.source_node_id[index],
		})
	}

	pub fn len(&self) -> usize {
		self.instance.len()
	}

	pub fn is_empty(&self) -> bool {
		self.instance.is_empty()
	}
}

impl<T> Default for Instances<T> {
	fn default() -> Self {
		Self {
			instance: Vec::new(),
			transform: Vec::new(),
			alpha_blending: Vec::new(),
			source_node_id: Vec::new(),
		}
	}
}

impl<T: Hash> Hash for Instances<T> {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		for instance in &self.instance {
			instance.hash(state);
		}
	}
}

impl<T: PartialEq> PartialEq for Instances<T> {
	fn eq(&self, other: &Self) -> bool {
		self.instance.len() == other.instance.len() && { self.instance.iter().zip(other.instance.iter()).all(|(a, b)| a == b) }
	}
}

unsafe impl<T: StaticType + 'static> StaticType for Instances<T> {
	type Static = Instances<T>;
}

fn one_daffine2_default() -> Vec<DAffine2> {
	vec![DAffine2::IDENTITY]
}
fn one_alpha_blending_default() -> Vec<AlphaBlending> {
	vec![AlphaBlending::default()]
}
fn one_source_node_id_default() -> Vec<Option<NodeId>> {
	vec![None]
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct InstanceRef<'a, T> {
	pub instance: &'a T,
	pub transform: &'a DAffine2,
	pub alpha_blending: &'a AlphaBlending,
	pub source_node_id: &'a Option<NodeId>,
}

impl<T> InstanceRef<'_, T> {
	pub fn to_instance_cloned(self) -> Instance<T>
	where
		T: Clone,
	{
		Instance {
			instance: self.instance.clone(),
			transform: *self.transform,
			alpha_blending: *self.alpha_blending,
			source_node_id: *self.source_node_id,
		}
	}
}

#[derive(Debug)]
pub struct InstanceMut<'a, T> {
	pub instance: &'a mut T,
	pub transform: &'a mut DAffine2,
	pub alpha_blending: &'a mut AlphaBlending,
	pub source_node_id: &'a mut Option<NodeId>,
}

#[derive(Copy, Clone, Default, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Instance<T> {
	pub instance: T,
	pub transform: DAffine2,
	pub alpha_blending: AlphaBlending,
	pub source_node_id: Option<NodeId>,
}

impl<T> Instance<T> {
	pub fn to_graphic_element<U>(self) -> Instance<U>
	where
		T: Into<U>,
	{
		Instance {
			instance: self.instance.into(),
			transform: self.transform,
			alpha_blending: self.alpha_blending,
			source_node_id: self.source_node_id,
		}
	}

	pub fn to_instance_ref(&self) -> InstanceRef<'_, T> {
		InstanceRef {
			instance: &self.instance,
			transform: &self.transform,
			alpha_blending: &self.alpha_blending,
			source_node_id: &self.source_node_id,
		}
	}

	pub fn to_instance_mut(&mut self) -> InstanceMut<'_, T> {
		InstanceMut {
			instance: &mut self.instance,
			transform: &mut self.transform,
			alpha_blending: &mut self.alpha_blending,
			source_node_id: &mut self.source_node_id,
		}
	}

	pub fn to_table(self) -> Instances<T> {
		Instances {
			instance: vec![self.instance],
			transform: vec![self.transform],
			alpha_blending: vec![self.alpha_blending],
			source_node_id: vec![self.source_node_id],
		}
	}
}