better drawable

This commit is contained in:
Zynh0722 2024-02-15 14:58:35 -08:00
parent 6d270344cf
commit 481ed64a85
3 changed files with 28 additions and 10 deletions

View file

@ -12,3 +12,19 @@ where
self.iter().for_each(|s| s.draw(draw)) self.iter().for_each(|s| s.draw(draw))
} }
} }
pub(crate) trait DrawShape<T>
where
T: Drawable,
{
fn draw(&self, drawable: &T);
}
impl<T> DrawShape<T> for Draw
where
T: Drawable,
{
fn draw(&self, drawable: &T) {
drawable.draw(self);
}
}

View file

@ -1,10 +1,13 @@
mod drawable; mod drawable;
mod particle; mod particle;
use drawable::Drawable; use drawable::{DrawShape, Drawable};
use nannou::prelude::*; use nannou::prelude::*;
use particle::Particle; use particle::Particle;
const PARTICLE_COUNT: u32 = 200;
const PARTICLE_SIZE: f32 = 10.0;
struct Model { struct Model {
// Store the window ID so we can refer to this specific window later if needed. // Store the window ID so we can refer to this specific window later if needed.
_window: WindowId, _window: WindowId,
@ -14,7 +17,7 @@ struct Model {
fn fill_particles(app: &App, particles: &mut Vec<Particle>) { fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
let boundary = app.window_rect(); let boundary = app.window_rect();
for _ in 0..200 { for _ in 0..PARTICLE_COUNT {
let x = random_range(boundary.left(), boundary.right()); let x = random_range(boundary.left(), boundary.right());
let y = random_range(boundary.top(), boundary.bottom()); let y = random_range(boundary.top(), boundary.bottom());
@ -59,13 +62,13 @@ fn event(app: &App, model: &mut Model, event: WindowEvent) {
// Draw the state of your `Model` into the given `Frame` here. // Draw the state of your `Model` into the given `Frame` here.
fn view(app: &App, model: &Model, frame: Frame) { fn view(app: &App, model: &Model, frame: Frame) {
let draw = app.draw(); let canvas = app.draw();
draw.background().color(CORNFLOWERBLUE); canvas.background().color(CORNFLOWERBLUE);
model.particles.draw(&draw); canvas.draw(&model.particles);
// I don't think there is even a fail condition in this function, but it returns a result? // I don't think there is even a fail condition in this function, but it returns a result?
draw.to_frame(app, &frame).unwrap(); canvas.to_frame(app, &frame).unwrap();
} }
fn main() { fn main() {

View file

@ -1,6 +1,5 @@
use drawable::Drawable; use crate::drawable::Drawable;
use crate::PARTICLE_SIZE;
use crate::drawable;
use nannou::prelude::*; use nannou::prelude::*;
@ -23,7 +22,7 @@ impl Default for Particle {
fn default() -> Self { fn default() -> Self {
Self { Self {
pos: Default::default(), pos: Default::default(),
radius: 10.0, radius: PARTICLE_SIZE,
} }
} }
} }