canary-rs/src/lib.rs

211 lines
5.9 KiB
Rust
Raw Normal View History

2022-10-07 21:52:35 +00:00
// Copyright (c) 2022 Marceline Cramer
// SDPX-License-Identifier: LGPL-3.0-or-later
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;
2022-11-02 23:42:01 +00:00
use backend::{Backend, Instance};
/// The main interface to Canary.
pub struct Runtime {
backend: Box<dyn Backend>,
}
impl Runtime {
pub fn new(backend: Box<dyn Backend>) -> anyhow::Result<Self> {
Ok(Self { backend })
}
pub fn load_module(&self, module: &[u8]) -> anyhow::Result<Script> {
let instance = self.backend.load_module(module)?;
Ok(Script {
instance,
next_panel: 0,
})
}
}
/// A loaded instance of a Canary script.
pub struct Script {
instance: Arc<dyn Instance>,
next_panel: usize,
}
impl Script {
pub fn create_panel(&mut self, msg: Vec<u8>) -> anyhow::Result<Panel> {
let id = PanelId(self.next_panel);
self.next_panel += 1;
let userdata = self.instance.bind_panel(id, msg);
Ok(Panel {
instance: self.instance.clone(),
id,
userdata,
})
}
}
/// A Canary panel.
pub struct Panel {
instance: Arc<dyn Instance>,
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);
}
}
/// Proportion constant between pixels (at 96dpi) to millimeters (Canary's unit measurement).
pub const PX_PER_MM: f32 = 25.4 / 96.0;
2022-07-15 21:11:35 +00:00
/// Low-level script API callbacks.
///
/// If you're a casual user of Canary the struct you're looking for is
/// [ScriptAbiImpl]. This trait exists to help with making mocks for testing.
2022-07-15 21:11:35 +00:00
pub trait ScriptAbi {
fn start_draw(&self);
fn draw_indexed(&self, vertices: &[MeshVertex], indices: &[MeshIndex]);
fn draw_text_layout(&self, id: u32, offset: Vec2, scale: f32, color: Color);
2022-07-15 21:11:35 +00:00
fn with_draw_commands(&self, f: impl FnOnce(&[DrawCommand]));
2022-07-18 23:44:42 +00:00
2022-07-19 03:24:39 +00:00
fn font_load(&self, family: &str) -> u32;
fn text_layout_new(&self, font_id: u32, text: &str) -> u32;
2022-07-18 23:44:42 +00:00
fn text_layout_delete(&self, id: u32);
fn text_layout_get_bounds(&self, id: u32, rect: &mut Rect);
2022-08-17 00:11:56 +00:00
fn message_new(&self, data: Vec<u8>) -> u32;
fn message_free(&self, id: u32);
fn message_get_len(&self, id: u32) -> u32;
fn message_get_data(&self, id: u32, dst: &mut [u8]);
2022-07-15 21:11:35 +00:00
}
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>,
},
}
/// The standard [ScriptAbi] implementation to use.
2022-07-15 21:11:35 +00:00
#[derive(Default)]
pub struct ScriptAbiImpl {
2022-07-15 21:11:35 +00:00
draw_cmds: Mutex<Vec<DrawCommand>>,
font_store: text::FontStore,
2022-07-19 03:24:39 +00:00
font_families: Mutex<HashMap<String, u32>>,
loaded_fonts: RwLock<Vec<Arc<text::Font>>>,
2022-07-18 23:44:42 +00:00
text_layouts: RwLock<Slab<text::TextLayout>>,
2022-08-17 00:11:56 +00:00
message_store: RwLock<Slab<Vec<u8>>>,
2022-07-15 21:11:35 +00:00
}
impl ScriptAbi for ScriptAbiImpl {
2022-07-15 21:11:35 +00:00
fn start_draw(&self) {
2022-07-16 17:44:22 +00:00
let mut lock = self.draw_cmds.lock();
lock.clear();
2022-07-15 21:11:35 +00:00
}
fn draw_indexed(&self, vertices: &[MeshVertex], indices: &[MeshIndex]) {
self.draw_cmds.lock().push(DrawCommand::Mesh {
vertices: vertices.to_vec(),
indices: indices.to_vec(),
})
}
fn draw_text_layout(&self, id: u32, offset: Vec2, scale: f32, color: Color) {
2022-07-19 03:24:39 +00:00
// TODO multiple fonts per layout
let layouts = self.text_layouts.read();
2022-07-27 05:32:18 +00:00
let layout = layouts.get(id as usize).unwrap();
let glyphs = layout.glyphs.as_slice();
2022-07-19 03:24:39 +00:00
let loaded = self.loaded_fonts.read();
2022-07-27 05:32:18 +00:00
let font = loaded.get(layout.font_id as usize).unwrap();
2022-07-19 02:51:54 +00:00
let cmds = font.draw(glyphs, offset, scale, color);
2022-07-18 23:51:33 +00:00
self.draw_cmds.lock().extend(cmds.into_iter());
}
2022-07-18 23:44:42 +00:00
fn with_draw_commands(&self, f: impl FnOnce(&[DrawCommand])) {
f(self.draw_cmds.lock().as_slice());
}
2022-07-19 03:24:39 +00:00
fn font_load(&self, family: &str) -> u32 {
let mut family_cache = self.font_families.lock();
if let Some(cached) = family_cache.get(family) {
return *cached;
}
let font = self.font_store.load_font(family);
let mut loaded = self.loaded_fonts.write();
let id = loaded.len() as u32;
family_cache.insert(family.to_string(), id);
loaded.push(font);
id
}
fn text_layout_new(&self, font_id: u32, text: &str) -> u32 {
let loaded = self.loaded_fonts.read();
let font = loaded.get(font_id as usize).unwrap();
2022-07-18 23:44:42 +00:00
let layout = font.shape(text);
self.text_layouts.write().insert(layout) as u32
}
2022-07-18 23:44:42 +00:00
fn text_layout_delete(&self, id: u32) {
self.text_layouts.write().remove(id as usize);
}
fn text_layout_get_bounds(&self, id: u32, dst: &mut Rect) {
let src = self.text_layouts.read().get(id as usize).unwrap().bounds;
let _ = std::mem::replace(dst, src);
}
2022-08-17 00:11:56 +00:00
fn message_new(&self, data: Vec<u8>) -> u32 {
let mut store = self.message_store.write();
let id = store.insert(data) as u32;
id
}
fn message_free(&self, id: u32) {
let mut store = self.message_store.write();
store.remove(id as usize);
}
fn message_get_len(&self, id: u32) -> u32 {
self.message_store.read().get(id as usize).unwrap().len() as u32
}
fn message_get_data(&self, id: u32, dst: &mut [u8]) {
let store = self.message_store.read();
let src = store.get(id as usize).unwrap();
dst.copy_from_slice(src);
}
2022-07-15 21:11:35 +00:00
}