Skip to content
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ license = "MIT"
name = "dataplotlib"
version = "0.1.3"

[features]
default = ["use-sdl2"]
use-sdl2 = ["sdl2_mt"]
use-image = ["image"]

[dependencies]

[dependencies.sdl2]
features = ["gfx", "image"]
version = "0.27.2"
[dependencies.image]
version = "0.15.0"
optional = true

[dependencies.sdl2_mt]
version = "0.1.0"
optional = true

[lib]
name = "dataplotlib"
Expand Down
7 changes: 5 additions & 2 deletions examples/coloredxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate dataplotlib;
use dataplotlib::util::{linspace, zip2};
use dataplotlib::plotbuilder::PlotBuilder2D;
use dataplotlib::plotter::Plotter;
use dataplotlib::draw_sdl::DrawSDL;

fn main() {
let x = linspace(0, 10, 100);
Expand All @@ -18,7 +19,9 @@ fn main() {
pb.add_color_xy(xy_sin, [1.0, 0.0, 0.0, 1.0]);
pb.add_color_xy(xy_lin, [0.0, 0.0, 1.0, 1.0]);

let sdlh = dataplotlib::sdl2_init();
let sdl2_window = DrawSDL::new(sdlh);

let mut plt = Plotter::new();
plt.plot2d(pb);
plt.join();
plt.plot2d(pb, sdl2_window);
}
7 changes: 5 additions & 2 deletions examples/simplexy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate dataplotlib;
use dataplotlib::util::{linspace, zip2};
use dataplotlib::plotbuilder::PlotBuilder2D;
use dataplotlib::plotter::Plotter;
use dataplotlib::draw_sdl::DrawSDL;

fn main() {
let x = linspace(0, 10, 100);
Expand All @@ -18,7 +19,9 @@ fn main() {
pb.add_simple_xy(xy_sin);
pb.add_simple_xy(xy_lin);

let sdlh = dataplotlib::sdl2_init();
let sdl2_window = DrawSDL::new(sdlh);

let mut plt = Plotter::new();
plt.plot2d(pb);
plt.join();
plt.plot2d(pb, sdl2_window);
}
60 changes: 53 additions & 7 deletions src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
#![allow(dead_code)]

#[derive(Copy, Clone, Debug)]
pub enum MouseButton {
Left,
Middle,
Right,
}

#[derive(Copy, Clone, Debug)]
pub enum Event {
Quit,
Resize(f64, f64),
KeyDown(i32),
KeyUp(i32),
MouseDown(Option<MouseButton>, f64, f64),
MouseUp(Option<MouseButton>, f64, f64),
MouseMove(Option<MouseButton>, f64, f64),
MouseDown(MouseButton, f64, f64),
MouseUp(MouseButton, f64, f64),
MouseMove(MouseButton, f64, f64),
MouseScroll(i32, i32),
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub struct Range {
pub min: f64,
pub max: f64,
}

pub trait Drawable {
impl Range {
pub fn size(&self) -> f64 {
self.max - self.min
}

pub fn contains(&self, pt: f64) -> bool {
pt <= self.max && pt >= self.min
}
}

#[derive(Copy, Clone, Debug)]
pub struct Range2d(pub Range, pub Range);

impl Range2d {
pub fn contains(&self, pt: (f64, f64)) -> bool {
self.0.contains(pt.0) && self.1.contains(pt.1)
}
}

impl From<(Range, Range)> for Range2d {
fn from((a, b): (Range, Range)) -> Self {
Range2d(a, b)
}
}

pub trait Drawable: Send {
/// Sets the visible range of worldspace
fn set_view(&mut self, x: Range, y: Range);
fn set_view(&mut self, view: Range2d);

/// Gets the visible range of worldspace
fn get_view(&self) -> Range2d;

/// Set color for various drawing actions
fn set_color(&mut self, color: [u8; 4]);
Expand All @@ -35,12 +66,27 @@ pub trait Drawable {
/// Draws a line from (x, y) -> (x, y) in worldspace
fn line(&mut self, a: (f64, f64), b: (f64, f64));

/// Draws a line from (x, y) -> (x, y) in worldspace
fn thick_line(&mut self, a: (f64, f64), b: (f64, f64), thickness: u16);

/// Draws a rectangle bounded by two corners
fn rectangle(&mut self, a: (f64, f64), b: (f64, f64));

/// Returns the next pending event
/// Draws a rectangle bounded by two corners
fn unfilled_rectangle(&mut self, a: (f64, f64), b: (f64, f64));

/// Presents the previously drawn buffer
fn present(&mut self);

/// Returns the next pending events
fn get_events(&mut self) -> Vec<Event>;

/// Asks that the Drawable stop any tasks and cleanup
fn close(&mut self) {} // provide empty default impl
}

pub fn point2window(pt: f64, view: Range, window: Range, invert: bool) -> f64 {
let moved_pt = if invert { view.max - pt } else { pt - view.min };

(moved_pt / view.size()) * (window.size() + window.min)
}
Loading