aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
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 /src/app.rs
parentc84c38544bc6e81f0b0482e4e82b6c95848c1a0c (diff)
use floating angles
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs50
1 files changed, 11 insertions, 39 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();