Use shared Cache group in the_matrix example
This commit is contained in:
parent
b5b78d505e
commit
3c7b43d031
1 changed files with 17 additions and 15 deletions
|
|
@ -1,12 +1,13 @@
|
||||||
use iced::mouse;
|
use iced::mouse;
|
||||||
use iced::time::{self, Instant};
|
use iced::time::{self, Instant};
|
||||||
use iced::widget::canvas;
|
use iced::widget::canvas;
|
||||||
use iced::widget::canvas::{Cache, Geometry};
|
|
||||||
use iced::{
|
use iced::{
|
||||||
Color, Element, Font, Length, Point, Rectangle, Renderer, Subscription,
|
Color, Element, Font, Length, Point, Rectangle, Renderer, Subscription,
|
||||||
Theme,
|
Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view)
|
iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view)
|
||||||
.subscription(TheMatrix::subscription)
|
.subscription(TheMatrix::subscription)
|
||||||
|
|
@ -15,8 +16,7 @@ pub fn main() -> iced::Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TheMatrix {
|
struct TheMatrix {
|
||||||
ticks: usize,
|
tick: usize,
|
||||||
backgrounds: Vec<Cache>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
|
@ -28,7 +28,7 @@ impl TheMatrix {
|
||||||
fn update(&mut self, message: Message) {
|
fn update(&mut self, message: Message) {
|
||||||
match message {
|
match message {
|
||||||
Message::Tick(_now) => {
|
Message::Tick(_now) => {
|
||||||
self.ticks += 1;
|
self.tick += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -47,33 +47,35 @@ impl TheMatrix {
|
||||||
|
|
||||||
impl Default for TheMatrix {
|
impl Default for TheMatrix {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut backgrounds = Vec::with_capacity(30);
|
Self { tick: 0 }
|
||||||
backgrounds.resize_with(30, Cache::default);
|
|
||||||
|
|
||||||
Self {
|
|
||||||
ticks: 0,
|
|
||||||
backgrounds,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> canvas::Program<Message> for TheMatrix {
|
impl<Message> canvas::Program<Message> for TheMatrix {
|
||||||
type State = ();
|
type State = RefCell<Vec<canvas::Cache>>;
|
||||||
|
|
||||||
fn draw(
|
fn draw(
|
||||||
&self,
|
&self,
|
||||||
_state: &Self::State,
|
state: &Self::State,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
_theme: &Theme,
|
_theme: &Theme,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
_cursor: mouse::Cursor,
|
_cursor: mouse::Cursor,
|
||||||
) -> Vec<Geometry> {
|
) -> Vec<canvas::Geometry> {
|
||||||
use rand::distributions::Distribution;
|
use rand::distributions::Distribution;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
const CELL_SIZE: f32 = 10.0;
|
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,
|
renderer,
|
||||||
bounds.size(),
|
bounds.size(),
|
||||||
|frame| {
|
|frame| {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue