use renamite_animation::{Animated, AnimatedTransform};
use renamite_history::{EditorCommand, NodeTree};
use renamite_model::{Document, ModifierKind, Node, NodeId, NodeKind, Parent, TrimMode};
pub fn cmd_add_trim_path_to(parent: NodeId) -> EditorCommand {
EditorCommand::InsertNode {
parent: Parent::Node(parent),
index: usize::MAX,
tree: NodeTree::leaf(Node::new(
"Trim Path",
NodeKind::Modifier(ModifierKind::TrimPath {
start: Animated::new(0.0),
end: Animated::new(1.0),
offset: Animated::new(0.0),
mode: TrimMode::Individually,
}),
)),
}
}
pub fn cmd_add_trim_path_after(doc: &Document, after: NodeId) -> Option<EditorCommand> {
let (parent, index) = doc.locate(after)?;
Some(EditorCommand::InsertNode {
parent,
index: index + 1,
tree: NodeTree::leaf(Node::new(
"Trim Path",
NodeKind::Modifier(ModifierKind::TrimPath {
start: Animated::new(0.0),
end: Animated::new(1.0),
offset: Animated::new(0.0),
mode: TrimMode::Individually,
}),
)),
})
}
pub fn cmd_add_round_corners_after(
doc: &Document,
after: NodeId,
radius: f64,
) -> Option<EditorCommand> {
let (parent, index) = doc.locate(after)?;
Some(EditorCommand::InsertNode {
parent,
index: index + 1,
tree: NodeTree::leaf(Node::new(
"Round Corners",
NodeKind::Modifier(ModifierKind::RoundCorners {
radius: Animated::new(radius),
}),
)),
})
}
pub fn cmd_add_offset_path_after(
doc: &Document,
after: NodeId,
amount: f64,
) -> Option<EditorCommand> {
let (parent, index) = doc.locate(after)?;
Some(EditorCommand::InsertNode {
parent,
index: index + 1,
tree: NodeTree::leaf(Node::new(
"Offset Path",
NodeKind::Modifier(ModifierKind::OffsetPath {
amount: Animated::new(amount),
}),
)),
})
}
pub fn cmd_add_repeater_after(doc: &Document, after: NodeId) -> Option<EditorCommand> {
let (parent, index) = doc.locate(after)?;
let mut step = AnimatedTransform::identity();
step.position = Animated::new(glam::DVec2::new(50.0, 0.0));
Some(EditorCommand::InsertNode {
parent,
index: index + 1,
tree: NodeTree::leaf(Node::new(
"Repeater",
NodeKind::Modifier(ModifierKind::Repeater {
copies: Animated::new(3.0),
offset: Animated::new(0.0),
transform: step,
start_opacity: Animated::new(1.0),
end_opacity: Animated::new(1.0),
}),
)),
})
}
pub fn cmd_add_zigzag_after(doc: &Document, after: NodeId) -> Option<EditorCommand> {
let (parent, index) = doc.locate(after)?;
Some(EditorCommand::InsertNode {
parent,
index: index + 1,
tree: NodeTree::leaf(Node::new(
"Zig Zag",
NodeKind::Modifier(ModifierKind::ZigZag {
amplitude: Animated::new(10.0),
frequency: Animated::new(4.0),
smooth: false,
}),
)),
})
}
pub fn cmd_add_pucker_bloat_after(
doc: &Document,
after: NodeId,
amount: f64,
) -> Option<EditorCommand> {
let (parent, index) = doc.locate(after)?;
Some(EditorCommand::InsertNode {
parent,
index: index + 1,
tree: NodeTree::leaf(Node::new(
"Pucker & Bloat",
NodeKind::Modifier(ModifierKind::PuckerBloat {
amount: Animated::new(amount),
}),
)),
})
}
pub fn get_trim_mode(doc: &Document, id: NodeId) -> Option<TrimMode> {
match &doc.nodes.get(id)?.kind {
NodeKind::Modifier(ModifierKind::TrimPath { mode, .. }) => Some(*mode),
_ => None,
}
}
pub fn cmd_set_trim_mode(doc: &Document, id: NodeId, mode: TrimMode) -> Option<EditorCommand> {
if get_trim_mode(doc, id)? == mode {
return None;
}
Some(EditorCommand::SetTrimMode { id, mode })
}
pub fn cmd_toggle_trim_mode(doc: &Document, id: NodeId) -> Option<EditorCommand> {
let cur = get_trim_mode(doc, id)?;
let next = match cur {
TrimMode::Individually => TrimMode::Simultaneously,
TrimMode::Simultaneously => TrimMode::Individually,
};
cmd_set_trim_mode(doc, id, next)
}