2019-02-09 22:00:07 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-08 20:25:28 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
use {
|
2020-04-12 10:07:51 +00:00
|
|
|
emigui::{
|
2020-05-04 19:35:16 +00:00
|
|
|
color::srgba, containers::*, example_app::ExampleWindow, label, widgets::Separator, Align,
|
2020-05-08 20:25:28 +00:00
|
|
|
RawInput, TextStyle, *,
|
2020-04-12 10:07:51 +00:00
|
|
|
},
|
2019-02-11 19:27:32 +00:00
|
|
|
emigui_wasm::now_sec,
|
|
|
|
};
|
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-05-01 00:08:01 +00:00
|
|
|
example_app: ExampleWindow,
|
2020-05-08 20:25:28 +00:00
|
|
|
ctx: Arc<Context>,
|
2019-02-09 22:00:07 +00:00
|
|
|
webgl_painter: emigui_wasm::webgl::Painter,
|
2020-04-21 12:40:46 +00:00
|
|
|
|
2020-05-03 11:28:47 +00:00
|
|
|
frame_times: emigui::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);
|
|
|
|
emigui_wasm::load_memory(&ctx);
|
2019-02-09 22:00:07 +00:00
|
|
|
Ok(State {
|
2020-04-12 10:07:51 +00:00
|
|
|
example_app: Default::default(),
|
2020-05-08 20:25:28 +00:00
|
|
|
ctx,
|
2019-02-09 22:00:07 +00:00
|
|
|
webgl_painter: emigui_wasm::webgl::Painter::new(canvas_id)?,
|
2020-05-03 11:28:47 +00:00
|
|
|
frame_times: emigui::MovementTracker::new(1000, 1.0),
|
2019-02-09 22:00:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
fn run(&mut self, raw_input: RawInput) -> 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-04-28 21:05:22 +00:00
|
|
|
let pixels_per_point = raw_input.pixels_per_point;
|
2020-05-08 20:25:28 +00:00
|
|
|
self.ctx.begin_frame(raw_input);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
let mut region = self.ctx.background_region();
|
2020-04-19 22:48:54 +00:00
|
|
|
let mut region = region.centered_column(region.available_width().min(480.0));
|
2020-04-20 21:32:06 +00:00
|
|
|
region.set_align(Align::Min);
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(label!("Emigui!").text_style(TextStyle::Heading));
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.");
|
|
|
|
region.add_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-04-23 07:50:03 +00:00
|
|
|
region.add_label("This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech.");
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("This is also work in progress, and not ready for production... yet :)");
|
2020-05-01 07:35:44 +00:00
|
|
|
region.horizontal(|region| {
|
2020-04-23 17:15:17 +00:00
|
|
|
region.add_label("Project home page:");
|
|
|
|
region.add_hyperlink("https://github.com/emilk/emigui/");
|
|
|
|
});
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(Separator::new());
|
|
|
|
|
|
|
|
region.set_align(Align::Min);
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("WebGl painter info:");
|
2020-04-24 16:32:27 +00:00
|
|
|
region.indent("webgl region", |region| {
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label(self.webgl_painter.debug_info());
|
2019-02-09 22:00:07 +00:00
|
|
|
});
|
2020-04-21 12:40:46 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.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),
|
|
|
|
);
|
|
|
|
region.add(
|
|
|
|
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-20 21:33:16 +00:00
|
|
|
// TODO: Make it even simpler to show a window
|
|
|
|
|
2020-04-23 07:50:03 +00:00
|
|
|
Window::new("Examples")
|
|
|
|
.default_pos(pos2(32.0, 300.0))
|
|
|
|
.default_size(vec2(300.0, 400.0))
|
|
|
|
.show(region.ctx(), |region| {
|
|
|
|
self.example_app.ui(region);
|
|
|
|
});
|
2020-04-20 21:32:06 +00:00
|
|
|
|
2020-04-23 07:50:03 +00:00
|
|
|
Window::new("Emigui settings")
|
|
|
|
.default_pos(pos2(400.0, 300.0))
|
|
|
|
.default_size(vec2(400.0, 400.0))
|
2020-04-20 21:32:06 +00:00
|
|
|
.show(region.ctx(), |region| {
|
2020-05-08 20:25:28 +00:00
|
|
|
self.ctx.ui(region);
|
2020-04-20 21:32:06 +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-04-28 21:05:22 +00:00
|
|
|
pixels_per_point,
|
2020-04-23 17:15:17 +00:00
|
|
|
)?;
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-05-08 20:25:28 +00:00
|
|
|
emigui_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-04-23 17:15:17 +00:00
|
|
|
pub fn run_gui(state: &mut State, raw_input_json: &str) -> Result<String, JsValue> {
|
2019-02-09 22:00:07 +00:00
|
|
|
// TODO: nicer interface than JSON
|
|
|
|
let raw_input: RawInput = serde_json::from_str(raw_input_json).unwrap();
|
2020-04-23 17:15:17 +00:00
|
|
|
let output = state.run(raw_input)?;
|
|
|
|
Ok(serde_json::to_string(&output).unwrap())
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|