Skip to content

Commit 52fe66b

Browse files
henryksloanKeavon
authored andcommitted
MVP eyedropper tool for fill colors (#300)
* Implement eyedropper for layer fill colors * Add shortcut for eyedropper * Add right mouse sampling for secondary color
1 parent 6a5d3cc commit 52fe66b

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

client/web/src/components/panels/Document.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<ShelfItemInput :icon="'LayoutSelectTool'" title="Select Tool (V)" :active="activeTool === 'Select'" @click="selectTool('Select')" />
6666
<ShelfItemInput :icon="'LayoutCropTool'" title="Crop Tool" :active="activeTool === 'Crop'" @click="'tool not implemented' || selectTool('Crop')" />
6767
<ShelfItemInput :icon="'LayoutNavigateTool'" title="Navigate Tool (Z)" :active="activeTool === 'Navigate'" @click="'tool not implemented' || selectTool('Navigate')" />
68-
<ShelfItemInput :icon="'LayoutEyedropperTool'" title="Eyedropper Tool (I)" :active="activeTool === 'Eyedropper'" @click="'tool not implemented' || selectTool('Eyedropper')" />
68+
<ShelfItemInput :icon="'LayoutEyedropperTool'" title="Eyedropper Tool (I)" :active="activeTool === 'Eyedropper'" @click="selectTool('Eyedropper')" />
6969

7070
<Separator :type="SeparatorType.Section" :direction="SeparatorDirection.Vertical" />
7171

core/document/src/layers/style/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ impl Fill {
1919
pub fn new(color: Color) -> Self {
2020
Self { color: Some(color) }
2121
}
22+
pub fn color(&self) -> Option<Color> {
23+
self.color
24+
}
2225
pub fn none() -> Self {
2326
Self { color: None }
2427
}
@@ -41,6 +44,12 @@ impl Stroke {
4144
pub fn new(color: Color, width: f32) -> Self {
4245
Self { color, width }
4346
}
47+
pub fn color(&self) -> Color {
48+
self.color
49+
}
50+
pub fn width(&self) -> f32 {
51+
self.width
52+
}
4453
pub fn render(&self) -> String {
4554
format!(r##" stroke="#{}"{} stroke-width="{}""##, self.color.rgb_hex(), format_opacity("stroke", self.color.a()), self.width)
4655
}

core/editor/src/input/input_mapper.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ impl Default for Mapping {
122122
entry! {action=SelectMessage::DragStop, key_up=Lmb},
123123
entry! {action=SelectMessage::Abort, key_down=Rmb},
124124
entry! {action=SelectMessage::Abort, key_down=KeyEscape},
125+
// Eyedropper
126+
entry! {action=EyedropperMessage::LeftMouseDown, key_down=Lmb},
127+
entry! {action=EyedropperMessage::RightMouseDown, key_down=Rmb},
125128
// Rectangle
126129
entry! {action=RectangleMessage::Center, key_down=KeyAlt},
127130
entry! {action=RectangleMessage::UnCenter, key_up=KeyAlt},
@@ -181,6 +184,7 @@ impl Default for Mapping {
181184
entry! {action=ToolMessage::SelectTool(ToolType::Line), key_down=KeyL},
182185
entry! {action=ToolMessage::SelectTool(ToolType::Pen), key_down=KeyP},
183186
entry! {action=ToolMessage::SelectTool(ToolType::Shape), key_down=KeyY},
187+
entry! {action=ToolMessage::SelectTool(ToolType::Eyedropper), key_down=KeyI},
184188
entry! {action=ToolMessage::ResetColors, key_down=KeyX, modifiers=[KeyShift, KeyControl]},
185189
entry! {action=ToolMessage::SwapColors, key_down=KeyX, modifiers=[KeyShift]},
186190
// Document Actions
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
1+
use crate::consts::SELECTION_TOLERANCE;
2+
use crate::frontend::FrontendMessage;
13
use crate::message_prelude::*;
2-
use crate::tool::ToolActionHandlerData;
4+
use crate::tool::{ToolActionHandlerData, ToolMessage};
5+
use glam::DVec2;
36

47
#[derive(Default)]
58
pub struct Eyedropper;
69

710
#[impl_message(Message, ToolMessage, Eyedropper)]
811
#[derive(PartialEq, Clone, Debug)]
912
pub enum EyedropperMessage {
10-
MouseMove,
13+
LeftMouseDown,
14+
RightMouseDown,
1115
}
1216

1317
impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for Eyedropper {
1418
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+
}
1647
}
17-
advertise_actions!();
48+
advertise_actions!(EyedropperMessageDiscriminant; LeftMouseDown, RightMouseDown);
1849
}

0 commit comments

Comments
 (0)