Created
July 27, 2014 10:11
-
-
Save bvssvni/2e9c55aa98a0f65fe942 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(globs)] | |
#![feature(default_type_params)] | |
extern crate debug; | |
extern crate graphics; | |
extern crate piston; | |
extern crate sdl2_game_window; | |
extern crate opengl_graphics; | |
use Window = sdl2_game_window::GameWindowSDL2; | |
use graphics::*; | |
use opengl_graphics::{ | |
Gl, | |
}; | |
use piston::{ | |
Render, | |
GameEvent, | |
GameIterator, | |
GameIteratorSettings, | |
GameWindowSettings, | |
MouseMove, | |
MousePress, | |
MouseRelease, | |
}; | |
pub use render::Render; | |
pub use circle::Circle; | |
fn main() { | |
let mut window = Window::new( | |
GameWindowSettings { | |
title: "Do da di".to_string(), | |
size: [1440, 900], | |
fullscreen: true, | |
exit_on_esc: true | |
} | |
); | |
let circles_model: Vec<Circle> = Vec::new(); | |
let mut circles = CircleTool::new().model(circles_model); | |
let game_iter_settings = GameIteratorSettings { | |
updates_per_second: 120, | |
max_frames_per_second: 30, | |
}; | |
let ref mut gl = Gl::new(); | |
for e in GameIterator::new(&mut window, &game_iter_settings) { | |
circles.edit(&e); | |
match e { | |
Render(args) => { | |
gl.viewport(0, 0, args.width as i32, args.height as i32); | |
let ref c = Context::abs( | |
args.width as f64, | |
args.height as f64 | |
); | |
// Clear screen. | |
c.rgb(0.0, 0.0, 0.0).draw(gl); | |
circles.circles.render(c, gl); | |
}, | |
_ => {} | |
} | |
} | |
} | |
pub mod render; | |
pub mod circle; | |
pub trait CircleModel { | |
fn add_circle(&mut self, x: f64, y: f64); | |
fn set_last_radius(&mut self, radius: f64); | |
} | |
impl CircleModel for Vec<Circle> { | |
fn add_circle(&mut self, x: f64, y: f64) { | |
self.push(Circle::new(x, y)) | |
} | |
fn set_last_radius(&mut self, radius: f64) { | |
let n = self.len(); | |
if n == 0 { return; } | |
let last_circle = self.get_mut(n - 1); | |
last_circle.radius = radius; | |
} | |
} | |
pub struct CircleTool<CircleModel=()> { | |
pub circles: CircleModel, | |
pub cursor: (f64, f64), | |
pub down_pos: (f64, f64), | |
pub change_radius: bool, | |
} | |
impl<M: CircleModel> CircleTool<M> { | |
pub fn edit(&mut self, e: &GameEvent) { | |
match *e { | |
MousePress(_) => { | |
// Add a new circle. | |
let (x, y) = self.cursor; | |
self.circles.add_circle(x, y); | |
self.down_pos = self.cursor; | |
self.change_radius = true; | |
}, | |
MouseRelease(_) => { | |
self.change_radius = false; | |
}, | |
MouseMove(args) => { | |
// Register cursor position. | |
self.cursor = (args.x, args.y); | |
let (down_x, down_y) = self.down_pos; | |
// Change radius of last circle. | |
if !self.change_radius { return; } | |
let (dx, dy) = (args.x - down_x, args.y - down_y); | |
let rad = (dx * dx + dy * dy).sqrt(); | |
self.circles.set_last_radius(rad); | |
}, | |
_ => {} | |
}; | |
} | |
} | |
impl CircleTool { | |
/// Creates a new circle tool. | |
pub fn new() -> CircleTool { | |
CircleTool { | |
circles: (), | |
cursor: (0.0, 0.0), | |
down_pos: (0.0, 0.0), | |
change_radius: false, | |
} | |
} | |
/// Sets the model. | |
pub fn model<M: CircleModel>(self, model: M) -> CircleTool<M> { | |
CircleTool { | |
circles: model, | |
cursor: self.cursor, | |
down_pos: self.down_pos, | |
change_radius: self.change_radius | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment