aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-07 14:37:33 +0100
committerAkshay <[email protected]>2021-04-07 14:37:33 +0100
commitf57f03cd87d205d8304008d08c05f53c34b44435 (patch)
treee1a1da435a4ae8a21209c304d15130d254be7c97
parentc84c38544bc6e81f0b0482e4e82b6c95848c1a0c (diff)
use floating angles
-rw-r--r--src/app.rs50
-rw-r--r--src/bitmap.rs6
-rw-r--r--src/consts.rs1
-rw-r--r--src/main.rs1
4 files changed, 16 insertions, 42 deletions
diff --git a/src/app.rs b/src/app.rs
index f42b48c..e8c5260 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -3,9 +3,10 @@ use crate::{
3 brush::{Brush, CircleBrush, LineBrush}, 3 brush::{Brush, CircleBrush, LineBrush},
4 cache::Cache, 4 cache::Cache,
5 command::CommandBox, 5 command::CommandBox,
6 consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH}, 6 consts::{colors::*, ANGLE, FONT_PATH, RC_PATH, STDLIB_PATH},
7 dither, 7 dither,
8 error::AppError, 8 error::AppError,
9 grid::Grid,
9 guide::Guide, 10 guide::Guide,
10 lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, 11 lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList},
11 message::Message, 12 message::Message,
@@ -68,20 +69,6 @@ pub struct AppState<'ctx> {
68 pub zoom: u8, 69 pub zoom: u8,
69} 70}
70 71
71pub struct Grid {
72 pub enabled: bool,
73 pub color: Color,
74}
75
76impl Grid {
77 fn new() -> Self {
78 Self {
79 enabled: true,
80 color: GRID_COLOR,
81 }
82 }
83}
84
85// private actions on appstate 72// private actions on appstate
86impl<'ctx> AppState<'ctx> { 73impl<'ctx> AppState<'ctx> {
87 fn pan<P: Into<Point>>(&mut self, direction: P) { 74 fn pan<P: Into<Point>>(&mut self, direction: P) {
@@ -331,27 +318,6 @@ impl<'ctx> AppState<'ctx> {
331 self.mode = Mode::Draw; 318 self.mode = Mode::Draw;
332 } 319 }
333 320
334 fn draw_grid(&mut self) {
335 let cs = self.zoom as u32;
336 let (width, height) = (self.width(), self.height());
337 let canvas = &mut self.canvas;
338 canvas.set_draw_color(self.grid.color);
339 for i in 1..width {
340 let x = (i * cs) as i32;
341 let y = (height * cs) as i32;
342 let start = self.start + Point::new(x, 0);
343 let end = self.start + Point::new(x, y);
344 canvas.draw_line(start, end).unwrap();
345 }
346 for j in 1..height {
347 let x = (width * cs) as i32;
348 let y = (j * cs) as i32;
349 let start = self.start + Point::new(0, y);
350 let end = self.start + Point::new(x, y);
351 canvas.draw_line(start, end).unwrap();
352 }
353 }
354
355 fn draw_statusline(&mut self) { 321 fn draw_statusline(&mut self) {
356 let container = Container::new(Offset::Left(0), Offset::Bottom(40), &self.canvas) 322 let container = Container::new(Offset::Left(0), Offset::Bottom(40), &self.canvas)
357 .width(Size::Max, &self.canvas) 323 .width(Size::Max, &self.canvas)
@@ -482,10 +448,15 @@ impl<'ctx> AppState<'ctx> {
482 let size = self.zoom as u32 * (size as u32 + 5); 448 let size = self.zoom as u32 * (size as u32 + 5);
483 if let (Some(from), Some(to)) = (start, pt) { 449 if let (Some(from), Some(to)) = (start, pt) {
484 let line = self.pixmap.get_line(from, to.into()); 450 let line = self.pixmap.get_line(from, to.into());
451 let angle = positive_angle_with_x(from, to.into());
485 draw_text( 452 draw_text(
486 &mut self.canvas, 453 &mut self.canvas,
487 self.ttf_context, 454 self.ttf_context,
488 format!("{}°", positive_angle_with_x(from, to.into())), 455 format!(
456 "{:.width$}°",
457 angle,
458 width = if (angle - ANGLE).abs() < 1e-3 { 3 } else { 0 }
459 ),
489 PINK, 460 PINK,
490 (self.mouse.0 + size as i32, self.mouse.1 + size as i32), 461 (self.mouse.0 + size as i32, self.mouse.1 + size as i32),
491 ); 462 );
@@ -546,7 +517,7 @@ impl<'ctx> AppState<'ctx> {
546 517
547 fn draw(&mut self) { 518 fn draw(&mut self) {
548 let cs = self.zoom as u32; 519 let cs = self.zoom as u32;
549 let (width, _) = (self.width(), self.height()); 520 let (width, height) = (self.width(), self.height());
550 let start = self.start; 521 let start = self.start;
551 let grid_enabled = self.grid.enabled; 522 let grid_enabled = self.grid.enabled;
552 self.canvas.set_draw_color(WHITE); 523 self.canvas.set_draw_color(WHITE);
@@ -576,7 +547,8 @@ impl<'ctx> AppState<'ctx> {
576 )) 547 ))
577 .unwrap(); 548 .unwrap();
578 if grid_enabled { 549 if grid_enabled {
579 self.draw_grid(); 550 self.grid
551 .draw(&mut self.canvas, self.zoom, &self.start, width, height);
580 } 552 }
581 self.draw_guides(); 553 self.draw_guides();
582 self.draw_symmetry(); 554 self.draw_symmetry();
diff --git a/src/bitmap.rs b/src/bitmap.rs
index 2f96a99..ba5c8f1 100644
--- a/src/bitmap.rs
+++ b/src/bitmap.rs
@@ -325,11 +325,11 @@ pub fn reflect_figure(figure: &[MapPoint], around: MapPoint) -> Vec<MapPoint> {
325 figure.iter().map(|pt| pt.reflect(around)).collect() 325 figure.iter().map(|pt| pt.reflect(around)).collect()
326} 326}
327 327
328pub fn positive_angle_with_x(start: MapPoint, end: MapPoint) -> u32 { 328pub fn positive_angle_with_x(start: MapPoint, end: MapPoint) -> f64 {
329 if end.x == start.x { 329 if end.x == start.x {
330 return 90; 330 return 90.;
331 } 331 }
332 let numer = (end.y as f64 - start.y as f64).abs(); 332 let numer = (end.y as f64 - start.y as f64).abs();
333 let denum = (end.x as f64 - start.x as f64).abs(); 333 let denum = (end.x as f64 - start.x as f64).abs();
334 (numer / denum).atan().to_degrees() as u32 334 (numer / denum).atan().to_degrees()
335} 335}
diff --git a/src/consts.rs b/src/consts.rs
index 0b43f0a..7087073 100644
--- a/src/consts.rs
+++ b/src/consts.rs
@@ -11,3 +11,4 @@ pub mod colors {
11pub const FONT_PATH: &str = "./assets/NerdInput-Regular.ttf"; 11pub const FONT_PATH: &str = "./assets/NerdInput-Regular.ttf";
12pub const STDLIB_PATH: &str = "./src/lisp/std.lisp"; 12pub const STDLIB_PATH: &str = "./src/lisp/std.lisp";
13pub const RC_PATH: &str = "/home/np/.config/sdl-tests"; 13pub const RC_PATH: &str = "/home/np/.config/sdl-tests";
14pub const ANGLE: f64 = 26.565;
diff --git a/src/main.rs b/src/main.rs
index 245ee83..220956a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,6 +8,7 @@ mod command;
8mod consts; 8mod consts;
9mod dither; 9mod dither;
10mod error; 10mod error;
11mod grid;
11mod guide; 12mod guide;
12mod lisp; 13mod lisp;
13mod message; 14mod message;