Mercurial > tablet-emu
comparison src/main.rs @ 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 |
comparison
equal
deleted
inserted
replaced
1:6dbe2bbeef70 | 2:39f87ef69b2a |
---|---|
177 | 177 |
178 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event { | 178 fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event { |
179 input_event_new(EventKind::Absolute, code as u16, value) | 179 input_event_new(EventKind::Absolute, code as u16, value) |
180 } | 180 } |
181 | 181 |
182 fn input_key_new(code: Key, value: i32) -> input_event { | |
183 input_event_new(EventKind::Key, code as u16, value) | |
184 } | |
185 | |
182 fn input_misc_new(code: MiscKind, value: i32) -> input_event { | 186 fn input_misc_new(code: MiscKind, value: i32) -> input_event { |
183 input_event_new(EventKind::Absolute, code as u16, value) | 187 input_event_new(EventKind::Misc, code as u16, value) |
184 } | 188 } |
185 | 189 |
186 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event { | 190 fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event { |
187 input_event_new(EventKind::Absolute, code as u16, value) | 191 input_event_new(EventKind::Synchronize, code as u16, value) |
188 } | 192 } |
189 | 193 |
190 fn build_ui(application: >k::Application) { | 194 fn build_ui(application: >k::Application) { |
191 let dev = match create_uinput_device() { | 195 let dev = match create_uinput_device() { |
192 Ok(dev) => Arc::new(dev), | 196 Ok(dev) => Arc::new(dev), |
217 window.set_position(gtk::WindowPosition::Center); | 221 window.set_position(gtk::WindowPosition::Center); |
218 | 222 |
219 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); | 223 let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0); |
220 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); | 224 let tools_box = gtk::Box::new(gtk::Orientation::Vertical, 0); |
221 | 225 |
222 let tool_name = Arc::new(Mutex::new(String::from("Pen"))); | 226 let current_tool = Arc::new(Mutex::new(Key::ButtonToolPen)); |
223 let tool1 = gtk::Button::with_label("Pen"); | 227 let tool1 = gtk::Button::with_label("Pen"); |
224 let tool2 = gtk::Button::with_label("Rubber"); | 228 let tool2 = gtk::Button::with_label("Rubber"); |
225 let tool3 = gtk::Button::with_label("Brush"); | 229 let tool3 = gtk::Button::with_label("Brush"); |
226 let tool4 = gtk::Button::with_label("Pencil"); | 230 let tool4 = gtk::Button::with_label("Pencil"); |
227 let tool5 = gtk::Button::with_label("Airbrush"); | 231 let tool5 = gtk::Button::with_label("Airbrush"); |
228 | 232 |
229 macro_rules! impl_tool_signal { | 233 macro_rules! impl_tool_signal { |
230 ($tool:ident) => { | 234 ($tool:ident) => { |
231 let tool_name_weak = Arc::downgrade(&tool_name); | 235 let current_tool_weak = Arc::downgrade(¤t_tool); |
232 $tool.connect_clicked(move |b| { | 236 $tool.connect_clicked(move |b| { |
233 let tool_name = tool_name_weak.upgrade().unwrap(); | 237 let current_tool = current_tool_weak.upgrade().unwrap(); |
234 let mut tool_name = tool_name.lock().unwrap(); | 238 let mut current_tool = current_tool.lock().unwrap(); |
235 *tool_name = b.get_label().unwrap().to_string(); | 239 *current_tool = match b.get_label().unwrap().as_str() { |
240 "Pen" => Key::ButtonToolPen, | |
241 "Rubber" => Key::ButtonToolRubber, | |
242 "Brush" => Key::ButtonToolBrush, | |
243 "Pencil" => Key::ButtonToolPencil, | |
244 "Airbrush" => Key::ButtonToolAirbrush, | |
245 _ => unreachable!(), | |
246 }; | |
236 }); | 247 }); |
237 }; | 248 }; |
238 }; | 249 }; |
239 impl_tool_signal!(tool1); | 250 impl_tool_signal!(tool1); |
240 impl_tool_signal!(tool2); | 251 impl_tool_signal!(tool2); |
268 }; | 279 }; |
269 true | 280 true |
270 }); | 281 }); |
271 let dev_weak = Arc::downgrade(&dev); | 282 let dev_weak = Arc::downgrade(&dev); |
272 let current_size_weak = Arc::downgrade(¤t_size); | 283 let current_size_weak = Arc::downgrade(¤t_size); |
284 let current_tool_weak = Arc::downgrade(¤t_tool); | |
273 let pressed_weak = Arc::downgrade(&pressed); | 285 let pressed_weak = Arc::downgrade(&pressed); |
274 drawing_area.connect_button_press_event(move |_, event| { | 286 drawing_area.connect_button_press_event(move |_, event| { |
275 if event.get_button() != 1 { | 287 if event.get_button() != 1 { |
276 return Inhibit(false); | 288 return Inhibit(false); |
277 } | 289 } |
278 | 290 |
279 let dev = dev_weak.upgrade().unwrap(); | 291 let dev = dev_weak.upgrade().unwrap(); |
292 let current_tool = current_tool_weak.upgrade().unwrap(); | |
280 let pressed = pressed_weak.upgrade().unwrap(); | 293 let pressed = pressed_weak.upgrade().unwrap(); |
281 let mut pressed = pressed.lock().unwrap(); | 294 let mut pressed = pressed.lock().unwrap(); |
282 *pressed = true; | 295 *pressed = true; |
283 let (x, y) = event.get_position(); | 296 let (x, y) = event.get_position(); |
284 println!("press tool {} at {}, {}", tool_name.lock().unwrap(), x, y); | 297 //println!("press tool {} at {}, {}", current_tool.lock().unwrap(), x, y); |
285 let current_size = current_size_weak.upgrade().unwrap(); | 298 let current_size = current_size_weak.upgrade().unwrap(); |
286 let current_size = current_size.lock().unwrap(); | 299 let current_size = current_size.lock().unwrap(); |
287 let (width, height) = *current_size; | 300 let (width, height) = *current_size; |
288 dev.write(&[ | 301 dev.write(&[ |
289 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), | 302 input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), |
293 input_axis_new(AbsoluteAxis::Pressure, 1024), | 306 input_axis_new(AbsoluteAxis::Pressure, 1024), |
294 input_axis_new(AbsoluteAxis::Distance, 0), | 307 input_axis_new(AbsoluteAxis::Distance, 0), |
295 input_axis_new(AbsoluteAxis::TiltX, 16), | 308 input_axis_new(AbsoluteAxis::TiltX, 16), |
296 input_axis_new(AbsoluteAxis::TiltY, 0), | 309 input_axis_new(AbsoluteAxis::TiltY, 0), |
297 input_misc_new(MiscKind::Serial, 0), | 310 input_misc_new(MiscKind::Serial, 0), |
311 input_key_new(*current_tool.lock().unwrap(), 0), | |
298 input_synchronize_new(SynchronizeKind::Report, 0), | 312 input_synchronize_new(SynchronizeKind::Report, 0), |
299 ]) | 313 ]) |
300 .unwrap(); | 314 .unwrap(); |
301 Inhibit(false) | 315 Inhibit(false) |
302 }); | 316 }); |
303 let dev_weak = Arc::downgrade(&dev); | 317 let dev_weak = Arc::downgrade(&dev); |
304 let current_size_weak = Arc::downgrade(¤t_size); | 318 let current_size_weak = Arc::downgrade(¤t_size); |
319 let current_tool_weak = Arc::downgrade(¤t_tool); | |
305 let pressed_weak = Arc::downgrade(&pressed); | 320 let pressed_weak = Arc::downgrade(&pressed); |
306 drawing_area.connect_button_release_event(move |_, event| { | 321 drawing_area.connect_button_release_event(move |_, event| { |
307 if event.get_button() != 1 { | 322 if event.get_button() != 1 { |
308 return Inhibit(false); | 323 return Inhibit(false); |
309 } | 324 } |
310 | 325 |
311 let dev = dev_weak.upgrade().unwrap(); | 326 let dev = dev_weak.upgrade().unwrap(); |
327 let current_tool = current_tool_weak.upgrade().unwrap(); | |
312 let (x, y) = event.get_position(); | 328 let (x, y) = event.get_position(); |
313 let pressed = pressed_weak.upgrade().unwrap(); | 329 let pressed = pressed_weak.upgrade().unwrap(); |
314 let mut pressed = pressed.lock().unwrap(); | 330 let mut pressed = pressed.lock().unwrap(); |
315 *pressed = false; | 331 *pressed = false; |
316 //println!("release {}, {}", x, y); | 332 //println!("release {}, {}", x, y); |
325 input_axis_new(AbsoluteAxis::Pressure, 0), | 341 input_axis_new(AbsoluteAxis::Pressure, 0), |
326 input_axis_new(AbsoluteAxis::Distance, 16), | 342 input_axis_new(AbsoluteAxis::Distance, 16), |
327 input_axis_new(AbsoluteAxis::TiltX, 16), | 343 input_axis_new(AbsoluteAxis::TiltX, 16), |
328 input_axis_new(AbsoluteAxis::TiltY, 0), | 344 input_axis_new(AbsoluteAxis::TiltY, 0), |
329 input_misc_new(MiscKind::Serial, 0), | 345 input_misc_new(MiscKind::Serial, 0), |
346 input_key_new(*current_tool.lock().unwrap(), 0), | |
330 input_synchronize_new(SynchronizeKind::Report, 0), | 347 input_synchronize_new(SynchronizeKind::Report, 0), |
331 ]) | 348 ]) |
332 .unwrap(); | 349 .unwrap(); |
333 Inhibit(false) | 350 Inhibit(false) |
334 }); | 351 }); |
346 input_axis_new(AbsoluteAxis::Pressure, if *pressed { 2048 } else { 0 }), | 363 input_axis_new(AbsoluteAxis::Pressure, if *pressed { 2048 } else { 0 }), |
347 input_axis_new(AbsoluteAxis::Distance, if *pressed { 0 } else { 32 }), | 364 input_axis_new(AbsoluteAxis::Distance, if *pressed { 0 } else { 32 }), |
348 input_axis_new(AbsoluteAxis::TiltX, 16), | 365 input_axis_new(AbsoluteAxis::TiltX, 16), |
349 input_axis_new(AbsoluteAxis::TiltY, 0), | 366 input_axis_new(AbsoluteAxis::TiltY, 0), |
350 input_misc_new(MiscKind::Serial, 0), | 367 input_misc_new(MiscKind::Serial, 0), |
368 input_key_new(*current_tool.lock().unwrap(), 0), | |
351 input_synchronize_new(SynchronizeKind::Report, 0), | 369 input_synchronize_new(SynchronizeKind::Report, 0), |
352 ]) | 370 ]) |
353 .unwrap(); | 371 .unwrap(); |
354 Inhibit(false) | 372 Inhibit(false) |
355 }); | 373 }); |