2019-02-09 22:00:07 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-10 17:04:10 +00:00
|
|
|
#![warn(clippy::all)]
|
|
|
|
|
2020-07-18 22:44:06 +00:00
|
|
|
use egui::{label, Align, Layout, TextStyle};
|
2019-02-09 22:00:07 +00:00
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
2020-04-12 10:07:51 +00:00
|
|
|
|
2020-07-18 16:00:05 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
/// This is the entry-point for all the web-assembly.
|
|
|
|
#[wasm_bindgen]
|
2020-07-18 22:44:06 +00:00
|
|
|
pub fn start(canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
|
2020-07-18 22:16:14 +00:00
|
|
|
let backend = egui_web::Backend::new(canvas_id, egui_web::RunMode::Reactive)?;
|
2020-07-18 16:35:17 +00:00
|
|
|
let app = Box::new(MyApp::default());
|
2020-07-18 17:40:24 +00:00
|
|
|
let runner = egui_web::AppRunner::new(backend, app)?;
|
2020-07-18 16:35:17 +00:00
|
|
|
egui_web::run(runner)?;
|
|
|
|
Ok(())
|
2020-07-18 16:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MyApp {
|
|
|
|
example_app: egui::examples::ExampleApp,
|
2020-07-18 22:44:06 +00:00
|
|
|
frames_painted: u64,
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
impl egui_web::App for MyApp {
|
2020-07-18 17:40:24 +00:00
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, backend: &mut egui_web::Backend, info: &egui_web::WebInfo) {
|
2020-07-18 16:35:17 +00:00
|
|
|
self.example_app.ui(ui, &info.web_location_hash);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
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-07-18 22:44:06 +00:00
|
|
|
ui.separator();
|
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 17:40:24 +00:00
|
|
|
ui.label(&backend.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-07-18 17:40:24 +00:00
|
|
|
"CPU usage: {:.2} ms / frame (excludes painting)",
|
|
|
|
1e3 * backend.cpu_time()
|
2020-04-29 05:20:27 +00:00
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
2020-04-12 10:07:51 +00:00
|
|
|
);
|
2020-07-18 17:40:24 +00:00
|
|
|
|
2020-07-18 22:44:06 +00:00
|
|
|
ui.separator();
|
|
|
|
|
2020-07-18 17:40:24 +00:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
let mut run_mode = backend.run_mode();
|
|
|
|
ui.label("Run mode:");
|
|
|
|
ui.radio_value("Continuous", &mut run_mode, egui_web::RunMode::Continuous)
|
|
|
|
.tooltip_text("Repaint everything each frame");
|
|
|
|
ui.radio_value("Reactive", &mut run_mode, egui_web::RunMode::Reactive)
|
2020-07-18 22:44:06 +00:00
|
|
|
.tooltip_text("Repaint when there are animations or input (e.g. mouse movement)");
|
2020-07-18 17:40:24 +00:00
|
|
|
backend.set_run_mode(run_mode);
|
|
|
|
});
|
|
|
|
|
|
|
|
if backend.run_mode() == egui_web::RunMode::Continuous {
|
|
|
|
ui.add(
|
|
|
|
label!("Repainting the UI each frame. FPS: {:.1}", backend.fps())
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
|
|
|
} else {
|
2020-07-18 22:44:06 +00:00
|
|
|
ui.label("Only running UI code when there are animations or input");
|
2020-07-18 17:40:24 +00:00
|
|
|
}
|
2020-07-18 22:44:06 +00:00
|
|
|
|
|
|
|
self.frames_painted += 1;
|
|
|
|
ui.label(format!("Total frames painted: {}", self.frames_painted));
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
}
|