use canvist_scene::NodeId;
use canvist_scene::NodeKind;
use canvist_scene::Prop;
use canvist_scene::PropKey;
use canvist_scene::Scene;
use canvist_scene::Step;
use kurbo::Affine;
use kurbo::Point;
use kurbo::Vec2;
use serde_json::Value as Json;
use serde_json::json;
use crate::CanvasError;
use crate::CanvasState;
#[derive(Clone, Debug, PartialEq)]
pub enum Control {
Number {
min: Option<f64>,
max: Option<f64>,
step: f64,
unit: &'static str,
},
Angle,
Toggle,
Select(Vec<&'static str>),
Text,
Paints,
Json,
}
impl Control {
#[must_use]
pub fn number(step: f64, unit: &'static str) -> Self {
Self::Number {
min: None,
max: None,
step,
unit,
}
}
#[must_use]
pub fn meta(&self) -> Json {
match self {
Self::Number {
min,
max,
step,
unit,
} => {
json!({ "type": "number", "min": min, "max": max, "step": step, "unit": unit })
}
Self::Angle => json!({ "type": "angle" }),
Self::Toggle => json!({ "type": "toggle" }),
Self::Select(options) => json!({ "type": "select", "options": options }),
Self::Text => json!({ "type": "text" }),
Self::Paints => json!({ "type": "paints" }),
Self::Json => json!({ "type": "json" }),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Field {
pub key: String,
pub label: String,
pub control: Control,
}
impl Field {
#[must_use]
pub fn new(key: &str, label: &str, control: Control) -> Self {
Self {
key: key.to_owned(),
label: label.to_owned(),
control,
}
}
#[must_use]
pub fn meta(&self) -> Json {
json!({ "key": self.key, "label": self.label, "control": self.control.meta() })
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Section {
pub id: String,
pub title: String,
pub kinds: Option<Vec<NodeKind>>,
pub fields: Vec<Field>,
}
impl Section {
#[must_use]
pub fn new(id: &str, title: &str, fields: Vec<Field>) -> Self {
Self {
id: id.to_owned(),
title: title.to_owned(),
kinds: None,
fields,
}
}
#[must_use]
pub fn for_kinds(mut self, kinds: &[NodeKind]) -> Self {
self.kinds = Some(kinds.to_vec());
self
}
fn applies(&self, kind: NodeKind) -> bool {
self.kinds
.as_ref()
.is_none_or(|kinds| kinds.contains(&kind))
}
}
const SHAPED: [NodeKind; 11] = [
NodeKind::Frame,
NodeKind::Rectangle,
NodeKind::Ellipse,
NodeKind::Polygon,
NodeKind::Path,
NodeKind::Boolean,
NodeKind::Image,
NodeKind::Text,
NodeKind::Component,
NodeKind::Instance,
NodeKind::Custom,
];
#[must_use]
pub fn core_sections() -> Vec<Section> {
vec![
layer_section(),
Section::new(
"geometry",
"Geometry",
vec![
Field::new("x", "X", Control::number(1.0, "px")),
Field::new("y", "Y", Control::number(1.0, "px")),
Field::new("width", "W", Control::number(1.0, "px")),
Field::new("height", "H", Control::number(1.0, "px")),
Field::new("rotation", "Rotation", Control::Angle),
],
)
.for_kinds(&SHAPED),
Section::new(
"fill",
"Fill",
vec![Field::new("fills", "Fills", Control::Paints)],
)
.for_kinds(&SHAPED),
Section::new(
"stroke",
"Stroke",
vec![
Field::new("strokes", "Strokes", Control::Paints),
Field::new("stroke", "Style", Control::Json),
],
)
.for_kinds(&SHAPED),
Section::new(
"effects",
"Effects",
vec![Field::new("effects", "Effects", Control::Json)],
)
.for_kinds(&SHAPED),
]
}
fn layer_section() -> Section {
let blends: Vec<&'static str> = canvist_scene::BlendMode::ALL
.iter()
.map(|mode| mode.name())
.collect();
Section::new(
"layer",
"Layer",
vec![
Field::new("name", "Name", Control::Text),
Field::new("visible", "Visible", Control::Toggle),
Field::new("locked", "Locked", Control::Toggle),
Field::new(
"opacity",
"Opacity",
Control::Number {
min: Some(0.0),
max: Some(1.0),
step: 0.01,
unit: "",
},
),
Field::new("blend", "Blend", Control::Select(blends)),
],
)
}
#[must_use]
pub fn inspect(state: &CanvasState, sections: &[Section]) -> Json {
let scene = state.scene();
let nodes = state.selection().nodes();
let kinds: Vec<NodeKind> = nodes.iter().filter_map(|&id| scene.kind(id)).collect();
if kinds.is_empty() {
return json!({ "sections": [] });
}
let sections: Vec<Json> = sections
.iter()
.filter(|section| kinds.iter().all(|&kind| section.applies(kind)))
.map(|section| {
let fields: Vec<Json> = section
.fields
.iter()
.map(|field| field_value(scene, nodes, field))
.collect();
json!({ "id": section.id, "title": section.title, "fields": fields })
})
.collect();
json!({ "sections": sections })
}
fn field_value(scene: &Scene, nodes: &[NodeId], field: &Field) -> Json {
let mut values = nodes.iter().map(|&id| read(scene, id, &field.key));
let first = values.next().flatten();
let mixed = values.any(|value| value != first);
let mut out = field.meta();
if let Some(object) = out.as_object_mut() {
if mixed {
object.insert("mixed".into(), Json::Bool(true));
} else {
object.insert("value".into(), first.unwrap_or(Json::Null));
}
}
out
}
#[must_use]
pub fn read(scene: &Scene, id: NodeId, key: &str) -> Option<Json> {
let props = scene.node(id)?.props();
let geometry = Geometry::from_transform(props.transform, props.size);
Some(match key {
"x" => json!(geometry.origin.x),
"y" => json!(geometry.origin.y),
"width" => json!(props.size.x),
"height" => json!(props.size.y),
"rotation" => json!(geometry.rotation.to_degrees()),
_ => props.get(&PropKey::parse(key)?).to_json(),
})
}
pub fn set_field(state: &mut CanvasState, key: &str, value: &Json) -> Result<(), CanvasError> {
let nodes = state.selection().nodes().to_vec();
let steps = nodes
.iter()
.map(|&id| write(state.scene(), id, key, value))
.collect::<Result<Vec<_>, _>>()?;
state.apply(steps.into_iter().flatten().collect(), false)?;
Ok(())
}
fn write(scene: &Scene, id: NodeId, key: &str, value: &Json) -> Result<Vec<Step>, CanvasError> {
let Some(node) = scene.node(id) else {
return Ok(Vec::new());
};
let props = node.props();
let number = || {
value
.as_f64()
.filter(|number| number.is_finite())
.ok_or_else(|| CanvasError::InvalidArgs(format!("`{key}` needs a number")))
};
let set = |prop: Prop| Step::SetProp { node: id, prop };
let geometry = Geometry::from_transform(props.transform, props.size);
Ok(match key {
"x" => {
vec![set(Prop::Transform(
geometry.moved(Point::new(number()?, geometry.origin.y)),
))]
}
"y" => {
vec![set(Prop::Transform(
geometry.moved(Point::new(geometry.origin.x, number()?)),
))]
}
"width" => vec![set(Prop::Size(Vec2::new(number()?.max(0.0), props.size.y)))],
"height" => vec![set(Prop::Size(Vec2::new(props.size.x, number()?.max(0.0))))],
"rotation" => {
vec![set(Prop::Transform(
geometry.rotated(number()?.to_radians()),
))]
}
_ => {
let key =
PropKey::parse(key).ok_or_else(|| CanvasError::UnknownField(key.to_owned()))?;
vec![set(Prop::from_json(&key, value)?)]
}
})
}
struct Geometry {
transform: Affine,
size: Vec2,
origin: Point,
rotation: f64,
}
impl Geometry {
fn from_transform(transform: Affine, size: Vec2) -> Self {
let [a, b, ..] = transform.as_coeffs();
Self {
transform,
size,
origin: transform * Point::ORIGIN,
rotation: b.atan2(a),
}
}
fn moved(&self, origin: Point) -> Affine {
self.transform.with_translation(origin.to_vec2())
}
fn rotated(&self, angle: f64) -> Affine {
let center = self.transform * (self.size / 2.0).to_point();
let turn = Affine::rotate_about(angle - self.rotation, center);
turn * self.transform
}
}