Test harness has text box for custom messages
This commit is contained in:
parent
237ef96dfa
commit
43474fe849
|
@ -26,8 +26,9 @@ fn main() {
|
|||
|
||||
struct App {
|
||||
script: canary::WasmtimeScript<canary::ScriptAbiImpl>,
|
||||
panel: canary::PanelId,
|
||||
panels: Vec<canary::PanelId>,
|
||||
last_update: Instant,
|
||||
bind_message_buf: String,
|
||||
}
|
||||
|
||||
impl App {
|
||||
|
@ -35,14 +36,13 @@ impl App {
|
|||
let runtime = canary::WasmtimeRuntime::new().unwrap();
|
||||
let abi = canary::ScriptAbiImpl::default();
|
||||
let module = std::fs::read(module_path).unwrap();
|
||||
let mut script = runtime.load_module(abi, &module).unwrap();
|
||||
let msg = "Hello, script!".to_string().as_bytes().to_vec();
|
||||
let panel = script.bind_panel(msg);
|
||||
let script = runtime.load_module(abi, &module).unwrap();
|
||||
|
||||
Self {
|
||||
script,
|
||||
panel,
|
||||
panels: vec![],
|
||||
last_update: Instant::now(),
|
||||
bind_message_buf: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,75 +51,90 @@ impl eframe::App for App {
|
|||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
ctx.request_repaint();
|
||||
|
||||
egui::SidePanel::left("left_panel").show(ctx, |ui| {
|
||||
let text_edit = egui::TextEdit::multiline(&mut self.bind_message_buf).code_editor();
|
||||
ui.add(text_edit);
|
||||
|
||||
if ui.button("Bind Panel").clicked() {
|
||||
let msg = self.bind_message_buf.as_bytes().to_vec();
|
||||
let panel = self.script.bind_panel(msg);
|
||||
self.panels.push(panel);
|
||||
}
|
||||
});
|
||||
|
||||
let dt = self.last_update.elapsed().as_secs_f32();
|
||||
self.last_update = Instant::now();
|
||||
self.script.update(self.panel, dt);
|
||||
|
||||
egui::Window::new("Panel").show(ctx, |ui| {
|
||||
let size = egui::vec2(800.0, 800.0);
|
||||
let sense = egui::Sense {
|
||||
click: true,
|
||||
drag: true,
|
||||
focusable: true,
|
||||
};
|
||||
for (idx, panel) in self.panels.iter().enumerate() {
|
||||
self.script.update(*panel, dt);
|
||||
|
||||
let (rect, response) = ui.allocate_at_least(size, sense);
|
||||
|
||||
if let Some(hover_pos) = response.hover_pos() {
|
||||
let local = (hover_pos - rect.left_top()) / rect.size();
|
||||
let norm = local * 2.0 - egui::vec2(1.0, 1.0);
|
||||
let x = norm.x;
|
||||
let y = -norm.y;
|
||||
let pos = canary::Vec2 { x, y };
|
||||
|
||||
let kind = if response.drag_started() {
|
||||
CursorEventKind::Select
|
||||
} else if response.drag_released() {
|
||||
CursorEventKind::Deselect
|
||||
} else if response.dragged() {
|
||||
CursorEventKind::Drag
|
||||
} else {
|
||||
CursorEventKind::Hover
|
||||
let window_id = egui::Id::new(format!("panel_{}", idx));
|
||||
egui::Window::new("Panel").id(window_id).show(ctx, |ui| {
|
||||
let size = egui::vec2(800.0, 800.0);
|
||||
let sense = egui::Sense {
|
||||
click: true,
|
||||
drag: true,
|
||||
focusable: true,
|
||||
};
|
||||
|
||||
self.script.on_cursor_event(self.panel, kind, pos);
|
||||
}
|
||||
let (rect, response) = ui.allocate_at_least(size, sense);
|
||||
|
||||
let texture = egui::TextureId::Managed(0);
|
||||
let uv = egui::pos2(0.0, 0.0);
|
||||
let mut mesh = egui::Mesh::with_texture(texture);
|
||||
if let Some(hover_pos) = response.hover_pos() {
|
||||
let local = (hover_pos - rect.left_top()) / rect.size();
|
||||
let norm = local * 2.0 - egui::vec2(1.0, 1.0);
|
||||
let x = norm.x;
|
||||
let y = -norm.y;
|
||||
let pos = canary::Vec2 { x, y };
|
||||
|
||||
self.script.draw(self.panel, |commands| {
|
||||
for command in commands.iter() {
|
||||
let voff = mesh.vertices.len() as u32;
|
||||
let kind = if response.drag_started() {
|
||||
CursorEventKind::Select
|
||||
} else if response.drag_released() {
|
||||
CursorEventKind::Deselect
|
||||
} else if response.dragged() {
|
||||
CursorEventKind::Drag
|
||||
} else {
|
||||
CursorEventKind::Hover
|
||||
};
|
||||
|
||||
match command {
|
||||
canary::DrawCommand::Mesh { vertices, indices } => {
|
||||
for v in vertices.iter() {
|
||||
use egui::epaint::Vertex;
|
||||
let pos = egui::pos2(v.position.x, -v.position.y);
|
||||
let pos = pos.to_vec2() / 2.0 + egui::vec2(0.5, 0.5);
|
||||
let pos = rect.left_top() + pos * rect.size();
|
||||
let (r, g, b, a) = v.color.to_rgba_unmultiplied();
|
||||
let color = egui::Color32::from_rgba_unmultiplied(r, g, b, a);
|
||||
let v = Vertex { pos, uv, color };
|
||||
mesh.vertices.push(v);
|
||||
}
|
||||
|
||||
for i in indices.iter() {
|
||||
mesh.indices.push(i + voff);
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
self.script.on_cursor_event(*panel, kind, pos);
|
||||
}
|
||||
|
||||
let texture = egui::TextureId::Managed(0);
|
||||
let uv = egui::pos2(0.0, 0.0);
|
||||
let mut mesh = egui::Mesh::with_texture(texture);
|
||||
|
||||
self.script.draw(*panel, |commands| {
|
||||
for command in commands.iter() {
|
||||
let voff = mesh.vertices.len() as u32;
|
||||
|
||||
match command {
|
||||
canary::DrawCommand::Mesh { vertices, indices } => {
|
||||
for v in vertices.iter() {
|
||||
use egui::epaint::Vertex;
|
||||
let pos = egui::pos2(v.position.x, -v.position.y);
|
||||
let pos = pos.to_vec2() / 2.0 + egui::vec2(0.5, 0.5);
|
||||
let pos = rect.left_top() + pos * rect.size();
|
||||
let (r, g, b, a) = v.color.to_rgba_unmultiplied();
|
||||
let color = egui::Color32::from_rgba_unmultiplied(r, g, b, a);
|
||||
let v = Vertex { pos, uv, color };
|
||||
mesh.vertices.push(v);
|
||||
}
|
||||
|
||||
for i in indices.iter() {
|
||||
mesh.indices.push(i + voff);
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let painter = ui.painter_at(rect);
|
||||
let shape = egui::Shape::mesh(mesh);
|
||||
painter.add(shape);
|
||||
|
||||
response
|
||||
});
|
||||
|
||||
let painter = ui.painter_at(rect);
|
||||
let shape = egui::Shape::mesh(mesh);
|
||||
painter.add(shape);
|
||||
|
||||
response
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue