summaryrefslogtreecommitdiffstats
path: root/examples/integration/src/controls.rs
blob: 0999336bbc0bb4c5771790a37d5b4a36cabe64c5 (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
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
use crate::Scene;

use iced_wgpu::Renderer;
use iced_winit::{
    slider, Align, Color, Column, Element, Length, Row, Slider, Text,
};

pub struct Controls {
    sliders: [slider::State; 3],
}

#[derive(Debug)]
pub enum Message {
    BackgroundColorChanged(Color),
}

impl Controls {
    pub fn new() -> Controls {
        Controls {
            sliders: Default::default(),
        }
    }

    pub fn update(&self, message: Message, scene: &mut Scene) {
        match message {
            Message::BackgroundColorChanged(color) => {
                scene.background_color = color;
            }
        }
    }

    pub fn view(&mut self, scene: &Scene) -> Element<Message, Renderer> {
        let [r, g, b] = &mut self.sliders;
        let background_color = scene.background_color;

        let sliders = Row::new()
            .width(Length::Units(500))
            .spacing(20)
            .push(Slider::new(
                r,
                0.0..=1.0,
                scene.background_color.r,
                move |r| {
                    Message::BackgroundColorChanged(Color {
                        r,
                        ..background_color
                    })
                },
            ))
            .push(Slider::new(
                g,
                0.0..=1.0,
                scene.background_color.g,
                move |g| {
                    Message::BackgroundColorChanged(Color {
                        g,
                        ..background_color
                    })
                },
            ))
            .push(Slider::new(
                b,
                0.0..=1.0,
                scene.background_color.b,
                move |b| {
                    Message::BackgroundColorChanged(Color {
                        b,
                        ..background_color
                    })
                },
            ));

        Row::new()
            .width(Length::Fill)
            .height(Length::Fill)
            .align_items(Align::End)
            .push(
                Column::new()
                    .width(Length::Fill)
                    .align_items(Align::End)
                    .push(
                        Column::new()
                            .padding(10)
                            .spacing(10)
                            .push(
                                Text::new("Background color")
                                    .color(Color::WHITE),
                            )
                            .push(sliders)
                            .push(
                                Text::new(format!("{:?}", background_color))
                                    .size(14)
                                    .color(Color::WHITE),
                            ),
                    ),
            )
            .into()
    }
}