aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorArne Dußin2020-10-30 22:32:28 +0100
committerArne Dußin2020-10-30 22:32:28 +0100
commit48c425a193cb13012eb9303df56ac04b9d683ed4 (patch)
treedbc7fc9bd4e595bd8438d4233c08fe3775c5c690 /src/main.rs
parent20c73199167ce3ef1b4a256db5a95acab8f467b3 (diff)
downloadgraf_karto-48c425a193cb13012eb9303df56ac04b9d683ed4.tar.gz
graf_karto-48c425a193cb13012eb9303df56ac04b9d683ed4.zip
Rewrite project to use raylib instead of piston
Sorry piston.. I really tried liking you, but I just couldn't :/ It's not you, it's me. What am I saying? It's you, sorry not sorry.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs146
1 files changed, 31 insertions, 115 deletions
diff --git a/src/main.rs b/src/main.rs
index 4969261..3f36123 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,132 +1,48 @@
-pub mod infinite_grid;
+pub mod grid;
+pub mod math;
+pub mod tool;
pub mod transform;
-use infinite_grid::*;
-use transform::*;
+pub use transform::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]
-}
+use raylib::prelude::*;
+use tool::{RoomTool, Tool};
fn main() {
- let mut window: PistonWindow<Sdl2Window> = 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();
- let mut grid = InfiniteGrid::new(&transform, window.draw_size());
-
- /* 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();
-
- let mut mouse_pos_px = [0., 0.];
- let mut mouse_pos_m = [0., 0.];
- let mut canvas_follows_mouse = false;
-
- let mut events = Events::new(EventSettings::new().lazy(true));
- while let Some(e) = events.next(&mut window) {
- // Update the mouse cursor position and possibly the canvas position too, in case the user
- // is currently dragging the canvas.
- e.mouse_cursor(|new_pos| {
- if canvas_follows_mouse {
- let move_by = math::sub(new_pos, mouse_pos_px);
- transform.move_by_px(move_by);
- }
+ let (mut rl, thread) = raylib::init().resizable().title("Hello there!").build();
- mouse_pos_px = new_pos;
- mouse_pos_m = transform.point_px_to_m(new_pos);
- });
+ let mut current_tool = RoomTool::new();
- // 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 {
- grid.on_scale_change(&transform, window.draw_size());
- println!(
- "Changed scale to {} pixels per m.",
- transform.pixels_per_m()
- );
- }
- });
-
- // Handle the movement of the canvas when pressing the middle mouse button.
- if let Some(Button::Mouse(MouseButton::Middle)) = e.press_args() {
- canvas_follows_mouse = true;
- println!("Canvas now follows the mouse");
- }
- if let Some(Button::Mouse(MouseButton::Middle)) = e.release_args() {
- canvas_follows_mouse = false;
- println!("Canvas no longer follows the mouse");
- }
+ 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();
- // 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;
+ // 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);
}
- // Close the window when the user presses escape
- if let Some(Button::Keyboard(Key::Escape)) = e.press_args() {
- window.set_should_close(true);
+ // Handle scrolling of the canvas
+ if rl.get_mouse_wheel_move() > 0 {
+ transform.try_zoom_in();
+ } else if rl.get_mouse_wheel_move() < 0 {
+ transform.try_zoom_out();
}
- window.draw_2d(&e, |c, g, _device| {
- clear([0.4, 0.2, 0., 1.], g);
+ current_tool.update(&rl, &transform);
- grid.draw(&transform, &c, g);
+ // Update the last mouse position
+ last_mouse_pos = rl.get_mouse_position();
- // 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);
- }
+ // Drawing section
+ {
+ let mut d = rl.begin_drawing(&thread);
+ d.clear_background(Color::BLACK);
+ grid::draw_grid(&mut d, screen_width, screen_height, &transform);
- // 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,
- );
- }
- });
+ current_tool.draw(&mut d, &transform);
+ }
}
}