changeset 2:39f87ef69b2a

Add support for selecting tools.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 09 Oct 2020 16:04:59 +0200
parents 6dbe2bbeef70
children 72e63d6a3f8a
files src/main.rs
diffstat 1 files changed, 26 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.rs
+++ b/src/main.rs
@@ -179,12 +179,16 @@ fn input_axis_new(code: AbsoluteAxis, va
     input_event_new(EventKind::Absolute, code as u16, value)
 }
 
+fn input_key_new(code: Key, value: i32) -> input_event {
+    input_event_new(EventKind::Key, code as u16, value)
+}
+
 fn input_misc_new(code: MiscKind, value: i32) -> input_event {
-    input_event_new(EventKind::Absolute, code as u16, value)
+    input_event_new(EventKind::Misc, code as u16, value)
 }
 
 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event {
-    input_event_new(EventKind::Absolute, code as u16, value)
+    input_event_new(EventKind::Synchronize, code as u16, value)
 }
 
 fn build_ui(application: &gtk::Application) {
@@ -219,7 +223,7 @@ fn build_ui(application: &gtk::Applicati
     let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
     let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
 
-    let tool_name = Arc::new(Mutex::new(String::from("Pen")));
+    let current_tool = Arc::new(Mutex::new(Key::ButtonToolPen));
     let tool1 = gtk::Button::with_label("Pen");
     let tool2 = gtk::Button::with_label("Rubber");
     let tool3 = gtk::Button::with_label("Brush");
@@ -228,11 +232,18 @@ fn build_ui(application: &gtk::Applicati
 
     macro_rules! impl_tool_signal {
         ($tool:ident) => {
-            let tool_name_weak = Arc::downgrade(&tool_name);
+            let current_tool_weak = Arc::downgrade(&current_tool);
             $tool.connect_clicked(move |b| {
-                let tool_name = tool_name_weak.upgrade().unwrap();
-                let mut tool_name = tool_name.lock().unwrap();
-                *tool_name = b.get_label().unwrap().to_string();
+                let current_tool = current_tool_weak.upgrade().unwrap();
+                let mut current_tool = current_tool.lock().unwrap();
+                *current_tool = match b.get_label().unwrap().as_str() {
+                    "Pen" => Key::ButtonToolPen,
+                    "Rubber" => Key::ButtonToolRubber,
+                    "Brush" => Key::ButtonToolBrush,
+                    "Pencil" => Key::ButtonToolPencil,
+                    "Airbrush" => Key::ButtonToolAirbrush,
+                    _ => unreachable!(),
+                };
             });
         };
     };
@@ -270,6 +281,7 @@ fn build_ui(application: &gtk::Applicati
     });
     let dev_weak = Arc::downgrade(&dev);
     let current_size_weak = Arc::downgrade(&current_size);
+    let current_tool_weak = Arc::downgrade(&current_tool);
     let pressed_weak = Arc::downgrade(&pressed);
     drawing_area.connect_button_press_event(move |_, event| {
         if event.get_button() != 1 {
@@ -277,11 +289,12 @@ fn build_ui(application: &gtk::Applicati
         }
 
         let dev = dev_weak.upgrade().unwrap();
+        let current_tool = current_tool_weak.upgrade().unwrap();
         let pressed = pressed_weak.upgrade().unwrap();
         let mut pressed = pressed.lock().unwrap();
         *pressed = true;
         let (x, y) = event.get_position();
-        println!("press tool {} at {}, {}", tool_name.lock().unwrap(), x, y);
+        //println!("press tool {} at {}, {}", current_tool.lock().unwrap(), x, y);
         let current_size = current_size_weak.upgrade().unwrap();
         let current_size = current_size.lock().unwrap();
         let (width, height) = *current_size;
@@ -295,6 +308,7 @@ fn build_ui(application: &gtk::Applicati
             input_axis_new(AbsoluteAxis::TiltX, 16),
             input_axis_new(AbsoluteAxis::TiltY, 0),
             input_misc_new(MiscKind::Serial, 0),
+            input_key_new(*current_tool.lock().unwrap(), 0),
             input_synchronize_new(SynchronizeKind::Report, 0),
         ])
         .unwrap();
@@ -302,6 +316,7 @@ fn build_ui(application: &gtk::Applicati
     });
     let dev_weak = Arc::downgrade(&dev);
     let current_size_weak = Arc::downgrade(&current_size);
+    let current_tool_weak = Arc::downgrade(&current_tool);
     let pressed_weak = Arc::downgrade(&pressed);
     drawing_area.connect_button_release_event(move |_, event| {
         if event.get_button() != 1 {
@@ -309,6 +324,7 @@ fn build_ui(application: &gtk::Applicati
         }
 
         let dev = dev_weak.upgrade().unwrap();
+        let current_tool = current_tool_weak.upgrade().unwrap();
         let (x, y) = event.get_position();
         let pressed = pressed_weak.upgrade().unwrap();
         let mut pressed = pressed.lock().unwrap();
@@ -327,6 +343,7 @@ fn build_ui(application: &gtk::Applicati
             input_axis_new(AbsoluteAxis::TiltX, 16),
             input_axis_new(AbsoluteAxis::TiltY, 0),
             input_misc_new(MiscKind::Serial, 0),
+            input_key_new(*current_tool.lock().unwrap(), 0),
             input_synchronize_new(SynchronizeKind::Report, 0),
         ])
         .unwrap();
@@ -348,6 +365,7 @@ fn build_ui(application: &gtk::Applicati
             input_axis_new(AbsoluteAxis::TiltX, 16),
             input_axis_new(AbsoluteAxis::TiltY, 0),
             input_misc_new(MiscKind::Serial, 0),
+            input_key_new(*current_tool.lock().unwrap(), 0),
             input_synchronize_new(SynchronizeKind::Report, 0),
         ])
         .unwrap();