M13.1/M13.2: paint (Brush, Clone Stamp, Eraser) and puppet pins
Paint: non-destructive Paint effect with Brush/Clone/Eraser stroke groups (Path, Stroke Options, Transform, Duration spans, Write On End keys), dab rasterizer (diameter/angle/roundness/hardness/spacing/flow/opacity, pressure dynamics, modes, channels), eraser modes, clone from layer/time, cache keys include visible strokes. paint.stroke/options/brushPreset/ setCloneSource/removeStroke/presets. Layer panel with paint tools, Paint and Brushes panels. Puppet: Puppet effect with meshes and Position/Advanced/Bend/Starch/Overlap pins; alpha contour -> expansion (exact EDT) -> simplified outline -> Delaunay with constraint recovery; ARAP (Igarashi 2005) two-step solve with CG; anti-aliased texture-mapped rendering with overlap depth. puppet.addPin/ movePin/setPin/removePin/mesh/info. Puppet tools in the viewer with mesh overlay and header options. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5273f1ba96
commit
d6f6a10757
@@ -109,6 +109,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
x = r.max.x + 14.0;
|
||||
}
|
||||
if app.ui.tool.puppet_kind().is_some() {
|
||||
x = puppet_options(app, ui, &p, x, cy);
|
||||
}
|
||||
let snap = Rect::from_min_size(pos2(x, cy - 10.0), vec2(20.0, 20.0));
|
||||
if widgets::checkbox(ui, snap, app.ui.snapping, &t, egui::Id::new("snapping")).clicked() {
|
||||
app.ui.snapping = !app.ui.snapping;
|
||||
@@ -192,6 +195,33 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Puppet tool options: Mesh: Show, Expansion, Density (for new meshes and the selected
|
||||
/// layer's meshes). Returns the next x.
|
||||
fn puppet_options(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, mut x: f32, cy: f32) -> f32 {
|
||||
let t = app.tokens;
|
||||
let o = app.session.state.puppet.clone();
|
||||
p.text(pos2(x, cy), Align2::LEFT_CENTER, "Mesh:", Tokens::ui(12.0), t.text_dim);
|
||||
x += 40.0;
|
||||
let show = Rect::from_min_size(pos2(x, cy - 10.0), vec2(20.0, 20.0));
|
||||
if widgets::checkbox(ui, show, o.show_mesh, &t, egui::Id::new("puppet-show-mesh")).clicked() {
|
||||
let _ = app.session.execute("puppet.mesh", json!({"showMesh": !o.show_mesh}));
|
||||
}
|
||||
app.auto.add("header.puppet.showMesh", show, "Show mesh");
|
||||
p.text(pos2(show.max.x + 2.0, cy), Align2::LEFT_CENTER, "Show", Tokens::ui(12.0), t.text_dim);
|
||||
x = show.max.x + 44.0;
|
||||
for (label, key, v, range) in [("Expansion:", "expansion", o.expansion, (-100.0, 200.0)), ("Density:", "density", o.density, (0.0, 100.0))] {
|
||||
p.text(pos2(x, cy), Align2::LEFT_CENTER, label, Tokens::ui(12.0), t.text_dim);
|
||||
x += if key == "expansion" { 64.0 } else { 52.0 };
|
||||
let (r, nv, _) = widgets::hot_number_at(ui, pos2(x, cy - 9.0), egui::Id::new(("puppet-opt", key)), v, 0.2, range, 0, "", &t);
|
||||
app.auto.add(&format!("header.puppet.{key}"), r, label);
|
||||
if let Some(nv) = nv {
|
||||
let _ = app.session.execute("puppet.mesh", json!({key: nv, "merge": format!("puppet-opt-{key}")}));
|
||||
}
|
||||
x = r.max.x + 12.0;
|
||||
}
|
||||
x + 4.0
|
||||
}
|
||||
|
||||
fn ws_menu(app: &mut EffectcraftApp, ui: &mut egui::Ui) {
|
||||
for w in crate::dock::WORKSPACES {
|
||||
if ui.selectable_label(app.ui.workspace == w, w).clicked() {
|
||||
|
||||
@@ -133,6 +133,7 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
|
||||
"shape" => Some(8),
|
||||
"type" => Some(10),
|
||||
"brush" => Some(11),
|
||||
"puppet" => Some(15),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(si) = slot {
|
||||
@@ -447,6 +448,9 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
|
||||
Value::Null
|
||||
}
|
||||
"layer.openLayer" => {
|
||||
let id = p.get("layer").and_then(Value::as_u64).or_else(|| app.session.state.selected_layers.first().map(|l| l.0));
|
||||
app.ui.layer_panel = id;
|
||||
app.ui.layer_view = None;
|
||||
app.show_panel(PanelKind::Layer);
|
||||
Value::Null
|
||||
}
|
||||
|
||||
@@ -248,6 +248,10 @@ fn group_rows(
|
||||
}
|
||||
}
|
||||
Node::Group(sg) => {
|
||||
// Paint strokes live in the Timeline only (AE's Paint shows Paint on Transparent).
|
||||
if effectcraft_engine::effects::paint::is_paint(g) {
|
||||
continue;
|
||||
}
|
||||
*y += 24.0;
|
||||
let open = !app.ui.fx_closed.contains(&sg.uid);
|
||||
let tw = Rect::from_center_size(pos2(r.min.x + 12.0 + 14.0 * depth as f32, r.center().y), vec2(12.0, 12.0));
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
//! The Layer panel: one layer in its own (layer) space, where the Brush, Clone Stamp and Eraser
|
||||
//! tools paint. Double-clicking a footage or solid layer (or Layer ▸ Open Layer) opens it.
|
||||
//!
|
||||
//! The image is the layer's source and masks followed by its effects up to the View menu's
|
||||
//! choice (by default the last Paint effect, as After Effects shows "Paint"). Dragging with a
|
||||
//! paint tool records the stroke's points in layer space and, on release, runs `paint.stroke`
|
||||
//! at the current time (the drag's duration feeds Write On). Alt/Option-click with the Clone
|
||||
//! Stamp sets the clone source.
|
||||
|
||||
use effectcraft_engine::effects::paint;
|
||||
use effectcraft_engine::project::{Layer, LayerId};
|
||||
use effectcraft_engine::render::{EvalCtx, RenderOpts, Renderer};
|
||||
use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::state::Tool;
|
||||
use crate::theme::Tokens;
|
||||
use crate::{EffectcraftApp, widgets};
|
||||
|
||||
/// A stroke being drawn.
|
||||
#[derive(Clone, Debug)]
|
||||
struct Drag {
|
||||
layer: u64,
|
||||
points: Vec<[f64; 2]>,
|
||||
started: f64,
|
||||
}
|
||||
|
||||
/// Cached texture of the rendered layer: (revision, layer, time, view, scale) → texture and the
|
||||
/// layer-space rectangle it covers.
|
||||
#[derive(Clone)]
|
||||
struct Tex {
|
||||
key: (u64, u64, i64, usize, u32),
|
||||
tex: egui::TextureHandle,
|
||||
/// Layer-space rect of the image `[x0, y0, x1, y1]`.
|
||||
rect: [f64; 4],
|
||||
}
|
||||
|
||||
fn tex_id() -> egui::Id {
|
||||
egui::Id::new("layer-panel-tex")
|
||||
}
|
||||
|
||||
/// The layer shown: the opened one, else the first selected layer.
|
||||
pub fn current_layer(app: &EffectcraftApp) -> Option<Layer> {
|
||||
let comp = app.session.active_comp()?;
|
||||
let id = app.ui.layer_panel.map(LayerId).filter(|id| comp.layer(*id).is_some()).or_else(|| app.session.state.selected_layers.first().copied())?;
|
||||
comp.layer(id).cloned()
|
||||
}
|
||||
|
||||
/// Number of effects shown by default: through the last Paint effect (none when there isn't one).
|
||||
fn default_view(layer: &Layer) -> usize {
|
||||
layer.effects().and_then(|fx| fx.groups().enumerate().filter(|(_, g)| paint::is_paint(g)).map(|(i, _)| i + 1).last()).unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
let t = app.tokens;
|
||||
let ctx = ui.ctx().clone();
|
||||
let p = ui.painter().with_clip_rect(rect);
|
||||
let (Some(cid), Some(layer)) = (app.session.active_comp_id(), current_layer(app)) else {
|
||||
p.rect_filled(rect, 0.0, t.pasteboard);
|
||||
p.text(rect.center(), Align2::CENTER_CENTER, "Double-click a layer to open it in the Layer panel", Tokens::ui(12.0), t.text_faint);
|
||||
app.auto.add("layerPanel.empty", rect, "Layer panel (empty)");
|
||||
return;
|
||||
};
|
||||
let comp = app.session.project.comp(cid).cloned().unwrap_or_else(|| effectcraft_engine::project::Comp::new(1, 1, Default::default(), Default::default()));
|
||||
let time = app.session.time();
|
||||
let fx_count = layer.effects().map(|f| f.groups().count()).unwrap_or(0);
|
||||
let view = app.ui.layer_view.map(|v| v.min(fx_count)).unwrap_or_else(|| default_view(&layer));
|
||||
|
||||
// Title strip.
|
||||
let top = Rect::from_min_size(rect.min, vec2(rect.width(), 24.0));
|
||||
p.rect_filled(top, 0.0, t.panel_bg);
|
||||
p.text(pos2(top.min.x + 10.0, top.center().y), Align2::LEFT_CENTER, format!("Layer: {}", layer.name), Tokens::semibold(12.0), t.text);
|
||||
app.auto.add("layerPanel.title", top, &layer.name);
|
||||
// Bottom bar: View menu.
|
||||
let bar = Rect::from_min_max(pos2(rect.min.x, rect.max.y - 30.0), rect.max);
|
||||
p.rect_filled(bar, 0.0, t.panel_bg);
|
||||
p.text(pos2(bar.min.x + 10.0, bar.center().y), Align2::LEFT_CENTER, "View:", Tokens::ui(12.0), t.text_dim);
|
||||
let mut names = vec!["Masks".to_string()];
|
||||
if let Some(fx) = layer.effects() {
|
||||
names.extend(fx.groups().map(|g| g.name.clone()));
|
||||
}
|
||||
let vr = Rect::from_min_size(pos2(bar.min.x + 46.0, bar.center().y - 10.0), vec2(150.0, 20.0));
|
||||
let pop = egui::Id::new("layer-view-pop");
|
||||
if widgets::dropdown(ui, vr, &names[view.min(names.len() - 1)], &t, egui::Id::new("layer-view")).clicked() {
|
||||
widgets::open_popup(ui, pop);
|
||||
}
|
||||
app.auto.add("layerPanel.view", vr, "View");
|
||||
if let Some(i) = widgets::popup_menu(ui, pop, vr.left_bottom(), &names, Some(view)) {
|
||||
app.ui.layer_view = Some(i);
|
||||
}
|
||||
let tc = super::timecode(&app.session, &comp, time);
|
||||
p.text(pos2(bar.max.x - 10.0, bar.center().y), Align2::RIGHT_CENTER, tc, Tokens::mono(12.0), t.timecode);
|
||||
|
||||
// Canvas.
|
||||
let area = Rect::from_min_max(pos2(rect.min.x, top.max.y), pos2(rect.max.x, bar.min.y));
|
||||
p.rect_filled(area, 0.0, t.pasteboard);
|
||||
let (w, h) = effectcraft_engine::render::source_size(&app.session.project, &layer);
|
||||
let (lw, lh) = if w == 0 { (comp.width as f64, comp.height as f64) } else { (w as f64, h as f64) };
|
||||
let zoom = ((area.width() - 40.0) as f64 / lw).min((area.height() - 40.0) as f64 / lh).max(0.01);
|
||||
let origin = area.center() - vec2((lw * zoom / 2.0) as f32, (lh * zoom / 2.0) as f32);
|
||||
let to_screen = |q: [f64; 2]| pos2(origin.x + (q[0] * zoom) as f32, origin.y + (q[1] * zoom) as f32);
|
||||
let to_layer = |s: Pos2| [((s.x - origin.x) as f64) / zoom, ((s.y - origin.y) as f64) / zoom];
|
||||
let frame = Rect::from_min_max(to_screen([0.0, 0.0]), to_screen([lw, lh]));
|
||||
let painter = p.with_clip_rect(area);
|
||||
super::viewer::checker(&painter, frame);
|
||||
|
||||
// Render (synchronously; the layer cache makes repeats cheap).
|
||||
let ppp = ctx.pixels_per_point() as f64;
|
||||
let scale = (zoom * ppp).clamp(0.05, 1.0);
|
||||
let scale = if scale > 0.75 {
|
||||
1.0
|
||||
} else if scale > 0.4 {
|
||||
0.5
|
||||
} else {
|
||||
0.25
|
||||
};
|
||||
let key = (app.session.revision, layer.id.0, time.0, view, (scale * 1000.0) as u32);
|
||||
let cached: Option<Tex> = ctx.data(|d| d.get_temp(tex_id()));
|
||||
let tex = match cached {
|
||||
Some(c) if c.key == key => Some(c),
|
||||
_ => {
|
||||
let ectx = EvalCtx { project: &app.session.project, comp_id: cid, comp: &comp, time, expr: app.session.expr.as_deref() };
|
||||
let mut r = Renderer::new(&app.session.project, &*app.session.footage, RenderOpts { scale, ..Default::default() });
|
||||
r.expr = app.session.expr.as_deref();
|
||||
r.cache = Some(&app.session.layer_cache);
|
||||
r.layer_input(&ectx, &layer, view).map(|buf| {
|
||||
let img = crate::frames::to_color_image(&buf.img);
|
||||
let x0 = -buf.offset[0] / buf.scale;
|
||||
let y0 = -buf.offset[1] / buf.scale;
|
||||
let rect = [x0, y0, x0 + buf.img.width as f64 / buf.scale, y0 + buf.img.height as f64 / buf.scale];
|
||||
let tex = ctx.load_texture("layer-panel", img, egui::TextureOptions::LINEAR);
|
||||
let c = Tex { key, tex, rect };
|
||||
ctx.data_mut(|d| d.insert_temp(tex_id(), c.clone()));
|
||||
c
|
||||
})
|
||||
}
|
||||
};
|
||||
if let Some(tx) = &tex {
|
||||
let r = Rect::from_min_max(to_screen([tx.rect[0], tx.rect[1]]), to_screen([tx.rect[2], tx.rect[3]]));
|
||||
painter.image(tx.tex.id(), r, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
|
||||
}
|
||||
painter.rect_stroke(frame, 0.0, Stroke::new(1.0, Color32::from_black_alpha(160)), StrokeKind::Outside);
|
||||
app.auto.add("layerPanel.canvas", frame, &layer.name);
|
||||
app.auto.add("layerPanel.area", area, "Layer panel");
|
||||
|
||||
// Interaction: paint tools.
|
||||
let resp = ui.interact(area, egui::Id::new("layer-panel-interact"), Sense::click_and_drag());
|
||||
let kind = app.ui.tool.paint_kind();
|
||||
let o = app.session.state.paint.clone();
|
||||
let did = egui::Id::new("layer-panel-drag");
|
||||
if let Some(hp) = resp.hover_pos() {
|
||||
let lp = to_layer(hp);
|
||||
app.pointer_comp = Some([lp[0] as f32, lp[1] as f32]);
|
||||
if kind.is_some() {
|
||||
ctx.set_cursor_icon(egui::CursorIcon::Crosshair);
|
||||
// Brush tip outline.
|
||||
let rad = (o.diameter * 0.5 * zoom) as f32;
|
||||
let ang = (o.angle as f32).to_radians();
|
||||
let rnd = (o.roundness / 100.0).clamp(0.01, 1.0) as f32;
|
||||
let pts: Vec<Pos2> = (0..48)
|
||||
.map(|i| {
|
||||
let a = i as f32 / 48.0 * std::f32::consts::TAU;
|
||||
let (x, y) = (a.cos() * rad, a.sin() * rad * rnd);
|
||||
hp + vec2(x * ang.cos() - y * ang.sin(), x * ang.sin() + y * ang.cos())
|
||||
})
|
||||
.collect();
|
||||
painter.add(egui::Shape::closed_line(pts, Stroke::new(1.0, Color32::from_white_alpha(200))));
|
||||
// Clone source overlay.
|
||||
if app.ui.tool == Tool::Clone
|
||||
&& o.show_overlay
|
||||
&& let (Some(tx), Some(src)) = (&tex, o.clone_point)
|
||||
{
|
||||
let off = o.clone_offset.unwrap_or([src[0] - lp[0], src[1] - lp[1]]);
|
||||
let r = Rect::from_min_max(to_screen([tx.rect[0] - off[0], tx.rect[1] - off[1]]), to_screen([tx.rect[2] - off[0], tx.rect[3] - off[1]]));
|
||||
let a = (o.overlay_opacity / 100.0 * 255.0).clamp(0.0, 255.0) as u8;
|
||||
painter.with_clip_rect(frame).image(tx.tex.id(), r, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::from_white_alpha(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(src) = o.clone_point.filter(|_| app.ui.tool == Tool::Clone && o.clone_source.is_none_or(|l| l == layer.id.0)) {
|
||||
let s = to_screen(src);
|
||||
painter.line_segment([s - vec2(6.0, 0.0), s + vec2(6.0, 0.0)], Stroke::new(1.0, Color32::WHITE));
|
||||
painter.line_segment([s - vec2(0.0, 6.0), s + vec2(0.0, 6.0)], Stroke::new(1.0, Color32::WHITE));
|
||||
}
|
||||
let mods = ui.input(|i| i.modifiers);
|
||||
let now = ui.input(|i| i.time);
|
||||
// Alt/Option-click with the Clone Stamp sets the source.
|
||||
if app.ui.tool == Tool::Clone
|
||||
&& mods.alt
|
||||
&& resp.clicked()
|
||||
&& let Some(pos) = resp.interact_pointer_pos()
|
||||
{
|
||||
let lp = to_layer(pos);
|
||||
let _ = app.session.execute("paint.setCloneSource", json!({"layer": layer.id.0, "point": lp}));
|
||||
return;
|
||||
}
|
||||
if let Some(kind) = kind
|
||||
&& !mods.alt
|
||||
{
|
||||
if resp.drag_started()
|
||||
&& let Some(pos) = ui.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos())
|
||||
{
|
||||
ctx.data_mut(|d| d.insert_temp(did, Drag { layer: layer.id.0, points: vec![to_layer(pos)], started: now }));
|
||||
}
|
||||
if let Some(mut d) = ctx.data(|d| d.get_temp::<Drag>(did)) {
|
||||
if let Some(pos) = resp.interact_pointer_pos() {
|
||||
let lp = to_layer(pos);
|
||||
let last = d.points.last().copied().unwrap_or(lp);
|
||||
if (lp[0] - last[0]).hypot(lp[1] - last[1]) * zoom >= 1.0 {
|
||||
d.points.push(lp);
|
||||
ctx.data_mut(|dd| dd.insert_temp(did, d.clone()));
|
||||
}
|
||||
}
|
||||
// Live preview of the stroke.
|
||||
let col = if kind == "eraser" {
|
||||
Color32::from_white_alpha(140)
|
||||
} else {
|
||||
Color32::from_rgba_unmultiplied((o.color[0] * 255.0) as u8, (o.color[1] * 255.0) as u8, (o.color[2] * 255.0) as u8, 200)
|
||||
};
|
||||
let width = ((o.diameter * zoom) as f32).max(1.0);
|
||||
let pts: Vec<Pos2> = d.points.iter().map(|q| to_screen(*q)).collect();
|
||||
if pts.len() == 1 {
|
||||
painter.circle_filled(pts[0], width / 2.0, col);
|
||||
} else {
|
||||
painter.add(egui::Shape::line(pts, Stroke::new(width, col)));
|
||||
}
|
||||
if resp.drag_stopped() {
|
||||
ctx.data_mut(|dd| dd.remove::<Drag>(did));
|
||||
let pts: Vec<[f64; 2]> = d.points.clone();
|
||||
let r = app.session.execute("paint.stroke", json!({"layer": d.layer, "kind": kind, "points": pts, "duration": (now - d.started).max(0.0)}));
|
||||
if let Err(e) = r {
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
if resp.clicked()
|
||||
&& let Some(pos) = resp.interact_pointer_pos()
|
||||
{
|
||||
let r = app.session.execute("paint.stroke", json!({"layer": layer.id.0, "kind": kind, "points": [to_layer(pos)], "duration": 0.0}));
|
||||
if let Err(e) = r {
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,12 @@ pub mod forms;
|
||||
pub mod graph;
|
||||
pub mod info;
|
||||
pub mod key_dialogs;
|
||||
pub mod layer_panel;
|
||||
pub mod misc;
|
||||
pub mod paint_panels;
|
||||
pub mod project;
|
||||
pub mod properties;
|
||||
pub mod puppet_tool;
|
||||
pub mod render_queue;
|
||||
pub mod text_panels;
|
||||
pub mod timeline;
|
||||
@@ -69,6 +72,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: PanelKind, rect: Rec
|
||||
PanelKind::Markers => misc::markers(app, ui, rect),
|
||||
PanelKind::Wiggler => misc::wiggler(app, ui, rect),
|
||||
PanelKind::RenderQueue => render_queue::show(app, ui, rect),
|
||||
PanelKind::Layer => layer_panel::show(app, ui, rect),
|
||||
PanelKind::Paint => paint_panels::paint(app, ui, rect),
|
||||
PanelKind::Brushes => paint_panels::brushes(app, ui, rect),
|
||||
other => misc::placeholder(app, ui, rect, other),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,360 @@
|
||||
//! Paint and Brushes panels (Window ▸ Paint ⌘8, Window ▸ Brushes ⌘9).
|
||||
//!
|
||||
//! Both edit the session's paint options through `paint.options` / `paint.brushPreset`, so
|
||||
//! agents see and set exactly what the panels show.
|
||||
|
||||
use effectcraft_engine::effects::paint::{BRUSH_PRESETS, CHANNELS, DURATIONS, ERASE_MODES, MODES};
|
||||
use egui::{Align2, Color32, Rect, Sense, Stroke, StrokeKind, pos2, vec2};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::state::Tool;
|
||||
use crate::theme::Tokens;
|
||||
use crate::{EffectcraftApp, widgets};
|
||||
|
||||
fn set(app: &mut EffectcraftApp, v: Value) {
|
||||
if let Err(e) = app.session.execute("paint.options", v) {
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
/// A label + hot number row; returns the new value when changed.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn number(
|
||||
app: &mut EffectcraftApp,
|
||||
ui: &mut egui::Ui,
|
||||
p: &egui::Painter,
|
||||
x: f32,
|
||||
y: f32,
|
||||
label: &str,
|
||||
key: &str,
|
||||
v: f64,
|
||||
range: (f64, f64),
|
||||
suffix: &str,
|
||||
enabled: bool,
|
||||
) -> Option<f64> {
|
||||
let t = app.tokens;
|
||||
p.text(pos2(x, y + 9.0), Align2::LEFT_CENTER, label, Tokens::ui(11.5), if enabled { t.text_dim } else { t.text_faint });
|
||||
let lw = p.layout_no_wrap(label.to_string(), Tokens::ui(11.5), t.text).size().x;
|
||||
let (r, nv, _) = widgets::hot_number_at(ui, pos2(x + lw + 6.0, y), egui::Id::new(("paint-num", key)), v, 0.5, range, 0, suffix, &t);
|
||||
app.auto.add(key, r, label);
|
||||
nv.filter(|_| enabled)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn popup_row(
|
||||
app: &mut EffectcraftApp,
|
||||
ui: &mut egui::Ui,
|
||||
p: &egui::Painter,
|
||||
x: f32,
|
||||
y: f32,
|
||||
w: f32,
|
||||
label: &str,
|
||||
key: &str,
|
||||
options: &[String],
|
||||
cur: usize,
|
||||
enabled: bool,
|
||||
) -> Option<usize> {
|
||||
let t = app.tokens;
|
||||
p.text(pos2(x, y + 10.0), Align2::LEFT_CENTER, label, Tokens::ui(11.5), if enabled { t.text_dim } else { t.text_faint });
|
||||
let r = Rect::from_min_size(pos2(x + 70.0, y), vec2((w - 70.0).max(60.0), 20.0));
|
||||
let pop = egui::Id::new(("paint-pop", key));
|
||||
if widgets::dropdown(ui, r, options.get(cur).map(String::as_str).unwrap_or(""), &t, egui::Id::new(("paint-dd", key))).clicked() && enabled {
|
||||
widgets::open_popup(ui, pop);
|
||||
}
|
||||
app.auto.add(key, r, label);
|
||||
widgets::popup_menu(ui, pop, r.left_bottom(), options, Some(cur))
|
||||
}
|
||||
|
||||
fn rgb(c: [f64; 4]) -> [f32; 3] {
|
||||
[c[0] as f32, c[1] as f32, c[2] as f32]
|
||||
}
|
||||
|
||||
pub fn paint(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
let t = app.tokens;
|
||||
let p = ui.painter().with_clip_rect(rect);
|
||||
let o = app.session.state.paint.clone();
|
||||
let x0 = rect.min.x + 10.0;
|
||||
let w = rect.width() - 20.0;
|
||||
let mut y = rect.min.y + 10.0;
|
||||
let eraser = app.ui.tool == Tool::Eraser;
|
||||
let clone = app.ui.tool == Tool::Clone;
|
||||
|
||||
// Opacity / Flow.
|
||||
if let Some(v) = number(app, ui, &p, x0, y, "Opacity:", "paint.opacity", o.opacity, (0.0, 100.0), " %", true) {
|
||||
set(app, json!({"opacity": v}));
|
||||
}
|
||||
if let Some(v) = number(app, ui, &p, x0 + w / 2.0, y, "Flow:", "paint.flow", o.flow, (0.0, 100.0), " %", true) {
|
||||
set(app, json!({"flow": v}));
|
||||
}
|
||||
y += 28.0;
|
||||
// Colour swatches (foreground over background) with swap / reset.
|
||||
let fg = Rect::from_min_size(pos2(x0, y), vec2(26.0, 20.0));
|
||||
let bg = Rect::from_min_size(pos2(x0 + 14.0, y + 10.0), vec2(26.0, 20.0));
|
||||
widgets::swatch(ui, bg, [o.background[0] as f32, o.background[1] as f32, o.background[2] as f32, 1.0], egui::Id::new("paint-bg"), &t);
|
||||
let bresp = ui.interact(Rect::from_min_max(pos2(fg.max.x, bg.min.y), bg.max), egui::Id::new("paint-bg-hit"), Sense::click());
|
||||
if bresp.clicked() {
|
||||
widgets::open_popup(ui, egui::Id::new("paint-bg-pop"));
|
||||
}
|
||||
if widgets::swatch(ui, fg, [o.color[0] as f32, o.color[1] as f32, o.color[2] as f32, 1.0], egui::Id::new("paint-fg"), &t).clicked() {
|
||||
widgets::open_popup(ui, egui::Id::new("paint-fg-pop"));
|
||||
}
|
||||
app.auto.add("paint.color", fg, "Foreground color");
|
||||
app.auto.add("paint.background", bg, "Background color");
|
||||
let mut fc = rgb(o.color);
|
||||
if crate::header::color_popup(ui, egui::Id::new("paint-fg-pop"), fg.left_bottom(), &mut fc) {
|
||||
set(app, json!({"color": [fc[0], fc[1], fc[2], 1.0]}));
|
||||
}
|
||||
let mut bc = rgb(o.background);
|
||||
if crate::header::color_popup(ui, egui::Id::new("paint-bg-pop"), bg.left_bottom(), &mut bc) {
|
||||
set(app, json!({"background": [bc[0], bc[1], bc[2], 1.0]}));
|
||||
}
|
||||
let swap = Rect::from_min_size(pos2(x0 + 46.0, y), vec2(18.0, 14.0));
|
||||
let sresp = ui.interact(swap, egui::Id::new("paint-swap"), Sense::click());
|
||||
p.text(swap.center(), Align2::CENTER_CENTER, "⇄", Tokens::ui(12.0), if sresp.hovered() { t.text } else { t.text_dim });
|
||||
app.auto.add("paint.swapColors", swap, "Swap colors");
|
||||
if sresp.on_hover_text("Swap Paint Colors (X)").clicked() {
|
||||
set(app, json!({"color": o.background, "background": o.color}));
|
||||
}
|
||||
let reset = Rect::from_min_size(pos2(x0 + 46.0, y + 18.0), vec2(18.0, 14.0));
|
||||
let rresp = ui.interact(reset, egui::Id::new("paint-reset-colors"), Sense::click());
|
||||
p.rect_filled(Rect::from_min_size(reset.min + vec2(4.0, 3.0), vec2(7.0, 6.0)), 0.0, Color32::WHITE);
|
||||
p.rect_stroke(Rect::from_min_size(reset.min + vec2(8.0, 6.0), vec2(7.0, 6.0)), 0.0, Stroke::new(1.0, t.text_dim), StrokeKind::Inside);
|
||||
app.auto.add("paint.resetColors", reset, "Default colors");
|
||||
if rresp.on_hover_text("Set Paint Colors to Black and White (D)").clicked() {
|
||||
set(app, json!({"color": [1.0, 1.0, 1.0, 1.0], "background": [0.0, 0.0, 0.0, 1.0]}));
|
||||
}
|
||||
y += 40.0;
|
||||
|
||||
// Mode / Channels / Duration.
|
||||
let modes: Vec<String> = MODES.iter().map(|m| m.label().to_string()).collect();
|
||||
if let Some(i) = popup_row(app, ui, &p, x0, y, w, "Mode:", "paint.mode", &modes, o.mode as usize, !eraser) {
|
||||
set(app, json!({"mode": i}));
|
||||
}
|
||||
y += 26.0;
|
||||
let chans: Vec<String> = CHANNELS.iter().map(|s| s.to_string()).collect();
|
||||
if let Some(i) = popup_row(app, ui, &p, x0, y, w, "Channels:", "paint.channels", &chans, o.channels as usize, !eraser) {
|
||||
set(app, json!({"channels": i}));
|
||||
}
|
||||
y += 26.0;
|
||||
let durs: Vec<String> = DURATIONS.iter().map(|s| s.to_string()).collect();
|
||||
let dw = if o.duration == 3 { w - 60.0 } else { w };
|
||||
if let Some(i) = popup_row(app, ui, &p, x0, y, dw, "Duration:", "paint.duration", &durs, o.duration as usize, true) {
|
||||
set(app, json!({"durationMode": i}));
|
||||
}
|
||||
if o.duration == 3 {
|
||||
let (r, nv, _) = widgets::hot_number_at(
|
||||
ui,
|
||||
pos2(x0 + w - 52.0, y + 1.0),
|
||||
egui::Id::new("paint-custom-frames"),
|
||||
o.custom_frames as f64,
|
||||
0.2,
|
||||
(1.0, 10000.0),
|
||||
0,
|
||||
" f",
|
||||
&t,
|
||||
);
|
||||
app.auto.add("paint.customFrames", r, "Custom duration (frames)");
|
||||
if let Some(v) = nv {
|
||||
set(app, json!({"customFrames": v.round() as u64}));
|
||||
}
|
||||
}
|
||||
y += 26.0;
|
||||
let erase: Vec<String> = ERASE_MODES.iter().map(|s| s.to_string()).collect();
|
||||
if let Some(i) = popup_row(app, ui, &p, x0, y, w, "Erase:", "paint.erase", &erase, o.erase as usize, eraser) {
|
||||
set(app, json!({"eraseMode": i}));
|
||||
}
|
||||
y += 32.0;
|
||||
|
||||
// Clone Options.
|
||||
p.line_segment([pos2(x0, y), pos2(x0 + w, y)], Stroke::new(1.0, t.separator));
|
||||
y += 8.0;
|
||||
p.text(pos2(x0, y + 8.0), Align2::LEFT_CENTER, "Clone Options", Tokens::semibold(11.5), if clone { t.text } else { t.text_faint });
|
||||
y += 22.0;
|
||||
p.text(pos2(x0, y + 10.0), Align2::LEFT_CENTER, "Preset:", Tokens::ui(11.5), t.text_dim);
|
||||
for i in 1..=5u32 {
|
||||
let r = Rect::from_min_size(pos2(x0 + 56.0 + (i - 1) as f32 * 26.0, y), vec2(22.0, 20.0));
|
||||
let resp = ui.interact(r, egui::Id::new(("clone-preset", i)), Sense::click());
|
||||
let on = o.clone_preset == i;
|
||||
p.rect_filled(
|
||||
r,
|
||||
3.0,
|
||||
if on {
|
||||
t.accent
|
||||
} else if resp.hovered() {
|
||||
t.hover
|
||||
} else {
|
||||
t.field_bg
|
||||
},
|
||||
);
|
||||
p.text(r.center(), Align2::CENTER_CENTER, i.to_string(), Tokens::ui(11.0), if on { Color32::WHITE } else { t.text_dim });
|
||||
app.auto.add(&format!("paint.clonePreset.{i}"), r, &format!("Clone preset {i}"));
|
||||
if resp.clicked() {
|
||||
set(app, json!({"clonePreset": i}));
|
||||
}
|
||||
}
|
||||
y += 26.0;
|
||||
// Source layer.
|
||||
let comp_layers: Vec<(u64, String)> = app.session.active_comp().map(|c| c.layers.iter().map(|l| (l.id.0, l.name.clone())).collect()).unwrap_or_default();
|
||||
let mut names = vec!["Current Layer".to_string()];
|
||||
names.extend(comp_layers.iter().map(|(_, n)| n.clone()));
|
||||
let cur = o.clone_source.and_then(|id| comp_layers.iter().position(|(l, _)| *l == id).map(|i| i + 1)).unwrap_or(0);
|
||||
if let Some(i) = popup_row(app, ui, &p, x0, y, w, "Source:", "paint.cloneSource", &names, cur, true) {
|
||||
let v = if i == 0 { Value::Null } else { json!(comp_layers[i - 1].0) };
|
||||
set(app, json!({"cloneSource": v}));
|
||||
}
|
||||
y += 26.0;
|
||||
for (i, (label, key, on, k)) in
|
||||
[("Aligned", "paint.aligned", o.aligned, "aligned"), ("Lock Source Time", "paint.lockSourceTime", o.lock_source_time, "lockSourceTime")]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let r = Rect::from_min_size(pos2(x0 + i as f32 * (w / 2.0), y), vec2(18.0, 20.0));
|
||||
if widgets::checkbox(ui, r, on, &t, egui::Id::new(key)).clicked() {
|
||||
set(app, json!({k: !on}));
|
||||
}
|
||||
p.text(pos2(r.max.x + 4.0, r.center().y), Align2::LEFT_CENTER, label, Tokens::ui(11.5), t.text_dim);
|
||||
app.auto.add(key, r, label);
|
||||
}
|
||||
y += 26.0;
|
||||
let off = o.clone_offset.unwrap_or([0.0, 0.0]);
|
||||
p.text(pos2(x0, y + 9.0), Align2::LEFT_CENTER, "Offset:", Tokens::ui(11.5), t.text_dim);
|
||||
for (d, v) in off.iter().enumerate() {
|
||||
let (r, nv, _) = widgets::hot_number_at(ui, pos2(x0 + 56.0 + d as f32 * 64.0, y), egui::Id::new(("clone-off", d)), *v, 1.0, (-1e5, 1e5), 0, "", &t);
|
||||
app.auto.add(&format!("paint.cloneOffset.{d}"), r, "Clone offset");
|
||||
if let Some(nv) = nv {
|
||||
let mut no = off;
|
||||
no[d] = nv;
|
||||
set(app, json!({"cloneOffset": no}));
|
||||
}
|
||||
}
|
||||
y += 26.0;
|
||||
let (label, key, v) =
|
||||
if o.lock_source_time { ("Source Time:", "paint.cloneTime", o.clone_time) } else { ("Source Time Shift:", "paint.cloneTimeShift", o.clone_time_shift) };
|
||||
let k = if o.lock_source_time { "cloneTime" } else { "cloneTimeShift" };
|
||||
if let Some(nv) = number(app, ui, &p, x0, y, label, key, v, (-1e5, 1e5), " s", true) {
|
||||
set(app, json!({k: nv}));
|
||||
}
|
||||
y += 26.0;
|
||||
let r = Rect::from_min_size(pos2(x0, y), vec2(18.0, 20.0));
|
||||
if widgets::checkbox(ui, r, o.show_overlay, &t, egui::Id::new("paint.showOverlay")).clicked() {
|
||||
set(app, json!({"showOverlay": !o.show_overlay}));
|
||||
}
|
||||
app.auto.add("paint.showOverlay", r, "Clone Source Overlay");
|
||||
p.text(pos2(r.max.x + 4.0, r.center().y), Align2::LEFT_CENTER, "Clone Source Overlay:", Tokens::ui(11.5), t.text_dim);
|
||||
if let Some(nv) = number(app, ui, &p, x0 + 150.0, y, "", "paint.overlayOpacity", o.overlay_opacity, (0.0, 100.0), " %", true) {
|
||||
set(app, json!({"overlayOpacity": nv}));
|
||||
}
|
||||
let dr = Rect::from_min_size(pos2(x0 + w - 18.0, y), vec2(18.0, 20.0));
|
||||
if widgets::checkbox(ui, dr, o.overlay_difference, &t, egui::Id::new("paint.overlayDifference")).clicked() {
|
||||
set(app, json!({"overlayDifference": !o.overlay_difference}));
|
||||
}
|
||||
app.auto.add("paint.overlayDifference", dr, "Difference mode overlay");
|
||||
}
|
||||
|
||||
/// Draw a brush tip preview in `r`.
|
||||
fn tip_preview(p: &egui::Painter, r: Rect, diameter: f64, angle: f64, roundness: f64, hardness: f64, col: Color32) {
|
||||
let max = r.width().min(r.height()) * 0.42;
|
||||
let rad = ((diameter / 2.0).sqrt() as f32 * 2.2).clamp(1.5, max);
|
||||
let rnd = (roundness / 100.0).clamp(0.05, 1.0) as f32;
|
||||
let ang = (angle as f32).to_radians();
|
||||
// Soft tips: concentric rings with falling alpha.
|
||||
let rings = if hardness >= 99.0 { 1 } else { 6 };
|
||||
for k in (0..rings).rev() {
|
||||
let f = (k + 1) as f32 / rings as f32;
|
||||
let h = (hardness / 100.0) as f32;
|
||||
let a = if rings == 1 { 1.0 } else { (1.0 - ((f - h) / (1.0 - h).max(1e-3)).clamp(0.0, 1.0)).max(0.15) };
|
||||
let pts: Vec<egui::Pos2> = (0..32)
|
||||
.map(|i| {
|
||||
let t = i as f32 / 32.0 * std::f32::consts::TAU;
|
||||
let (x, y) = (t.cos() * rad * f, t.sin() * rad * f * rnd);
|
||||
r.center() + vec2(x * ang.cos() - y * ang.sin(), x * ang.sin() + y * ang.cos())
|
||||
})
|
||||
.collect();
|
||||
p.add(egui::Shape::convex_polygon(pts, col.gamma_multiply(a / rings as f32 * if rings == 1 { 1.0 } else { 2.5 }), Stroke::NONE));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn brushes(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
let t = app.tokens;
|
||||
let p = ui.painter().with_clip_rect(rect);
|
||||
let o = app.session.state.paint.clone();
|
||||
let x0 = rect.min.x + 10.0;
|
||||
let w = rect.width() - 20.0;
|
||||
let mut y = rect.min.y + 8.0;
|
||||
// Preset grid (thumbnails with size labels).
|
||||
let cell = 38.0;
|
||||
let cols = ((w / cell).floor() as usize).max(1);
|
||||
for (i, tip) in BRUSH_PRESETS.iter().enumerate() {
|
||||
let r = Rect::from_min_size(pos2(x0 + (i % cols) as f32 * cell, y + (i / cols) as f32 * cell), vec2(cell - 2.0, cell - 2.0));
|
||||
let resp = ui.interact(r, egui::Id::new(("brush-preset", i)), Sense::click());
|
||||
let on = o.preset == Some(i);
|
||||
p.rect_filled(
|
||||
r,
|
||||
3.0,
|
||||
if on {
|
||||
t.row_selected
|
||||
} else if resp.hovered() {
|
||||
t.hover
|
||||
} else {
|
||||
t.field_bg
|
||||
},
|
||||
);
|
||||
tip_preview(&p, Rect::from_min_size(r.min, vec2(r.width(), r.height() - 9.0)), tip.diameter, tip.angle, tip.roundness, tip.hardness, t.text);
|
||||
p.text(pos2(r.center().x, r.max.y - 5.0), Align2::CENTER_CENTER, format!("{}", tip.diameter), Tokens::ui(8.5), t.text_dim);
|
||||
app.auto.add(&format!("brushes.preset.{i}"), r, tip.name);
|
||||
if resp.on_hover_text(tip.name).clicked()
|
||||
&& let Err(e) = app.session.execute("paint.brushPreset", json!({"preset": i}))
|
||||
{
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
}
|
||||
y += (BRUSH_PRESETS.len().div_ceil(cols)) as f32 * cell + 8.0;
|
||||
p.line_segment([pos2(x0, y), pos2(x0 + w, y)], Stroke::new(1.0, t.separator));
|
||||
y += 8.0;
|
||||
// Tip preview + values.
|
||||
let prev = Rect::from_min_size(pos2(x0 + w - 56.0, y), vec2(56.0, 56.0));
|
||||
p.rect_filled(prev, 3.0, t.field_bg);
|
||||
tip_preview(&p, prev, o.diameter.min(60.0), o.angle, o.roundness, o.hardness, t.text);
|
||||
app.auto.add("brushes.tipPreview", prev, "Brush tip");
|
||||
for (label, key, v, range, suffix) in [
|
||||
("Diameter:", "diameter", o.diameter, (1.0, 2500.0), " px"),
|
||||
("Angle:", "angle", o.angle, (-180.0, 180.0), "°"),
|
||||
("Roundness:", "roundness", o.roundness, (0.0, 100.0), " %"),
|
||||
("Hardness:", "hardness", o.hardness, (0.0, 100.0), " %"),
|
||||
] {
|
||||
if let Some(nv) = number(app, ui, &p, x0, y, label, &format!("brushes.{key}"), v, range, suffix, true) {
|
||||
set(app, json!({key: nv}));
|
||||
}
|
||||
y += 24.0;
|
||||
}
|
||||
let r = Rect::from_min_size(pos2(x0, y), vec2(18.0, 20.0));
|
||||
// Spacing (checkbox in AE turns spacing on; we always space dabs, so it shows the value).
|
||||
p.text(pos2(x0, y + 9.0), Align2::LEFT_CENTER, "Spacing:", Tokens::ui(11.5), t.text_dim);
|
||||
let _ = r;
|
||||
let (sr, nv, _) = widgets::hot_number_at(ui, pos2(x0 + 58.0, y), egui::Id::new("brushes-spacing"), o.spacing, 0.5, (1.0, 1000.0), 0, " %", &t);
|
||||
app.auto.add("brushes.spacing", sr, "Spacing");
|
||||
if let Some(nv) = nv {
|
||||
set(app, json!({"spacing": nv}));
|
||||
}
|
||||
y += 30.0;
|
||||
// Brush Dynamics.
|
||||
p.text(pos2(x0, y + 8.0), Align2::LEFT_CENTER, "Brush Dynamics", Tokens::semibold(11.5), t.text);
|
||||
y += 22.0;
|
||||
let opts = vec!["Off".to_string(), "Pen Pressure".to_string()];
|
||||
for (label, key, on) in
|
||||
[("Size:", "sizePressure", o.size_pressure), ("Opacity:", "opacityPressure", o.opacity_pressure), ("Flow:", "flowPressure", o.flow_pressure)]
|
||||
{
|
||||
if let Some(i) = popup_row(app, ui, &p, x0, y, w, label, &format!("brushes.{key}"), &opts, on as usize, true) {
|
||||
set(app, json!({key: i == 1}));
|
||||
}
|
||||
y += 26.0;
|
||||
if key == "sizePressure"
|
||||
&& let Some(nv) = number(app, ui, &p, x0 + 12.0, y, "Minimum Size:", "brushes.minSize", o.min_size, (0.0, 100.0), " %", o.size_pressure)
|
||||
{
|
||||
set(app, json!({"minSize": nv}));
|
||||
}
|
||||
if key == "sizePressure" {
|
||||
y += 26.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
//! Puppet tools in the Composition viewer: the deformed mesh and pins of the selected layers'
|
||||
//! Puppet effect, click to add a pin (`puppet.addPin`), drag a pin to move it
|
||||
//! (`puppet.movePin`, merged into one undo step per drag).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_engine::effects::puppet::{self, Mesh, Pin, PinKind};
|
||||
use effectcraft_engine::geom::vec2 as gv2;
|
||||
use effectcraft_engine::project::{ItemId, Layer, LayerId};
|
||||
use effectcraft_engine::render::EvalCtx;
|
||||
use effectcraft_engine::time::Tick;
|
||||
use egui::{Color32, Pos2, Stroke};
|
||||
|
||||
use super::viewer::{ViewerMap, l2c};
|
||||
use crate::EffectcraftApp;
|
||||
|
||||
/// Deformed meshes of a layer's Puppet effect: (mesh uid, mesh, deformed vertices, pins).
|
||||
pub type Overlay = Arc<Vec<(u64, Arc<Mesh>, Vec<[f64; 2]>, Vec<Pin>)>>;
|
||||
|
||||
/// A pin drawn this frame.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct PinHit {
|
||||
pub layer: LayerId,
|
||||
pub pin: u64,
|
||||
pub kind: PinKind,
|
||||
pub pos: Pos2,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Cached {
|
||||
key: (u64, u64, u64, i64),
|
||||
overlay: Overlay,
|
||||
}
|
||||
|
||||
/// The deformed mesh of `layer`'s Puppet effect at comp time `t` (cached per project revision).
|
||||
pub fn overlay(app: &EffectcraftApp, ctx: &egui::Context, cid: ItemId, layer: &Layer, t: Tick) -> Option<Overlay> {
|
||||
let fx = layer.effects()?.groups().find(|g| puppet::is_puppet(g))?;
|
||||
let key = (app.session.revision, cid.0, layer.id.0, t.0);
|
||||
let id = egui::Id::new(("puppet-overlay", layer.id.0));
|
||||
if let Some(c) = ctx.data(|d| d.get_temp::<Cached>(id))
|
||||
&& c.key == key
|
||||
{
|
||||
return Some(c.overlay);
|
||||
}
|
||||
let (buf, params) = effectcraft_engine::commands::puppet::puppet_eval(&app.session, cid, layer.id, fx.uid, t)?;
|
||||
let overlay: Overlay = Arc::new(puppet::overlay(&buf, ¶ms));
|
||||
ctx.data_mut(|d| d.insert_temp(id, Cached { key, overlay: overlay.clone() }));
|
||||
Some(overlay)
|
||||
}
|
||||
|
||||
/// Where a pin sits now (layer space): its Position, or (Bend / Starch / Overlap pins) its rest
|
||||
/// point carried along by the deformation.
|
||||
pub fn pin_position(mesh: &Mesh, def: &[[f64; 2]], p: &Pin) -> [f64; 2] {
|
||||
if p.kind.moves() {
|
||||
return p.position;
|
||||
}
|
||||
let v = (0..mesh.verts.len()).min_by(|a, b| {
|
||||
let da = (mesh.verts[*a][0] - p.rest[0]).hypot(mesh.verts[*a][1] - p.rest[1]);
|
||||
let db = (mesh.verts[*b][0] - p.rest[0]).hypot(mesh.verts[*b][1] - p.rest[1]);
|
||||
da.total_cmp(&db)
|
||||
});
|
||||
match v {
|
||||
Some(v) => [def[v][0] + p.rest[0] - mesh.verts[v][0], def[v][1] + p.rest[1] - mesh.verts[v][1]],
|
||||
None => p.rest,
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw the mesh (when `show_mesh`) and pins of `layer`; returns pin hit targets.
|
||||
pub fn draw(painter: &egui::Painter, map: &ViewerMap, ectx: &EvalCtx, layer: &Layer, ov: &Overlay, show_mesh: bool, selected: &[u64]) -> Vec<PinHit> {
|
||||
let (m, _) = l2c(ectx, layer);
|
||||
let scr = |q: [f64; 2]| {
|
||||
let c = m.apply(gv2(q[0], q[1]));
|
||||
map.to_screen([c.x, c.y])
|
||||
};
|
||||
let mut hits = vec![];
|
||||
for (_, mesh, def, pins) in ov.iter() {
|
||||
if show_mesh {
|
||||
let st = Stroke::new(1.0, Color32::from_rgba_unmultiplied(0xf0, 0xd0, 0x40, 110));
|
||||
for t in &mesh.tris {
|
||||
let (a, b, c) = (scr(def[t[0]]), scr(def[t[1]]), scr(def[t[2]]));
|
||||
painter.line_segment([a, b], st);
|
||||
painter.line_segment([b, c], st);
|
||||
painter.line_segment([c, a], st);
|
||||
}
|
||||
}
|
||||
for pn in pins {
|
||||
let pos = scr(pin_position(mesh, def, pn));
|
||||
let sel = selected.contains(&pn.uid);
|
||||
let col = match pn.kind {
|
||||
PinKind::Starch => Color32::from_rgb(0xe0, 0x50, 0x50),
|
||||
PinKind::Overlap => Color32::from_rgb(0x40, 0x90, 0xf0),
|
||||
PinKind::Bend => Color32::from_rgb(0xf0, 0x90, 0x30),
|
||||
_ => Color32::from_rgb(0xf0, 0xd0, 0x40),
|
||||
};
|
||||
if matches!(pn.kind, PinKind::Starch | PinKind::Overlap) {
|
||||
// Extent ring (layer pixels → screen).
|
||||
let r = (scr([pn.rest[0] + pn.extent, pn.rest[1]]) - scr(pn.rest)).length();
|
||||
painter.circle_stroke(pos, r.max(2.0), Stroke::new(1.0, col.gamma_multiply(0.6)));
|
||||
}
|
||||
if matches!(pn.kind, PinKind::Bend | PinKind::Advanced) {
|
||||
painter.circle_stroke(pos, 9.0, Stroke::new(1.5, col));
|
||||
}
|
||||
painter.circle_filled(pos, if sel { 5.5 } else { 4.5 }, if sel { col } else { col.gamma_multiply(0.85) });
|
||||
painter.circle_stroke(pos, if sel { 5.5 } else { 4.5 }, Stroke::new(1.0, Color32::BLACK));
|
||||
hits.push(PinHit { layer: layer.id, pin: pn.uid, kind: pn.kind, pos });
|
||||
}
|
||||
}
|
||||
hits
|
||||
}
|
||||
@@ -813,6 +813,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
if row_resp.double_clicked() {
|
||||
match &layer.source {
|
||||
LayerSource::Comp { item } => actions.push(("comp.open".into(), json!({"comp": item.0}))),
|
||||
// Footage and solids open in the Layer panel (paint happens there).
|
||||
LayerSource::Footage { .. } | LayerSource::Solid { .. } => {
|
||||
actions.push(("layer.openLayer".into(), json!({"layer": layer.id.0})));
|
||||
}
|
||||
_ => {
|
||||
ctx.data_mut(|d| d.insert_temp(rename_id, (layer.id.0, layer.name.clone())));
|
||||
}
|
||||
|
||||
@@ -110,6 +110,12 @@ enum Gesture {
|
||||
vertex: [f64; 2],
|
||||
inv: Mat3,
|
||||
},
|
||||
/// Dragging a Puppet pin (comp → layer matrix inverse).
|
||||
PuppetPin {
|
||||
layer: LayerId,
|
||||
pin: u64,
|
||||
inv: Mat3,
|
||||
},
|
||||
/// Pen tool: pulling the tangents of the vertex just placed.
|
||||
Pen {
|
||||
layer: LayerId,
|
||||
@@ -153,7 +159,7 @@ fn cam_state(ctx: &EvalCtx) -> CameraState {
|
||||
}
|
||||
|
||||
/// Layer → comp (viewer) matrix through the current 3D view.
|
||||
fn l2c(ctx: &EvalCtx, layer: &Layer) -> (Mat3, f64) {
|
||||
pub(crate) fn l2c(ctx: &EvalCtx, layer: &Layer) -> (Mat3, f64) {
|
||||
match VIEW_CAM.with(|c| c.get()) {
|
||||
Some(cam) if layer.is_3d() => (three_d::layer_to_view(ctx, layer, &cam), 0.0),
|
||||
_ => ctx.layer_to_comp(layer),
|
||||
@@ -201,7 +207,7 @@ fn parent_inverse(ctx: &EvalCtx, layer: &Layer) -> Mat3 {
|
||||
}
|
||||
}
|
||||
|
||||
fn checker(p: &egui::Painter, r: Rect) {
|
||||
pub(crate) fn checker(p: &egui::Painter, r: Rect) {
|
||||
let s = 10.0;
|
||||
p.rect_filled(r, 0.0, Color32::from_gray(0xcc));
|
||||
let mut y = r.min.y;
|
||||
@@ -513,6 +519,22 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
painter.text(area.left_top() + vec2(12.0, 10.0), Align2::LEFT_TOP, label, Tokens::ui(12.0), t.text_dim);
|
||||
}
|
||||
|
||||
// Puppet pins and meshes of the selected layers (Puppet tools).
|
||||
let mut pin_hits: Vec<super::puppet_tool::PinHit> = vec![];
|
||||
if app.ui.tool.puppet_kind().is_some() {
|
||||
let sel_props: Vec<u64> = app.session.state.selected_props.iter().map(|(_, u)| *u).collect();
|
||||
for lid in &selected {
|
||||
if let Some(l) = comp.layer(*lid)
|
||||
&& let Some(ov) = super::puppet_tool::overlay(app, &ctx, cid, l, time)
|
||||
{
|
||||
pin_hits.extend(super::puppet_tool::draw(&painter, &map, &ectx, l, &ov, app.session.state.puppet.show_mesh, &sel_props));
|
||||
}
|
||||
}
|
||||
for h in &pin_hits {
|
||||
app.auto.add(&format!("viewer.puppetPin.{}", h.pin), Rect::from_center_size(h.pos, vec2(10.0, 10.0)), "Puppet pin");
|
||||
}
|
||||
}
|
||||
|
||||
// Interaction.
|
||||
let resp = ui.interact(area, egui::Id::new("viewer-interact"), Sense::click_and_drag());
|
||||
let gid = egui::Id::new("viewer-gesture");
|
||||
@@ -529,6 +551,13 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
Tool::PanCamera => egui::CursorIcon::Move,
|
||||
Tool::Dolly => egui::CursorIcon::ResizeVertical,
|
||||
t if t.is_shape() || t == Tool::Pen => egui::CursorIcon::Crosshair,
|
||||
t if t.puppet_kind().is_some() => {
|
||||
if pin_hits.iter().any(|h| h.pos.distance(hp) < 8.0) {
|
||||
egui::CursorIcon::Move
|
||||
} else {
|
||||
egui::CursorIcon::Crosshair
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if handle_hits.iter().any(|(_, _, h)| h.distance(hp) < HANDLE) {
|
||||
egui::CursorIcon::ResizeNwSe
|
||||
@@ -609,6 +638,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
})
|
||||
}),
|
||||
t if t.is_shape() => Some(Gesture::Create { tool: t, start: cpt }),
|
||||
t if t.puppet_kind().is_some() => pin_hits.iter().rev().find(|h| h.pos.distance(press) < 8.0).and_then(|h| {
|
||||
let l = comp.layer(h.layer)?;
|
||||
Some(Gesture::PuppetPin { layer: h.layer, pin: h.pin, inv: l2c(&ectx, l).0.inverse()? })
|
||||
}),
|
||||
Tool::Orbit | Tool::PanCamera | Tool::Dolly => Some(Gesture::Camera { tool }),
|
||||
Tool::Selection if vertex_at(press, true).is_some() => vertex_at(press, true).map(|h| Gesture::Tangent {
|
||||
layer: h.layer,
|
||||
@@ -784,6 +817,12 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
let _ = app.session.execute("mask.setVertex", params);
|
||||
}
|
||||
Gesture::PuppetPin { layer, pin, inv } => {
|
||||
let lp = inv.apply(gv2(cpt[0], cpt[1]));
|
||||
if let Err(e) = app.session.execute("puppet.movePin", json!({"layer": layer.0, "pin": pin, "position": [lp.x, lp.y], "merge": merge})) {
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
}
|
||||
Gesture::Pen { layer, mask, index, vertex, inv, press } => {
|
||||
if pos.distance(press) > 3.0 {
|
||||
let lp = inv.apply(gv2(cpt[0], cpt[1]));
|
||||
@@ -850,6 +889,18 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
}
|
||||
Tool::Pen => {}
|
||||
t if t.puppet_kind().is_some() => {
|
||||
if pin_hits.iter().all(|h| h.pos.distance(pos) >= 8.0)
|
||||
&& let Some(l) = pick(app, &ectx, cpt, false).and_then(|l| comp.layer(l))
|
||||
&& let Some(inv) = l2c(&ectx, l).0.inverse()
|
||||
{
|
||||
let lp = inv.apply(gv2(cpt[0], cpt[1]));
|
||||
let r = app.session.execute("puppet.addPin", json!({"layer": l.id.0, "kind": t.puppet_kind(), "position": [lp.x, lp.y]}));
|
||||
if let Err(e) = r {
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
Tool::Selection if vertex_at(pos, false).is_some() => {
|
||||
if let Some(h) = vertex_at(pos, false) {
|
||||
let me = json!({"layer": h.layer.0, "mask": h.mask, "index": h.index});
|
||||
@@ -1030,7 +1081,7 @@ fn pen_press(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, map: &
|
||||
}
|
||||
|
||||
/// Topmost layer under a comp point (selects it; shift toggles). Returns the hit layer.
|
||||
fn pick(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], toggle: bool) -> Option<LayerId> {
|
||||
pub(crate) fn pick(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], toggle: bool) -> Option<LayerId> {
|
||||
let hit = ectx
|
||||
.comp
|
||||
.layers
|
||||
|
||||
@@ -31,6 +31,10 @@ pub enum Tool {
|
||||
Eraser,
|
||||
RotoBrush,
|
||||
Puppet,
|
||||
PuppetStarch,
|
||||
PuppetBend,
|
||||
PuppetAdvanced,
|
||||
PuppetOverlap,
|
||||
}
|
||||
|
||||
impl Tool {
|
||||
@@ -51,9 +55,9 @@ impl Tool {
|
||||
&[Tool::Clone],
|
||||
&[Tool::Eraser],
|
||||
&[Tool::RotoBrush],
|
||||
&[Tool::Puppet],
|
||||
&[Tool::Puppet, Tool::PuppetStarch, Tool::PuppetBend, Tool::PuppetAdvanced, Tool::PuppetOverlap],
|
||||
];
|
||||
pub const ALL: [Tool; 21] = [
|
||||
pub const ALL: [Tool; 25] = [
|
||||
Tool::Selection,
|
||||
Tool::Hand,
|
||||
Tool::Zoom,
|
||||
@@ -75,6 +79,10 @@ impl Tool {
|
||||
Tool::Eraser,
|
||||
Tool::RotoBrush,
|
||||
Tool::Puppet,
|
||||
Tool::PuppetStarch,
|
||||
Tool::PuppetBend,
|
||||
Tool::PuppetAdvanced,
|
||||
Tool::PuppetOverlap,
|
||||
];
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
@@ -99,6 +107,10 @@ impl Tool {
|
||||
Tool::Eraser => "Eraser Tool",
|
||||
Tool::RotoBrush => "Roto Brush Tool",
|
||||
Tool::Puppet => "Puppet Position Pin Tool",
|
||||
Tool::PuppetStarch => "Puppet Starch Pin Tool",
|
||||
Tool::PuppetBend => "Puppet Bend Pin Tool",
|
||||
Tool::PuppetAdvanced => "Puppet Advanced Pin Tool",
|
||||
Tool::PuppetOverlap => "Puppet Overlap Pin Tool",
|
||||
}
|
||||
}
|
||||
pub fn shortcut(self) -> Option<&'static str> {
|
||||
@@ -116,7 +128,7 @@ impl Tool {
|
||||
Tool::Type | Tool::TypeVertical => Some("Cmd+T"),
|
||||
Tool::Brush | Tool::Clone | Tool::Eraser => Some("Cmd+B"),
|
||||
Tool::RotoBrush => Some("Alt+W"),
|
||||
Tool::Puppet => Some("Cmd+P"),
|
||||
Tool::Puppet | Tool::PuppetStarch | Tool::PuppetBend | Tool::PuppetAdvanced | Tool::PuppetOverlap => Some("Cmd+P"),
|
||||
}
|
||||
}
|
||||
pub fn icon(self) -> Icon {
|
||||
@@ -141,13 +153,33 @@ impl Tool {
|
||||
Tool::Clone => Icon::Clone,
|
||||
Tool::Eraser => Icon::Eraser,
|
||||
Tool::RotoBrush => Icon::RotoBrush,
|
||||
Tool::Puppet => Icon::Puppet,
|
||||
Tool::Puppet | Tool::PuppetStarch | Tool::PuppetBend | Tool::PuppetAdvanced | Tool::PuppetOverlap => Icon::Puppet,
|
||||
}
|
||||
}
|
||||
pub fn from_name(s: &str) -> Option<Tool> {
|
||||
let n = s.to_ascii_lowercase().replace([' ', '_', '-'], "");
|
||||
Tool::ALL.into_iter().find(|t| format!("{t:?}").to_ascii_lowercase() == n || t.label().to_ascii_lowercase().replace(' ', "").starts_with(&n))
|
||||
}
|
||||
/// Puppet pin tools, with the pin kind they place (`puppet.addPin` kind).
|
||||
pub fn puppet_kind(self) -> Option<&'static str> {
|
||||
Some(match self {
|
||||
Tool::Puppet => "position",
|
||||
Tool::PuppetStarch => "starch",
|
||||
Tool::PuppetBend => "bend",
|
||||
Tool::PuppetAdvanced => "advanced",
|
||||
Tool::PuppetOverlap => "overlap",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
/// Paint tools, with their stroke kind (`paint.stroke` kind).
|
||||
pub fn paint_kind(self) -> Option<&'static str> {
|
||||
Some(match self {
|
||||
Tool::Brush => "brush",
|
||||
Tool::Clone => "clone",
|
||||
Tool::Eraser => "eraser",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
pub fn is_shape(self) -> bool {
|
||||
matches!(self, Tool::Rectangle | Tool::RoundedRect | Tool::Ellipse | Tool::Polygon | Tool::Star)
|
||||
}
|
||||
@@ -363,6 +395,12 @@ pub struct UiState {
|
||||
pub start_screen: bool,
|
||||
/// Composition ▸ Preview ▸ Cache Frames When Idle.
|
||||
pub cache_when_idle: bool,
|
||||
/// Layer shown in the Layer panel (None = the first selected layer).
|
||||
#[serde(default)]
|
||||
pub layer_panel: Option<u64>,
|
||||
/// Layer panel View: number of effects rendered (None = through the last Paint effect).
|
||||
#[serde(default)]
|
||||
pub layer_view: Option<usize>,
|
||||
}
|
||||
|
||||
impl Default for UiState {
|
||||
@@ -396,6 +434,8 @@ impl Default for UiState {
|
||||
preview_audio: true,
|
||||
start_screen: false,
|
||||
cache_when_idle: false,
|
||||
layer_panel: None,
|
||||
layer_view: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user