refactoring

This commit is contained in:
Simon Gardling
2022-02-16 09:07:58 -05:00
parent f83994a2fa
commit ec7d2e4bb1
2 changed files with 36 additions and 29 deletions

30
src/misc.rs Normal file
View File

@@ -0,0 +1,30 @@
use wasm_bindgen::prelude::*;
pub type DrawResult<T> = Result<T, Box<dyn std::error::Error>>;
/// Result of screen to chart coordinates conversion.
#[wasm_bindgen]
pub struct Point {
pub x: f32,
pub y: f32,
}
#[wasm_bindgen]
impl Point {
pub fn new(x: f32, y: f32) -> Self { Self { x, y } }
}
#[wasm_bindgen]
pub struct Chart {
pub(crate) convert: Box<dyn Fn((i32, i32)) -> Option<(f32, f32)>>,
pub(crate) area: f32,
}
#[wasm_bindgen]
impl Chart {
pub fn get_area(&self) -> f32 { self.area }
pub fn coord(&self, x: i32, y: i32) -> Option<Point> {
(self.convert)((x, y)).map(|(x, y)| Point::new(x, y))
}
}