2019-02-09 22:00:07 +00:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate wasm_bindgen;
|
|
|
|
|
|
|
|
extern crate emigui;
|
|
|
|
extern crate emigui_wasm;
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
use {
|
2020-04-12 10:07:51 +00:00
|
|
|
emigui::{
|
|
|
|
color::srgba,
|
|
|
|
example_app::ExampleApp,
|
|
|
|
label,
|
|
|
|
widgets::{Label, Separator},
|
|
|
|
Align, Emigui, RawInput, TextStyle,
|
|
|
|
},
|
2019-02-11 19:27:32 +00:00
|
|
|
emigui_wasm::now_sec,
|
|
|
|
};
|
2019-02-09 22:00:07 +00:00
|
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[wasm_bindgen]
|
2020-04-12 10:07:51 +00:00
|
|
|
|
2019-02-09 22:00:07 +00:00
|
|
|
pub struct State {
|
2020-04-12 10:07:51 +00:00
|
|
|
example_app: ExampleApp,
|
2019-02-09 22:00:07 +00:00
|
|
|
emigui: Emigui,
|
|
|
|
webgl_painter: emigui_wasm::webgl::Painter,
|
|
|
|
everything_ms: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
fn new(canvas_id: &str, pixels_per_point: f32) -> Result<State, JsValue> {
|
|
|
|
Ok(State {
|
2020-04-12 10:07:51 +00:00
|
|
|
example_app: Default::default(),
|
2019-02-09 22:00:07 +00:00
|
|
|
emigui: Emigui::new(pixels_per_point),
|
|
|
|
webgl_painter: emigui_wasm::webgl::Painter::new(canvas_id)?,
|
|
|
|
everything_ms: 0.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(&mut self, raw_input: RawInput) -> Result<(), JsValue> {
|
2019-02-11 19:27:32 +00:00
|
|
|
let everything_start = now_sec();
|
2019-02-09 22:00:07 +00:00
|
|
|
|
|
|
|
self.emigui.new_frame(raw_input);
|
|
|
|
|
2020-04-19 09:13:24 +00:00
|
|
|
let mut region = self.emigui.background_region();
|
2020-04-19 22:48:54 +00:00
|
|
|
let mut region = region.centered_column(region.available_width().min(480.0));
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(label!("Emigui!").text_style(TextStyle::Heading));
|
|
|
|
region.add(label!("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."));
|
|
|
|
region.add(label!(
|
|
|
|
"Everything you see is rendered as textured triangles. There is no DOM. There are not HTML elements."
|
|
|
|
));
|
|
|
|
region.add(Separator::new());
|
|
|
|
self.example_app.ui(&mut region);
|
|
|
|
self.emigui.ui(&mut region);
|
|
|
|
|
|
|
|
region.set_align(Align::Min);
|
2019-02-09 22:00:07 +00:00
|
|
|
region.add(label!("WebGl painter info:"));
|
|
|
|
region.indent(|region| {
|
|
|
|
region.add(label!(self.webgl_painter.debug_info()));
|
|
|
|
});
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(
|
|
|
|
label!("Everything: {:.1} ms", self.everything_ms).text_style(TextStyle::Monospace),
|
|
|
|
);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2019-03-16 12:37:29 +00:00
|
|
|
let bg_color = srgba(16, 16, 16, 255);
|
2019-03-10 20:00:28 +00:00
|
|
|
let mesh = self.emigui.paint();
|
2019-03-16 12:37:29 +00:00
|
|
|
let result = self.webgl_painter.paint(
|
|
|
|
bg_color,
|
|
|
|
mesh,
|
|
|
|
self.emigui.texture(),
|
|
|
|
raw_input.pixels_per_point,
|
|
|
|
);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
self.everything_ms = 1000.0 * (now_sec() - everything_start);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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]
|
|
|
|
pub fn run_gui(state: &mut State, raw_input_json: &str) -> Result<(), JsValue> {
|
|
|
|
// TODO: nicer interface than JSON
|
|
|
|
let raw_input: RawInput = serde_json::from_str(raw_input_json).unwrap();
|
|
|
|
state.run(raw_input)
|
|
|
|
}
|