fyrox_ui/
nine_patch.rs

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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use fyrox_core::{scope_profile, uuid_provider};

use crate::{
    brush::Brush,
    core::{
        algebra::Vector2, color::Color, math::Rect, pool::Handle, reflect::prelude::*,
        type_traits::prelude::*, visitor::prelude::*,
    },
    draw::{CommandTexture, Draw, DrawingContext},
    message::UiMessage,
    widget::{Widget, WidgetBuilder},
    BuildContext, Control, UiNode, UserInterface,
};
use fyrox_core::variable::InheritableVariable;
use fyrox_graph::BaseSceneGraph;
use fyrox_resource::untyped::UntypedResource;
use std::ops::{Deref, DerefMut};

/// Automatically arranges children by rows and columns
#[derive(Default, Clone, Visit, Reflect, Debug, ComponentProvider)]
pub struct NinePatch {
    pub widget: Widget,
    pub texture: InheritableVariable<Option<UntypedResource>>,
    pub bottom_margin_uv: InheritableVariable<f32>,
    pub left_margin_uv: InheritableVariable<f32>,
    pub right_margin_uv: InheritableVariable<f32>,
    pub top_margin_uv: InheritableVariable<f32>,

    pub bottom_margin_pixel: InheritableVariable<u32>,
    pub left_margin_pixel: InheritableVariable<u32>,
    pub right_margin_pixel: InheritableVariable<u32>,
    pub top_margin_pixel: InheritableVariable<u32>,
}

crate::define_widget_deref!(NinePatch);

uuid_provider!(NinePatch = "c345033e-8c10-4186-b101-43f73b85981d");

impl Control for NinePatch {
    fn measure_override(&self, ui: &UserInterface, available_size: Vector2<f32>) -> Vector2<f32> {
        scope_profile!();
        let mut size: Vector2<f32> = available_size;

        let column1_width_pixels = *self.left_margin_pixel as f32;
        let column3_width_pixels = *self.right_margin_pixel as f32;

        let row1_height_pixels = *self.top_margin_pixel as f32;
        let row3_height_pixels = *self.bottom_margin_pixel as f32;

        let x_overflow = column1_width_pixels + column3_width_pixels;
        let y_overflow = row1_height_pixels + row3_height_pixels;

        let center_size =
            Vector2::new(available_size.x - x_overflow, available_size.y - y_overflow);

        for &child in self.children.iter() {
            ui.measure_node(child, center_size);
            let desired_size = ui.node(child).desired_size();
            size.x = size.x.max(desired_size.x.ceil());
            size.y = size.y.max(desired_size.y.ceil());
        }
        size
    }

    fn arrange_override(&self, ui: &UserInterface, final_size: Vector2<f32>) -> Vector2<f32> {
        scope_profile!();

        let column1_width_pixels = *self.left_margin_pixel as f32;
        let column3_width_pixels = *self.right_margin_pixel as f32;

        let row1_height_pixels = *self.top_margin_pixel as f32;
        let row3_height_pixels = *self.bottom_margin_pixel as f32;

        let x_overflow = column1_width_pixels + column3_width_pixels;
        let y_overflow = row1_height_pixels + row3_height_pixels;

        let final_rect = Rect::new(
            column1_width_pixels,
            row1_height_pixels,
            final_size.x - x_overflow,
            final_size.y - y_overflow,
        );

        for &child in self.children.iter() {
            ui.arrange_node(child, &final_rect);
        }

        final_size
    }

    fn draw(&self, drawing_context: &mut DrawingContext) {
        let texture = self.texture.as_ref().unwrap();

        let patch_bounds = self.widget.bounding_rect();

        let column1_width_pixels = *self.left_margin_pixel as f32;
        let column3_width_pixels = *self.right_margin_pixel as f32;

        let row1_height_pixels = *self.top_margin_pixel as f32;
        let row3_height_pixels = *self.bottom_margin_pixel as f32;

        let x_fence_post1_uv = *self.left_margin_uv;
        let x_fence_post2_uv = 1.0 - *self.right_margin_uv;
        let y_fence_post1_uv = *self.top_margin_uv;
        let y_fence_post2_uv = 1.0 - *self.bottom_margin_uv;

        let x_overflow = column1_width_pixels + column3_width_pixels;
        let y_overlfow = row1_height_pixels + row3_height_pixels;

        //top left
        let bounds = Rect {
            position: patch_bounds.position,
            size: Vector2::new(column1_width_pixels, row1_height_pixels),
        };
        let tex_coords = [
            Vector2::<f32>::new(0.0, 0.0),
            Vector2::new(x_fence_post1_uv, 0.0),
            Vector2::new(x_fence_post1_uv, y_fence_post1_uv),
            Vector2::new(0.0, y_fence_post1_uv),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //top center
        let bounds = Rect {
            position: Vector2::new(
                patch_bounds.position.x + column1_width_pixels,
                patch_bounds.position.y,
            ),
            size: Vector2::new(patch_bounds.size.x - x_overflow, row1_height_pixels),
        };
        let tex_coords = [
            Vector2::<f32>::new(x_fence_post1_uv, 0.0),
            Vector2::new(x_fence_post2_uv, 0.0),
            Vector2::new(x_fence_post2_uv, y_fence_post1_uv),
            Vector2::new(x_fence_post1_uv, y_fence_post1_uv),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //top right
        let bounds = Rect {
            position: Vector2::new(
                (patch_bounds.position.x + patch_bounds.size.x) - column3_width_pixels,
                patch_bounds.position.y,
            ),
            size: Vector2::new(column3_width_pixels, row1_height_pixels),
        };
        let tex_coords = [
            Vector2::<f32>::new(x_fence_post2_uv, 0.0),
            Vector2::new(1.0, 0.0),
            Vector2::new(1.0, y_fence_post1_uv),
            Vector2::new(x_fence_post2_uv, y_fence_post1_uv),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );
        ////////////////////////////////////////////////////////////////////////////////
        //middle left
        let bounds = Rect {
            position: Vector2::new(
                patch_bounds.position.x,
                patch_bounds.position.y + row1_height_pixels,
            ),
            size: Vector2::new(column1_width_pixels, patch_bounds.size.y - y_overlfow),
        };
        let tex_coords = [
            Vector2::<f32>::new(0.0, y_fence_post1_uv),
            Vector2::new(x_fence_post1_uv, y_fence_post1_uv),
            Vector2::new(x_fence_post1_uv, y_fence_post2_uv),
            Vector2::new(0.0, y_fence_post2_uv),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //middle center
        let bounds = Rect {
            position: Vector2::new(
                patch_bounds.position.x + column1_width_pixels,
                patch_bounds.position.y + row1_height_pixels,
            ),
            size: Vector2::new(
                patch_bounds.size.x - x_overflow,
                patch_bounds.size.y - y_overlfow,
            ),
        };
        let tex_coords = [
            Vector2::<f32>::new(x_fence_post1_uv, y_fence_post1_uv),
            Vector2::new(x_fence_post2_uv, y_fence_post1_uv),
            Vector2::new(x_fence_post2_uv, y_fence_post2_uv),
            Vector2::new(x_fence_post1_uv, y_fence_post2_uv),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //middle right
        let bounds = Rect {
            position: Vector2::new(
                (patch_bounds.position.x + patch_bounds.size.x) - column3_width_pixels,
                patch_bounds.position.y + row1_height_pixels,
            ),
            size: Vector2::new(column3_width_pixels, patch_bounds.size.y - y_overlfow),
        };
        let tex_coords = [
            Vector2::<f32>::new(x_fence_post2_uv, y_fence_post1_uv),
            Vector2::new(1.0, y_fence_post1_uv),
            Vector2::new(1.0, y_fence_post2_uv),
            Vector2::new(x_fence_post2_uv, y_fence_post2_uv),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        ////////////////////////////////////////////////////////////////////////////////
        //bottom left
        let bounds = Rect {
            position: Vector2::new(
                patch_bounds.position.x,
                (patch_bounds.position.y + patch_bounds.size.y) - row3_height_pixels,
            ),
            size: Vector2::new(column1_width_pixels, row3_height_pixels),
        };
        let tex_coords = [
            Vector2::<f32>::new(0.0, y_fence_post2_uv),
            Vector2::new(x_fence_post1_uv, y_fence_post2_uv),
            Vector2::new(x_fence_post1_uv, 1.0),
            Vector2::new(0.0, 1.0),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //bottom center
        let bounds = Rect {
            position: Vector2::new(
                patch_bounds.position.x + column1_width_pixels,
                (patch_bounds.position.y + patch_bounds.size.y) - row3_height_pixels,
            ),
            size: Vector2::new(patch_bounds.size.x - x_overflow, row3_height_pixels),
        };
        let tex_coords = [
            Vector2::<f32>::new(x_fence_post1_uv, y_fence_post2_uv),
            Vector2::new(x_fence_post2_uv, y_fence_post2_uv),
            Vector2::new(x_fence_post2_uv, 1.0),
            Vector2::new(x_fence_post1_uv, 1.0),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //bottom right
        let bounds = Rect {
            position: Vector2::new(
                (patch_bounds.position.x + patch_bounds.size.x) - column3_width_pixels,
                (patch_bounds.position.y + patch_bounds.size.y) - row3_height_pixels,
            ),
            size: Vector2::new(column3_width_pixels, row3_height_pixels),
        };
        let tex_coords = [
            Vector2::<f32>::new(x_fence_post2_uv, y_fence_post2_uv),
            Vector2::new(1.0, y_fence_post2_uv),
            Vector2::new(1.0, 1.0),
            Vector2::new(x_fence_post2_uv, 1.0),
        ];
        draw_image(
            texture,
            bounds,
            &tex_coords,
            self.clip_bounds(),
            self.widget.background(),
            drawing_context,
        );

        //end drawing
    }

    fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
        self.widget.handle_routed_message(ui, message);
    }
}

pub struct NinePatchBuilder {
    widget_builder: WidgetBuilder,
    texture: Option<UntypedResource>,

    pub bottom_margin_pixel: Option<u32>,
    pub left_margin_pixel: Option<u32>,
    pub right_margin_pixel: Option<u32>,
    pub top_margin_pixel: Option<u32>,

    pub bottom_margin_uv: Option<f32>,
    pub left_margin_uv: Option<f32>,
    pub right_margin_uv: Option<f32>,
    pub top_margin_uv: Option<f32>,
}

impl NinePatchBuilder {
    pub fn new(widget_builder: WidgetBuilder) -> Self {
        Self {
            widget_builder,
            texture: None,

            bottom_margin_uv: None,
            left_margin_uv: None,
            right_margin_uv: None,
            top_margin_uv: None,

            bottom_margin_pixel: None,
            left_margin_pixel: None,
            right_margin_pixel: None,
            top_margin_pixel: None,
        }
    }

    pub fn with_texture(mut self, texture: UntypedResource) -> Self {
        self.texture = Some(texture);
        self
    }
    pub fn with_bottom_margin_uv(mut self, margin: f32) -> Self {
        self.bottom_margin_uv = Some(margin);
        self
    }
    pub fn with_left_margin_uv(mut self, margin: f32) -> Self {
        self.left_margin_uv = Some(margin);
        self
    }
    pub fn with_right_margin_uv(mut self, margin: f32) -> Self {
        self.right_margin_uv = Some(margin);
        self
    }
    pub fn with_top_margin_uv(mut self, margin: f32) -> Self {
        self.top_margin_uv = Some(margin);
        self
    }
    pub fn with_bottom_margin_pixel(mut self, margin: u32) -> Self {
        self.bottom_margin_pixel = Some(margin);
        self
    }
    pub fn with_left_margin_pixel(mut self, margin: u32) -> Self {
        self.left_margin_pixel = Some(margin);
        self
    }
    pub fn with_right_margin_pixel(mut self, margin: u32) -> Self {
        self.right_margin_pixel = Some(margin);
        self
    }
    pub fn with_top_margin_pixel(mut self, margin: u32) -> Self {
        self.top_margin_pixel = Some(margin);
        self
    }
    pub fn build(mut self, ui: &mut BuildContext) -> Handle<UiNode> {
        if self.widget_builder.background.is_none() {
            self.widget_builder.background = Some(Brush::Solid(Color::WHITE))
        }

        // if one of the margins hasn't been set just mirror the opposite one.
        let (left_margin_pixel, right_margin_pixel) =
            match (self.left_margin_pixel, self.right_margin_pixel) {
                (Some(x), None) => (x, x),
                (None, Some(x)) => (x, x),
                (Some(one), Some(two)) => (one, two),
                (None, None) => (0, 0),
            };

        let (top_margin_pixel, bottom_margin_pixel) =
            match (self.top_margin_pixel, self.bottom_margin_pixel) {
                (Some(y), None) => (y, y),
                (None, Some(y)) => (y, y),
                (Some(one), Some(two)) => (one, two),
                (None, None) => (0, 0),
            };

        let (left_margin_uv, right_margin_uv) = match (self.left_margin_uv, self.right_margin_uv) {
            (Some(x), None) => (x, x),
            (None, Some(x)) => (x, x),
            (Some(one), Some(two)) => (one, two),
            (None, None) => (0.0, 0.0),
        };

        let (top_margin_uv, bottom_margin_uv) = match (self.top_margin_uv, self.bottom_margin_uv) {
            (Some(y), None) => (y, y),
            (None, Some(y)) => (y, y),
            (Some(one), Some(two)) => (one, two),
            (None, None) => (0.0, 0.0),
        };

        let grid = NinePatch {
            widget: self.widget_builder.build(),
            texture: self.texture.into(),
            bottom_margin_pixel: bottom_margin_pixel.into(),
            bottom_margin_uv: bottom_margin_uv.into(),
            left_margin_pixel: left_margin_pixel.into(),
            left_margin_uv: left_margin_uv.into(),
            right_margin_pixel: right_margin_pixel.into(),
            right_margin_uv: right_margin_uv.into(),
            top_margin_pixel: top_margin_pixel.into(),
            top_margin_uv: top_margin_uv.into(),
        };
        ui.add_node(UiNode::new(grid))
    }
}
fn draw_image(
    image: &UntypedResource,
    bounds: Rect<f32>,
    tex_coords: &[Vector2<f32>; 4],
    clip_bounds: Rect<f32>,
    background: Brush,
    drawing_context: &mut DrawingContext,
) {
    drawing_context.push_rect_filled(&bounds, Some(tex_coords));
    let texture = CommandTexture::Texture(image.clone());
    drawing_context.commit(clip_bounds, background, texture, None);
}