mainwindow: can now draw a vector of rectangles.

This commit is contained in:
Stéphane Cerveau 2021-11-15 17:27:08 +01:00
parent 52718e0388
commit 12e7958886
3 changed files with 28 additions and 13 deletions

1
Cargo.lock generated
View file

@ -292,6 +292,7 @@ dependencies = [
name = "gst_pipeline_studio" name = "gst_pipeline_studio"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"gtk", "gtk",
] ]

View file

@ -6,4 +6,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
gtk = "0.14.3" gtk = "0.14.3"
anyhow = "1"

View file

@ -1,5 +1,4 @@
use gtk::ffi::GtkDrawingArea; use gtk::cairo::Context;
use gtk::gdk::Display;
use gtk::glib; use gtk::glib;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{ use gtk::{
@ -9,6 +8,20 @@ use gtk::{
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug, Clone, Default)]
struct Element {
name: String,
position: (f64, f64),
size: (f64, f64),
}
fn draw_elements(elements: &Vec<Element>, c: &Context) {
for element in elements {
c.rectangle(element.position.0, element.position.1, 80.0, 45.0);
c.fill().expect("Can not draw into context");
}
}
pub fn build_ui(application: &gtk::Application) { pub fn build_ui(application: &gtk::Application) {
let glade_src = include_str!("gps.ui"); let glade_src = include_str!("gps.ui");
let builder = Builder::from_string(glade_src); let builder = Builder::from_string(glade_src);
@ -25,20 +38,20 @@ pub fn build_ui(application: &gtk::Application) {
let event_box = EventBox::new(); let event_box = EventBox::new();
event_box.add(&drawing_area); event_box.add(&drawing_area);
view_port.add(&event_box); view_port.add(&event_box);
let mut position = (0.0, 0.0); let elements: Rc<RefCell<Vec<Element>>> = Rc::new(RefCell::new(vec![]));
let position = Rc::new(RefCell::new((0.0, 0.0))); let e_clone = elements.clone();
let p_clone = position.clone();
drawing_area.connect_draw(move |w, c| { drawing_area.connect_draw(move |w, c| {
println!("w: {} c:{} p: {:?}", w, c, p_clone); println!("w: {} c:{} e: {:?}", w, c, e_clone);
c.rectangle(p_clone.borrow().0, p_clone.borrow().1, 10.0, 20.0); draw_elements(&e_clone.borrow().to_vec(), c);
c.fill();
gtk::Inhibit(false) gtk::Inhibit(false)
}); });
event_box.connect_button_release_event(move |w, evt| { event_box.connect_button_release_event(move |_w, evt| {
let mut p = position.borrow_mut(); let mut elements = elements.borrow_mut();
p.0 = evt.position().0; // = evt.position().clone(); let mut element: Element = Default::default();
p.1 = evt.position().1; element.position.0 = evt.position().0;
element.position.1 = evt.position().1;
elements.push(element);
drawing_area.queue_draw(); drawing_area.queue_draw();
gtk::Inhibit(false) gtk::Inhibit(false)
}); });