canary-rs/crates/egui/src/main.rs

126 lines
4.1 KiB
Rust
Raw Normal View History

2022-07-26 00:27:18 +00:00
use canary::{CursorEventKind, ScriptInstance};
2022-07-15 21:11:35 +00:00
use eframe::egui;
use std::time::Instant;
fn main() {
let args: Vec<String> = std::env::args().collect();
let module_path = args
.get(1)
.expect("Please pass a path to a Canary script!")
.to_owned();
let native_options = eframe::NativeOptions {
multisampling: 8,
..Default::default()
};
eframe::run_native(
"Canary egui Harness",
native_options,
Box::new(move |cc| {
cc.egui_ctx.set_visuals(egui::Visuals::dark());
Box::new(App::new(&module_path))
}),
);
}
struct App {
script: canary::WasmtimeScript<canary::ScriptAbiImpl>,
2022-07-31 06:15:04 +00:00
panel: canary::PanelId,
2022-07-15 21:11:35 +00:00
last_update: Instant,
}
impl App {
pub fn new(module_path: &str) -> Self {
let runtime = canary::WasmtimeRuntime::new().unwrap();
let abi = canary::ScriptAbiImpl::default();
2022-07-15 21:11:35 +00:00
let module = std::fs::read(module_path).unwrap();
2022-07-31 06:15:04 +00:00
let mut script = runtime.load_module(abi, &module).unwrap();
2022-08-17 00:11:56 +00:00
let msg = "Hello, script!".to_string().as_bytes().to_vec();
let panel = script.bind_panel(msg);
2022-07-15 21:11:35 +00:00
Self {
script,
2022-07-31 06:15:04 +00:00
panel,
2022-07-15 21:11:35 +00:00
last_update: Instant::now(),
}
}
}
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
ctx.request_repaint();
let dt = self.last_update.elapsed().as_secs_f32();
self.last_update = Instant::now();
2022-07-31 06:15:04 +00:00
self.script.update(self.panel, dt);
2022-07-15 21:11:35 +00:00
egui::Window::new("Panel").show(ctx, |ui| {
let size = egui::vec2(800.0, 800.0);
let sense = egui::Sense {
click: true,
drag: true,
2022-07-26 00:27:18 +00:00
focusable: true,
2022-07-15 21:11:35 +00:00
};
let (rect, response) = ui.allocate_at_least(size, sense);
2022-07-26 00:27:18 +00:00
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
2022-07-15 21:11:35 +00:00
};
2022-07-31 06:15:04 +00:00
self.script.on_cursor_event(self.panel, kind, pos);
2022-07-26 00:27:18 +00:00
}
2022-07-15 21:11:35 +00:00
let texture = egui::TextureId::Managed(0);
let uv = egui::pos2(0.0, 0.0);
let mut mesh = egui::Mesh::with_texture(texture);
2022-07-31 06:15:04 +00:00
self.script.draw(self.panel, |commands| {
2022-07-15 21:11:35 +00:00
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
});
}
}