diff --git a/.travis.yml b/.travis.yml index a861c8d..d9f426d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ install: - (cd SDL2-2.0.5 && ./configure && make && sudo make install) script: - cargo build -v - - cargo test --no-run \ No newline at end of file + - cargo test --no-run --features="use-sdl2 use-image" \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 23d6aa3..ea564c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ name = "dataplotlib" version = "0.1.3" [features] -default = ["use-sdl2"] +default = ["use-image"] use-sdl2 = ["sdl2_mt"] use-image = ["image"] diff --git a/src/draw_image.rs b/src/draw_image.rs new file mode 100644 index 0000000..d39c55b --- /dev/null +++ b/src/draw_image.rs @@ -0,0 +1,144 @@ +use image::{self, ImageBuffer}; +use draw::*; + + +pub struct DrawImage { + context: ImageBuffer, + screenspace: Range2d, + realspace: Range2d, + color: [u8; 4], +} + +impl DrawImage { + pub fn new() -> Box { + let default_s = Range { min: 0.0, max: 0.0 }; + + let default_r = Range { + min: 0.0, + max: 720.0, + }; + + Box::new(DrawImage { + screenspace: Range2d(default_s, default_s), + realspace: Range2d(default_r, default_r), + color: [0, 0, 0, 255], + }) + } +} + +impl Drawable for DrawImage { + /// Sets the visible range of worldspace + fn set_view(&mut self, view: Range2d) { + self.screenspace = view; + } + + /// Gets the visible range of worldspace + fn get_view(&self) -> Range2d { + self.screenspace + } + + /// Set color for various drawing actions + fn set_color(&mut self, color: [u8; 4]) { + self.color = color; + } + + /// Clears the output surface + fn clear(&mut self) { + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas.clear(); +// })) +// .unwrap(); + } + + /// Draws a line from (x, y) -> (x, y) in worldspace + fn line(&mut self, p1: (f64, f64), p2: (f64, f64)) { + self.thick_line(p1, p2, 1); + } + + /// Draws a line from (x, y) -> (x, y) in worldspace + fn thick_line(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64), thickness: u16) { + + let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false); + let y1 = point2window(y1, self.screenspace.1, self.realspace.1, true); + + let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false); + let y2 = point2window(y2, self.screenspace.1, self.realspace.1, true); + + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas +// .draw_line((x1 as i32, y1 as i32), (x2 as i32, y2 as i32)) +// .unwrap(); +// })) +// .unwrap(); + } + + /// Draws a rectangle bounded by two corners + fn rectangle(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64)) { + + let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false); + let y1 = point2window(y1, self.screenspace.1, self.realspace.1, false); + + let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false); + let y2 = point2window(y2, self.screenspace.1, self.realspace.1, false); + + let x1 = x1 as i32; + let y1 = y1 as i32; + let w = (x2 as i32 - x1) as u32; + let h = (y2 as i32 - y1) as u32; + + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas.fill_rect(Rect::new(x1, y1, w, h)).unwrap(); +// })) +// .unwrap(); + } + + /// Draws a rectangle bounded by two corners + fn unfilled_rectangle(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64)) { + + let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false); + let y1 = point2window(y1, self.screenspace.1, self.realspace.1, false); + + let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false); + let y2 = point2window(y2, self.screenspace.1, self.realspace.1, false); + + let x1 = x1 as i32; + let y1 = y1 as i32; + let w = (x2 as i32 - x1) as u32; + let h = (y2 as i32 - y1) as u32; + + let color = self.color; +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.set_draw_color(color); +// canvas.draw_rect(Rect::new(x1, y1, w, h)).unwrap(); +// })) +// .unwrap(); + } + + fn present(&mut self) { +// self.sdlh +// .run_on_ui_thread(Box::new(move |_sdl, windows| { +// let canvas = windows.get_mut(&window_id).unwrap(); +// canvas.present(); +// })) +// .unwrap(); + } + + /// Returns the next pending event + fn get_events(&mut self) -> Vec { + vec![Event::Quit] + } +} diff --git a/src/lib.rs b/src/lib.rs index 96b380c..ef9d741 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,9 @@ //! let sdlh = dataplotlib::sdl2_init(); //! let sdl2_window = DrawSDL::new(sdlh); //! +//! let sdlh = dataplotlib::sdl2_init(); +//! let sdl2_window = DrawSDL::new(sdlh); +//! //! let mut plt = Plotter::new(); //! plt.plot2d(pb, sdl2_window); //! # plt.disown(); // make sure the doc test doesn't last forever @@ -42,6 +45,8 @@ pub mod draw_sdl; #[cfg(feature = "use-image")] extern crate image; +#[cfg(feature = "use-image")] +pub mod draw_image; pub mod draw; mod plot; diff --git a/src/plotter.rs b/src/plotter.rs index b285e84..9f5a9fc 100644 --- a/src/plotter.rs +++ b/src/plotter.rs @@ -55,6 +55,7 @@ impl Drop for Plotter { } #[cfg(test)] +#[cfg(feature="use-sdl2")] mod test { use super::*; use plotbuilder::*;