Simplify integration example with Program
This commit is contained in:
parent
ae5e2c6c73
commit
d54f17c6aa
3 changed files with 67 additions and 116 deletions
|
|
@ -1,11 +1,11 @@
|
|||
use crate::Scene;
|
||||
|
||||
use iced_wgpu::Renderer;
|
||||
use iced_winit::{
|
||||
slider, Align, Color, Column, Element, Length, Row, Slider, Text,
|
||||
slider, Align, Color, Column, Command, Element, Length, Program, Row,
|
||||
Slider, Text,
|
||||
};
|
||||
|
||||
pub struct Controls {
|
||||
background_color: Color,
|
||||
sliders: [slider::State; 3],
|
||||
}
|
||||
|
||||
|
|
@ -17,58 +17,55 @@ pub enum Message {
|
|||
impl Controls {
|
||||
pub fn new() -> Controls {
|
||||
Controls {
|
||||
background_color: Color::BLACK,
|
||||
sliders: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&self, message: Message, scene: &mut Scene) {
|
||||
pub fn background_color(&self) -> Color {
|
||||
self.background_color
|
||||
}
|
||||
}
|
||||
|
||||
impl Program for Controls {
|
||||
type Renderer = Renderer;
|
||||
type Message = Message;
|
||||
|
||||
fn update(&mut self, message: Message) -> Command<Message> {
|
||||
match message {
|
||||
Message::BackgroundColorChanged(color) => {
|
||||
scene.background_color = color;
|
||||
self.background_color = color;
|
||||
}
|
||||
}
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
||||
pub fn view(&mut self, scene: &Scene) -> Element<Message, Renderer> {
|
||||
fn view(&mut self) -> Element<Message, Renderer> {
|
||||
let [r, g, b] = &mut self.sliders;
|
||||
let background_color = scene.background_color;
|
||||
let background_color = self.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
|
||||
})
|
||||
},
|
||||
));
|
||||
.push(Slider::new(r, 0.0..=1.0, background_color.r, move |r| {
|
||||
Message::BackgroundColorChanged(Color {
|
||||
r,
|
||||
..background_color
|
||||
})
|
||||
}))
|
||||
.push(Slider::new(g, 0.0..=1.0, background_color.g, move |g| {
|
||||
Message::BackgroundColorChanged(Color {
|
||||
g,
|
||||
..background_color
|
||||
})
|
||||
}))
|
||||
.push(Slider::new(b, 0.0..=1.0, background_color.b, move |b| {
|
||||
Message::BackgroundColorChanged(Color {
|
||||
b,
|
||||
..background_color
|
||||
})
|
||||
}));
|
||||
|
||||
Row::new()
|
||||
.width(Length::Fill)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue