use crate::{Color, Vec2, CursorEventKind}; static mut PANEL_IMPLS: Vec> = Vec::new(); #[no_mangle] pub extern "C" fn bind_panel(panel: u32) -> u32 { unsafe { let panel = UiPanel::bind(panel); let panel_impl = Box::new(crate::DummyPanel::bind(panel)); let id = PANEL_IMPLS.len() as u32; PANEL_IMPLS.push(panel_impl); id } } #[no_mangle] pub extern "C" fn update(dt: f32) { unsafe { for panel in PANEL_IMPLS.iter_mut() { panel.update(dt); } } } #[macro_export] macro_rules! handle_cursor_event { ($event: ident, $kind: expr) => { #[no_mangle] pub extern "C" fn $event(id: u32, x: f32, y: f32) { unsafe { let panel = &mut PANEL_IMPLS[id as usize]; let at = Vec2::new(x, y); (&mut *panel).on_cursor_event($kind, at); } } }; } handle_cursor_event!(on_hover, CursorEventKind::Hover); handle_cursor_event!(on_select, CursorEventKind::Select); handle_cursor_event!(on_drag, CursorEventKind::Drag); handle_cursor_event!(on_deselect, CursorEventKind::Deselect); #[allow(unused)] pub trait PanelImpl { fn update(&mut self, dt: f32) {} fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {} } extern "C" { fn UiPanel_getWidth(panel: u32) -> f32; fn UiPanel_getHeight(panel: u32) -> f32; fn UiPanel_setSize(panel: u32, width: f32, height: f32); fn UiPanel_setColor(panel: u32, r: f32, g: f32, b: f32, a: f32); #[link_name = "UiPanel_drawTriangle"] fn UiPanel_drawTriangle( panel: u32, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, r: f32, g: f32, b: f32, a: f32, ); } #[repr(transparent)] #[derive(Copy, Clone)] pub struct UiPanel(u32); impl UiPanel { pub unsafe fn bind(id: u32) -> Self { Self(id) } pub fn get_width(&self) -> f32 { unsafe { UiPanel_getWidth(self.0) } } pub fn get_height(&self) -> f32 { unsafe { UiPanel_getHeight(self.0) } } pub fn set_size(&mut self, width: f32, height: f32) { unsafe { UiPanel_setSize(self.0, width, height) } } pub fn set_color(&mut self, color: &Color) { unsafe { UiPanel_setColor(self.0, color.r, color.g, color.b, color.a) } } pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, color: Color) { unsafe { UiPanel_drawTriangle( self.0, v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, color.r, color.g, color.b, color.a, ) } } }