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() {
|
||||
println!("Client #{}: {:?}", self.token.0, msg);
|
||||
match msg {
|
||||
MagpieServerMsg::CreatePanel(CreatePanel { id }) => {
|
||||
MagpieServerMsg::CreatePanel(CreatePanel { id, script }) => {
|
||||
let window = self.data.write().new_window_id();
|
||||
|
||||
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 msg = WindowMessage::OpenWindow { id: window };
|
||||
let msg = WindowMessage::OpenWindow { id: window, script };
|
||||
let _ = self.window_sender.send_event(msg);
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
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::{glutin, Surface};
|
||||
use glutin::event::{Event, WindowEvent};
|
||||
|
@ -10,7 +13,7 @@ use crate::gl::Graphics;
|
|||
use crate::ipc::{IpcMessage, IpcMessageSender};
|
||||
|
||||
pub enum WindowMessage {
|
||||
OpenWindow { id: usize },
|
||||
OpenWindow { id: usize, script: PathBuf },
|
||||
CloseWindow { id: usize },
|
||||
Quit,
|
||||
}
|
||||
|
@ -19,20 +22,30 @@ pub type WindowMessageSender = EventLoopProxy<WindowMessage>;
|
|||
|
||||
pub struct Window {
|
||||
pub graphics: Graphics,
|
||||
pub script: WasmtimeScript<ScriptAbiImpl>,
|
||||
pub panel: PanelId,
|
||||
pub last_update: Instant,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub fn new(
|
||||
script: WasmtimeScript<ScriptAbiImpl>,
|
||||
panel: PanelId,
|
||||
event_loop: &EventLoopWindowTarget<WindowMessage>,
|
||||
) -> Result<Self, DisplayCreationError> {
|
||||
let wb = glutin::window::WindowBuilder::new();
|
||||
let cb = glutin::ContextBuilder::new();
|
||||
let display = glium::Display::new(wb, cb, &event_loop)?;
|
||||
let graphics = Graphics::new(display);
|
||||
|
||||
Ok(Self { graphics })
|
||||
let last_update = Instant::now();
|
||||
Ok(Self {
|
||||
graphics,
|
||||
script,
|
||||
panel,
|
||||
last_update,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
pub fn get_id(&self) -> WindowId {
|
||||
self.graphics.display.gl_window().window().id()
|
||||
}
|
||||
|
@ -40,12 +53,25 @@ impl Window {
|
|||
pub fn request_redraw(&mut self) {
|
||||
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 ipc_sender: IpcMessageSender,
|
||||
pub ipc_to_window: HashMap<usize, WindowId>,
|
||||
pub windows: HashMap<WindowId, Window>,
|
||||
pub runtime: WasmtimeRuntime,
|
||||
}
|
||||
|
||||
impl WindowStore {
|
||||
|
@ -54,6 +80,7 @@ impl WindowStore {
|
|||
ipc_sender,
|
||||
ipc_to_window: Default::default(),
|
||||
windows: Default::default(),
|
||||
runtime: WasmtimeRuntime::new().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,13 +96,23 @@ impl WindowStore {
|
|||
}
|
||||
Event::RedrawRequested(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 {
|
||||
WindowMessage::OpenWindow { id } => {
|
||||
println!("Opening window {}", id);
|
||||
let window = Window::new(&event_loop).unwrap();
|
||||
WindowMessage::OpenWindow { id, script } => {
|
||||
println!("Opening window {} with script {:?}", id, script);
|
||||
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();
|
||||
self.windows.insert(window_id, window);
|
||||
self.ipc_to_window.insert(id, window_id);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::collections::VecDeque;
|
||||
use std::io::{Read, Write};
|
||||
use std::marker::PhantomData;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
|
||||
|
@ -17,6 +18,7 @@ pub type PanelId = u32;
|
|||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct CreatePanel {
|
||||
pub id: PanelId,
|
||||
pub script: PathBuf,
|
||||
}
|
||||
|
||||
/// Sends a panel a message.
|
||||
|
|
Loading…
Reference in New Issue