pub mod transform; use transform::*; use piston_window::grid::Grid; use piston_window::rectangle::{Border, Rectangle}; use piston_window::*; use sdl2_window::Sdl2Window; /// Helper function to turn two given points into a rectangle. The order of the two points is not /// important, they are considered two endpoints of a diagonal and therefore identify the rectangle /// unambiguously. fn bounding_box(p0: [f64; 2], p1: [f64; 2]) -> [f64; 4] { let min_x = p0[0].min(p1[0]); let min_y = p0[1].min(p1[1]); let max_x = p0[0].max(p1[0]); let max_y = p0[1].max(p1[1]); [min_x, min_y, max_x - min_x, max_y - min_y] } fn main() { let mut window: PistonWindow = WindowSettings::new("Hello there!", [1000, 1000]) .build() .expect("Could not initialise window"); // The amount of on-screen pixels used to represent a meter of actual terrain. let mut transform = Transform::new(); /* Create a rectangle that is used to draw all rectangles that were created by the user. It has * a thicc blacc border and white colour. */ let render_rect = Rectangle::new([0.7, 0.7, 0.7, 1.]).border(Border { color: [0.5, 0.5, 0.5, 1.], radius: 2., }); // The point the user has clicked. This is where they want the rectangle to start. let mut starting_rect_point: Option<[f64; 2]> = None; let mut rectangles = Vec::new(); // Line used to draw the square grid. let grid_line = Line::new([1., 1., 1., 0.3], 1.5); let mut mouse_pos_m = [0., 0.]; let mut events = Events::new(EventSettings::new().lazy(true)); while let Some(e) = events.next(&mut window) { // Update the mouse cursor position e.mouse_cursor(|pos| { mouse_pos_m = transform.point_px_to_m(pos); }); // The zoom factor is changed with the mouse wheel. e.mouse_scroll(|[_, y]| { let scale_changed = if y < 0. && transform.try_zoom_in() { true } else if y > 0. && transform.try_zoom_out() { true } else { false }; // Notify the user of the change if there was any if scale_changed { println!( "Changed scale to {} pixels per m.", transform.pixels_per_m() ); } }); // Handle drawing a rectangle or finishing the rectangle when clicking with the mouse. if let Some(Button::Mouse(MouseButton::Left)) = e.press_args() { if let Some(first_point) = starting_rect_point { rectangles.push(bounding_box(first_point, mouse_pos_m)); starting_rect_point = None; } else { starting_rect_point = Some(mouse_pos_m); } } // Abort drawing a rectangle when clicking with the right mouse button if let Some(Button::Mouse(MouseButton::Right)) = e.press_args() { starting_rect_point = None; } // Close the window when the user presses escape if let Some(Button::Keyboard(Key::Escape)) = e.press_args() { window.set_should_close(true); } /* Update the Grid draw size to the actual window draw size. * TODO: Currently, the window canvas draw size is never updated. This has to be changed in * order to deal with the user resizing the window. */ let win_size = window.draw_size(); let grid = Grid { cols: (win_size.width / transform.pixels_per_m()) as u32 + 1, rows: (win_size.height / transform.pixels_per_m()) as u32 + 1, units: transform.pixels_per_m(), }; window.draw_2d(&e, |c, g, _device| { clear([0.4, 0.2, 0., 1.], g); grid.draw(&grid_line, &c.draw_state, c.transform, g); // Draw all rectangles that are part of the map for &rect in &rectangles { render_rect.draw(transform.rect_m_to_px(rect), &c.draw_state, c.transform, g); } // Draw the current rectangle that is being drawn, but not part of the map if let Some(starting_rect_point) = starting_rect_point { render_rect.draw( transform.rect_m_to_px(bounding_box(starting_rect_point, mouse_pos_m)), &c.draw_state, c.transform, g, ); } }); } }