mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Gradient Tool (#546)
* Refactor to support fill enum & svg defs * Init tool * Fix advertise * Gradient tool click and drag * Overlays * Drag overlays * Cleanup * Fix transform on elongated shapes * Snap rotate * Snapping * Add hints * Rename to solid * Code review changes Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
committed by
Keavon Chambers
parent
8bf1289429
commit
93dffb8741
@@ -30,7 +30,7 @@ impl MessageHandler<ArtboardMessage, ()> for ArtboardMessageHandler {
|
||||
match message {
|
||||
// Sub-messages
|
||||
#[remain::unsorted]
|
||||
DispatchOperation(operation) => match self.artboards_graphene_document.handle_operation(&operation) {
|
||||
DispatchOperation(operation) => match self.artboards_graphene_document.handle_operation(*operation) {
|
||||
Ok(_) => (),
|
||||
Err(e) => log::error!("Artboard Error: {:?}", e),
|
||||
},
|
||||
@@ -46,7 +46,7 @@ impl MessageHandler<ArtboardMessage, ()> for ArtboardMessageHandler {
|
||||
path: vec![artboard_id],
|
||||
insert_index: -1,
|
||||
transform: DAffine2::from_scale_angle_translation(size.into(), 0., position.into()).to_cols_array(),
|
||||
style: style::PathStyle::new(None, Some(Fill::new(Color::WHITE))),
|
||||
style: style::PathStyle::new(None, Fill::solid(Color::WHITE)),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
|
||||
@@ -628,7 +628,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessorMessageHandler> for Docum
|
||||
match message {
|
||||
// Sub-messages
|
||||
#[remain::unsorted]
|
||||
DispatchOperation(op) => match self.graphene_document.handle_operation(&op) {
|
||||
DispatchOperation(op) => match self.graphene_document.handle_operation(*op) {
|
||||
Ok(Some(document_responses)) => {
|
||||
for response in document_responses {
|
||||
match &response {
|
||||
|
||||
@@ -26,15 +26,17 @@ pub fn layer_panel_entry(layer_metadata: &LayerMetadata, transform: DAffine2, la
|
||||
let arr = arr.iter().map(|x| (*x).into()).collect::<Vec<(f64, f64)>>();
|
||||
|
||||
let mut thumbnail = String::new();
|
||||
layer.data.clone().render(&mut thumbnail, &mut vec![transform], ViewMode::Normal);
|
||||
let mut svg_defs = String::new();
|
||||
layer.data.clone().render(&mut thumbnail, &mut svg_defs, &mut vec![transform], ViewMode::Normal);
|
||||
let transform = transform.to_cols_array().iter().map(ToString::to_string).collect::<Vec<_>>().join(",");
|
||||
let thumbnail = if let [(x_min, y_min), (x_max, y_max)] = arr.as_slice() {
|
||||
format!(
|
||||
r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="{} {} {} {}"><g transform="matrix({})">{}</g></svg>"#,
|
||||
r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="{} {} {} {}"><defs>{}</defs><g transform="matrix({})">{}</g></svg>"#,
|
||||
x_min,
|
||||
y_min,
|
||||
x_max - x_min,
|
||||
y_max - y_min,
|
||||
svg_defs,
|
||||
transform,
|
||||
thumbnail,
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ impl MessageHandler<OverlaysMessage, bool> for OverlaysMessageHandler {
|
||||
match message {
|
||||
// Sub-messages
|
||||
#[remain::unsorted]
|
||||
DispatchOperation(operation) => match self.overlays_graphene_document.handle_operation(&operation) {
|
||||
DispatchOperation(operation) => match self.overlays_graphene_document.handle_operation(*operation) {
|
||||
Ok(_) => responses.push_back(OverlaysMessage::Rerender.into()),
|
||||
Err(e) => log::error!("OverlaysError: {:?}", e),
|
||||
},
|
||||
|
||||
@@ -72,6 +72,10 @@ impl Default for Mapping {
|
||||
entry! {action=TextMessage::Interact, key_up=Lmb},
|
||||
entry! {action=TextMessage::Abort, key_down=KeyEscape},
|
||||
entry! {action=TextMessage::CommitText, key_down=KeyEnter, modifiers=[KeyControl]},
|
||||
// Gradient
|
||||
entry! {action=GradientToolMessage::PointerDown, key_down=Lmb},
|
||||
entry! {action=GradientToolMessage::PointerMove { constrain_axis: KeyShift }, message=InputMapperMessage::PointerMove},
|
||||
entry! {action=GradientToolMessage::PointerUp, key_up=Lmb},
|
||||
// Rectangle
|
||||
entry! {action=RectangleToolMessage::DragStart, key_down=Lmb},
|
||||
entry! {action=RectangleToolMessage::DragStop, key_up=Lmb},
|
||||
@@ -127,6 +131,7 @@ impl Default for Mapping {
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Eyedropper }, key_down=KeyI},
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Text }, key_down=KeyT},
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Fill }, key_down=KeyF},
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Gradient }, key_down=KeyH},
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Path }, key_down=KeyA},
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Pen }, key_down=KeyP},
|
||||
entry! {action=ToolMessage::ActivateTool { tool_type: ToolType::Freehand }, key_down=KeyN},
|
||||
|
||||
@@ -77,6 +77,7 @@ pub mod message_prelude {
|
||||
pub use crate::viewport_tools::tools::eyedropper_tool::{EyedropperToolMessage, EyedropperToolMessageDiscriminant};
|
||||
pub use crate::viewport_tools::tools::fill_tool::{FillToolMessage, FillToolMessageDiscriminant};
|
||||
pub use crate::viewport_tools::tools::freehand_tool::{FreehandToolMessage, FreehandToolMessageDiscriminant};
|
||||
pub use crate::viewport_tools::tools::gradient_tool::{GradientToolMessage, GradientToolMessageDiscriminant};
|
||||
pub use crate::viewport_tools::tools::line_tool::{LineToolMessage, LineToolMessageDiscriminant};
|
||||
pub use crate::viewport_tools::tools::navigate_tool::{NavigateToolMessage, NavigateToolMessageDiscriminant};
|
||||
pub use crate::viewport_tools::tools::path_tool::{PathToolMessage, PathToolMessageDiscriminant};
|
||||
|
||||
@@ -34,7 +34,7 @@ impl SnapHandler {
|
||||
Operation::AddOverlayLine {
|
||||
path: layer_path.clone(),
|
||||
transform,
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), None),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), style::Fill::None),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
|
||||
@@ -83,13 +83,13 @@ impl Default for ToolFsmState {
|
||||
Eyedropper => eyedropper_tool::EyedropperTool,
|
||||
Text => text_tool::TextTool,
|
||||
Fill => fill_tool::FillTool,
|
||||
// Gradient => gradient::Gradient,
|
||||
// Brush => brush::Brush,
|
||||
// Heal => heal::Heal,
|
||||
// Clone => clone::Clone,
|
||||
// Patch => patch::Patch,
|
||||
// BlurSharpen => blursharpen::BlurSharpen,
|
||||
// Relight => relight::Relight,
|
||||
Gradient => gradient_tool::GradientTool,
|
||||
// Brush => brush_tool::BrushTool,
|
||||
// Heal => heal_tool::HealTool,
|
||||
// Clone => clone_tool:::CloneTool,
|
||||
// Patch => patch_tool:::PatchTool,
|
||||
// BlurSharpen => blursharpen_tool:::BlurSharpenTool,
|
||||
// Relight => relight_tool:::RelightTool,
|
||||
Path => path_tool::PathTool,
|
||||
Pen => pen_tool::PenTool,
|
||||
Freehand => freehand_tool::FreehandTool,
|
||||
@@ -191,8 +191,8 @@ pub fn standard_tool_message(tool: ToolType, message_type: StandardToolMessageTy
|
||||
ToolType::Navigate => None, // Some(NavigateToolMessage::DocumentIsDirty.into()),
|
||||
ToolType::Eyedropper => None, // Some(EyedropperToolMessage::DocumentIsDirty.into()),
|
||||
ToolType::Text => Some(TextMessage::DocumentIsDirty.into()),
|
||||
ToolType::Fill => None, // Some(FillToolMessage::DocumentIsDirty.into()),
|
||||
ToolType::Gradient => None, // Some(GradientMessage::DocumentIsDirty.into()),
|
||||
ToolType::Fill => None, // Some(FillToolMessage::DocumentIsDirty.into()),
|
||||
ToolType::Gradient => Some(GradientToolMessage::DocumentIsDirty.into()),
|
||||
ToolType::Brush => None, // Some(BrushMessage::DocumentIsDirty.into()),
|
||||
ToolType::Heal => None, // Some(HealMessage::DocumentIsDirty.into()),
|
||||
ToolType::Clone => None, // Some(CloneMessage::DocumentIsDirty.into()),
|
||||
@@ -215,7 +215,7 @@ pub fn standard_tool_message(tool: ToolType, message_type: StandardToolMessageTy
|
||||
ToolType::Eyedropper => Some(EyedropperToolMessage::Abort.into()),
|
||||
ToolType::Text => Some(TextMessage::Abort.into()),
|
||||
ToolType::Fill => Some(FillToolMessage::Abort.into()),
|
||||
// ToolType::Gradient => Some(GradientMessage::Abort.into()),
|
||||
ToolType::Gradient => Some(GradientToolMessage::Abort.into()),
|
||||
// ToolType::Brush => Some(BrushMessage::Abort.into()),
|
||||
// ToolType::Heal => Some(HealMessage::Abort.into()),
|
||||
// ToolType::Clone => Some(CloneMessage::Abort.into()),
|
||||
@@ -249,7 +249,7 @@ pub fn message_to_tool_type(message: &ToolMessage) -> ToolType {
|
||||
Eyedropper(_) => ToolType::Eyedropper,
|
||||
Text(_) => ToolType::Text,
|
||||
Fill(_) => ToolType::Fill,
|
||||
// Gradient(_) => ToolType::Gradient,
|
||||
Gradient(_) => ToolType::Gradient,
|
||||
// Brush(_) => ToolType::Brush,
|
||||
// Heal(_) => ToolType::Heal,
|
||||
// Clone(_) => ToolType::Clone,
|
||||
|
||||
@@ -31,27 +31,27 @@ pub enum ToolMessage {
|
||||
#[remain::unsorted]
|
||||
#[child]
|
||||
Fill(FillToolMessage),
|
||||
#[remain::unsorted]
|
||||
#[child]
|
||||
Gradient(GradientToolMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Gradient(GradientMessage),
|
||||
// Brush(BrushToolMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Brush(BrushMessage),
|
||||
// Heal(HealToolMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Heal(HealMessage),
|
||||
// Clone(CloneToolMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Clone(CloneMessage),
|
||||
// Patch(PatchToolMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Patch(PatchMessage),
|
||||
// Detail(DetailToolMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Detail(DetailMessage),
|
||||
// #[remain::unsorted]
|
||||
// #[child]
|
||||
// Relight(RelightMessage),
|
||||
// Relight(RelightToolMessage),
|
||||
#[remain::unsorted]
|
||||
#[child]
|
||||
Path(PathToolMessage),
|
||||
|
||||
@@ -120,7 +120,7 @@ impl Fsm for EllipseToolFsmState {
|
||||
path: shape_data.path.clone().unwrap(),
|
||||
insert_index: -1,
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
style: style::PathStyle::new(None, Some(style::Fill::new(tool_data.primary_color))),
|
||||
style: style::PathStyle::new(None, style::Fill::solid(tool_data.primary_color)),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
|
||||
@@ -101,10 +101,10 @@ impl Fsm for EyedropperToolFsmState {
|
||||
if let Some(path) = document.graphene_document.intersects_quad_root(quad).last() {
|
||||
if let Ok(layer) = document.graphene_document.layer(path) {
|
||||
if let LayerDataType::Shape(shape) = &layer.data {
|
||||
if let Some(fill) = shape.style.fill() {
|
||||
if shape.style.fill().is_some() {
|
||||
match lmb_or_rmb {
|
||||
EyedropperToolMessage::LeftMouseDown => responses.push_back(ToolMessage::SelectPrimaryColor { color: fill.color() }.into()),
|
||||
EyedropperToolMessage::RightMouseDown => responses.push_back(ToolMessage::SelectSecondaryColor { color: fill.color() }.into()),
|
||||
EyedropperToolMessage::LeftMouseDown => responses.push_back(ToolMessage::SelectPrimaryColor { color: shape.style.fill().color() }.into()),
|
||||
EyedropperToolMessage::RightMouseDown => responses.push_back(ToolMessage::SelectSecondaryColor { color: shape.style.fill().color() }.into()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use graphene::intersection::Quad;
|
||||
use graphene::Operation;
|
||||
|
||||
use glam::DVec2;
|
||||
use graphene::layers::style::Fill;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -103,8 +104,10 @@ impl Fsm for FillToolFsmState {
|
||||
RightMouseDown => tool_data.secondary_color,
|
||||
Abort => unreachable!(),
|
||||
};
|
||||
let fill = Fill::Solid(color);
|
||||
|
||||
responses.push_back(DocumentMessage::StartTransaction.into());
|
||||
responses.push_back(Operation::SetLayerFill { path: path.to_vec(), color }.into());
|
||||
responses.push_back(Operation::SetLayerFill { path: path.to_vec(), fill }.into());
|
||||
responses.push_back(DocumentMessage::CommitTransaction.into());
|
||||
}
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ fn add_polyline(data: &FreehandToolData, tool_data: &DocumentToolData) -> Messag
|
||||
insert_index: -1,
|
||||
transform: DAffine2::IDENTITY.to_cols_array(),
|
||||
points,
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), None),
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), style::Fill::None),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
416
editor/src/viewport_tools/tools/gradient_tool.rs
Normal file
416
editor/src/viewport_tools/tools/gradient_tool.rs
Normal file
@@ -0,0 +1,416 @@
|
||||
use crate::consts::{COLOR_ACCENT, LINE_ROTATE_SNAP_ANGLE, SELECTION_TOLERANCE, VECTOR_MANIPULATOR_ANCHOR_MARKER_SIZE};
|
||||
use crate::document::DocumentMessageHandler;
|
||||
use crate::frontend::utility_types::MouseCursorIcon;
|
||||
use crate::input::keyboard::{Key, MouseMotion};
|
||||
use crate::input::InputPreprocessorMessageHandler;
|
||||
use crate::layout::widgets::PropertyHolder;
|
||||
use crate::message_prelude::*;
|
||||
use crate::misc::{HintData, HintGroup, HintInfo, KeysGroup};
|
||||
use crate::viewport_tools::snapping::SnapHandler;
|
||||
use crate::viewport_tools::tool::{DocumentToolData, Fsm, ToolActionHandlerData};
|
||||
|
||||
use graphene::color::Color;
|
||||
use graphene::intersection::Quad;
|
||||
use graphene::layers::layer_info::{Layer, LayerDataType};
|
||||
use graphene::layers::style::{Fill, Gradient, PathStyle, Stroke};
|
||||
use graphene::Operation;
|
||||
|
||||
use glam::{DAffine2, DVec2};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GradientTool {
|
||||
fsm_state: GradientToolFsmState,
|
||||
data: GradientToolData,
|
||||
}
|
||||
|
||||
#[remain::sorted]
|
||||
#[impl_message(Message, ToolMessage, Gradient)]
|
||||
#[derive(PartialEq, Clone, Debug, Hash, Serialize, Deserialize)]
|
||||
pub enum GradientToolMessage {
|
||||
// Standard messages
|
||||
#[remain::unsorted]
|
||||
Abort,
|
||||
#[remain::unsorted]
|
||||
DocumentIsDirty,
|
||||
|
||||
// Tool-specific messages
|
||||
PointerDown,
|
||||
PointerMove {
|
||||
constrain_axis: Key,
|
||||
},
|
||||
PointerUp,
|
||||
}
|
||||
|
||||
impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for GradientTool {
|
||||
fn process_action(&mut self, action: ToolMessage, data: ToolActionHandlerData<'a>, responses: &mut VecDeque<Message>) {
|
||||
if action == ToolMessage::UpdateHints {
|
||||
self.fsm_state.update_hints(responses);
|
||||
return;
|
||||
}
|
||||
|
||||
if action == ToolMessage::UpdateCursor {
|
||||
self.fsm_state.update_cursor(responses);
|
||||
return;
|
||||
}
|
||||
|
||||
let new_state = self.fsm_state.transition(action, data.0, data.1, &mut self.data, &(), data.2, responses);
|
||||
|
||||
if self.fsm_state != new_state {
|
||||
self.fsm_state = new_state;
|
||||
self.fsm_state.update_hints(responses);
|
||||
}
|
||||
}
|
||||
|
||||
advertise_actions!(GradientToolMessageDiscriminant; PointerDown, PointerUp, PointerMove, Abort);
|
||||
}
|
||||
|
||||
impl PropertyHolder for GradientTool {}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum GradientToolFsmState {
|
||||
Ready,
|
||||
Drawing,
|
||||
}
|
||||
|
||||
impl Default for GradientToolFsmState {
|
||||
fn default() -> Self {
|
||||
GradientToolFsmState::Ready
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the transform from gradient space to layer space (where gradient space is 0..1 in layer space)
|
||||
fn gradient_space_transform(path: &[LayerId], layer: &Layer, document: &DocumentMessageHandler) -> DAffine2 {
|
||||
let bounds = layer.current_bounding_box().unwrap();
|
||||
let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
|
||||
|
||||
document.graphene_document.multiply_transforms(&path[..path.len() - 1]).unwrap() * bound_transform
|
||||
}
|
||||
|
||||
/// Contains info on the overlays for a single gradient
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct GradientOverlay {
|
||||
pub handles: [Vec<LayerId>; 2],
|
||||
pub line: Vec<LayerId>,
|
||||
path: Vec<LayerId>,
|
||||
transform: DAffine2,
|
||||
gradient: Gradient,
|
||||
}
|
||||
|
||||
impl GradientOverlay {
|
||||
fn generate_overlay_handle(translation: DVec2, responses: &mut VecDeque<Message>, selected: bool) -> Vec<LayerId> {
|
||||
let path = vec![generate_uuid()];
|
||||
|
||||
let size = DVec2::splat(VECTOR_MANIPULATOR_ANCHOR_MARKER_SIZE);
|
||||
|
||||
let fill = if selected { Fill::solid(COLOR_ACCENT) } else { Fill::solid(Color::WHITE) };
|
||||
|
||||
let operation = Operation::AddOverlayEllipse {
|
||||
path: path.clone(),
|
||||
transform: DAffine2::from_scale_angle_translation(size, 0., translation - size / 2.).to_cols_array(),
|
||||
style: PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), fill),
|
||||
};
|
||||
responses.push_back(DocumentMessage::Overlays(operation.into()).into());
|
||||
|
||||
path
|
||||
}
|
||||
fn generate_overlay_line(start: DVec2, end: DVec2, responses: &mut VecDeque<Message>) -> Vec<LayerId> {
|
||||
let path = vec![generate_uuid()];
|
||||
|
||||
let line_vector = end - start;
|
||||
let scale = DVec2::splat(line_vector.length());
|
||||
let angle = -line_vector.angle_between(DVec2::X);
|
||||
let translation = start;
|
||||
let transform = DAffine2::from_scale_angle_translation(scale, angle, translation).to_cols_array();
|
||||
|
||||
let operation = Operation::AddOverlayLine {
|
||||
path: path.clone(),
|
||||
transform,
|
||||
style: PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), Fill::None),
|
||||
};
|
||||
responses.push_back(DocumentMessage::Overlays(operation.into()).into());
|
||||
|
||||
path
|
||||
}
|
||||
|
||||
pub fn new(fill: &Gradient, dragging_start: Option<bool>, path: &[LayerId], layer: &Layer, document: &DocumentMessageHandler, responses: &mut VecDeque<Message>) -> Self {
|
||||
let transform = gradient_space_transform(path, layer, document);
|
||||
let Gradient { start, end, .. } = fill;
|
||||
let [start, end] = [transform.transform_point2(*start), transform.transform_point2(*end)];
|
||||
|
||||
let line = Self::generate_overlay_line(start, end, responses);
|
||||
let handles = [
|
||||
Self::generate_overlay_handle(start, responses, dragging_start == Some(true)),
|
||||
Self::generate_overlay_handle(end, responses, dragging_start == Some(false)),
|
||||
];
|
||||
|
||||
let path = path.to_vec();
|
||||
let gradient = fill.clone();
|
||||
|
||||
Self {
|
||||
handles,
|
||||
line,
|
||||
path,
|
||||
transform,
|
||||
gradient,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_overlays(self, responses: &mut VecDeque<Message>) {
|
||||
responses.push_back(DocumentMessage::Overlays(Operation::DeleteLayer { path: self.line }.into()).into());
|
||||
let [start, end] = self.handles;
|
||||
responses.push_back(DocumentMessage::Overlays(Operation::DeleteLayer { path: start }.into()).into());
|
||||
responses.push_back(DocumentMessage::Overlays(Operation::DeleteLayer { path: end }.into()).into());
|
||||
}
|
||||
|
||||
pub fn evaluate_gradient_start(&self) -> DVec2 {
|
||||
self.transform.transform_point2(self.gradient.start)
|
||||
}
|
||||
|
||||
pub fn evaluate_gradient_end(&self) -> DVec2 {
|
||||
self.transform.transform_point2(self.gradient.end)
|
||||
}
|
||||
}
|
||||
|
||||
/// Contains information about the selected gradient handle
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct SelectedGradient {
|
||||
path: Vec<LayerId>,
|
||||
transform: DAffine2,
|
||||
gradient: Gradient,
|
||||
dragging_start: bool,
|
||||
}
|
||||
|
||||
impl SelectedGradient {
|
||||
pub fn new(gradient: Gradient, path: &[LayerId], layer: &Layer, document: &DocumentMessageHandler) -> Self {
|
||||
let transform = gradient_space_transform(path, layer, document);
|
||||
Self {
|
||||
path: path.to_vec(),
|
||||
transform,
|
||||
gradient,
|
||||
dragging_start: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_gradient_start(mut self, start: DVec2) -> Self {
|
||||
self.gradient.start = self.transform.inverse().transform_point2(start);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn update_gradient(&mut self, mut mouse: DVec2, responses: &mut VecDeque<Message>, snap_rotate: bool) {
|
||||
if snap_rotate {
|
||||
let point = if self.dragging_start {
|
||||
self.transform.transform_point2(self.gradient.end)
|
||||
} else {
|
||||
self.transform.transform_point2(self.gradient.start)
|
||||
};
|
||||
|
||||
let delta = point - mouse;
|
||||
|
||||
let length = delta.length();
|
||||
let mut angle = -delta.angle_between(DVec2::X);
|
||||
|
||||
let snap_resolution = LINE_ROTATE_SNAP_ANGLE.to_radians();
|
||||
angle = (angle / snap_resolution).round() * snap_resolution;
|
||||
|
||||
let rotated = DVec2::new(length * angle.cos(), length * angle.sin());
|
||||
mouse = point - rotated;
|
||||
}
|
||||
|
||||
mouse = self.transform.inverse().transform_point2(mouse);
|
||||
|
||||
if self.dragging_start {
|
||||
self.gradient.start = mouse;
|
||||
} else {
|
||||
self.gradient.end = mouse;
|
||||
}
|
||||
|
||||
self.gradient.transform = self.transform.inverse();
|
||||
let fill = Fill::LinearGradient(self.gradient.clone());
|
||||
let path = self.path.clone();
|
||||
responses.push_back(Operation::SetLayerFill { path, fill }.into());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct GradientToolData {
|
||||
gradient_overlays: Vec<GradientOverlay>,
|
||||
selected_gradient: Option<SelectedGradient>,
|
||||
snap_handler: SnapHandler,
|
||||
}
|
||||
|
||||
pub fn start_snap(snap_handler: &mut SnapHandler, document: &DocumentMessageHandler, layer: &Layer, path: &[LayerId]) {
|
||||
snap_handler.start_snap(document, document.bounding_boxes(None, None), true, true);
|
||||
if let LayerDataType::Shape(s) = &layer.data {
|
||||
let transform = document.graphene_document.multiply_transforms(path).unwrap();
|
||||
let snap_points = s
|
||||
.path
|
||||
.iter()
|
||||
.filter_map(|shape| match shape {
|
||||
kurbo::PathEl::MoveTo(point) => Some(point),
|
||||
kurbo::PathEl::LineTo(point) => Some(point),
|
||||
kurbo::PathEl::QuadTo(_, point) => Some(point),
|
||||
kurbo::PathEl::CurveTo(_, _, point) => Some(point),
|
||||
kurbo::PathEl::ClosePath => None,
|
||||
})
|
||||
.map(|point| DVec2::new(point.x, point.y))
|
||||
.map(|pos| transform.transform_point2(pos))
|
||||
.collect();
|
||||
snap_handler.add_snap_points(document, snap_points);
|
||||
}
|
||||
}
|
||||
|
||||
impl Fsm for GradientToolFsmState {
|
||||
type ToolData = GradientToolData;
|
||||
type ToolOptions = ();
|
||||
|
||||
fn transition(
|
||||
self,
|
||||
event: ToolMessage,
|
||||
document: &DocumentMessageHandler,
|
||||
tool_data: &DocumentToolData,
|
||||
data: &mut Self::ToolData,
|
||||
_tool_options: &Self::ToolOptions,
|
||||
input: &InputPreprocessorMessageHandler,
|
||||
responses: &mut VecDeque<Message>,
|
||||
) -> Self {
|
||||
if let ToolMessage::Gradient(event) = event {
|
||||
match (self, event) {
|
||||
(_, GradientToolMessage::DocumentIsDirty) => {
|
||||
while let Some(overlay) = data.gradient_overlays.pop() {
|
||||
overlay.delete_overlays(responses);
|
||||
}
|
||||
|
||||
for path in document.selected_visible_layers() {
|
||||
let layer = document.graphene_document.layer(path).unwrap();
|
||||
|
||||
if let Ok(Fill::LinearGradient(gradient)) = layer.style().map(|style| style.fill()) {
|
||||
let dragging_start = data
|
||||
.selected_gradient
|
||||
.as_ref()
|
||||
.map_or(None, |selected| if selected.path == path { Some(selected.dragging_start) } else { None });
|
||||
data.gradient_overlays.push(GradientOverlay::new(gradient, dragging_start, path, layer, document, responses))
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
(GradientToolFsmState::Ready, GradientToolMessage::PointerDown) => {
|
||||
responses.push_back(ToolMessage::DocumentIsDirty.into());
|
||||
|
||||
let mouse = input.mouse.position;
|
||||
let tolerance = VECTOR_MANIPULATOR_ANCHOR_MARKER_SIZE.powi(2);
|
||||
|
||||
let mut dragging = false;
|
||||
for overlay in &data.gradient_overlays {
|
||||
if overlay.evaluate_gradient_start().distance_squared(mouse) < tolerance {
|
||||
dragging = true;
|
||||
start_snap(&mut data.snap_handler, document, document.graphene_document.layer(&overlay.path).unwrap(), &overlay.path);
|
||||
data.selected_gradient = Some(SelectedGradient {
|
||||
path: overlay.path.clone(),
|
||||
transform: overlay.transform.clone(),
|
||||
gradient: overlay.gradient.clone(),
|
||||
dragging_start: true,
|
||||
})
|
||||
}
|
||||
if overlay.evaluate_gradient_end().distance_squared(mouse) < tolerance {
|
||||
dragging = true;
|
||||
start_snap(&mut data.snap_handler, document, document.graphene_document.layer(&overlay.path).unwrap(), &overlay.path);
|
||||
data.selected_gradient = Some(SelectedGradient {
|
||||
path: overlay.path.clone(),
|
||||
transform: overlay.transform.clone(),
|
||||
gradient: overlay.gradient.clone(),
|
||||
dragging_start: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
if dragging {
|
||||
GradientToolFsmState::Drawing
|
||||
} else {
|
||||
let tolerance = DVec2::splat(SELECTION_TOLERANCE);
|
||||
let quad = Quad::from_box([input.mouse.position - tolerance, input.mouse.position + tolerance]);
|
||||
let intersection = document.graphene_document.intersects_quad_root(quad).pop();
|
||||
|
||||
if let Some(intersection) = intersection {
|
||||
if !document.selected_layers_contains(&intersection) {
|
||||
let replacement_selected_layers = vec![intersection.clone()];
|
||||
|
||||
responses.push_back(DocumentMessage::SetSelectedLayers { replacement_selected_layers }.into());
|
||||
}
|
||||
|
||||
let layer = document.graphene_document.layer(&intersection).unwrap();
|
||||
|
||||
let gradient = Gradient::new(DVec2::ZERO, tool_data.secondary_color, DVec2::ONE, tool_data.primary_color, DAffine2::IDENTITY, generate_uuid());
|
||||
let mut selected_gradient = SelectedGradient::new(gradient, &intersection, layer, document).with_gradient_start(input.mouse.position);
|
||||
selected_gradient.update_gradient(input.mouse.position, responses, false);
|
||||
|
||||
data.selected_gradient = Some(selected_gradient);
|
||||
|
||||
start_snap(&mut data.snap_handler, document, layer, &intersection);
|
||||
|
||||
GradientToolFsmState::Drawing
|
||||
} else {
|
||||
GradientToolFsmState::Ready
|
||||
}
|
||||
}
|
||||
}
|
||||
(GradientToolFsmState::Drawing, GradientToolMessage::PointerMove { constrain_axis }) => {
|
||||
if let Some(selected_gradient) = &mut data.selected_gradient {
|
||||
let mouse = data.snap_handler.snap_position(responses, input.viewport_bounds.size(), document, input.mouse.position);
|
||||
selected_gradient.update_gradient(mouse, responses, input.keyboard.get(constrain_axis as usize));
|
||||
}
|
||||
GradientToolFsmState::Drawing
|
||||
}
|
||||
|
||||
(GradientToolFsmState::Drawing, GradientToolMessage::PointerUp) => {
|
||||
data.snap_handler.cleanup(responses);
|
||||
|
||||
GradientToolFsmState::Ready
|
||||
}
|
||||
|
||||
(_, GradientToolMessage::Abort) => {
|
||||
data.snap_handler.cleanup(responses);
|
||||
|
||||
while let Some(overlay) = data.gradient_overlays.pop() {
|
||||
overlay.delete_overlays(responses);
|
||||
}
|
||||
GradientToolFsmState::Ready
|
||||
}
|
||||
_ => self,
|
||||
}
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn update_hints(&self, responses: &mut VecDeque<Message>) {
|
||||
let hint_data = match self {
|
||||
GradientToolFsmState::Ready => HintData(vec![HintGroup(vec![
|
||||
HintInfo {
|
||||
key_groups: vec![],
|
||||
mouse: Some(MouseMotion::LmbDrag),
|
||||
label: String::from("Draw Gradient"),
|
||||
plus: false,
|
||||
},
|
||||
HintInfo {
|
||||
key_groups: vec![KeysGroup(vec![Key::KeyShift])],
|
||||
mouse: None,
|
||||
label: String::from("Snap 15°"),
|
||||
plus: true,
|
||||
},
|
||||
])]),
|
||||
GradientToolFsmState::Drawing => HintData(vec![HintGroup(vec![HintInfo {
|
||||
key_groups: vec![KeysGroup(vec![Key::KeyShift])],
|
||||
mouse: None,
|
||||
label: String::from("Snap 15°"),
|
||||
plus: false,
|
||||
}])]),
|
||||
};
|
||||
|
||||
responses.push_back(FrontendMessage::UpdateInputHints { hint_data }.into());
|
||||
}
|
||||
|
||||
fn update_cursor(&self, responses: &mut VecDeque<Message>) {
|
||||
responses.push_back(FrontendMessage::UpdateMouseCursor { cursor: MouseCursorIcon::Default }.into());
|
||||
}
|
||||
}
|
||||
@@ -169,7 +169,7 @@ impl Fsm for LineToolFsmState {
|
||||
path: data.path.clone().unwrap(),
|
||||
insert_index: -1,
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), None),
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), style::Fill::None),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod ellipse_tool;
|
||||
pub mod eyedropper_tool;
|
||||
pub mod fill_tool;
|
||||
pub mod freehand_tool;
|
||||
pub mod gradient_tool;
|
||||
pub mod line_tool;
|
||||
pub mod navigate_tool;
|
||||
pub mod path_tool;
|
||||
|
||||
@@ -182,7 +182,7 @@ impl Fsm for PenToolFsmState {
|
||||
transform: transform.to_cols_array(),
|
||||
insert_index: -1,
|
||||
bez_path: data.bez_path.clone().into_iter().collect(),
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), None),
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), style::Fill::None),
|
||||
closed: false,
|
||||
}
|
||||
.into(),
|
||||
|
||||
@@ -119,7 +119,7 @@ impl Fsm for RectangleToolFsmState {
|
||||
path: shape_data.path.clone().unwrap(),
|
||||
insert_index: -1,
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
style: style::PathStyle::new(None, Some(style::Fill::new(tool_data.primary_color))),
|
||||
style: style::PathStyle::new(None, style::Fill::solid(tool_data.primary_color)),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
|
||||
@@ -162,7 +162,7 @@ impl Fsm for ShapeToolFsmState {
|
||||
insert_index: -1,
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
sides: data.sides,
|
||||
style: style::PathStyle::new(None, Some(style::Fill::new(tool_data.primary_color))),
|
||||
style: style::PathStyle::new(None, style::Fill::solid(tool_data.primary_color)),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
|
||||
@@ -127,7 +127,7 @@ pub fn add_bounding_box(responses: &mut Vec<Message>) -> Vec<LayerId> {
|
||||
let operation = Operation::AddOverlayRect {
|
||||
path: path.clone(),
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), None),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), Fill::None),
|
||||
};
|
||||
responses.push(DocumentMessage::Overlays(operation.into()).into());
|
||||
|
||||
@@ -145,7 +145,7 @@ fn add_transform_handles(responses: &mut Vec<Message>) -> [Vec<LayerId>; 8] {
|
||||
let operation = Operation::AddOverlayRect {
|
||||
path: current_path.clone(),
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 2.0)), Some(Fill::new(Color::WHITE))),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 2.0)), Fill::solid(Color::WHITE)),
|
||||
};
|
||||
responses.push(DocumentMessage::Overlays(operation.into()).into());
|
||||
|
||||
|
||||
@@ -266,7 +266,7 @@ fn add_spline(data: &SplineToolData, tool_data: &DocumentToolData, show_preview:
|
||||
insert_index: -1,
|
||||
transform: DAffine2::IDENTITY.to_cols_array(),
|
||||
points,
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), None),
|
||||
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), style::Fill::None),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ fn resize_overlays(overlays: &mut Vec<Vec<LayerId>>, responses: &mut VecDeque<Me
|
||||
let operation = Operation::AddOverlayRect {
|
||||
path,
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), None),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), Fill::None),
|
||||
};
|
||||
responses.push_back(DocumentMessage::Overlays(operation.into()).into());
|
||||
}
|
||||
@@ -253,7 +253,7 @@ impl Fsm for TextToolFsmState {
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
insert_index: -1,
|
||||
text: r#""#.to_string(),
|
||||
style: style::PathStyle::new(None, Some(Fill::new(tool_data.primary_color))),
|
||||
style: style::PathStyle::new(None, Fill::solid(tool_data.primary_color)),
|
||||
size: font_size as f64,
|
||||
}
|
||||
.into(),
|
||||
|
||||
@@ -63,7 +63,7 @@ impl VectorControlPoint {
|
||||
DocumentMessage::Overlays(
|
||||
Operation::SetLayerStyle {
|
||||
path: overlay_path.clone(),
|
||||
style: PathStyle::new(Some(Stroke::new(stroke_color, stroke_width)), Some(Fill::new(fill_color))),
|
||||
style: PathStyle::new(Some(Stroke::new(stroke_color, stroke_width)), Fill::solid(fill_color)),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
|
||||
@@ -355,7 +355,7 @@ impl VectorShape {
|
||||
let operation = Operation::AddOverlayShape {
|
||||
path: layer_path.clone(),
|
||||
bez_path: self.bez_path.clone(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), None),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), Fill::None),
|
||||
closed: false,
|
||||
};
|
||||
responses.push_back(DocumentMessage::Overlays(operation.into()).into());
|
||||
@@ -369,7 +369,7 @@ impl VectorShape {
|
||||
let operation = Operation::AddOverlayRect {
|
||||
path: layer_path.clone(),
|
||||
transform: DAffine2::IDENTITY.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 2.0)), Some(Fill::new(Color::WHITE))),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 2.0)), Fill::solid(Color::WHITE)),
|
||||
};
|
||||
responses.push_back(DocumentMessage::Overlays(operation.into()).into());
|
||||
layer_path
|
||||
@@ -381,7 +381,7 @@ impl VectorShape {
|
||||
let operation = Operation::AddOverlayEllipse {
|
||||
path: layer_path.clone(),
|
||||
transform: DAffine2::IDENTITY.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 2.0)), Some(Fill::new(Color::WHITE))),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 2.0)), Fill::solid(Color::WHITE)),
|
||||
};
|
||||
responses.push_back(DocumentMessage::Overlays(operation.into()).into());
|
||||
layer_path
|
||||
@@ -397,7 +397,7 @@ impl VectorShape {
|
||||
let operation = Operation::AddOverlayLine {
|
||||
path: layer_path.clone(),
|
||||
transform: DAffine2::IDENTITY.to_cols_array(),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), None),
|
||||
style: style::PathStyle::new(Some(Stroke::new(COLOR_ACCENT, 1.0)), style::Fill::None),
|
||||
};
|
||||
responses.push_front(DocumentMessage::Overlays(operation.into()).into());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user