Allow to optionally draw dots instead of lines

This commit is contained in:
Enrico Lumetti 2020-05-04 18:19:40 +02:00
parent 33fdcfbfaf
commit e736eb4c60
1 changed files with 33 additions and 11 deletions

View File

@ -4,9 +4,10 @@ use std::rc::Rc;
use gtk::prelude::*;
use gio::prelude::*;
use gtk::{Application, ApplicationWindow, DrawingArea, Box, Button, HeaderBar};
use gtk::{Application, ApplicationWindow, DrawingArea, Box, Button, CheckButton, HeaderBar};
struct AppState {
draw_lines: bool,
do_draw: bool,
point_buffer: Vec<(f64, f64)>,
}
@ -20,6 +21,7 @@ fn main() {
application.connect_activate(|app| {
let app_state = Rc::new(RefCell::new(AppState{
draw_lines: false,
do_draw: false,
point_buffer: Vec::new(),
}));
@ -40,18 +42,27 @@ fn main() {
wa.height as f64
);
let state = &*state_clone_1.borrow();
let state = state_clone_1.borrow();
if state.point_buffer.len() > 0 {
let (x, y) = state.point_buffer[0];
cr.move_to(x - wa.x as f64, y - wa.y as f64);
for pt in &state.point_buffer[1..] {
cr.line_to(pt.0 - wa.x as f64, pt.1 - wa.y as f64);
}
cr.set_source_rgb(1.0, 0.0, 0.0);
cr.set_line_width(3.);
cr.stroke();
if state.draw_lines {
let (x, y) = state.point_buffer[0];
cr.move_to(x - wa.x as f64, y - wa.y as f64);
for pt in &state.point_buffer[1..] {
cr.line_to(pt.0 - wa.x as f64, pt.1 - wa.y as f64);
}
cr.set_line_width(3.);
cr.stroke();
} else {
for pt in &state.point_buffer[1..] {
use std::f64::consts::PI;
cr.arc(pt.0 - wa.x as f64, pt.1 - wa.y as f64, 3., 0., 2. * PI);
cr.fill();
}
}
}
println!("{} points drawn", state.point_buffer.len());
@ -63,10 +74,12 @@ fn main() {
//
//});
let clear_button = Button::new_from_icon_name(Some("edit-clear-all-symbolic"), gtk::IconSize::Menu);
let check_button = CheckButton::new_with_label("Join Points");
let header = HeaderBar::new();
header.pack_start(&save_button);
header.pack_start(&clear_button);
header.pack_start(&check_button);
let w_box = Box::new(gtk::Orientation::Vertical, 10);
w_box.pack_start(&header, false, false, 0);
@ -83,6 +96,14 @@ fn main() {
win_clone.queue_draw();
});
let state_clone_3 = Rc::clone(&app_state);
let win_clone_2 = Rc::clone(&window);
check_button.connect_clicked(move |_| {
let mut borrow = state_clone_3.borrow_mut();
borrow.draw_lines = ! borrow.draw_lines;
win_clone_2.queue_draw();
});
let state_clone_2 = Rc::clone(&app_state);
window.connect_event(move |widget, event| {
let mut capture_event = false;
@ -96,7 +117,7 @@ fn main() {
std::println!("pen in proximity");
},
gdk::EventType::MotionNotify => {
let state = &mut *state_clone_2.borrow_mut();
let mut state = state_clone_2.borrow_mut();
if state.do_draw {
state.point_buffer.push(event.get_coords().unwrap());
widget.queue_draw();
@ -120,6 +141,7 @@ fn main() {
//std::println!("{:#?}\n", event.get_device_tool().map(|tool| tool.get_tool_type()));
});
window.add(&w_box);
window.show_all();
});