Purple triangle
This commit is contained in:
commit
144a441e2e
|
@ -0,0 +1,3 @@
|
|||
[build]
|
||||
target = "wasm32-unknown-unknown"
|
||||
rustflags = ["-C", "link-arg=-s"]
|
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
/Cargo.lock
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "sao-ui-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
wee_alloc = "^0.4"
|
||||
|
||||
[profile.release]
|
||||
opt-level = "s"
|
||||
lto = "fat"
|
|
@ -0,0 +1,149 @@
|
|||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
|
||||
static mut PANEL_IMPLS: Vec<Box<dyn PanelImpl>> = Vec::new();
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn bind_panel(panel: u32) -> u32 {
|
||||
unsafe {
|
||||
let panel = UiPanel::bind(panel);
|
||||
let panel_impl = Box::new(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_event_at {
|
||||
($event: ident) => {
|
||||
#[no_mangle]
|
||||
pub extern "C" fn $event(id: u32, x: f32, y: f32) {
|
||||
unsafe {
|
||||
let panel = &mut PANEL_IMPLS[id as usize];
|
||||
let point = Point { x, y };
|
||||
(&mut *panel).$event(point);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
handle_event_at!(on_hover);
|
||||
handle_event_at!(on_select);
|
||||
handle_event_at!(on_drag);
|
||||
handle_event_at!(on_deselect);
|
||||
|
||||
#[allow(unused)]
|
||||
pub trait PanelImpl {
|
||||
fn update(&mut self, dt: f32) {}
|
||||
fn on_hover(&mut self, at: Point) {}
|
||||
fn on_select(&mut self, at: Point) {}
|
||||
fn on_drag(&mut self, at: Point) {}
|
||||
fn on_deselect(&mut self, at: Point) {}
|
||||
}
|
||||
|
||||
pub struct DummyPanel {
|
||||
panel: UiPanel,
|
||||
}
|
||||
|
||||
impl DummyPanel {
|
||||
fn bind(panel: UiPanel) -> Self {
|
||||
Self { panel }
|
||||
}
|
||||
}
|
||||
|
||||
impl PanelImpl for DummyPanel {
|
||||
fn update(&mut self, _dt: f32) {
|
||||
self.panel.draw_triangle(
|
||||
&Point { x: 0.0, y: 0.0 },
|
||||
&Point { x: 0.1, y: 0.0 },
|
||||
&Point { x: 0.0, y: 0.1 },
|
||||
&Color {
|
||||
r: 1.0,
|
||||
g: 0.0,
|
||||
b: 1.0,
|
||||
a: 1.0,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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(C)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct Point {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct Color {
|
||||
pub r: f32,
|
||||
pub g: f32,
|
||||
pub b: f32,
|
||||
pub a: f32,
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
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(&mut self, v1: &Point, v2: &Point, v3: &Point, 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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue