Introduce Program API

This commit is contained in:
Héctor Ramón Jiménez 2024-03-16 05:33:47 +01:00
parent 0524e9b457
commit c22269bff3
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
43 changed files with 1141 additions and 831 deletions

View file

@ -2,18 +2,17 @@ mod scene;
use scene::Scene;
use iced::executor;
use iced::time::Instant;
use iced::widget::shader::wgpu;
use iced::widget::{checkbox, column, container, row, shader, slider, text};
use iced::window;
use iced::{
Alignment, Application, Color, Command, Element, Length, Subscription,
Theme,
};
use iced::{Alignment, Color, Element, Length, Subscription};
fn main() -> iced::Result {
IcedCubes::run(iced::Settings::default())
iced::sandbox(IcedCubes::update, IcedCubes::view)
.subscription(IcedCubes::subscription)
.title("Custom Shader - Iced")
.run()
}
struct IcedCubes {
@ -30,27 +29,15 @@ enum Message {
LightColorChanged(Color),
}
impl Application for IcedCubes {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();
fn new(_flags: Self::Flags) -> (Self, Command<Self::Message>) {
(
Self {
start: Instant::now(),
scene: Scene::new(),
},
Command::none(),
)
impl IcedCubes {
fn new() -> Self {
Self {
start: Instant::now(),
scene: Scene::new(),
}
}
fn title(&self) -> String {
"Iced Cubes".to_string()
}
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
fn update(&mut self, message: Message) {
match message {
Message::CubeAmountChanged(amount) => {
self.scene.change_amount(amount);
@ -68,11 +55,9 @@ impl Application for IcedCubes {
self.scene.light_color = color;
}
}
Command::none()
}
fn view(&self) -> Element<'_, Self::Message> {
fn view(&self) -> Element<'_, Message> {
let top_controls = row![
control(
"Amount",
@ -147,11 +132,17 @@ impl Application for IcedCubes {
.into()
}
fn subscription(&self) -> Subscription<Self::Message> {
fn subscription(&self) -> Subscription<Message> {
window::frames().map(Message::Tick)
}
}
impl Default for IcedCubes {
fn default() -> Self {
Self::new()
}
}
fn control<'a>(
label: &'static str,
control: impl Into<Element<'a, Message>>,