egui/example_wasm/src/lib.rs

79 lines
2.9 KiB
Rust
Raw Normal View History

2019-02-09 22:00:07 +00:00
#![deny(warnings)]
2020-05-10 17:04:10 +00:00
#![warn(clippy::all)]
use egui::{label, widgets::Separator, Align, TextStyle, *};
2019-02-09 22:00:07 +00:00
use wasm_bindgen::prelude::*;
2020-04-12 10:07:51 +00:00
// ----------------------------------------------------------------------------
/// This is the entry-point for all the web-assembly.
#[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), JsValue> {
let backend = egui_web::Backend::new(canvas_id, egui_web::RunMode::Continuous)?;
let app = Box::new(MyApp::default());
let runner = egui_web::AppRunner::new(backend, app)?;
egui_web::run(runner)?;
Ok(())
}
// ----------------------------------------------------------------------------
#[derive(Default)]
pub struct MyApp {
example_app: egui::examples::ExampleApp,
2019-02-09 22:00:07 +00:00
}
impl egui_web::App for MyApp {
fn ui(&mut self, ui: &mut egui::Ui, backend: &mut egui_web::Backend, info: &egui_web::WebInfo) {
self.example_app.ui(ui, &info.web_location_hash);
2019-02-09 22:00:07 +00:00
let mut ui = ui.centered_column(ui.available().width().min(480.0));
ui.set_layout(Layout::vertical(Align::Min));
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(
"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| {
ui.label(&backend.painter_debug_info());
2019-02-09 22:00:07 +00:00
});
2020-05-08 20:42:31 +00:00
ui.add(
label!(
"CPU usage: {:.2} ms / frame (excludes painting)",
1e3 * backend.cpu_time()
)
.text_style(TextStyle::Monospace),
2020-04-12 10:07:51 +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)
.tooltip_text("Repaint when there is new input (e.g. mouse movement)");
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 {
ui.label("Only running UI code when there is new input");
}
2019-02-09 22:00:07 +00:00
}
}