canary-rs/crates/script/src/api/abi.rs

55 lines
1.4 KiB
Rust

// Copyright (c) 2022 Marceline Cramer
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]
use super::*;
static mut PANEL_IMPLS: Vec<Box<dyn PanelImpl>> = Vec::new();
pub fn bind_panel(
cb: impl Fn(Panel, Message, Message) -> Box<dyn PanelImpl>,
panel: u32,
protocol: u32,
msg: u32,
) -> u32 {
unsafe {
let panel = Panel(panel);
let protocol = Message(protocol);
let msg = Message(msg);
let panel_impl = cb(panel, protocol, msg);
let id = PANEL_IMPLS.len() as u32;
PANEL_IMPLS.push(panel_impl);
id
}
}
pub fn update(panel_data: u32, dt: f32) {
let panel = unsafe { &mut PANEL_IMPLS[panel_data as usize] };
panel.update(dt);
}
pub fn draw(panel_data: u32) {
let panel = unsafe { &mut PANEL_IMPLS[panel_data as usize] };
panel.draw();
}
pub fn on_resize(panel_data: u32, width: f32, height: f32) {
let panel = unsafe { &mut PANEL_IMPLS[panel_data as usize] };
let new_size = Vec2::new(width, height);
panel.on_resize(new_size);
}
pub fn on_cursor_event(panel_data: u32, kind: u32, x: f32, y: f32) {
let panel = unsafe { &mut PANEL_IMPLS[panel_data as usize] };
let at = Vec2::new(x, y);
let kind = CursorEventKind::from_u32(kind).unwrap();
panel.on_cursor_event(kind, at);
}
pub fn on_message(panel_data: u32, msg: u32) {
let panel = unsafe { &mut PANEL_IMPLS[panel_data as usize] };
let msg = Message(msg);
panel.on_message(msg)
}