feat: render interactive manuscript from Endor through generic graphics
This commit is contained in:
@@ -474,6 +474,8 @@ pub struct MountGeometry {
|
||||
pub width: f32,
|
||||
pub height: f32,
|
||||
pub interactive: bool,
|
||||
#[serde(default)]
|
||||
pub pointer_events: bool,
|
||||
}
|
||||
|
||||
impl MountGeometry {
|
||||
@@ -560,6 +562,19 @@ fn default_background() -> String {
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
/// 可校验、可序列化的逻辑绘制命令;转换到设备像素在 display_list 阶段完成。
|
||||
pub enum RenderCommand {
|
||||
#[serde(skip_deserializing)]
|
||||
Image {
|
||||
x: f32,
|
||||
y: f32,
|
||||
width: f32,
|
||||
height: f32,
|
||||
offset_x: f32,
|
||||
offset_y: f32,
|
||||
angle: f32,
|
||||
opacity: f32,
|
||||
tint: Option<Color>,
|
||||
image: Arc<crate::render::image::Image>,
|
||||
},
|
||||
Scene3D {
|
||||
x: f32,
|
||||
y: f32,
|
||||
@@ -603,6 +618,29 @@ impl RenderCommand {
|
||||
/// 检查当前输入或发布值的不变量,调用方据校验结果决定是否继续。
|
||||
pub(crate) fn validate(&self) -> Result<()> {
|
||||
match self {
|
||||
Self::Image {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
offset_x,
|
||||
offset_y,
|
||||
angle,
|
||||
opacity,
|
||||
image,
|
||||
..
|
||||
} => {
|
||||
validate_rect(*x, *y, *width, *height, 0.)?;
|
||||
if ![offset_x, offset_y, angle, opacity]
|
||||
.iter()
|
||||
.all(|v| v.is_finite())
|
||||
|| !(0. ..=1.).contains(opacity)
|
||||
|| image.width == 0
|
||||
|| image.height == 0
|
||||
{
|
||||
bail!("invalid image command");
|
||||
}
|
||||
}
|
||||
Self::Scene3D {
|
||||
x,
|
||||
y,
|
||||
@@ -676,6 +714,25 @@ impl RenderCommand {
|
||||
/// 把一条已验证的发布命令变成原生绘制原语,缩放几何和文字样式。
|
||||
fn to_draw_command(&self, scale: f32) -> Result<DrawCommand> {
|
||||
Ok(match self {
|
||||
Self::Image {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
offset_x,
|
||||
offset_y,
|
||||
angle,
|
||||
opacity,
|
||||
tint,
|
||||
image,
|
||||
} => DrawCommand::Image {
|
||||
image: image.clone(),
|
||||
origin: Point::new(x * scale, y * scale),
|
||||
rect: scaled_rect(*offset_x, *offset_y, *width, *height, scale),
|
||||
angle: *angle,
|
||||
opacity: *opacity,
|
||||
tint: *tint,
|
||||
},
|
||||
Self::Scene3D {
|
||||
x,
|
||||
y,
|
||||
|
||||
@@ -70,6 +70,72 @@ impl NativeCallable for At {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Edit {
|
||||
owner: ObjectRef,
|
||||
desktop: ObjectRef,
|
||||
remove: bool,
|
||||
persistent: bool,
|
||||
extend: bool,
|
||||
}
|
||||
impl NativeCallable for Edit {
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(1))
|
||||
}
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let mut members = items(cx.store(), self.owner)?.to_vec();
|
||||
if self.extend {
|
||||
let [Value::Object(other)] = args else {
|
||||
return Err(VmError::InvalidArgument("extend expects ObjectList"));
|
||||
};
|
||||
let incoming = items(cx.store(), *other)?;
|
||||
if members.len() + incoming.len() > 10_000 {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"object list exceeds 10000 members",
|
||||
));
|
||||
}
|
||||
members.extend_from_slice(incoming);
|
||||
} else if self.remove {
|
||||
let [Value::Number(index)] = args else {
|
||||
return Err(VmError::InvalidArgument("remove expects index"));
|
||||
};
|
||||
let index = index
|
||||
.as_i64()
|
||||
.and_then(|v| usize::try_from(v).ok())
|
||||
.filter(|&i| i < members.len())
|
||||
.ok_or(VmError::InvalidArgument("list index out of range"))?;
|
||||
members.remove(index);
|
||||
} else {
|
||||
let [Value::Object(item)] = args else {
|
||||
return Err(VmError::InvalidArgument("append expects object"));
|
||||
};
|
||||
if members.len() >= 10_000 {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"object list exceeds 10000 members",
|
||||
));
|
||||
}
|
||||
members.push(*item);
|
||||
}
|
||||
if self.persistent {
|
||||
return Ok(NativeResult::Return(Value::Object(collection(
|
||||
cx,
|
||||
members,
|
||||
self.desktop,
|
||||
)?)));
|
||||
}
|
||||
let count = members.len();
|
||||
let first = members
|
||||
.first()
|
||||
.copied()
|
||||
.map(Value::Object)
|
||||
.unwrap_or(Value::Nil);
|
||||
cx.replace_payload(self.owner, ObjectList(members))?;
|
||||
cx.set_attr(self.owner, "count", Value::Number((count as i64).into()))?;
|
||||
cx.set_attr(self.owner, "first", first)?;
|
||||
Ok(NativeResult::Return(Value::Nil))
|
||||
}
|
||||
}
|
||||
|
||||
/// 将已有对象引用包装为新的 ObjectList,并安装默认名义类型及下标查询方法。
|
||||
pub fn collection(
|
||||
cx: &mut NativeContext<'_>,
|
||||
@@ -108,6 +174,26 @@ fn build(
|
||||
let at = cx.allocate(ContextNativeFunction::new("at", At(set), vec![set]))?;
|
||||
crate::ui_bridge::attach_desktop_type(cx, at, "Function", desktop)?;
|
||||
cx.set_attr(set, "at", Value::Object(at))?;
|
||||
for (name, remove, persistent, extend) in [
|
||||
("append", false, false, false),
|
||||
("remove", true, false, false),
|
||||
("added", false, true, false),
|
||||
("extend", false, false, true),
|
||||
] {
|
||||
let method = cx.allocate(ContextNativeFunction::new(
|
||||
name,
|
||||
Edit {
|
||||
owner: set,
|
||||
desktop,
|
||||
remove,
|
||||
persistent,
|
||||
extend,
|
||||
},
|
||||
vec![set, desktop],
|
||||
))?;
|
||||
crate::ui_bridge::attach_desktop_type(cx, method, "Function", desktop)?;
|
||||
cx.set_attr(set, name, Value::Object(method))?;
|
||||
}
|
||||
Ok(set)
|
||||
}
|
||||
|
||||
@@ -142,10 +228,12 @@ impl NativeCallable for Constructor {
|
||||
.payload::<endor_core::EndorTypeObject>(*definition, "Type")?
|
||||
.fields()
|
||||
.to_vec();
|
||||
if fields
|
||||
.iter()
|
||||
.any(|field| matches!(field.name.as_str(), "count" | "first" | "at"))
|
||||
{
|
||||
if fields.iter().any(|field| {
|
||||
matches!(
|
||||
field.name.as_str(),
|
||||
"count" | "first" | "at" | "append" | "remove" | "added" | "extend"
|
||||
)
|
||||
}) {
|
||||
return Err(VmError::InvalidArgument("reserved ObjectList field"));
|
||||
}
|
||||
let count = values
|
||||
|
||||
@@ -203,6 +203,7 @@ mod tests {
|
||||
protocol_key: "rect".into(),
|
||||
slots: Vec::new(),
|
||||
interactive: false,
|
||||
pointer_events: false,
|
||||
kind: UiKind::Rect {
|
||||
width: 500.0,
|
||||
height: 500.0,
|
||||
|
||||
@@ -223,6 +223,11 @@ impl RenderCache {
|
||||
continue;
|
||||
}
|
||||
path.remove(&data_ref);
|
||||
let pointer_connection = data.subscriptions.iter().any(|&o| {
|
||||
store
|
||||
.payload::<crate::event_dispatch::Connection>(o, "Subscription")
|
||||
.is_ok_and(|s| s.handler.is_some() && s.kind.starts_with("pointer"))
|
||||
});
|
||||
let mut protocol_key = String::new();
|
||||
let mut custom_event = false;
|
||||
if let Some(definition) = store.type_of(block)? {
|
||||
@@ -402,7 +407,9 @@ impl RenderCache {
|
||||
block_id: block.to_string(),
|
||||
protocol_key,
|
||||
slots: data.children.iter().map(|c| c.slot).collect(),
|
||||
interactive: matches!(&data.kind, Kind::Button | Kind::Viewport3D { .. })
|
||||
pointer_events: pointer_connection,
|
||||
interactive: pointer_connection
|
||||
|| matches!(&data.kind, Kind::Button | Kind::Viewport3D { .. })
|
||||
|| data.click.is_some()
|
||||
|| click_connection
|
||||
|| custom_event,
|
||||
|
||||
Reference in New Issue
Block a user