pub mod button; pub mod config; pub mod editor; pub mod grid; pub mod map_data; pub mod math; pub mod svg; pub mod tool; pub mod transform; use config::Config; use editor::Editor; use raylib::prelude::*; use std::io; use transform::Transform; pub const CONFIG_FILE: &str = "config.ron"; fn main() { let (mut rl, thread) = raylib::init().resizable().title("Hello there!").build(); rl.set_target_fps(120); rl.set_exit_key(None); // Load the configuration file, if available. let config = match Config::from_file(CONFIG_FILE) { Ok(config) => config, Err(err) => { /* Create a default config file if it doesn't exist, otherwise leave the incorrectly * formatted/corrupted or otherwise unreadable file alone. */ let config = Config::default(); if err.kind() == io::ErrorKind::NotFound { println!("Could not find a configuration file. Creating default."); config .write_file(CONFIG_FILE) .expect("Could not write config file."); } else { println!("Could not read configuration file: {}", err); println!("Using defaults for this run."); } config } }; let mut editor = Editor::new(&mut rl, &thread, config); let mut transform = Transform::new(); let mut last_mouse_pos = rl.get_mouse_position(); while !rl.window_should_close() { let screen_width = rl.get_screen_width(); let screen_height = rl.get_screen_height(); // Move the canvas together with the mouse if rl.is_mouse_button_down(MouseButton::MOUSE_MIDDLE_BUTTON) { transform.move_by_px((rl.get_mouse_position() - last_mouse_pos).into()); } let mouse_wheel_move = rl.get_mouse_wheel_move(); if mouse_wheel_move != 0 { // Zoom in for positive and zoom out for negative mouse wheel rotations. let scale_factor = if mouse_wheel_move > 0 { 1.2 } else { 1. / 1.2 }; transform.try_zoom( rl.get_mouse_position().into(), mouse_wheel_move.abs() as f32 * scale_factor, ); } editor.update(&rl, &transform); // Update the last mouse position last_mouse_pos = rl.get_mouse_position(); // Drawing section { let mut d = rl.begin_drawing(&thread); d.clear_background(Color::BLACK); grid::draw_grid(&mut d, screen_width, screen_height, &transform); editor.draw_tools(&mut d, &transform); } } }