mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-21 10:01:55 +01:00
276 lines
12 KiB
Rust
276 lines
12 KiB
Rust
//! The Bone tool and armatures on the stage (ADR 0043).
|
|
//!
|
|
//! Bones show over the artwork of every visible armature layer as wedges, wide at the round
|
|
//! joint they turn about and narrowing to a smaller round end, in the layer's colour; the
|
|
//! selected bone in the selection colour. With the **Bone tool** a drag from one symbol
|
|
//! instance to another adds a bone (a drag starting on a bone's end continues the chain from
|
|
//! it), and a click picks a bone. With the **Selection tool** a drag on a bone, or on an
|
|
//! instance a bone moves, poses the armature by inverse kinematics; a drag on the root instance
|
|
//! moves the whole armature. Each drag is one undo step, and the pose keys the frame.
|
|
|
|
use crate::app::{App, Drag, Tool};
|
|
use crate::pathtools::Xf;
|
|
use crate::theme::{T, to_color32};
|
|
use anicraft_engine::doc::armature::{self, BoneAt};
|
|
use anicraft_engine::doc::{BoneId, ElementId, ElementKind, LayerId, LayerKind, evaluate_layer_elements};
|
|
use anicraft_engine::geom::Vec2;
|
|
use egui::{Color32, Key, Modifiers, Pos2, Stroke, pos2};
|
|
use serde_json::json;
|
|
|
|
/// How close (screen points) a press must be to a bone's end to grab it, or to its wedge.
|
|
const GRAB_END: f32 = 8.0;
|
|
const GRAB_BODY: f32 = 6.0;
|
|
/// Joint radius on screen, and the widest a wedge gets.
|
|
const JOINT: f32 = 5.0;
|
|
const END: f32 = 3.5;
|
|
const WIDE: f32 = 9.0;
|
|
|
|
/// One armature as the stage shows it at the playhead.
|
|
pub struct Shown {
|
|
pub layer: LayerId,
|
|
pub color: egui::Color32,
|
|
pub locked: bool,
|
|
pub bones: Vec<BoneAt>,
|
|
}
|
|
|
|
/// Every visible armature on the scene at the playhead, bottom layer first.
|
|
pub fn shown(app: &App) -> Vec<Shown> {
|
|
let tl = app.s.timeline();
|
|
tl.layers
|
|
.iter()
|
|
.enumerate()
|
|
.rev()
|
|
.filter(|(i, l)| l.kind == LayerKind::Armature && l.visible && !tl.hidden_by_folder(*i))
|
|
.filter_map(|(_, l)| {
|
|
let arm = l.armature.as_deref()?;
|
|
let (_, els) = evaluate_layer_elements(l, app.s.frame)?;
|
|
let pose = armature::pose_at(l, app.s.frame).unwrap_or_default();
|
|
Some(Shown { layer: l.id, color: to_color32(l.color), locked: l.locked || tl.locked_by_folder(l.id), bones: armature::bones_at(arm, &els, &pose) })
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// What part of a bone a point is on.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Part {
|
|
End,
|
|
Body,
|
|
}
|
|
|
|
/// The bone under screen point `p` (ends before bodies, top layer first) on an unlocked
|
|
/// armature.
|
|
pub fn hit(app: &App, xf: Xf, p: Pos2) -> Option<(LayerId, BoneId, Part)> {
|
|
let all = shown(app);
|
|
let open = || all.iter().rev().filter(|s| !s.locked);
|
|
for s in open() {
|
|
if let Some(b) = s.bones.iter().find(|b| xf.to_screen(b.tail).distance(p) <= GRAB_END) {
|
|
return Some((s.layer, b.id, Part::End));
|
|
}
|
|
}
|
|
for s in open() {
|
|
if let Some(b) = s.bones.iter().find(|b| segment_distance(p, xf.to_screen(b.head), xf.to_screen(b.tail)) <= GRAB_BODY) {
|
|
return Some((s.layer, b.id, Part::Body));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
fn segment_distance(p: Pos2, a: Pos2, b: Pos2) -> f32 {
|
|
let (ab, ap) = (b - a, p - a);
|
|
let t = if ab.length_sq() > 0.0 { (ap.dot(ab) / ab.length_sq()).clamp(0.0, 1.0) } else { 0.0 };
|
|
(a + ab * t).distance(p)
|
|
}
|
|
|
|
/// The topmost symbol instance under stage point `sp` other than `skip`, on an unlocked layer:
|
|
/// its layer and id.
|
|
fn instance_at(app: &App, sp: Vec2, slop: f32, skip: Option<ElementId>) -> Option<(LayerId, ElementId)> {
|
|
let dl = app.s.display_list();
|
|
let tl = app.s.timeline();
|
|
for item in dl.items.iter().rev() {
|
|
if Some(item.element) == skip || item.guide || item.masks.is_some() {
|
|
continue;
|
|
}
|
|
let Some(layer) = tl.layer(item.layer).filter(|l| !l.locked && !tl.locked_by_folder(l.id)) else { continue };
|
|
if item.clip.is_some_and(|c| !dl.clip_contains(c, sp)) {
|
|
continue;
|
|
}
|
|
let hw = item.stroke.as_ref().map_or(0.0, |s| s.reach() * item.transform.mean_scale());
|
|
let inside = item.fill.is_some() && item.path.contains(sp, &item.transform, item.fill_rule);
|
|
if !(inside || item.path.distance_to_edge(sp, &item.transform) <= hw + slop) {
|
|
continue;
|
|
}
|
|
let is_instance = evaluate_layer_elements(layer, app.s.frame).is_some_and(|(_, els)| els.iter().any(|e| e.id == item.element && matches!(e.kind, ElementKind::Symbol { .. })));
|
|
if is_instance {
|
|
return Some((layer.id, item.element));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
/// Selects bone `bone` on armature layer `layer` (its layer becomes the active one).
|
|
pub fn select(app: &mut App, layer: LayerId, bone: Option<BoneId>) {
|
|
app.bone_sel = bone;
|
|
if bone.is_some() && app.s.layer != layer {
|
|
app.run("layer.select", json!({"layer": layer.0}));
|
|
}
|
|
}
|
|
|
|
/// A Bone tool drag starting at screen point `press`: a new bone from the instance there (or
|
|
/// from the end of the bone there).
|
|
pub fn drag_start(app: &mut App, xf: Xf, press: Pos2) -> Drag {
|
|
let sp = xf.to_stage(press);
|
|
if let Some((layer, id, Part::End)) = hit(app, xf, press)
|
|
&& let Some(child) = app.s.timeline().layer(layer).and_then(|l| l.armature.as_ref()).and_then(|a| a.bone(id)).map(|b| b.child)
|
|
&& let Some(b) = shown(app).into_iter().find(|s| s.layer == layer).and_then(|s| s.bones.into_iter().find(|b| b.id == id))
|
|
{
|
|
// A chain joins end to start.
|
|
let head = pos2(b.tail.x, b.tail.y);
|
|
return Drag::Bone { from: child, head, cur: head };
|
|
}
|
|
match instance_at(app, sp, 3.0 / xf.zoom.max(1e-3), None) {
|
|
Some((_, from)) => Drag::Bone { from, head: pos2(sp.x, sp.y), cur: pos2(sp.x, sp.y) },
|
|
None => {
|
|
app.status_ok(tr!("Start a bone on a symbol instance (convert artwork with F8), then drag to the instance it moves."));
|
|
Drag::None
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Ends a Bone tool drag: adds the bone if it ends on another instance.
|
|
pub fn drag_end(app: &mut App, xf: Xf, from: ElementId, head: Pos2, cur: Pos2) {
|
|
let tail = Vec2::new(cur.x, cur.y);
|
|
let Some((_, to)) = instance_at(app, tail, 3.0 / xf.zoom.max(1e-3), Some(from)) else {
|
|
app.status_ok(tr!("A bone ends on another symbol instance: release over the instance it should move."));
|
|
return;
|
|
};
|
|
if let Some(v) = app.run("bone.add", json!({"from": from.0, "to": to.0, "head": [head.x, head.y], "tail": [tail.x, tail.y]})) {
|
|
let (layer, bone) = (LayerId(v["layer"].as_u64().unwrap_or(0)), v["bone"].as_u64().map(BoneId));
|
|
select(app, layer, bone);
|
|
app.status_ok("Added a bone. Drag a bone or an instance with the Selection tool to pose the armature. Ctrl+Z to undo.");
|
|
}
|
|
}
|
|
|
|
/// A Bone tool click: picks the bone there (or none).
|
|
pub fn click(app: &mut App, xf: Xf, p: Pos2) {
|
|
match hit(app, xf, p) {
|
|
Some((layer, id, _)) => select(app, layer, Some(id)),
|
|
None => app.bone_sel = None,
|
|
}
|
|
}
|
|
|
|
/// A Selection tool drag starting at screen point `press` on an armature: posing (on a bone, or
|
|
/// an instance a bone moves) or moving the whole armature (on its root). `None` when the press
|
|
/// isn't on an armature.
|
|
pub fn select_drag_start(app: &mut App, xf: Xf, press: Pos2) -> Option<Drag> {
|
|
let sp = xf.to_stage(press);
|
|
let tl = app.s.timeline();
|
|
let (layer, bone, local) = match hit(app, xf, press) {
|
|
Some((layer, id, _)) => (layer, Some(id), None),
|
|
None => {
|
|
let (layer, el) = instance_at(app, sp, 3.0 / xf.zoom.max(1e-3), None).filter(|(l, _)| tl.layer(*l).is_some_and(|l| l.kind == LayerKind::Armature))?;
|
|
let l = tl.layer(layer)?;
|
|
let into = l.armature.as_ref().and_then(|a| a.bone_into(el)).map(|b| b.id);
|
|
// The point grabbed, in the instance's own space: it follows the pointer.
|
|
let w = evaluate_layer_elements(l, app.s.frame).and_then(|(_, els)| els.iter().find(|e| e.id == el).map(|e| e.transform))?;
|
|
(layer, into, w.inverse().map(|m| m.apply(sp)))
|
|
}
|
|
};
|
|
match bone {
|
|
Some(id) => {
|
|
select(app, layer, Some(id));
|
|
let arm = app.s.timeline().layer(layer).and_then(|l| l.armature.clone())?;
|
|
if armature::movable_chain(&arm, id).is_empty() {
|
|
app.status_ok(tr!("That bone is pinned (or hangs from a pinned bone): unpin it in Properties to drag it."));
|
|
return Some(Drag::None);
|
|
}
|
|
app.s.begin_gesture(tr_noop!("Pose Armature"));
|
|
let local = local.or_else(|| arm.bone(id).map(|b| b.tail)).unwrap_or(Vec2::new(0.0, 0.0));
|
|
Some(Drag::BonePose { bone: id, local: pos2(local.x, local.y) })
|
|
}
|
|
None => {
|
|
select(app, layer, None);
|
|
app.run("layer.select", json!({"layer": layer.0}));
|
|
app.s.begin_gesture(tr_noop!("Move Armature"));
|
|
Some(Drag::ArmatureMove { layer, last: pos2(sp.x, sp.y) })
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Continues a pose or armature drag to stage point `c`.
|
|
pub fn drag_to(app: &mut App, drag: &mut Drag, c: Pos2) {
|
|
match drag {
|
|
Drag::BonePose { bone, local } => {
|
|
app.run("bone.solve", json!({"bone": bone.0, "x": c.x, "y": c.y, "local": [local.x, local.y]}));
|
|
}
|
|
Drag::ArmatureMove { layer, last } => {
|
|
let d = c - *last;
|
|
if d.length_sq() > 0.0 {
|
|
*last = c;
|
|
app.run("bone.pose", json!({"layer": layer.0, "dx": d.x, "dy": d.y}));
|
|
}
|
|
}
|
|
Drag::Bone { cur, .. } => *cur = c,
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// Delete removes the selected bone (and those below it); Esc deselects it.
|
|
pub fn consume_keys(app: &mut App, ctx: &egui::Context) {
|
|
let Some(id) = app.bone_sel else { return };
|
|
if !matches!(app.tool, Tool::Bone | Tool::Select) {
|
|
return;
|
|
}
|
|
let take = |k: Key| ctx.input_mut(|i| i.consume_key(Modifiers::NONE, k));
|
|
if take(Key::Delete) || take(Key::Backspace) {
|
|
app.request("bone.remove", json!({"bone": id.0}));
|
|
} else if take(Key::Escape) {
|
|
app.bone_sel = None;
|
|
}
|
|
}
|
|
|
|
/// Drops a selected bone that no longer exists.
|
|
pub fn upkeep(app: &mut App) {
|
|
if let Some(id) = app.bone_sel {
|
|
let tl = app.s.timeline();
|
|
if !tl.layers.iter().any(|l| l.kind == LayerKind::Armature && l.armature.as_ref().is_some_and(|a| a.bone(id).is_some())) {
|
|
app.bone_sel = None;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The bones of every visible armature, and the bone being drawn.
|
|
pub fn draw(app: &App, painter: &egui::Painter, xf: Xf) {
|
|
let tool = matches!(app.tool, Tool::Bone | Tool::Select);
|
|
for s in shown(app) {
|
|
for b in &s.bones {
|
|
let picked = app.bone_sel == Some(b.id);
|
|
let color = if picked { T.selection } else { s.color };
|
|
// Bones fade when they can't be edited (another tool, a locked layer).
|
|
let color = if tool && !s.locked { color } else { color.gamma_multiply(0.5) };
|
|
wedge(painter, xf.to_screen(b.head), xf.to_screen(b.tail), color, picked);
|
|
}
|
|
}
|
|
if let Drag::Bone { head, cur, .. } = app.drag {
|
|
let (a, b) = (xf.to_screen(Vec2::new(head.x, head.y)), xf.to_screen(Vec2::new(cur.x, cur.y)));
|
|
wedge(painter, a, b, T.selection, true);
|
|
}
|
|
}
|
|
|
|
/// A bone from joint `a` to end `b` on screen: a wedge, widest just past the joint, with a
|
|
/// round joint and a smaller round end.
|
|
fn wedge(painter: &egui::Painter, a: Pos2, b: Pos2, color: Color32, strong: bool) {
|
|
let d = b - a;
|
|
let len = d.length();
|
|
let edge = Stroke::new(if strong { 1.5 } else { 1.0 }, color);
|
|
if len > JOINT * 2.0 {
|
|
let dir = d / len;
|
|
let n = egui::vec2(-dir.y, dir.x);
|
|
let w = (len * 0.12).clamp(JOINT, WIDE);
|
|
let shoulder = a + dir * (w * 1.2).min(len * 0.3);
|
|
let pts = vec![a, shoulder + n * w, b, shoulder - n * w];
|
|
painter.add(egui::Shape::convex_polygon(pts, color.gamma_multiply(if strong { 0.55 } else { 0.35 }), edge));
|
|
}
|
|
painter.circle(a, JOINT, Color32::WHITE, edge);
|
|
painter.circle(b, END, Color32::WHITE, edge);
|
|
}
|