use crate::json::JsonSkeleton;
use crate::skeleton::{Info, Skeleton};
use bevy_math::Vec2;
use serde::Deserialize;
use std::path::PathBuf;
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JsonInfo {
pub hash: String,
#[serde(rename = "spine")]
pub version: String,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
pub fps: Option<f32>,
pub images: Option<PathBuf>,
pub audio: Option<PathBuf>,
}
impl JsonInfo {
pub fn size(&self) -> Vec2 {
Vec2::new(self.width, self.height)
}
pub fn origin(&self) -> Vec2 {
Vec2::new(self.x, self.y)
}
}
impl From<JsonInfo> for Info {
fn from(json: JsonInfo) -> Self {
Info {
hash: json.hash.to_owned(),
version: json.version.to_owned(),
bottom_left: Vec2::new(json.x, json.y),
size: Vec2::new(json.width, json.height),
fps: json.fps,
images: json.images,
audio: json.audio,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic() {
let json = r#"
{
"hash": "itfFESDjM1c",
"spine": "4.1.06",
"x": -188.63,
"y": -7.94,
"width": 418.45,
"height": 686.2,
"images": "./images/",
"audio": ""
}
"#;
let skeleton = serde_json::from_str::<JsonInfo>(json).unwrap();
assert_eq!(skeleton.origin(), Vec2::new(-188.63, -7.94));
assert_eq!(skeleton.size(), Vec2::new(418.45, 686.2));
}
}