From aeede66963a2e512ed299bdfd502c2c63a99cca9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 27 Apr 2024 15:11:03 +0200 Subject: Create `the_matrix` example --- examples/the_matrix/src/main.rs | 116 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 examples/the_matrix/src/main.rs (limited to 'examples/the_matrix/src/main.rs') diff --git a/examples/the_matrix/src/main.rs b/examples/the_matrix/src/main.rs new file mode 100644 index 00000000..55c9da4b --- /dev/null +++ b/examples/the_matrix/src/main.rs @@ -0,0 +1,116 @@ +use iced::mouse; +use iced::time::{self, Instant}; +use iced::widget::canvas; +use iced::widget::canvas::{Cache, Geometry}; +use iced::{ + Color, Element, Font, Length, Point, Rectangle, Renderer, Subscription, + Theme, +}; + +pub fn main() -> iced::Result { + iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view) + .subscription(TheMatrix::subscription) + .antialiasing(true) + .run() +} + +struct TheMatrix { + ticks: usize, + backgrounds: Vec, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Tick(Instant), +} + +impl TheMatrix { + fn update(&mut self, message: Message) { + match message { + Message::Tick(_now) => { + self.ticks += 1; + } + } + } + + fn view(&self) -> Element { + canvas(self as &Self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } + + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(50)).map(Message::Tick) + } +} + +impl Default for TheMatrix { + fn default() -> Self { + let mut backgrounds = Vec::with_capacity(30); + backgrounds.resize_with(30, Cache::default); + + Self { + ticks: 0, + backgrounds, + } + } +} + +impl canvas::Program for TheMatrix { + type State = (); + + fn draw( + &self, + _state: &Self::State, + renderer: &Renderer, + _theme: &Theme, + bounds: Rectangle, + _cursor: mouse::Cursor, + ) -> Vec { + use rand::distributions::Distribution; + use rand::Rng; + + const CELL_SIZE: f32 = 10.0; + + vec![self.backgrounds[self.ticks % self.backgrounds.len()].draw( + renderer, + bounds.size(), + |frame| { + frame.fill_rectangle(Point::ORIGIN, frame.size(), Color::BLACK); + + let mut rng = rand::thread_rng(); + let rows = (frame.height() / CELL_SIZE).ceil() as usize; + let columns = (frame.width() / CELL_SIZE).ceil() as usize; + + for row in 0..rows { + for column in 0..columns { + let position = Point::new( + column as f32 * CELL_SIZE, + row as f32 * CELL_SIZE, + ); + + let alphas = [0.05, 0.1, 0.2, 0.5]; + let weights = [10, 4, 2, 1]; + let distribution = + rand::distributions::WeightedIndex::new(weights) + .expect("Create distribution"); + + frame.fill_text(canvas::Text { + content: rng.gen_range('!'..'z').to_string(), + position, + color: Color { + a: alphas[distribution.sample(&mut rng)], + g: 1.0, + ..Color::BLACK + }, + size: CELL_SIZE.into(), + font: Font::MONOSPACE, + ..canvas::Text::default() + }); + } + } + }, + )] + } +} -- cgit From 3c7b43d031a06d59afbba83bc9d088517c096f20 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Apr 2024 08:06:51 +0200 Subject: Use shared `Cache` group in `the_matrix` example --- examples/the_matrix/src/main.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'examples/the_matrix/src/main.rs') diff --git a/examples/the_matrix/src/main.rs b/examples/the_matrix/src/main.rs index 55c9da4b..97ad31b9 100644 --- a/examples/the_matrix/src/main.rs +++ b/examples/the_matrix/src/main.rs @@ -1,12 +1,13 @@ use iced::mouse; use iced::time::{self, Instant}; use iced::widget::canvas; -use iced::widget::canvas::{Cache, Geometry}; use iced::{ Color, Element, Font, Length, Point, Rectangle, Renderer, Subscription, Theme, }; +use std::cell::RefCell; + pub fn main() -> iced::Result { iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view) .subscription(TheMatrix::subscription) @@ -15,8 +16,7 @@ pub fn main() -> iced::Result { } struct TheMatrix { - ticks: usize, - backgrounds: Vec, + tick: usize, } #[derive(Debug, Clone, Copy)] @@ -28,7 +28,7 @@ impl TheMatrix { fn update(&mut self, message: Message) { match message { Message::Tick(_now) => { - self.ticks += 1; + self.tick += 1; } } } @@ -47,33 +47,35 @@ impl TheMatrix { impl Default for TheMatrix { fn default() -> Self { - let mut backgrounds = Vec::with_capacity(30); - backgrounds.resize_with(30, Cache::default); - - Self { - ticks: 0, - backgrounds, - } + Self { tick: 0 } } } impl canvas::Program for TheMatrix { - type State = (); + type State = RefCell>; fn draw( &self, - _state: &Self::State, + state: &Self::State, renderer: &Renderer, _theme: &Theme, bounds: Rectangle, _cursor: mouse::Cursor, - ) -> Vec { + ) -> Vec { use rand::distributions::Distribution; use rand::Rng; const CELL_SIZE: f32 = 10.0; - vec![self.backgrounds[self.ticks % self.backgrounds.len()].draw( + let mut caches = state.borrow_mut(); + + if caches.is_empty() { + let group = canvas::Group::unique(); + + caches.resize_with(30, || canvas::Cache::with_group(group)); + } + + vec![caches[self.tick % caches.len()].draw( renderer, bounds.size(), |frame| { -- cgit From cfe4ddb86620f9b21027c8a828e80df9a8887407 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Apr 2024 08:10:22 +0200 Subject: Fix `clippy` lint in `the_matrix` example --- examples/the_matrix/src/main.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'examples/the_matrix/src/main.rs') diff --git a/examples/the_matrix/src/main.rs b/examples/the_matrix/src/main.rs index 97ad31b9..4ed1db32 100644 --- a/examples/the_matrix/src/main.rs +++ b/examples/the_matrix/src/main.rs @@ -15,6 +15,7 @@ pub fn main() -> iced::Result { .run() } +#[derive(Default)] struct TheMatrix { tick: usize, } @@ -45,12 +46,6 @@ impl TheMatrix { } } -impl Default for TheMatrix { - fn default() -> Self { - Self { tick: 0 } - } -} - impl canvas::Program for TheMatrix { type State = RefCell>; -- cgit From c51b85e7ab067f5e7411eccd10a5ae192e6ee0a8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Apr 2024 21:59:46 +0200 Subject: Invalidate text uploads after atlas trimming --- examples/the_matrix/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples/the_matrix/src/main.rs') diff --git a/examples/the_matrix/src/main.rs b/examples/the_matrix/src/main.rs index 4ed1db32..f3a67ac8 100644 --- a/examples/the_matrix/src/main.rs +++ b/examples/the_matrix/src/main.rs @@ -9,6 +9,8 @@ use iced::{ use std::cell::RefCell; pub fn main() -> iced::Result { + tracing_subscriber::fmt::init(); + iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view) .subscription(TheMatrix::subscription) .antialiasing(true) -- cgit