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

@ -1,25 +1,20 @@
use std::fmt::Debug;
use iced::executor;
use iced::mouse;
use iced::widget::canvas::event::{self, Event};
use iced::widget::canvas::{self, Canvas};
use iced::widget::{column, row, slider, text};
use iced::{
Application, Color, Command, Length, Point, Rectangle, Renderer, Settings,
Size, Theme,
};
use iced::{Color, Length, Point, Rectangle, Renderer, Size, Theme};
use rand::Rng;
use std::fmt::Debug;
fn main() -> iced::Result {
SierpinskiEmulator::run(Settings {
antialiasing: true,
..Settings::default()
})
iced::sandbox(SierpinskiEmulator::update, SierpinskiEmulator::view)
.title("Sierpinski Triangle - Iced")
.antialiased()
.run()
}
#[derive(Debug)]
#[derive(Debug, Default)]
struct SierpinskiEmulator {
graph: SierpinskiGraph,
}
@ -31,27 +26,8 @@ pub enum Message {
PointRemoved,
}
impl Application for SierpinskiEmulator {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();
fn new(_flags: Self::Flags) -> (Self, iced::Command<Self::Message>) {
let emulator = SierpinskiEmulator {
graph: SierpinskiGraph::new(),
};
(emulator, Command::none())
}
fn title(&self) -> String {
"Sierpinski Triangle Emulator".to_string()
}
fn update(
&mut self,
message: Self::Message,
) -> iced::Command<Self::Message> {
impl SierpinskiEmulator {
fn update(&mut self, message: Message) {
match message {
Message::IterationSet(cur_iter) => {
self.graph.iteration = cur_iter;
@ -67,11 +43,9 @@ impl Application for SierpinskiEmulator {
}
self.graph.redraw();
Command::none()
}
fn view(&self) -> iced::Element<'_, Self::Message> {
fn view(&self) -> iced::Element<'_, Message> {
column![
Canvas::new(&self.graph)
.width(Length::Fill)
@ -167,10 +141,6 @@ impl canvas::Program<Message> for SierpinskiGraph {
}
impl SierpinskiGraph {
fn new() -> SierpinskiGraph {
SierpinskiGraph::default()
}
fn redraw(&mut self) {
self.cache.clear();
}