|
| 1 | +use crate::consts::SELECTION_TOLERANCE; |
| 2 | +use crate::frontend::FrontendMessage; |
1 | 3 | use crate::message_prelude::*; |
2 | | -use crate::tool::ToolActionHandlerData; |
| 4 | +use crate::tool::{ToolActionHandlerData, ToolMessage}; |
| 5 | +use glam::DVec2; |
3 | 6 |
|
4 | 7 | #[derive(Default)] |
5 | 8 | pub struct Eyedropper; |
6 | 9 |
|
7 | 10 | #[impl_message(Message, ToolMessage, Eyedropper)] |
8 | 11 | #[derive(PartialEq, Clone, Debug)] |
9 | 12 | pub enum EyedropperMessage { |
10 | | - MouseMove, |
| 13 | + LeftMouseDown, |
| 14 | + RightMouseDown, |
11 | 15 | } |
12 | 16 |
|
13 | 17 | impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for Eyedropper { |
14 | 18 | fn process_action(&mut self, action: ToolMessage, data: ToolActionHandlerData<'a>, responses: &mut VecDeque<Message>) { |
15 | | - todo!("{}::handle_input {:?} {:?} {:?} ", module_path!(), action, data, responses); |
| 19 | + let mouse_pos = data.2.mouse.position; |
| 20 | + let (x, y) = (mouse_pos.x as f64, mouse_pos.y as f64); |
| 21 | + let (point_1, point_2) = ( |
| 22 | + DVec2::new(x - SELECTION_TOLERANCE, y - SELECTION_TOLERANCE), |
| 23 | + DVec2::new(x + SELECTION_TOLERANCE, y + SELECTION_TOLERANCE), |
| 24 | + ); |
| 25 | + |
| 26 | + let quad = [ |
| 27 | + DVec2::new(point_1.x, point_1.y), |
| 28 | + DVec2::new(point_2.x, point_1.y), |
| 29 | + DVec2::new(point_2.x, point_2.y), |
| 30 | + DVec2::new(point_1.x, point_2.y), |
| 31 | + ]; |
| 32 | + |
| 33 | + if let Some(path) = data.0.document.intersects_quad_root(quad).last() { |
| 34 | + if let Ok(layer) = data.0.document.layer(path) { |
| 35 | + if let Some(fill) = layer.style.fill() { |
| 36 | + if let Some(color) = fill.color() { |
| 37 | + let (primary, secondary) = match action { |
| 38 | + ToolMessage::Eyedropper(EyedropperMessage::LeftMouseDown) => (color, data.1.secondary_color), |
| 39 | + ToolMessage::Eyedropper(EyedropperMessage::RightMouseDown) => (data.1.primary_color, color), |
| 40 | + _ => (data.1.primary_color, data.1.secondary_color), |
| 41 | + }; |
| 42 | + responses.push_back(FrontendMessage::UpdateWorkingColors { primary, secondary }.into()); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
16 | 47 | } |
17 | | - advertise_actions!(); |
| 48 | + advertise_actions!(EyedropperMessageDiscriminant; LeftMouseDown, RightMouseDown); |
18 | 49 | } |
0 commit comments