Acord/viewport/src/lib.rs

130 lines
2.7 KiB
Rust

use std::ffi::{c_char, c_void};
mod bridge;
mod editor;
mod handle;
pub use swiftly_core::*;
use editor::EditorState;
use iced_graphics::Viewport;
use iced_runtime::user_interface;
use iced_wgpu::core::Event;
#[allow(dead_code)]
pub struct ViewportHandle {
surface: wgpu::Surface<'static>,
device: wgpu::Device,
queue: wgpu::Queue,
format: wgpu::TextureFormat,
width: u32,
height: u32,
scale: f32,
renderer: iced_wgpu::Renderer,
viewport: Viewport,
cache: user_interface::Cache,
state: EditorState,
events: Vec<Event>,
cursor: iced_wgpu::core::mouse::Cursor,
}
#[no_mangle]
pub extern "C" fn viewport_create(
nsview: *mut c_void,
width: f32,
height: f32,
scale: f32,
) -> *mut ViewportHandle {
if nsview.is_null() {
return std::ptr::null_mut();
}
match handle::create(nsview, width, height, scale) {
Some(h) => Box::into_raw(Box::new(h)),
None => std::ptr::null_mut(),
}
}
#[no_mangle]
pub extern "C" fn viewport_destroy(handle: *mut ViewportHandle) {
if handle.is_null() {
return;
}
unsafe {
drop(Box::from_raw(handle));
}
}
#[no_mangle]
pub extern "C" fn viewport_render(handle: *mut ViewportHandle) {
let h = match unsafe { handle.as_mut() } {
Some(h) => h,
None => return,
};
handle::render(h);
}
#[no_mangle]
pub extern "C" fn viewport_resize(
handle: *mut ViewportHandle,
width: f32,
height: f32,
scale: f32,
) {
let h = match unsafe { handle.as_mut() } {
Some(h) => h,
None => return,
};
handle::resize(h, width, height, scale);
}
#[no_mangle]
pub extern "C" fn viewport_mouse_event(
handle: *mut ViewportHandle,
x: f32,
y: f32,
button: u8,
pressed: bool,
) {
let h = match unsafe { handle.as_mut() } {
Some(h) => h,
None => return,
};
bridge::push_mouse_event(h, x, y, button, pressed);
}
#[no_mangle]
pub extern "C" fn viewport_key_event(
handle: *mut ViewportHandle,
key: u32,
modifiers: u32,
pressed: bool,
text: *const c_char,
) {
let h = match unsafe { handle.as_mut() } {
Some(h) => h,
None => return,
};
let text_str = if text.is_null() {
None
} else {
Some(unsafe { std::ffi::CStr::from_ptr(text) }.to_string_lossy())
};
bridge::push_key_event(h, key, modifiers, pressed, text_str.as_deref());
}
#[no_mangle]
pub extern "C" fn viewport_scroll_event(
handle: *mut ViewportHandle,
x: f32,
y: f32,
delta_x: f32,
delta_y: f32,
) {
let h = match unsafe { handle.as_mut() } {
Some(h) => h,
None => return,
};
bridge::push_scroll_event(h, x, y, delta_x, delta_y);
}