spine2d 0.3.0

Pure Rust runtime for Spine 4.3 (unofficial)
Documentation
use crate::{MixBlend, Skeleton, SkeletonData, apply_animation};

const JSON: &str = r#"
{
  "skeleton": { "spine": "4.3.00" },
  "bones": [
    { "name": "root", "x": 1, "y": 2, "length": 3 }
  ],
  "slots": [
    { "name": "pathSlot", "bone": "root", "attachment": "p" }
  ],
  "skins": [
    {
      "name": "default",
      "attachments": {
        "pathSlot": {
          "p": {
            "type": "path",
            "vertexCount": 6,
            "vertices": [ 0, 0, 0, 0, 3.3333333, 0, 6.6666665, 0, 10, 0, 10, 0 ],
            "lengths": [ 10 ],
            "closed": false,
            "constantSpeed": true
          }
        }
      }
    }
  ],
  "path": [
    {
      "name": "pc_fixed",
      "order": 0,
      "bones": ["root"],
      "target": "pathSlot",
      "positionMode": "fixed",
      "spacingMode": "length",
      "rotateMode": "tangent",
      "position": 2,
      "spacing": 3,
      "mixRotate": 0,
      "mixX": 0,
      "mixY": 0
    },
    {
      "name": "pc_percent",
      "order": 0,
      "bones": ["root"],
      "target": "pathSlot",
      "positionMode": "percent",
      "spacingMode": "percent",
      "rotateMode": "tangent",
      "position": 0.25,
      "spacing": 0.1,
      "mixRotate": 0,
      "mixX": 0,
      "mixY": 0
    }
  ],
  "animations": {
    "anim": {
      "path": {
        "pc_fixed": {
          "position": [
            { "time": 0.0, "position": 2.0 },
            { "time": 1.0, "position": 4.0 }
          ],
          "spacing": [
            { "time": 0.0, "spacing": 3.0 },
            { "time": 1.0, "spacing": 5.0 }
          ]
        },
        "pc_percent": {
          "position": [
            { "time": 0.0, "position": 0.25 },
            { "time": 1.0, "position": 0.50 }
          ],
          "spacing": [
            { "time": 0.0, "spacing": 0.1 },
            { "time": 1.0, "spacing": 0.2 }
          ]
        }
      }
    }
  }
}
"#;

fn assert_approx(actual: f32, expected: f32) {
    let diff = (actual - expected).abs();
    assert!(
        diff <= 1.0e-3,
        "expected {expected}, got {actual} (diff {diff})"
    );
}

#[test]
fn json_scale_applies_to_geometry_and_conditionally_to_path_fields() {
    let data = SkeletonData::from_json_str_with_scale(JSON, 2.0).unwrap();

    assert_approx(data.bones[0].x, 2.0);
    assert_approx(data.bones[0].y, 4.0);
    assert_approx(data.bones[0].length, 6.0);

    let fixed = &data.path_constraints[0];
    assert_approx(fixed.position, 4.0);
    assert_approx(fixed.spacing, 6.0);

    let percent = &data.path_constraints[1];
    assert_approx(percent.position, 0.25);
    assert_approx(percent.spacing, 0.1);

    let anim = data.animation("anim").unwrap().1.clone();
    let mut skeleton = Skeleton::new(data);
    skeleton.set_to_setup_pose();
    apply_animation(&anim, &mut skeleton, 1.0, false, 1.0, MixBlend::Replace);

    assert_approx(skeleton.path_constraints[0].position, 8.0);
    assert_approx(skeleton.path_constraints[0].spacing, 10.0);
    assert_approx(skeleton.path_constraints[1].position, 0.50);
    assert_approx(skeleton.path_constraints[1].spacing, 0.2);
}