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
use crate::{render::Image, render_object::*};

/// Used to render an image.
pub struct ImageRenderObject;

impl Into<Box<dyn RenderObject>> for ImageRenderObject {
    fn into(self) -> Box<dyn RenderObject> {
        Box::new(self)
    }
}

impl RenderObject for ImageRenderObject {
    fn render_self(&self, ctx: &mut Context, global_position: &Point) {
        let (bounds, mut image) = {
            let widget = ctx.widget();
            (
                widget.clone::<Rectangle>("bounds"),
                widget.try_clone::<Image>("image"),
            )
        };

        if let Some(image) = &mut image {
            ctx.render_context_2_d().draw_image(
                image,
                bounds.x() + global_position.x(),
                bounds.y() + global_position.y(),
            );
        }
    }
}