changeset 10:06d77bb94a50

Move more state handling inside of State.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 15:50:38 +0100
parents d1972fc49a5b
children 0193041f01d4
files src/main.rs
diffstat 1 files changed, 63 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.rs
+++ b/src/main.rs
@@ -233,6 +233,63 @@ impl State {
             pressed: false,
         })))
     }
+
+    fn select_tool(&mut self, tool: Key) {
+        self.selected_tool = tool;
+    }
+
+    fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> {
+        self.pressed = true;
+        self.dev.write(&[
+            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
+            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
+            input_axis_new(AbsoluteAxis::Z, 0),
+            input_axis_new(AbsoluteAxis::Wheel, 0),
+            input_axis_new(AbsoluteAxis::Pressure, 1024),
+            input_axis_new(AbsoluteAxis::Distance, 0),
+            input_axis_new(AbsoluteAxis::TiltX, 16),
+            input_axis_new(AbsoluteAxis::TiltY, 0),
+            input_misc_new(MiscKind::Serial, 0),
+            input_key_new(self.selected_tool, 1),
+            input_synchronize_new(SynchronizeKind::Report, 0),
+        ])?;
+        Ok(())
+    }
+
+    fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> {
+        self.pressed = false;
+        self.dev.write(&[
+            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
+            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
+            input_axis_new(AbsoluteAxis::Z, 0),
+            input_axis_new(AbsoluteAxis::Wheel, 0),
+            input_axis_new(AbsoluteAxis::Pressure, 0),
+            input_axis_new(AbsoluteAxis::Distance, 16),
+            input_axis_new(AbsoluteAxis::TiltX, 16),
+            input_axis_new(AbsoluteAxis::TiltY, 0),
+            input_misc_new(MiscKind::Serial, 0),
+            input_key_new(self.selected_tool, 1),
+            input_synchronize_new(SynchronizeKind::Report, 0),
+        ])?;
+        Ok(())
+    }
+
+    fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> {
+        self.dev.write(&[
+            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
+            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
+            input_axis_new(AbsoluteAxis::Z, 0),
+            input_axis_new(AbsoluteAxis::Wheel, 0),
+            input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }),
+            input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }),
+            input_axis_new(AbsoluteAxis::TiltX, 16),
+            input_axis_new(AbsoluteAxis::TiltY, 0),
+            input_misc_new(MiscKind::Serial, 0),
+            input_key_new(self.selected_tool, 1),
+            input_synchronize_new(SynchronizeKind::Report, 0),
+        ])?;
+        Ok(())
+    }
 }
 
 fn build_main_menu(application: &gtk::Application) {
@@ -306,7 +363,7 @@ fn build_ui(application: &gtk::Applicati
             tool.connect_clicked(move |b| {
                 let state = state_weak.upgrade().unwrap();
                 let mut state = state.lock().unwrap();
-                state.selected_tool = match b.get_label().unwrap().as_str() {
+                let tool = match b.get_label().unwrap().as_str() {
                     "_Pen" => Key::ButtonToolPen,
                     "_Rubber" => Key::ButtonToolRubber,
                     "_Brush" => Key::ButtonToolBrush,
@@ -314,6 +371,7 @@ fn build_ui(application: &gtk::Applicati
                     "_Airbrush" => Key::ButtonToolAirbrush,
                     _ => unreachable!(),
                 };
+                state.select_tool(tool);
             });
             tools_box.add(&tool);
         };
@@ -352,23 +410,8 @@ fn build_ui(application: &gtk::Applicati
 
         let state = state_weak.upgrade().unwrap();
         let mut state = state.lock().unwrap();
-        state.pressed = true;
         let (x, y) = event.get_position();
-        //println!("press tool {} at {}, {}", current_tool.lock().unwrap(), x, y);
-        state.dev.write(&[
-            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
-            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
-            input_axis_new(AbsoluteAxis::Z, 0),
-            input_axis_new(AbsoluteAxis::Wheel, 0),
-            input_axis_new(AbsoluteAxis::Pressure, 1024),
-            input_axis_new(AbsoluteAxis::Distance, 0),
-            input_axis_new(AbsoluteAxis::TiltX, 16),
-            input_axis_new(AbsoluteAxis::TiltY, 0),
-            input_misc_new(MiscKind::Serial, 0),
-            input_key_new(state.selected_tool, 1),
-            input_synchronize_new(SynchronizeKind::Report, 0),
-        ])
-        .unwrap();
+        state.press(x, y).unwrap();
         Inhibit(false)
     });
     let state_weak = Arc::downgrade(&state);
@@ -380,42 +423,13 @@ fn build_ui(application: &gtk::Applicati
         let state = state_weak.upgrade().unwrap();
         let mut state = state.lock().unwrap();
         let (x, y) = event.get_position();
-        state.pressed = false;
-        //println!("release {}, {}", x, y);
-        state.dev.write(&[
-            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
-            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
-            input_axis_new(AbsoluteAxis::Z, 0),
-            input_axis_new(AbsoluteAxis::Wheel, 0),
-            input_axis_new(AbsoluteAxis::Pressure, 0),
-            input_axis_new(AbsoluteAxis::Distance, 16),
-            input_axis_new(AbsoluteAxis::TiltX, 16),
-            input_axis_new(AbsoluteAxis::TiltY, 0),
-            input_misc_new(MiscKind::Serial, 0),
-            input_key_new(state.selected_tool, 1),
-            input_synchronize_new(SynchronizeKind::Report, 0),
-        ])
-        .unwrap();
+        state.release(x, y).unwrap();
         Inhibit(false)
     });
     drawing_area.connect_motion_notify_event(move |_, event| {
-        let state = state.lock().unwrap();
+        let mut state = state.lock().unwrap();
         let (x, y) = event.get_position();
-        //println!("motion {}, {}", x, y);
-        state.dev.write(&[
-            input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
-            input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
-            input_axis_new(AbsoluteAxis::Z, 0),
-            input_axis_new(AbsoluteAxis::Wheel, 0),
-            input_axis_new(AbsoluteAxis::Pressure, if state.pressed { 2048 } else { 0 }),
-            input_axis_new(AbsoluteAxis::Distance, if state.pressed { 0 } else { 32 }),
-            input_axis_new(AbsoluteAxis::TiltX, 16),
-            input_axis_new(AbsoluteAxis::TiltY, 0),
-            input_misc_new(MiscKind::Serial, 0),
-            input_key_new(state.selected_tool, 1),
-            input_synchronize_new(SynchronizeKind::Report, 0),
-        ])
-        .unwrap();
+        state.motion(x, y).unwrap();
         Inhibit(false)
     });
     drawing_area.connect_draw(move |_, ctx| {