summaryrefslogtreecommitdiffstats
path: root/examples/tour/renderer/radio.rs
blob: 0f7815d6195ea1aa629dcfba4d4d890d99b76a7c (plain) (blame)
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
use super::Renderer;

use ggez::graphics::{DrawParam, Rect};
use iced::{radio, MouseCursor, Point, Rectangle};

const SPRITE: Rect = Rect {
    x: 98.0,
    y: 28.0,
    w: 28.0,
    h: 28.0,
};

impl radio::Renderer for Renderer<'_> {
    fn draw(
        &mut self,
        cursor_position: Point,
        bounds: Rectangle,
        bounds_with_label: Rectangle,
        is_selected: bool,
    ) -> MouseCursor {
        let mouse_over = bounds_with_label.contains(cursor_position);

        let width = self.spritesheet.width() as f32;
        let height = self.spritesheet.height() as f32;

        self.sprites.add(DrawParam {
            src: Rect {
                x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 }))
                    / width,
                y: SPRITE.y / height,
                w: SPRITE.w / width,
                h: SPRITE.h / height,
            },
            dest: ggez::mint::Point2 {
                x: bounds.x,
                y: bounds.y,
            },
            ..DrawParam::default()
        });

        if is_selected {
            self.sprites.add(DrawParam {
                src: Rect {
                    x: (SPRITE.x + SPRITE.w * 2.0) / width,
                    y: SPRITE.y / height,
                    w: SPRITE.w / width,
                    h: SPRITE.h / height,
                },
                dest: ggez::mint::Point2 {
                    x: bounds.x,
                    y: bounds.y,
                },
                ..DrawParam::default()
            });
        }

        if mouse_over {
            MouseCursor::Pointer
        } else {
            MouseCursor::OutOfBounds
        }
    }
}