1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//!A an attempt to port Tk's Canvas, written in Rust and SFML, aiming to provide simple and Python-like syntax.
//!
//! # Example:
//!
//! ```no_run
//! use canrust::canvas::{Canvas, Color}; fn main() { // Create a canvas:
//!     // size, name, background color
//!     let mut canvas = Canvas::new((800, 600), "Canvas", Color::WHITE);
//!     
//!     // create shapes:
//!     canvas.create_circle((1., 1.), 20., Color::BLACK, None, None);
//!     canvas.create_rectangle((20., 20.), (30., 80.), Color::BLACK, None, None);
//!     canvas.create_line((18., 20.), (18., 80.), 2., Color::RED, None, None);
//!     canvas.create_line((32., 20.), (32. 80.), 2., Color::RED, None, None);
//!     
//!     // call mainloop, objects will be rendered as long as the program is running
//!     canvas.mainloop()
//! }
//! ```
//!
//! # Dependencies
//! - SFML 2.6
//! - CSFML 2.6
//!
//! # Installation
//!
//! Add this to your Cargo.toml file:
//!
//! ```toml
//! [dependencies]
//! canrust = "1.3.1"
//! ```

#![feature(try_trait)]

pub mod canvas;
pub mod utils;

#[cfg(test)]
fn test_all() -> Result<(), utils::Error> {
    use canvas::{Color, Line, Text};
    use utils::Image;

    let mut canvas = Canvas::new((800, 800), "ha".to_string(), Color::RED);
    let _ = canvas.create_from_text(&Text::new("From Text", 20, Color::WHITE, (400., 500.)));
    canvas.create_circle((15., 18.), 200., Color::BLUE, None, None);
    canvas.create_rectangle((1., 1.), (200., 200.), Color::CYAN, None, None);
    canvas.create_text((500., 500.), "Hello", 50, "Arial", Color::WHITE);

    let line = Line::new((200., 200.), (400., 400.), 12., Color::WHITE);

    canvas.create_from_line(&line);

    let image = Image::from_file("~/programming/rust/canrust/example.png", (80., 300.))?;
    canvas.create_from_image(&image);
    canvas.mainloop();
    Ok(())
}

#[cfg(test)]
fn test_fonts() {
    use canvas::{Color, Text};

    let mut canvas = Canvas::new((800, 600), "ha".to_string(), Color::WHITE);

    let mut text = Text::new("This is a font test ha", 50, Color::RED, (1., 1.));
    text.set_font("sourcecodepro".to_string()).unwrap();
    canvas.create_from_text(&text);
    canvas.mainloop()
}
#[cfg(test)]
fn bind_test() {
    use canvas::Color;

    let mut canvas = Canvas::new((800, 800), "ha".to_string(), Color::WHITE);
    canvas.create_circle((1., 1.), 20., Color::BLACK, None, None);
    canvas.create_rectangle((20., 20.), (30., 80.), Color::BLACK, None, None);
    canvas.create_line((18., 20.), (18., 80.), 2., Color::RED, None, None);
    canvas.create_line((32., 20.), (32., 80.), 2., Color::RED, None, None);

    canvas.mainloop();
}
use canvas::Canvas;
#[cfg(test)]
fn hello(can: &mut Canvas) {
    use canvas::Color;
    println!("hello from hello");
    for i in 0..5 {
        let ha = i as f32 * 20.;
        can.create_circle((56. + ha, 64. + ha), 10., Color::RED, None, None);
    }
}
#[cfg(test)]
#[test]
fn mouse_test() {
    use canvas::{Button, Canvas, Color};
    let mut canvas = Canvas::new((800, 800), "ha".to_string(), Color::BLACK);

    let mut button = Button::new("hello", (300., 300.), (200., 100.));
    button.bind(mouse);
    button.set_fill_color(Color::RED);
    button.set_style("circle").unwrap();

    canvas.create_from_button(&button);

    canvas.mainloop()
}
#[cfg(test)]
fn mouse(can: &mut Canvas, _: (f32, f32)) {
    use canvas::Color;
    use canvas::Retrieve;
    let _x = match can.retrieve(1).unwrap() {
        Retrieve::Button(b) => {
            if b.get_fill_color().unwrap() == Color::RED {
                b.set_fill_color(Color::BLUE)
            } else {
                b.set_fill_color(Color::RED)
            }
        }
        _ => (),
    };
}