From 12e7958886433c6124ba98580b55d79067d53fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= Date: Mon, 15 Nov 2021 17:27:08 +0100 Subject: [PATCH] mainwindow: can now draw a vector of rectangles. --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/mainwindow.rs | 37 +++++++++++++++++++++++++------------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 033269f..6868766 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,6 +292,7 @@ dependencies = [ name = "gst_pipeline_studio" version = "0.1.0" dependencies = [ + "anyhow", "gtk", ] diff --git a/Cargo.toml b/Cargo.toml index c8b4c38..23d532c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -gtk = "0.14.3" \ No newline at end of file +gtk = "0.14.3" +anyhow = "1" diff --git a/src/mainwindow.rs b/src/mainwindow.rs index 8e21710..11523fd 100644 --- a/src/mainwindow.rs +++ b/src/mainwindow.rs @@ -1,5 +1,4 @@ -use gtk::ffi::GtkDrawingArea; -use gtk::gdk::Display; +use gtk::cairo::Context; use gtk::glib; use gtk::prelude::*; use gtk::{ @@ -9,6 +8,20 @@ use gtk::{ use std::cell::RefCell; use std::rc::Rc; +#[derive(Debug, Clone, Default)] +struct Element { + name: String, + position: (f64, f64), + size: (f64, f64), +} + +fn draw_elements(elements: &Vec, 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: >k::Application) { let glade_src = include_str!("gps.ui"); let builder = Builder::from_string(glade_src); @@ -25,20 +38,20 @@ pub fn build_ui(application: >k::Application) { let event_box = EventBox::new(); event_box.add(&drawing_area); view_port.add(&event_box); - let mut position = (0.0, 0.0); - let position = Rc::new(RefCell::new((0.0, 0.0))); - let p_clone = position.clone(); + let elements: Rc>> = Rc::new(RefCell::new(vec![])); + let e_clone = elements.clone(); drawing_area.connect_draw(move |w, c| { - println!("w: {} c:{} p: {:?}", w, c, p_clone); - c.rectangle(p_clone.borrow().0, p_clone.borrow().1, 10.0, 20.0); - c.fill(); + println!("w: {} c:{} e: {:?}", w, c, e_clone); + draw_elements(&e_clone.borrow().to_vec(), c); gtk::Inhibit(false) }); - event_box.connect_button_release_event(move |w, evt| { - let mut p = position.borrow_mut(); - p.0 = evt.position().0; // = evt.position().clone(); - p.1 = evt.position().1; + event_box.connect_button_release_event(move |_w, evt| { + let mut elements = elements.borrow_mut(); + let mut element: Element = Default::default(); + element.position.0 = evt.position().0; + element.position.1 = evt.position().1; + elements.push(element); drawing_area.queue_draw(); gtk::Inhibit(false) });