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
//! Functions and types relating to user interfaces.

use crate::graphics::{self, ActiveShader, DrawParams, Drawable, Rectangle, Texture};
use crate::Context;

/// A panel made up of nine slices of an image. Useful for panels with borders.
///
/// Note that `NineSlice` does not currently support the `clip` `DrawParam`.
#[derive(Debug, Clone)]
pub struct NineSlice {
    texture: Texture,
    width: f32,
    height: f32,
    fill_rect: Rectangle,
}

impl NineSlice {
    /// Creates a new NineSlice from the given texture.
    ///
    /// The `fill_rect` is used to determine how to slice the texture - it should be set
    /// to the region of the texture that represents the center of the panel.
    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<P>(&self, ctx: &mut Context, params: P)
    where
        P: Into<DrawParams>,
    {
        let params = params.into();
        let transform = params.build_matrix();

        let texture_width = self.texture.width() as f32;
        let texture_height = self.texture.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);

        // Top left
        graphics::push_quad(
            ctx,
            x1,
            y1,
            x2,
            y2,
            u1,
            v1,
            u2,
            v2,
            &transform,
            params.color,
        );

        // Top
        graphics::push_quad(
            ctx,
            x2,
            y1,
            x3,
            y2,
            u2,
            v1,
            u3,
            v2,
            &transform,
            params.color,
        );

        // Top right
        graphics::push_quad(
            ctx,
            x3,
            y1,
            x4,
            y2,
            u3,
            v1,
            u4,
            v2,
            &transform,
            params.color,
        );

        // Left
        graphics::push_quad(
            ctx,
            x1,
            y2,
            x2,
            y3,
            u1,
            v2,
            u2,
            v3,
            &transform,
            params.color,
        );

        // Center
        graphics::push_quad(
            ctx,
            x2,
            y2,
            x3,
            y3,
            u2,
            v2,
            u3,
            v3,
            &transform,
            params.color,
        );

        // Right
        graphics::push_quad(
            ctx,
            x3,
            y2,
            x4,
            y3,
            u3,
            v2,
            u4,
            v3,
            &transform,
            params.color,
        );

        // Bottom left
        graphics::push_quad(
            ctx,
            x1,
            y3,
            x2,
            y4,
            u1,
            v3,
            u2,
            v4,
            &transform,
            params.color,
        );

        // Bottom
        graphics::push_quad(
            ctx,
            x2,
            y3,
            x3,
            y4,
            u2,
            v3,
            u3,
            v4,
            &transform,
            params.color,
        );

        // Bottom right
        graphics::push_quad(
            ctx,
            x3,
            y3,
            x4,
            y4,
            u3,
            v3,
            u4,
            v4,
            &transform,
            params.color,
        );
    }
}