getting some drawing done
This commit is contained in:
parent
92b0716d92
commit
df6dceab70
2 changed files with 88 additions and 2 deletions
12
src/drawable.rs
Normal file
12
src/drawable.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
pub(crate) trait Drawable {
|
||||
fn draw(&self, draw: &Draw);
|
||||
}
|
||||
|
||||
impl<T> Drawable for Vec<T>
|
||||
where
|
||||
T: Drawable,
|
||||
{
|
||||
fn draw(&self, draw: &Draw) {
|
||||
self.iter().for_each(|s| s.draw(draw))
|
||||
}
|
||||
}
|
78
src/main.rs
78
src/main.rs
|
@ -1,3 +1,77 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod drawable;
|
||||
|
||||
use drawable::Drawable;
|
||||
use nannou::prelude::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Particle {
|
||||
pos: Vec2,
|
||||
radius: f32,
|
||||
}
|
||||
|
||||
impl Particle {
|
||||
fn new(pos: Vec2) -> Self {
|
||||
Self {
|
||||
pos,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Particle {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
pos: Default::default(),
|
||||
radius: 10.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Particle {
|
||||
fn draw(&self, draw: &Draw) {
|
||||
draw.ellipse().color(RED).radius(self.radius).xy(self.pos);
|
||||
}
|
||||
}
|
||||
|
||||
struct Model {
|
||||
// Store the window ID so we can refer to this specific window later if needed.
|
||||
_window: WindowId,
|
||||
}
|
||||
|
||||
fn model(app: &App) -> Model {
|
||||
// Create a new window! Store the ID so we can refer to it later.
|
||||
let _window = app
|
||||
.new_window()
|
||||
.size(512, 512)
|
||||
.title("nannou")
|
||||
.view(view) // The function that will be called for presenting graphics to a frame.
|
||||
.event(event) // The function that will be called when the window receives events.
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
Model { _window }
|
||||
}
|
||||
|
||||
// Handle events related to the window and update the model if necessary
|
||||
fn event(_app: &App, _model: &mut Model, event: WindowEvent) {
|
||||
if let KeyReleased(Key::Escape) = event {
|
||||
_app.quit()
|
||||
}
|
||||
|
||||
println!("{:?}", event);
|
||||
}
|
||||
|
||||
// Draw the state of your `Model` into the given `Frame` here.
|
||||
fn view(app: &App, _model: &Model, frame: Frame) {
|
||||
let draw = app.draw();
|
||||
draw.background().color(CORNFLOWERBLUE);
|
||||
|
||||
draw.ellipse().color(STEELBLUE).x_y(200.0, -100.0);
|
||||
|
||||
// I don't think there is even a fail condition in this function, but it returns a result?
|
||||
draw.to_frame(app, &frame).unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
nannou::app(model).run();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue