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 gtk::prelude::*;
use gio::prelude::*; use gio::prelude::*;
use gtk::{Application, ApplicationWindow, DrawingArea, Box, Button, HeaderBar}; use gtk::{Application, ApplicationWindow, DrawingArea, Box, Button, CheckButton, HeaderBar};
struct AppState { struct AppState {
draw_lines: bool,
do_draw: bool, do_draw: bool,
point_buffer: Vec<(f64, f64)>, point_buffer: Vec<(f64, f64)>,
} }
@ -20,6 +21,7 @@ fn main() {
application.connect_activate(|app| { application.connect_activate(|app| {
let app_state = Rc::new(RefCell::new(AppState{ let app_state = Rc::new(RefCell::new(AppState{
draw_lines: false,
do_draw: false, do_draw: false,
point_buffer: Vec::new(), point_buffer: Vec::new(),
})); }));
@ -40,18 +42,27 @@ fn main() {
wa.height as f64 wa.height as f64
); );
let state = &*state_clone_1.borrow(); let state = state_clone_1.borrow();
if state.point_buffer.len() > 0 { 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_source_rgb(1.0, 0.0, 0.0);
cr.set_line_width(3.); if state.draw_lines {
cr.stroke(); 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()); 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 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(); let header = HeaderBar::new();
header.pack_start(&save_button); header.pack_start(&save_button);
header.pack_start(&clear_button); header.pack_start(&clear_button);
header.pack_start(&check_button);
let w_box = Box::new(gtk::Orientation::Vertical, 10); let w_box = Box::new(gtk::Orientation::Vertical, 10);
w_box.pack_start(&header, false, false, 0); w_box.pack_start(&header, false, false, 0);
@ -83,6 +96,14 @@ fn main() {
win_clone.queue_draw(); 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); let state_clone_2 = Rc::clone(&app_state);
window.connect_event(move |widget, event| { window.connect_event(move |widget, event| {
let mut capture_event = false; let mut capture_event = false;
@ -96,7 +117,7 @@ fn main() {
std::println!("pen in proximity"); std::println!("pen in proximity");
}, },
gdk::EventType::MotionNotify => { gdk::EventType::MotionNotify => {
let state = &mut *state_clone_2.borrow_mut(); let mut state = state_clone_2.borrow_mut();
if state.do_draw { if state.do_draw {
state.point_buffer.push(event.get_coords().unwrap()); state.point_buffer.push(event.get_coords().unwrap());
widget.queue_draw(); widget.queue_draw();
@ -120,6 +141,7 @@ fn main() {
//std::println!("{:#?}\n", event.get_device_tool().map(|tool| tool.get_tool_type())); //std::println!("{:#?}\n", event.get_device_tool().map(|tool| tool.get_tool_type()));
}); });
window.add(&w_box); window.add(&w_box);
window.show_all(); window.show_all();
}); });