2019-02-09 22:00:07 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-10 17:04:10 +00:00
|
|
|
#![warn(clippy::all)]
|
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
use {
|
2020-05-30 08:22:35 +00:00
|
|
|
egui::{
|
2020-05-11 18:21:24 +00:00
|
|
|
color::srgba, examples::ExampleApp, label, widgets::Separator, Align, RawInput, TextStyle,
|
|
|
|
*,
|
2020-04-12 10:07:51 +00:00
|
|
|
},
|
2020-05-30 08:22:35 +00:00
|
|
|
egui_wasm::now_sec,
|
2019-02-11 19:27:32 +00:00
|
|
|
};
|
2019-02-09 22:00:07 +00:00
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
2020-04-12 10:07:51 +00:00
|
|
|
|
2020-05-21 10:04:14 +00:00
|
|
|
#[derive(Clone, Debug, Default, serde_derive::Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct WebInput {
|
2020-05-30 08:22:35 +00:00
|
|
|
egui: RawInput,
|
2020-05-21 10:04:14 +00:00
|
|
|
web: Web,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, serde_derive::Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct Web {
|
|
|
|
pub location: String,
|
|
|
|
/// i.e. "#fragment"
|
|
|
|
pub location_hash: String,
|
|
|
|
}
|
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
#[wasm_bindgen]
|
2019-02-09 22:00:07 +00:00
|
|
|
pub struct State {
|
2020-05-12 05:27:14 +00:00
|
|
|
example_app: ExampleApp,
|
2020-05-08 20:25:28 +00:00
|
|
|
ctx: Arc<Context>,
|
2020-05-30 08:22:35 +00:00
|
|
|
webgl_painter: egui_wasm::webgl::Painter,
|
2020-04-21 12:40:46 +00:00
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
frame_times: egui::MovementTracker<f32>,
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
fn new(canvas_id: &str, pixels_per_point: f32) -> Result<State, JsValue> {
|
2020-05-08 20:25:28 +00:00
|
|
|
let ctx = Context::new(pixels_per_point);
|
2020-05-30 08:22:35 +00:00
|
|
|
egui_wasm::load_memory(&ctx);
|
2019-02-09 22:00:07 +00:00
|
|
|
Ok(State {
|
2020-05-12 05:27:14 +00:00
|
|
|
example_app: Default::default(),
|
2020-05-08 20:25:28 +00:00
|
|
|
ctx,
|
2020-05-30 08:22:35 +00:00
|
|
|
webgl_painter: egui_wasm::webgl::Painter::new(canvas_id)?,
|
|
|
|
frame_times: egui::MovementTracker::new(1000, 1.0),
|
2019-02-09 22:00:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-21 10:04:14 +00:00
|
|
|
fn run(&mut self, web_input: WebInput) -> Result<Output, JsValue> {
|
2019-02-11 19:27:32 +00:00
|
|
|
let everything_start = now_sec();
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
self.ctx.begin_frame(web_input.egui);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
let mut ui = self.ctx.fullscreen_ui();
|
2020-05-21 10:04:14 +00:00
|
|
|
self.example_app.ui(&mut ui, &web_input.web.location_hash);
|
2020-05-12 16:21:09 +00:00
|
|
|
let mut ui = ui.centered_column(ui.available().width().min(480.0));
|
2020-05-13 19:36:15 +00:00
|
|
|
ui.set_layout(Layout::vertical(Align::Min));
|
2020-05-30 08:22:35 +00:00
|
|
|
ui.add(label!("Egui!").text_style(TextStyle::Heading));
|
|
|
|
ui.label("Egui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.");
|
2020-05-30 07:51:57 +00:00
|
|
|
ui.label(
|
2020-04-20 21:32:06 +00:00
|
|
|
"Everything you see is rendered as textured triangles. There is no DOM. There are no HTML elements."
|
2020-04-20 21:33:16 +00:00
|
|
|
);
|
2020-05-30 07:51:57 +00:00
|
|
|
ui.label("This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech.");
|
|
|
|
ui.label("This is also work in progress, and not ready for production... yet :)");
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.horizontal(|ui| {
|
2020-05-30 07:51:57 +00:00
|
|
|
ui.label("Project home page:");
|
|
|
|
ui.hyperlink("https://github.com/emilk/emigui/");
|
2020-04-23 17:15:17 +00:00
|
|
|
});
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.add(Separator::new());
|
2020-04-12 10:07:51 +00:00
|
|
|
|
2020-05-30 07:51:57 +00:00
|
|
|
ui.label("WebGl painter info:");
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.indent("webgl region id", |ui| {
|
2020-05-30 07:51:57 +00:00
|
|
|
ui.label(self.webgl_painter.debug_info());
|
2019-02-09 22:00:07 +00:00
|
|
|
});
|
2020-04-21 12:40:46 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.add(
|
2020-04-29 05:20:27 +00:00
|
|
|
label!(
|
2020-05-03 11:28:47 +00:00
|
|
|
"CPU usage: {:.2} ms (excludes painting)",
|
|
|
|
1e3 * self.frame_times.average().unwrap_or_default()
|
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.add(
|
2020-05-03 11:28:47 +00:00
|
|
|
label!(
|
|
|
|
"FPS: {:.1}",
|
|
|
|
1.0 / self.frame_times.mean_time_interval().unwrap_or_default()
|
2020-04-29 05:20:27 +00:00
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
2020-04-12 10:07:51 +00:00
|
|
|
);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-04-23 20:08:42 +00:00
|
|
|
let bg_color = srgba(0, 0, 0, 0); // Use background css color.
|
2020-05-08 20:25:28 +00:00
|
|
|
let (output, batches) = self.ctx.end_frame();
|
2020-04-29 05:20:27 +00:00
|
|
|
|
2020-05-03 11:28:47 +00:00
|
|
|
let now = now_sec();
|
|
|
|
self.frame_times.add(now, (now - everything_start) as f32);
|
2020-04-29 05:20:27 +00:00
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
self.webgl_painter.paint_batches(
|
2019-03-16 12:37:29 +00:00
|
|
|
bg_color,
|
2020-04-20 21:32:06 +00:00
|
|
|
batches,
|
2020-05-08 20:25:28 +00:00
|
|
|
self.ctx.texture(),
|
2020-05-20 19:19:42 +00:00
|
|
|
self.ctx.pixels_per_point(),
|
2020-04-23 17:15:17 +00:00
|
|
|
)?;
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
egui_wasm::save_memory(&self.ctx); // TODO: don't save every frame
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
Ok(output)
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn new_webgl_gui(canvas_id: &str, pixels_per_point: f32) -> Result<State, JsValue> {
|
|
|
|
State::new(canvas_id, pixels_per_point)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2020-05-21 10:04:14 +00:00
|
|
|
pub fn run_gui(state: &mut State, web_input_json: &str) -> Result<String, JsValue> {
|
2019-02-09 22:00:07 +00:00
|
|
|
// TODO: nicer interface than JSON
|
2020-05-21 10:04:14 +00:00
|
|
|
let web_input: WebInput = serde_json::from_str(web_input_json).unwrap();
|
|
|
|
let output = state.run(web_input)?;
|
2020-04-23 17:15:17 +00:00
|
|
|
Ok(serde_json::to_string(&output).unwrap())
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|