mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-28 03:08:12 +08:00
Integrate the node graph as a Node Graph Frame layer type (#812)
* Add node graph frame tool * Add a brighten * Use the node graph * Fix topological_sort * Update UI * Add icons for the tool and layer type * Avoid serde & use bitmaps to improve performance * Allow serialising a node graph * Fix missing ..Default::default() * Fix incorrect comments * Cache node graph output image * Suppress no-cycle import warning Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
Keavon Chambers
parent
596b9f4531
commit
9cdebfb1f0
@@ -78,6 +78,9 @@ pub enum ToolMessage {
|
||||
#[remain::unsorted]
|
||||
#[child]
|
||||
Imaginate(ImaginateToolMessage),
|
||||
#[remain::unsorted]
|
||||
#[child]
|
||||
NodeGraphFrame(NodeGraphFrameToolMessage),
|
||||
|
||||
// Messages
|
||||
#[remain::unsorted]
|
||||
@@ -114,6 +117,8 @@ pub enum ToolMessage {
|
||||
|
||||
#[remain::unsorted]
|
||||
ActivateToolImaginate,
|
||||
#[remain::unsorted]
|
||||
ActivateToolNodeGraphFrame,
|
||||
|
||||
ActivateTool {
|
||||
tool_type: ToolType,
|
||||
|
||||
@@ -7,6 +7,7 @@ pub mod gradient_tool;
|
||||
pub mod imaginate_tool;
|
||||
pub mod line_tool;
|
||||
pub mod navigate_tool;
|
||||
pub mod node_graph_frame_tool;
|
||||
pub mod path_tool;
|
||||
pub mod pen_tool;
|
||||
pub mod rectangle_tool;
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
use crate::consts::DRAG_THRESHOLD;
|
||||
use crate::messages::frontend::utility_types::MouseCursorIcon;
|
||||
use crate::messages::input_mapper::utility_types::input_keyboard::{Key, KeysGroup, MouseMotion};
|
||||
use crate::messages::layout::utility_types::layout_widget::PropertyHolder;
|
||||
use crate::messages::prelude::*;
|
||||
use crate::messages::tool::common_functionality::resize::Resize;
|
||||
use crate::messages::tool::utility_types::{EventToMessageMap, Fsm, ToolActionHandlerData, ToolMetadata, ToolTransition, ToolType};
|
||||
use crate::messages::tool::utility_types::{HintData, HintGroup, HintInfo};
|
||||
|
||||
use graphene::Operation;
|
||||
|
||||
use glam::DAffine2;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct NodeGraphFrameTool {
|
||||
fsm_state: NodeGraphToolFsmState,
|
||||
tool_data: NodeGraphToolData,
|
||||
}
|
||||
|
||||
#[remain::sorted]
|
||||
#[impl_message(Message, ToolMessage, NodeGraphFrame)]
|
||||
#[derive(PartialEq, Eq, Clone, Debug, Hash, Serialize, Deserialize)]
|
||||
pub enum NodeGraphFrameToolMessage {
|
||||
// Standard messages
|
||||
#[remain::unsorted]
|
||||
Abort,
|
||||
|
||||
// Tool-specific messages
|
||||
DragStart,
|
||||
DragStop,
|
||||
Resize {
|
||||
center: Key,
|
||||
lock_ratio: Key,
|
||||
},
|
||||
}
|
||||
|
||||
impl PropertyHolder for NodeGraphFrameTool {}
|
||||
|
||||
impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for NodeGraphFrameTool {
|
||||
fn process_message(&mut self, message: ToolMessage, tool_data: ToolActionHandlerData<'a>, responses: &mut VecDeque<Message>) {
|
||||
if message == ToolMessage::UpdateHints {
|
||||
self.fsm_state.update_hints(responses);
|
||||
return;
|
||||
}
|
||||
|
||||
if message == ToolMessage::UpdateCursor {
|
||||
self.fsm_state.update_cursor(responses);
|
||||
return;
|
||||
}
|
||||
|
||||
let new_state = self.fsm_state.transition(message, &mut self.tool_data, tool_data, &(), responses);
|
||||
|
||||
if self.fsm_state != new_state {
|
||||
self.fsm_state = new_state;
|
||||
self.fsm_state.update_hints(responses);
|
||||
self.fsm_state.update_cursor(responses);
|
||||
}
|
||||
}
|
||||
|
||||
fn actions(&self) -> ActionList {
|
||||
use NodeGraphToolFsmState::*;
|
||||
|
||||
match self.fsm_state {
|
||||
Ready => actions!(NodeGraphFrameToolMessageDiscriminant;
|
||||
DragStart,
|
||||
),
|
||||
Drawing => actions!(NodeGraphFrameToolMessageDiscriminant;
|
||||
DragStop,
|
||||
Abort,
|
||||
Resize,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolMetadata for NodeGraphFrameTool {
|
||||
fn icon_name(&self) -> String {
|
||||
"RasterNodesTool".into()
|
||||
}
|
||||
fn tooltip(&self) -> String {
|
||||
"Node Graph Frame Tool".into()
|
||||
}
|
||||
fn tool_type(&self) -> crate::messages::tool::utility_types::ToolType {
|
||||
ToolType::NodeGraphFrame
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolTransition for NodeGraphFrameTool {
|
||||
fn event_to_message_map(&self) -> EventToMessageMap {
|
||||
EventToMessageMap {
|
||||
document_dirty: None,
|
||||
tool_abort: Some(NodeGraphFrameToolMessage::Abort.into()),
|
||||
selection_changed: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum NodeGraphToolFsmState {
|
||||
Ready,
|
||||
Drawing,
|
||||
}
|
||||
|
||||
impl Default for NodeGraphToolFsmState {
|
||||
fn default() -> Self {
|
||||
NodeGraphToolFsmState::Ready
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct NodeGraphToolData {
|
||||
data: Resize,
|
||||
}
|
||||
|
||||
impl Fsm for NodeGraphToolFsmState {
|
||||
type ToolData = NodeGraphToolData;
|
||||
type ToolOptions = ();
|
||||
|
||||
fn transition(
|
||||
self,
|
||||
event: ToolMessage,
|
||||
tool_data: &mut Self::ToolData,
|
||||
(document, _document_id, _global_tool_data, input, font_cache): ToolActionHandlerData,
|
||||
_tool_options: &Self::ToolOptions,
|
||||
responses: &mut VecDeque<Message>,
|
||||
) -> Self {
|
||||
use NodeGraphFrameToolMessage::*;
|
||||
use NodeGraphToolFsmState::*;
|
||||
|
||||
let mut shape_data = &mut tool_data.data;
|
||||
|
||||
if let ToolMessage::NodeGraphFrame(event) = event {
|
||||
match (self, event) {
|
||||
(Ready, DragStart) => {
|
||||
shape_data.start(responses, document, input.mouse.position, font_cache);
|
||||
responses.push_back(DocumentMessage::StartTransaction.into());
|
||||
shape_data.path = Some(document.get_path_for_new_layer());
|
||||
responses.push_back(DocumentMessage::DeselectAllLayers.into());
|
||||
|
||||
responses.push_back(
|
||||
Operation::AddNodeGraphFrame {
|
||||
path: shape_data.path.clone().unwrap(),
|
||||
insert_index: -1,
|
||||
transform: DAffine2::ZERO.to_cols_array(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
|
||||
Drawing
|
||||
}
|
||||
(state, Resize { center, lock_ratio }) => {
|
||||
if let Some(message) = shape_data.calculate_transform(responses, document, center, lock_ratio, input) {
|
||||
responses.push_back(message);
|
||||
}
|
||||
|
||||
state
|
||||
}
|
||||
(Drawing, DragStop) => {
|
||||
match shape_data.drag_start.distance(input.mouse.position) <= DRAG_THRESHOLD {
|
||||
true => responses.push_back(DocumentMessage::AbortTransaction.into()),
|
||||
false => responses.push_back(DocumentMessage::CommitTransaction.into()),
|
||||
}
|
||||
|
||||
shape_data.cleanup(responses);
|
||||
|
||||
Ready
|
||||
}
|
||||
(Drawing, Abort) => {
|
||||
responses.push_back(DocumentMessage::AbortTransaction.into());
|
||||
|
||||
shape_data.cleanup(responses);
|
||||
|
||||
Ready
|
||||
}
|
||||
_ => self,
|
||||
}
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn update_hints(&self, responses: &mut VecDeque<Message>) {
|
||||
let hint_data = match self {
|
||||
NodeGraphToolFsmState::Ready => HintData(vec![HintGroup(vec![
|
||||
HintInfo {
|
||||
key_groups: vec![],
|
||||
key_groups_mac: None,
|
||||
mouse: Some(MouseMotion::LmbDrag),
|
||||
label: String::from("Draw Repaint Frame"),
|
||||
plus: false,
|
||||
},
|
||||
HintInfo {
|
||||
key_groups: vec![KeysGroup(vec![Key::Shift])],
|
||||
key_groups_mac: None,
|
||||
mouse: None,
|
||||
label: String::from("Constrain Square"),
|
||||
plus: true,
|
||||
},
|
||||
HintInfo {
|
||||
key_groups: vec![KeysGroup(vec![Key::Alt])],
|
||||
key_groups_mac: None,
|
||||
mouse: None,
|
||||
label: String::from("From Center"),
|
||||
plus: true,
|
||||
},
|
||||
])]),
|
||||
NodeGraphToolFsmState::Drawing => HintData(vec![HintGroup(vec![
|
||||
HintInfo {
|
||||
key_groups: vec![KeysGroup(vec![Key::Shift])],
|
||||
key_groups_mac: None,
|
||||
mouse: None,
|
||||
label: String::from("Constrain Square"),
|
||||
plus: false,
|
||||
},
|
||||
HintInfo {
|
||||
key_groups: vec![KeysGroup(vec![Key::Alt])],
|
||||
key_groups_mac: None,
|
||||
mouse: None,
|
||||
label: String::from("From Center"),
|
||||
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::Crosshair }.into());
|
||||
}
|
||||
}
|
||||
@@ -281,6 +281,7 @@ pub enum ToolType {
|
||||
Detail,
|
||||
Relight,
|
||||
Imaginate,
|
||||
NodeGraphFrame,
|
||||
}
|
||||
|
||||
enum ToolAvailability {
|
||||
@@ -293,28 +294,29 @@ fn list_tools_in_groups() -> Vec<Vec<ToolAvailability>> {
|
||||
vec![
|
||||
vec![
|
||||
// General tool group
|
||||
ToolAvailability::Available(Box::new(select_tool::SelectTool::default())),
|
||||
ToolAvailability::Available(Box::new(artboard_tool::ArtboardTool::default())),
|
||||
ToolAvailability::Available(Box::new(navigate_tool::NavigateTool::default())),
|
||||
ToolAvailability::Available(Box::new(eyedropper_tool::EyedropperTool::default())),
|
||||
ToolAvailability::Available(Box::new(fill_tool::FillTool::default())),
|
||||
ToolAvailability::Available(Box::new(gradient_tool::GradientTool::default())),
|
||||
ToolAvailability::Available(Box::<select_tool::SelectTool>::default()),
|
||||
ToolAvailability::Available(Box::<artboard_tool::ArtboardTool>::default()),
|
||||
ToolAvailability::Available(Box::<navigate_tool::NavigateTool>::default()),
|
||||
ToolAvailability::Available(Box::<eyedropper_tool::EyedropperTool>::default()),
|
||||
ToolAvailability::Available(Box::<fill_tool::FillTool>::default()),
|
||||
ToolAvailability::Available(Box::<gradient_tool::GradientTool>::default()),
|
||||
],
|
||||
vec![
|
||||
// Vector tool group
|
||||
ToolAvailability::Available(Box::new(path_tool::PathTool::default())),
|
||||
ToolAvailability::Available(Box::new(pen_tool::PenTool::default())),
|
||||
ToolAvailability::Available(Box::new(freehand_tool::FreehandTool::default())),
|
||||
ToolAvailability::Available(Box::new(spline_tool::SplineTool::default())),
|
||||
ToolAvailability::Available(Box::new(line_tool::LineTool::default())),
|
||||
ToolAvailability::Available(Box::new(rectangle_tool::RectangleTool::default())),
|
||||
ToolAvailability::Available(Box::new(ellipse_tool::EllipseTool::default())),
|
||||
ToolAvailability::Available(Box::new(shape_tool::ShapeTool::default())),
|
||||
ToolAvailability::Available(Box::new(text_tool::TextTool::default())),
|
||||
ToolAvailability::Available(Box::<path_tool::PathTool>::default()),
|
||||
ToolAvailability::Available(Box::<pen_tool::PenTool>::default()),
|
||||
ToolAvailability::Available(Box::<freehand_tool::FreehandTool>::default()),
|
||||
ToolAvailability::Available(Box::<spline_tool::SplineTool>::default()),
|
||||
ToolAvailability::Available(Box::<line_tool::LineTool>::default()),
|
||||
ToolAvailability::Available(Box::<rectangle_tool::RectangleTool>::default()),
|
||||
ToolAvailability::Available(Box::<ellipse_tool::EllipseTool>::default()),
|
||||
ToolAvailability::Available(Box::<shape_tool::ShapeTool>::default()),
|
||||
ToolAvailability::Available(Box::<text_tool::TextTool>::default()),
|
||||
],
|
||||
vec![
|
||||
// Raster tool group
|
||||
ToolAvailability::Available(Box::new(imaginate_tool::ImaginateTool::default())),
|
||||
ToolAvailability::Available(Box::<imaginate_tool::ImaginateTool>::default()),
|
||||
ToolAvailability::Available(Box::<node_graph_frame_tool::NodeGraphFrameTool>::default()),
|
||||
ToolAvailability::ComingSoon(ToolEntry {
|
||||
tool_type: ToolType::Brush,
|
||||
icon_name: "RasterBrushTool".into(),
|
||||
@@ -384,6 +386,7 @@ pub fn tool_message_to_tool_type(tool_message: &ToolMessage) -> ToolType {
|
||||
// ToolMessage::Detail(_) => ToolType::Detail,
|
||||
// ToolMessage::Relight(_) => ToolType::Relight,
|
||||
ToolMessage::Imaginate(_) => ToolType::Imaginate,
|
||||
ToolMessage::NodeGraphFrame(_) => ToolType::NodeGraphFrame,
|
||||
_ => panic!(
|
||||
"Conversion from ToolMessage to ToolType impossible because the given ToolMessage does not have a matching ToolType. Got: {:?}",
|
||||
tool_message
|
||||
@@ -420,6 +423,7 @@ pub fn tool_type_to_activate_tool_message(tool_type: ToolType) -> ToolMessageDis
|
||||
// ToolType::Detail => ToolMessageDiscriminant::ActivateToolDetail,
|
||||
// ToolType::Relight => ToolMessageDiscriminant::ActivateToolRelight,
|
||||
ToolType::Imaginate => ToolMessageDiscriminant::ActivateToolImaginate,
|
||||
ToolType::NodeGraphFrame => ToolMessageDiscriminant::ActivateToolNodeGraphFrame,
|
||||
_ => panic!(
|
||||
"Conversion from ToolType to ToolMessage impossible because the given ToolType does not have a matching ToolMessage. Got: {:?}",
|
||||
tool_type
|
||||
|
||||
Reference in New Issue
Block a user