image/
image.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5extern crate suzy;
6
7use suzy::dims::{Rect, SimplePadding2d};
8use suzy::platform::opengl::SlicedImage;
9use suzy::platform::opengl::Texture;
10use suzy::widget::*;
11
12const IMAGE: &[u8] = include_bytes!("cute.data");
13const IMAGE_WIDTH: u16 = 384;
14const IMAGE_HEIGHT: u16 = 512;
15const IMAGE_ASPECT: f32 = 384.0 / 512.0;
16
17// Root widget data
18#[derive(Default)]
19struct ImageViewer {
20    image: SlicedImage,
21}
22
23impl WidgetContent for ImageViewer {
24    fn init(mut init: impl WidgetInit<Self>) {
25        init.watch(|this, _rect| {
26            this.image.set_image(
27                Texture::from_rgb(IMAGE_WIDTH, IMAGE_HEIGHT, 1, IMAGE),
28                &SimplePadding2d::zero(),
29            );
30        });
31        init.watch(|this, rect| {
32            // fill the screen with the image
33            this.image.set_fill(&rect, &SimplePadding2d::zero());
34            // but shrink it so it stays the same aspect ratio
35            this.image.shrink_to_aspect(IMAGE_ASPECT);
36        });
37    }
38
39    fn children(&mut self, _receiver: impl WidgetChildReceiver) {
40        // no widget children
41    }
42
43    fn graphics(&mut self, mut receiver: impl WidgetGraphicReceiver) {
44        receiver.graphic(&mut self.image);
45    }
46}
47
48fn main() {
49    ImageViewer::run_as_app();
50}