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
use graphics::{self, ActiveShader, DrawParams, Drawable, Rectangle, Texture};
use Context;
pub struct NineSlice {
texture: Texture,
width: f32,
height: f32,
fill_rect: Rectangle,
}
impl NineSlice {
pub fn new(texture: Texture, width: f32, height: f32, fill_rect: Rectangle) -> NineSlice {
NineSlice {
texture,
width,
height,
fill_rect,
}
}
}
impl Drawable for NineSlice {
fn draw<T: Into<DrawParams>>(&self, ctx: &mut Context, params: T) {
let params = params.into();
let texture_width = self.texture.handle.width() as f32;
let texture_height = self.texture.handle.height() as f32;
let x1 = 0.0;
let y1 = 0.0;
let x2 = self.fill_rect.x;
let y2 = self.fill_rect.y;
let x3 = self.width - self.fill_rect.x;
let y3 = self.height - self.fill_rect.y;
let x4 = self.width;
let y4 = self.height;
let u1 = 0.0;
let v1 = 0.0;
let u2 = self.fill_rect.x / texture_width;
let v2 = self.fill_rect.y / texture_height;
let u3 = (self.fill_rect.x + self.fill_rect.width) / texture_width;
let v3 = (self.fill_rect.y + self.fill_rect.height) / texture_height;
let u4 = 1.0;
let v4 = 1.0;
graphics::set_texture(ctx, &self.texture);
graphics::set_shader_ex(ctx, ActiveShader::Default);
graphics::push_quad(ctx, x1, y1, x2, x2, u1, v1, u2, v2, ¶ms);
graphics::push_quad(ctx, x2, y1, x3, y2, u2, v1, u3, v2, ¶ms);
graphics::push_quad(ctx, x3, y1, x4, y2, u3, v1, u4, v2, ¶ms);
graphics::push_quad(ctx, x1, y2, x2, y3, u1, v2, u2, v3, ¶ms);
graphics::push_quad(ctx, x2, y2, x3, y3, u2, v2, u3, v3, ¶ms);
graphics::push_quad(ctx, x3, y2, x4, y3, u3, v2, u4, v3, ¶ms);
graphics::push_quad(ctx, x1, y3, x2, y4, u1, v3, u2, v4, ¶ms);
graphics::push_quad(ctx, x2, y3, x3, y4, u2, v3, u3, v4, ¶ms);
graphics::push_quad(ctx, x3, y3, x4, y4, u3, v3, u4, v4, ¶ms);
}
}