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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::sprites::{SheetRegion, Transform};
/// A repeat mode for 9-slice centers and edges. [`Repeat::Stretch`]
/// will scale one sprite to fill the space, while [`Repeat::Tile`]
/// will only fill exact multiples of the slice's size (i.e., it may not
/// fill the entire space if the space is not a multiple of the size).
#[derive(Clone, Copy, Debug)]
pub enum Repeat {
Stretch,
Tile,
}
/// Describes a slice that can be stretched or tiled. In stretching mode, w and h are minimum sizes.
#[derive(Clone, Copy, Debug)]
pub struct Slice {
pub w: f32,
pub h: f32,
pub region: SheetRegion,
pub repeat: Repeat,
}
/// Describes a slice that decorates a box, always drawn at the given native size.
#[derive(Clone, Copy, Debug)]
pub struct CornerSlice {
pub w: f32,
pub h: f32,
pub region: SheetRegion,
}
/// A nine-slice sized box drawing helper built for use with SpriteRenderer. It is the caller's responsibility to set the depth on the slices' sheet regions to achieve the desired rendering effects.
#[derive(Clone, Debug)]
pub struct NineSlice {
top_left: CornerSlice,
top_right: CornerSlice,
bottom_left: CornerSlice,
bottom_right: CornerSlice,
top: Slice,
left: Slice,
right: Slice,
bottom: Slice,
center: Slice,
}
impl NineSlice {
pub fn with_corner_edge_center(
corner_top_left: CornerSlice,
edge_left: Slice,
edge_top: Slice,
center: Slice,
) -> Self {
Self::new(
corner_top_left,
CornerSlice {
w: corner_top_left.h,
h: corner_top_left.w,
region: corner_top_left.region.flip_horizontal(),
},
CornerSlice {
region: corner_top_left.region.flip_vertical(),
w: corner_top_left.h,
h: corner_top_left.w,
},
CornerSlice {
region: corner_top_left.region.flip_vertical().flip_horizontal(),
..corner_top_left
},
edge_top,
Slice {
region: edge_top.region.flip_vertical(),
..edge_top
},
edge_left,
Slice {
region: edge_left.region.flip_horizontal(),
..edge_left
},
center,
)
}
#[allow(clippy::too_many_arguments)]
pub fn new(
top_left: CornerSlice,
top_right: CornerSlice,
bottom_left: CornerSlice,
bottom_right: CornerSlice,
top: Slice,
bottom: Slice,
left: Slice,
right: Slice,
center: Slice,
) -> Self {
assert_eq!(top_left.w, bottom_left.w);
assert_eq!(top_right.w, bottom_right.w);
assert_eq!(top_left.h, top_right.h);
assert_eq!(bottom_left.h, bottom_right.h);
Self {
top_left,
top_right,
bottom_left,
bottom_right,
top,
left,
right,
bottom,
center,
}
}
/// Returns how many sprites will be needed to render this nineslice box at the given width and height.
/// This may be an overestimate if the box is very small, but nineslice will zero out any sprites it doesn't use.
pub fn sprite_count(&self, w: f32, h: f32) -> usize {
let mut count: usize = 4; // 4 corners
for edge in &[self.left, self.right] {
count += match edge.repeat {
Repeat::Stretch => 1,
Repeat::Tile => (h / edge.h) as usize,
};
}
for edge in &[self.top, self.bottom] {
count += match edge.repeat {
Repeat::Stretch => 1,
Repeat::Tile => (w / edge.w) as usize,
};
}
count += match self.center.repeat {
Repeat::Stretch => 1,
Repeat::Tile => ((w / self.center.w) as usize) * ((h / self.center.h) as usize),
};
count
}
/// Draws a nineslice box from (x,y) (bottom left corner) to (x+w, y+h) (top right corner)
#[allow(clippy::too_many_arguments)]
pub fn draw(
&self,
trf: &mut [Transform],
uvs: &mut [SheetRegion],
x: f32,
y: f32,
w: f32,
h: f32,
z_offset: u16,
) -> usize {
let mut which = 0;
let limit = self.sprite_count(w, h);
// draw center
{
let x0 = x + self.top_left.w;
let x1 = x + w - self.top_right.w;
let w = x1 - x0;
let y0 = y + self.bottom_left.h;
let y1 = y + h - self.top_left.h;
let h = y1 - y0;
match self.center.repeat {
Repeat::Stretch => {
trf[which] = Transform {
w: w as u16,
h: h as u16,
x: x0 + w / 2.0,
y: y0 + h / 2.0,
rot: 0.0,
};
uvs[which] = self.center.region;
uvs[which].depth += z_offset;
which += 1;
}
Repeat::Tile => {
for row in 0..((h / self.center.h) as usize) {
for (col, (trf, uv)) in (0..(w / self.center.w) as usize)
.zip(trf[which..].iter_mut().zip(uvs[which..].iter_mut()))
{
*trf = Transform {
w: self.center.w as u16,
h: self.center.h as u16,
rot: 0.0,
x: x0 + (col as f32 * self.center.w) + (self.center.w / 2.0),
y: y0 + (row as f32 * self.center.h) + (self.center.h / 2.0),
};
*uv = self.center.region;
uv.depth += z_offset;
which += 1;
}
}
}
}
}
// draw edges
for (edge, xpos) in &[
(self.left, x + self.left.w / 2.0),
(self.right, x + w - self.right.w / 2.0),
] {
let y = y + self.bottom_left.h;
match edge.repeat {
Repeat::Stretch => {
let w = edge.w;
let h = (h - self.bottom_left.h - self.top_left.h).max(edge.h);
trf[which] = Transform {
w: w as u16,
h: h as u16,
x: *xpos,
y: y + h / 2.0,
rot: 0.0,
};
uvs[which] = edge.region;
uvs[which].depth += z_offset;
which += 1;
}
Repeat::Tile => {
let h = h - self.bottom_left.h - self.top_left.h;
for (row, (trf, uv)) in (0..((h / edge.h) as usize))
.zip(trf[which..].iter_mut().zip(uvs[which..].iter_mut()))
{
*trf = Transform {
w: edge.w as u16,
h: edge.h as u16,
rot: 0.0,
x: *xpos,
y: y + (row as f32 * edge.h) + (edge.h / 2.0),
};
*uv = edge.region;
uv.depth += z_offset;
which += 1;
}
}
};
}
for (edge, ypos) in &[
(self.bottom, y + self.bottom.h / 2.0),
(self.top, y + h - self.bottom.h / 2.0),
] {
let x = x + self.top_left.w;
match edge.repeat {
Repeat::Stretch => {
let w = (w - self.top_left.w - self.top_right.w).max(edge.w);
let h = edge.h;
trf[which] = Transform {
w: w as u16,
h: h as u16,
y: *ypos,
x: x + w / 2.0,
rot: 0.0,
};
uvs[which] = edge.region;
uvs[which].depth += z_offset;
which += 1;
}
Repeat::Tile => {
let w = w - self.top_left.w - self.top_right.w;
for (col, (trf, uv)) in (0..((w / edge.w) as usize))
.zip(trf[which..].iter_mut().zip(uvs[which..].iter_mut()))
{
*trf = Transform {
w: edge.w as u16,
h: edge.h as u16,
rot: 0.0,
y: *ypos,
x: x + (col as f32 * edge.w) + (edge.w / 2.0),
};
*uv = edge.region;
uv.depth += z_offset;
which += 1;
}
}
};
}
// draw corners
for ((corner, x, y), (trf, uv)) in [
(
self.top_left,
x + self.top_left.w / 2.0,
y + h - self.top_left.h / 2.0,
),
(
self.top_right,
x + w - self.top_right.w / 2.0,
y + h - self.top_right.h / 2.0,
),
(
self.bottom_left,
x + self.bottom_left.w / 2.0,
y + self.bottom_left.h / 2.0,
),
(
self.bottom_right,
x + w - self.bottom_right.w / 2.0,
y + self.bottom_right.h / 2.0,
),
]
.iter()
.zip(trf[which..].iter_mut().zip(uvs[which..].iter_mut()))
{
*trf = Transform {
x: *x,
y: *y,
w: corner.w as u16,
h: corner.h as u16,
rot: 0.0,
};
*uv = corner.region;
uv.depth += z_offset;
}
which += 4;
trf[which..limit].fill(Transform::ZERO);
uvs[which..limit].fill(SheetRegion::ZERO);
which
}
}