Magpie loads, draws, and updates scripts
This commit is contained in:
parent
a058d027ef
commit
839f8c6c6e
|
@ -96,7 +96,7 @@ impl Client {
|
||||||
while let Some(msg) = self.messenger.recv() {
|
while let Some(msg) = self.messenger.recv() {
|
||||||
println!("Client #{}: {:?}", self.token.0, msg);
|
println!("Client #{}: {:?}", self.token.0, msg);
|
||||||
match msg {
|
match msg {
|
||||||
MagpieServerMsg::CreatePanel(CreatePanel { id }) => {
|
MagpieServerMsg::CreatePanel(CreatePanel { id, script }) => {
|
||||||
let window = self.data.write().new_window_id();
|
let window = self.data.write().new_window_id();
|
||||||
|
|
||||||
if let Some(old_id) = self.id_to_window.insert(id, window) {
|
if let Some(old_id) = self.id_to_window.insert(id, window) {
|
||||||
|
@ -104,7 +104,7 @@ impl Client {
|
||||||
let _ = self.window_sender.send_event(msg);
|
let _ = self.window_sender.send_event(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg = WindowMessage::OpenWindow { id: window };
|
let msg = WindowMessage::OpenWindow { id: window, script };
|
||||||
let _ = self.window_sender.send_event(msg);
|
let _ = self.window_sender.send_event(msg);
|
||||||
}
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use canary::{PanelId, ScriptAbiImpl, ScriptInstance, WasmtimeRuntime, WasmtimeScript};
|
||||||
use glium::backend::glutin::DisplayCreationError;
|
use glium::backend::glutin::DisplayCreationError;
|
||||||
use glium::{glutin, Surface};
|
use glium::{glutin, Surface};
|
||||||
use glutin::event::{Event, WindowEvent};
|
use glutin::event::{Event, WindowEvent};
|
||||||
|
@ -10,7 +13,7 @@ use crate::gl::Graphics;
|
||||||
use crate::ipc::{IpcMessage, IpcMessageSender};
|
use crate::ipc::{IpcMessage, IpcMessageSender};
|
||||||
|
|
||||||
pub enum WindowMessage {
|
pub enum WindowMessage {
|
||||||
OpenWindow { id: usize },
|
OpenWindow { id: usize, script: PathBuf },
|
||||||
CloseWindow { id: usize },
|
CloseWindow { id: usize },
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
@ -19,20 +22,30 @@ pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
|
||||||
|
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
pub graphics: Graphics,
|
pub graphics: Graphics,
|
||||||
|
pub script: WasmtimeScript<ScriptAbiImpl>,
|
||||||
|
pub panel: PanelId,
|
||||||
|
pub last_update: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
script: WasmtimeScript<ScriptAbiImpl>,
|
||||||
|
panel: PanelId,
|
||||||
event_loop: &EventLoopWindowTarget<WindowMessage>,
|
event_loop: &EventLoopWindowTarget<WindowMessage>,
|
||||||
) -> Result<Self, DisplayCreationError> {
|
) -> Result<Self, DisplayCreationError> {
|
||||||
let wb = glutin::window::WindowBuilder::new();
|
let wb = glutin::window::WindowBuilder::new();
|
||||||
let cb = glutin::ContextBuilder::new();
|
let cb = glutin::ContextBuilder::new();
|
||||||
let display = glium::Display::new(wb, cb, &event_loop)?;
|
let display = glium::Display::new(wb, cb, &event_loop)?;
|
||||||
let graphics = Graphics::new(display);
|
let graphics = Graphics::new(display);
|
||||||
|
let last_update = Instant::now();
|
||||||
Ok(Self { graphics })
|
Ok(Self {
|
||||||
|
graphics,
|
||||||
|
script,
|
||||||
|
panel,
|
||||||
|
last_update,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_id(&self) -> WindowId {
|
pub fn get_id(&self) -> WindowId {
|
||||||
self.graphics.display.gl_window().window().id()
|
self.graphics.display.gl_window().window().id()
|
||||||
}
|
}
|
||||||
|
@ -40,12 +53,25 @@ impl Window {
|
||||||
pub fn request_redraw(&mut self) {
|
pub fn request_redraw(&mut self) {
|
||||||
self.graphics.display.gl_window().window().request_redraw();
|
self.graphics.display.gl_window().window().request_redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
let now = Instant::now();
|
||||||
|
let dt = now.duration_since(self.last_update).as_secs_f32();
|
||||||
|
self.script.update(self.panel, dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&mut self) {
|
||||||
|
self.script.draw(self.panel, |commands| {
|
||||||
|
self.graphics.draw(commands);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WindowStore {
|
pub struct WindowStore {
|
||||||
pub ipc_sender: IpcMessageSender,
|
pub ipc_sender: IpcMessageSender,
|
||||||
pub ipc_to_window: HashMap<usize, WindowId>,
|
pub ipc_to_window: HashMap<usize, WindowId>,
|
||||||
pub windows: HashMap<WindowId, Window>,
|
pub windows: HashMap<WindowId, Window>,
|
||||||
|
pub runtime: WasmtimeRuntime,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowStore {
|
impl WindowStore {
|
||||||
|
@ -54,6 +80,7 @@ impl WindowStore {
|
||||||
ipc_sender,
|
ipc_sender,
|
||||||
ipc_to_window: Default::default(),
|
ipc_to_window: Default::default(),
|
||||||
windows: Default::default(),
|
windows: Default::default(),
|
||||||
|
runtime: WasmtimeRuntime::new().unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,13 +96,23 @@ impl WindowStore {
|
||||||
}
|
}
|
||||||
Event::RedrawRequested(id) => {
|
Event::RedrawRequested(id) => {
|
||||||
if let Some(window) = self.windows.get_mut(&id) {
|
if let Some(window) = self.windows.get_mut(&id) {
|
||||||
window.graphics.draw(&[]);
|
window.draw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::MainEventsCleared => {
|
||||||
|
for (_id, window) in self.windows.iter_mut() {
|
||||||
|
window.update();
|
||||||
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::UserEvent(event) => match event {
|
Event::UserEvent(event) => match event {
|
||||||
WindowMessage::OpenWindow { id } => {
|
WindowMessage::OpenWindow { id, script } => {
|
||||||
println!("Opening window {}", id);
|
println!("Opening window {} with script {:?}", id, script);
|
||||||
let window = Window::new(&event_loop).unwrap();
|
let abi = Default::default();
|
||||||
|
let module = std::fs::read(script).unwrap();
|
||||||
|
let mut script = self.runtime.load_module(abi, &module).unwrap();
|
||||||
|
let panel = script.bind_panel(vec![]);
|
||||||
|
let window = Window::new(script, panel, &event_loop).unwrap();
|
||||||
let window_id = window.get_id();
|
let window_id = window.get_id();
|
||||||
self.windows.insert(window_id, window);
|
self.windows.insert(window_id, window);
|
||||||
self.ipc_to_window.insert(id, window_id);
|
self.ipc_to_window.insert(id, window_id);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
|
|
||||||
|
@ -17,6 +18,7 @@ pub type PanelId = u32;
|
||||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct CreatePanel {
|
pub struct CreatePanel {
|
||||||
pub id: PanelId,
|
pub id: PanelId,
|
||||||
|
pub script: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a panel a message.
|
/// Sends a panel a message.
|
||||||
|
|
Loading…
Reference in New Issue