Merge pull request #443 from Aavirash/fix/shape-drag-preview
# Conflicts: # crates/ui-egui/src/frames.rs # crates/ui-egui/src/panels/viewer.rs
This commit is contained in:
@@ -4,9 +4,10 @@
|
||||
//! 3D views (Active Camera / Front / … / Custom View 3), camera tools (orbit, pan, dolly) and
|
||||
//! camera/light wireframes.
|
||||
|
||||
use effectcraft_engine::commands::shape_tool::PaintKind;
|
||||
use effectcraft_engine::effects::puppet::PinKind;
|
||||
use effectcraft_engine::geom::{Mat3, vec2 as gv2};
|
||||
use effectcraft_engine::project::{Comp, Layer, LayerId};
|
||||
use effectcraft_engine::project::{Comp, Layer, LayerId, LayerSource};
|
||||
use effectcraft_engine::render::EvalCtx;
|
||||
use effectcraft_engine::render::three_d::{self, CameraState, View3D};
|
||||
use effectcraft_engine::time::Tick;
|
||||
@@ -667,16 +668,19 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
let frame = comp.frame_rate.frame_at(time);
|
||||
let key = app.frame_key(cid, frame, scale);
|
||||
app.request_frame_urgent(cid, frame, scale);
|
||||
if let Some((key, img)) = app.frames.get_for_viewer(&key, app.viewer_shown.as_ref().map(|(_, k)| k)) {
|
||||
let stale = app.viewer_shown.as_ref().is_none_or(|(_, k)| *k != key);
|
||||
// The frame on screen. While a drag or a property scrub is rendering, the exact key changes
|
||||
// with every edit and its render is still in flight; the newest completed revision of the
|
||||
// same frame keeps the picture following the pointer instead of freezing until it stops.
|
||||
if let Some((k, img)) = app.frames.get_for_viewer(&key, app.viewer_shown.as_ref().map(|(_, s)| s)) {
|
||||
let stale = app.viewer_shown.as_ref().is_none_or(|(_, s)| *s != k);
|
||||
// A CPU frame goes up again when the magnification calls for another factor.
|
||||
let minify = minify_factor(app.session.prefs.viewer_zoom_smooth(), scale, (zoom * ppp) as f64);
|
||||
let refit = !app.viewer_on_gpu() && app.viewer_tex.as_ref().is_some_and(|(_, _, m)| *m != minify);
|
||||
if stale || refit {
|
||||
show_frame(app, &ctx, key, img, minify);
|
||||
} else if let Some((_, k)) = app.viewer_shown.as_mut() {
|
||||
show_frame(app, &ctx, k, img, minify);
|
||||
} else if let Some((_, s)) = app.viewer_shown.as_mut() {
|
||||
// The same frame at a later revision (an edit elsewhere, an undo): it shows that one.
|
||||
k.revision = key.revision;
|
||||
s.revision = k.revision;
|
||||
}
|
||||
}
|
||||
if app.ui.viewer.transparency_grid {
|
||||
@@ -1173,7 +1177,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
anchor_only: mods.alt,
|
||||
})
|
||||
}),
|
||||
t if t.is_shape() => Some(Gesture::Create { tool: t, start: cpt }),
|
||||
// From where the button went down: by the time the pointer has moved far enough to
|
||||
// count as a drag it has already left the press point, and the shape is drawn there.
|
||||
t if t.is_shape() => Some(Gesture::Create { tool: t, start: map.to_comp(press) }),
|
||||
t if t.puppet_kind().is_some() && super::puppet_tool::hit(&pin_hits, press).is_none() => {
|
||||
// Off the pins: Alt or a press outside the art drags a marquee that selects pins.
|
||||
(mods.alt || puppet_layer(app, cpt).is_none()).then_some(Gesture::PinMarquee { start: press })
|
||||
@@ -1380,10 +1386,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Gesture::Create { start, .. } => {
|
||||
let a = map.to_screen(start);
|
||||
let r = Rect::from_two_pos(a, pos);
|
||||
painter.rect_stroke(r, 0.0, Stroke::new(1.0, t.accent), StrokeKind::Middle);
|
||||
Gesture::Create { tool, start } => {
|
||||
if let Some(d) = shape_drag(app, tool, start, cpt, mods) {
|
||||
draw_shape_ghost(&painter, &map, app, &d, &t);
|
||||
}
|
||||
}
|
||||
Gesture::Vertices { start, src, applied, inv, layer } => {
|
||||
// The grabbed vertex snaps (to other layers' vertices and features, guides, grid).
|
||||
@@ -1522,7 +1528,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
Gesture::PuppetPin { layer, pin, .. } => super::puppet_tool::reveal_pin(app, layer, pin),
|
||||
Gesture::Create { tool, start } => {
|
||||
let end = map.to_comp(pos);
|
||||
create_shape(app, tool, start, end, mods.shift);
|
||||
create_shape(app, tool, start, end, mods);
|
||||
}
|
||||
Gesture::Overlay(ov::Drag::Roi { start, keep }) => {
|
||||
let c = map.to_comp(pos);
|
||||
@@ -1896,38 +1902,159 @@ pub(crate) fn pick(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], togg
|
||||
Some(hit)
|
||||
}
|
||||
|
||||
fn create_shape(app: &mut EffectcraftApp, tool: Tool, a: [f64; 2], b: [f64; 2], square: bool) {
|
||||
let mut w = (b[0] - a[0]).abs();
|
||||
let mut h = (b[1] - a[1]).abs();
|
||||
if w < 2.0 && h < 2.0 {
|
||||
return;
|
||||
/// Where a shape drag is going to land: a mask on the selected layer, a group inside it, or a new
|
||||
/// shape layer.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum ShapeTarget {
|
||||
Mask,
|
||||
IntoLayer,
|
||||
NewLayer,
|
||||
}
|
||||
|
||||
impl ShapeTarget {
|
||||
/// What the drag will make and where, named the way the ghost captions it.
|
||||
fn label(self, layer_name: &str) -> String {
|
||||
match self {
|
||||
ShapeTarget::Mask => format!("Mask on “{layer_name}”"),
|
||||
ShapeTarget::IntoLayer => format!("Into “{layer_name}”"),
|
||||
ShapeTarget::NewLayer => "New shape layer".to_string(),
|
||||
}
|
||||
}
|
||||
if square {
|
||||
let m = w.max(h);
|
||||
w = m;
|
||||
h = m;
|
||||
}
|
||||
let cx = a[0].min(b[0]) + w / 2.0;
|
||||
let cy = a[1].min(b[1]) + h / 2.0;
|
||||
let kind = match tool {
|
||||
}
|
||||
|
||||
/// A shape drag as it stands, after the Tools' normalisations: the path it will make and where it
|
||||
/// will land.
|
||||
struct ShapeDrag {
|
||||
/// The `shape.newShape` / `layer.addMask` kind for the tool.
|
||||
kind: &'static str,
|
||||
/// Comp-space centre and size, what the drag will make.
|
||||
centre: [f64; 2],
|
||||
size: [f64; 2],
|
||||
target: ShapeTarget,
|
||||
/// The selected layer's name and the index its next mask would take, for the ghost's caption
|
||||
/// and colour.
|
||||
layer_name: String,
|
||||
mask_index: usize,
|
||||
}
|
||||
|
||||
/// The kind `tool` draws, as `shape.newShape` / `layer.addMask` name it.
|
||||
fn shape_kind(tool: Tool) -> &'static str {
|
||||
match tool {
|
||||
Tool::Rectangle => "rect",
|
||||
Tool::RoundedRect => "rounded",
|
||||
Tool::Ellipse => "ellipse",
|
||||
Tool::Polygon => "polygon",
|
||||
_ => "star",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// What a shape tool drag from `start` to `end` makes, or `None` while it is too small to make
|
||||
/// anything. Alt (Option) draws from the press point and Shift squares the box; the live ghost and
|
||||
/// [`create_shape`] both go through this, so the drag always shows what it will commit.
|
||||
fn shape_drag(app: &EffectcraftApp, tool: Tool, start: [f64; 2], end: [f64; 2], mods: egui::Modifiers) -> Option<ShapeDrag> {
|
||||
let comp = app.session.active_comp()?;
|
||||
let selected = app.session.state.selected_layers.first().copied();
|
||||
let sel = selected.and_then(|id| comp.layer(id));
|
||||
// A footage, solid or other non-shape layer selected, or a shape layer with Tool Creates
|
||||
// Mask: a mask on it instead.
|
||||
let creates_mask = app.session.state.shape_tool.creates_mask;
|
||||
let sel = app.session.state.selected_layers.first().copied();
|
||||
let sel_layer = sel.and_then(|id| app.session.active_comp().and_then(|c| c.layer(id)).cloned());
|
||||
if let Some(l) = sel_layer
|
||||
&& l.source.is_av()
|
||||
&& (creates_mask || !matches!(l.source, effectcraft_engine::project::LayerSource::Shape))
|
||||
{
|
||||
// Mask in layer space: invert the layer transform.
|
||||
let comp = app.session.active_comp_arc();
|
||||
if let Some(comp) = comp {
|
||||
// Mask: a mask on that layer. Otherwise the engine's own target: the first selected unlocked
|
||||
// shape layer, else a new one.
|
||||
let target = match sel {
|
||||
Some(l) if l.source.is_av() && (app.session.state.shape_tool.creates_mask || !matches!(l.source, LayerSource::Shape)) => (ShapeTarget::Mask, Some(l)),
|
||||
_ => match effectcraft_engine::commands::shape_tool::selected_shape_target(&app.session, comp).and_then(|id| comp.layer(id)) {
|
||||
Some(l) => (ShapeTarget::IntoLayer, Some(l)),
|
||||
None => (ShapeTarget::NewLayer, None),
|
||||
},
|
||||
};
|
||||
let (target, layer) = target;
|
||||
let mask_index = if target == ShapeTarget::Mask { layer.and_then(|l| l.masks()).map_or(0, |m| m.groups().count()) } else { 0 };
|
||||
let (mut w, mut h) = ((end[0] - start[0]).abs(), (end[1] - start[1]).abs());
|
||||
// Alt doubles the box about the press point, which is then its centre.
|
||||
if mods.alt {
|
||||
w *= 2.0;
|
||||
h *= 2.0;
|
||||
}
|
||||
if w < 2.0 && h < 2.0 {
|
||||
return None;
|
||||
}
|
||||
if mods.shift {
|
||||
let m = w.max(h);
|
||||
w = m;
|
||||
h = m;
|
||||
}
|
||||
Some(ShapeDrag {
|
||||
kind: shape_kind(tool),
|
||||
centre: if mods.alt { start } else { [start[0].min(end[0]) + w / 2.0, start[1].min(end[1]) + h / 2.0] },
|
||||
size: [w, h],
|
||||
target,
|
||||
layer_name: layer.map_or_else(String::new, |l| l.name.clone()),
|
||||
mask_index,
|
||||
})
|
||||
}
|
||||
|
||||
/// The Tools bar's paint colour as bytes, dimmed by `alpha` so the comp shows through the ghost.
|
||||
fn paint_rgb(c: [f32; 3], opacity: f64, alpha: f32) -> Color32 {
|
||||
let f = |k: f32| (k.clamp(0.0, 1.0) * 255.0).round() as u8;
|
||||
Color32::from_rgb(f(c[0]), f(c[1]), f(c[2])).gamma_multiply((opacity.clamp(0.0, 100.0) / 100.0) as f32 * alpha)
|
||||
}
|
||||
|
||||
/// Draw a shape drag as it will land: the tool's own path, filled with the Tools bar's Fill and
|
||||
/// outlined in its Stroke (or the mask colour, for a mask), captioned with where it goes.
|
||||
fn draw_shape_ghost(painter: &egui::Painter, map: &ViewerMap, app: &EffectcraftApp, d: &ShapeDrag, t: &Tokens) {
|
||||
// The engine's own geometry for the kind, so the ghost is the shape and not a stand-in for it.
|
||||
let Some(sp) = effectcraft_engine::commands::shape_tool::mask_path(d.kind, d.centre, d.size) else { return };
|
||||
let mut pts = ov::flatten(&sp, &Mat3::IDENTITY, map);
|
||||
if pts.first() == pts.last() {
|
||||
pts.pop();
|
||||
}
|
||||
if pts.len() < 3 {
|
||||
return;
|
||||
}
|
||||
let tool = &app.session.state.shape_tool;
|
||||
let (fill, line) = match d.target {
|
||||
ShapeTarget::Mask => {
|
||||
let [r, g, b] = effectcraft_engine::prefs::mask_color(app.session.prefs.appearance.cycle_mask_colors, d.mask_index);
|
||||
(Color32::from_rgb(r, g, b).gamma_multiply(0.25), Color32::from_rgb(r, g, b))
|
||||
}
|
||||
_ => {
|
||||
let stroke_on = tool.stroke.kind != PaintKind::None && tool.stroke_width > 0.0;
|
||||
(
|
||||
if tool.fill.kind == PaintKind::None { Color32::TRANSPARENT } else { paint_rgb(tool.fill.color, tool.fill.opacity, 0.45) },
|
||||
if stroke_on { paint_rgb(tool.stroke.color, tool.stroke.opacity, 1.0) } else { t.accent },
|
||||
)
|
||||
}
|
||||
};
|
||||
let Some(bounds) = bounds_of(&pts) else { return };
|
||||
// A screen-width outline reads at every zoom, where a comp-pixel hairline does not.
|
||||
painter.add(egui::Shape::Path(egui::epaint::PathShape { points: pts, closed: true, fill, stroke: Stroke::new(1.5, line).into() }));
|
||||
// Caption it with where the shape lands: a mask, a group in the selected shape layer or a
|
||||
// new layer.
|
||||
let font = Tokens::ui(11.0);
|
||||
let galley = painter.layout_no_wrap(d.target.label(&d.layer_name), font, line);
|
||||
let caption = Rect::from_min_size(pos2(bounds.left(), bounds.top() - 5.0 - galley.size().y), galley.size());
|
||||
painter.rect_filled(caption.expand(3.0), 2.0, Color32::from_black_alpha(150));
|
||||
painter.galley(caption.min, galley, line);
|
||||
}
|
||||
|
||||
/// The bounds of points on screen, or `None` for an empty set.
|
||||
fn bounds_of(pts: &[Pos2]) -> Option<Rect> {
|
||||
let first = *pts.first()?;
|
||||
let mut r = Rect::from_min_max(first, first);
|
||||
for p in pts.iter().skip(1) {
|
||||
r.extend_with(*p);
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
|
||||
fn create_shape(app: &mut EffectcraftApp, tool: Tool, a: [f64; 2], b: [f64; 2], mods: egui::Modifiers) {
|
||||
let Some(d) = shape_drag(app, tool, a, b, mods) else { return };
|
||||
let (kind, (w, h), (cx, cy)) = (d.kind, (d.size[0], d.size[1]), (d.centre[0], d.centre[1]));
|
||||
// A mask on the selected layer, as the ghost showed.
|
||||
if d.target == ShapeTarget::Mask {
|
||||
// In layer space: invert the layer transform.
|
||||
let sel_layer = app.session.state.selected_layers.first().copied().and_then(|id| app.session.active_comp().and_then(|c| c.layer(id)).cloned());
|
||||
if let Some(l) = sel_layer
|
||||
&& let Some(comp) = app.session.active_comp_arc()
|
||||
{
|
||||
let ectx = EvalCtx {
|
||||
project: &app.session.project,
|
||||
comp_id: app.session.active_comp_id().unwrap_or_default(),
|
||||
@@ -1943,8 +2070,8 @@ fn create_shape(app: &mut EffectcraftApp, tool: Tool, a: [f64; 2], b: [f64; 2],
|
||||
if let Err(e) = app.session.execute("layer.addMask", json!({"layer": l.id.0, "shape": kind, "rect": rect})) {
|
||||
app.ui.status = e.to_string();
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Into the selected shape layer's Contents as a new group (as in After Effects), else a new
|
||||
// shape layer; painted with the Tools bar's Fill and Stroke.
|
||||
|
||||
@@ -98,7 +98,9 @@ pub(crate) fn path_of(ectx: &EvalCtx, layer: LayerId, uid: u64) -> Option<PathIn
|
||||
Some(PathInfo { layer, uid, m, sp, is_mask, feather: Vec::new() })
|
||||
}
|
||||
|
||||
fn flatten(sp: &ShapePath, m: &Mat3, map: &ViewerMap) -> Vec<Pos2> {
|
||||
/// The path on screen: `sp` through `m` and the viewer map, flattened, with the closing point
|
||||
/// repeated for a closed path.
|
||||
pub(crate) fn flatten(sp: &ShapePath, m: &Mat3, map: &ViewerMap) -> Vec<Pos2> {
|
||||
let k = effectcraft_engine::render::kurbo_path(sp);
|
||||
let mut pts: Vec<Pos2> = vec![];
|
||||
kurbo::flatten(k.iter(), 0.5, |el| match el {
|
||||
|
||||
Reference in New Issue
Block a user