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},
|
2020-04-20 21:32:06 +00:00
|
|
|
Align, Emigui, RawInput, TextStyle, Window, *,
|
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::*;
|
|
|
|
#[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,
|
2020-04-21 12:40:46 +00:00
|
|
|
|
|
|
|
frame_times: std::collections::VecDeque<f64>,
|
2019-02-09 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)?,
|
2020-04-21 12:40:46 +00:00
|
|
|
frame_times: Default::default(),
|
2019-02-09 22:00:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
);
|
|
|
|
region.add_label("This not JavaScript. This is Rust code, running at 60 Hz. This is the web page, reinvented with game tech.");
|
|
|
|
region.add_label("This is also work in progress, and not ready for production... yet :)");
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(Separator::new());
|
|
|
|
self.example_app.ui(&mut region);
|
|
|
|
self.emigui.ui(&mut region);
|
|
|
|
|
|
|
|
region.set_align(Align::Min);
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("WebGl painter info:");
|
2020-04-21 18:48:31 +00:00
|
|
|
region.indent(Id::new("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
|
|
|
|
|
|
|
let mean_frame_time = if self.frame_times.is_empty() {
|
|
|
|
0.0
|
|
|
|
} else {
|
|
|
|
self.frame_times.iter().sum::<f64>() / (self.frame_times.len() as f64)
|
|
|
|
};
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(
|
2020-04-21 12:40:46 +00:00
|
|
|
label!("Total CPU usage: {:.1} ms", 1e3 * mean_frame_time)
|
|
|
|
.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-20 21:32:06 +00:00
|
|
|
Window::new("Test window").show(region.ctx(), |region| {
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("Grab the window and move it around!");
|
2020-04-20 21:32:06 +00:00
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("This window can be reisized, but not smaller than the contents.");
|
2020-04-20 21:32:06 +00:00
|
|
|
});
|
|
|
|
Window::new("Another test window")
|
|
|
|
.default_pos(pos2(400.0, 100.0))
|
|
|
|
.show(region.ctx(), |region| {
|
2020-04-20 21:33:16 +00:00
|
|
|
region.add_label("This might be on top of the other window?");
|
|
|
|
region.add_label("Second line of text");
|
2020-04-20 21:32:06 +00:00
|
|
|
});
|
|
|
|
|
2019-03-16 12:37:29 +00:00
|
|
|
let bg_color = srgba(16, 16, 16, 255);
|
2020-04-20 21:32:06 +00:00
|
|
|
let batches = self.emigui.paint();
|
|
|
|
let result = self.webgl_painter.paint_batches(
|
2019-03-16 12:37:29 +00:00
|
|
|
bg_color,
|
2020-04-20 21:32:06 +00:00
|
|
|
batches,
|
2019-03-16 12:37:29 +00:00
|
|
|
self.emigui.texture(),
|
|
|
|
raw_input.pixels_per_point,
|
|
|
|
);
|
2019-02-09 22:00:07 +00:00
|
|
|
|
2020-04-21 12:40:46 +00:00
|
|
|
self.frame_times.push_back(now_sec() - everything_start);
|
|
|
|
while self.frame_times.len() > 30 {
|
|
|
|
self.frame_times.pop_front();
|
|
|
|
}
|
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)
|
|
|
|
}
|