canary-rs/src/lib.rs

109 lines
2.6 KiB
Rust
Raw Normal View History

2022-10-07 21:52:35 +00:00
// Copyright (c) 2022 Marceline Cramer
2022-11-15 00:28:38 +00:00
// SPDX-License-Identifier: LGPL-3.0-or-later
2022-10-07 21:52:35 +00:00
pub use canary_script::*;
2022-07-18 23:44:42 +00:00
use parking_lot::{Mutex, RwLock};
use slab::Slab;
2022-07-19 03:24:39 +00:00
use std::collections::HashMap;
use std::sync::Arc;
2022-07-15 21:11:35 +00:00
2022-11-02 22:25:12 +00:00
pub mod backend;
2022-07-16 17:44:22 +00:00
pub mod text;
use backend::{Backend, Instance, ScriptAbi};
use text::FontStore;
2022-11-02 23:42:01 +00:00
/// The main interface to Canary.
pub struct Runtime {
backend: Box<dyn Backend>,
font_store: Arc<FontStore>,
2022-11-02 23:42:01 +00:00
}
impl Runtime {
pub fn new(backend: Box<dyn Backend>) -> anyhow::Result<Self> {
Ok(Self {
backend,
font_store: Arc::new(FontStore::new()),
})
2022-11-02 23:42:01 +00:00
}
pub fn load_module(&self, module: &[u8]) -> anyhow::Result<Script> {
let abi = ScriptAbi::new(self.font_store.to_owned());
2022-11-17 04:08:17 +00:00
let abi = Arc::new(abi);
let instance = self.backend.load_module(abi.to_owned(), module)?;
2022-11-02 23:42:01 +00:00
Ok(Script {
instance,
2022-11-17 04:08:17 +00:00
abi,
2022-11-02 23:42:01 +00:00
})
}
}
/// A loaded instance of a Canary script.
pub struct Script {
instance: Arc<dyn Instance>,
2022-11-17 04:08:17 +00:00
abi: Arc<ScriptAbi>,
2022-11-02 23:42:01 +00:00
}
impl Script {
pub fn create_panel(&mut self, protocol: &str, msg: Vec<u8>) -> anyhow::Result<Panel> {
2022-11-17 04:08:17 +00:00
let id = self.abi.create_panel();
let userdata = self.instance.bind_panel(id, protocol, msg);
2022-11-02 23:42:01 +00:00
Ok(Panel {
instance: self.instance.clone(),
2022-11-17 04:08:17 +00:00
abi: self.abi.clone(),
2022-11-02 23:42:01 +00:00
id,
userdata,
})
}
}
/// A Canary panel.
pub struct Panel {
instance: Arc<dyn Instance>,
2022-11-17 04:08:17 +00:00
abi: Arc<ScriptAbi>,
2022-11-02 23:42:01 +00:00
id: PanelId,
userdata: u32,
}
impl Panel {
pub fn update(&self, dt: f32) {
self.instance.update(self.userdata, dt);
}
pub fn draw(&self) -> Vec<DrawCommand> {
self.instance.draw(self.userdata)
}
2022-11-03 04:37:18 +00:00
pub fn on_resize(&self, new_size: Vec2) {
self.instance.on_resize(self.userdata, new_size);
}
2022-11-02 23:42:01 +00:00
pub fn on_cursor_event(&self, kind: CursorEventKind, at: Vec2) {
self.instance.on_cursor_event(self.userdata, kind, at);
}
pub fn on_message(&self, msg: Vec<u8>) {
self.instance.on_message(self.userdata, msg);
}
2022-11-17 04:08:17 +00:00
pub fn recv_messages(&self) -> Vec<Vec<u8>> {
self.abi.recv_panel_messages(self.id)
}
2022-11-02 23:42:01 +00:00
}
/// Proportion constant between pixels (at 96dpi) to millimeters (Canary's unit measurement).
pub const PX_PER_MM: f32 = 25.4 / 96.0;
2022-07-31 06:15:04 +00:00
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PanelId(pub(crate) usize);
2022-07-15 21:11:35 +00:00
#[non_exhaustive]
2022-11-02 23:42:01 +00:00
#[derive(Clone, Debug)]
2022-07-15 21:11:35 +00:00
pub enum DrawCommand {
Mesh {
vertices: Vec<MeshVertex>,
indices: Vec<MeshIndex>,
},
}