Initial on_resize() implementation

This commit is contained in:
lilith 2022-11-03 00:37:18 -04:00
parent d4fc420b18
commit 4b2396d137
7 changed files with 44 additions and 2 deletions

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Instant;
use canary::{Panel, Runtime};
use canary::{Panel, Runtime, Vec2};
use glium::backend::glutin::DisplayCreationError;
use glium::{glutin, Surface};
use glutin::event::{Event, WindowEvent};
@ -66,6 +66,10 @@ impl Window {
pub fn send_message(&mut self, msg: Vec<u8>) {
self.panel.on_message(msg);
}
pub fn resize(&mut self, new_size: Vec2) {
self.panel.on_resize(new_size);
}
}
pub struct WindowStore {
@ -100,7 +104,12 @@ impl WindowStore {
Event::WindowEvent { window_id, event } => {
if let Some(window) = self.windows.get_mut(&window_id) {
match event {
WindowEvent::Resized(_) => window.request_redraw(),
WindowEvent::Resized(size) => {
window.resize(
Vec2::new(size.width as f32, size.height as f32) * canary::PX_PER_MM
);
window.request_redraw()
},
_ => {}
}
}

View File

@ -28,6 +28,12 @@ pub fn draw(panel_data: u32) {
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);

View File

@ -25,6 +25,11 @@ macro_rules! export_abi {
::canary_script::api::abi::draw(panel_data)
}
#[no_mangle]
pub extern "C" fn on_resize(panel_data: u32, width: f32, height: f32) {
::canary_script::api::abi::on_resize(panel_data, width, height)
}
#[no_mangle]
pub extern "C" fn on_cursor_event(panel_data: u32, kind: u32, x: f32, y: f32) {
::canary_script::api::abi::on_cursor_event(panel_data, kind, x, y)
@ -44,6 +49,7 @@ pub trait BindPanel {
pub trait PanelImpl {
fn update(&mut self, dt: f32);
fn draw(&mut self);
fn on_resize(&mut self, new_size: Vec2);
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2);
fn on_message(&mut self, msg: Message);
}

View File

@ -39,6 +39,10 @@ impl PanelImpl for MusicPlayerPanel {
self.label.draw(&ctx, offset, size, color);
}
fn on_resize(&mut self, new_size: Vec2) {
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {}
fn on_message(&mut self, msg: Message) {

View File

@ -50,7 +50,10 @@ pub trait Instance {
fn draw(&self, panel_ud: u32) -> Vec<DrawCommand>;
fn on_resize(&self, panel_ud: u32, new_size: Vec2);
fn on_cursor_event(&self, panel_ud: u32, kind: CursorEventKind, at: Vec2);
fn on_message(&self, panel_ud: u32, msg: Vec<u8>);
}

View File

@ -37,6 +37,7 @@ impl Backend for WasmtimeBackend {
let bind_panel = instance.get_typed_func(&mut store, "bind_panel")?;
let update = instance.get_typed_func(&mut store, "update")?;
let draw = instance.get_typed_func(&mut store, "draw")?;
let on_resize = instance.get_typed_func(&mut store, "on_resize")?;
let on_cursor_event = instance.get_typed_func(&mut store, "on_cursor_event")?;
let on_message = instance.get_typed_func(&mut store, "on_message")?;
@ -45,6 +46,7 @@ impl Backend for WasmtimeBackend {
bind_panel,
update,
draw,
on_resize,
on_cursor_event,
on_message,
};
@ -60,6 +62,7 @@ pub struct WasmtimeInstance {
bind_panel: wasmtime::TypedFunc<(u32, u32), u32>,
update: wasmtime::TypedFunc<(u32, f32), ()>,
draw: wasmtime::TypedFunc<u32, ()>,
on_resize: wasmtime::TypedFunc<(u32, f32, f32), ()>,
on_cursor_event: wasmtime::TypedFunc<(u32, u32, f32, f32), ()>,
on_message: wasmtime::TypedFunc<(u32, u32), ()>,
}
@ -209,6 +212,13 @@ impl Instance for WasmtimeInstance {
cmds
}
fn on_resize(&self, panel_ud: u32, new_size: Vec2) {
let mut store = self.store.lock();
self.on_resize
.call(store.deref_mut(), (panel_ud, new_size.x, new_size.y))
.unwrap();
}
fn on_cursor_event(&self, panel_ud: u32, kind: CursorEventKind, at: Vec2) {
let mut store = self.store.lock();
self.on_cursor_event

View File

@ -67,6 +67,10 @@ impl Panel {
self.instance.draw(self.userdata)
}
pub fn on_resize(&self, new_size: Vec2) {
self.instance.on_resize(self.userdata, new_size);
}
pub fn on_cursor_event(&self, kind: CursorEventKind, at: Vec2) {
self.instance.on_cursor_event(self.userdata, kind, at);
}