comparison src/main.rs @ 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
comparison
equal deleted inserted replaced
9:d1972fc49a5b 10:06d77bb94a50
230 width: WIDTH as f64, 230 width: WIDTH as f64,
231 height: HEIGHT as f64, 231 height: HEIGHT as f64,
232 selected_tool: Key::ButtonToolPen, 232 selected_tool: Key::ButtonToolPen,
233 pressed: false, 233 pressed: false,
234 }))) 234 })))
235 }
236
237 fn select_tool(&mut self, tool: Key) {
238 self.selected_tool = tool;
239 }
240
241 fn press(&mut self, x: f64, y: f64) -> std::io::Result<()> {
242 self.pressed = true;
243 self.dev.write(&[
244 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
245 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
246 input_axis_new(AbsoluteAxis::Z, 0),
247 input_axis_new(AbsoluteAxis::Wheel, 0),
248 input_axis_new(AbsoluteAxis::Pressure, 1024),
249 input_axis_new(AbsoluteAxis::Distance, 0),
250 input_axis_new(AbsoluteAxis::TiltX, 16),
251 input_axis_new(AbsoluteAxis::TiltY, 0),
252 input_misc_new(MiscKind::Serial, 0),
253 input_key_new(self.selected_tool, 1),
254 input_synchronize_new(SynchronizeKind::Report, 0),
255 ])?;
256 Ok(())
257 }
258
259 fn release(&mut self, x: f64, y: f64) -> std::io::Result<()> {
260 self.pressed = false;
261 self.dev.write(&[
262 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
263 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
264 input_axis_new(AbsoluteAxis::Z, 0),
265 input_axis_new(AbsoluteAxis::Wheel, 0),
266 input_axis_new(AbsoluteAxis::Pressure, 0),
267 input_axis_new(AbsoluteAxis::Distance, 16),
268 input_axis_new(AbsoluteAxis::TiltX, 16),
269 input_axis_new(AbsoluteAxis::TiltY, 0),
270 input_misc_new(MiscKind::Serial, 0),
271 input_key_new(self.selected_tool, 1),
272 input_synchronize_new(SynchronizeKind::Report, 0),
273 ])?;
274 Ok(())
275 }
276
277 fn motion(&mut self, x: f64, y: f64) -> std::io::Result<()> {
278 self.dev.write(&[
279 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / self.width) as i32),
280 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / self.height) as i32),
281 input_axis_new(AbsoluteAxis::Z, 0),
282 input_axis_new(AbsoluteAxis::Wheel, 0),
283 input_axis_new(AbsoluteAxis::Pressure, if self.pressed { 2048 } else { 0 }),
284 input_axis_new(AbsoluteAxis::Distance, if self.pressed { 0 } else { 32 }),
285 input_axis_new(AbsoluteAxis::TiltX, 16),
286 input_axis_new(AbsoluteAxis::TiltY, 0),
287 input_misc_new(MiscKind::Serial, 0),
288 input_key_new(self.selected_tool, 1),
289 input_synchronize_new(SynchronizeKind::Report, 0),
290 ])?;
291 Ok(())
235 } 292 }
236 } 293 }
237 294
238 fn build_main_menu(application: &gtk::Application) { 295 fn build_main_menu(application: &gtk::Application) {
239 let quit = gio::SimpleAction::new("quit", None); 296 let quit = gio::SimpleAction::new("quit", None);
304 let tool = gtk::Button::with_mnemonic($tool); 361 let tool = gtk::Button::with_mnemonic($tool);
305 let state_weak = Arc::downgrade(&state); 362 let state_weak = Arc::downgrade(&state);
306 tool.connect_clicked(move |b| { 363 tool.connect_clicked(move |b| {
307 let state = state_weak.upgrade().unwrap(); 364 let state = state_weak.upgrade().unwrap();
308 let mut state = state.lock().unwrap(); 365 let mut state = state.lock().unwrap();
309 state.selected_tool = match b.get_label().unwrap().as_str() { 366 let tool = match b.get_label().unwrap().as_str() {
310 "_Pen" => Key::ButtonToolPen, 367 "_Pen" => Key::ButtonToolPen,
311 "_Rubber" => Key::ButtonToolRubber, 368 "_Rubber" => Key::ButtonToolRubber,
312 "_Brush" => Key::ButtonToolBrush, 369 "_Brush" => Key::ButtonToolBrush,
313 "P_encil" => Key::ButtonToolPencil, 370 "P_encil" => Key::ButtonToolPencil,
314 "_Airbrush" => Key::ButtonToolAirbrush, 371 "_Airbrush" => Key::ButtonToolAirbrush,
315 _ => unreachable!(), 372 _ => unreachable!(),
316 }; 373 };
374 state.select_tool(tool);
317 }); 375 });
318 tools_box.add(&tool); 376 tools_box.add(&tool);
319 }; 377 };
320 }; 378 };
321 impl_tool!("_Pen"); 379 impl_tool!("_Pen");
350 return Inhibit(false); 408 return Inhibit(false);
351 } 409 }
352 410
353 let state = state_weak.upgrade().unwrap(); 411 let state = state_weak.upgrade().unwrap();
354 let mut state = state.lock().unwrap(); 412 let mut state = state.lock().unwrap();
355 state.pressed = true;
356 let (x, y) = event.get_position(); 413 let (x, y) = event.get_position();
357 //println!("press tool {} at {}, {}", current_tool.lock().unwrap(), x, y); 414 state.press(x, y).unwrap();
358 state.dev.write(&[
359 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
360 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
361 input_axis_new(AbsoluteAxis::Z, 0),
362 input_axis_new(AbsoluteAxis::Wheel, 0),
363 input_axis_new(AbsoluteAxis::Pressure, 1024),
364 input_axis_new(AbsoluteAxis::Distance, 0),
365 input_axis_new(AbsoluteAxis::TiltX, 16),
366 input_axis_new(AbsoluteAxis::TiltY, 0),
367 input_misc_new(MiscKind::Serial, 0),
368 input_key_new(state.selected_tool, 1),
369 input_synchronize_new(SynchronizeKind::Report, 0),
370 ])
371 .unwrap();
372 Inhibit(false) 415 Inhibit(false)
373 }); 416 });
374 let state_weak = Arc::downgrade(&state); 417 let state_weak = Arc::downgrade(&state);
375 drawing_area.connect_button_release_event(move |_, event| { 418 drawing_area.connect_button_release_event(move |_, event| {
376 if event.get_button() != 1 { 419 if event.get_button() != 1 {
378 } 421 }
379 422
380 let state = state_weak.upgrade().unwrap(); 423 let state = state_weak.upgrade().unwrap();
381 let mut state = state.lock().unwrap(); 424 let mut state = state.lock().unwrap();
382 let (x, y) = event.get_position(); 425 let (x, y) = event.get_position();
383 state.pressed = false; 426 state.release(x, y).unwrap();
384 //println!("release {}, {}", x, y);
385 state.dev.write(&[
386 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
387 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
388 input_axis_new(AbsoluteAxis::Z, 0),
389 input_axis_new(AbsoluteAxis::Wheel, 0),
390 input_axis_new(AbsoluteAxis::Pressure, 0),
391 input_axis_new(AbsoluteAxis::Distance, 16),
392 input_axis_new(AbsoluteAxis::TiltX, 16),
393 input_axis_new(AbsoluteAxis::TiltY, 0),
394 input_misc_new(MiscKind::Serial, 0),
395 input_key_new(state.selected_tool, 1),
396 input_synchronize_new(SynchronizeKind::Report, 0),
397 ])
398 .unwrap();
399 Inhibit(false) 427 Inhibit(false)
400 }); 428 });
401 drawing_area.connect_motion_notify_event(move |_, event| { 429 drawing_area.connect_motion_notify_event(move |_, event| {
402 let state = state.lock().unwrap(); 430 let mut state = state.lock().unwrap();
403 let (x, y) = event.get_position(); 431 let (x, y) = event.get_position();
404 //println!("motion {}, {}", x, y); 432 state.motion(x, y).unwrap();
405 state.dev.write(&[
406 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / state.width) as i32),
407 input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / state.height) as i32),
408 input_axis_new(AbsoluteAxis::Z, 0),
409 input_axis_new(AbsoluteAxis::Wheel, 0),
410 input_axis_new(AbsoluteAxis::Pressure, if state.pressed { 2048 } else { 0 }),
411 input_axis_new(AbsoluteAxis::Distance, if state.pressed { 0 } else { 32 }),
412 input_axis_new(AbsoluteAxis::TiltX, 16),
413 input_axis_new(AbsoluteAxis::TiltY, 0),
414 input_misc_new(MiscKind::Serial, 0),
415 input_key_new(state.selected_tool, 1),
416 input_synchronize_new(SynchronizeKind::Report, 0),
417 ])
418 .unwrap();
419 Inhibit(false) 433 Inhibit(false)
420 }); 434 });
421 drawing_area.connect_draw(move |_, ctx| { 435 drawing_area.connect_draw(move |_, ctx| {
422 //println!("drawing {}", drawing_area); 436 //println!("drawing {}", drawing_area);
423 ctx.set_source_rgb(1., 0., 0.); 437 ctx.set_source_rgb(1., 0., 0.);