2019-02-09 22:00:07 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-10 17:04:10 +00:00
|
|
|
#![warn(clippy::all)]
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
use egui::{examples::ExampleApp, label, widgets::Separator, Align, RawInput, TextStyle, *};
|
2019-02-09 22:00:07 +00:00
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
2020-04-12 10:07:51 +00:00
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
#[wasm_bindgen]
|
2019-02-09 22:00:07 +00:00
|
|
|
pub struct State {
|
2020-07-18 08:54:31 +00:00
|
|
|
egui_web: egui_web::State,
|
2020-05-12 05:27:14 +00:00
|
|
|
example_app: ExampleApp,
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
2020-05-30 12:56:38 +00:00
|
|
|
fn new(canvas_id: &str) -> Result<State, JsValue> {
|
2019-02-09 22:00:07 +00:00
|
|
|
Ok(State {
|
2020-07-18 08:54:31 +00:00
|
|
|
egui_web: egui_web::State::new(canvas_id)?,
|
2020-05-12 05:27:14 +00:00
|
|
|
example_app: Default::default(),
|
2019-02-09 22:00:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
fn run(&mut self, raw_input: RawInput, web_location_hash: &str) -> Result<Output, JsValue> {
|
|
|
|
let mut ui = self.egui_web.begin_frame(raw_input);
|
|
|
|
self.ui(&mut ui, web_location_hash);
|
|
|
|
self.egui_web.end_frame()
|
|
|
|
}
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, web_location_hash: &str) {
|
|
|
|
self.example_app.ui(ui, 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-07-18 08:54:31 +00:00
|
|
|
ui.label(self.egui_web.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)",
|
2020-07-18 08:54:31 +00:00
|
|
|
1e3 * self.egui_web.cpu_usage()
|
2020-04-29 05:20:27 +00:00
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
2020-04-12 10:07:51 +00:00
|
|
|
);
|
2020-07-18 08:54:31 +00:00
|
|
|
ui.add(label!("FPS: {:.1}", self.egui_web.fps()).text_style(TextStyle::Monospace));
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2020-05-30 12:56:38 +00:00
|
|
|
pub fn new_webgl_gui(canvas_id: &str) -> Result<State, JsValue> {
|
|
|
|
State::new(canvas_id)
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2020-07-18 08:54:31 +00:00
|
|
|
pub fn resize_to_screen_size(canvas_id: &str) {
|
|
|
|
egui_web::resize_to_screen_size(canvas_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn run_gui(state: &mut State, web_input_json: &str) -> Result<(), JsValue> {
|
2019-02-09 22:00:07 +00:00
|
|
|
// TODO: nicer interface than JSON
|
2020-07-18 08:54:31 +00:00
|
|
|
let raw_input: RawInput = serde_json::from_str(web_input_json).unwrap();
|
|
|
|
let web_location_hash = egui_web::location_hash().unwrap_or_default();
|
|
|
|
let output = state.run(raw_input, &web_location_hash)?;
|
|
|
|
egui_web::handle_output(&output);
|
|
|
|
Ok(())
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|