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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// sprite.rs -- Aldaron's Device Interface / Screen
// Copyright (c) 2017-2018  Jeron A. Lau <jeron.lau@plopgrizzly.com>
// Licensed under the MIT LICENSE

use Window;
use { Texture, Model, Gradient, TexCoords };
use adi_gpu::{ Shape };
use adi_gpu;
use adi_gpu::DisplayTrait;
use ami::Mat4;

#[must_use]
/// Sprite represents anything that is rendered onto the screen.
pub struct Sprite(Shape);

#[must_use]
/// Builder for a `Sprite`.
pub struct SpriteBuilder(adi_gpu::Model, bool, bool);

/// Builder for multiple `Sprite`s.
#[must_use]
pub struct SpriteList(Vec<Sprite>, adi_gpu::Model, Mat4, bool, bool, bool);

impl SpriteList {
	/// Create a new list of `Sprite`s.
	#[inline(always)]
	pub fn new(model: Model) -> SpriteList {
		SpriteList(vec![], model.0, Mat4::new(), false, true, true)
	}

	/// Set the transform.
	#[inline(always)]
	pub fn transform(self, transform: Transform) -> SpriteList {
		SpriteList(self.0, self.1, transform.0, self.3, self.4, self.5)
	}

	/// Set the model.
	#[inline(always)]
	pub fn model(self, model: Model) -> SpriteList {
		SpriteList(self.0, model.0, self.2, self.3, self.4, self.5)
	}

	/// Enable alpha blending for following sprites.
	#[inline(always)]
	pub fn alpha(self) -> Self {
		SpriteList(self.0, self.1, self.2, true, self.4, self.5)
	}

	/// Disable all alpha blending for following sprites.
	#[inline(always)]
	pub fn opaque(self) -> Self {
		SpriteList(self.0, self.1, self.2, false, self.4, self.5)
	}

	/// Enable fog and camera.
	#[inline(always)]
	pub fn fog_cam(self) -> Self {
		SpriteList(self.0, self.1, self.2, self.3, true, true)
	}

	/// Disable fog and camera.
	#[inline(always)]
	pub fn gui(self) -> Self {
		SpriteList(self.0, self.1, self.2, self.3, false, false)
	}

	/// Camera without fog.
	#[inline(always)]
	pub fn camera(self) -> Self {
		SpriteList(self.0, self.1, self.2, self.3, false, true)
	}

	/// Create a sprite with a solid color.
	#[inline(always)]
	pub fn solid(mut self, window: &mut Window, color: [f32; 4]) -> Self {
		self.0.push(Sprite(window.window.shape_solid(&self.1,
			self.2, color, self.3, self.4,
			self.5)));
		self
	}

	/// Create a sprite shaded by a gradient (1 color per vertex).
	#[inline(always)]
	pub fn gradient(mut self, window: &mut Window, colors: Gradient)
		-> Self
	{
		self.0.push(Sprite(window.window.shape_gradient(&self.1,
			self.2, colors.0, self.3, self.4,
			self.5)));
		self
	}

	/// Create a sprite with a texture and texture coordinates.
	#[inline(always)]
	pub fn texture(mut self, window: &mut Window, texture: Texture,
		tc: TexCoords) -> Self
	{
		self.0.push(Sprite(window.window.shape_texture(&self.1,
			self.2, texture.0, tc.0, self.3,
			self.4, self.5)));
		self
	}

	/// Create a sprite with a texture, texture coordinates and alpha.
	/// Automatically Enables Alpha Blending. (no need to call `alpha()`)
	#[inline(always)]
	pub fn faded(mut self, window: &mut Window, texture: Texture,
		tc: TexCoords, alpha: f32) -> Self
	{
		self.0.push(Sprite(window.window.shape_faded(&self.1,
			self.2, texture.0, tc.0, alpha,
			self.4, self.5)));
		self
	}

	/// Create a sprite with a texture and texture coordinates and tint.
	#[inline(always)]
	pub fn tinted(mut self, window: &mut Window, texture: Texture,
		tc: TexCoords, tint: [f32; 4]) -> Self
	{
		self.0.push(Sprite(window.window.shape_tinted(&self.1,
			self.2, texture.0, tc.0, tint,
			self.3, self.4, self.5)));
		self
	}

	/// Create a sprite with a texture and texture coordinates and tint per
	/// vertex.
	#[inline(always)]
	pub fn complex(mut self, window: &mut Window, texture: Texture,
		tc: TexCoords, tint_pv: Gradient) -> Self
	{
		self.0.push(Sprite(window.window.shape_complex(&self.1, self.2,
			texture.0, tc.0, tint_pv.0, self.3, self.4, self.5)));

		self
	}

	/// Convert into a `Vec` of `Sprite`s
	#[inline(always)]
	pub fn to_vec(self) -> Vec<Sprite> {
		self.0
	}

	/// Convert into 1 `Sprite` if there's only 1 in the list.
	#[inline(always)]
	pub fn only(mut self) -> Sprite {
		self.0.pop().unwrap()
	}
}

/// Transform represents a transformation matrix.
pub struct Transform(Mat4);

impl SpriteBuilder {
	/// Create a new `SpriteBuilder`.
	#[inline(always)]
	pub fn new(vertices: Model) -> Self {
		SpriteBuilder(vertices.0, false, false)
	}

	/// Enable alpha blending for this sprite.
	#[inline(always)]
	pub fn alpha(self) -> Self {
		SpriteBuilder(self.0, true, false)
	}

	/// Enable per-fragment alpha blending for this sprite.
	#[inline(always)]
	pub fn blend(self) -> Self {
		SpriteBuilder(self.0, true, true)
	}

/*	/// Create a sprite with a solid color.
	#[inline(always)]
	pub fn solid(&self, window: &mut Window, color: [f32; 4]) -> Sprite {
		Sprite(self.0.push_solid(&mut window.window, color, self.1,
			self.2))
	}

	/// Create a sprite shaded by a gradient (1 color per vertex).
	#[inline(always)]
	pub fn gradient(&self, window: &mut Window, colors: Gradient) -> Sprite {
		Sprite(self.0.push_gradient(&mut window.window, colors.0,
			self.1, self.2))
	}

	/// Create a sprite with a texture and texture coordinates.
	#[inline(always)]
	pub fn texture(&self, window: &mut Window, texture: Texture, tc: TexCoords)
		-> Sprite
	{
		Sprite(self.0.push_texture(&mut window.window, texture.0, tc.0,
			self.1, self.2))
	}

	/// Create a sprite with a texture, texture coordinates and alpha.
	/// Automatically Enables Alpha Blending. (no need to call `alpha()`)
	#[inline(always)]
	pub fn faded(&self, window: &mut Window, texture: Texture, tc: TexCoords,
		alpha: f32) -> Sprite
	{
		Sprite(self.0.push_faded(&mut window.window, texture.0, tc.0,
			alpha, self.2))
	}

	/// Create a sprite with a texture and texture coordinates and tint.
	#[inline(always)]
	pub fn tinted(&self, window: &mut Window, texture: Texture,
		tc: TexCoords, tint: [f32; 4]) -> Sprite
	{
		Sprite(self.0.push_tinted(&mut window.window, texture.0, tc.0,
			tint, self.1, self.2))
	}

	/// Create a sprite with a texture and texture coordinates and tint per
	/// vertex.
	#[inline(always)]
	pub fn complex(&self, window: &mut Window, texture: Texture,
		tc: TexCoords, tint_pv: Gradient) -> Sprite
	{
		Sprite(self.0.push_complex(&mut window.window, texture.0, tc.0,
			tint_pv.0, self.1, self.2))
	}*/
}

/*	/// Change the style of self to style for instance i.
	pub fn style(&self, window: &mut Window, i: usize, style: &Style) -> (){
		match *style {
			Style::Invisible => {
				Shape::enable(window, self.0, i, false);
			}
			Style::Texture(s, ref tx) => {
				let shader = window.shader(s);
				Shape::animate(window, self.0, i, tx, shader);
			}
			Style::Solid(s) => {
				let shader = window.shader(s);
				Shape::animate(window, self.0, i, null_mut(),
					shader);
			}
		}
	}*/

/*	/// Change the vertices of self to v.
	pub fn vertices(&mut self, window: &mut Window, v: &[f32]) -> () {
		self.0.vertices(window, self.0, v);
	}*/
//}

impl Transform {
	/// Create a transform that does nothing. ( Underneath, this is an
	/// identity matrix ).
	pub fn new() -> Transform {
		Transform (Mat4::new())
	}

	/// Translate self by x, y and z.
	pub fn translate(self, x:f32, y:f32, z:f32) -> Transform {
		Transform(self.0.translate(x, y, z))
	}

	/// Scale self by x, y and z.
	pub fn scale(self, x:f32, y:f32, z:f32) -> Transform {
		Transform(self.0.scale(x, y, z))
	}

	/// Rotate self by yaw, pitch and roll.
	pub fn rotate(self, yaw:f32, pitch:f32, roll:f32) -> Transform {
		Transform(self.0.rotate(yaw, pitch, roll))
	}

	/// Multiply by a projection that scales width and height by the
	/// smallest widget size. The widget is put at position pos. Position
	/// isn't affected by aspect ratio.
	pub fn auto(self, window: &mut Window, pos: (f32, f32)) -> Transform {
		let size = window.unit_size();
		let t = self.scale(size.0, size.1, 1.0)
			.translate(pos.0, pos.1, 0.0);
		t
	}

	/// Apply a TransformApply onto instance i of Sprite.
	pub fn apply(self, window: &mut Window, sprite: &mut Sprite)
		-> Transform
	{
		window.window.transform(&mut sprite.0, self.0);

		self
	}
}